… | |
… | |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | use Coro::Semaphore; |
7 | use Coro::Semaphore; |
8 | |
8 | |
9 | $sig = new Coro::Semaphore [init]; |
9 | $sig = new Coro::Semaphore [initial value]; |
10 | |
10 | |
11 | $sig->down; # wait for signal |
11 | $sig->down; # wait for signal |
12 | |
12 | |
13 | # ... some other "thread" |
13 | # ... some other "thread" |
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::Process (); |
36 | use Coro (); |
26 | |
37 | |
27 | $VERSION = 0.01; |
38 | $VERSION = 0.45; |
|
|
39 | |
|
|
40 | =item new [inital count] |
|
|
41 | |
|
|
42 | Creates a new sempahore object with the given initial lock count. The |
|
|
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. |
|
|
46 | |
|
|
47 | =cut |
28 | |
48 | |
29 | sub new { |
49 | sub new { |
30 | bless [$_[1]], $_[0]; |
50 | bless [defined $_[1] ? $_[1] : 1], $_[0]; |
31 | } |
51 | } |
|
|
52 | |
|
|
53 | =item $sem->down |
|
|
54 | |
|
|
55 | Decrement the counter, therefore "locking" the semaphore. This method |
|
|
56 | waits until the semaphore is available if the counter is zero. |
|
|
57 | |
|
|
58 | =cut |
32 | |
59 | |
33 | sub down { |
60 | sub down { |
34 | my $self = shift; |
61 | my $self = shift; |
35 | while ($self->[0] <= 0) { |
62 | while ($self->[0] <= 0) { |
36 | push @{$self->[1]}, $Coro::current; |
63 | push @{$self->[1]}, $Coro::current; |
37 | Coro::Process::schedule; |
64 | Coro::schedule; |
38 | } |
65 | } |
39 | --$self->[0]; |
66 | --$self->[0]; |
40 | } |
67 | } |
41 | |
68 | |
|
|
69 | =item $sem->up |
|
|
70 | |
|
|
71 | Unlock the semaphore again. |
|
|
72 | |
|
|
73 | =cut |
|
|
74 | |
42 | sub up { |
75 | sub up { |
43 | my $self = shift; |
76 | my $self = shift; |
44 | if (++@{$self->[1]} == 0) { |
77 | if (++$self->[0] > 0) { |
45 | (shift @{$self->[1]})->ready if @{$self->[1]}; |
78 | (shift @{$self->[1]})->ready if @{$self->[1]}; |
46 | } |
79 | } |
47 | } |
80 | } |
|
|
81 | |
|
|
82 | =item $sem->try |
|
|
83 | |
|
|
84 | Try to C<down> the semaphore. Returns true when this was possible, |
|
|
85 | otherwise return false and leave the semaphore unchanged. |
|
|
86 | |
|
|
87 | =cut |
48 | |
88 | |
49 | sub try { |
89 | sub try { |
50 | my $self = shift; |
90 | my $self = shift; |
51 | if ($self->[0] > 0) { |
91 | if ($self->[0] > 0) { |
52 | --$self->[0]; |
92 | --$self->[0]; |
53 | return 1; |
93 | return 1; |
54 | } else { |
94 | } else { |
55 | return 0; |
95 | return 0; |
56 | } |
96 | } |
|
|
97 | } |
|
|
98 | |
|
|
99 | =item $sem->waiters |
|
|
100 | |
|
|
101 | In scalar context, returns the number of coroutines waiting for this |
|
|
102 | semaphore. |
|
|
103 | |
|
|
104 | =cut |
|
|
105 | |
|
|
106 | sub waiters { |
|
|
107 | @{$_[0][1]}; |
|
|
108 | } |
|
|
109 | |
|
|
110 | =item $guard = $sem->guard |
|
|
111 | |
|
|
112 | This method calls C<down> and then creates a guard object. When the guard |
|
|
113 | object is destroyed it automatically calls C<up>. |
|
|
114 | |
|
|
115 | =cut |
|
|
116 | |
|
|
117 | sub guard { |
|
|
118 | &down; |
|
|
119 | # double indirection because bless works on the referenced |
|
|
120 | # object, not (only) on the reference itself. |
|
|
121 | bless \\$_[0], Coro::Semaphore::Guard::; |
|
|
122 | } |
|
|
123 | |
|
|
124 | sub Coro::Semaphore::Guard::DESTROY { |
|
|
125 | &up(${${$_[0]}}); |
57 | } |
126 | } |
58 | |
127 | |
59 | 1; |
128 | 1; |
60 | |
129 | |
61 | =back |
130 | =back |