Commit | Line | Data |
---|---|---|
f06daabb CBW |
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) { | |
3d7c117d | 17 | require './t/test.pl'; # Test::More work-alike for Perl 5.8.0 |
f06daabb CBW |
18 | } else { |
19 | require Test::More; | |
20 | } | |
21 | Test::More->import(); | |
22 | plan('tests' => 8); | |
23 | ||
24 | ### Basic usage with multiple threads ### | |
25 | ||
26 | my $sm = Thread::Semaphore->new(0); | |
27 | my $st = Thread::Semaphore->new(0); | |
28 | ok($sm, 'New Semaphore'); | |
29 | ok($st, 'New Semaphore'); | |
30 | ||
31 | my $token :shared = 0; | |
32 | ||
c6eacdc3 | 33 | my $thread = threads->create(sub { |
f06daabb CBW |
34 | $st->down_force(2); |
35 | is($token++, 0, 'Thread got semaphore'); | |
36 | $sm->up(); | |
37 | ||
38 | $st->down(); | |
39 | is($token++, 3, 'Thread done'); | |
40 | $sm->up(); | |
c6eacdc3 | 41 | }); |
f06daabb CBW |
42 | |
43 | $sm->down(); | |
44 | is($token++, 1, 'Main has semaphore'); | |
45 | $st->up(2); | |
46 | threads::yield(); | |
47 | ||
48 | is($token++, 2, 'Main still has semaphore'); | |
49 | $st->up(); | |
50 | ||
51 | $sm->down(); | |
52 | is($token, 4, 'Main re-got semaphore'); | |
53 | ||
c6eacdc3 DM |
54 | $thread->join; |
55 | ||
51068c14 JH |
56 | ok(1, 'Main done'); |
57 | ||
f06daabb CBW |
58 | exit(0); |
59 | ||
60 | # EOF |