IPC-LockTicket
view release on metacpan or search on metacpan
IPC::LockTicket allows one to get a simple token/ticket locking mechanism, like C<flock()> does, but FIFO sorted.
It also makes it easy to transport small amount of data between processes.
The data you want to transfer must be saved as an anonymous reference, and returns as such.
The data is not transferred in real time, but only on request. While you might store whole objects if you need the most recent of them, lock the store, load it, change it and store it again before you unlock it again.
In theory, you can store as much data as your disk can hold, but be aware: this will slow down the lock mechanism. Use multiple files in this case: One only holds data (full path), the other is just for locking (dynamic path).
=cut
=head1 IPC::LockTicket Class METHODS
=head3 C<New>
my $object = IPC::LockTicket->new($str_Name, $oct_Permission) or die;
Creates a new IPC::LockTicket object, which is returned. Returns C<undef> on failure. Expects one or two arguments. First the name, optionally secondly the permission as an octal number.
=over 2
=item name
String just matching C<m{^[-_a-z0-9]+$}i> for dynamic naming or a full path to a file.
Mandatory.
=item permission
Access rights that the lock file will have.
For example, if just a collision protection shall be implemented, it might be the best to set it to 0666, so collisions can be prevented over the whole system for every user.
Expects an octal number, like L<chmod(1)>.
Defaults to C<0600>.
=back
=cut
=head3 C<DESTROY>
my $bol_Success = $object->DESTROY();
Destroys the object so it removes lock files or PIDs from lock files. Automatically called on C<undef($object)>, C<die>, C<exit>, and when C<$object> leaves the scope.
B<Not> called if another module C<croak()>s or if the application fails through an exception at runtime.
Z<>
If not called, the next C<MainLock()> will clear the orphan file, but it is not clear that the content was written properly. If the file is empty or cannot be read, it will not be cleared, but C<MainLock()> will fail. See L<C<MainLock>> for further d...
=cut
=head3 C<MainLock>
my $bol_Success = $object->MainLock();
my $bol_Success = $object->MainLock(1);
Checks if a lock file exists and creates it if not. Fails if file exists and process stored within is alive.
If a C<true> value is supplied, locking is non-exclusive but shared. If the file exists and was also created non-exclusively, locking is successful. This way the lock file is shared with several processes, requesting non-exclusive mode.
If C<false> is returned from C<MainLock> no lock file was created/claimed.
Meaning:
____________________________________________________________________________________________________________________________________
| Call mode | MainLock() | MainLock() | MainLock() | MainLock() | MainLock(1) | MainLock(1) | MainLock(1) | MainLock(1) |
| Lock file | empty* | shared | exclusive | non-existent | empty* | shared | exclusive | non-existent |
| MainLock returns | false | false | false | true | false | true | false | true |
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯Â...
*= empty or any other non-Storable data.
=cut
=head3 C<MainUnlock>
$object->MainUnlock();
Removes lock. The lock file is deleted if this is the last process accessing it.
Returns only a true value.
If it is a shared lock and not the last process accessing it, only the PID entries are removed from the lock file.
=cut
=head3 C<TokenLock>
$object->TokenLock();
Can only be called after C<MainLock> and before C<MainUnlock> otherwise C<die>s.
Requests exclusive lock, i.e., any C<TokenLock> is blocking until C<TokenUnlock> was called, or the locking process is dead. Process checks are done to prevent infinite locks through broken children or parents, but this is not proper usage. Don't jus...
Returns a true value or blocks.
=cut
=head3 C<TokenUnlock>
$object->TokenUnlock();
Can only be called after C<MainLock> and before C<MainUnlock> otherwise C<die>s.
Removes the token from the lock file so any other request of C<TokenLock> can be satisfied.
Returns false if unlock is done while not holding the lock.
Otherwise, it returns true.
=cut
=head3 C<SetCustomData>
$object->TokenLock();
$object->SetCustomData($reference);
$object->TokenUnlock();
Writes a custom data reference in a reserved area. See L<CAVEATS> and L<GOOD PRACTICE> for further details.
Should be preceded by C<TokenLock()> and followed by C<TokenUnlock()>.
Returns only a true value or C<die>s.
( run in 1.935 second using v1.01-cache-2.11-cpan-39bf76dae61 )