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