File Coverage

blib/lib/KinoSearch1/Store/Lock.pm
Criterion Covered Total %
statement 22 28 78.5
branch 1 2 50.0
condition n/a
subroutine 7 11 63.6
pod 0 5 0.0
total 30 46 65.2


line stmt bran cond sub pod time code
1             package KinoSearch1::Store::Lock;
2 40     40   279 use strict;
  40         90  
  40         1947  
3 40     40   222 use warnings;
  40         98  
  40         2440  
4 40     40   233 use KinoSearch1::Util::ToolSet;
  40         120  
  40         6334  
5 40     40   253 use base qw( KinoSearch1::Util::Class );
  40         79  
  40         5228  
6              
7             BEGIN {
8 40     40   1369 __PACKAGE__->init_instance_vars(
9             # constructor params / members
10             invindex => undef,
11             lock_name => undef,
12             timeout => 0,
13             );
14             }
15              
16 40     40   242 use constant LOCK_POLL_INTERVAL => 1000;
  40         105  
  40         14073  
17              
18             # Attempt to aquire lock once per second until the timeout has been reached.
19             sub obtain {
20 261     261 0 520 my $self = shift;
21              
22             # calculate maximum seconds to sleep
23 261         941 my $sleepcount = $self->{timeout} / LOCK_POLL_INTERVAL;
24              
25             # keep trying to obtain lock until timeout is reached
26 261         1031 my $locked = $self->do_obtain;
27 261         810 while ( !$locked ) {
28 2 50       510 croak("Couldn't get lock using '$self->{lock_name}'")
29             if $sleepcount-- <= 0;
30 0         0 sleep 1;
31 0         0 $locked = $self->do_obtain;
32             }
33              
34 259         860 return $locked;
35             }
36              
37             =begin comment
38              
39             my $locked = $lock->do_obtain;
40              
41             Do the actual work to aquire the lock and return a boolean reflecting
42             success/failure.
43              
44             =end comment
45             =cut
46              
47 0     0 0   sub do_obtain { shift->abstract_death }
48              
49             =begin comment
50              
51             $lock->release;
52              
53             Release the lock.
54              
55             =end comment
56             =cut
57              
58 0     0 0   sub release { shift->abstract_death }
59              
60             =begin comment
61              
62             my $locked_or_not = $lock->is_locked;
63              
64             Return true if the resource is locked, false otherwise.
65              
66             =end comment
67             =cut
68              
69 0     0 0   sub is_locked { shift->abstract_death }
70              
71             # Getter for lock_name.
72 0     0 0   sub get_lock_name { shift->{lock_name} }
73              
74             1;
75              
76             __END__