1 |
=head1 NAME |
2 |
|
3 |
Coro::Signal - thread signals (binary semaphores) |
4 |
|
5 |
=head1 SYNOPSIS |
6 |
|
7 |
use Coro; |
8 |
|
9 |
my $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> without some deep thinking: while it should work as |
26 |
documented, it can easily confuse you :-> |
27 |
|
28 |
You don't have to load C<Coro::Signal> manually, it will be loaded |
29 |
automatically when you C<use Coro> and call the C<new> constructor. |
30 |
|
31 |
=over 4 |
32 |
|
33 |
=cut |
34 |
|
35 |
package Coro::Signal; |
36 |
|
37 |
use common::sense; |
38 |
|
39 |
use Coro::Semaphore (); |
40 |
|
41 |
our $VERSION = 6.57; |
42 |
|
43 |
=item $sig = new Coro::Signal; |
44 |
|
45 |
Create a new signal. |
46 |
|
47 |
=item $sig->wait |
48 |
|
49 |
Wait for the signal to occur (via either C<send> or C<broadcast>). Returns |
50 |
immediately if the signal has been sent before. |
51 |
|
52 |
=item $sig->wait ($callback) |
53 |
|
54 |
If you pass a callback argument to C<wait>, it will not wait, but |
55 |
immediately return. The callback will be called under the same conditions |
56 |
as C<wait> without arguments would continue the thrad. |
57 |
|
58 |
The callback might wake up any number of threads, but is I<NOT> allowed to |
59 |
block (switch to other threads). |
60 |
|
61 |
=item $sig->send |
62 |
|
63 |
Send the signal, waking up I<one> waiting process or remember the signal |
64 |
if no process is waiting. |
65 |
|
66 |
=item $sig->broadcast |
67 |
|
68 |
Send the signal, waking up I<all> waiting process. If no process is |
69 |
waiting the signal is lost. |
70 |
|
71 |
=item $sig->awaited |
72 |
|
73 |
Return true when the signal is being awaited by some process. |
74 |
|
75 |
=cut |
76 |
|
77 |
#=item $status = $s->timed_wait ($timeout) |
78 |
# |
79 |
#Like C<wait>, but returns false if no signal happens within $timeout |
80 |
#seconds, otherwise true. |
81 |
# |
82 |
#See C<wait> for some reliability concerns. |
83 |
# |
84 |
#=cut |
85 |
|
86 |
#ub timed_wait { |
87 |
# require Coro::Timer; |
88 |
# my $timeout = Coro::Timer::timeout($_[1]); |
89 |
# |
90 |
# unless (delete $_[0][0]) { |
91 |
# push @{$_[0][1]}, $Coro::current; |
92 |
# &Coro::schedule; |
93 |
# |
94 |
# return 0 if $timeout; |
95 |
# } |
96 |
# |
97 |
# 1 |
98 |
# |
99 |
|
100 |
1; |
101 |
|
102 |
=back |
103 |
|
104 |
=head1 AUTHOR/SUPPORT/CONTACT |
105 |
|
106 |
Marc A. Lehmann <schmorp@schmorp.de> |
107 |
http://software.schmorp.de/pkg/Coro.html |
108 |
|
109 |
=cut |
110 |
|