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 |
root |
1.73 |
This module implements signals/binary semaphores/condition variables |
20 |
root |
1.11 |
(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 |
root |
1.73 |
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 |
root |
1.1 |
=over 4 |
29 |
|
|
|
30 |
|
|
=cut |
31 |
|
|
|
32 |
|
|
package Coro::Signal; |
33 |
|
|
|
34 |
root |
1.73 |
use strict qw(vars subs); |
35 |
|
|
no warnings; |
36 |
root |
1.16 |
|
37 |
root |
1.73 |
use Coro::Semaphore (); |
38 |
root |
1.1 |
|
39 |
root |
1.80 |
our $VERSION = 5.131; |
40 |
root |
1.1 |
|
41 |
root |
1.74 |
=item $sig = new Coro::Signal; |
42 |
root |
1.3 |
|
43 |
|
|
Create a new signal. |
44 |
|
|
|
45 |
root |
1.74 |
=item $sig->wait |
46 |
root |
1.3 |
|
47 |
root |
1.73 |
Wait for the signal to occur (via either C<send> or C<broadcast>). Returns |
48 |
|
|
immediately if the signal has been sent before. |
49 |
root |
1.3 |
|
50 |
root |
1.74 |
=item $sig->send |
51 |
|
|
|
52 |
|
|
Send the signal, waking up I<one> waiting process or remember the signal |
53 |
|
|
if no process is waiting. |
54 |
|
|
|
55 |
|
|
=item $sig->broadcast |
56 |
root |
1.52 |
|
57 |
root |
1.73 |
Send the signal, waking up I<all> waiting process. If no process is |
58 |
|
|
waiting the signal is lost. |
59 |
root |
1.48 |
|
60 |
root |
1.74 |
=item $sig->awaited |
61 |
root |
1.20 |
|
62 |
root |
1.73 |
Return true when the signal is being awaited by some process. |
63 |
root |
1.48 |
|
64 |
root |
1.3 |
=cut |
65 |
|
|
|
66 |
root |
1.73 |
#=item $status = $s->timed_wait ($timeout) |
67 |
|
|
# |
68 |
|
|
#Like C<wait>, but returns false if no signal happens within $timeout |
69 |
|
|
#seconds, otherwise true. |
70 |
|
|
# |
71 |
|
|
#See C<wait> for some reliability concerns. |
72 |
|
|
# |
73 |
|
|
#=cut |
74 |
|
|
|
75 |
|
|
#ub timed_wait { |
76 |
|
|
# require Coro::Timer; |
77 |
|
|
# my $timeout = Coro::Timer::timeout($_[1]); |
78 |
|
|
# |
79 |
|
|
# unless (delete $_[0][0]) { |
80 |
|
|
# push @{$_[0][1]}, $Coro::current; |
81 |
|
|
# &Coro::schedule; |
82 |
|
|
# |
83 |
|
|
# return 0 if $timeout; |
84 |
|
|
# } |
85 |
|
|
# |
86 |
|
|
# 1 |
87 |
|
|
# |
88 |
|
|
|
89 |
root |
1.1 |
1; |
90 |
|
|
|
91 |
|
|
=back |
92 |
|
|
|
93 |
|
|
=head1 AUTHOR |
94 |
|
|
|
95 |
root |
1.38 |
Marc Lehmann <schmorp@schmorp.de> |
96 |
root |
1.36 |
http://home.schmorp.de/ |
97 |
root |
1.1 |
|
98 |
|
|
=cut |
99 |
|
|
|