| 1 |
root |
1.1 |
package AnyEvent::Fork::RPC::Sync; |
| 2 |
|
|
|
| 3 |
|
|
use common::sense; # actually required to avoid spurious warnings... |
| 4 |
|
|
|
| 5 |
|
|
our ($master, $f, $t); |
| 6 |
|
|
|
| 7 |
|
|
sub main::test {#d# |
| 8 |
|
|
(@_, 5) |
| 9 |
|
|
} |
| 10 |
|
|
|
| 11 |
|
|
sub xwrite($) { |
| 12 |
|
|
my $got = syswrite $master, $_[0]; |
| 13 |
|
|
|
| 14 |
|
|
while ($got < length $_[0]) { |
| 15 |
|
|
my $len = syswrite $master, $_[0], 1<<30, $got; |
| 16 |
|
|
|
| 17 |
|
|
defined $len |
| 18 |
|
|
or die "AnyEvent::Fork::RPC::Sync: write error ($!), parent gone?"; |
| 19 |
|
|
|
| 20 |
|
|
$got += $len; |
| 21 |
|
|
} |
| 22 |
|
|
} |
| 23 |
|
|
|
| 24 |
root |
1.2 |
sub rlen($) { ($_[0] < 384 ? 512 + 16 : 2 << int +(log $_[0] + 512) / log 2) - $_[0] - 16 } |
| 25 |
|
|
|
| 26 |
root |
1.1 |
# the goal here is to keep this simple, small and efficient |
| 27 |
|
|
sub run { |
| 28 |
|
|
$master = shift; |
| 29 |
|
|
|
| 30 |
|
|
my ($function, $init, $serialiser) = splice @_, -3, 3,; |
| 31 |
|
|
|
| 32 |
|
|
{ |
| 33 |
|
|
package main; |
| 34 |
|
|
&$init if length $init; |
| 35 |
|
|
$function = \&$function; # resolve function early for extra speed |
| 36 |
|
|
} |
| 37 |
|
|
|
| 38 |
|
|
($f, $t) = eval $serialiser; die $@ if $@; |
| 39 |
|
|
|
| 40 |
|
|
my $rbuf; |
| 41 |
|
|
|
| 42 |
root |
1.2 |
# the read length here is not optimal, but since we re-use $rbuf, |
| 43 |
|
|
# its allocated size should eventually auto-adjust. |
| 44 |
|
|
while (sysread $master, $rbuf, rlen length $rbuf, length $rbuf) { |
| 45 |
|
|
while () { |
| 46 |
|
|
last if 5 > length $rbuf; |
| 47 |
|
|
my $len = unpack "L", $rbuf; |
| 48 |
|
|
last if 4 + $len > length $rbuf; |
| 49 |
|
|
my @r = $t->(substr $rbuf, 4, $len); |
| 50 |
|
|
substr $rbuf, 0, 4 + $len, ""; |
| 51 |
root |
1.1 |
|
| 52 |
root |
1.2 |
xwrite pack "L/a*", $f->($function->(@r), ""); |
| 53 |
|
|
} |
| 54 |
root |
1.1 |
} |
| 55 |
|
|
|
| 56 |
|
|
shutdown $master, 1; |
| 57 |
|
|
} |
| 58 |
|
|
|
| 59 |
|
|
sub AnyEvent::Fork::RPC::event { |
| 60 |
|
|
xwrite pack "L/a*", $f->(@_, 1); |
| 61 |
|
|
} |
| 62 |
|
|
|
| 63 |
|
|
1 |
| 64 |
|
|
|