diff options
author | Jerry D. Hedden <jdhedden@cpan.org> | 2007-04-13 08:51:40 -0400 |
---|---|---|
committer | Rafael Garcia-Suarez <rgarciasuarez@gmail.com> | 2007-04-15 07:12:33 +0000 |
commit | 9d40afd19ff47f5f5e62fab1f340c175ed9224e4 (patch) | |
tree | 720ebf1a59312d810003e4377148ce971b45d979 /lib/Thread.t | |
parent | 3f90d08564e6a1d7297ac8c4469eb0266b7d1eab (diff) | |
download | perl-9d40afd19ff47f5f5e62fab1f340c175ed9224e4.tar.gz |
Fix Thread.pm
From: "Jerry D. Hedden" <jdhedden@cpan.org>
Message-ID: <1ff86f510704130951t5f66baa0m4ed13018539976a3@mail.gmail.com>
p4raw-id: //depot/perl@30953
Diffstat (limited to 'lib/Thread.t')
-rw-r--r-- | lib/Thread.t | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/lib/Thread.t b/lib/Thread.t new file mode 100644 index 0000000000..2a0e2af5af --- /dev/null +++ b/lib/Thread.t @@ -0,0 +1,90 @@ +use strict; +use warnings; + +BEGIN { + chdir 't' if -d 't'; + @INC = '../lib'; + + use Config; + if (! $Config{usethreads}) { + print("1..0 # Skip: No threads\n"); + exit(0); + } +} + +use Thread qw(:DEFAULT async yield); + +use Test::More tests => 13; + +my $lock; +{ + no warnings 'once'; + if ($threads::shared::threads_shared) { + &threads::shared::share(\$lock); + } +} + + +BASIC: +{ + sub thr_sub + { + lock($lock); + my $self = Thread->self; + return $self->tid; + } + + my $thr; + { + lock($lock); + + $thr = Thread->new(\&thr_sub); + + isa_ok($thr, 'Thread'); + + ok(! $thr->done(), 'Thread running'); + is($thr->tid, 1, "thread's tid"); + + my ($thr2) = Thread->list; + ok($thr2->equal($thr), '->list returned thread'); + } + yield(); + sleep(1); + + ok($thr->done(), 'Thread done'); + is($thr->join(), 1, "->join returned thread's tid"); +} + +ASYNC: +{ + my $thr = async { Thread->self->tid; }; + isa_ok($thr, 'Thread'); + is($thr->tid, 2, "async thread's tid"); + is($thr->join, 2, "->join on async returned tid"); +} + +COND_: +{ + sub thr_wait + { + lock($lock); + cond_wait($lock); + return Thread->self->tid; + } + + my $thr = Thread->new(\&thr_wait); + isa_ok($thr, 'Thread'); + ok(! $thr->done(), 'Thread running'); + + { + lock($lock); + cond_signal($lock); + } + yield(); + sleep(1); + + ok($thr->done(), 'Thread done'); + is($thr->join(), 3, "->join returned thread's tid"); +} + +# EOF |