lib/Mojo/IOLoop/ReadWriteProcess/Shared/Lock.pm

filter: Statementstmt 35.7% 15 / 42 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 37.5% 3 / 8 Podpod 80.0% 4 / 5 totaltotal 28.2% 22 / 78 CC 9 SCAR 25.4
CC
9
Cov
25%
lock
20.0
lock_section
20.0
unlock
12.0
CRAP 12.7
« lib/Mojo/IOLoop/ReadWriteProcess/Namespace.pm ↑ Summary lib/Mojo/IOLoop/ReadWriteProcess/Container.pm »
1 package Mojo::IOLoop::ReadWriteProcess::Shared::Lock;
2  
3 39 ▶use Mojo::Base 'Mojo::IOLoop::ReadWriteProcess::Shared::Semaphore';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 39 our @EXPORT_OK = qw(shared_lock semaphore);
6 39 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 39 ▶use constant DEBUG => $ENV{MOJO_PROCESS_DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # Mojo::IOLoop::ReadWriteProcess::Shared::Semaphore has same defaults - but locks have 1 count and 1 as setup value
10 # Make it explict
11 39 has count => 1;
12 39 has _value => 1;
13 39 has locked => 0;
14  
15 0 ▶sub shared_lock { __PACKAGE__->new(@_) }
Subroutine shared_lock: not called CC=1 SCAR=6.9
Pod undocumented
16  
17 0 ▶sub lock {
Subroutine lock: not called CC=4 SCAR=30.0
Pod documented
18 0 my $self = shift;
19 0 warn "[debug:$$] Attempt to acquire lock " . $self->key if DEBUG;
20 0 ▶ my $r = @_ > 0 ? $self->acquire(@_) : $self->acquire(wait => 1, undo => 0);
Branch: 1 branch
BranchTrueFalseTotal
@_ > 0 ? : 0 0 0
21 0 warn "[debug:$$] lock Returned : $r" if DEBUG;
22 0 ▶ $self->locked(1) if defined $r && $r == 1;
Branch: 1 branch
BranchTrueFalseTotal
if defined $r and $r == 1 0 0 0
Condition0%0/3
defined $r and $r == 1
!ll&&!rl&&r
000
MC/DC0%0/2
defined $r and $r == 1
defined $r $r == 1
Truth table0%0/3
A and B
A= defined $rB= $r == 1
ABresult
0X0
100
111
23 0 return $r;
24 }
25  
26 0 ▶sub lock_section {
Subroutine lock_section: not called CC=4 SCAR=30.0
Pod documented
27 0 my ($self, $fn) = @_;
28 0 warn "[debug:$$] Acquiring lock (blocking)" if DEBUG;
29 0 ▶ 1 while $self->lock != 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->lock != 1 and do { '???' } 0 0 0
30 0 warn "[debug:$$] Lock acquired $$" if DEBUG;
31  
32 0 my $r;
33 {
34 0 local $@;
35 0 $r = eval { $fn->() };
36 0 $self->unlock();
37 0 ▶ warn "[debug:$$] Error inside locked section : $@" if $@ && DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $@ and undef 0 0 0
Condition0%0/2
$@ and undef
!ll
00
MC/DC0%0/1
$@ and undef
$@
Truth table0%0/2
A
A= $@
Aresult
00
11
38 };
39 0 return $r;
40 }
41  
42 39 *section = \&lock_section;
43  
44 0 ▶sub try_lock { shift->acquire(undo => 0, wait => 0) }
Subroutine try_lock: not called CC=1 SCAR=6.9
Pod documented
45  
46 0 ▶sub unlock {
Subroutine unlock: not called CC=3 SCAR=24.8
Pod documented
47 0 my $self = shift;
48 0 warn "[debug:$$] UNLock " . $self->key if DEBUG;
49 0 my $r;
50 0 eval {
51 0 $r = $self->release(@_);
52 0 ▶ $self->locked(0) if defined $r && $r == 1;
Branch: 1 branch
BranchTrueFalseTotal
if defined $r and $r == 1 0 0 0
Condition0%0/3
defined $r and $r == 1
!ll&&!rl&&r
000
MC/DC0%0/2
defined $r and $r == 1
defined $r $r == 1
Truth table0%0/3
A and B
A= defined $rB= $r == 1
ABresult
0X0
100
111
53 };
54 0 return $r;
55 }
56  
57 =encoding utf-8
58  
59 =head1 NAME
60  
61 Mojo::IOLoop::ReadWriteProcess::Shared::Lock - IPC Lock
62  
63 =head1 SYNOPSIS
64  
65 use Mojo::IOLoop::ReadWriteProcess qw(process queue lock);
66  
67 my $q = queue; # Create a Queue
68 $q->pool->maximum_processes(10); # 10 Concurrent processes at maximum
69 $q->queue->maximum_processes(50); # 50 is maximum total to be allowed in the queue
70  
71 $q->add(
72 process(
73 sub {
74 my $l = lock(key => 42); # IPC Lock
75 my $e = 1;
76 if ($l->lock) { # Blocking lock acquire
77 # Critical section
78 $e = 0;
79 $l->unlock;
80 }
81 exit($e);
82 }
83 )->set_pipes(0)->internal_pipes(0)) for 1 .. 20; # Fill with 20 processes
84  
85 $q->consume(); # Consume the processes
86  
87 =head1 DESCRIPTION
88  
89 L<Mojo::IOLoop::ReadWriteProcess::Shared::Lock> uses L<IPC::Semaphore> internally and creates a Lock from a semaphore that is available across different processes.
90  
91 =head1 METHODS
92  
93 L<Mojo::IOLoop::ReadWriteProcess::Shared::Lock> inherits all events from L<Mojo::IOLoop::ReadWriteProcess::Shared::Semaphore> and implements
94 the following new ones.
95  
96 =head2 lock/unlock
97  
98 use Mojo::IOLoop::ReadWriteProcess qw(lock);
99  
100 my $l = lock(key => "42"); # Create Lock with key 42
101  
102 if ($l->lock) { # Blocking call
103 # Critical section
104 ...
105  
106 $l->unlock; # Release the lock
107 }
108  
109 Acquire access to the lock and unlocks it.
110  
111 C<lock()> has the same arguments as L<Mojo::IOLoop::ReadWriteProcess::Shared::Semaphore> C<acquire()>.
112  
113 =head2 try_lock
114  
115 use Mojo::IOLoop::ReadWriteProcess qw(lock);
116  
117 my $l = lock(key => "42"); # Create Lock with key 42
118  
119 if ($l->try_lock) { # Non Blocking call
120 # Critical section
121 ...
122  
123 $l->unlock; # Release the lock
124 }
125  
126 Try to acquire lock in a non-blocking way.
127  
128 =head2 lock_section
129  
130 use Mojo::IOLoop::ReadWriteProcess qw(lock);
131 my $l = lock(key => 3331);
132 my $e = 1;
133 $l->lock_section(sub { $e = 0; die; }); # or also $l->section(sub { $e = 0 });
134  
135 $l->locked; # is 0
136  
137 Executes a function inside a locked section. Errors are caught so lock is released in case of failures.
138  
139 =head1 ATTRIBUTES
140  
141 L<Mojo::IOLoop::ReadWriteProcess::Shared::Lock> inherits all attributes from L<Mojo::IOLoop::ReadWriteProcess::Shared::Semaphore> and provides
142 the following new ones.
143  
144 =head2 flags
145  
146 use Mojo::IOLoop::ReadWriteProcess qw(lock);
147 use IPC::SysV qw(IPC_CREAT IPC_EXCL S_IRUSR S_IWUSR);
148  
149 my $l = lock(flags=> IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);
150  
151 Sets flag for the lock. In such way you can limit the access to the lock, e.g. to specific user/group process.
152  
153 =head2 key
154  
155 use Mojo::IOLoop::ReadWriteProcess qw(lock);
156 my $l = lock(key => 42);
157  
158 Sets the lock key that is used to retrieve the lock among different processes, must be an integer.
159  
160 =head2 locked
161  
162 use Mojo::IOLoop::ReadWriteProcess qw(lock);
163  
164 my $l = lock(key => 42);
165  
166 $l->lock_section(sub {
167 $l->locked; # 1
168 });
169  
170 $l->locked; # 0
171  
172 Returns the lock status
173  
174 =head1 DEBUGGING
175  
176 You can set MOJO_PROCESS_DEBUG environment variable to get diagnostics about the process execution.
177  
178 MOJO_PROCESS_DEBUG=1
179  
180 =head1 LICENSE
181  
182 Copyright (C) Ettore Di Giacinto.
183  
184 This library is free software; you can redistribute it and/or modify
185 it under the same terms as Perl itself.
186  
187 =head1 AUTHOR
188  
189 Ettore Di Giacinto E<lt>edigiacinto@suse.comE<gt>
190  
191 =cut
192  
193 39 !!42;
« lib/Mojo/IOLoop/ReadWriteProcess/Namespace.pm ↑ Summary lib/Mojo/IOLoop/ReadWriteProcess/Container.pm »