xref: /openbsd/gnu/usr.bin/perl/lib/Thread.t (revision 256a93a4)
1850e2753Smillertuse strict;
2850e2753Smillertuse warnings;
3850e2753Smillert
4850e2753SmillertBEGIN {
5850e2753Smillert    chdir 't' if -d 't';
6850e2753Smillert    @INC = '../lib';
7850e2753Smillert
8850e2753Smillert    use Config;
9850e2753Smillert    if (! $Config{usethreads}) {
10850e2753Smillert        print("1..0 # Skip: No threads\n");
11850e2753Smillert        exit(0);
12850e2753Smillert    }
13850e2753Smillert}
14850e2753Smillert
15850e2753Smillertuse Thread qw(:DEFAULT async yield);
16850e2753Smillert
17*256a93a4Safresh1use Test::More;
18850e2753Smillert
19850e2753Smillertmy $lock;
20850e2753Smillert{
21850e2753Smillert    no warnings 'once';
22850e2753Smillert    if ($threads::shared::threads_shared) {
23850e2753Smillert        &threads::shared::share(\$lock);
24850e2753Smillert    }
25850e2753Smillert}
26850e2753Smillert
27850e2753Smillert
28850e2753SmillertBASIC:
29850e2753Smillert{
30850e2753Smillert    sub thr_sub
31850e2753Smillert    {
32850e2753Smillert        lock($lock);
33850e2753Smillert        my $self = Thread->self;
34850e2753Smillert        return $self->tid;
35850e2753Smillert    }
36850e2753Smillert
37850e2753Smillert    my $thr;
38850e2753Smillert    {
39850e2753Smillert        lock($lock);
40850e2753Smillert
41850e2753Smillert        $thr = Thread->new(\&thr_sub);
42850e2753Smillert
43850e2753Smillert        isa_ok($thr, 'Thread');
44850e2753Smillert
45850e2753Smillert        ok(! $thr->done(), 'Thread running');
46850e2753Smillert        is($thr->tid, 1, "thread's tid");
47850e2753Smillert
48850e2753Smillert        my ($thr2) = Thread->list;
49850e2753Smillert        ok($thr2->equal($thr), '->list returned thread');
50850e2753Smillert    }
51850e2753Smillert    yield();
52850e2753Smillert    sleep(1);
53850e2753Smillert
54850e2753Smillert    ok($thr->done(), 'Thread done');
55850e2753Smillert    is($thr->join(), 1, "->join returned thread's tid");
56850e2753Smillert}
57850e2753Smillert
58850e2753SmillertASYNC:
59850e2753Smillert{
60850e2753Smillert    my $thr = async { Thread->self->tid; };
61850e2753Smillert    isa_ok($thr, 'Thread');
62850e2753Smillert    is($thr->tid, 2, "async thread's tid");
63850e2753Smillert    is($thr->join, 2, "->join on async returned tid");
64850e2753Smillert}
65850e2753Smillert
66850e2753SmillertCOND_:
67850e2753Smillert{
68850e2753Smillert    sub thr_wait
69850e2753Smillert    {
70850e2753Smillert        lock($lock);
71850e2753Smillert        cond_wait($lock);
72850e2753Smillert        return Thread->self->tid;
73850e2753Smillert    }
74850e2753Smillert
75850e2753Smillert    my $thr;
76850e2753Smillert    {
77850e2753Smillert        lock($lock);
78850e2753Smillert        $thr = Thread->new(\&thr_wait);
79850e2753Smillert        isa_ok($thr, 'Thread');
80850e2753Smillert        ok(! $thr->done(), 'Thread running');
81850e2753Smillert    }
82850e2753Smillert    yield();
83850e2753Smillert    sleep(1);
84850e2753Smillert
85850e2753Smillert    {
86850e2753Smillert        lock($lock);
87850e2753Smillert        cond_signal($lock);
88850e2753Smillert    }
89850e2753Smillert    yield();
90850e2753Smillert    sleep(1);
91850e2753Smillert
92850e2753Smillert    ok($thr->done(), 'Thread done');
93850e2753Smillert    is($thr->join(), 3, "->join returned thread's tid");
94850e2753Smillert}
95850e2753Smillert
96*256a93a4Safresh1done_testing();
97