X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FThread%2FQueue.pm;h=3b5c7c9301028412f7c6f44b8fd3462b85c5b20a;hb=878090d5582120ef9336936d4fc06895b4fd242a;hp=ebecb7433f7fbc61bd14fa236db26838195a78d8;hpb=3d1f1caf68f964a756e1ffb5a4c6bc032cad2402;p=p5sagit%2Fp5-mst-13.2.git diff --git a/lib/Thread/Queue.pm b/lib/Thread/Queue.pm index ebecb74..3b5c7c9 100644 --- a/lib/Thread/Queue.pm +++ b/lib/Thread/Queue.pm @@ -1,44 +1,13 @@ package Thread::Queue; +use threads::shared; use strict; -our $VERSION = '1.00'; - -use Thread qw(cond_wait cond_broadcast); - -BEGIN { - use Config; - if ($Config{useithreads}) { - require 'threads/shared/queue.pm'; - for my $meth (qw(new enqueue dequeue dequeue_nb pending)) { - no strict 'refs'; - *{"Thread::Queue::$meth"} = \&{"threads::shared::queue::$meth"}; - } - } elsif ($Config{use5005threads}) { - for my $meth (qw(new enqueue dequeue dequeue_nb pending)) { - no strict 'refs'; - *{"Thread::Queue::$meth"} = \&{"Thread::Queue::${meth}_othread"}; - } - } else { - require Carp; - Carp::croak("This Perl has neither ithreads nor 5005threads"); - } -} - +our $VERSION = '2.00'; =head1 NAME -Thread::Queue - thread-safe queues (for old code only) - -=head1 CAVEAT - -For new code the use of the C module is discouraged and -the direct use of the C, C and -C modules is encouraged instead. - -For the whole story about the development of threads in Perl, and why you -should B be using this module unless you know what you're doing, see the -CAVEAT of the C module. +Thread::Queue - thread-safe queues =head1 SYNOPSIS @@ -46,16 +15,16 @@ CAVEAT of the C module. my $q = new Thread::Queue; $q->enqueue("foo", "bar"); my $foo = $q->dequeue; # The "bar" is still in the queue. - my $foo = $q->dequeue_nb; # returns "bar", or undef if the queue was - # empty + my $foo = $q->dequeue_nb; # returns "bar", or undef if the queue was empty my $left = $q->pending; # returns the number of items still in the queue =head1 DESCRIPTION -A queue, as implemented by C is a thread-safe data structure -much like a list. Any number of threads can safely add elements to the end -of the list, or remove elements from the head of the list. (Queues don't -permit adding or removing elements from the middle of the list) +A queue, as implemented by C is a thread-safe +data structure much like a list. Any number of threads can safely +add elements to the end of the list, or remove elements from the head +of the list. (Queues don't permit adding or removing elements from +the middle of the list). =head1 FUNCTIONS AND METHODS @@ -68,7 +37,7 @@ The C function creates a new empty queue. =item enqueue LIST The C method adds a list of scalars on to the end of the queue. -The queue will grow as needed to accomodate the list. +The queue will grow as needed to accommodate the list. =item dequeue @@ -85,45 +54,48 @@ C. =item pending -The C method returns the number of items still in the queue. (If -there can be multiple readers on the queue it's best to lock the queue -before checking to make sure that it stays in a consistent state) +The C method returns the number of items still in the queue. =back =head1 SEE ALSO -L +L, L =cut -sub new_othread { +sub new { my $class = shift; - return bless [@_], $class; + my @q : shared = @_; + return bless \@q, $class; } -sub dequeue_othread : locked : method { +sub dequeue { my $q = shift; - cond_wait $q until @$q; + lock(@$q); + cond_wait @$q until @$q; + cond_signal @$q if @$q > 1; return shift @$q; } -sub dequeue_nb_othread : locked : method { - my $q = shift; - if (@$q) { +sub dequeue_nb { + my $q = shift; + lock(@$q); return shift @$q; - } else { - return undef; - } } -sub enqueue_othread : locked : method { +sub enqueue { my $q = shift; - push(@$q, @_) and cond_broadcast $q; + lock(@$q); + push @$q, @_ and cond_signal @$q; } -sub pending_othread : locked : method { - return scalar(@{(shift)}); +sub pending { + my $q = shift; + lock(@$q); + return scalar(@$q); } 1; + +