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   1032283 use strict;
  8         16  
  8         284  
3 8     8   37 use warnings;
  8         17  
  8         5076  
4             our $VERSION = '0.02';
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   1918 my $self = shift;
14 2 50       30 $self->[0]->${\$self->[1]} if $self->[0];
  2         21  
15             }
16             }
17              
18             sub Data::Sync::Shared::RWLock::rdlock_guard {
19 1     1 0 216243 my $self = shift;
20 1         14 $self->rdlock(@_);
21 1         25 bless [$self, 'rdunlock'], 'Data::Sync::Shared::RWLock::Guard';
22             }
23              
24             sub Data::Sync::Shared::RWLock::wrlock_guard {
25 1     1 0 989 my $self = shift;
26 1         16 $self->wrlock(@_);
27 1         8 bless [$self, 'wrunlock'], 'Data::Sync::Shared::RWLock::Guard';
28             }
29              
30             package Data::Sync::Shared::Condvar::Guard {
31             sub DESTROY {
32 1     1   440 my $self = shift;
33 1 50       25 $self->[0]->unlock if $self->[0];
34             }
35             }
36              
37             sub Data::Sync::Shared::Condvar::lock_guard {
38 1     1 0 1026 my $self = shift;
39 1         20 $self->lock;
40 1         11 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 8678 my ($self, $pred, $timeout) = @_;
47 4   50     80 $timeout //= -1;
48 4 100       76 return $pred->() ? 0 : 1 if $timeout == 0;
    100          
49 2         13 my $deadline;
50 2 50       63 if ($timeout > 0) {
51 2         95 require Time::HiRes;
52 2         42 $deadline = Time::HiRes::time() + $timeout;
53             }
54 2         89 while ($pred->()) {
55 2 50       82 if (defined $deadline) {
56 2         24 require Time::HiRes;
57 2         20 my $remaining = $deadline - Time::HiRes::time();
58 2 50       31 return 0 if $remaining <= 0;
59 2 100       149712 $self->wait($remaining) or return 0;
60             } else {
61 0         0 $self->wait;
62             }
63             }
64 1         67 return 1;
65             }
66              
67             # Semaphore guard
68              
69             sub Data::Sync::Shared::Semaphore::acquire_guard {
70 3     3 0 278869 my ($self, $n, $timeout) = @_;
71 3   100     17 $n //= 1;
72 3 100       9 if ($n == 1) {
73 2 100 100     24 $self->acquire($timeout // -1) or return undef;
74             } else {
75 1 50 50     10 $self->acquire_n($n, $timeout // -1) or return undef;
76             }
77 2         12 bless [$self, $n], 'Data::Sync::Shared::Semaphore::Guard';
78             }
79              
80             package Data::Sync::Shared::Semaphore::Guard {
81             sub DESTROY {
82 2     2   1244 my $self = shift;
83 2         20 $self->[0]->release($self->[1]);
84             }
85             }
86              
87             1;
88              
89             __END__