File Coverage

blib/lib/Data/Sync/Shared.pm
Criterion Covered Total %
statement 42 43 97.6
branch 16 22 72.7
condition 6 8 75.0
subroutine 10 10 100.0
pod 0 5 0.0
total 74 88 84.0


line stmt bran cond sub pod time code
1             package Data::Sync::Shared;
2 8     8   1255224 use strict;
  8         17  
  8         332  
3 8     8   46 use warnings;
  8         19  
  8         4758  
4             our $VERSION = '0.03';
5              
6             require XSLoader;
7             XSLoader::load('Data::Sync::Shared', $VERSION);
8              
9             # Guard objects — auto-release on scope exit
10              
11             package Data::Sync::Shared::RWLock::Guard {
12             sub DESTROY {
13 2     2   1236 my $self = shift;
14 2 50       19 $self->[0]->${\$self->[1]} if $self->[0];
  2         20  
15             }
16             }
17              
18             sub Data::Sync::Shared::RWLock::rdlock_guard {
19 1     1 0 215952 my $self = shift;
20 1         11 $self->rdlock(@_);
21 1         18 bless [$self, 'rdunlock'], 'Data::Sync::Shared::RWLock::Guard';
22             }
23              
24             sub Data::Sync::Shared::RWLock::wrlock_guard {
25 1     1 0 669 my $self = shift;
26 1         11 $self->wrlock(@_);
27 1         6 bless [$self, 'wrunlock'], 'Data::Sync::Shared::RWLock::Guard';
28             }
29              
30             package Data::Sync::Shared::Condvar::Guard {
31             sub DESTROY {
32 1     1   239 my $self = shift;
33 1 50       17 $self->[0]->unlock if $self->[0];
34             }
35             }
36              
37             sub Data::Sync::Shared::Condvar::lock_guard {
38 1     1 0 718 my $self = shift;
39 1         16 $self->lock;
40 1         12 bless [$self], 'Data::Sync::Shared::Condvar::Guard';
41             }
42              
43             # Condvar wait_while — loop until predicate returns false
44              
45             sub Data::Sync::Shared::Condvar::wait_while {
46 4     4 0 6438 my ($self, $pred, $timeout) = @_;
47 4   50     99 $timeout //= -1;
48 4 100       84 return $pred->() ? 0 : 1 if $timeout == 0;
    100          
49 2         34 my $deadline;
50 2 50       27 if ($timeout > 0) {
51 2         83 require Time::HiRes;
52 2         41 $deadline = Time::HiRes::time() + $timeout;
53             }
54 2         44 while ($pred->()) {
55 2 50       102 if (defined $deadline) {
56 2         14 require Time::HiRes;
57 2         23 my $remaining = $deadline - Time::HiRes::time();
58 2 50       45 return 0 if $remaining <= 0;
59 2 100       151037 $self->wait($remaining) or return 0;
60             } else {
61 0         0 $self->wait;
62             }
63             }
64 1         39 return 1;
65             }
66              
67             # Semaphore guard
68              
69             sub Data::Sync::Shared::Semaphore::acquire_guard {
70 3     3 0 354479 my ($self, $n, $timeout) = @_;
71 3   100     23 $n //= 1;
72 3 100       12 if ($n == 1) {
73 2 100 100     27 $self->acquire($timeout // -1) or return undef;
74             } else {
75 1 50 50     13 $self->acquire_n($n, $timeout // -1) or return undef;
76             }
77 2         17 bless [$self, $n], 'Data::Sync::Shared::Semaphore::Guard';
78             }
79              
80             package Data::Sync::Shared::Semaphore::Guard {
81             sub DESTROY {
82 2     2   1444 my $self = shift;
83 2         20 $self->[0]->release($self->[1]);
84             }
85             }
86              
87             1;
88              
89             __END__