line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
############################################################################### |
2
|
|
|
|
|
|
|
## ---------------------------------------------------------------------------- |
3
|
|
|
|
|
|
|
## MCE::Mutex::Flock - Mutex locking via Fcntl. |
4
|
|
|
|
|
|
|
## |
5
|
|
|
|
|
|
|
############################################################################### |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
package MCE::Mutex::Flock; |
8
|
|
|
|
|
|
|
|
9
|
2
|
|
|
2
|
|
980
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
98
|
|
10
|
2
|
|
|
2
|
|
11
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
58
|
|
11
|
|
|
|
|
|
|
|
12
|
2
|
|
|
2
|
|
10
|
no warnings qw( threads recursion uninitialized once ); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
103
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $VERSION = '1.889'; |
15
|
|
|
|
|
|
|
|
16
|
2
|
|
|
2
|
|
10
|
use base 'MCE::Mutex'; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
224
|
|
17
|
2
|
|
|
2
|
|
15
|
use Fcntl ':flock'; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
295
|
|
18
|
2
|
|
|
2
|
|
15
|
use Scalar::Util 'looks_like_number'; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
143
|
|
19
|
2
|
|
|
2
|
|
527
|
use Time::HiRes 'alarm'; |
|
2
|
|
|
|
|
1363
|
|
|
2
|
|
|
|
|
10
|
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
my $tid = $INC{'threads.pm'} ? threads->tid() : 0; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub CLONE { |
24
|
0
|
0
|
|
0
|
|
0
|
$tid = threads->tid() if $INC{'threads.pm'}; |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub MCE::Mutex::Flock::_guard::DESTROY { |
28
|
0
|
|
|
0
|
|
0
|
my ($pid, $obj) = @{ $_[0] }; |
|
0
|
|
|
|
|
0
|
|
29
|
0
|
0
|
|
|
|
0
|
CORE::flock ($obj->{_fh}, LOCK_UN), $obj->{ $pid } = 0 if $obj->{ $pid }; |
30
|
|
|
|
|
|
|
|
31
|
0
|
|
|
|
|
0
|
return; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub DESTROY { |
35
|
2
|
50
|
|
2
|
|
1336
|
my ($pid, $obj) = ($tid ? $$ .'.'. $tid : $$, @_); |
36
|
2
|
50
|
|
|
|
7
|
$obj->unlock(), close(delete $obj->{_fh}) if $obj->{ $pid }; |
37
|
2
|
100
|
66
|
|
|
70
|
unlink $obj->{path} if ($obj->{_init} && $obj->{_init} eq $pid); |
38
|
|
|
|
|
|
|
|
39
|
2
|
|
|
|
|
13
|
return; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub _open { |
43
|
0
|
0
|
|
0
|
|
0
|
my ($pid, $obj) = ($tid ? $$ .'.'. $tid : $$, @_); |
44
|
0
|
0
|
|
|
|
0
|
return if exists $obj->{ $pid }; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
open $obj->{_fh}, '+>>:raw:stdio', $obj->{path} |
47
|
0
|
0
|
|
|
|
0
|
or Carp::croak("Could not create temp file $obj->{path}: $!"); |
48
|
|
|
|
|
|
|
|
49
|
0
|
|
|
|
|
0
|
return; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
############################################################################### |
53
|
|
|
|
|
|
|
## ---------------------------------------------------------------------------- |
54
|
|
|
|
|
|
|
## Public methods. |
55
|
|
|
|
|
|
|
## |
56
|
|
|
|
|
|
|
############################################################################### |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
my ($id, $prog_name) = (0); |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
$prog_name = $0; |
61
|
|
|
|
|
|
|
$prog_name =~ s{^.*[\\/]}{}g; |
62
|
|
|
|
|
|
|
$prog_name = 'perl' if ($prog_name eq '-e' || $prog_name eq '-'); |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub new { |
65
|
2
|
|
|
2
|
1
|
9
|
my ($class, %obj) = (@_, impl => 'Flock'); |
66
|
|
|
|
|
|
|
|
67
|
2
|
100
|
|
|
|
5
|
if (! defined $obj{path}) { |
68
|
1
|
|
|
|
|
5
|
my ($pid, $tmp_dir, $tmp_file) = ( abs($$) ); |
69
|
|
|
|
|
|
|
|
70
|
1
|
50
|
33
|
|
|
35
|
if ($ENV{TEMP} && -d $ENV{TEMP} && -w _) { |
|
|
50
|
33
|
|
|
|
|
|
|
50
|
33
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
33
|
|
|
|
|
71
|
0
|
0
|
|
|
|
0
|
if ($^O =~ /mswin|mingw|msys|cygwin/i) { |
72
|
0
|
|
|
|
|
0
|
$tmp_dir = $ENV{TEMP}; |
73
|
0
|
0
|
|
|
|
0
|
$tmp_dir .= ($^O eq 'MSWin32') ? "\\Perl-MCE" : "/Perl-MCE"; |
74
|
0
|
0
|
|
|
|
0
|
mkdir $tmp_dir unless (-d $tmp_dir); |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
else { |
77
|
0
|
|
|
|
|
0
|
$tmp_dir = $ENV{TEMP}; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
elsif ($ENV{TMPDIR} && -d $ENV{TMPDIR} && -w _) { |
81
|
0
|
|
|
|
|
0
|
$tmp_dir = $ENV{TMPDIR}; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
elsif (-d '/tmp' && -w _) { |
84
|
1
|
|
|
|
|
4
|
$tmp_dir = '/tmp'; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
else { |
87
|
0
|
|
|
|
|
0
|
Carp::croak("No writable dir found for a temp file"); |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
1
|
|
|
|
|
3
|
$id++, $tmp_dir =~ s{[\\/]$}{}; |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
# remove tainted'ness from $tmp_dir |
93
|
1
|
50
|
|
|
|
4
|
if ($^O eq 'MSWin32') { |
94
|
0
|
|
|
|
|
0
|
($tmp_file) = "$tmp_dir\\$prog_name.$pid.$tid.$id" =~ /(.*)/; |
95
|
|
|
|
|
|
|
} else { |
96
|
1
|
|
|
|
|
7
|
($tmp_file) = "$tmp_dir/$prog_name.$pid.$tid.$id" =~ /(.*)/; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
1
|
50
|
|
|
|
5
|
$obj{_init} = $tid ? $$ .'.'. $tid : $$; |
100
|
1
|
|
|
|
|
3
|
$obj{ path} = $tmp_file.'.lock'; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# test open |
103
|
|
|
|
|
|
|
open my $fh, '+>>:raw:stdio', $obj{path} |
104
|
1
|
50
|
|
|
|
127
|
or Carp::croak("Could not create temp file $obj{path}: $!"); |
105
|
|
|
|
|
|
|
|
106
|
1
|
|
|
|
|
21
|
close $fh; |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
# set permission |
109
|
1
|
|
|
|
|
27
|
chmod 0600, $obj{path}; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
else { |
112
|
|
|
|
|
|
|
# test open |
113
|
|
|
|
|
|
|
open my $fh, '+>>:raw:stdio', $obj{path} |
114
|
1
|
50
|
|
|
|
84
|
or Carp::croak("Could not obtain flock on file $obj{path}: $!"); |
115
|
|
|
|
|
|
|
|
116
|
1
|
|
|
|
|
17
|
close $fh; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
2
|
|
|
|
|
19
|
return bless(\%obj, $class); |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
sub lock { |
123
|
0
|
0
|
|
0
|
1
|
|
my ($pid, $obj) = ($tid ? $$ .'.'. $tid : $$, shift); |
124
|
0
|
0
|
|
|
|
|
$obj->_open() unless exists $obj->{ $pid }; |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
CORE::flock ($obj->{_fh}, LOCK_EX), $obj->{ $pid } = 1 |
127
|
0
|
0
|
|
|
|
|
unless $obj->{ $pid }; |
128
|
|
|
|
|
|
|
|
129
|
0
|
|
|
|
|
|
return; |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
sub guard_lock { |
133
|
0
|
|
|
0
|
1
|
|
&lock(@_); |
134
|
0
|
0
|
|
|
|
|
bless([ $tid ? $$ .'.'. $tid : $$, $_[0] ], MCE::Mutex::Flock::_guard::); |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
*lock_exclusive = \&lock; |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
sub lock_shared { |
140
|
0
|
0
|
|
0
|
1
|
|
my ($pid, $obj) = ($tid ? $$ .'.'. $tid : $$, shift); |
141
|
0
|
0
|
|
|
|
|
$obj->_open() unless exists $obj->{ $pid }; |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
CORE::flock ($obj->{_fh}, LOCK_SH), $obj->{ $pid } = 1 |
144
|
0
|
0
|
|
|
|
|
unless $obj->{ $pid }; |
145
|
|
|
|
|
|
|
|
146
|
0
|
|
|
|
|
|
return; |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
sub unlock { |
150
|
0
|
0
|
|
0
|
1
|
|
my ($pid, $obj) = ($tid ? $$ .'.'. $tid : $$, shift); |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
CORE::flock ($obj->{_fh}, LOCK_UN), $obj->{ $pid } = 0 |
153
|
0
|
0
|
|
|
|
|
if $obj->{ $pid }; |
154
|
|
|
|
|
|
|
|
155
|
0
|
|
|
|
|
|
return; |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
sub synchronize { |
159
|
0
|
0
|
|
0
|
1
|
|
my ($pid, $obj, $code) = ($tid ? $$ .'.'. $tid : $$, shift, shift); |
160
|
0
|
|
|
|
|
|
my (@ret); |
161
|
|
|
|
|
|
|
|
162
|
0
|
0
|
|
|
|
|
return unless ref($code) eq 'CODE'; |
163
|
|
|
|
|
|
|
|
164
|
0
|
0
|
|
|
|
|
$obj->_open() unless exists $obj->{ $pid }; |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
# lock, run, unlock - inlined for performance |
167
|
0
|
|
|
|
|
|
my $guard = bless([ $pid, $obj ], MCE::Mutex::Flock::_guard::); |
168
|
0
|
0
|
|
|
|
|
unless ($obj->{ $pid }) { |
169
|
0
|
|
|
|
|
|
CORE::flock ($obj->{_fh}, LOCK_EX), $obj->{ $pid } = 1; |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
(defined wantarray) |
172
|
0
|
0
|
|
|
|
|
? @ret = wantarray ? $code->(@_) : scalar $code->(@_) |
|
|
0
|
|
|
|
|
|
173
|
|
|
|
|
|
|
: $code->(@_); |
174
|
|
|
|
|
|
|
|
175
|
0
|
0
|
|
|
|
|
return wantarray ? @ret : $ret[-1]; |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
*enter = \&synchronize; |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
sub timedwait { |
181
|
0
|
|
|
0
|
1
|
|
my ($obj, $timeout) = @_; |
182
|
0
|
0
|
|
|
|
|
die 'MCE::Mutex::Flock::timedwait() unimplemented in this platform' |
183
|
|
|
|
|
|
|
if ($^O eq 'MSWin32'); |
184
|
|
|
|
|
|
|
|
185
|
0
|
0
|
|
|
|
|
$timeout = 1 unless defined $timeout; |
186
|
0
|
0
|
0
|
|
|
|
Carp::croak('MCE::Mutex: timedwait (timeout) is not valid') |
187
|
|
|
|
|
|
|
if (!looks_like_number($timeout) || $timeout < 0); |
188
|
|
|
|
|
|
|
|
189
|
0
|
0
|
|
|
|
|
$timeout = 0.0003 if $timeout < 0.0003; |
190
|
|
|
|
|
|
|
|
191
|
0
|
|
|
0
|
|
|
local $@; local $SIG{ALRM} = sub { alarm 0; die "timed out\n" }; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
192
|
0
|
|
|
|
|
|
eval { alarm $timeout; $obj->lock_exclusive }; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
193
|
0
|
|
|
|
|
|
alarm 0; |
194
|
|
|
|
|
|
|
|
195
|
0
|
0
|
0
|
|
|
|
( $@ && $@ eq "timed out\n" ) ? '' : 1; |
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
1; |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
__END__ |