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.94 by root, Tue Jun 23 23:40:06 2009 UTC vs.
Revision 1.95 by root, Wed Jun 24 02:52:38 2009 UTC

17=head1 DESCRIPTION 17=head1 DESCRIPTION
18 18
19This module implements counting 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 thread can C<down> it while another
23can C<up> it. 23can C<up> it.
24 24
25Counting semaphores are typically used to coordinate access to 25Counting semaphores are typically used to coordinate access to
26resources, with the semaphore count initialized to the number of free 26resources, with the semaphore count initialized to the number of free
27resources. Coroutines then increment the count when resources are added 27resources. Threads then increment the count when resources are added
28and decrement the count when resources are removed. 28and decrement the count when resources are removed.
29 29
30=over 4 30=over 4
31 31
32=cut 32=cut
63 63
64=item $sem->wait 64=item $sem->wait
65 65
66Similar to C<down>, but does not actually decrement the counter. Instead, 66Similar to C<down>, but does not actually decrement the counter. Instead,
67when this function returns, a following call to C<down> or C<try> is 67when this function returns, a following call to C<down> or C<try> is
68guaranteed to succeed without blocking, until the next coroutine switch 68guaranteed to succeed without blocking, until the next thread switch
69(C<cede> etc.). 69(C<cede> etc.).
70 70
71Note that using C<wait> is much less efficient than using C<down>, so try 71Note that using C<wait> is much less efficient than using C<down>, so try
72to prefer C<down> whenever possible. 72to prefer C<down> whenever possible.
73 73
77immediately return. The callback will be called as soon as the semaphore 77immediately return. The callback will be called as soon as the semaphore
78becomes available (which might be instantly), and gets passed the 78becomes available (which might be instantly), and gets passed the
79semaphore as first argument. 79semaphore as first argument.
80 80
81The callback might C<down> the semaphore exactly once, might wake up other 81The callback might C<down> the semaphore exactly once, might wake up other
82coroutines, but is I<NOT> allowed to block (switch to other coroutines). 82threads, but is I<NOT> allowed to block (switch to other threads).
83 83
84=cut 84=cut
85 85
86#=item $status = $sem->timed_down ($timeout) 86#=item $status = $sem->timed_down ($timeout)
87# 87#
120Try to C<down> the semaphore. Returns true when this was possible, 120Try to C<down> the semaphore. Returns true when this was possible,
121otherwise return false and leave the semaphore unchanged. 121otherwise return false and leave the semaphore unchanged.
122 122
123=item $sem->waiters 123=item $sem->waiters
124 124
125In scalar context, returns the number of coroutines waiting for this 125In scalar context, returns the number of threads waiting for this
126semaphore. 126semaphore.
127 127
128=item $guard = $sem->guard 128=item $guard = $sem->guard
129 129
130This method calls C<down> and then creates a guard object. When the guard 130This method calls C<down> and then creates a guard object. When the guard

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines