| 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 | =cut | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 5 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 32 | 5 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 5 | ▶ | use Module::Load; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 5 | ▶ | use Linux::Perl; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 37 | 5 | ▶ | use Linux::Perl::Constants; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 38 | 5 | ▶ | use Linux::Perl::Constants::Fcntl; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 39 | 5 | ▶ | use Linux::Perl::Endian; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | use constant { | ||||||||||||||||||||
| 42 | 5 | PERL_CAN_64BIT => !!do { local $@; eval { pack 'Q', 1 } }, | |||||||||||||||||||
| 43 | 5 | ▶ | }; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 5 | *flag_CLOEXEC = *Linux::Perl::Constants::Fcntl::flag_CLOEXEC; | |||||||||||||||||||
| 46 | 5 | *flag_NONBLOCK = *Linux::Perl::Constants::Fcntl::flag_NONBLOCK; | |||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | =head1 METHODS | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | =head2 I<CLASS>->new( %OPTS ) | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | %OPTS is: | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | =over | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | =item * C<initval> - Optional, as described in the eventfd documentation. | ||||||||||||||||||||
| 57 | Defaults to 0. | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | =item * C<flags> - Optional, an array reference of any or all of: | ||||||||||||||||||||
| 60 | C<NONBLOCK>, C<CLOEXEC>, C<SEMAPHORE>. See C<man 2 eventfd> for | ||||||||||||||||||||
| 61 | more details. | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | Note that, in conformity with Perl convention, this module honors | ||||||||||||||||||||
| 64 | the $^F variable, which in its default configuration causes CLOEXEC | ||||||||||||||||||||
| 65 | even if the flag is not given. To have a non-CLOEXEC eventfd instance, | ||||||||||||||||||||
| 66 | then, set $^F to a high enough value that the eventfd file descriptor | ||||||||||||||||||||
| 67 | will not be an “OS” filehandle, e.g.: | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | my $eventfd = do { | ||||||||||||||||||||
| 70 | local $^F = 1000; | ||||||||||||||||||||
| 71 | Linux::Perl::eventfd->new(); | ||||||||||||||||||||
| 72 | }; | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | =back | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | =cut | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | sub new { | ||||||||||||||||||||
| 79 | 10 | ▶ | my ($class, %opts) = @_; | ||||||||||||||||||
|
Subroutine
new: called CC=11 SCAR=24.1
Pod
documented
| |||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 10 | local ($!, $^E); | |||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | 10 | ▶ | 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
| |||||||||||||||||||||
| 84 | 10 | ▶ | $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)
}
| |||||||||||||||||||||
| 85 | 6 | require Linux::Perl::ArchLoader; | |||||||||||||||||||
| 86 | 6 | Linux::Perl::ArchLoader::get_arch_module($class); | |||||||||||||||||||
| 87 | }; | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 10 | ▶ | 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 | 10 | my $is_cloexec; | |||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 10 | my $flags = 0; | |||||||||||||||||||
| 94 | 10 | ▶ | if ( $opts{'flags'} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 95 | 4 | for my $fl ( @{ $opts{'flags'} } ) { | |||||||||||||||||||
| 96 | 4 | ▶ | my $val_cr = $arch_module->can("flag_$fl") or do { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 97 | 0 | die "unknown flag: “$fl”"; | |||||||||||||||||||
| 98 | }; | ||||||||||||||||||||
| 99 | 4 | $flags |= $val_cr->(); | |||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 4 | ▶ | $is_cloexec = 1 if $fl eq 'CLOEXEC'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | } | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 10 | ▶ | my $call = 'NR_' . ($flags ? 'eventfd2' : 'eventfd'); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 10 | ▶ | 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= ()
| |||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | #Force CLOEXEC if the flag was given. | ||||||||||||||||||||
| 110 | 10 | ▶ | local $^F = 0 if $is_cloexec; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | 10 | open my $fh, '+<&=' . $fd; | |||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | 10 | return bless [$fh], $arch_module; | |||||||||||||||||||
| 115 | } | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | =head2 I<OBJ>->fileno() | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | Returns the file descriptor number. | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | =cut | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | 10 | ▶ | sub fileno { fileno $_[0][0] } | ||||||||||||||||||
|
Subroutine
fileno: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | =head2 $val = I<OBJ>->read() | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | Reads a value from the eventfd instance. Sets C<$!> and returns undef | ||||||||||||||||||||
| 128 | on error. | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | =cut | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | 5 | my ($big, $low); | |||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | sub read { | ||||||||||||||||||||
| 135 | 8 | ▶ | return undef if !sysread $_[0][0], my $buf, 8; | ||||||||||||||||||
|
Subroutine
read: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | 6 | if (PERL_CAN_64BIT) { | |||||||||||||||||||
| 138 | 6 | ($big, $low) = (0, unpack('Q', $buf)); | |||||||||||||||||||
| 139 | } | ||||||||||||||||||||
| 140 | else { | ||||||||||||||||||||
| 141 | if (Linux::Perl::Endian::SYSTEM_IS_BIG_ENDIAN) { | ||||||||||||||||||||
| 142 | ($big, $low) = unpack 'NN', $buf; | ||||||||||||||||||||
| 143 | } | ||||||||||||||||||||
| 144 | else { | ||||||||||||||||||||
| 145 | ($low, $big) = unpack 'VV', $buf; | ||||||||||||||||||||
| 146 | } | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | #TODO: Need to test what happens on a 32-bit Perl. | ||||||||||||||||||||
| 149 | die "No 64-bit support! (high=$big, low=$low)" if $big; | ||||||||||||||||||||
| 150 | } | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | 6 | return $low; | |||||||||||||||||||
| 153 | } | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | =head2 I<OBJ>->add( NUMBER ) | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | Adds NUMBER to the counter. | ||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | =cut | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | 5 | my $packed; | |||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | sub add { | ||||||||||||||||||||
| 164 | 8 | ▶ | if (PERL_CAN_64BIT) { | ||||||||||||||||||
|
Subroutine
add: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||
| 165 | 8 | $packed = pack 'Q', $_[1]; | |||||||||||||||||||
| 166 | } | ||||||||||||||||||||
| 167 | elsif (Linux::Perl::Endian::SYSTEM_IS_BIG_ENDIAN) { | ||||||||||||||||||||
| 168 | $packed = pack 'x4N', $_[1]; | ||||||||||||||||||||
| 169 | } | ||||||||||||||||||||
| 170 | else { | ||||||||||||||||||||
| 171 | $packed = pack 'Vx4', $_[1]; | ||||||||||||||||||||
| 172 | } | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | 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)
| |||||||||||||||||||||
| 175 | } | ||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | 5 | 1; | |||||||||||||||||||