Skip to content

Commit

Permalink
add write_pid method to the lock object
Browse files Browse the repository at this point in the history
and reimplement File::Flock::Tiny->write_pid to use it
  • Loading branch information
trinitum committed Sep 20, 2012
1 parent d55b689 commit 14ba724
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions lib/File/Flock/Tiny.pm
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,7 @@ explicitely.
sub write_pid {
my ( $class, $file ) = @_;
my $lock = $class->trylock($file);
if ($lock) {
$lock->truncate(0);
$lock->print("$$\n");
$lock->flush;
*$lock->{destroy_only_in} = $$;
}
$lock->write_pid if $lock;
return $lock;
}

Expand All @@ -100,6 +95,24 @@ use Fcntl qw(:flock);

=head1 LOCK OBJECT METHODS
=head2 $lock->write_pid
Truncates locked file and saves PID into it. Also marks the lock object as tied
to the current process, so it only will be automatically released when goes out
of scope in the current process but not in any of the child processes created
after this call.
=cut

sub write_pid {
my $lock = shift;
$lock->truncate(0);
$lock->print("$$\n");
$lock->flush;
*$lock->{destroy_only_in} = $$;
return;
}

=head2 $lock->release
Unlock file
Expand Down Expand Up @@ -132,7 +145,8 @@ demonstrates the use for this method:
{
my $lock = File::Flock::Tiny->lock("lockfile");
my $pid = fork;
unless ( defined $pid ) {
if( $pid == 0 ) {
# We are in child process
do_something();
}
$lock->close;
Expand Down

0 comments on commit 14ba724

Please sign in to comment.