| 1 | package Object::Remote::Loop; | ||
| 2 | |||
| 3 | use Scalar::Util qw( refaddr weaken ); | ||
| 4 | use Module::Runtime qw( use_module ); | ||
| 5 | use Object::Remote::MiniLoop; | ||
| 6 | use Object::Remote::Logging qw( :log :dlog router ); | ||
| 7 | use Moo; | ||
| 8 | |||
| 9 | BEGIN { | ||
| 10 | $SIG{PIPE} = sub { log_debug { "Got a PIPE signal" } }; | ||
| 11 | |||
| 12 | router()->exclude_forwarding | ||
| 13 | } | ||
| 14 | |||
| 15 | has loop => ( | ||
| 16 | is => 'ro', | ||
| 17 | default => sub { | ||
| 18 | if ($ENV{OBJECT_REMOTE_LOOP}) { | ||
| 19 | return use_module($ENV{OBJECT_REMOTE_LOOP})->new; | ||
| 20 | } | ||
| 21 | Object::Remote::MiniLoop->new | ||
| 22 | }); | ||
| 23 | |||
| 24 | has _timers => (is => 'ro', default => sub { {} }); | ||
| 25 | |||
| 26 | for my $fn (qw( watch_io unwatch_io | ||
| 27 | loop_once run stop await await_all | ||
| 28 | )) { | ||
| 29 | my $code = "sub ${fn} { my \$self = shift; \$self->loop->${fn}(\@_); }"; | ||
| 30 | eval $code; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub want_run { | ||
| 34 | my ($self) = @_; | ||
| 35 | Dlog_debug { "Run loop: Incremeting want_running, is now $_" } | ||
| 36 | ++$self->{want_running}; | ||
| 37 | } | ||
| 38 | |||
| 39 | sub run_while_wanted { | ||
| 40 | my ($self) = @_; | ||
| 41 | log_debug { my $wr = $self->{want_running}; "Run loop: run_while_wanted() invoked; want_running: $wr" }; | ||
| 42 | $self->loop_once while $self->{want_running}; | ||
| 43 | log_debug { "Run loop: run_while_wanted() completed" }; | ||
| 44 | return; | ||
| 45 | } | ||
| 46 | |||
| 47 | sub want_stop { | ||
| 48 | my ($self) = @_; | ||
| 49 | if (! $self->{want_running}) { | ||
| 50 | log_debug { "Run loop: want_stop() was called but want_running was not true" }; | ||
| 51 | return; | ||
| 52 | } | ||
| 53 | Dlog_debug { "Run loop: decrimenting want_running, is now $_" } | ||
| 54 | --$self->{want_running}; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub new_future { | ||
| 58 | return $_[0]->loop->new_future; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub watch_time { | ||
| 62 | my ($self, %watch) = @_; | ||
| 63 | my ($code, $id); | ||
| 64 | my $our_id; | ||
| 65 | if (not ($watch{every} or $watch{after} or $watch{at})) { | ||
| 66 | die "watch_time requires every, after or at"; | ||
| 67 | } | ||
| 68 | elsif ($watch{every}) { | ||
| 69 | $code = sub { | ||
| 70 | $id = $self->loop->watch_time( %watch, code => $code ); | ||
| 71 | $watch{code}->(@_); | ||
| 72 | }; | ||
| 73 | $watch{after} = delete $watch{every}; | ||
| 74 | } | ||
| 75 | else { | ||
| 76 | $code = sub { | ||
| 77 | delete $self->_timers->{"$our_id"}; | ||
| 78 | $watch{code}->(@_); | ||
| 79 | } | ||
| 80 | } | ||
| 81 | $our_id = refaddr $code; | ||
| 82 | $self->_timers->{$our_id} = \$id; | ||
| 83 | $id = $self->loop->watch_time( %watch, code => $code ); | ||
| 84 | weaken($code); | ||
| 85 | return $our_id; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub unwatch_time { | ||
| 89 | my ($self, $id) = @_; | ||
| 90 | my $timer = delete $self->_timers->{$id}; | ||
| 91 | if ($timer) { | ||
| 92 | $self->loop->unwatch_time( ${$timer} ) | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | 1; |