++ed by:
FLORIAN

1 PAUSE user

Ettore Di Giacinto
and 2 contributors

NAME

Mojo::IOLoop::ReadWriteProcess::Shared::Lock - IPC Lock

SYNOPSIS

    use Mojo::IOLoop::ReadWriteProcess qw(process queue lock);

    my $q = queue; # Create a Queue
    $q->pool->maximum_processes(10); # 10 Concurrent processes at maximum
    $q->queue->maximum_processes(50); # 50 is maximum total to be allowed in the queue

    $q->add(
      process(
        sub {
          my $l = lock(key => 42); # IPC Lock
          my $e = 1;
          if ($l->lock) { # Blocking lock acquire
            # Critical section
            $e = 0;
            $l->unlock;
          }
          exit($e);
        }
      )->set_pipes(0)->internal_pipes(0)) for 1 .. 20; # Fill with 20 processes

    $q->consume(); # Consume the processes

DESCRIPTION

Mojo::IOLoop::ReadWriteProcess::Shared::Lock uses IPC::Semaphore internally and creates a Lock from a semaphore that is available across different processes.

METHODS

Mojo::IOLoop::ReadWriteProcess::Shared::Lock inherits all events from Mojo::IOLoop::ReadWriteProcess::Shared::Semaphore and implements the following new ones.

lock/unlock

    use Mojo::IOLoop::ReadWriteProcess qw(lock);

    my $l = lock(key => "42"); # Create Lock with key 42

    if ($l->lock) { # Blocking call
      # Critical section
      ...

      $l->unlock; # Release the lock
    }

Acquire access to the lock and unlocks it.

lock() has the same arguments as Mojo::IOLoop::ReadWriteProcess::Shared::Semaphore acquire().

try_lock

    use Mojo::IOLoop::ReadWriteProcess qw(lock);

    my $l = lock(key => "42"); # Create Lock with key 42

    if ($l->try_lock) { # Non Blocking call
      # Critical section
      ...

      $l->unlock; # Release the lock
    }

Try to acquire lock in a non-blocking way.

lock_section

    use Mojo::IOLoop::ReadWriteProcess qw(lock);
    my $l = lock(key => 3331);
    my $e = 1;
    $l->lock_section(sub { $e = 0; die; }); # or also $l->section(sub { $e = 0 });

    $l->locked; # is 0

Executes a function inside a locked section. Errors are catched so lock is released in case of failures.

ATTRIBUTES

Mojo::IOLoop::ReadWriteProcess::Shared::Lock inherits all attributes from Mojo::IOLoop::ReadWriteProcess::Shared::Semaphore and provides the following new ones.

flags

    use Mojo::IOLoop::ReadWriteProcess qw(lock);
    use IPC::SysV qw(IPC_CREAT IPC_EXCL S_IRUSR S_IWUSR);

    my $l = lock(flags=> IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);

Sets flag for the lock. In such way you can limit the access to the lock, e.g. to specific user/group process.

key

    use Mojo::IOLoop::ReadWriteProcess qw(lock);
    my $l = lock(key => 42);

Sets the lock key that is used to retrieve the lock among different processes, must be an integer.

locked

    use Mojo::IOLoop::ReadWriteProcess qw(lock);

    my $l = lock(key => 42);

    $l->lock_section(sub {
      $l->locked; # 1
    });

    $l->locked; # 0

Returns the lock status

DEBUGGING

You can set MOJO_PROCESS_DEBUG environment variable to get diagnostics about the process execution.

    MOJO_PROCESS_DEBUG=1

LICENSE

Copyright (C) Ettore Di Giacinto.

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

AUTHOR

Ettore Di Giacinto <edigiacinto@suse.com>