Mojo::...::Lock(3pm)

🌸 NAME 🌸

Mojo::IOLoop::ReadWriteProcess::Shared::Lock β€” it's an IPC Lock, bestie. That's the whole vibe. πŸ’…


✨ SYNOPSIS ✨

okay so picture this: you want a bunch of processes to share ONE lock, no chaos, no drama:

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

twenty processes, one lock, zero fighting. we love to see it. 🫢


πŸ’– DESCRIPTION πŸ’–

Mojo::IOLoop::ReadWriteProcess::Shared::Lock is lowkey doing magic under the hood β€” it uses IPC::Semaphore internally and creates a Lock from a semaphore that's available across different processes. so like, all your little process babies can share it. iconic behavior fr fr. ✨


πŸ‘‘ METHODS πŸ‘‘

Mojo::IOLoop::ReadWriteProcess::Shared::Lock inherits all events from Mojo::IOLoop::ReadWriteProcess::Shared::Semaphore and implements the following new ones. periodt. πŸ’…

πŸ”’ 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
}

So what this does: it acquires access to the lock and unlocks it. (Yes, that's exactly what the page says, we're not editorializing, we're being ✨ faithful ✨.)

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

πŸŽ€ 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
}

It tries to acquire the lock in a non-blocking way. so if it can't get it, it just moves on with its life, no waiting around, main character energy only. πŸ’…

🌈 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

This runs a function inside a locked section. and here's the best part β€” errors are caught, so the lock gets released even if things go sideways. no deadlocks, no crying. 😭🫢


πŸ’œ ATTRIBUTES πŸ’œ

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

🚩 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 the flag for the lock. and the way this works is β€” you can limit access to the lock, like, to a specific user or group process. gatekeeping, but make it permissions. πŸ’…

πŸ”‘ key πŸ”‘

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

Sets the lock key, which is how different processes find and retrieve the same lock. must be an integer, no funny business. πŸ”’

πŸ” 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. that's it, that's the whole thing. ⭐


πŸ¦‹ DEBUGGING πŸ¦‹

You can set the MOJO_PROCESS_DEBUG environment variable to get diagnostics about the process execution. bestie detective mode. πŸ”

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>


and that's the whole page, babe 🫢 β€” this one comes from User Contributed Perl Documentation, perl v5.42.0, dated 2025-07-25. go forth and lock responsibly. πŸ’…βœ¨

πŸ“¦ where this page comes from