Commit | Line | Data |
---|---|---|
6794f985 | 1 | |
2 | BEGIN { | |
3 | chdir 't' if -d 't'; | |
f1f3224a | 4 | @INC = qw(../lib .); |
6794f985 | 5 | require Config; import Config; |
6 | unless ($Config{'useithreads'}) { | |
7 | print "1..0 # Skip: no useithreads\n"; | |
8 | exit 0; | |
9 | } | |
f1f3224a | 10 | require "test.pl"; |
6794f985 | 11 | } |
12 | ||
13 | use ExtUtils::testlib; | |
14 | ||
15 | use strict; | |
16 | ||
17 | ||
18 | BEGIN { $| = 1; print "1..8\n" }; | |
6794f985 | 19 | |
f1f3224a | 20 | use_ok('threads'); |
6794f985 | 21 | |
f1f3224a | 22 | ok(threads->self == (threads->list)[0]); |
6794f985 | 23 | |
6794f985 | 24 | |
f1f3224a | 25 | threads->create(sub {})->join(); |
26 | ok(scalar @{[threads->list]} == 1); | |
6794f985 | 27 | |
f1f3224a | 28 | my $thread = threads->create(sub {}); |
29 | ok(scalar @{[threads->list]} == 2); | |
30 | $thread->join(); | |
31 | ok(scalar @{[threads->list]} == 1); | |
6794f985 | 32 | |
f1f3224a | 33 | curr_test(6); |
6794f985 | 34 | |
f1f3224a | 35 | # Just a sleep() would not guarantee that we sleep and will not |
36 | # wake up before the just created thread finishes. Instead, let's | |
37 | # use the filesystem as a semaphore. Creating a directory and removing | |
38 | # it should be a reasonably atomic operation even over NFS. | |
39 | # Also, we do not want to depend here on shared variables. | |
6794f985 | 40 | |
f1f3224a | 41 | mkdir "thrsem", 0700; |
6794f985 | 42 | |
f1f3224a | 43 | $thread = threads->create(sub { my $ret = threads->self == (threads->list)[1]; |
44 | rmdir "thrsem"; | |
45 | return $ret }); | |
6794f985 | 46 | |
f1f3224a | 47 | sleep 1 while -d "thrsem"; |
6794f985 | 48 | |
f1f3224a | 49 | ok($thread == (threads->list)[1]); |
50 | ok($thread->join()); | |
51 | ok(scalar @{[threads->list]} == 1); | |
6794f985 | 52 | |
f1f3224a | 53 | END { |
54 | 1 while rmdir "thrsem"; | |
55 | } |