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.53 by root, Wed Feb 1 23:59:41 2006 UTC vs.
Revision 1.131 by root, Thu May 9 05:40:14 2013 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 can
23can C<up> it. 23C<up> it (or vice versa), C<up> can be called before C<down> and so on:
24the semaphore is really just an integer counter that optionally blocks
25when it is 0.
24 26
25Counting semaphores are typically used to coordinate access to 27Counting semaphores are typically used to coordinate access to
26resources, with the semaphore count initialized to the number of free 28resources, with the semaphore count initialized to the number of free
27resources. Coroutines then increment the count when resources are added 29resources. Threads then increment the count when resources are added
28and decrement the count when resources are removed. 30and decrement the count when resources are removed.
31
32You don't have to load C<Coro::Semaphore> manually, it will be loaded
33automatically when you C<use Coro> and call the C<new> constructor.
29 34
30=over 4 35=over 4
31 36
32=cut 37=cut
33 38
34package Coro::Semaphore; 39package Coro::Semaphore;
35 40
36BEGIN { eval { require warnings } && warnings->unimport ("uninitialized") } 41use common::sense;
37 42
38use Coro (); 43use Coro ();
39 44
40$VERSION = 1.9; 45our $VERSION = 6.31;
41 46
42=item new [inital count] 47=item new [inital count]
43 48
44Creates a new sempahore object with the given initial lock count. The 49Creates 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 50default lock count is 1, which means it is unlocked by default. Zero (or
46negative values) are also allowed, in which case the semaphore is locked 51negative values) are also allowed, in which case the semaphore is locked
47by default. 52by default.
48 53
49=cut 54=item $sem->count
50 55
51sub new { 56Returns the current semaphore count.
52 bless [defined $_[1] ? $_[1] : 1], $_[0]; 57
53} 58=item $sem->adjust ($diff)
59
60Atomically adds the amount given to the current semaphore count. If the
61count becomes positive, wakes up any waiters. Does not block if the count
62becomes negative, however.
54 63
55=item $sem->down 64=item $sem->down
56 65
57Decrement the counter, therefore "locking" the semaphore. This method 66Decrement the counter, therefore "locking" the semaphore. This method
58waits until the semaphore is available if the counter is zero. 67waits until the semaphore is available if the counter is zero.
59 68
60=item $status = $sem->timed_down($timeout) 69=item $sem->wait
61 70
62Like C<down>, but returns false if semaphore couldn't be acquired within 71Similar to C<down>, but does not actually decrement the counter. Instead,
63$timeout seconds, otherwise true. 72when this function returns, a following call to C<down> or C<try> is
73guaranteed to succeed without blocking, until the next thread switch
74(C<cede> etc.).
75
76Note that using C<wait> is much less efficient than using C<down>, so try
77to prefer C<down> whenever possible.
78
79=item $sem->wait ($callback)
80
81If you pass a callback argument to C<wait>, it will not wait, but
82immediately return. The callback will be called as soon as the semaphore
83becomes available (which might be instantly), and gets passed the
84semaphore as first argument.
85
86The callback might C<down> the semaphore exactly once, might wake up other
87threads, but is I<NOT> allowed to block (switch to other threads).
64 88
65=cut 89=cut
66 90
67sub down { 91#=item $status = $sem->timed_down ($timeout)
68 while ($_[0][0] <= 0) { 92#
69 push @{$_[0][1]}, $Coro::current; 93#Like C<down>, but returns false if semaphore couldn't be acquired within
70 Coro::schedule; 94#$timeout seconds, otherwise true.
71 }
72 --$_[0][0];
73}
74 95
75sub timed_down { 96#sub timed_down {
76 require Coro::Timer; 97# require Coro::Timer;
77 my $timeout = Coro::Timer::timeout($_[1]); 98# my $timeout = Coro::Timer::timeout ($_[1]);
78 99#
79 while ($_[0][0] <= 0) { 100# while ($_[0][0] <= 0) {
80 push @{$_[0][1]}, $Coro::current; 101# push @{$_[0][1]}, $Coro::current;
81 Coro::schedule; 102# &Coro::schedule;
82 if ($timeout) { 103# if ($timeout) {
83 # ugly as hell. slow, too, btw! 104# # ugly as hell. slow, too, btw!
84 for (0..$#{$_[0][1]}) { 105# for (0..$#{$_[0][1]}) {
85 if ($_[0][1][$_] == $Coro::current) { 106# if ($_[0][1][$_] == $Coro::current) {
86 splice @{$_[0][1]}, $_, 1; 107# splice @{$_[0][1]}, $_, 1;
87 return; 108# return;
88 } 109# }
89 } 110# }
90 die; 111# die;
91 } 112# }
92 } 113# }
93 114#
94 --$_[0][0]; 115# --$_[0][0];
95 return 1; 116# return 1;
96} 117#}
97 118
98=item $sem->up 119=item $sem->up
99 120
100Unlock the semaphore again. 121Unlock the semaphore again.
101
102=cut
103
104sub up {
105 if (++$_[0][0] > 0) {
106 (shift @{$_[0][1]})->ready if @{$_[0][1]};
107 }
108}
109 122
110=item $sem->try 123=item $sem->try
111 124
112Try to C<down> the semaphore. Returns true when this was possible, 125Try to C<down> the semaphore. Returns true when this was possible,
113otherwise return false and leave the semaphore unchanged. 126otherwise return false and leave the semaphore unchanged.
114 127
115=cut
116
117sub try {
118 if ($_[0][0] > 0) {
119 --$_[0][0];
120 return 1;
121 } else {
122 return 0;
123 }
124}
125
126=item $sem->waiters 128=item $sem->waiters
127 129
128In scalar context, returns the number of coroutines waiting for this 130In scalar context, returns the number of threads waiting for this
129semaphore. 131semaphore. Might accidentally cause WW3 if called in other contexts, so
130 132don't use these.
131=cut
132
133sub waiters {
134 @{$_[0][1]};
135}
136 133
137=item $guard = $sem->guard 134=item $guard = $sem->guard
138 135
139This method calls C<down> and then creates a guard object. When the guard 136This method calls C<down> and then creates a guard object. When the guard
140object is destroyed it automatically calls C<up>. 137object is destroyed it automatically calls C<up>.
141 138
142=item $guard = $sem->timed_guard($timeout)
143
144Like C<guard>, but returns undef if semaphore couldn't be acquired within
145$timeout seconds, otherwise the guard object.
146
147=cut 139=cut
148 140
149sub guard { 141sub guard {
150 &down; 142 &down;
151 # double indirection because bless works on the referenced
152 # object, not (only) on the reference itself.
153 bless \\$_[0], Coro::Semaphore::guard::; 143 bless [$_[0]], Coro::Semaphore::guard::
154} 144}
155 145
146#=item $guard = $sem->timed_guard ($timeout)
147#
148#Like C<guard>, but returns undef if semaphore couldn't be acquired within
149#$timeout seconds, otherwise the guard object.
150
156sub timed_guard { 151#sub timed_guard {
157 &timed_down 152# &timed_down
158 ? bless \\$_[0], Coro::Semaphore::guard:: 153# ? bless \\$_[0], Coro::Semaphore::guard::
159 : (); 154# : ();
160} 155#}
161 156
162sub Coro::Semaphore::guard::DESTROY { 157sub Coro::Semaphore::guard::DESTROY {
163 &up(${${$_[0]}}); 158 &up($_[0][0]);
164} 159}
165 160
166=back 161=back
167 162
168=head1 AUTHOR 163=head1 AUTHOR

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines