File Coverage

lib/IPC/SRLock.pm
Criterion Covered Total %
statement 28 28 100.0
branch 2 2 100.0
condition n/a
subroutine 9 9 100.0
pod 1 1 100.0
total 40 40 100.0


line stmt bran cond sub pod time code
1             package IPC::SRLock;
2              
3 1     1   477 use 5.010001;
  1         2  
4 1     1   3 use namespace::autoclean;
  1         1  
  1         5  
5 1     1   48 use version; our $VERSION = qv( sprintf '0.29.%d', q$Rev: 1 $ =~ /\d+/gmx );
  1         1  
  1         3  
6              
7 1     1   430 use File::DataClass::Types qw( HashRef LoadableClass NonEmptySimpleStr Object );
  1         24334  
  1         9  
8 1     1   896 use IPC::SRLock::Utils qw( merge_attributes );
  1         1  
  1         6  
9 1     1   166 use Moo;
  1         1  
  1         5  
10              
11             my $_build__implementation = sub {
12 5     5   546 return $_[ 0 ]->_implementation_class->new( $_[ 0 ]->_implementation_attr );
13             };
14              
15             my $_build__implementation_class = sub {
16 5     5   393 my $self = shift; my $type = $self->type; my $class;
  5         14  
  5         6  
17              
18 5 100       13 if ('+' eq substr $type, 0, 1) { $class = substr $type, 1 }
  1         2  
19 4         10 else { $class = __PACKAGE__.'::'.(ucfirst $type) }
20              
21 5         63 return $class;
22             };
23              
24             # Public attributes
25             has 'type' => is => 'ro', isa => NonEmptySimpleStr, default => 'fcntl';
26              
27             # Private attributes
28             has '_implementation' => is => 'lazy', isa => Object,
29             handles => [ qw( get_table list reset set ) ],
30             builder => $_build__implementation;
31              
32             has '_implementation_attr' => is => 'ro', isa => HashRef, required => 1;
33              
34             has '_implementation_class' => is => 'lazy', isa => LoadableClass,
35             builder => $_build__implementation_class;
36              
37             # Construction
38             around 'BUILDARGS' => sub {
39             my ($orig, $self, @args) = @_; my $attr = $orig->( $self, @args );
40              
41             my $builder = $attr->{builder};
42             my $conf = $builder && $builder->can( 'config' ) ? $builder->config : 0;
43              
44             $conf and $conf->can( 'lock_attributes' )
45             and merge_attributes $attr, $conf->lock_attributes,
46             [ keys %{ $conf->lock_attributes } ];
47              
48             $attr->{name} //= lc join '_', split m{ :: }mx, __PACKAGE__, -1;
49              
50             my $type = delete $attr->{type}; $attr = { _implementation_attr => $attr };
51              
52             $type and $type !~ m{ \A ([a-zA-Z0-9\:\+]+) \z }mx
53             and die "Type ${type} tainted";
54             $type and $attr->{type} = $1;
55              
56             return $attr;
57             };
58              
59             sub BUILD {
60 5     5 1 373 my $self = shift; $self->_implementation; return;
  5         57  
  5         13524  
61             }
62              
63             1;
64              
65             __END__
66              
67             =pod
68              
69             =encoding utf-8
70              
71             =begin html
72              
73             <a href="https://travis-ci.org/pjfl/p5-ipc-srlock"><img src="https://travis-ci.org/pjfl/p5-ipc-srlock.svg?branch=master" alt="Travis CI Badge"></a>
74             <a href="https://roxsoft.co.uk/coverage/report/ipc-srlock/latest"><img src="https://roxsoft.co.uk/coverage/badge/ipc-srlock/latest" alt="Coverage Badge"></a>
75             <a href="http://badge.fury.io/pl/IPC-SRLock"><img src="https://badge.fury.io/pl/IPC-SRLock.svg" alt="CPAN Badge"></a>
76             <a href="http://cpants.cpanauthors.org/dist/IPC-SRLock"><img src="http://cpants.cpanauthors.org/dist/IPC-SRLock.png" alt="Kwalitee Badge"></a>
77              
78             =end html
79              
80             =head1 Name
81              
82             IPC::SRLock - Set / reset locking semantics to single thread processes
83              
84             =head1 Version
85              
86             This documents version v0.29.$Rev: 1 $ of L<IPC::SRLock>
87              
88             =head1 Synopsis
89              
90             use IPC::SRLock;
91              
92             my $config = { tempdir => 'path_to_tmp_directory', type => 'fcntl' };
93              
94             my $lock_obj = IPC::SRLock->new( $config );
95              
96             $lock_obj->set( k => 'some_resource_identfier' );
97              
98             # This critical region of code is guaranteed to be single threaded
99              
100             $lock_obj->reset( k => 'some_resource_identfier' );
101              
102             =head1 Description
103              
104             Provides set/reset locking methods which will force a critical region
105             of code to run single threaded
106              
107             Implements a factory pattern, three implementations are provided. The
108             LCD option L<IPC::SRLock::Fcntl> which works on non Unixen,
109             L<IPC::SRLock::Sysv> which uses System V IPC, and
110             L<IPC::SRLock::Memcached> which uses C<libmemcache> to implement a
111             distributed lock manager
112              
113             =head1 Configuration and Environment
114              
115             Defines the following attributes;
116              
117             =over 3
118              
119             =item C<type>
120              
121             Determines which factory subclass is loaded. Defaults to C<fcntl>, can
122             be; C<fcntl>, C<memcached>, or C<sysv>
123              
124             =back
125              
126             =head1 Subroutines/Methods
127              
128             =head2 BUILDARGS
129              
130             Extracts the C<type> attribute from those passed to the factory subclass
131              
132             =head2 BUILD
133              
134             Called after an instance is created this subroutine triggers the lazy
135             evaluation of the concrete subclass
136              
137             =head2 get_table
138              
139             my $data = $lock_obj->get_table;
140              
141             Returns a hash ref that contains the current lock table contents. The
142             keys/values in the hash are suitable for passing to
143             L<HTML::FormWidgets>
144              
145             =head2 list
146              
147             my $array_ref = $lock_obj->list;
148              
149             Returns an array of hash refs that represent the current lock table
150              
151             =head2 reset
152              
153             $lock_obj->reset( k => 'some_resource_key' );
154              
155             Resets the lock referenced by the C<k> attribute.
156              
157             =head2 set
158              
159             $lock_obj->set( k => 'some_resource_key' );
160              
161             Sets the specified lock. Attributes are:
162              
163             =over 3
164              
165             =item C<k>
166              
167             Unique key to identify the lock. Mandatory no default
168              
169             =item C<p>
170              
171             Explicitly set the process id associated with the lock. Defaults to
172             the current process id
173              
174             =item C<t>
175              
176             Set the time to live for this lock. Defaults to five minutes. Setting
177             it to zero makes the lock last indefinitely
178              
179             =back
180              
181             =head1 Diagnostics
182              
183             Setting C<debug> to true will cause the C<set> methods to log
184             the lock record at the debug level
185              
186             =head1 Dependencies
187              
188             =over 3
189              
190             =item L<File::DataClass>
191              
192             =item L<Moo>
193              
194             =item L<Type::Tiny>
195              
196             =back
197              
198             =head1 Incompatibilities
199              
200             The C<sysv> subclass type will not work on C<MSWin32> and C<cygwin> platforms
201              
202             =head1 Bugs and Limitations
203              
204             Testing of the C<memcached> subclass type is skipped on all platforms as it
205             requires C<memcached> to be listening on the localhost's default
206             memcached port C<localhost:11211>
207              
208             There are no known bugs in this module.
209             Please report problems to the address below.
210             Patches are welcome
211              
212             =head1 Author
213              
214             Peter Flanigan, C<< <pjfl@cpan.org> >>
215              
216             =head1 License and Copyright
217              
218             Copyright (c) 2016 Peter Flanigan. All rights reserved
219              
220             This program is free software; you can redistribute it and/or modify it
221             under the same terms as Perl itself. See L<perlartistic>
222              
223             This program is distributed in the hope that it will be useful,
224             but WITHOUT WARRANTY; without even the implied warranty of
225             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
226              
227             =cut
228              
229             # Local Variables:
230             # mode: perl
231             # tab-width: 3
232             # End: