Integrate mainline
[p5sagit/p5-mst-13.2.git] / lib / Thread / Queue.pm
CommitLineData
d21067e0 1package Thread::Queue;
28b605d8 2
3our $VERSION = '1.00';
4
9c6f8578 5our $ithreads;
6our $othreads;
7
d21067e0 8use Thread qw(cond_wait cond_broadcast);
9
9c6f8578 10BEGIN {
11 use Config;
12 $ithreads = $Config{useithreads};
13 $othreads = $Config{use5005threads};
14 if($ithreads) {
15 require 'threads/shared/queue.pm';
16 for my $m (qw(new enqueue dequeue dequeue_nb pending)) {
17 no strict 'refs';
18 *{"Thread::Queue::$m"} = \&{"threads::shared::queue::${m}"};
19 }
20 } else {
21 for my $m (qw(new enqueue dequeue dequeue_nb pending)) {
22 no strict 'refs';
23 *{"Thread::Queue::$m"} = \&{"Thread::Queue::${m}_othread"};
24 }
25 }
26}
27
28
d516a115 29=head1 NAME
30
31Thread::Queue - thread-safe queues
32
33=head1 SYNOPSIS
34
35 use Thread::Queue;
36 my $q = new Thread::Queue;
37 $q->enqueue("foo", "bar");
a99072da 38 my $foo = $q->dequeue; # The "bar" is still in the queue.
39 my $foo = $q->dequeue_nb; # returns "bar", or undef if the queue was
40 # empty
41 my $left = $q->pending; # returns the number of items still in the queue
d516a115 42
a99072da 43=head1 DESCRIPTION
44
45A queue, as implemented by C<Thread::Queue> is a thread-safe data structure
46much like a list. Any number of threads can safely add elements to the end
47of the list, or remove elements from the head of the list. (Queues don't
48permit adding or removing elements from the middle of the list)
49
50=head1 FUNCTIONS AND METHODS
51
52=over 8
53
54=item new
55
56The C<new> function creates a new empty queue.
57
58=item enqueue LIST
59
60The C<enqueue> method adds a list of scalars on to the end of the queue.
61The queue will grow as needed to accomodate the list.
62
63=item dequeue
64
65The C<dequeue> method removes a scalar from the head of the queue and
66returns it. If the queue is currently empty, C<dequeue> will block the
67thread until another thread C<enqueue>s a scalar.
68
69=item dequeue_nb
70
71The C<dequeue_nb> method, like the C<dequeue> method, removes a scalar from
72the head of the queue and returns it. Unlike C<dequeue>, though,
73C<dequeue_nb> won't block if the queue is empty, instead returning
74C<undef>.
75
76=item pending
77
78The C<pending> method returns the number of items still in the queue. (If
79there can be multiple readers on the queue it's best to lock the queue
80before checking to make sure that it stays in a consistent state)
81
82=back
83
84=head1 SEE ALSO
85
86L<Thread>
bbc7dcd2 87
d516a115 88=cut
89
9c6f8578 90sub new_othread {
d21067e0 91 my $class = shift;
92 return bless [@_], $class;
93}
94
9c6f8578 95sub dequeue_othread : locked : method {
d21067e0 96 my $q = shift;
97 cond_wait $q until @$q;
98 return shift @$q;
99}
100
9c6f8578 101sub dequeue_nb_othread : locked : method {
a99072da 102 my $q = shift;
103 if (@$q) {
104 return shift @$q;
105 } else {
106 return undef;
107 }
108}
109
9c6f8578 110sub enqueue_othread : locked : method {
d21067e0 111 my $q = shift;
112 push(@$q, @_) and cond_broadcast $q;
113}
114
9c6f8578 115sub pending_othread : locked : method {
a99072da 116 my $q = shift;
117 return scalar(@$q);
118}
119
d21067e0 1201;