Add Thread modules Queue.pm and Semaphore.pm
Malcolm Beattie [Tue, 9 Sep 1997 16:49:08 +0000 (16:49 +0000)]
p4raw-id: //depot/perlext/Thread@59

Queue.pm [new file with mode: 0644]
Semaphore.pm [new file with mode: 0644]

diff --git a/Queue.pm b/Queue.pm
new file mode 100644 (file)
index 0000000..4eef978
--- /dev/null
+++ b/Queue.pm
@@ -0,0 +1,22 @@
+package Thread::Queue;
+use Thread qw(cond_wait cond_broadcast);
+
+sub new {
+    my $class = shift;
+    return bless [@_], $class;
+}
+
+sub dequeue {
+    use attrs qw(locked method);
+    my $q = shift;
+    cond_wait $q until @$q;
+    return shift @$q;
+}
+
+sub enqueue {
+    use attrs qw(locked method);
+    my $q = shift;
+    push(@$q, @_) and cond_broadcast $q;
+}
+
+1;
diff --git a/Semaphore.pm b/Semaphore.pm
new file mode 100644 (file)
index 0000000..d34d6bd
--- /dev/null
@@ -0,0 +1,23 @@
+package Thread::Semaphore;
+use Thread qw(cond_wait cond_broadcast);
+
+sub new {
+    my $class = shift;
+    my $val = @_ ? shift : 1;
+    bless \$val, $class;
+}
+
+sub down {
+    use attrs qw(locked method);
+    my $s = shift;
+    cond_wait $s until $$s > 0;
+    $$s--;
+}
+
+sub up {
+    use attrs qw(locked method);
+    my $s = shift;
+    $$s++ > 0 and cond_broadcast $s;
+}
+
+1;