1 |
=head1 NAME |
2 |
|
3 |
Coro::Signal - thread signals (binary semaphores) |
4 |
|
5 |
=head1 SYNOPSIS |
6 |
|
7 |
use Coro::Signal; |
8 |
|
9 |
$sig = new Coro::Signal; |
10 |
|
11 |
$sig->wait; # wait for signal |
12 |
|
13 |
# ... some other "thread" |
14 |
|
15 |
$sig->send; |
16 |
|
17 |
=head1 DESCRIPTION |
18 |
|
19 |
This module implements signals/binary semaphores/condition variables |
20 |
(basically all the same thing). You can wait for a signal to occur or send |
21 |
it, in which case it will wake up one waiter, or it can be broadcast, |
22 |
waking up all waiters. |
23 |
|
24 |
It is recommended not to mix C<send> and C<broadcast> calls on the same |
25 |
C<Coro::Signal> - it should work as documented, but it can easily confuse |
26 |
you :-> |
27 |
|
28 |
=over 4 |
29 |
|
30 |
=cut |
31 |
|
32 |
package Coro::Signal; |
33 |
|
34 |
use common::sense; |
35 |
|
36 |
use Coro::Semaphore (); |
37 |
|
38 |
our $VERSION = 5.2; |
39 |
|
40 |
=item $sig = new Coro::Signal; |
41 |
|
42 |
Create a new signal. |
43 |
|
44 |
=item $sig->wait |
45 |
|
46 |
Wait for the signal to occur (via either C<send> or C<broadcast>). Returns |
47 |
immediately if the signal has been sent before. |
48 |
|
49 |
=item $sem->wait ($callback) |
50 |
|
51 |
If you pass a callback argument to C<wait>, it will not wait, but |
52 |
immediately return. The callback will be called under the same conditions |
53 |
as C<wait> without arguments would continue the thrad. |
54 |
|
55 |
The callback might wake up any number of threads, but is I<NOT> allowed to |
56 |
block (switch to other threads). |
57 |
|
58 |
=item $sig->send |
59 |
|
60 |
Send the signal, waking up I<one> waiting process or remember the signal |
61 |
if no process is waiting. |
62 |
|
63 |
=item $sig->broadcast |
64 |
|
65 |
Send the signal, waking up I<all> waiting process. If no process is |
66 |
waiting the signal is lost. |
67 |
|
68 |
=item $sig->awaited |
69 |
|
70 |
Return true when the signal is being awaited by some process. |
71 |
|
72 |
=cut |
73 |
|
74 |
#=item $status = $s->timed_wait ($timeout) |
75 |
# |
76 |
#Like C<wait>, but returns false if no signal happens within $timeout |
77 |
#seconds, otherwise true. |
78 |
# |
79 |
#See C<wait> for some reliability concerns. |
80 |
# |
81 |
#=cut |
82 |
|
83 |
#ub timed_wait { |
84 |
# require Coro::Timer; |
85 |
# my $timeout = Coro::Timer::timeout($_[1]); |
86 |
# |
87 |
# unless (delete $_[0][0]) { |
88 |
# push @{$_[0][1]}, $Coro::current; |
89 |
# &Coro::schedule; |
90 |
# |
91 |
# return 0 if $timeout; |
92 |
# } |
93 |
# |
94 |
# 1 |
95 |
# |
96 |
|
97 |
1; |
98 |
|
99 |
=back |
100 |
|
101 |
=head1 AUTHOR |
102 |
|
103 |
Marc Lehmann <schmorp@schmorp.de> |
104 |
http://home.schmorp.de/ |
105 |
|
106 |
=cut |
107 |
|