| 1 | package Linux::Event::Timer; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use v5.36; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 5 | our $VERSION = '0.010'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 5 | ▶ | use Linux::FD::Timer 0.015 (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 5 | ▶ | use Fcntl qw(FD_CLOEXEC F_GETFD F_SETFD); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | # ------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 16 | # Constructor | ||||||||||||||||||||||||||||
| 17 | # ------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | ▶ | sub new ($class, %opt) { | |||||||||||||||||||||||||||
|
Subroutine
new: called CC=20 SCAR=35.5
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 4 | ▶ | croak "Linux::Event::Timer only works on Linux" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | unless $^O eq 'linux'; | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 4 | my ($after, $every) = delete @opt{qw(after every)}; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 4 | my $mode_count = 0; | |||||||||||||||||||||||||||
| 27 | 4 | ▶ | $mode_count++ if defined $after; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 4 | ▶ | $mode_count++ if defined $every; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 4 | ▶ | croak "Only one of after/every may be supplied" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | if $mode_count > 1; | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 4 | my $self = bless {}, $class; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 4 | ▶ | if (defined $opt{timerfd}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 0 | my $tfd = delete $opt{timerfd}; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 0 | ▶ | croak "timerfd must be an object" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | unless blessed($tfd); | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # Linux::FD::Timer objects are filehandles with timer methods. | ||||||||||||||||||||||||||||
| 41 | # We only require the operations this wrapper exposes. | ||||||||||||||||||||||||||||
| 42 | 0 | for my $m (qw(set_timeout receive)) { | |||||||||||||||||||||||||||
| 43 | 0 | ▶ | croak "timerfd missing required method '$m'" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | unless $tfd->can($m); | ||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | $self->{tfd} = $tfd; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | else { | ||||||||||||||||||||||||||||
| 50 | 4 | ▶ | my $nonblocking = exists $opt{nonblocking} ? !!$opt{nonblocking} : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 4 | ▶ | my $cloexec = exists $opt{cloexec} ? !!$opt{cloexec} : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 4 | ▶ | my $clock = exists $opt{clock} ? $opt{clock} : 'monotonic'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 4 | my @flags; | |||||||||||||||||||||||||||
| 55 | 4 | ▶ | push @flags, 'non-blocking' if $nonblocking; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | # Linux::FD::Timer currently documents 'non-blocking' as a supported flag. | ||||||||||||||||||||||||||||
| 58 | # We implement cloexec ourselves via fcntl if requested. | ||||||||||||||||||||||||||||
| 59 | 4 | ▶ | my $tfd = Linux::FD::Timer->new($clock, @flags) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | or croak "Linux::FD::Timer->new('$clock') failed"; | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 4 | $self->{tfd} = $tfd; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 4 | ▶ | if ($cloexec) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 4 | my $fd = fileno($tfd); | |||||||||||||||||||||||||||
| 66 | 4 | ▶ | if (defined $fd) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 4 | my $old = fcntl($tfd, F_GETFD, 0); | |||||||||||||||||||||||||||
| 68 | 4 | ▶ | fcntl($tfd, F_SETFD, ($old // 0) | FD_CLOEXEC); | ||||||||||||||||||||||||||
|
Condition50%1/2
$old // 0
MC/DC0%0/1
$old // 0
$old
Truth table50%1/2
A
A= $old
| |||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 4 | ▶ | if (keys %opt) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 0 | croak "Unknown option(s): " . join(", ", sort keys %opt); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | # Optional constructor arming | ||||||||||||||||||||||||||||
| 78 | 4 | ▶ | $self->after($after) if defined $after; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 4 | ▶ | $self->every($every) if defined $every; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 4 | return $self; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # ------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 85 | # Event loop integration | ||||||||||||||||||||||||||||
| 86 | # ------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 5 | ▶ | sub fd ($self) { fileno($self->{tfd}) } | ||||||||||||||||||||||||||
|
Subroutine
fd: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 0 | ▶ | sub fh ($self) { $self->{tfd} } | ||||||||||||||||||||||||||
|
Subroutine
fh: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | ▶ | sub read_ticks ($self) { | |||||||||||||||||||||||||||
|
Subroutine
read_ticks: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 93 | 5 | my $n = $self->{tfd}->receive; | |||||||||||||||||||||||||||
| 94 | 5 | ▶ | return defined($n) ? $n : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | # ------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 98 | # Arming | ||||||||||||||||||||||||||||
| 99 | # ------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | ▶ | sub disarm ($self) { | |||||||||||||||||||||||||||
|
Subroutine
disarm: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 102 | 1 | $self->{tfd}->set_timeout(0, 0); | |||||||||||||||||||||||||||
| 103 | 1 | return $self; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | ▶ | sub after ($self, $seconds) { | |||||||||||||||||||||||||||
|
Subroutine
after: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 107 | 3 | _num($seconds, 'seconds'); | |||||||||||||||||||||||||||
| 108 | 3 | ▶ | $seconds = 0 if $seconds < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 3 | $self->{tfd}->set_timeout($seconds, 0); | |||||||||||||||||||||||||||
| 110 | 3 | return $self; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | ▶ | sub every ($self, $interval) { | |||||||||||||||||||||||||||
|
Subroutine
every: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 114 | 1 | _num($interval, 'interval'); | |||||||||||||||||||||||||||
| 115 | 1 | ▶ | croak "interval must be > 0" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | unless $interval > 0; | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 1 | $self->{tfd}->set_timeout($interval, $interval); | |||||||||||||||||||||||||||
| 119 | 1 | return $self; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | # ------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 123 | # Internal validation | ||||||||||||||||||||||||||||
| 124 | # ------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | ▶ | sub _num ($v, $name) { | |||||||||||||||||||||||||||
|
Subroutine
_num: called CC=4 SCAR=16.1
| |||||||||||||||||||||||||||||
| 127 | 4 | ▶ | croak "$name is required" unless defined $v; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 4 | ▶ | croak "$name must be numeric" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $v or not $v =~ /\A-?(?:\d+(?:\.\d*)?|\.\d+)\z/u
MC/DC0%0/2
ref $v or not $v =~ /\A-?(?:\d+(?:\.\d*)?|\.\d+)\z/u
ref $v
not $v =~ /\A-?(?:\d+(?:\.\d*)?|\.\d+)\z/u
Truth table33%1/3
A or not(B)
A= ref $vB= not $v =~ /\A-?(?:\d+(?:\.\d*)?|\.\d+)\z/u
| |||||||||||||||||||||||||||||
| 129 | if ref($v) || $v !~ /\A-?(?:\d+(?:\.\d*)?|\.\d+)\z/; | ||||||||||||||||||||||||||||
| 130 | 4 | return; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 5 | 1; | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | __END__ | ||||||||||||||||||||||||||||