| 1 |
=head1 NAME |
| 2 |
|
| 3 |
Coro::Signal - coroutine 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 signal/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 |
=over 4 |
| 25 |
|
| 26 |
=cut |
| 27 |
|
| 28 |
package Coro::Signal; |
| 29 |
|
| 30 |
BEGIN { eval { require warnings } && warnings->unimport ("uninitialized") } |
| 31 |
|
| 32 |
use Coro (); |
| 33 |
|
| 34 |
$VERSION = 1.9; |
| 35 |
|
| 36 |
=item $s = new Coro::Signal; |
| 37 |
|
| 38 |
Create a new signal. |
| 39 |
|
| 40 |
=cut |
| 41 |
|
| 42 |
sub new { |
| 43 |
# [flag, [pid's]] |
| 44 |
bless [], $_[0]; |
| 45 |
} |
| 46 |
|
| 47 |
=item $s->wait |
| 48 |
|
| 49 |
Wait for the signal to occur. Returns immediately if the signal has been |
| 50 |
sent before. |
| 51 |
|
| 52 |
=item $status = $s->timed_wait($timeout) |
| 53 |
|
| 54 |
Like C<wait>, but returns false if no signal happens within $timeout |
| 55 |
seconds, otherwise true. |
| 56 |
|
| 57 |
=cut |
| 58 |
|
| 59 |
sub wait { |
| 60 |
if ($_[0][0]) { |
| 61 |
$_[0][0] = 0; |
| 62 |
} else { |
| 63 |
push @{$_[0][1]}, $Coro::current; |
| 64 |
Coro::schedule; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
sub timed_wait { |
| 69 |
if ($_[0][0]) { |
| 70 |
$_[0][0] = 0; |
| 71 |
return 1; |
| 72 |
} else { |
| 73 |
require Coro::Timer; |
| 74 |
my $timeout = Coro::Timer::timeout($_[1]); |
| 75 |
push @{$_[0][1]}, $Coro::current; |
| 76 |
Coro::schedule; |
| 77 |
return !$timeout; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
=item $s->send |
| 82 |
|
| 83 |
Send the signal, waking up I<one> waiting process or remember the signal |
| 84 |
if no process is waiting. |
| 85 |
|
| 86 |
=cut |
| 87 |
|
| 88 |
sub send { |
| 89 |
if (@{$_[0][1]}) { |
| 90 |
(shift @{$_[0][1]})->ready; |
| 91 |
} else { |
| 92 |
$_[0][0] = 1; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
=item $s->broadcast |
| 97 |
|
| 98 |
Send the signal, waking up I<all> waiting process. If no process is |
| 99 |
waiting the signal is lost. |
| 100 |
|
| 101 |
=cut |
| 102 |
|
| 103 |
sub broadcast { |
| 104 |
(shift @{$_[0][1]})->ready while @{$_[0][1]}; |
| 105 |
} |
| 106 |
|
| 107 |
=item $s->awaited |
| 108 |
|
| 109 |
Return true when the signal is being awaited by some process. |
| 110 |
|
| 111 |
=cut |
| 112 |
|
| 113 |
sub awaited { |
| 114 |
!!@{$_[0][1]}; |
| 115 |
} |
| 116 |
|
| 117 |
1; |
| 118 |
|
| 119 |
=back |
| 120 |
|
| 121 |
=head1 BUGS |
| 122 |
|
| 123 |
This implementation is not currently very robust when the process is woken |
| 124 |
up by other sources, i.e. C<wait> might return early. |
| 125 |
|
| 126 |
=head1 AUTHOR |
| 127 |
|
| 128 |
Marc Lehmann <schmorp@schmorp.de> |
| 129 |
http://home.schmorp.de/ |
| 130 |
|
| 131 |
=cut |
| 132 |
|