Commit | Line | Data |
d21067e0 |
1 | package Thread::Queue; |
28b605d8 |
2 | |
3 | our $VERSION = '1.00'; |
4 | |
9c6f8578 |
5 | our $ithreads; |
6 | our $othreads; |
7 | |
d21067e0 |
8 | use Thread qw(cond_wait cond_broadcast); |
9 | |
9c6f8578 |
10 | BEGIN { |
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 | |
31 | Thread::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 | |
45 | A queue, as implemented by C<Thread::Queue> is a thread-safe data structure |
46 | much like a list. Any number of threads can safely add elements to the end |
47 | of the list, or remove elements from the head of the list. (Queues don't |
48 | permit 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 | |
56 | The C<new> function creates a new empty queue. |
57 | |
58 | =item enqueue LIST |
59 | |
60 | The C<enqueue> method adds a list of scalars on to the end of the queue. |
61 | The queue will grow as needed to accomodate the list. |
62 | |
63 | =item dequeue |
64 | |
65 | The C<dequeue> method removes a scalar from the head of the queue and |
66 | returns it. If the queue is currently empty, C<dequeue> will block the |
67 | thread until another thread C<enqueue>s a scalar. |
68 | |
69 | =item dequeue_nb |
70 | |
71 | The C<dequeue_nb> method, like the C<dequeue> method, removes a scalar from |
72 | the head of the queue and returns it. Unlike C<dequeue>, though, |
73 | C<dequeue_nb> won't block if the queue is empty, instead returning |
74 | C<undef>. |
75 | |
76 | =item pending |
77 | |
78 | The C<pending> method returns the number of items still in the queue. (If |
79 | there can be multiple readers on the queue it's best to lock the queue |
80 | before checking to make sure that it stays in a consistent state) |
81 | |
82 | =back |
83 | |
84 | =head1 SEE ALSO |
85 | |
86 | L<Thread> |
bbc7dcd2 |
87 | |
d516a115 |
88 | =cut |
89 | |
9c6f8578 |
90 | sub new_othread { |
d21067e0 |
91 | my $class = shift; |
92 | return bless [@_], $class; |
93 | } |
94 | |
9c6f8578 |
95 | sub dequeue_othread : locked : method { |
d21067e0 |
96 | my $q = shift; |
97 | cond_wait $q until @$q; |
98 | return shift @$q; |
99 | } |
100 | |
9c6f8578 |
101 | sub 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 |
110 | sub enqueue_othread : locked : method { |
d21067e0 |
111 | my $q = shift; |
112 | push(@$q, @_) and cond_broadcast $q; |
113 | } |
114 | |
9c6f8578 |
115 | sub pending_othread : locked : method { |
a99072da |
116 | my $q = shift; |
117 | return scalar(@$q); |
118 | } |
119 | |
d21067e0 |
120 | 1; |