… | |
… | |
35 | |
35 | |
36 | BEGIN { eval { require warnings } && warnings->unimport ("uninitialized") } |
36 | BEGIN { eval { require warnings } && warnings->unimport ("uninitialized") } |
37 | |
37 | |
38 | use Coro (); |
38 | use Coro (); |
39 | |
39 | |
40 | $VERSION = 0.8; |
40 | $VERSION = 1.9; |
41 | |
41 | |
42 | =item new [inital count] |
42 | =item new [inital count] |
43 | |
43 | |
44 | Creates a new sempahore object with the given initial lock count. The |
44 | 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 |
45 | default lock count is 1, which means it is unlocked by default. Zero (or |
… | |
… | |
48 | |
48 | |
49 | =cut |
49 | =cut |
50 | |
50 | |
51 | sub new { |
51 | sub new { |
52 | bless [defined $_[1] ? $_[1] : 1], $_[0]; |
52 | bless [defined $_[1] ? $_[1] : 1], $_[0]; |
|
|
53 | } |
|
|
54 | |
|
|
55 | =item $sem->count |
|
|
56 | |
|
|
57 | Returns the current semaphore count. |
|
|
58 | |
|
|
59 | =cut |
|
|
60 | |
|
|
61 | sub count { |
|
|
62 | $_[0][0] |
53 | } |
63 | } |
54 | |
64 | |
55 | =item $sem->down |
65 | =item $sem->down |
56 | |
66 | |
57 | Decrement the counter, therefore "locking" the semaphore. This method |
67 | Decrement the counter, therefore "locking" the semaphore. This method |
… | |
… | |
65 | =cut |
75 | =cut |
66 | |
76 | |
67 | sub down { |
77 | sub down { |
68 | while ($_[0][0] <= 0) { |
78 | while ($_[0][0] <= 0) { |
69 | push @{$_[0][1]}, $Coro::current; |
79 | push @{$_[0][1]}, $Coro::current; |
70 | Coro::schedule; |
80 | &Coro::schedule; |
71 | } |
81 | } |
72 | --$_[0][0]; |
82 | --$_[0][0]; |
73 | } |
83 | } |
74 | |
84 | |
75 | sub timed_down { |
85 | sub timed_down { |
76 | require Coro::Timer; |
86 | require Coro::Timer; |
77 | my $timeout = Coro::Timer::timeout($_[1]); |
87 | my $timeout = Coro::Timer::timeout($_[1]); |
78 | |
88 | |
79 | while ($_[0][0] <= 0) { |
89 | while ($_[0][0] <= 0) { |
80 | push @{$_[0][1]}, $Coro::current; |
90 | push @{$_[0][1]}, $Coro::current; |
81 | Coro::schedule; |
91 | &Coro::schedule; |
82 | if ($timeout) { |
92 | if ($timeout) { |
83 | # ugly as hell. slow, too, btw! |
93 | # ugly as hell. slow, too, btw! |
84 | for (0..$#{$_[0][1]}) { |
94 | for (0..$#{$_[0][1]}) { |
85 | if ($_[0][1][$_] == $Coro::current) { |
95 | if ($_[0][1][$_] == $Coro::current) { |
86 | splice @{$_[0][1]}, $_, 1; |
96 | splice @{$_[0][1]}, $_, 1; |
… | |
… | |
161 | |
171 | |
162 | sub Coro::Semaphore::guard::DESTROY { |
172 | sub Coro::Semaphore::guard::DESTROY { |
163 | &up(${${$_[0]}}); |
173 | &up(${${$_[0]}}); |
164 | } |
174 | } |
165 | |
175 | |
166 | 1; |
|
|
167 | |
|
|
168 | =back |
176 | =back |
169 | |
177 | |
170 | =head1 AUTHOR |
178 | =head1 AUTHOR |
171 | |
179 | |
172 | Marc Lehmann <pcg@goof.com> |
180 | Marc Lehmann <schmorp@schmorp.de> |
173 | http://www.goof.com/pcg/marc/ |
181 | http://home.schmorp.de/ |
174 | |
182 | |
175 | =cut |
183 | =cut |
176 | |
184 | |
|
|
185 | 1 |
|
|
186 | |