This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[Fwd: [PATCH] Pod::Find should ignore SCM files and dirs]
[perl5.git] / lib / Thread / Semaphore.pm
CommitLineData
d21067e0 1package Thread::Semaphore;
3d1f1caf 2
83272a45 3use threads::shared;
9c6f8578 4
2af1ab88 5our $VERSION = '2.01';
9c6f8578 6
d516a115
JH
7=head1 NAME
8
83272a45 9Thread::Semaphore - thread-safe semaphores
d516a115
JH
10
11=head1 SYNOPSIS
12
13 use Thread::Semaphore;
14 my $s = new Thread::Semaphore;
14180c03 15 $s->down; # Also known as the semaphore P operation.
d516a115 16 # The guarded section is here
14180c03 17 $s->up; # Also known as the semaphore V operation.
d516a115
JH
18
19 # The default semaphore value is 1.
20 my $s = new Thread::Semaphore($initial_value);
14180c03 21 $s->down($down_value);
d516a115 22 $s->up($up_value);
d516a115 23
5d582a37
HM
24=head1 DESCRIPTION
25
26Semaphores provide a mechanism to regulate access to resources. Semaphores,
27unlike locks, aren't tied to particular scalars, and so may be used to
28control access to anything you care to use them for.
29
30Semaphores don't limit their values to zero or one, so they can be used to
83272a45 31control access to some resource that there may be more than one of. (For
14180c03 32example, filehandles.) Increment and decrement amounts aren't fixed at one
5d582a37
HM
33either, so threads can reserve or return multiple resources at once.
34
35=head1 FUNCTIONS AND METHODS
36
37=over 8
38
39=item new
40
41=item new NUMBER
42
43C<new> creates a new semaphore, and initializes its count to the passed
44number. If no number is passed, the semaphore's count is set to one.
45
46=item down
47
48=item down NUMBER
49
50The C<down> method decreases the semaphore's count by the specified number,
83272a45 51or by one if no number has been specified. If the semaphore's count would drop
5d582a37
HM
52below zero, this method will block until such time that the semaphore's
53count is equal to or larger than the amount you're C<down>ing the
54semaphore's count by.
55
14180c03
PN
56This is the semaphore "P operation" (the name derives from the Dutch
57word "pak", which means "capture" -- the semaphore operations were
58named by the late Dijkstra, who was Dutch).
59
5d582a37
HM
60=item up
61
62=item up NUMBER
63
64The C<up> method increases the semaphore's count by the number specified,
83272a45 65or by one if no number has been specified. This will unblock any thread blocked
5d582a37 66trying to C<down> the semaphore if the C<up> raises the semaphore count
83272a45 67above the amount that the C<down>s are trying to decrement it by.
5d582a37 68
14180c03
PN
69This is the semaphore "V operation" (the name derives from the Dutch
70word "vrij", which means "release").
71
5d582a37
HM
72=back
73
d516a115
JH
74=cut
75
83272a45 76sub new {
d21067e0 77 my $class = shift;
83272a45 78 my $val : shared = @_ ? shift : 1;
d21067e0
MB
79 bless \$val, $class;
80}
81
83272a45 82sub down {
d21067e0 83 my $s = shift;
83272a45 84 lock($$s);
0a00ffdb 85 my $inc = @_ ? shift : 1;
83272a45 86 cond_wait $$s until $$s >= $inc;
0a00ffdb 87 $$s -= $inc;
d21067e0
MB
88}
89
83272a45 90sub up {
d21067e0 91 my $s = shift;
83272a45 92 lock($$s);
0a00ffdb 93 my $inc = @_ ? shift : 1;
83272a45 94 ($$s += $inc) > 0 and cond_broadcast $$s;
d21067e0
MB
95}
96
971;