1 package Object::Remote::MiniLoop;
4 use Time::HiRes qw(time);
5 use Object::Remote::Logging qw( :log :dlog );
8 # this is ro because we only actually set it using local in sub run
10 has is_running => (is => 'ro', clearer => 'stop');
12 has _read_watches => (is => 'ro', default => sub { {} });
13 has _read_select => (is => 'ro', default => sub { IO::Select->new });
15 has _write_watches => (is => 'ro', default => sub { {} });
16 has _write_select => (is => 'ro', default => sub { IO::Select->new });
18 has _timers => (is => 'ro', default => sub { [] });
21 my ($self, $new_loop) = @_;
22 log_debug { "passing watches to new run loop" };
23 foreach my $fh ($self->_read_select->handles) {
26 on_read_ready => $self->_read_watches->{$fh}
29 foreach my $fh ($self->_write_select->handles) {
32 on_write_ready => $self->_write_watches->{$fh}
38 my ($self, %watch) = @_;
39 my $fh = $watch{handle};
40 log_debug { my $type = ref($fh); "Adding watch for ref of type '$type'" };
41 if (my $cb = $watch{on_read_ready}) {
42 log_trace { "IO watcher on_read_ready has been invoked" };
43 $self->_read_select->add($fh);
44 $self->_read_watches->{$fh} = $cb;
46 if (my $cb = $watch{on_write_ready}) {
47 log_trace { "IO watcher on_write_ready has been invoked" };
48 $self->_write_select->add($fh);
49 $self->_write_watches->{$fh} = $cb;
55 my ($self, %watch) = @_;
56 my $fh = $watch{handle};
57 log_debug { my $type = ref($fh); "Removing watch for ref of type '$type'" };
58 if ($watch{on_read_ready}) {
59 $self->_read_select->remove($fh);
60 delete $self->_read_watches->{$fh};
62 if ($watch{on_write_ready}) {
63 $self->_write_select->remove($fh);
64 delete $self->_write_watches->{$fh};
70 my ($self, %watch) = @_;
71 my $at = $watch{at} || do {
72 die "watch_time requires at or after" unless my $after = $watch{after};
75 die "watch_time requires code" unless my $code = $watch{code};
76 my $timers = $self->_timers;
77 my $new = [ $at => $code ];
78 @{$timers} = sort { $a->[0] <=> $b->[0] } @{$timers}, $new;
79 log_debug { "Created new timer that expires at '$at'" };
85 log_debug { "Removing timer with id of '$id'" };
86 @$_ = grep !($_ eq $id), @$_ for $self->_timers;
90 sub _next_timer_expires_delay {
92 my $timers = $self->_timers;
93 #undef means no timeout, only returns
94 #when data is ready - when the system
95 #deadlocks the chatter from the timeout in
96 #select clogs up the logs
97 my $delay_max = undef;
99 return $delay_max unless @$timers;
100 my $duration = $timers->[0]->[0] - time;
102 log_trace { "next timer fires in '$duration' seconds " };
106 } elsif (! defined($delay_max)) {
108 } elsif ($duration > $delay_max) {
109 $duration = $delay_max;
117 my $read = $self->_read_watches;
118 my $write = $self->_write_watches;
122 my $wait_time = $self->_next_timer_expires_delay;
123 log_debug { sprintf("Run loop: loop_once() has been invoked by $c[1]:$c[2] with read:%i write:%i select timeout:%s",
124 scalar(keys(%$read)), scalar(keys(%$write)), defined $wait_time ? $wait_time : 'indefinite' ) };
125 my ($readable, $writeable) = IO::Select->select(
126 $self->_read_select, $self->_write_select, undef, $wait_time
129 my $readable_count = defined $readable ? scalar(@$readable) : 0;
130 my $writable_count = defined $writeable ? scalar(@$writeable) : 0;
131 "Run loop: select returned readable:$readable_count writeable:$writable_count";
133 # I would love to trap errors in the select call but IO::Select doesn't
134 # differentiate between an error and a timeout.
136 log_trace { "Reading from all ready filehandles" };
137 foreach my $fh (@$readable) {
138 next unless $read->{$fh};
141 # $read->{$fh}() if $read->{$fh};
143 log_trace { "Writing to all ready filehandles" };
144 foreach my $fh (@$writeable) {
145 next unless $write->{$fh};
148 # $write->{$fh}() if $write->{$fh};
150 log_trace { "Read from $read_count filehandles; wrote to $write_count filehandles" };
151 my $timers = $self->_timers;
153 log_trace { "Checking timers" };
154 while (@$timers and $timers->[0][0] <= $now) {
155 Dlog_debug { "Found timer that needs to be executed: $_" } $timers->[0];
156 (shift @$timers)->[1]->();
158 log_debug { "Run loop: single loop is completed" };
164 Dlog_debug { "Run loop: Incrimenting want_running, is now $_" }
165 ++$self->{want_running};
168 sub run_while_wanted {
170 log_debug { "Run loop: run_while_wanted() invoked" };
171 $self->loop_once while $self->{want_running};
172 log_debug { "Run loop: run_while_wanted() completed" };
178 if (! $self->{want_running}) {
179 log_debug { "Run loop: want_stop() was called but want_running was not true" };
182 Dlog_debug { "Run loop: decrimenting want_running, is now $_" }
183 --$self->{want_running};
188 log_info { "Run loop: run() invoked" };
189 local $self->{is_running} = 1;
190 while ($self->is_running) {
193 log_info { "Run loop: run() completed" };