line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package threads; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
658
|
use 5.008; |
|
1
|
|
|
|
|
3
|
|
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
20
|
|
6
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
432
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '2.22_01'; # remember to update version in POD! |
9
|
|
|
|
|
|
|
my $XS_VERSION = $VERSION; |
10
|
|
|
|
|
|
|
$VERSION = eval $VERSION; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# Verify this Perl supports threads |
13
|
|
|
|
|
|
|
require Config; |
14
|
|
|
|
|
|
|
if (! $Config::Config{useithreads}) { |
15
|
|
|
|
|
|
|
die("This Perl not built to support threads\n"); |
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# Complain if 'threads' is loaded after 'threads::shared' |
19
|
|
|
|
|
|
|
if ($threads::shared::threads_shared) { |
20
|
|
|
|
|
|
|
warn <<'_MSG_'; |
21
|
|
|
|
|
|
|
Warning, threads::shared has already been loaded. To |
22
|
|
|
|
|
|
|
enable shared variables, 'use threads' must be called |
23
|
|
|
|
|
|
|
before threads::shared or any module that uses it. |
24
|
|
|
|
|
|
|
_MSG_ |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# Declare that we have been loaded |
28
|
|
|
|
|
|
|
$threads::threads = 1; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# Load the XS code |
31
|
|
|
|
|
|
|
require XSLoader; |
32
|
|
|
|
|
|
|
XSLoader::load("threads", $XS_VERSION); |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
### Export ### |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub import |
37
|
|
|
|
|
|
|
{ |
38
|
0
|
|
|
0
|
|
|
my $class = shift; # Not used |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
# Exported subroutines |
41
|
0
|
|
|
|
|
|
my @EXPORT = qw(async); |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
# Handle args |
44
|
0
|
|
|
|
|
|
while (my $sym = shift) { |
45
|
0
|
0
|
|
|
|
|
if ($sym =~ /^(?:stack|exit)/i) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
46
|
0
|
0
|
|
|
|
|
if (defined(my $arg = shift)) { |
47
|
0
|
0
|
|
|
|
|
if ($sym =~ /^stack/i) { |
48
|
0
|
|
|
|
|
|
threads->set_stack_size($arg); |
49
|
|
|
|
|
|
|
} else { |
50
|
0
|
|
|
|
|
|
$threads::thread_exit_only = $arg =~ /^thread/i; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
} else { |
53
|
0
|
|
|
|
|
|
require Carp; |
54
|
0
|
|
|
|
|
|
Carp::croak("threads: Missing argument for option: $sym"); |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
} elsif ($sym =~ /^str/i) { |
58
|
0
|
|
|
|
|
|
import overload ('""' => \&tid); |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
} elsif ($sym =~ /^(?::all|yield)$/) { |
61
|
0
|
|
|
|
|
|
push(@EXPORT, qw(yield)); |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
} else { |
64
|
0
|
|
|
|
|
|
require Carp; |
65
|
0
|
|
|
|
|
|
Carp::croak("threads: Unknown import option: $sym"); |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
# Export subroutine names |
70
|
0
|
|
|
|
|
|
my $caller = caller(); |
71
|
0
|
|
|
|
|
|
foreach my $sym (@EXPORT) { |
72
|
1
|
|
|
1
|
|
9
|
no strict 'refs'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
349
|
|
73
|
0
|
|
|
|
|
|
*{$caller.'::'.$sym} = \&{$sym}; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
# Set stack size via environment variable |
77
|
0
|
0
|
|
|
|
|
if (exists($ENV{'PERL5_ITHREADS_STACK_SIZE'})) { |
78
|
0
|
|
|
|
|
|
threads->set_stack_size($ENV{'PERL5_ITHREADS_STACK_SIZE'}); |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
### Methods, etc. ### |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
# Exit from a thread (only) |
86
|
|
|
|
|
|
|
sub exit |
87
|
|
|
|
|
|
|
{ |
88
|
0
|
|
|
0
|
|
|
my ($class, $status) = @_; |
89
|
0
|
0
|
|
|
|
|
if (! defined($status)) { |
90
|
0
|
|
|
|
|
|
$status = 0; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
# Class method only |
94
|
0
|
0
|
|
|
|
|
if (ref($class)) { |
95
|
0
|
|
|
|
|
|
require Carp; |
96
|
0
|
|
|
|
|
|
Carp::croak('Usage: threads->exit(status)'); |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
0
|
|
|
|
|
|
$class->set_thread_exit_only(1); |
100
|
0
|
|
|
|
|
|
CORE::exit($status); |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
# 'Constant' args for threads->list() |
104
|
|
|
|
0
|
|
|
sub threads::all { } |
105
|
0
|
|
|
0
|
|
|
sub threads::running { 1 } |
106
|
0
|
|
|
0
|
|
|
sub threads::joinable { 0 } |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
# 'new' is an alias for 'create' |
109
|
|
|
|
|
|
|
*new = \&create; |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
# 'async' is a function alias for the 'threads->create()' method |
112
|
|
|
|
|
|
|
sub async (&;@) |
113
|
|
|
|
|
|
|
{ |
114
|
0
|
|
|
0
|
|
|
unshift(@_, 'threads'); |
115
|
|
|
|
|
|
|
# Use "goto" trick to avoid pad problems from 5.8.1 (fixed in 5.8.2) |
116
|
0
|
|
|
|
|
|
goto &create; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
# Thread object equality checking |
120
|
|
|
|
|
|
|
use overload ( |
121
|
|
|
|
|
|
|
'==' => \&equal, |
122
|
0
|
|
|
0
|
|
|
'!=' => sub { ! equal(@_) }, |
123
|
1
|
|
|
|
|
11
|
'fallback' => 1 |
124
|
1
|
|
|
1
|
|
8
|
); |
|
1
|
|
|
|
|
3
|
|
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
1; |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
__END__ |