blib/lib/Proc/Spawner/Loop/Win32.pm untested

filter: Statementstmt 0.0% 0 / 58 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 133 CC 12 SCAR 29.5
CC
12
Cov
0%
wait
56.0
status
12.0
pipe
12.0
CRAP 19.2
1 package Proc::Spawner::Loop::Win32;
2 # EXPERIMENTAL (W5, post-0.10; the user, 2026-11-08: "Use the Windows' way"). The Windows backend of
3 # Proc::Spawner::Loop: one I/O completion port (win_loop.h, through Proc::Spawner's XSUBs __wl_*). Overlapped I/O on a
4 # handle opened for it; a helper thread for a synchronous handle; RegisterWaitForSingleObject for a child's end, whose
5 # status then comes from waitpid. The interface (new, begin, end, read, write, child, cancel, wait, pipe) is the one
6 # every backend has.
7 use strict; use warnings;
8 use POSIX ();
9 use Proc::Spawner (); # (its XSUBs __wl_io, __wl_child, __wl_cancel, __wl_wait, __wl_pipe)
10 our $VERSION = '0.11';
11 our $CTRL = 1; # wake up for Ctrl-C (and the other console control events) while wait_until runs
12  
13 sub new { bless { kid => {}, now => [] }, shift }
14 sub begin { Proc::Spawner::__wl_ctrl(1) if $CTRL } # (process-wide while wait_until runs, as SIGCHLD on POSIX: D3)
15 sub end { Proc::Spawner::__wl_ctrl(0) }
16  
17 sub failed { my ($self, @e) = @_; push @{$self->{now}}, [@e]; return } # an event for the next wait: an operation that
18 # could not start
19 sub read { my ($self, $id, $fd, $max) = @_;
20 Proc::Spawner::__wl_io($id, 'r', Proc::Spawner::Loop::fd_of($fd), $max) or $self->failed($id, undef, $! + 0) }
21 sub write { my ($self, $id, $fd, $bytes) = @_;
22 Proc::Spawner::__wl_io($id, 'w', Proc::Spawner::Loop::fd_of($fd), $bytes) or $self->failed($id, $! + 0) }
23 sub child { my ($self, $id, $pid) = @_;
24 $self->{kid}{$id} = $pid;
25 Proc::Spawner::__wl_child($id, $pid) or $self->failed($id, $self->status(delete $self->{kid}{$id})) }
26 sub cancel { my ($self, $id) = @_;
27 Proc::Spawner::__wl_cancel($id); delete $self->{kid}{$id}; @{$self->{now}} = grep $_->[0] != $id, @{$self->{now}} }
28  
29 sub status { # the wait status of an ended child, or undef if other code took it
30 my ($self, $pid) = @_;
31 my $r = waitpid $pid, POSIX::WNOHANG(); my $w = $r > 0 ? $? : undef;
32 Proc::Spawner::Loop::tr_("reaped $pid: ", defined $w ? "wait status $w" : 'status taken by other code');
33 $w }
34  
35 sub wait { # the events of one round: [ID, ARGS...], at most TIMEOUT seconds (undef: until there is one, or a console
36 my ($self, $timeout) = @_; # control event for Perl's signal handling, or a slice while an alarm is set: win_loop.h)
37 if (my @e = splice @{$self->{now}}) { return @e }
38 map { my ($id, $kind, $bytes, $err) = @$_;
39 $kind eq 'r' ? [$id, $bytes, $err ? $err : ()]
40 : $kind eq 'w' ? [$id, $err ? $err : ()]
41 : [$id, $self->status(delete $self->{kid}{$id})] } Proc::Spawner::__wl_wait(defined $timeout ? int($timeout * 1000 + 0.5) : -1) }
42  
43 sub pipe { # (READ, WRITE) handles; overlapped => 'r', 'w' or 'rw': those ends opened for overlapped I/O
44 my ($self, %o) = @_; my $ov = $o{overlapped} // '';
45 my ($r, $w) = Proc::Spawner::__wl_pipe($ov =~ /r/ ? 1 : 0, $ov =~ /w/ ? 1 : 0) or return;
46 open my $rh, '<&=', $r or return; open my $wh, '>&=', $w or return;
47 binmode $_ for $rh, $wh; ($rh, $wh) }
48  
49 1;