… | |
… | |
70 | You can create I/O watcher by calling the C<< AnyEvent->io >> method with |
70 | You can create I/O watcher by calling the C<< AnyEvent->io >> method with |
71 | the following mandatory arguments: |
71 | the following mandatory arguments: |
72 | |
72 | |
73 | C<fh> the Perl I<filehandle> (not filedescriptor) to watch for |
73 | C<fh> the Perl I<filehandle> (not filedescriptor) to watch for |
74 | events. C<poll> must be a string that is either C<r> or C<w>, that creates |
74 | events. C<poll> must be a string that is either C<r> or C<w>, that creates |
75 | a watcher waiting for "r"eadable or "w"ritable events. C<cb> teh callback |
75 | a watcher waiting for "r"eadable or "w"ritable events. C<cb> the callback |
76 | to invoke everytime the filehandle becomes ready. |
76 | to invoke everytime the filehandle becomes ready. |
77 | |
77 | |
78 | Only one io watcher per C<fh> and C<poll> combination is allowed (i.e. on |
78 | Only one io watcher per C<fh> and C<poll> combination is allowed (i.e. on |
79 | a socket you can have one r + one w, not any more (limitation comes from |
79 | a socket you can have one r + one w, not any more (limitation comes from |
80 | Tk - if you are sure you are not using Tk this limitation is gone). |
80 | Tk - if you are sure you are not using Tk this limitation is gone). |
… | |
… | |
89 | chomp (my $input = <STDIN>); |
89 | chomp (my $input = <STDIN>); |
90 | warn "read: $input\n"; |
90 | warn "read: $input\n"; |
91 | undef $w; |
91 | undef $w; |
92 | }); |
92 | }); |
93 | |
93 | |
94 | =head2 TIMER WATCHERS |
94 | =head2 TIME WATCHERS |
95 | |
95 | |
96 | You can create a timer watcher by calling the C<< AnyEvent->timer >> |
96 | You can create a time watcher by calling the C<< AnyEvent->timer >> |
97 | method with the following mandatory arguments: |
97 | method with the following mandatory arguments: |
98 | |
98 | |
99 | C<after> after how many seconds (fractions are supported) should the timer |
99 | C<after> after how many seconds (fractions are supported) should the timer |
100 | activate. C<cb> the callback to invoke. |
100 | activate. C<cb> the callback to invoke. |
101 | |
101 | |
… | |
… | |
109 | my $w = AnyEvent->timer (after => 7.7, cb => sub { |
109 | my $w = AnyEvent->timer (after => 7.7, cb => sub { |
110 | warn "timeout\n"; |
110 | warn "timeout\n"; |
111 | }); |
111 | }); |
112 | |
112 | |
113 | # to cancel the timer: |
113 | # to cancel the timer: |
114 | undef $w |
114 | undef $w; |
115 | |
115 | |
116 | =head2 CONDITION WATCHERS |
116 | =head2 CONDITION WATCHERS |
117 | |
117 | |
118 | Condition watchers can be created by calling the C<< AnyEvent->condvar >> |
118 | Condition watchers can be created by calling the C<< AnyEvent->condvar >> |
119 | method without any arguments. |
119 | method without any arguments. |
… | |
… | |
157 | |
157 | |
158 | $result_ready->wait; |
158 | $result_ready->wait; |
159 | |
159 | |
160 | =back |
160 | =back |
161 | |
161 | |
|
|
162 | =head2 SIGNAL WATCHERS |
|
|
163 | |
|
|
164 | You can listen for signals using a signal watcher, C<signal> is the signal |
|
|
165 | I<name> without any C<SIG> prefix. Multiple signals events can be clumped |
|
|
166 | together into one callback invocation, and callback invocation might or |
|
|
167 | might not be asynchronous. |
|
|
168 | |
|
|
169 | These watchers might use C<%SIG>, so programs overwriting those signals |
|
|
170 | directly will likely not work correctly. |
|
|
171 | |
|
|
172 | Example: exit on SIGINT |
|
|
173 | |
|
|
174 | my $w = AnyEvent->signal (signal => "INT", cb => sub { exit 1 }); |
|
|
175 | |
|
|
176 | =head2 CHILD PROCESS WATCHERS |
|
|
177 | |
|
|
178 | You can also listen for the status of a child process specified by the |
|
|
179 | C<pid> argument (or any child if the pid argument is 0). The watcher will |
|
|
180 | trigger as often as status change for the child are received. This works |
|
|
181 | by installing a signal handler for C<SIGCHLD>. The callback will be called with |
|
|
182 | the pid and exit status (as returned by waitpid). |
|
|
183 | |
|
|
184 | Example: wait for pid 1333 |
|
|
185 | |
|
|
186 | my $w = AnyEvent->child (pid => 1333, cb => sub { warn "exit status $?" }); |
|
|
187 | |
162 | =head1 GLOBALS |
188 | =head1 GLOBALS |
163 | |
189 | |
164 | =over 4 |
190 | =over 4 |
165 | |
191 | |
166 | =item $AnyEvent::MODEL |
192 | =item $AnyEvent::MODEL |
… | |
… | |
171 | C<AnyEvent::Impl:xxx> modules, but can be any other class in the case |
197 | C<AnyEvent::Impl:xxx> modules, but can be any other class in the case |
172 | AnyEvent has been extended at runtime (e.g. in I<rxvt-unicode>). |
198 | AnyEvent has been extended at runtime (e.g. in I<rxvt-unicode>). |
173 | |
199 | |
174 | The known classes so far are: |
200 | The known classes so far are: |
175 | |
201 | |
|
|
202 | AnyEvent::Impl::CoroEV based on Coro::EV, best choice. |
|
|
203 | AnyEvent::Impl::EV based on EV (an interface to libev, also best choice). |
176 | AnyEvent::Impl::Coro based on Coro::Event, best choise. |
204 | AnyEvent::Impl::Coro based on Coro::Event, second best choice. |
177 | AnyEvent::Impl::Event based on Event, also best choice :) |
205 | AnyEvent::Impl::Event based on Event, also second best choice :) |
178 | AnyEvent::Impl::Glib based on Glib, second-best choice. |
206 | AnyEvent::Impl::Glib based on Glib, second-best choice. |
179 | AnyEvent::Impl::Tk based on Tk, very bad choice. |
207 | AnyEvent::Impl::Tk based on Tk, very bad choice. |
180 | AnyEvent::Impl::Perl pure-perl implementation, inefficient. |
208 | AnyEvent::Impl::Perl pure-perl implementation, inefficient. |
|
|
209 | |
|
|
210 | =item AnyEvent::detect |
|
|
211 | |
|
|
212 | Returns C<$AnyEvent::MODEL>, forcing autodetection of the event model if |
|
|
213 | necessary. You should only call this function right before you would have |
|
|
214 | created an AnyEvent watcher anyway, that is, very late at runtime. |
181 | |
215 | |
182 | =back |
216 | =back |
183 | |
217 | |
184 | =head1 WHAT TO DO IN A MODULE |
218 | =head1 WHAT TO DO IN A MODULE |
185 | |
219 | |
… | |
… | |
214 | =cut |
248 | =cut |
215 | |
249 | |
216 | package AnyEvent; |
250 | package AnyEvent; |
217 | |
251 | |
218 | no warnings; |
252 | no warnings; |
219 | use strict 'vars'; |
253 | use strict; |
|
|
254 | |
220 | use Carp; |
255 | use Carp; |
221 | |
256 | |
222 | our $VERSION = '2.0'; |
257 | our $VERSION = '2.7'; |
223 | our $MODEL; |
258 | our $MODEL; |
224 | |
259 | |
225 | our $AUTOLOAD; |
260 | our $AUTOLOAD; |
226 | our @ISA; |
261 | our @ISA; |
227 | |
262 | |
228 | our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
263 | our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
229 | |
264 | |
230 | our @REGISTRY; |
265 | our @REGISTRY; |
231 | |
266 | |
232 | my @models = ( |
267 | my @models = ( |
|
|
268 | [Coro::EV:: => AnyEvent::Impl::CoroEV::], |
|
|
269 | [EV:: => AnyEvent::Impl::EV::], |
233 | [Coro::Event:: => AnyEvent::Impl::Coro::], |
270 | [Coro::Event:: => AnyEvent::Impl::Coro::], |
234 | [Event:: => AnyEvent::Impl::Event::], |
271 | [Event:: => AnyEvent::Impl::Event::], |
235 | [Glib:: => AnyEvent::Impl::Glib::], |
272 | [Glib:: => AnyEvent::Impl::Glib::], |
236 | [Tk:: => AnyEvent::Impl::Tk::], |
273 | [Tk:: => AnyEvent::Impl::Tk::], |
237 | [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], |
274 | [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], |
238 | ); |
275 | ); |
239 | |
276 | |
240 | our %method = map +($_ => 1), qw(io timer condvar broadcast wait DESTROY); |
277 | our %method = map +($_ => 1), qw(io timer condvar broadcast wait signal one_event DESTROY); |
241 | |
278 | |
242 | sub AUTOLOAD { |
279 | sub detect() { |
243 | $AUTOLOAD =~ s/.*://; |
|
|
244 | |
|
|
245 | $method{$AUTOLOAD} |
|
|
246 | or croak "$AUTOLOAD: not a valid method for AnyEvent objects"; |
|
|
247 | |
|
|
248 | unless ($MODEL) { |
280 | unless ($MODEL) { |
|
|
281 | no strict 'refs'; |
|
|
282 | |
249 | # check for already loaded models |
283 | # check for already loaded models |
250 | for (@REGISTRY, @models) { |
284 | for (@REGISTRY, @models) { |
251 | my ($package, $model) = @$_; |
285 | my ($package, $model) = @$_; |
252 | if (${"$package\::VERSION"} > 0) { |
286 | if (${"$package\::VERSION"} > 0) { |
253 | if (eval "require $model") { |
287 | if (eval "require $model") { |
… | |
… | |
261 | unless ($MODEL) { |
295 | unless ($MODEL) { |
262 | # try to load a model |
296 | # try to load a model |
263 | |
297 | |
264 | for (@REGISTRY, @models) { |
298 | for (@REGISTRY, @models) { |
265 | my ($package, $model) = @$_; |
299 | my ($package, $model) = @$_; |
|
|
300 | if (eval "require $package" |
|
|
301 | and ${"$package\::VERSION"} > 0 |
266 | if (eval "require $model") { |
302 | and eval "require $model") { |
267 | $MODEL = $model; |
303 | $MODEL = $model; |
268 | warn "AnyEvent: autoprobed and loaded model '$model', using it.\n" if $verbose > 1; |
304 | warn "AnyEvent: autoprobed and loaded model '$model', using it.\n" if $verbose > 1; |
269 | last; |
305 | last; |
270 | } |
306 | } |
271 | } |
307 | } |
272 | |
308 | |
273 | $MODEL |
309 | $MODEL |
274 | or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: Coro, Event, Glib or Tk."; |
310 | or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: EV (or Coro+EV), Event (or Coro+Event), Glib or Tk."; |
275 | } |
311 | } |
|
|
312 | |
|
|
313 | unshift @ISA, $MODEL; |
|
|
314 | push @{"$MODEL\::ISA"}, "AnyEvent::Base"; |
276 | } |
315 | } |
277 | |
316 | |
278 | @ISA = $MODEL; |
317 | $MODEL |
|
|
318 | } |
|
|
319 | |
|
|
320 | sub AUTOLOAD { |
|
|
321 | (my $func = $AUTOLOAD) =~ s/.*://; |
|
|
322 | |
|
|
323 | $method{$func} |
|
|
324 | or croak "$func: not a valid method for AnyEvent objects"; |
|
|
325 | |
|
|
326 | detect unless $MODEL; |
279 | |
327 | |
280 | my $class = shift; |
328 | my $class = shift; |
281 | $class->$AUTOLOAD (@_); |
329 | $class->$func (@_); |
|
|
330 | } |
|
|
331 | |
|
|
332 | package AnyEvent::Base; |
|
|
333 | |
|
|
334 | # default implementation for ->condvar, ->wait, ->broadcast |
|
|
335 | |
|
|
336 | sub condvar { |
|
|
337 | bless \my $flag, "AnyEvent::Base::CondVar" |
|
|
338 | } |
|
|
339 | |
|
|
340 | sub AnyEvent::Base::CondVar::broadcast { |
|
|
341 | ${$_[0]}++; |
|
|
342 | } |
|
|
343 | |
|
|
344 | sub AnyEvent::Base::CondVar::wait { |
|
|
345 | AnyEvent->one_event while !${$_[0]}; |
|
|
346 | } |
|
|
347 | |
|
|
348 | # default implementation for ->signal |
|
|
349 | |
|
|
350 | our %SIG_CB; |
|
|
351 | |
|
|
352 | sub signal { |
|
|
353 | my (undef, %arg) = @_; |
|
|
354 | |
|
|
355 | my $signal = uc $arg{signal} |
|
|
356 | or Carp::croak "required option 'signal' is missing"; |
|
|
357 | |
|
|
358 | $SIG_CB{$signal}{$arg{cb}} = $arg{cb}; |
|
|
359 | $SIG{$signal} ||= sub { |
|
|
360 | $_->() for values %{ $SIG_CB{$signal} || {} }; |
|
|
361 | }; |
|
|
362 | |
|
|
363 | bless [$signal, $arg{cb}], "AnyEvent::Base::Signal" |
|
|
364 | } |
|
|
365 | |
|
|
366 | sub AnyEvent::Base::Signal::DESTROY { |
|
|
367 | my ($signal, $cb) = @{$_[0]}; |
|
|
368 | |
|
|
369 | delete $SIG_CB{$signal}{$cb}; |
|
|
370 | |
|
|
371 | $SIG{$signal} = 'DEFAULT' unless keys %{ $SIG_CB{$signal} }; |
|
|
372 | } |
|
|
373 | |
|
|
374 | # default implementation for ->child |
|
|
375 | |
|
|
376 | our %PID_CB; |
|
|
377 | our $CHLD_W; |
|
|
378 | our $CHLD_DELAY_W; |
|
|
379 | our $PID_IDLE; |
|
|
380 | our $WNOHANG; |
|
|
381 | |
|
|
382 | sub _child_wait { |
|
|
383 | while (0 <= (my $pid = waitpid -1, $WNOHANG)) { |
|
|
384 | $_->($pid, $?) for (values %{ $PID_CB{$pid} || {} }), |
|
|
385 | (values %{ $PID_CB{0} || {} }); |
|
|
386 | } |
|
|
387 | |
|
|
388 | undef $PID_IDLE; |
|
|
389 | } |
|
|
390 | |
|
|
391 | sub _sigchld { |
|
|
392 | # make sure we deliver these changes "synchronous" with the event loop. |
|
|
393 | $CHLD_DELAY_W ||= AnyEvent->timer (after => 0, cb => sub { |
|
|
394 | undef $CHLD_DELAY_W; |
|
|
395 | &_child_wait; |
|
|
396 | }); |
|
|
397 | } |
|
|
398 | |
|
|
399 | sub child { |
|
|
400 | my (undef, %arg) = @_; |
|
|
401 | |
|
|
402 | defined (my $pid = $arg{pid} + 0) |
|
|
403 | or Carp::croak "required option 'pid' is missing"; |
|
|
404 | |
|
|
405 | $PID_CB{$pid}{$arg{cb}} = $arg{cb}; |
|
|
406 | |
|
|
407 | unless ($WNOHANG) { |
|
|
408 | $WNOHANG = eval { require POSIX; &POSIX::WNOHANG } || 1; |
|
|
409 | } |
|
|
410 | |
|
|
411 | unless ($CHLD_W) { |
|
|
412 | $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_sigchld); |
|
|
413 | # child could be a zombie already, so make at least one round |
|
|
414 | &_sigchld; |
|
|
415 | } |
|
|
416 | |
|
|
417 | bless [$pid, $arg{cb}], "AnyEvent::Base::Child" |
|
|
418 | } |
|
|
419 | |
|
|
420 | sub AnyEvent::Base::Child::DESTROY { |
|
|
421 | my ($pid, $cb) = @{$_[0]}; |
|
|
422 | |
|
|
423 | delete $PID_CB{$pid}{$cb}; |
|
|
424 | delete $PID_CB{$pid} unless keys %{ $PID_CB{$pid} }; |
|
|
425 | |
|
|
426 | undef $CHLD_W unless keys %PID_CB; |
282 | } |
427 | } |
283 | |
428 | |
284 | =head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE |
429 | =head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE |
285 | |
430 | |
286 | If you need to support another event library which isn't directly |
431 | If you need to support another event library which isn't directly |
… | |
… | |
297 | This tells AnyEvent to (literally) use the C<urxvt::anyevent::> |
442 | This tells AnyEvent to (literally) use the C<urxvt::anyevent::> |
298 | package/class when it finds the C<urxvt> package/module is loaded. When |
443 | package/class when it finds the C<urxvt> package/module is loaded. When |
299 | AnyEvent is loaded and asked to find a suitable event model, it will |
444 | AnyEvent is loaded and asked to find a suitable event model, it will |
300 | first check for the presence of urxvt. |
445 | first check for the presence of urxvt. |
301 | |
446 | |
302 | The class should prove implementations for all watcher types (see |
447 | The class should provide implementations for all watcher types (see |
303 | L<AnyEvent::Impl::Event> (source code), L<AnyEvent::Impl::Glib> |
448 | L<AnyEvent::Impl::Event> (source code), L<AnyEvent::Impl::Glib> |
304 | (Source code) and so on for actual examples, use C<perldoc -m |
449 | (Source code) and so on for actual examples, use C<perldoc -m |
305 | AnyEvent::Impl::Glib> to see the sources). |
450 | AnyEvent::Impl::Glib> to see the sources). |
306 | |
451 | |
307 | The above isn't fictitious, the I<rxvt-unicode> (a.k.a. urxvt) |
452 | The above isn't fictitious, the I<rxvt-unicode> (a.k.a. urxvt) |
… | |
… | |
311 | I<rxvt-unicode> distribution. |
456 | I<rxvt-unicode> distribution. |
312 | |
457 | |
313 | I<rxvt-unicode> also cheats a bit by not providing blocking access to |
458 | I<rxvt-unicode> also cheats a bit by not providing blocking access to |
314 | condition variables: code blocking while waiting for a condition will |
459 | condition variables: code blocking while waiting for a condition will |
315 | C<die>. This still works with most modules/usages, and blocking calls must |
460 | C<die>. This still works with most modules/usages, and blocking calls must |
316 | not be in an interactive appliation, so it makes sense. |
461 | not be in an interactive application, so it makes sense. |
317 | |
462 | |
318 | =head1 ENVIRONMENT VARIABLES |
463 | =head1 ENVIRONMENT VARIABLES |
319 | |
464 | |
320 | The following environment variables are used by this module: |
465 | The following environment variables are used by this module: |
321 | |
466 | |