File Coverage

blib/lib/Dir/Flock.pm
Criterion Covered Total %
statement 152 232 65.5
branch 43 110 39.0
condition 16 39 41.0
subroutine 25 32 78.1
pod 12 12 100.0
total 248 425 58.3


line stmt bran cond sub pod time code
1             package Dir::Flock;
2 14     14   892385 use strict;
  14         136  
  14         403  
3 14     14   70 use warnings;
  14         23  
  14         337  
4 14     14   105 use Carp;
  14         26  
  14         908  
5 14     14   7695 use Time::HiRes;
  14         19626  
  14         58  
6 14     14   12021 use File::Temp;
  14         294091  
  14         994  
7 14     14   111 use Fcntl ':flock';
  14         22  
  14         1568  
8 14     14   8869 use Data::Dumper; # when debugging is on
  14         94305  
  14         930  
9 14     14   104 use base 'Exporter';
  14         27  
  14         39181  
10              
11             our $VERSION = '0.08';
12             my %TMPFILE;
13             my %LOCK;
14             our @EXPORT_OK = qw(getDir lock lock_ex lock_sh unlock lockobj lockobj_ex
15             lockobj_sh sync sync_ex sync_sh);
16             our %EXPORT_TAGS = ('all' => \@EXPORT_OK);
17             our $errstr;
18              
19             # configuration that may be updated by user
20             our $LOCKFILE_STUB = "dir-flock-";
21             our $PAUSE_LENGTH = 0.001; # seconds
22             our $HEARTBEAT_CHECK = 30; # seconds
23             our $_DEBUG = $ENV{DEBUG} || $ENV{DIR_FLOCK_DEBUG} || 0;
24              
25             sub getDir {
26 10     10 1 20003786 my $rootdir = shift;
27 10 50 33     470 if (-f $rootdir && ! -d $rootdir) {
28 0         0 require Cwd;
29 0         0 require File::Basename;
30 0         0 $rootdir = File::Basename::dirname(Cwd::abs_path($rootdir));
31             }
32 10         132 my $tmpdir = File::Temp::tempdir(
33             TEMPLATE => "dflock-XXXXXX",
34             DIR => $rootdir, CLEANUP => 1 );
35 10         8124 $tmpdir;
36             }
37              
38             ### core functions
39              
40 8     8 1 335061 sub lock { goto &lock_ex }
41              
42             sub lock_ex {
43 15     15 1 4755 my ($dir, $timeout) = @_;
44 15         204 $errstr = "";
45 15 50       203 return if !_validate_dir($dir);
46 15   33     136 my $P = $_DEBUG && _pid();
47 15         159 my ($filename,$now) = _create_tempfile( $dir, "excl" );
48 15         61 my $last_check = $now;
49 15   100     329 my $expire = $now + ($timeout || 0);
50 15         146 $TMPFILE{ $filename } = _pid();
51 15         130 while (_oldest_file($dir) ne $filename) {
52 13074         645169 unlink $filename;
53 13074         70474 delete $TMPFILE{$filename};
54 13074 50       34008 $P && print STDERR "$P $filename not the oldest file...\n";
55 13074 100 100     41813 if (defined($timeout) && Time::HiRes::time > $expire) {
56 5         59 $errstr = "timeout waiting for exclusive lock for '$dir'";
57 5 50       43 $P && print STDERR "$P timeout waiting for lock\n";
58 5         28 return;
59             }
60 13069         27934514 Time::HiRes::sleep $PAUSE_LENGTH * (1 + 2 * rand());
61 13069 100       144059 if (Time::HiRes::time > $last_check + $HEARTBEAT_CHECK) {
62 1 50       7 $P && print STDERR "$P check heartbeat of lock holder\n";
63 1         46 _ping_oldest_file($dir);
64 1         6 $last_check = Time::HiRes::time;
65             }
66 13069         44412 ($filename,$now) = _create_tempfile( $dir, "excl" );
67 13069         50352 $TMPFILE{ $filename } = _pid();
68             }
69 10 50       81 $P && print STDERR "$P lock successful to $filename\n";
70 10         88 $LOCK{$dir}{_pid()} = $filename;
71 10         88 1;
72             }
73              
74             sub lock_sh {
75 4     4 1 23492 my ($dir, $timeout) = @_;
76 4         88 $errstr = "";
77 4 50       123 return if !_validate_dir($dir);
78 4   33     45 my $P = $_DEBUG && _pid();
79 4         59 my ($filename,$now) = _create_tempfile( $dir, "shared" );
80 4         18 my $last_check = $now;
81 4   50     78 my $expire = $now + ($timeout || 0);
82 4         22 $TMPFILE{ $filename } = _pid();
83 4         48 while (_oldest_file($dir) =~ /_excl_/) {
84 3907         193172 unlink $filename;
85 3907         23069 delete $TMPFILE{$filename};
86 3907 50       12058 $P && print STDERR "$P $filename not the oldest file...\n";
87 3907 100 66     12623 if (defined($timeout) && Time::HiRes::time > $expire) {
88 1         6 $errstr = "timeout waiting for exclusive lock for '$dir'";
89 1 50       4 $P && print STDERR "$P timeout waiting for lock\n";
90 1         4 return;
91             }
92 3906         8309674 Time::HiRes::sleep $PAUSE_LENGTH * (1 + 2 * rand());
93 3906 50       48803 if (Time::HiRes::time > $last_check + $HEARTBEAT_CHECK) {
94 0 0       0 $P && print STDERR "$P check heartbeat of lock holder\n";
95 0         0 _ping_oldest_file($dir);
96 0         0 $last_check = Time::HiRes::time;
97             }
98 3906         15049 ($filename,$now) = _create_tempfile( $dir, "shared" );
99 3906         15586 $TMPFILE{ $filename } = _pid();
100             }
101 3 50       16 $P && print STDERR "$P lock successful to $filename\n";
102 3         19 $LOCK{$dir}{_pid()} = $filename;
103 3         32 1;
104             }
105              
106             ### flock semantics
107              
108             sub flock {
109 5     5 1 10012419 my ($dir, $op) = @_;
110 5         42 my $timeout = undef;
111 5 100       88 if ($op & LOCK_NB) {
112 1         8 $timeout = 0;
113             }
114 5 100       83 if ($op & LOCK_EX) {
115 3         68 return lock_ex($dir,$timeout);
116             }
117 2 50       13 if ($op & LOCK_SH) {
118 0         0 return lock_sh($dir,$timeout);
119             }
120 2 50       11 if ($op & LOCK_UN) {
121 2         13 return unlock($dir);
122             }
123 0         0 $errstr = "invalid flock operation '$op'";
124 0         0 carp "Dir::Flock::flock: invalid operation";
125 0         0 return;
126             }
127              
128             sub unlock {
129 14     14 1 30013180 my ($dir) = @_;
130 14 100       112 if (!defined $LOCK{$dir}) {
131 1 50       165 return if __inGD();
132 1         5 $errstr = "lock for '$dir' not held by " . _pid()
133             . " nor any proc";
134 1         576 carp "Dir::Flock::unlock: $errstr";
135 1         7 return;
136             }
137 13         71 my $filename = delete $LOCK{$dir}{_pid()};
138 13 50       83 if (!defined($filename)) {
139 0 0       0 return if __inGD();
140 0         0 $errstr = "lock for '$dir' not held by " . _pid();
141 0         0 carp "Dir::Flock::unlock: $errstr";
142 0         0 return;
143             }
144 13 50       96 $_DEBUG && print STDERR _pid()," unlocking $filename\n";
145 13 50       335 if (! -f $filename) {
146 0 0       0 return if __inGD();
147 0         0 $errstr = "lock file '$filename' is missing";
148             carp "Dir::Flock::unlock: lock file is missing ",
149 0         0 %{$LOCK{$dir}};
  0         0  
150 0         0 return;
151             }
152 13         778 my $z = unlink($filename);
153 13 50       81 if ($z) {
154 13 50       77 $_DEBUG && print STDERR _pid()," deleted $filename\n";
155 13         78 delete $TMPFILE{$filename};
156 13         91 return $z;
157             }
158 0 0       0 return if __inGD();
159 0         0 $errstr = "unlink called failed on file '$filename'";
160 0         0 carp "Dir::Flock::unlock: failed to unlink lock file ",
161             "'$filename'";
162 0         0 return; # this could be bad
163             }
164              
165             ### scope semantics
166              
167 0     0 1 0 sub lockobj { goto &lockobj_ex }
168              
169             sub lockobj_ex {
170 0     0 1 0 my ($dir, $timeout) = @_;
171 0         0 my $ok = lock_ex($dir,$timeout);
172 0 0       0 return if !$ok;
173 0         0 return bless \$dir, 'Dir::Flock::SyncObject2';
174             }
175              
176             sub lockobj_sh {
177 0     0 1 0 my ($dir, $timeout) = @_;
178 0         0 my $ok = lock_sh($dir,$timeout);
179 0 0       0 return if !$ok;
180 0         0 return bless \$dir, 'Dir::Flock::SyncObject2';
181             }
182              
183             sub Dir::Flock::SyncObject2::DESTROY {
184 0     0   0 my $self = shift;
185 0         0 my $dir = $$self;
186 0         0 my $ok = unlock($dir);
187 0 0 0     0 if (!$ok && !__inGD()) {
188             # $errstr set in unlock
189 0         0 carp "unlock: failed for dir '$dir' as sync object went out of scope";
190             }
191 0         0 return;
192             }
193              
194             ### block semantics
195              
196 0     0 1 0 sub sync (&$;$) { goto &sync_ex }
197              
198             sub sync_ex (&$;$) {
199 0     0 1 0 my ($code, $dir, $timeout) = @_;
200 0 0       0 if (!lock_ex($dir,$timeout)) {
201             # $errstr set in lock_ex
202 0         0 return;
203             }
204 0         0 my @r;
205 0 0       0 if (wantarray) {
206 0         0 @r = eval { $code->() };
  0         0  
207             } else {
208 0         0 $r[0] = eval { $code->() };
  0         0  
209             }
210 0         0 unlock($dir);
211 0 0       0 if ($@) {
212 0         0 $errstr = "error from sync_ex BLOCK: $@";
213 0         0 die $@;
214             }
215 0 0       0 wantarray ? @r : $r[0];
216             }
217              
218             sub sync_sh (&$;$) {
219 0     0 1 0 my ($code, $dir, $timeout) = @_;
220 0 0       0 if (!lock_sh($dir,$timeout)) {
221             # $errstr set in lock_sh
222 0         0 return;
223             }
224 0         0 my @r;
225 0 0       0 if (wantarray) {
226 0         0 @r = eval { $code->() };
  0         0  
227             } else {
228 0         0 $r[0] = eval { $code->() };
  0         0  
229             }
230 0         0 unlock($dir);
231 0 0       0 if ($@){
232 0         0 $errstr = "error from sync_sh BLOCK: $@";
233 0         0 die $@;
234             }
235 0 0       0 wantarray ? @r : $r[0];
236             }
237              
238             ### utilities
239              
240             sub _host {
241             $ENV{HOSTNAME} || ($^O eq 'MSWin32' && $ENV{COMPUTERNAME})
242 34030 0 0 34030   150916 || "localhost";
      33        
243             }
244              
245             sub _pid {
246 34029     34029   68873 my $host = _host();
247 34029 50       446952 join("_", $host, $$, $INC{"threads.pm"} ? threads->tid : ());
248             }
249              
250             sub _create_tempfile {
251 16994     16994   68928 my ($dir,$type) = @_;
252 16994   50     43431 $type ||= "excl";
253 16994         40840 my $now = Time::HiRes::time;
254 16994         56554 my $file = sprintf "$dir/%s_%s_%s_%s", $LOCKFILE_STUB,
255             $now, $type, _pid();
256 16994         1550850 open my $fh, ">>", $file;
257 16994         383063 return ($file,$now);
258             }
259              
260             sub _oldest_file {
261 16995     16995   44444 my ($dir, $excl) = @_;
262 16995         25071 my $dh;
263 16995         42737 _refresh_dir($dir); # is this necessary? is this sufficient?
264 16995         1413126 my @f1 = sort glob("$dir/$LOCKFILE_STUB*");
265 16995 50       87663 if ($excl) {
266 0         0 @f1 = grep /_excl_/, @f1;
267             }
268 16995 50       130010 @f1 > 0 && $f1[0];
269             }
270              
271             sub _ping_oldest_file {
272 1     1   9 my ($dir,$excl) = @_;
273 1         9 my $file = _oldest_file($dir,$excl);
274 1 50       11 return unless $file;
275 1         7 my $file0 = $file;
276 1         86 $file0 =~ s/.*$LOCKFILE_STUB.//;
277 1         15 my ($time, $type, $host, $pid, $tid) = split /_/, $file0;
278 1         34 $pid =~ s/\D+$//;
279 1 50       18 $_DEBUG && print STDERR _pid(), ": ping host=$host pid=$pid tid=$tid\n";
280 1 50       12 $_DEBUG && print STDERR "$dir holds:\n", join(" ",glob("$dir/*")),"\n";
281 1         4 my $status;
282              
283              
284             # TODO: what if $tid is defined? How do you signal a thread
285             # and how do you signal or terminate a remote thread?
286            
287 1 50 33     8 if ($host eq _host() || $host eq 'localhost') {
288             # TODO: more robust way to inspect process on local machine.
289             # kill 'ZERO',... can mislead for a number of reasons, such as
290             # if the process is owned by a different user.
291 1         29 $status = kill 'ZERO', $pid;
292 1 50       62 $_DEBUG && print STDERR _pid(), " local kill ZERO => $pid: $status\n";
293             } else {
294             # TODO: need a more robust way to inspect a process on a remote machine
295 0         0 my $c1 = system("ssh $host kill -0 $pid");
296 0         0 $status = ($c1 == 0);
297 0 0       0 $_DEBUG && print STDERR _pid(),
298             " remote kill ZERO => $host:$pid: $status\n";
299             }
300 1 50       10 if (! $status) {
301 1         89 warn "Dir::Flock: lock holder that created $file appears dead\n";
302 1         95 unlink $file;
303             }
304             }
305              
306             sub _refresh_dir {
307             # https://stackoverflow.com/a/30630912
308             # "Within a given process, calling opendir and closedir on the
309             # parent directory of a file invalidates the NFS cache."
310 17014     17014   33001 my $dir = shift;
311 17014         25707 my $dh;
312 17014         505558 opendir $dh, $dir;
313 17014         192671 closedir $dh;
314 17014         96715 return;
315             }
316              
317             sub _validate_dir {
318 19     19   189 my $dir = shift;
319 19 50       739 if (! -d $dir) {
320 0         0 $errstr = "lock dir '$dir' is not a directory";
321 0         0 carp "Dir::Flock::lock: $errstr";
322 0         0 return;
323             }
324 19 0 33     561 if (! -r $dir && -w $dir && -x $dir) {
      33        
325 0         0 $errstr = "lock dir '$dir' is not an accessible directory";
326 0         0 carp "Dir::Flock::lock: $errstr";
327 0         0 return;
328             }
329 19         182 _refresh_dir($dir);
330 19         133 1;
331             }
332              
333             BEGIN {
334 14 50   14   112 if (defined(${^GLOBAL_PHASE})) {
335 14 50       1406 eval 'sub __inGD(){%{^GLOBAL_PHASE} eq q{DESTRUCT} && __END()};1'
336             } else {
337 0         0 require B;
338 0         0 eval 'sub __inGD(){${B::main_cv()}==0 && __END()};1'
339             }
340             }
341              
342             END {
343 14     13   25105 my $p = _pid();
344 14     14   96 no warnings 'redefine';
  14         32  
  14         2592  
345 13     1   410 *DB::DB = sub {};
346 13         306 *__inGD = sub () { 1 };
347 13         310 unlink grep{ $TMPFILE{$_} eq $p } keys %TMPFILE;
  0            
348             }
349              
350             1;
351              
352             =head1 NAME
353              
354             Dir::Flock - advisory locking of a dedicated directory
355              
356              
357              
358             =head1 VERSION
359              
360             0.08
361              
362              
363              
364             =head1 SYNOPSIS
365              
366             use Dir::Flock;
367             my $dir = Dir::Flock::getDir("/home/mob/projects/foo");
368             my $success = Dir::Flock::lock($dir);
369             # ... synchronized code
370             $success = Dir::Flock::unlock($dir);
371              
372             # flock semantics
373             use Fcntl ':flock';
374             $success = Dir::Flock::flock($dir, LOCK_EX | LOCK_NB);
375             ...
376             Dir::Flock::flock($dir, LOCK_UN);
377              
378             # mutex/scoping semantics
379             {
380             my $lock = Dir::Flock::lockobj($dir);
381             ... synchronized code ...
382             } # lock released when $lock goes out of scope
383              
384             # code ref semantics
385             Dir::Flock::sync {
386             ... synchronized code ...
387             }, $dir
388              
389              
390              
391             =head1 DESCRIPTION
392              
393             C implements advisory locking of a directory.
394             The use case is to execute synchronized code (code that should
395             only be executed by one process or thread at a time) or provide
396             exclusive access to a file or other resource. C has
397             more overhead than some of the other synchronization techniques
398             available to Perl programmers, but it might be the only technique
399             that works on NFS (Networked File System).
400              
401             =head2 Algorithm
402              
403             File locking is difficult on NFS because, as I understand it, each
404             node maintains a cache that includes file contents and file metadata.
405             When a system call wants to check whether a lock exists on a file,
406             the filesystem driver might inspect the cached file rather than
407             the file on the server, and it might miss an action taken by another
408             node to lock a file.
409              
410             The cache is not used, again, as I understand it, when the filesystem
411             driver reads a directory. If advisory locking is accomplished through
412             reading the contents of a directory, it will not be affected by NFS's
413             caching behavior.
414              
415             To acquire a lock in a directory, this module writes a small file
416             into the directory. Then it checks if this new file is the "oldest"
417             file in the directory. If it is the oldest file, then the process
418             has acquired the lock. If there is already an older file in the
419             directory, than that file specifies what process has a lock on the
420             directory, and we have to wait and try again later. To unlock the
421             directory, the module simply deletes the file in the directory
422             that represents its lock.
423              
424             =head2 Semantics
425              
426             This module offers several different semantics for advisory
427             locking of a directory.
428              
429             =head3 functional semantics
430              
431             The core L and
432             L functions begin and end advisory
433             locking on a directory. All of the other semantics are implemented in
434             terms of these functions.
435              
436             $ok = Dir::Flock::lock( "/some/path" );
437             $ok = Dir::Flock::lock( "/some/path", $timeout );
438             $ok = Dir::Flock::unlock( "/some/path" );
439              
440             =head3 flock semantics
441              
442             The function L emulates the Perl
443             L builtin, accepting the same arguments
444             for the operation argument.
445              
446             use Fcntl ':flock';
447             $ok = Dir::Flock::flock( "/some/path", LOCK_EX );
448             ...
449             $ok = Dir::Flock::flock( "/some/path", LOCK_UN );
450              
451             =head3 scope-oriented semantics
452              
453             The L function returns an
454             object representing a directory lock. The lock is released
455             when the object goes out of scope.
456              
457             {
458             my $lock = Dir::Flock::lockobj( "/some/path" );
459             ...
460             } # $lock out of scope, lock released
461              
462             =head3 BLOCK semantics
463              
464             The L accepts a block of code or other
465             code reference, to be executed with an advisory lock on a
466             directory.
467              
468             Dir::Flock::sync {
469             ... synchronized code ...
470             } "/some/path";
471              
472              
473             =head1 FUNCTIONS
474              
475             Most functions return a false value and set the package variable
476             C<$Dir::Flock::errstr> if they are unsuccessful.
477              
478              
479             =head2 lock
480              
481             =head2 lock_ex
482              
483             =head2 $success = Dir::Flock::lock( $directory [, $timeout ] )
484              
485             =head2 $success = Dir::Flock::lock_ex( $directory [, $timeout ] )
486              
487             Attempts to obtain an I lock on the given directory. While
488             the directory is locked, the C or C call on the
489             same directory from
490             other processes or threads will block until the directory is unlocked
491             (see L<"unlock">). Returns true if the lock was successfully acquired.
492              
493             If an optional C<$timeout> argument is provided, the function will
494             try for at least C<$timeout> seconds to acquire the lock, and return
495             a false value if it is not successful in that time. Use a timeout of
496             zero to make a "non-blocking" exclusive lock request.
497              
498              
499             =head2 lock_sh
500              
501             =head2 $success = Dir::Flock::lock_sh( $directory [, $timeout ] )
502              
503             Attempts to obtain a I lock on the given directory.
504             While there are shared locks on a directory, other calls to C
505             may also receive a shared lock on the directory but calls to
506             C/C on the directory will block until all
507             shared locks are removed.
508              
509             If an optional C<$timeout> argument is provided, the function will
510             try for at least C<$timeout> seconds to acquire the lock, and
511             return a false value if it is not successful in that time.
512             Use a timeout of zero to make a "non-blocking" shared lock request.
513              
514              
515             =head2 unlock
516              
517             =head2 $success = Dir::Flock::unlock( $directory )
518              
519             Releases the exclusive or shared lock on the given directory held
520             by this process. Returns a false value if the current process did
521             not possess the lock on the directory.
522              
523              
524             =head2 getDir
525              
526             =head2 $tmp_directory = Dir::Flock::getDir( $root )
527              
528             Creates a temporary and empty directory in a subdirectory of C<$root>
529             that is suitable for use as a synchronization directory. The directory
530             will automatically be cleaned up when the process that called this
531             function exits.
532              
533             If the input to C is a filename rather than a directory name,
534             a new subdirectory will be created in the directory where the file
535             is located.
536              
537              
538             =head2 flock
539              
540             =head2 $success = Dir::Flock::flock( $dir, $op )
541              
542             Acquires and releases advisory locks on the given directory
543             with the same semantics as the Perl builtin
544             L function.
545              
546              
547              
548             =head2 lockobj
549              
550             =head2 lockobj_ex
551              
552             =head2 $lock = Dir::Flock::lockobj( $dir [, $timeout] );
553              
554             =head2 $lock = Dir::Flock::lockobj_ex( $dir [, $timeout] );
555              
556             Attempts to acquire an exclusive advisory lock for the given
557             directory. On success, returns a handle to the directory lock
558             with the feature that the lock will be released when the handle
559             goes out of scope. This allows you to use this module with
560             syntax such as
561              
562             {
563             my $lock = Dir::Flock::lockobj( "/some/path" );
564             ... synchronized code ...
565             }
566             # $lock out of scope, so directory lock released
567             ... unsynchronized code ...
568              
569             Optional C<$timeout> argument causes the function to block
570             for a maximum of C<$timeout> seconds attempting to acquire
571             the lock. If C<$timeout> is not provided or is C,
572             the function will block indefinitely while waiting for the
573             lock.
574              
575             Returns a false value and may sets C<$Dir::Flock::errstr> if the function
576             times out or is otherwise unable to acquire the directory lock.
577              
578             C is an alias for C.
579              
580              
581             =head2 lockobj_sh
582              
583             =head2 my $lock = Dir::Flock::lockobj_sh($dir [, $timeout])
584              
585             Analogue to L<"lockobj_ex">. Returns a reference to a shared lock
586             on a directory that will be released when the reference goes
587             out of scope.
588              
589             Returns a false value and may set C<$Dir::Flock::errstr> if the
590             function times out or otherwise fails to acquire a shared lock
591             on the directory.
592              
593              
594             =head2 sync
595              
596             =head2 sync_ex
597              
598             =head2 $result = Dir::Flock::sync CODE $dir [, $timeout]
599              
600             =head2 @result = Dir::Flock::sync_ex CODE $dir [, $timeout]
601              
602             Semantics for executing a block of code while there is an
603             advisory exclusive lock on the given directory. The code can
604             be evaluated in both scalar or list contexts. An optional
605             C<$timeout> argument will cause the function to give up and
606             return a false value if the lock cannot be acquired after
607             C<$timeout> seconds. Callers should be careful to distinguish
608             cases where the specified code reference returns nothing and
609             where the C function times out and returns nothing.
610             One way to distinguish these cases is to check the value of
611             C<$Dir::Flock::errstr>, which will generally be set if there
612             was an issue with the locking mechanics.
613              
614             The lock is released in the event that the given C<$code>
615             produces a fatal error.
616              
617              
618             =head2 sync_sh
619              
620             =head2 $result = Dir::Flock::sync_sh BLOCK $dir [, $timeout]
621              
622             =head2 @result = Dir::Flock::sync_sh BLOCK $dir [, $timeout]
623              
624             Analogue of L<"sync_ex"> but executes the code block while
625             there is an advisory shared lock on the given directory.
626              
627              
628             =head1 DEPENDENCIES
629              
630             C requires L where the C
631             function has subsection resolution.
632              
633              
634             =head1 EXPORTS
635              
636             Nothing is exported from C by default, but all of
637             the functions documented here may be exported by name.
638              
639             Many of the core functions of C have the same name
640             as Perl builtin functions or functions from other popular modules,
641             so users should be wary of importing functions from this module
642             into their working namespace.
643              
644              
645              
646             =head1 VARIABLES
647              
648             =head2 PAUSE_LENGTH
649              
650             =head2 $Dir::Flock::PAUSE_LENGTH
651              
652             C<$Dir::Flock::PAUSE_LENGTH> is the average number of seconds that
653             the module will wait after a failed attempt to acquire a lock before
654             attempting to acquire it again. The default value is 0.001,
655             which is a good setting for having a high throughput when the
656             synchronized operations take a short amount of time. In contexts
657             where the synchronized operations take a longer time, it may
658             be appropriate to increase this value to reduce busy-waiting CPU
659             utilization.
660              
661             =cut
662              
663             # also under VARIABLES: HEARTBEAT_CHECK
664              
665             # =head1 ENVIRONMENT => DIR_FLOCK_DEBUG
666              
667             # =cut
668            
669              
670              
671             =head1 LIMITATIONS
672              
673             See L<"System requirements"> above.
674              
675             The L module can be loaded when necessary
676             to provide a consistent synchronization API on systems that
677             require C to work properly and on systems that
678             don't support C.
679              
680             =cut
681              
682             # =head1 SEE ALSO L, L.
683             # L, L
684              
685              
686             =head1 SUPPORT
687              
688             You can find documentation for this module with the perldoc command.
689              
690             perldoc Dir::Flock
691              
692              
693             You can also look for information at:
694              
695             =over 4
696              
697             =item * CPAN Ratings
698              
699             L
700              
701             =item * Emob@cpan.orgE
702              
703             With the decommissioning of http://rt.cpan.org/,
704             please send bug reports and feature requests
705             directly to the author's email address.
706              
707             =back
708              
709              
710              
711              
712             =head1 AUTHOR
713              
714             Marty O'Brien, Emob@cpan.orgE
715              
716              
717              
718              
719             =head1 LICENSE AND COPYRIGHT
720              
721             Copyright (c) 2019-2020, Marty O'Brien
722              
723             This library is free software; you can redistribute it and/or modify
724             it under the same terms as Perl itself, either Perl version 5.8.8 or,
725             at your option, any later version of Perl 5 you may have available.
726              
727             See http://dev.perl.org/licenses/ for more information.
728              
729             =cut
730              
731              
732              
733             =begin TODO
734              
735             Heartbeat
736              
737             a running process should be able to update the timestamp of
738             their lockfiles (either the mtime known to the filesystem or
739             in the file data themselves) to let other processes (on the
740             same and other hosts) know that the locking process is still
741             alive. Can you do that without releasing the lock?
742              
743             Include heartbeat data in the file names?
744              
745             Threads
746              
747             In _ping_oldest_file , how to detect whether a thread is
748             still alive? How to detect whether a thread on a remote
749             machine is still alive?
750              
751             =end TODO