From: Malcolm Beattie Date: Tue, 9 Sep 1997 16:49:08 +0000 (+0000) Subject: Add Thread modules Queue.pm and Semaphore.pm X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=d21067e0e6e271ee3d35a64e45d24ef18d4be947;p=p5sagit%2Fp5-mst-13.2.git Add Thread modules Queue.pm and Semaphore.pm p4raw-id: //depot/perlext/Thread@59 --- diff --git a/Queue.pm b/Queue.pm new file mode 100644 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 index 0000000..d34d6bd --- /dev/null +++ b/Semaphore.pm @@ -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;