p4raw-id: //depot/perlext/Thread@59
--- /dev/null
+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;
--- /dev/null
+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;