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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
use strict;
use warnings;
BEGIN {
if ($ENV{'PERL_CORE'}){
chdir 't';
unshift @INC, '../lib';
}
use Config;
if (! $Config{'useithreads'}) {
print("1..0 # Skip: Perl not compiled with 'useithreads'\n");
exit(0);
}
}
use ExtUtils::testlib;
use threads;
BEGIN {
eval {
require threads::shared;
import threads::shared;
};
if ($@ || ! $threads::shared::threads_shared) {
print("1..0 # Skip: threads::shared not available\n");
exit(0);
}
$| = 1;
print("1..5\n"); ### Number of tests that will be run ###
};
my ($TEST, $COUNT, $TOTAL);
BEGIN {
share($TEST);
$TEST = 1;
share($COUNT);
$COUNT = 0;
$TOTAL = 0;
}
ok(1, 'Loaded');
sub ok {
my ($ok, $name) = @_;
lock($TEST);
my $id = $TEST++;
# You have to do it this way or VMS will get confused.
if ($ok) {
print("ok $id - $name\n");
} else {
print("not ok $id - $name\n");
printf("# Failed test at line %d\n", (caller)[2]);
print(STDERR "# FAIL: $name\n") if (! exists($ENV{'PERL_CORE'}));
}
return ($ok);
}
### Start of Testing ###
$SIG{'__WARN__'} = sub { ok(0, "Warning: $_[0]"); };
sub foo { lock($COUNT); $COUNT++; }
sub baz { 42 }
my $bthr;
BEGIN {
$SIG{'__WARN__'} = sub { ok(0, "BEGIN: $_[0]"); };
$TOTAL++;
threads->create('foo')->join();
$TOTAL++;
threads->create(\&foo)->join();
$TOTAL++;
threads->create(sub { lock($COUNT); $COUNT++; })->join();
$TOTAL++;
threads->create('foo')->detach();
$TOTAL++;
threads->create(\&foo)->detach();
$TOTAL++;
threads->create(sub { lock($COUNT); $COUNT++; })->detach();
$bthr = threads->create('baz');
}
my $mthr;
MAIN: {
$TOTAL++;
threads->create('foo')->join();
$TOTAL++;
threads->create(\&foo)->join();
$TOTAL++;
threads->create(sub { lock($COUNT); $COUNT++; })->join();
$TOTAL++;
threads->create('foo')->detach();
$TOTAL++;
threads->create(\&foo)->detach();
$TOTAL++;
threads->create(sub { lock($COUNT); $COUNT++; })->detach();
$mthr = threads->create('baz');
}
ok($mthr, 'Main thread');
ok($bthr, 'BEGIN thread');
ok($mthr->join() == 42, 'Main join');
ok($bthr->join() == 42, 'BEGIN join');
# Wait for detached threads to finish
{
threads->yield();
sleep(1);
lock($COUNT);
redo if ($COUNT < $TOTAL);
}
# EOF
|