| 1 | package AnyEvent::XSPromises::Loader; | ||||||||||
| 2 | 2 | ▶ | use 5.010; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 2 | our $VERSION = '0.006'; | |||||||||
| 7 | |||||||||||
| 8 | 2 | ▶ | use AnyEvent; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 2 | require XSLoader; | |||||||||
| 11 | 2 | XSLoader::load('AnyEvent::XSPromises', $VERSION); | |||||||||
| 12 | |||||||||||
| 13 | 5 | ▶ | AnyEvent::XSPromises::___set_conversion_helper(sub { | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||
| 14 | 5 | my $promise= shift; | |||||||||
| 15 | 5 | my $deferred= AnyEvent::XSPromises::deferred(); | |||||||||
| 16 | 5 | my $called; | |||||||||
| 17 | eval { | ||||||||||
| 18 | 5 | ▶ | $promise->then(sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 19 | 5 | ▶ | return if $called++; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 20 | 4 | $deferred->resolve(@_); | |||||||||
| 21 | 3 | ▶ | }, sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 22 | 3 | ▶ | return if $called++; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 23 | 1 | $deferred->reject(@_); | |||||||||
| 24 | 5 | }); | |||||||||
| 25 | 4 | 1; | |||||||||
| 26 | 5 | ▶ | } or do { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | 1 | my $error= $@; | |||||||||
| 28 | 1 | ▶ | if (!$called++) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | 0 | $deferred->reject($error); | |||||||||
| 30 | } | ||||||||||
| 31 | }; | ||||||||||
| 32 | 5 | return $deferred->promise; | |||||||||
| 33 | 2 | }); | |||||||||
| 34 | |||||||||||
| 35 | # We do not use AE::postpone, because it sets a timer of 0 seconds. While that sounds great in | ||||||||||
| 36 | # theory, the underlying libraries (eg. epoll, used by EV) don't support 0 second timers, and | ||||||||||
| 37 | # so they get passed 1ms instead. To avoid actually waiting a millisecond every time, we write | ||||||||||
| 38 | # data onto a socket read by the event loop. Of course, these sockets need to be carefully managed | ||||||||||
| 39 | # in case the code does a fork, so we need to frequently check $$. | ||||||||||
| 40 | 2 | my ($AE_PID, $AE_WATCH, $PIPE_IN, $PIPE_OUT); | |||||||||
| 41 | 2 | ▶ | BEGIN { $AE_PID= -1; } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 42 | |||||||||||
| 43 | 5 | ▶ | sub ___notify_callback { | ||||||||
|
Subroutine
___notify_callback: called CC=2 SCAR=7.4
| |||||||||||
| 44 | 5 | ▶ | if ($$ != $AE_PID) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | 0 | ___reset_pipe(); | |||||||||
| 46 | } else { | ||||||||||
| 47 | 5 | sysread $PIPE_IN, my $read_buf, 16; | |||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | # sort makes perl push a pseudo-block on the stack that prevents callback code from using | ||||||||||
| 51 | # next/last/redo. Without it, an accidental invocation of one of those could cause serious | ||||||||||
| 52 | # problems. We have to assign it to @useless_variable or Perl thinks our code is a no-op | ||||||||||
| 53 | # and optimizes it away. | ||||||||||
| 54 | 5 | my @useless_variable= sort { AnyEvent::XSPromises::___flush(); 0 } 1, 2; | |||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | 2 | ▶ | sub ___reset_pipe { | ||||||||
|
Subroutine
___reset_pipe: called CC=3 SCAR=11.3
| |||||||||||
| 58 | 2 | ▶ | close $PIPE_IN if $PIPE_IN; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | 2 | ▶ | close $PIPE_OUT if $PIPE_OUT; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | 2 | pipe($PIPE_IN, $PIPE_OUT); | |||||||||
| 61 | 2 | $AE_WATCH= AE::io($PIPE_IN, 0, \&___notify_callback); | |||||||||
| 62 | 2 | $AE_PID= $$; | |||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | 5 | ▶ | AnyEvent::XSPromises::___set_backend(sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 66 | 5 | ▶ | ___reset_pipe() if $$ != $AE_PID; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 67 | 5 | syswrite $PIPE_OUT, "\0"; | |||||||||
| 68 | 2 | }); | |||||||||
| 69 | |||||||||||
| 70 | 2 | 1; | |||||||||