Integrate #16254 from macperl;
[p5sagit/p5-mst-13.2.git] / ext / threads / t / list.t
CommitLineData
6794f985 1
2BEGIN {
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
13use ExtUtils::testlib;
14
15use strict;
16
17
18BEGIN { $| = 1; print "1..8\n" };
6794f985 19
f1f3224a 20use_ok('threads');
6794f985 21
f1f3224a 22ok(threads->self == (threads->list)[0]);
6794f985 23
6794f985 24
f1f3224a 25threads->create(sub {})->join();
26ok(scalar @{[threads->list]} == 1);
6794f985 27
f1f3224a 28my $thread = threads->create(sub {});
29ok(scalar @{[threads->list]} == 2);
30$thread->join();
31ok(scalar @{[threads->list]} == 1);
6794f985 32
f1f3224a 33curr_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 41mkdir "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 47sleep 1 while -d "thrsem";
6794f985 48
f1f3224a 49ok($thread == (threads->list)[1]);
50ok($thread->join());
51ok(scalar @{[threads->list]} == 1);
6794f985 52
f1f3224a 53END {
54 1 while rmdir "thrsem";
55}