1 |
root |
1.1 |
=head1 NAME |
2 |
|
|
|
3 |
root |
1.2 |
Coro::Signal - coroutine signals (binary semaphores) |
4 |
root |
1.1 |
|
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 |
|
|
=over 4 |
20 |
|
|
|
21 |
|
|
=cut |
22 |
|
|
|
23 |
|
|
package Coro::Signal; |
24 |
|
|
|
25 |
root |
1.5 |
use Coro (); |
26 |
root |
1.1 |
|
27 |
root |
1.8 |
$VERSION = 0.09; |
28 |
root |
1.1 |
|
29 |
root |
1.3 |
=item $s = new Coro::Signal; |
30 |
|
|
|
31 |
|
|
Create a new signal. |
32 |
|
|
|
33 |
|
|
=cut |
34 |
|
|
|
35 |
root |
1.1 |
sub new { |
36 |
root |
1.3 |
# [flag, [pid's]] |
37 |
root |
1.1 |
bless [], $_[0]; |
38 |
|
|
} |
39 |
|
|
|
40 |
root |
1.3 |
=item $s->wait |
41 |
|
|
|
42 |
root |
1.8 |
Wait for the signal to occur. Returns immediately if the signal has been |
43 |
|
|
sent before. |
44 |
root |
1.3 |
|
45 |
|
|
=cut |
46 |
|
|
|
47 |
root |
1.1 |
sub wait { |
48 |
root |
1.3 |
if ($_[0][0]) { |
49 |
|
|
$_[0][0] = 0; |
50 |
root |
1.1 |
} else { |
51 |
root |
1.5 |
push @{$_[0][1]}, $Coro::current; |
52 |
|
|
Coro::schedule; |
53 |
root |
1.1 |
} |
54 |
|
|
} |
55 |
|
|
|
56 |
root |
1.3 |
=item $s->send |
57 |
|
|
|
58 |
root |
1.8 |
Send the signal, waking up I<one> waiting process or remember the signal |
59 |
|
|
if no process is waiting. |
60 |
root |
1.3 |
|
61 |
|
|
=cut |
62 |
|
|
|
63 |
root |
1.1 |
sub send { |
64 |
root |
1.3 |
if (@{$_[0][1]}) { |
65 |
|
|
(shift @{$_[0][1]})->ready; |
66 |
root |
1.1 |
} else { |
67 |
root |
1.3 |
$_[0][0] = 1; |
68 |
root |
1.1 |
} |
69 |
|
|
} |
70 |
|
|
|
71 |
root |
1.3 |
=item $s->broadcast |
72 |
|
|
|
73 |
root |
1.8 |
Send the signal, waking up I<all> waiting process. If no process is |
74 |
|
|
waiting the signal is lost. |
75 |
root |
1.3 |
|
76 |
|
|
=cut |
77 |
|
|
|
78 |
|
|
sub broadcast { |
79 |
|
|
(shift @{$_[0][1]})->ready while @{$_[0][1]}; |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
=item $s->awaited |
83 |
|
|
|
84 |
|
|
Return true when the signal is beign awaited by some process. |
85 |
|
|
|
86 |
|
|
=cut |
87 |
|
|
|
88 |
root |
1.1 |
sub awaited { |
89 |
root |
1.3 |
!!@{$_[0][1]}; |
90 |
root |
1.1 |
} |
91 |
|
|
|
92 |
|
|
1; |
93 |
|
|
|
94 |
|
|
=back |
95 |
|
|
|
96 |
|
|
=head1 AUTHOR |
97 |
|
|
|
98 |
|
|
Marc Lehmann <pcg@goof.com> |
99 |
|
|
http://www.goof.com/pcg/marc/ |
100 |
|
|
|
101 |
|
|
=cut |
102 |
|
|
|