1Mojo::IOLoop::ReadWriteUPsreorceCsosn:t:rMSiohbjauort:ee:ddI:O:PLLeooroclpk:(D:3oR)ceuamdeWnrtiatteiPornocess::Shared::Lock(3)
2
3
4

NAME

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

SYNOPSIS

9           use Mojo::IOLoop::ReadWriteProcess qw(process queue lock);
10
11           my $q = queue; # Create a Queue
12           $q->pool->maximum_processes(10); # 10 Concurrent processes at maximum
13           $q->queue->maximum_processes(50); # 50 is maximum total to be allowed in the queue
14
15           $q->add(
16             process(
17               sub {
18                 my $l = lock(key => 42); # IPC Lock
19                 my $e = 1;
20                 if ($l->lock) { # Blocking lock acquire
21                   # Critical section
22                   $e = 0;
23                   $l->unlock;
24                 }
25                 exit($e);
26               }
27             )->set_pipes(0)->internal_pipes(0)) for 1 .. 20; # Fill with 20 processes
28
29           $q->consume(); # Consume the processes
30

DESCRIPTION

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

METHODS

37       Mojo::IOLoop::ReadWriteProcess::Shared::Lock inherits all events from
38       Mojo::IOLoop::ReadWriteProcess::Shared::Semaphore and implements the
39       following new ones.
40
41   lock/unlock
42           use Mojo::IOLoop::ReadWriteProcess qw(lock);
43
44           my $l = lock(key => "42"); # Create Lock with key 42
45
46           if ($l->lock) { # Blocking call
47             # Critical section
48             ...
49
50             $l->unlock; # Release the lock
51           }
52
53       Acquire access to the lock and unlocks it.
54
55       "lock()" has the same arguments as
56       Mojo::IOLoop::ReadWriteProcess::Shared::Semaphore "acquire()".
57
58   try_lock
59           use Mojo::IOLoop::ReadWriteProcess qw(lock);
60
61           my $l = lock(key => "42"); # Create Lock with key 42
62
63           if ($l->try_lock) { # Non Blocking call
64             # Critical section
65             ...
66
67             $l->unlock; # Release the lock
68           }
69
70       Try to acquire lock in a non-blocking way.
71
72   lock_section
73           use Mojo::IOLoop::ReadWriteProcess qw(lock);
74           my $l = lock(key => 3331);
75           my $e = 1;
76           $l->lock_section(sub { $e = 0; die; }); # or also $l->section(sub { $e = 0 });
77
78           $l->locked; # is 0
79
80       Executes a function inside a locked section. Errors are catched so lock
81       is released in case of failures.
82

ATTRIBUTES

84       Mojo::IOLoop::ReadWriteProcess::Shared::Lock inherits all attributes
85       from Mojo::IOLoop::ReadWriteProcess::Shared::Semaphore and provides the
86       following new ones.
87
88   flags
89           use Mojo::IOLoop::ReadWriteProcess qw(lock);
90           use IPC::SysV qw(IPC_CREAT IPC_EXCL S_IRUSR S_IWUSR);
91
92           my $l = lock(flags=> IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);
93
94       Sets flag for the lock. In such way you can limit the access to the
95       lock, e.g. to specific user/group process.
96
97   key
98           use Mojo::IOLoop::ReadWriteProcess qw(lock);
99           my $l = lock(key => 42);
100
101       Sets the lock key that is used to retrieve the lock among different
102       processes, must be an integer.
103
104   locked
105           use Mojo::IOLoop::ReadWriteProcess qw(lock);
106
107           my $l = lock(key => 42);
108
109           $l->lock_section(sub {
110             $l->locked; # 1
111           });
112
113           $l->locked; # 0
114
115       Returns the lock status
116

DEBUGGING

118       You can set MOJO_PROCESS_DEBUG environment variable to get diagnostics
119       about the process execution.
120
121           MOJO_PROCESS_DEBUG=1
122

LICENSE

124       Copyright (C) Ettore Di Giacinto.
125
126       This library is free software; you can redistribute it and/or modify it
127       under the same terms as Perl itself.
128

AUTHOR

130       Ettore Di Giacinto <edigiacinto@suse.com>
131
132
133
134perl v5.34.0                   Moj2o0:2:1I-O0L7o-o2p2::ReadWriteProcess::Shared::Lock(3)
Impressum