Skip to content
This repository was archived by the owner on Jun 1, 2023. It is now read-only.

Commit eef6b5f

Browse files
author
Reini Urban
committed
Thread-Semaphore: Update to 2.13
Added down_timed method.
1 parent 7d10617 commit eef6b5f

File tree

10 files changed

+132
-14
lines changed

10 files changed

+132
-14
lines changed

MANIFEST

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3781,6 +3781,7 @@ dist/Thread-Semaphore/t/02_errs.t Thread::Semaphore tests
37813781
dist/Thread-Semaphore/t/03_nothreads.t Thread::Semaphore tests
37823782
dist/Thread-Semaphore/t/04_nonblocking.t Thread::Semaphore tests
37833783
dist/Thread-Semaphore/t/05_force.t Thread::Semaphore tests
3784+
dist/Thread-Semaphore/t/06_timed.t
37843785
dist/threads/hints/hpux.pl Hint file for HPUX
37853786
dist/threads/hints/linux.pl Hint file for Linux
37863787
dist/threads/lib/threads.pm ithreads

Porting/Maintainers.pl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1393,7 +1393,7 @@ package Maintainers;
13931393
},
13941394

13951395
'Thread::Semaphore' => {
1396-
'DISTRIBUTION' => 'JDHEDDEN/Thread-Semaphore-2.12.tar.gz',
1396+
'DISTRIBUTION' => 'JDHEDDEN/Thread-Semaphore-2.13.tar.gz',
13971397
'FILES' => q[dist/Thread-Semaphore],
13981398
'EXCLUDED' => [
13991399
qw( examples/semaphore.pl

dist/Module-CoreList/lib/Module/CoreList.pm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13365,6 +13365,7 @@ for my $version ( sort { version_sort($a, $b) } keys %released ) {
1336513365
'ExtUtils::Command::MM' => '8.04_04',
1336613366
'Storable' => '3.01c',
1336713367
'Term::ReadKey' => '2.37_01',
13368+
'Thread::Semaphore' => '2.13',
1336813369
'Cpanel::JSON::XS' => '3.0218',
1336913370
'CPAN::Meta' => '2.150010c',
1337013371
'CPAN::Meta::Converter' => '2.150010',

dist/Thread-Semaphore/lib/Thread/Semaphore.pm

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package Thread::Semaphore;
33
use strict;
44
use warnings;
55

6-
our $VERSION = '2.12';
6+
our $VERSION = '2.13';
77
$VERSION = eval $VERSION;
88

99
use 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)
6884
sub 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.
190206
This method does not block, and may cause the semaphore's count to drop
191207
below 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
224260
L<threads>, L<threads::shared>
225261
262+
Sample code in the I<examples> directory of this distribution on CPAN.
263+
226264
=head1 MAINTAINER
227265
228266
Jerry D. Hedden, S<E<lt>jdhedden AT cpan DOT orgE<gt>>

dist/Thread-Semaphore/t/01_basic.t

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ my @threads;
3535
push @threads, threads->create(sub {
3636
$st->down();
3737
is($token++, 1, 'Thread 1 got semaphore');
38-
$st->up();
3938
$sm->up();
4039

4140
$st->down(4);
@@ -46,7 +45,6 @@ push @threads, threads->create(sub {
4645
push @threads, threads->create(sub {
4746
$st->down(2);
4847
is($token++, 3, 'Thread 2 got semaphore');
49-
$st->up();
5048
$sm->up();
5149

5250
$st->down(4);
@@ -68,11 +66,11 @@ $st->up(9);
6866

6967
$sm->down(2);
7068
$st->down();
71-
ok(1, 'Main done');
72-
threads::yield();
7369

7470
$_->join for @threads;
7571

72+
ok(1, 'Main done');
73+
7674
exit(0);
7775

7876
# EOF

dist/Thread-Semaphore/t/03_nothreads.t

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use strict;
22
use warnings;
33

4-
use Test::More 'tests' => 6;
4+
use Test::More 'tests' => 7;
55

66
use Thread::Semaphore;
77

@@ -15,6 +15,7 @@ $s->down();
1515
is($$s, 1, 'Non-threaded semaphore');
1616
ok(! $s->down_nb(2), 'Non-threaded semaphore');
1717
ok($s->down_nb(), 'Non-threaded semaphore');
18+
ok(! $s->down_timed(1), 'Non-threaded semaphore');
1819

1920
exit(0);
2021

dist/Thread-Semaphore/t/05_force.t

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,10 @@ $st->up();
5151
$sm->down();
5252
is($token, 4, 'Main re-got semaphore');
5353

54-
ok(1, 'Main done');
55-
threads::yield();
56-
5754
$thread->join;
5855

56+
ok(1, 'Main done');
57+
5958
exit(0);
6059

6160
# EOF

dist/Thread-Semaphore/t/06_timed.t

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
use strict;
2+
use warnings;
3+
4+
BEGIN {
5+
use Config;
6+
if (! $Config{'useithreads'}) {
7+
print("1..0 # SKIP Perl not compiled with 'useithreads'\n");
8+
exit(0);
9+
}
10+
}
11+
12+
use threads;
13+
use threads::shared;
14+
use Thread::Semaphore;
15+
16+
if ($] == 5.008) {
17+
require 't/test.pl'; # Test::More work-alike for Perl 5.8.0
18+
} else {
19+
require Test::More;
20+
}
21+
Test::More->import();
22+
plan('tests' => 10);
23+
24+
### Basic usage with multiple threads ###
25+
26+
my $sm = Thread::Semaphore->new();
27+
my $st = Thread::Semaphore->new(0);
28+
ok($sm, 'New Semaphore');
29+
ok($st, 'New Semaphore');
30+
31+
my $token :shared = 0;
32+
33+
my @threads;
34+
35+
push @threads, threads->create(sub {
36+
$st->down_timed(3);
37+
is($token++, 1, 'Thread 1 got semaphore');
38+
$sm->up();
39+
40+
$st->down_timed(3, 4);
41+
is($token, 5, 'Thread 1 done');
42+
$sm->up();
43+
});
44+
45+
push @threads, threads->create(sub {
46+
$st->down_timed(3, 2);
47+
is($token++, 3, 'Thread 2 got semaphore');
48+
$sm->up();
49+
50+
# Force timeout by asking for more than will ever show up
51+
ok(! $st->down_timed(1, 10), 'Thread 2 timed out');
52+
$sm->up();
53+
});
54+
55+
$sm->down();
56+
is($token++, 0, 'Main has semaphore');
57+
$st->up();
58+
59+
$sm->down();
60+
is($token++, 2, 'Main got semaphore');
61+
$st->up(2);
62+
63+
$sm->down();
64+
is($token++, 4, 'Main re-got semaphore');
65+
$st->up(5);
66+
67+
$sm->down(2);
68+
$st->down();
69+
70+
$_->join for @threads;
71+
72+
ok(1, 'Main done');
73+
74+
exit(0);
75+
76+
# EOF

pod/perlcdelta.pod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,10 @@ See L<https://metacpan.org/changes/distribution/Sys-Syslog>
445445
Kept our smoker logic in F<t/syslog.t>, for slow darwin systems,
446446
the suse patch and disabled the lexical filehandle patch.
447447

448+
=item Thread-Semaphore 2.13
449+
450+
Added C<down_timed> method.
451+
448452
=back
449453

450454
=head1 Documentation

t/porting/customized.dat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ File::Path cpan/File-Path/t/Path_win32.t 94b9276557ce7f80b91f6fd9bfa7a0cd9bf9683
6262
File::Spec dist/PathTools/t/rel2abs_vs_symlink.t abbf1a890a1b6fefebc0c8a9e2849694ade89fa9
6363
IO::Socket::IP cpan/IO-Socket-IP/lib/IO/Socket/IP.pm fb1175286262913bec59482b84a3587ec48339ea
6464
JSON::PP cpan/JSON-PP/lib/JSON/PP.pm ea210ef037088b8ff77db8a0c149e09032a4beab
65-
Module::CoreList dist/Module-CoreList/lib/Module/CoreList.pm 943a8048cd3f5f01899850c0e4c46559245918aa
65+
Module::CoreList dist/Module-CoreList/lib/Module/CoreList.pm f27b15d4b275a468ffa4c0867ac6a759b7e54d12
6666
Module::CoreList dist/Module-CoreList/lib/Module/CoreList/Utils.pm c968b0977900360ef0cf739ee9f9c6be6ce91afc
6767
Module::Metadata cpan/Module-Metadata/t/lib/GeneratePackage.pm 502ffbe2609947430e6aa1a3df8064b3fef3e086
6868
Net::Domain cpan/libnet/lib/Net/Cmd.pm 70a007c38833667ad47ea8059c37c1b7d1c77b6c

0 commit comments

Comments
 (0)