1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | Coro::Semaphore - non-binary semaphores |
3 | Coro::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 | |
19 | This module implements counting semaphores. You can initialize a mutex |
19 | This module implements counting semaphores. You can initialize a mutex |
20 | with any level of parallel users, that is, you can intialize a sempahore |
20 | with any level of parallel users, that is, you can intialize a sempahore |
21 | that can be C<down>ed more than once until it blocks. There is no owner |
21 | that can be C<down>ed more than once until it blocks. There is no owner |
22 | associated with semaphores, so one coroutine can C<down> it while another |
22 | associated with semaphores, so one thread can C<down> it while another can |
23 | can C<up> it. |
23 | C<up> it (or vice versa), C<up> can be called before C<down> and so on: |
|
|
24 | the semaphore is really just an integer counter that optionally blocks |
|
|
25 | when it is 0. |
24 | |
26 | |
25 | Counting semaphores are typically used to coordinate access to |
27 | Counting semaphores are typically used to coordinate access to |
26 | resources, with the semaphore count initialized to the number of free |
28 | resources, with the semaphore count initialized to the number of free |
27 | resources. Coroutines then increment the count when resources are added |
29 | resources. Threads then increment the count when resources are added |
28 | and decrement the count when resources are removed. |
30 | and decrement the count when resources are removed. |
|
|
31 | |
|
|
32 | You don't have to load C<Coro::Semaphore> manually, it will be loaded |
|
|
33 | automatically 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 | |
34 | package Coro::Semaphore; |
39 | package Coro::Semaphore; |
35 | |
40 | |
36 | BEGIN { eval { require warnings } && warnings->unimport ("uninitialized") } |
41 | use common::sense; |
37 | |
42 | |
38 | use Coro (); |
43 | use Coro (); |
39 | |
44 | |
40 | $VERSION = 1.9; |
45 | our $VERSION = 6.31; |
41 | |
46 | |
42 | =item new [inital count] |
47 | =item new [inital count] |
43 | |
48 | |
44 | Creates a new sempahore object with the given initial lock count. The |
49 | Creates a new sempahore object with the given initial lock count. The |
45 | default lock count is 1, which means it is unlocked by default. Zero (or |
50 | default lock count is 1, which means it is unlocked by default. Zero (or |
46 | negative values) are also allowed, in which case the semaphore is locked |
51 | negative values) are also allowed, in which case the semaphore is locked |
47 | by default. |
52 | by default. |
48 | |
53 | |
49 | =cut |
54 | =item $sem->count |
50 | |
55 | |
51 | sub new { |
56 | Returns the current semaphore count. |
52 | bless [defined $_[1] ? $_[1] : 1], $_[0]; |
57 | |
53 | } |
58 | =item $sem->adjust ($diff) |
|
|
59 | |
|
|
60 | Atomically adds the amount given to the current semaphore count. If the |
|
|
61 | count becomes positive, wakes up any waiters. Does not block if the count |
|
|
62 | becomes negative, however. |
54 | |
63 | |
55 | =item $sem->down |
64 | =item $sem->down |
56 | |
65 | |
57 | Decrement the counter, therefore "locking" the semaphore. This method |
66 | Decrement the counter, therefore "locking" the semaphore. This method |
58 | waits until the semaphore is available if the counter is zero. |
67 | waits 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 | |
62 | Like C<down>, but returns false if semaphore couldn't be acquired within |
71 | Similar to C<down>, but does not actually decrement the counter. Instead, |
63 | $timeout seconds, otherwise true. |
72 | when this function returns, a following call to C<down> or C<try> is |
|
|
73 | guaranteed to succeed without blocking, until the next thread switch |
|
|
74 | (C<cede> etc.). |
|
|
75 | |
|
|
76 | Note that using C<wait> is much less efficient than using C<down>, so try |
|
|
77 | to prefer C<down> whenever possible. |
|
|
78 | |
|
|
79 | =item $sem->wait ($callback) |
|
|
80 | |
|
|
81 | If you pass a callback argument to C<wait>, it will not wait, but |
|
|
82 | immediately return. The callback will be called as soon as the semaphore |
|
|
83 | becomes available (which might be instantly), and gets passed the |
|
|
84 | semaphore as first argument. |
|
|
85 | |
|
|
86 | The callback might C<down> the semaphore exactly once, might wake up other |
|
|
87 | threads, but is I<NOT> allowed to block (switch to other threads). |
64 | |
88 | |
65 | =cut |
89 | =cut |
66 | |
90 | |
67 | sub 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 | |
75 | sub 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 | |
100 | Unlock the semaphore again. |
121 | Unlock the semaphore again. |
101 | |
|
|
102 | =cut |
|
|
103 | |
|
|
104 | sub 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 | |
112 | Try to C<down> the semaphore. Returns true when this was possible, |
125 | Try to C<down> the semaphore. Returns true when this was possible, |
113 | otherwise return false and leave the semaphore unchanged. |
126 | otherwise return false and leave the semaphore unchanged. |
114 | |
127 | |
115 | =cut |
|
|
116 | |
|
|
117 | sub 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 | |
128 | In scalar context, returns the number of coroutines waiting for this |
130 | In scalar context, returns the number of threads waiting for this |
129 | semaphore. |
131 | semaphore. Might accidentally cause WW3 if called in other contexts, so |
130 | |
132 | don't use these. |
131 | =cut |
|
|
132 | |
|
|
133 | sub waiters { |
|
|
134 | @{$_[0][1]}; |
|
|
135 | } |
|
|
136 | |
133 | |
137 | =item $guard = $sem->guard |
134 | =item $guard = $sem->guard |
138 | |
135 | |
139 | This method calls C<down> and then creates a guard object. When the guard |
136 | This method calls C<down> and then creates a guard object. When the guard |
140 | object is destroyed it automatically calls C<up>. |
137 | object is destroyed it automatically calls C<up>. |
141 | |
138 | |
142 | =item $guard = $sem->timed_guard($timeout) |
|
|
143 | |
|
|
144 | Like 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 | |
149 | sub guard { |
141 | sub 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 | |
156 | sub 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 | |
162 | sub Coro::Semaphore::guard::DESTROY { |
157 | sub 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 |