ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/Semaphore.pm
(Generate patch)

Comparing Coro/Coro/Semaphore.pm (file contents):
Revision 1.11 by root, Wed Jul 25 19:28:00 2001 UTC vs.
Revision 1.16 by root, Thu Aug 30 02:58:17 2001 UTC

14 14
15 $sig->up; 15 $sig->up;
16 16
17=head1 DESCRIPTION 17=head1 DESCRIPTION
18 18
19This module implements counted semaphores. You can initialize a mutex 19This module implements counting semaphores. You can initialize a mutex
20with any level of parallel users, that is, you can intialize a sempahore 20with any level of parallel users, that is, you can intialize a sempahore
21that can be C<down>ed more than once until it blocks. There is no owner 21that can be C<down>ed more than once until it blocks. There is no owner
22associated with semaphores, so one coroutine can C<down> it while another 22associated with semaphores, so one coroutine can C<down> it while another
23can C<up> it. 23can C<up> it.
24 24
33 33
34package Coro::Semaphore; 34package Coro::Semaphore;
35 35
36use Coro (); 36use Coro ();
37 37
38$VERSION = 0.12; 38$VERSION = 0.45;
39 39
40=item new [inital count, default one] 40=item new [inital count]
41 41
42Creates a new sempahore object with the given initial lock count. The 42Creates a new sempahore object with the given initial lock count. The
43default lock count is 1, which means it is unlocked by default. Zero (or 43default lock count is 1, which means it is unlocked by default. Zero (or
44negative values) are also allowed, in which case the semaphore is locked 44negative values) are also allowed, in which case the semaphore is locked
45by default. 45by default.
94 } else { 94 } else {
95 return 0; 95 return 0;
96 } 96 }
97} 97}
98 98
99=item $sem->waiters
100
101In scalar context, returns the number of coroutines waiting for this
102semaphore.
103
104=cut
105
106sub waiters {
107 @{$_[0][1]};
108}
109
110=item $guard = $sem->guard
111
112This method calls C<down> and then creates a guard object. When the guard
113object is destroyed it automatically calls C<up>.
114
115=cut
116
117sub guard {
118 &down;
119 # double indirection because bless works on the referenced
120 # object, not (only) on the reference itself.
121 bless \\$_[0], Coro::Semaphore::Guard::;
122}
123
124sub Coro::Semaphore::Guard::DESTROY {
125 &up(${${$_[0]}});
126}
127
991; 1281;
100 129
101=back 130=back
102 131
103=head1 AUTHOR 132=head1 AUTHOR

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines