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