@@ -3,7 +3,7 @@ package Thread::Semaphore;
33use strict;
44use warnings;
55
6- our $VERSION = ' 2.12 ' ;
6+ our $VERSION = ' 2.13 ' ;
77$VERSION = eval $VERSION ;
88
99use threads::shared;
@@ -64,6 +64,22 @@ sub down_force {
6464 $$sema -= $dec ;
6565}
6666
67+ # Decrement a semaphore's count with timeout
68+ # (timeout in seconds; decrement amount defaults to 1)
69+ sub down_timed {
70+ my $sema = shift ;
71+ my $timeout = $validate_arg -> (shift );
72+ my $dec = @_ ? $validate_arg -> (shift ) : 1;
73+
74+ lock($$sema );
75+ my $abs = time () + $timeout ;
76+ until ($$sema >= $dec ) {
77+ return if !cond_timedwait($$sema , $abs );
78+ }
79+ $$sema -= $dec ;
80+ return 1;
81+ }
82+
6783# Increment a semaphore's count (increment amount defaults to 1)
6884sub up {
6985 my $sema = shift ;
@@ -102,7 +118,7 @@ Thread::Semaphore - Thread-safe semaphores
102118
103119=head1 VERSION
104120
105- This document describes Thread::Semaphore version 2.12
121+ This document describes Thread::Semaphore version 2.13
106122
107123=head1 SYNOPSIS
108124
@@ -190,6 +206,23 @@ number (which must be an integer >= 1), or by one if no number is specified.
190206This method does not block, and may cause the semaphore's count to drop
191207below zero.
192208
209+ =item ->down_timed(TIMEOUT)
210+
211+ =item ->down_timed(TIMEOUT, NUMBER)
212+
213+ The C<down_timed > method attempts to decrease the semaphore's count by 1
214+ or by the specified number within the specified timeout period given in
215+ seconds (which must be an integer >= 0).
216+
217+ If the semaphore's count would drop below zero, this method will block
218+ until either the semaphore's count is greater than or equal to the
219+ amount you're C<down > ing the semaphore's count by, or until the timeout is
220+ reached.
221+
222+ If the timeout is reached, this method will return I<false > , and the
223+ semaphore's count remains unchanged. Otherwise, the semaphore's count is
224+ decremented and this method returns I<true > .
225+
193226=item ->up()
194227
195228=item ->up(NUMBER)
@@ -218,11 +251,16 @@ environment.
218251
219252=head1 SEE ALSO
220253
221- Thread::Semaphore Discussion Forum on CPAN:
222- L<http://www.cpanforum.com/dist/Thread-Semaphore>
254+ Thread::Semaphore on MetaCPAN:
255+ L<https://metacpan.org/release/Thread-Semaphore>
256+
257+ Code repository for CPAN distribution:
258+ L<https://github.com/Dual-Life/Thread-Semaphore>
223259
224260L<threads> , L<threads::shared>
225261
262+ Sample code in the I<examples > directory of this distribution on CPAN.
263+
226264=head1 MAINTAINER
227265
228266Jerry D. Hedden, S<E<lt> jdhedden AT cpan DOT orgE<gt> >
0 commit comments