/
Event.pm
316 lines (245 loc) · 7.1 KB
/
Event.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# $Id$
# Event.pm event loop bridge for POE::Kernel.
# Empty package to appease perl.
package POE::Loop::Event;
use strict;
use vars qw($VERSION);
$VERSION = do {my@r=(q$Revision$=~/\d+/g);sprintf"%d."."%04d"x$#r,@r};
# Everything plugs into POE::Kernel.
package POE::Kernel;
use strict;
my $_watcher_timer;
my @fileno_watcher;
my %signal_watcher;
#------------------------------------------------------------------------------
# Loop construction and destruction.
sub loop_initialize {
my $self = shift;
$_watcher_timer = Event->timer(
cb => \&_loop_event_callback,
after => 0,
parked => 1,
);
}
sub loop_finalize {
foreach my $fd (0..$#fileno_watcher) {
next unless defined $fileno_watcher[$fd];
foreach my $mode (MODE_RD, MODE_WR, MODE_EX) {
POE::Kernel::_warn(
"Mode $mode watcher for fileno $fd is defined during loop finalize"
) if defined $fileno_watcher[$fd]->[$mode];
}
}
}
#------------------------------------------------------------------------------
# Signal handlers/callbacks.
sub _loop_signal_handler_generic {
if (TRACE_SIGNALS) {
POE::Kernel::_warn "<sg> Enqueuing generic SIG$_[0] event";
}
$poe_kernel->_data_ev_enqueue(
$poe_kernel, $poe_kernel, EN_SIGNAL, ET_SIGNAL, [ $_[0]->w->signal ],
__FILE__, __LINE__, time(),
);
}
sub _loop_signal_handler_pipe {
if (TRACE_SIGNALS) {
POE::Kernel::_warn "<sg> Enqueuing PIPE-like SIG$_[0] event";
}
$poe_kernel->_data_ev_enqueue(
$poe_kernel->get_active_session(), $poe_kernel,
EN_SIGNAL, ET_SIGNAL, [ $_[0]->w->signal ],
__FILE__, __LINE__, time(),
);
}
sub _loop_signal_handler_child {
if (TRACE_SIGNALS) {
POE::Kernel::_warn "<sg> Enqueuing CHLD-like SIG$_[0] event";
}
$poe_kernel->_data_ev_enqueue(
$poe_kernel, $poe_kernel, EN_SCPOLL, ET_SCPOLL, [ ],
__FILE__, __LINE__, time(),
);
}
#------------------------------------------------------------------------------
# Signal handler maintenance functions.
sub loop_watch_signal {
my ($self, $signal) = @_;
# Child process has stopped.
if ($signal eq 'CHLD' or $signal eq 'CLD') {
# Begin constant polling loop. Only start it on CHLD or on CLD if
# CHLD doesn't exist.
$SIG{$signal} = 'DEFAULT';
$self->_data_ev_enqueue(
$self, $self, EN_SCPOLL, ET_SCPOLL, [ ],
__FILE__, __LINE__, time() + 1,
) if $signal eq 'CHLD' or not exists $SIG{CHLD};
return;
}
# Broken pipe.
if ($signal eq 'PIPE') {
$signal_watcher{$signal} = Event->signal(
signal => $signal,
cb => \&_loop_signal_handler_pipe
);
return;
}
# Event doesn't like watching nonmaskable signals.
return if $signal eq 'KILL' or $signal eq 'STOP';
# Everything else.
$signal_watcher{$signal} = Event->signal(
signal => $signal,
cb => \&_loop_signal_handler_generic
);
}
sub loop_ignore_signal {
my ($self, $signal) = @_;
if (defined $signal_watcher{$signal}) {
$signal_watcher{$signal}->stop();
delete $signal_watcher{$signal};
}
}
sub loop_attach_uidestroy {
# does nothing
}
#------------------------------------------------------------------------------
# Maintain time watchers.
sub loop_resume_time_watcher {
my ($self, $next_time) = @_;
$_watcher_timer->at($next_time);
$_watcher_timer->start();
}
sub loop_reset_time_watcher {
my ($self, $next_time) = @_;
$self->loop_pause_time_watcher();
$self->loop_resume_time_watcher($next_time);
}
sub loop_pause_time_watcher {
$_watcher_timer->stop();
}
#------------------------------------------------------------------------------
# Maintain filehandle watchers.
sub loop_watch_filehandle {
my ($self, $handle, $mode) = @_;
my $fileno = fileno($handle);
# Overwriting a pre-existing watcher?
if (defined $fileno_watcher[$fileno]->[$mode]) {
$fileno_watcher[$fileno]->[$mode]->cancel();
undef $fileno_watcher[$fileno]->[$mode];
}
$fileno_watcher[$fileno]->[$mode] = Event->io(
fd => $fileno,
poll => (
( $mode == MODE_RD )
? 'r'
: (
( $mode == MODE_WR )
? 'w'
: 'e'
)
),
cb => \&_loop_select_callback,
);
}
sub loop_ignore_filehandle {
my ($self, $handle, $mode) = @_;
my $fileno = fileno($handle);
# Don't bother removing a select if none was registered.
if (defined $fileno_watcher[$fileno]->[$mode]) {
$fileno_watcher[$fileno]->[$mode]->cancel();
undef $fileno_watcher[$fileno]->[$mode];
}
}
sub loop_pause_filehandle {
my ($self, $handle, $mode) = @_;
my $fileno = fileno($handle);
$fileno_watcher[$fileno]->[$mode]->stop();
}
sub loop_resume_filehandle {
my ($self, $handle, $mode) = @_;
my $fileno = fileno($handle);
$fileno_watcher[$fileno]->[$mode]->start();
}
# Timer callback to dispatch events.
my $last_time = time();
sub _loop_event_callback {
my $self = $poe_kernel;
if (TRACE_STATISTICS) {
# TODO - I'm pretty sure the startup time will count as an unfair
# amout of idleness.
#
# TODO - Introducing many new time() syscalls. Bleah.
$self->_data_stat_add('idle_seconds', time() - $last_time);
}
$self->_data_ev_dispatch_due();
# Register the next timed callback if there are events left.
my $next_time = $self->get_next_event_time();
if (defined $next_time) {
$_watcher_timer->at($next_time);
$_watcher_timer->start();
# POE::Kernel's signal polling loop always keeps oe event in the
# queue. We test for an idle kernel if the queue holds only one
# event. A more generic method would be to keep counts of user
# vs. kernel events, and GC the kernel when the user events drop
# to 0.
if ($self->_data_ses_count() == 1) {
$self->_test_if_kernel_is_idle();
}
}
# Make sure the kernel can still run.
else {
$self->_test_if_kernel_is_idle();
}
# Transfering control back to Event; this is idle time.
$last_time = time() if TRACE_STATISTICS;
}
# Event filehandle callback to dispatch selects.
sub _loop_select_callback {
my $self = $poe_kernel;
my $event = shift;
my $watcher = $event->w;
my $fileno = $watcher->fd;
my $mode = (
( $event->got eq 'r' )
? MODE_RD
: (
( $event->got eq 'w' )
? MODE_WR
: (
( $event->got eq 'e' )
? MODE_EX
: return
)
)
);
$self->_data_handle_enqueue_ready($mode, $fileno);
$self->_test_if_kernel_is_idle();
}
#------------------------------------------------------------------------------
# The event loop itself.
sub loop_do_timeslice {
die "doing timeslices currently not supported in the Event loop";
}
sub loop_run {
Event::loop();
}
sub loop_halt {
$_watcher_timer->stop();
Event::unloop_all(0);
}
1;
__END__
=head1 NAME
POE::Loop::Event - a bridge that supports Event.pm from POE
=head1 SYNOPSIS
See L<POE::Loop>.
=head1 DESCRIPTION
This class is an implementation of the abstract POE::Loop interface.
It follows POE::Loop's public interface exactly. Therefore, please
see L<POE::Loop> for its documentation.
=head1 SEE ALSO
L<POE>, L<POE::Loop>, L<Event>
=head1 AUTHORS & LICENSING
Please see L<POE> for more information about authors, contributors,
and POE's licensing.
=cut