found location of hang; make annotations; added more log lines
[scpubgit/Object-Remote.git] / lib / Object / Remote / MiniLoop.pm
CommitLineData
9e72f0cf 1package Object::Remote::MiniLoop;
2
3use IO::Select;
befabdee 4use Time::HiRes qw(time);
5d59cb98 5use Object::Remote::Logging qw( :log :dlog );
9e72f0cf 6use Moo;
7
8# this is ro because we only actually set it using local in sub run
9
10has is_running => (is => 'ro', clearer => 'stop');
11
12has _read_watches => (is => 'ro', default => sub { {} });
13has _read_select => (is => 'ro', default => sub { IO::Select->new });
14
fbd3b8ec 15has _write_watches => (is => 'ro', default => sub { {} });
16has _write_select => (is => 'ro', default => sub { IO::Select->new });
17
befabdee 18has _timers => (is => 'ro', default => sub { [] });
19
9e72f0cf 20sub pass_watches_to {
21 my ($self, $new_loop) = @_;
a63cd862 22 log_debug { "passing watches to new run loop" };
9e72f0cf 23 foreach my $fh ($self->_read_select->handles) {
24 $new_loop->watch_io(
25 handle => $fh,
26 on_read_ready => $self->_read_watches->{$fh}
27 );
28 }
fbd3b8ec 29 foreach my $fh ($self->_write_select->handles) {
30 $new_loop->watch_io(
31 handle => $fh,
32 on_write_ready => $self->_write_watches->{$fh}
33 );
34 }
9e72f0cf 35}
36
37sub watch_io {
38 my ($self, %watch) = @_;
39 my $fh = $watch{handle};
9d64d2d9 40 Dlog_debug { my $type = ref($fh); "Adding IO watch for $_" } $fh;
9e72f0cf 41 if (my $cb = $watch{on_read_ready}) {
9d64d2d9 42 log_trace { "IO watcher is registering with select() for reading" };
9e72f0cf 43 $self->_read_select->add($fh);
44 $self->_read_watches->{$fh} = $cb;
45 }
fbd3b8ec 46 if (my $cb = $watch{on_write_ready}) {
9d64d2d9 47 log_trace { "IO watcher is registering with select() for writing" };
fbd3b8ec 48 $self->_write_select->add($fh);
49 $self->_write_watches->{$fh} = $cb;
50 }
498c4ad5 51 return;
9e72f0cf 52}
53
54sub unwatch_io {
55 my ($self, %watch) = @_;
56 my $fh = $watch{handle};
9d64d2d9 57 Dlog_debug { "Removing IO watch for $_" } $fh;
9e72f0cf 58 if ($watch{on_read_ready}) {
9d64d2d9 59 log_trace { "IO watcher is removing read from select()" };
9e72f0cf 60 $self->_read_select->remove($fh);
61 delete $self->_read_watches->{$fh};
62 }
fbd3b8ec 63 if ($watch{on_write_ready}) {
9d64d2d9 64 log_trace { "IO watcher is removing write from select()" };
fbd3b8ec 65 $self->_write_select->remove($fh);
66 delete $self->_write_watches->{$fh};
67 }
befabdee 68 return;
69}
70
71sub watch_time {
72 my ($self, %watch) = @_;
73 my $at = $watch{at} || do {
74 die "watch_time requires at or after" unless my $after = $watch{after};
75 time() + $after;
76 };
77 die "watch_time requires code" unless my $code = $watch{code};
78 my $timers = $self->_timers;
79 my $new = [ $at => $code ];
80 @{$timers} = sort { $a->[0] <=> $b->[0] } @{$timers}, $new;
5d59cb98 81 log_debug { "Created new timer that expires at '$at'" };
befabdee 82 return "$new";
83}
84
85sub unwatch_time {
86 my ($self, $id) = @_;
a63cd862 87 log_debug { "Removing timer with id of '$id'" };
befabdee 88 @$_ = grep !($_ eq $id), @$_ for $self->_timers;
89 return;
9e72f0cf 90}
91
5d59cb98 92sub _next_timer_expires_delay {
93 my ($self) = @_;
94 my $timers = $self->_timers;
a6786dda 95 #undef means no timeout, select only returns
5d59cb98 96 #when data is ready - when the system
97 #deadlocks the chatter from the timeout in
98 #select clogs up the logs
9d64d2d9 99 #TODO should make this an attribute
5d59cb98 100 my $delay_max = undef;
101
102 return $delay_max unless @$timers;
103 my $duration = $timers->[0]->[0] - time;
104
105 log_trace { "next timer fires in '$duration' seconds " };
106
107 if ($duration < 0) {
108 $duration = 0;
a6786dda 109 } elsif (defined $delay_max && $duration > $delay_max) {
110 $duration = $delay_max;
5d59cb98 111 }
a6786dda 112
9d64d2d9 113 #uncomment for original behavior
114 #return .5;
5d59cb98 115 return $duration;
116}
117
9e72f0cf 118sub loop_once {
119 my ($self) = @_;
120 my $read = $self->_read_watches;
fbd3b8ec 121 my $write = $self->_write_watches;
5d59cb98 122 my $read_count = 0;
123 my $write_count = 0;
a63cd862 124 my @c = caller;
5d59cb98 125 my $wait_time = $self->_next_timer_expires_delay;
126 log_debug { sprintf("Run loop: loop_once() has been invoked by $c[1]:$c[2] with read:%i write:%i select timeout:%s",
127 scalar(keys(%$read)), scalar(keys(%$write)), defined $wait_time ? $wait_time : 'indefinite' ) };
9d64d2d9 128 #TODO The docs state that select() in some instances can return a socket as ready to
129 #read data even if reading from it would block and the recomendation is to set
130 #handles used with select() as non-blocking but Perl on Windows can not set a
131 #handle to use non-blocking IO - If Windows is not one of the operating
132 #systems where select() returns a handle that could block it would work to
133 #enable non-blocking mode only under Posix - the non-blocking sysread()
134 #logic would work unmodified for both blocking and non-blocking handles
135 #under Posix and Windows.
fbd3b8ec 136 my ($readable, $writeable) = IO::Select->select(
9d64d2d9 137 #TODO how come select() isn't used to identify handles with errors on them?
138 #TODO is there a specific reason for a half second maximum wait duration?
139 #The two places I've found for the runloop to be invoked don't return control
140 #to the caller until a controlling variable interrupts the loop that invokes
141 #loop_once() - is this to allow that variable to be polled and exit the
142 #run loop? If so why isn't that behavior event driven and causes select() to
143 #return?
5d59cb98 144 $self->_read_select, $self->_write_select, undef, $wait_time
145 );
a63cd862 146 log_debug {
5d59cb98 147 my $readable_count = defined $readable ? scalar(@$readable) : 0;
148 my $writable_count = defined $writeable ? scalar(@$writeable) : 0;
149 "Run loop: select returned readable:$readable_count writeable:$writable_count";
a63cd862 150 };
9e72f0cf 151 # I would love to trap errors in the select call but IO::Select doesn't
152 # differentiate between an error and a timeout.
153 # -- no, love, mst.
a63cd862 154 log_trace { "Reading from all ready filehandles" };
9e72f0cf 155 foreach my $fh (@$readable) {
5d59cb98 156 next unless $read->{$fh};
157 $read_count++;
158 $read->{$fh}();
159# $read->{$fh}() if $read->{$fh};
fbd3b8ec 160 }
a63cd862 161 log_trace { "Writing to all ready filehandles" };
fbd3b8ec 162 foreach my $fh (@$writeable) {
5d59cb98 163 next unless $write->{$fh};
164 $write_count++;
165 $write->{$fh}();
166# $write->{$fh}() if $write->{$fh};
9e72f0cf 167 }
5d59cb98 168 log_trace { "Read from $read_count filehandles; wrote to $write_count filehandles" };
befabdee 169 my $timers = $self->_timers;
170 my $now = time();
a63cd862 171 log_trace { "Checking timers" };
befabdee 172 while (@$timers and $timers->[0][0] <= $now) {
5d59cb98 173 Dlog_debug { "Found timer that needs to be executed: $_" } $timers->[0];
befabdee 174 (shift @$timers)->[1]->();
175 }
a63cd862 176 log_debug { "Run loop: single loop is completed" };
befabdee 177 return;
9e72f0cf 178}
179
9d64d2d9 180#::Node and ::ConnectionServer use the want_run() / want_stop()
181#counter to cause a run-loop to execute while something is active;
182#the futures do this via a different mechanism
6c597351 183sub want_run {
184 my ($self) = @_;
5d59cb98 185 Dlog_debug { "Run loop: Incrimenting want_running, is now $_" }
186 ++$self->{want_running};
6c597351 187}
188
189sub run_while_wanted {
190 my ($self) = @_;
9d64d2d9 191 log_debug { my $wr = $self->{want_running}; "Run loop: run_while_wanted() invoked; want_running: $wr" };
6c597351 192 $self->loop_once while $self->{want_running};
a63cd862 193 log_debug { "Run loop: run_while_wanted() completed" };
befabdee 194 return;
6c597351 195}
196
197sub want_stop {
198 my ($self) = @_;
5d59cb98 199 if (! $self->{want_running}) {
200 log_debug { "Run loop: want_stop() was called but want_running was not true" };
201 return;
202 }
203 Dlog_debug { "Run loop: decrimenting want_running, is now $_" }
204 --$self->{want_running};
6c597351 205}
206
9d64d2d9 207#TODO Hypothesis: Futures invoke run() which gives that future
208#it's own localized is_running attribute - any adjustment to the
209#is_running attribute outside of that future will not effect that
210#future so each future winds up able to call run() and stop() at
211#will with out interfering with each other
9e72f0cf 212sub run {
213 my ($self) = @_;
a63cd862 214 log_info { "Run loop: run() invoked" };
9e72f0cf 215 local $self->{is_running} = 1;
216 while ($self->is_running) {
217 $self->loop_once;
218 }
a63cd862 219 log_info { "Run loop: run() completed" };
befabdee 220 return;
9e72f0cf 221}
222
2231;