| 1 | package Linux::Perl::eventfd; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | =encoding utf-8 | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | Linux::Perl::eventfd | ||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | =head1 SYNOPSIS | ||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | my $efd = Linux::Perl::eventfd->new( | ||||||||||||||||||||
| 12 | initval => 4, | ||||||||||||||||||||
| 13 | flags => [ 'NONBLOCK', 'CLOEXEC' ], #only on 2.6.27+ | ||||||||||||||||||||
| 14 | ); | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | #or, e.g., Linux::Perl::eventfd::x86_64 | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | my $fd = $efd->fileno(); | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | $efd->add(12); | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | my $read = $efd->read(); | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | =head1 DESCRIPTION | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | This is an interface to the C<eventfd>/C<eventfd2> system call. | ||||||||||||||||||||
| 27 | (C<eventfd2> is only called if the given parameters require it.) | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | This class inherits from L<Linux::Perl::Base::TimerEventFD>. | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | =cut | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 5 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 34 | 5 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 5 | ▶ | use parent ( | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 37 | 5 | 'Linux::Perl::Base', | |||||||||||||||||||
| 38 | 'Linux::Perl::Base::TimerEventFD', | ||||||||||||||||||||
| 39 | 5 | ); | |||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 5 | ▶ | use Module::Load; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 5 | ▶ | use Linux::Perl; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 5 | ▶ | use Linux::Perl::Endian; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 46 | 5 | ▶ | use Linux::Perl::ParseFlags; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 5 | ▶ | use constant { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 49 | 5 | _flag_SEMAPHORE => 1, | |||||||||||||||||||
| 50 | 5 | }; | |||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | =head1 METHODS | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | =head2 I<CLASS>->new( %OPTS ) | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | %OPTS is: | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | =over | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | =item * C<initval> - Optional, as described in the eventfd documentation. | ||||||||||||||||||||
| 61 | Defaults to 0. | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | =item * C<flags> - Optional, an array reference of any or all of: | ||||||||||||||||||||
| 64 | C<NONBLOCK>, C<CLOEXEC>, C<SEMAPHORE>. See C<man 2 eventfd> for | ||||||||||||||||||||
| 65 | more details. | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | Note that, in conformity with Perl convention, this module honors | ||||||||||||||||||||
| 68 | the $^F variable, which in its default configuration causes CLOEXEC | ||||||||||||||||||||
| 69 | even if the flag is not given. To have a non-CLOEXEC eventfd instance, | ||||||||||||||||||||
| 70 | then, set $^F to a high enough value that the eventfd file descriptor | ||||||||||||||||||||
| 71 | will not be an “OS” filehandle, e.g.: | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | my $eventfd = do { | ||||||||||||||||||||
| 74 | local $^F = 1000; | ||||||||||||||||||||
| 75 | Linux::Perl::eventfd->new(); | ||||||||||||||||||||
| 76 | }; | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | =back | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | =cut | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | 12 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||
| 83 | 12 | my ($class, %opts) = @_; | |||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 12 | local ($!, $^E); | |||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 12 | my $arch_module = $class->_get_arch_module(); | |||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 12 | ▶ | my $initval = 0 + ( $opts{'initval'} || 0 ); | ||||||||||||||||||
|
Condition100%2/2
$opts{'initval'} || 0
MC/DC100%1/1
$opts{'initval'} || 0
$opts{'initval'}
Truth table100%2/2
A
A= $opts{'initval'}
| |||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 12 | my $flags = Linux::Perl::ParseFlags::parse($arch_module, $opts{'flags'}); | |||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 12 | ▶ | my $call = 'NR_' . ($flags ? 'eventfd2' : 'eventfd'); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 12 | ▶ | my $fd = Linux::Perl::call( 0 + $arch_module->$call(), $initval, $flags || () ); | ||||||||||||||||||
|
Condition100%3/3
$flags || ()
MC/DC100%2/2
$flags || ()
$flags
()
Truth table100%3/3
A || B
A= $flagsB= ()
| |||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | #Force CLOEXEC if the flag was given. | ||||||||||||||||||||
| 98 | 12 | ▶ | local $^F = 0 if $flags & $arch_module->_flag_CLOEXEC(); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 12 | open my $fh, '+<&=' . $fd; | |||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 12 | return bless [$fh], $arch_module; | |||||||||||||||||||
| 103 | } | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | #---------------------------------------------------------------------- | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | =head2 $val = I<OBJ>->read() | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | Reads a value from the eventfd instance. Sets C<$!> and returns undef | ||||||||||||||||||||
| 110 | on error. | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | =cut | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | 5 | *read = __PACKAGE__->can('_read'); | |||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | #---------------------------------------------------------------------- | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | =head2 I<OBJ>->add( NUMBER ) | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | Adds NUMBER to the counter. Returns undef and sets C<$!> on failure. | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | =cut | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | 5 | my $packed; | |||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | 8 | ▶ | sub add { | ||||||||||||||||||
|
Subroutine
add: called CC=3 SCAR=12.5
Pod
documented
| |||||||||||||||||||||
| 127 | 8 | ▶ | if ($_[0]->_PERL_CAN_64BIT()) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 128 | 8 | $packed = pack 'Q', $_[1]; | |||||||||||||||||||
| 129 | } | ||||||||||||||||||||
| 130 | elsif (Linux::Perl::Endian::SYSTEM_IS_BIG_ENDIAN) { | ||||||||||||||||||||
| 131 | $packed = pack 'x4N', $_[1]; | ||||||||||||||||||||
| 132 | } | ||||||||||||||||||||
| 133 | else { | ||||||||||||||||||||
| 134 | 0 | $packed = pack 'Vx4', $_[1]; | |||||||||||||||||||
| 135 | } | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | 8 | ▶ | return syswrite( $_[0][0], $packed ) && 1; | ||||||||||||||||||
|
Condition50%1/2
syswrite($_[0][0], $packed) && 1
MC/DC0%0/1
syswrite($_[0][0], $packed) && 1
syswrite($_[0][0], $packed)
Truth table50%1/2
A
A= syswrite($_[0][0], $packed)
| |||||||||||||||||||||
| 138 | } | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | 5 | 1; | |||||||||||||||||||