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.82 by root, Wed Nov 19 11:11:10 2008 UTC vs.
Revision 1.100 by root, Tue Jul 28 02:04:21 2009 UTC

1=head1 NAME 1=head1 NAME
2 2
3Coro::Semaphore - non-binary semaphores 3Coro::Semaphore - counting semaphores
4 4
5=head1 SYNOPSIS 5=head1 SYNOPSIS
6 6
7 use Coro::Semaphore; 7 use Coro::Semaphore;
8 8
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
35 35
36no warnings; 36no warnings;
37 37
38use Coro (); 38use Coro ();
39 39
40$VERSION = 5.0; 40$VERSION = 5.162;
41 41
42=item new [inital count] 42=item new [inital count]
43 43
44Creates a new sempahore object with the given initial lock count. The 44Creates a new sempahore object with the given initial lock count. The
45default lock count is 1, which means it is unlocked by default. Zero (or 45default lock count is 1, which means it is unlocked by default. Zero (or
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
84This is considered a rather experimental interface, and is subject to
85change.
86 83
87=cut 84=cut
88 85
89#=item $status = $sem->timed_down ($timeout) 86#=item $status = $sem->timed_down ($timeout)
90# 87#
123Try to C<down> the semaphore. Returns true when this was possible, 120Try to C<down> the semaphore. Returns true when this was possible,
124otherwise return false and leave the semaphore unchanged. 121otherwise return false and leave the semaphore unchanged.
125 122
126=item $sem->waiters 123=item $sem->waiters
127 124
128In scalar context, returns the number of coroutines waiting for this 125In scalar context, returns the number of threads waiting for this
129semaphore. 126semaphore.
130 127
131=item $guard = $sem->guard 128=item $guard = $sem->guard
132 129
133This 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
135 132
136=cut 133=cut
137 134
138sub guard { 135sub guard {
139 &down; 136 &down;
140 # double indirection because bless works on the referenced
141 # object, not (only) on the reference itself.
142 bless \\$_[0], Coro::Semaphore::guard::; 137 bless [$_[0]], Coro::Semaphore::guard::
143} 138}
144 139
145#=item $guard = $sem->timed_guard ($timeout) 140#=item $guard = $sem->timed_guard ($timeout)
146# 141#
147#Like C<guard>, but returns undef if semaphore couldn't be acquired within 142#Like C<guard>, but returns undef if semaphore couldn't be acquired within
152# ? bless \\$_[0], Coro::Semaphore::guard:: 147# ? bless \\$_[0], Coro::Semaphore::guard::
153# : (); 148# : ();
154#} 149#}
155 150
156sub Coro::Semaphore::guard::DESTROY { 151sub Coro::Semaphore::guard::DESTROY {
157 &up(${${$_[0]}}); 152 &up($_[0][0]);
158} 153}
159 154
160=back 155=back
161 156
162=head1 AUTHOR 157=head1 AUTHOR

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines