summaryrefslogtreecommitdiff
path: root/lib/Thread.t
blob: 18694c5b98d1415a6806f77177e8f9b63724520d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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;
    {
        lock($lock);
        $thr = Thread->new(\&thr_wait);
        isa_ok($thr, 'Thread');
        ok(! $thr->done(), 'Thread running');
    }
    yield();
    sleep(1);

    {
        lock($lock);
        cond_signal($lock);
    }
    yield();
    sleep(1);

    ok($thr->done(), 'Thread done');
    is($thr->join(), 3, "->join returned thread's tid");
}

# EOF