Lambda/Mutex.pm

filter: Statementstmt 95.0% 76 / 80 Branchbran 73.3% 22 / 30 Conditioncond 50.0% 4 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 90.0% 18 / 20 Podpod 100% 9 / 9 totaltotal 84.8% 129 / 152 CC 19 SCAR 12.4
CC
19
Cov
86%
DESTROY
6.0
release
5.1
__ANON__
5.1
CRAP 3.5
« Lambda/HTTP/UserAgent.pm ↑ Summary Lambda/Flock.pm »
1 # $Id: Mutex.pm,v 1.13 2010/03/27 19:56:34 dk Exp $
2 package IO::Lambda::Mutex;
3 2 ▶use vars qw($DEBUG @ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶$DEBUG = $IO::Lambda::DEBUG{mutex} || 0;
Condition50%1/2
$IO::Lambda::DEBUG{'mutex'} || 0
l!l
02
MC/DC0%0/1
$IO::Lambda::DEBUG{'mutex'} || 0
$IO::Lambda::DEBUG{'mutex'}
Truth table50%1/2
A
A= $IO::Lambda::DEBUG{'mutex'}
Aresult
11
00
5 2 @ISA = qw(Exporter);
6 2 @EXPORT_OK = qw(mutex);
7 2 %EXPORT_TAGS = ( all => \@EXPORT_OK);
8
9 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use IO::Lambda qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
12
13 sub new
14 {
15 1 ▶ return bless {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
16 taken => 0,
17 queue => [],
18 }, shift;
19 }
20
21 6 ▶sub is_taken { $_[0]-> {taken} }
Subroutine is_taken: called CC=1 SCAR=0.0
Pod documented
22 7 ▶sub is_free { not $_[0]-> {taken} }
Subroutine is_free: called CC=1 SCAR=0.0
Pod documented
23
24 # non-blocking take
25 sub take
26 {
27 8 ▶ my $self = shift;
Subroutine take: called CC=4 SCAR=16.1
Pod documented
28 8 ▶ warn "$self is taken\n" if $DEBUG and not $self->{taken};
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Mutex::DEBUG and not $self->{"taken"} 0 8 8
Condition33%1/3
$IO::Lambda::Mutex::DEBUG and not $self->{"taken"}
!ll&&!rl&&r
800
MC/DC0%0/2
$IO::Lambda::Mutex::DEBUG and not $self->{"taken"}
$IO::Lambda::Mutex::DEBUG not $self->{"taken"}
Truth table33%1/3
A and not(B)
A= $IO::Lambda::Mutex::DEBUGB= not $self->{"taken"}
ABresult
0X0
100
111
29 8 ▶ return $self-> {taken} ? 0 : ($self-> {taken} = 1);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'taken'} ? : 0 8 8
30 }
31
32 # remove the lambda from queue
33 sub remove
34 {
35 4 ▶ my ( $self, $lambda) = @_;
Subroutine remove: called CC=5 SCAR=16.1
Pod documented
36 4 my $found;
37 4 my $q = $self-> {queue};
38 4 ▶ for ( my $i = 0; $i < @$q; $i ++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$q) 3 1 4
39 3 ▶ next if $q->[$i] != $lambda;
Branch: 1 branch
BranchTrueFalseTotal
if $q->[$i] != $lambda 0 3 3
40 3 $found = $i;
41 3 last;
42 }
43 4 ▶ if ( defined $found) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $found) { } 3 1 4
44 3 splice( @$q, $found, 1);
45 3 return 1;
46 } else {
47 1 ▶ warn "$self failed to remove $lambda from queue\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Mutex::DEBUG 0 1 1
48 1 return 0;
49 }
50 }
51
52 sub waiter
53 {
54 11 ▶ my ( $self, $timeout) = @_;
Subroutine waiter: called CC=3 SCAR=11.0
Pod documented
55
56 # mutex is free, can take now
57 11 ▶ unless ( $self-> {taken}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"taken"}) 3 8 11
58 3 $self-> take;
59 3 ▶ return lambda { undef };
Subroutine __ANON__: called CC=1 SCAR=0.0
60 }
61
62 # mutex is not free, wait for it
63 8 my $waiter = IO::Lambda-> new;
64 my $bind = $waiter-> bind( sub {
65 7 ▶ my ($w,$rec) = (shift,shift);
Subroutine __ANON__: called CC=5 SCAR=16.2
66 # lambda was terminated, relinquish waiting and kill timeout
67 7 ▶ unless ($w->{__already_removed}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($w->{"__already_removed"}) 2 5 7
68 2 my $removed = $self->remove($w);
69 2 ▶ $self->release if !$removed && 0 == $self->{queue};
Branch: 1 branch
BranchTrueFalseTotal
if not $removed and 0 == $self->{"queue"} 0 2 2
Condition66%2/3
not $removed and 0 == $self->{"queue"}
!ll&&!rl&&r
110
MC/DC0%0/2
not $removed and 0 == $self->{"queue"}
not $removed 0 == $self->{"queue"}
Truth table66%2/3
not(A) and B
A= not $removedB= 0 == $self->{"queue"}
ABresult
0X0
100
111
70 }
71 7 ▶ $w-> cancel_event($timeout) if defined $timeout;
Branch: 1 branch
BranchTrueFalseTotal
if defined $timeout 1 6 7
72 7 return @_; # propagate error
73 8 });
74 8 push @{$self-> {queue}}, $waiter;
75
76 $timeout = $waiter-> watch_timer( $timeout, sub {
77 1 ▶ $self-> remove($waiter);
Subroutine __ANON__: called CC=1 SCAR=0.0
78 1 $waiter-> resolve($bind);
79 1 return 'timeout';
80 8 ▶ }) if defined $timeout;
Branch: 1 branch
BranchTrueFalseTotal
if defined $timeout 2 6 8
81
82 8 return $waiter;
83 }
84
85 sub release
86 {
87 13 ▶ my $self = shift;
Subroutine release: called CC=5 SCAR=16.3
Pod documented
88 13 ▶ return unless $self-> {taken};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"taken"} 0 13 13
89
90 13 ▶ unless (@{$self-> {queue}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->{"queue"};}) 8 5 13
91 8 ▶ warn "$self is free\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Mutex::DEBUG 0 8 8
92 8 $self-> {taken} = 0;
93 8 return;
94 }
95
96 5 my $lambda = shift @{$self-> {queue}};
97
98 5 ▶ warn "$self gives ownership to $lambda\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Mutex::DEBUG 0 5 5
99 5 $lambda-> {__already_removed} = 1;
100 5 $lambda-> terminate(undef);
101 }
102
103 0 ▶sub DESTROY { $_-> terminate('dead') for @{shift-> {queue}} }
Subroutine DESTROY: not called CC=2 SCAR=17.9
104
105 sub mutex(&)
106 {
107 0 ▶ my ( $self, $timeout) = context;
Subroutine mutex: not called CC=1 SCAR=6.9
Pod documented
108 0 $self-> waiter($timeout)-> condition(shift, \&mutex, 'mutex')
109 }
110
111 sub pipeline
112 {
113 5 ▶ my ( $self, $lambda, $timeout) = @_;
Subroutine pipeline: called CC=1 SCAR=0.0
Pod documented
114 lambda {
115 5 ▶ my @p = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
116 5 context $self-> waiter($timeout);
117 tail {
118 5 ▶ context $lambda, @p;
Subroutine __ANON__: called CC=1 SCAR=0.0
119 autocatch tail {
120 5 ▶ $self-> release;
Subroutine __ANON__: called CC=1 SCAR=0.0
121 5 return @_;
122 5 }}}
123 5 }
124
125
126 2 1;
127
128 =pod
129
130 =head1 NAME
131
132 IO::Lambda::Mutex - wait for a shared resource
133
134 =head1 DESCRIPTION
135
136 Objects of class C<IO::Lambda::Mutex> are mutexes, that as normal mutexes,
137 can be taken and released. The mutexes allow lambdas to wait for their
138 availability with method C<waiter>, that creates and returns a new lambda,
139 that in turn will finish as soon as the caller can acquire the mutex.
140
141 =head1 SYNOPSIS
142
143 use IO::Lambda qw(:lambda);
144 use IO::Lambda::Mutex qw(mutex);
145
146 my $mutex = IO::Lambda::Mutex-> new;
147
148 # wait for mutex that shall be available immediately
149 my $waiter = $mutex-> waiter;
150 my $error = $waiter-> wait;
151 die "error:$error" if $error;
152
153 # create and start a lambda that sleeps 2 seconds and then releases the mutex
154 my $sleeper = lambda {
155 context 2;
156 timeout { $mutex-> release }
157 };
158 $sleeper-> start;
159
160 # Create a new lambda that shall only wait for 0.5 seconds.
161 # It will surely fail, since $sleeper is well, still sleeping
162 lambda {
163 context $mutex-> waiter(0.5);
164 tail {
165 my $error = shift;
166 print $error ? "error:$error\n" : "ok\n";
167 # $error is expected to be 'timeout'
168 }
169 }-> wait;
170
171 # Again, wait for the same mutex but using different syntax.
172 # This time should be ok - $sleeper will sleep for 1.5 seconds and
173 # then the mutex will be available.
174 lambda {
175 context $mutex, 3;
176 mutex {
177 my $error = shift;
178 print $error ? "error:$error\n" : "ok\n";
179 # expected to be 'ok'
180 }
181 }->wait;
182
183 # pipeline - manage a queue of lambdas, stuff new ones to it, guarantees
184 # sequential execution:
185 lambda {
186 context
187 $mutex-> pipeline( lambda { print 1 } ),
188 $mutex-> pipeline( lambda { print 2 } ),
189 $mutex-> pipeline( lambda { print 3 } )
190 ;
191 &tails();
192 }-> wait;
193 # prints 123 guaranteedly in that order, even if intermediate lambdas sleep etc
194
195 =head1 API
196
197 =over
198
199 =item new
200
201 The constructor creates a new free mutex.
202
203 =item is_free
204
205 Returns boolean flag whether the mutex is free or not.
206 Opposite of L<is_taken>.
207
208 =item is_taken
209
210 Returns boolean flag whether the mutex is taken or not.
211 Opposite of L<is_free>.
212
213 =item take
214
215 Attempts to take the mutex. If the mutex is free, the operation
216 is successful and true value is returned. Otherwise, the operation
217 is failed and false value is returned.
218
219 =item release
220
221 Tries to releases the taken mutex. If there are lambdas waiting (see L<waiter>)
222 in the queue, the first lambda will be terminated, and thus whoever waits for
223 the lambda can be notified; it will be up to the code associated with the
224 waiter lambda to call C<release> again. If there are no waiters in the queue,
225 the mutex is set free.
226
227 =item waiter($timeout = undef) :: () -> error
228
229 Creates a new lambda, that is finished when the mutex becomes available.
230 The lambda is inserted into the internal waiting queue. It takes as
231 many calls to C<release> as many lambdas are in queue, until the mutex
232 becomes free. The lambda returns an error flags, which is C<undef> if
233 the mutex was acquired successfully, or the error string.
234
235 If C<$timeout> is defined, and by the time it is expired the mutex
236 could not be obtained, the lambda is removed from the queue, and
237 returned error value is 'timeout'. The mutex state is then unchanged.
238
239 If C<waiter> succeeds, a C<release> call is issued. Thus, if the next
240 waiter awaits for the mutex, it will be notified; otherwise the mutex
241 becomes free.
242
243 =item pipeline($lambda, $timeout = undef)
244
245 Creates a new lambda, that wraps over C<$lambda> so that it is executed
246 after mutex had been obtained. Also, as soon as C<$lambda> is finished,
247 the mutex is released, thus allowing others to take it.
248
249 =item remove($lambda)
250
251 Internal function, do not use directly, use C<< $lambda-> terminate >>
252 instead.
253
254 Removes the lambda created previously by waiter() from internal queue. Note
255 that after that operation the lambda will never finish by itself.
256
257 =item mutex($mutex, $timeout = undef) -> error
258
259 Condition wrapper over C<waiter>.
260
261 =back
262
263 =head1 AUTHOR
264
265 Dmitry Karasik, E<lt>dmitry@karasik.eu.orgE<gt>.
266
267 =cut
« Lambda/HTTP/UserAgent.pm ↑ Summary Lambda/Flock.pm »