The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Lock::File - file locker with an automatic out-of-scope unlocking mechanism

VERSION

version 1.00

SYNOPSIS

    use Lock::File qw(lockf);

    # blocking mode is default
    my $lock = lockf('/var/lock/my_script.lock');

    # unlock
    undef $lock;

    # or:
    $lock->unlockf();

    # print filename
    say $lock->name;

    $lock = lockf('./my.lock', { blocking => 0 }) or die "Already locked";

    # lock an open file:
    $lock = lockf($fh);

    $lock = lockf_multi('./my.lock', 5); # will try to lock on files "my.lock.0", "my.lock.1" .. "my.lock.4"

    $lock = lockf_any('foo', 'bar');

DESCRIPTION

lockf is a perlfunc flock wrapper. The lock is autotamically released as soon as the assotiated object is no longer referenced.

lockf_multi makes non-blocking lockf calls for multiple files and throws and exception if all are locked.

FUNCTIONS

lockf($file, $options)

Create a Lock instance. Always save the result in some variable(s), otherwise the lock will be released immediately.

The lock is automatically released when all the references to the Lockf object are lost. The lockf mandatory parameter can be either a string representing a filename or a reference to an already opened filehandle. The second optional parameter is a hash of boolean options. Supported options are:

shared

OFF by default. Tells to achieve a shared lock. If not set, an exclusive lock is requested.

blocking

ON by default. If unset, a non-blocking mode of flock is used. If this flock fails because the lock is already held by some other process, undef is returned. If the failure reason is somewhat different, e.g. permissions problems or the absence of a target file directory, an exception is thrown.

timeout

Unset by default. If set, specifies the wait timeout for acquiring the blocking lock.

Throws an exception on timeout.

The value of 0 is equivalent to blocking => 0 option, except that it throws an exception instead of returning undef if the file is already locked.

mode

Undef by default. If set, a chmod with the specified mode is performed on a newly created file. Ignored when filehandle is passed instead of a filename.

remove

OFF by default. If set, the lock file will be deleted before unlocking.

Alternatively, you can use OO interface:

    my $lock = Lock::File->new($file, $options);
lockf_multi($file, $max, $options)

Calls non-blocking lockf's for files from $fname.0 to $fname.$max-1, and returns a Lock::File object for the first successful lock.

Only remove and mode options are supported.

lockf_any($filenames, $options)

Same as lockf_multi, but accepts arrayref of filenames.

METHODS

unlockf()

Force the lock to be released independent of how many references to the object are still alive.

share()

Transform exclusive lock to shared.

unshare()

Transform shared lock to exclusive. Can block if other shared/exclusive locks are held by some other processes.

name()

Gives the name of the file, as it was when the lock was taken.

FAQ

Yet another locking module? Why?

There're tons of file locking modules on CPAN. Last time I counted, there were at least 17.

And yet, every time I tried to find a replacement for our in-house code on which this module is based, every one of them had quirks which I found too uncomfortable. I had to copy our code as Ubic::Lockf when I opensourced Ubic.

I wanted to do the review of all those modules, neilb style. I never got around to that, and then I realized how much this task holds me back from releasing other useful stuff.

So... sorry for bringing yet-another-file-locking module into the world.

Why Lock::File instead of File::Lock?

First, there's File::Lock::Multi, which is completely unrelated to this module.

Second, there are so many locking modules that choosing a good name is *hard*.

Third, maybe I'm going to release Lock::Zookeeper with the similar interface in the future.

SIMILAR MODULES

File::Flock::Tiny

File::Lockfile

Lazy::Lockfile

Lockfile::Simple

File::Lock::Multi

File::Lock

File::FcntlLock

File::TinyLock

ExclusiveLock::Guard

File::lockf

And many others.

AUTHOR

Vyacheslav Matyukhin <mmcleric@yandex-team.ru>

COPYRIGHT AND LICENSE

This software is copyright (c) 2013 by Yandex LLC.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.