Many of the feared z/OS failures turned out to be false alarms.
[p5sagit/p5-mst-13.2.git] / lib / Thread / Semaphore.pm
CommitLineData
d21067e0 1package Thread::Semaphore;
3d1f1caf 2
3use strict;
d21067e0 4
28b605d8 5our $VERSION = '1.00';
6
3d1f1caf 7use Thread qw(cond_wait cond_broadcast);
8
9c6f8578 9BEGIN {
10 use Config;
3d1f1caf 11 if ($Config{useithreads}) {
9c6f8578 12 require 'threads/shared/semaphore.pm';
3d1f1caf 13 for my $meth (qw(new up down)) {
9c6f8578 14 no strict 'refs';
3d1f1caf 15 *{"Thread::Semaphore::$meth"} = \&{"threads::shared::semaphore::$meth"};
9c6f8578 16 }
3d1f1caf 17 } elsif ($Config{use5005threads}) {
18 for my $meth (qw(new up down)) {
9c6f8578 19 no strict 'refs';
3d1f1caf 20 *{"Thread::Semaphore::$meth"} = \&{"Thread::Semaphore::${meth}_othread"};
9c6f8578 21 }
3d1f1caf 22 } else {
23 require Carp;
24 Carp::croak("This Perl has neither ithreads nor 5005threads");
9c6f8578 25 }
26}
27
28
d516a115 29=head1 NAME
30
3d1f1caf 31Thread::Semaphore - thread-safe semaphores (for old code only)
32
33=head1 CAVEAT
34
35For new code the use of the C<Thread::Semaphore> module is discouraged and
36the direct use of the C<threads>, C<threads::shared> and
37C<threads::shared::semaphore> modules is encouraged instead.
38
39For the whole story about the development of threads in Perl, and why you
40should B<not> be using this module unless you know what you're doing, see the
41CAVEAT of the C<Thread> module.
d516a115 42
43=head1 SYNOPSIS
44
45 use Thread::Semaphore;
46 my $s = new Thread::Semaphore;
47 $s->up; # Also known as the semaphore V -operation.
48 # The guarded section is here
49 $s->down; # Also known as the semaphore P -operation.
50
51 # The default semaphore value is 1.
52 my $s = new Thread::Semaphore($initial_value);
53 $s->up($up_value);
54 $s->down($up_value);
55
5d582a37 56=head1 DESCRIPTION
57
58Semaphores provide a mechanism to regulate access to resources. Semaphores,
59unlike locks, aren't tied to particular scalars, and so may be used to
60control access to anything you care to use them for.
61
62Semaphores don't limit their values to zero or one, so they can be used to
63control access to some resource that may have more than one of. (For
64example, filehandles) Increment and decrement amounts aren't fixed at one
65either, so threads can reserve or return multiple resources at once.
66
67=head1 FUNCTIONS AND METHODS
68
69=over 8
70
71=item new
72
73=item new NUMBER
74
75C<new> creates a new semaphore, and initializes its count to the passed
76number. If no number is passed, the semaphore's count is set to one.
77
78=item down
79
80=item down NUMBER
81
82The C<down> method decreases the semaphore's count by the specified number,
83or one if no number has been specified. If the semaphore's count would drop
84below zero, this method will block until such time that the semaphore's
85count is equal to or larger than the amount you're C<down>ing the
86semaphore's count by.
87
88=item up
89
90=item up NUMBER
91
92The C<up> method increases the semaphore's count by the number specified,
93or one if no number's been specified. This will unblock any thread blocked
94trying to C<down> the semaphore if the C<up> raises the semaphore count
95above what the C<down>s are trying to decrement it by.
96
97=back
98
d516a115 99=cut
100
9c6f8578 101sub new_othread {
d21067e0 102 my $class = shift;
103 my $val = @_ ? shift : 1;
104 bless \$val, $class;
105}
106
9c6f8578 107sub down_othread : locked : method {
d21067e0 108 my $s = shift;
0a00ffdb 109 my $inc = @_ ? shift : 1;
110 cond_wait $s until $$s >= $inc;
111 $$s -= $inc;
d21067e0 112}
113
9c6f8578 114sub up_othread : locked : method {
d21067e0 115 my $s = shift;
0a00ffdb 116 my $inc = @_ ? shift : 1;
117 ($$s += $inc) > 0 and cond_broadcast $s;
d21067e0 118}
119
1201;