Reverse integrate Malcolm's chanes into local
[p5sagit/p5-mst-13.2.git] / ext / Thread / Thread / Semaphore.pm
CommitLineData
d21067e0 1package Thread::Semaphore;
2use Thread qw(cond_wait cond_broadcast);
3
4sub new {
5 my $class = shift;
6 my $val = @_ ? shift : 1;
7 bless \$val, $class;
8}
9
10sub down {
11 use attrs qw(locked method);
12 my $s = shift;
0a00ffdb 13 my $inc = @_ ? shift : 1;
14 cond_wait $s until $$s >= $inc;
15 $$s -= $inc;
d21067e0 16}
17
18sub up {
19 use attrs qw(locked method);
20 my $s = shift;
0a00ffdb 21 my $inc = @_ ? shift : 1;
22 ($$s += $inc) > 0 and cond_broadcast $s;
d21067e0 23}
24
251;