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.83 by root, Thu Nov 20 09:37:21 2008 UTC vs.
Revision 1.112 by root, Sat Feb 19 06:51:23 2011 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;
8 8
9 $sig = new Coro::Semaphore [initial value]; 9 $sig = new Coro::Semaphore [initial value];
10 10
11 $sig->down; # wait for signal 11 $sig->down; # wait for signal
12 12
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
30You don't have to load C<Coro::Semaphore> manually, it will be loaded
31automatically when you C<use Coro> and call the C<new> constructor.
29 32
30=over 4 33=over 4
31 34
32=cut 35=cut
33 36
34package Coro::Semaphore; 37package Coro::Semaphore;
35 38
36no warnings; 39use common::sense;
37 40
38use Coro (); 41use Coro ();
39 42
40$VERSION = "5.0"; 43our $VERSION = 5.37;
41 44
42=item new [inital count] 45=item new [inital count]
43 46
44Creates a new sempahore object with the given initial lock count. The 47Creates 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 48default lock count is 1, which means it is unlocked by default. Zero (or
63 66
64=item $sem->wait 67=item $sem->wait
65 68
66Similar to C<down>, but does not actually decrement the counter. Instead, 69Similar 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 70when this function returns, a following call to C<down> or C<try> is
68guaranteed to succeed without blocking, until the next coroutine switch 71guaranteed to succeed without blocking, until the next thread switch
69(C<cede> etc.). 72(C<cede> etc.).
70 73
71Note that using C<wait> is much less efficient than using C<down>, so try 74Note that using C<wait> is much less efficient than using C<down>, so try
72to prefer C<down> whenever possible. 75to prefer C<down> whenever possible.
73 76
77immediately return. The callback will be called as soon as the semaphore 80immediately return. The callback will be called as soon as the semaphore
78becomes available (which might be instantly), and gets passed the 81becomes available (which might be instantly), and gets passed the
79semaphore as first argument. 82semaphore as first argument.
80 83
81The callback might C<down> the semaphore exactly once, might wake up other 84The callback might C<down> the semaphore exactly once, might wake up other
82coroutines, but is I<NOT> allowed to block (switch to other coroutines). 85threads, 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 86
87=cut 87=cut
88 88
89#=item $status = $sem->timed_down ($timeout) 89#=item $status = $sem->timed_down ($timeout)
90# 90#
123Try to C<down> the semaphore. Returns true when this was possible, 123Try to C<down> the semaphore. Returns true when this was possible,
124otherwise return false and leave the semaphore unchanged. 124otherwise return false and leave the semaphore unchanged.
125 125
126=item $sem->waiters 126=item $sem->waiters
127 127
128In scalar context, returns the number of coroutines waiting for this 128In scalar context, returns the number of threads waiting for this
129semaphore. 129semaphore.
130 130
131=item $guard = $sem->guard 131=item $guard = $sem->guard
132 132
133This method calls C<down> and then creates a guard object. When the guard 133This method calls C<down> and then creates a guard object. When the guard
135 135
136=cut 136=cut
137 137
138sub guard { 138sub guard {
139 &down; 139 &down;
140 # double indirection because bless works on the referenced
141 # object, not (only) on the reference itself.
142 bless \\$_[0], Coro::Semaphore::guard::; 140 bless [$_[0]], Coro::Semaphore::guard::
143} 141}
144 142
145#=item $guard = $sem->timed_guard ($timeout) 143#=item $guard = $sem->timed_guard ($timeout)
146# 144#
147#Like C<guard>, but returns undef if semaphore couldn't be acquired within 145#Like C<guard>, but returns undef if semaphore couldn't be acquired within
152# ? bless \\$_[0], Coro::Semaphore::guard:: 150# ? bless \\$_[0], Coro::Semaphore::guard::
153# : (); 151# : ();
154#} 152#}
155 153
156sub Coro::Semaphore::guard::DESTROY { 154sub Coro::Semaphore::guard::DESTROY {
157 &up(${${$_[0]}}); 155 &up($_[0][0]);
158} 156}
159 157
160=back 158=back
161 159
162=head1 AUTHOR 160=head1 AUTHOR

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines