… | |
… | |
14 | |
14 | |
15 | $sig->up; |
15 | $sig->up; |
16 | |
16 | |
17 | =head1 DESCRIPTION |
17 | =head1 DESCRIPTION |
18 | |
18 | |
|
|
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 |
|
|
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 |
|
|
23 | can C<up> it. |
|
|
24 | |
|
|
25 | Counting semaphores are typically used to coordinate access to |
|
|
26 | resources, with the semaphore count initialized to the number of free |
|
|
27 | resources. Coroutines then increment the count when resources are added |
|
|
28 | and decrement the count when resources are removed. |
|
|
29 | |
19 | =over 4 |
30 | =over 4 |
20 | |
31 | |
21 | =cut |
32 | =cut |
22 | |
33 | |
23 | package Coro::Semaphore; |
34 | package Coro::Semaphore; |
24 | |
35 | |
25 | use Coro (); |
36 | use Coro (); |
26 | |
37 | |
27 | $VERSION = 0.12; |
38 | $VERSION = 0.49; |
28 | |
39 | |
29 | =item new [inital count, default zero] |
40 | =item new [inital count] |
30 | |
41 | |
31 | Creates a new sempahore object with the given initial lock count. The |
42 | Creates a new sempahore object with the given initial lock count. The |
32 | default lock count is 1, which means it is unlocked by default. |
43 | default lock count is 1, which means it is unlocked by default. Zero (or |
|
|
44 | negative values) are also allowed, in which case the semaphore is locked |
|
|
45 | by default. |
33 | |
46 | |
34 | =cut |
47 | =cut |
35 | |
48 | |
36 | sub new { |
49 | sub new { |
37 | bless [defined $_[1] ? $_[1] : 1], $_[0]; |
50 | bless [defined $_[1] ? $_[1] : 1], $_[0]; |
… | |
… | |
43 | waits until the semaphore is available if the counter is zero. |
56 | waits until the semaphore is available if the counter is zero. |
44 | |
57 | |
45 | =cut |
58 | =cut |
46 | |
59 | |
47 | sub down { |
60 | sub down { |
48 | my $self = shift; |
|
|
49 | while ($self->[0] <= 0) { |
61 | while ($_[0][0] <= 0) { |
50 | push @{$self->[1]}, $Coro::current; |
62 | push @{$_[0][1]}, $Coro::current; |
51 | Coro::schedule; |
63 | Coro::schedule; |
52 | } |
64 | } |
53 | --$self->[0]; |
65 | --$_[0][0]; |
54 | } |
66 | } |
55 | |
67 | |
56 | =item $sem->up |
68 | =item $sem->up |
57 | |
69 | |
58 | Unlock the semaphore again. |
70 | Unlock the semaphore again. |
59 | |
71 | |
60 | =cut |
72 | =cut |
61 | |
73 | |
62 | sub up { |
74 | sub up { |
63 | my $self = shift; |
|
|
64 | if (++$self->[0] > 0) { |
75 | if (++$_[0][0] > 0) { |
65 | (shift @{$self->[1]})->ready if @{$self->[1]}; |
76 | (shift @{$_[0][1]})->ready if @{$_[0][1]}; |
66 | } |
77 | } |
67 | } |
78 | } |
68 | |
79 | |
69 | =item $sem->try |
80 | =item $sem->try |
70 | |
81 | |
… | |
… | |
72 | otherwise return false and leave the semaphore unchanged. |
83 | otherwise return false and leave the semaphore unchanged. |
73 | |
84 | |
74 | =cut |
85 | =cut |
75 | |
86 | |
76 | sub try { |
87 | sub try { |
77 | my $self = shift; |
|
|
78 | if ($self->[0] > 0) { |
88 | if ($_[0][0] > 0) { |
79 | --$self->[0]; |
89 | --$_[0][0]; |
80 | return 1; |
90 | return 1; |
81 | } else { |
91 | } else { |
82 | return 0; |
92 | return 0; |
83 | } |
93 | } |
|
|
94 | } |
|
|
95 | |
|
|
96 | =item $sem->waiters |
|
|
97 | |
|
|
98 | In scalar context, returns the number of coroutines waiting for this |
|
|
99 | semaphore. |
|
|
100 | |
|
|
101 | =cut |
|
|
102 | |
|
|
103 | sub waiters { |
|
|
104 | @{$_[0][1]}; |
|
|
105 | } |
|
|
106 | |
|
|
107 | =item $guard = $sem->guard |
|
|
108 | |
|
|
109 | This method calls C<down> and then creates a guard object. When the guard |
|
|
110 | object is destroyed it automatically calls C<up>. |
|
|
111 | |
|
|
112 | =cut |
|
|
113 | |
|
|
114 | sub guard { |
|
|
115 | &down; |
|
|
116 | # double indirection because bless works on the referenced |
|
|
117 | # object, not (only) on the reference itself. |
|
|
118 | bless \\$_[0], Coro::Semaphore::Guard::; |
|
|
119 | } |
|
|
120 | |
|
|
121 | sub Coro::Semaphore::Guard::DESTROY { |
|
|
122 | &up(${${$_[0]}}); |
84 | } |
123 | } |
85 | |
124 | |
86 | 1; |
125 | 1; |
87 | |
126 | |
88 | =back |
127 | =back |