ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/Signal.pm
Revision: 1.17
Committed: Mon Sep 24 01:36:20 2001 UTC (22 years, 8 months ago) by root
Branch: MAIN
Changes since 1.16: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

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