This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Added queue.pm and test case, still disabled because of discovered race (or am I...
[perl5.git] / ext / threads / shared / queue.pm
1
2 package threads::shared::queue;
3
4 use threads::shared;
5 use strict;
6
7 sub new {
8     my $class = shift;
9     my @q : shared = @_;
10     my $q = \@q;
11     return bless $q, $class;
12 }
13
14 sub dequeue  {
15     my $q = shift;
16     lock(@$q);
17     until(@$q) {
18         cond_wait(@$q);
19     }
20     return shift @$q;
21 }
22
23 sub dequeue_nb {
24   my $q = shift;
25   lock(@$q);
26   if (@$q) {
27     return shift @$q;
28   } else {
29     return undef;
30   }
31 }
32
33 sub enqueue {
34     my $q = shift;
35     lock(@$q);
36     push(@$q, @_) and cond_broadcast @$q;
37 }
38
39 sub pending  {
40   my $q = shift;
41   lock(@$q);
42   return scalar(@$q);
43 }
44
45 1;
46