… | |
… | |
121 | The interface itself is vaguely similar, but not identical to the L<Event> |
121 | The interface itself is vaguely similar, but not identical to the L<Event> |
122 | module. |
122 | module. |
123 | |
123 | |
124 | During the first call of any watcher-creation method, the module tries |
124 | During the first call of any watcher-creation method, the module tries |
125 | to detect the currently loaded event loop by probing whether one of the |
125 | to detect the currently loaded event loop by probing whether one of the |
126 | following modules is already loaded: L<EV>, L<AnyEvent::Impl::Perl>, |
126 | following modules is already loaded: L<EV>, L<AnyEvent::Loop>, |
127 | L<Event>, L<Glib>, L<Tk>, L<Event::Lib>, L<Qt>, L<POE>. The first one |
127 | L<Event>, L<Glib>, L<Tk>, L<Event::Lib>, L<Qt>, L<POE>. The first one |
128 | found is used. If none are detected, the module tries to load the first |
128 | found is used. If none are detected, the module tries to load the first |
129 | four modules in the order given; but note that if L<EV> is not |
129 | four modules in the order given; but note that if L<EV> is not |
130 | available, the pure-perl L<AnyEvent::Impl::Perl> should always work, so |
130 | available, the pure-perl L<AnyEvent::Loop> should always work, so |
131 | the other two are not normally tried. |
131 | the other two are not normally tried. |
132 | |
132 | |
133 | Because AnyEvent first checks for modules that are already loaded, loading |
133 | Because AnyEvent first checks for modules that are already loaded, loading |
134 | an event model explicitly before first using AnyEvent will likely make |
134 | an event model explicitly before first using AnyEvent will likely make |
135 | that model the default. For example: |
135 | that model the default. For example: |
… | |
… | |
142 | The I<likely> means that, if any module loads another event model and |
142 | The I<likely> means that, if any module loads another event model and |
143 | starts using it, all bets are off - this case should be very rare though, |
143 | starts using it, all bets are off - this case should be very rare though, |
144 | as very few modules hardcode event loops without announcing this very |
144 | as very few modules hardcode event loops without announcing this very |
145 | loudly. |
145 | loudly. |
146 | |
146 | |
147 | The pure-perl implementation of AnyEvent is called |
147 | The pure-perl implementation of AnyEvent is called C<AnyEvent::Loop>. Like |
148 | C<AnyEvent::Impl::Perl>. Like other event modules you can load it |
148 | other event modules you can load it explicitly and enjoy the high |
149 | explicitly and enjoy the high availability of that event loop :) |
149 | availability of that event loop :) |
150 | |
150 | |
151 | =head1 WATCHERS |
151 | =head1 WATCHERS |
152 | |
152 | |
153 | AnyEvent has the central concept of a I<watcher>, which is an object that |
153 | AnyEvent has the central concept of a I<watcher>, which is an object that |
154 | stores relevant data for each kind of event you are waiting for, such as |
154 | stores relevant data for each kind of event you are waiting for, such as |
… | |
… | |
356 | difference between C<< AnyEvent->time >> and C<< AnyEvent->now >> into |
356 | difference between C<< AnyEvent->time >> and C<< AnyEvent->now >> into |
357 | account. |
357 | account. |
358 | |
358 | |
359 | =item AnyEvent->now_update |
359 | =item AnyEvent->now_update |
360 | |
360 | |
361 | Some event loops (such as L<EV> or L<AnyEvent::Impl::Perl>) cache |
361 | Some event loops (such as L<EV> or L<AnyEvent::Loop>) cache the current |
362 | the current time for each loop iteration (see the discussion of L<< |
362 | time for each loop iteration (see the discussion of L<< AnyEvent->now >>, |
363 | AnyEvent->now >>, above). |
363 | above). |
364 | |
364 | |
365 | When a callback runs for a long time (or when the process sleeps), then |
365 | When a callback runs for a long time (or when the process sleeps), then |
366 | this "current" time will differ substantially from the real time, which |
366 | this "current" time will differ substantially from the real time, which |
367 | might affect timers and time-outs. |
367 | might affect timers and time-outs. |
368 | |
368 | |
… | |
… | |
482 | thing in an AnyEvent program, you I<have> to create at least one |
482 | thing in an AnyEvent program, you I<have> to create at least one |
483 | watcher before you C<fork> the child (alternatively, you can call |
483 | watcher before you C<fork> the child (alternatively, you can call |
484 | C<AnyEvent::detect>). |
484 | C<AnyEvent::detect>). |
485 | |
485 | |
486 | As most event loops do not support waiting for child events, they will be |
486 | As most event loops do not support waiting for child events, they will be |
487 | emulated by AnyEvent in most cases, in which the latency and race problems |
487 | emulated by AnyEvent in most cases, in which case the latency and race |
488 | mentioned in the description of signal watchers apply. |
488 | problems mentioned in the description of signal watchers apply. |
489 | |
489 | |
490 | Example: fork a process and wait for it |
490 | Example: fork a process and wait for it |
491 | |
491 | |
492 | my $done = AnyEvent->condvar; |
492 | my $done = AnyEvent->condvar; |
493 | |
493 | |
… | |
… | |
860 | use. If EV is not installed, then AnyEvent will fall back to its own |
860 | use. If EV is not installed, then AnyEvent will fall back to its own |
861 | pure-perl implementation, which is available everywhere as it comes with |
861 | pure-perl implementation, which is available everywhere as it comes with |
862 | AnyEvent itself. |
862 | AnyEvent itself. |
863 | |
863 | |
864 | AnyEvent::Impl::EV based on EV (interface to libev, best choice). |
864 | AnyEvent::Impl::EV based on EV (interface to libev, best choice). |
865 | AnyEvent::Impl::Perl pure-perl implementation, fast and portable. |
865 | AnyEvent::Impl::Perl pure-perl AnyEvent::Loop, fast and portable. |
866 | |
866 | |
867 | =item Backends that are transparently being picked up when they are used. |
867 | =item Backends that are transparently being picked up when they are used. |
868 | |
868 | |
869 | These will be used if they are already loaded when the first watcher |
869 | These will be used if they are already loaded when the first watcher |
870 | is created, in which case it is assumed that the application is using |
870 | is created, in which case it is assumed that the application is using |
… | |
… | |
1005 | # AnyEvent not yet initialised, so make sure to load Coro::AnyEvent |
1005 | # AnyEvent not yet initialised, so make sure to load Coro::AnyEvent |
1006 | # as soon as it is |
1006 | # as soon as it is |
1007 | push @AnyEvent::post_detect, sub { require Coro::AnyEvent }; |
1007 | push @AnyEvent::post_detect, sub { require Coro::AnyEvent }; |
1008 | } |
1008 | } |
1009 | |
1009 | |
|
|
1010 | =item AnyEvent::postpone { BLOCK } |
|
|
1011 | |
|
|
1012 | Arranges for the block to be executed as soon as possible, but not before |
|
|
1013 | the call itself returns. In practise, the block will be executed just |
|
|
1014 | before the event loop polls for new events, or shortly afterwards. |
|
|
1015 | |
|
|
1016 | This function never returns anything (to make the C<return postpone { ... |
|
|
1017 | }> idiom more useful. |
|
|
1018 | |
|
|
1019 | To understand the usefulness of this function, consider a function that |
|
|
1020 | asynchronously does something for you and returns some transaction |
|
|
1021 | object or guard to let you cancel the operation. For example, |
|
|
1022 | C<AnyEvent::Socket::tcp_connect>: |
|
|
1023 | |
|
|
1024 | # start a conenction attempt unless one is active |
|
|
1025 | $self->{connect_guard} ||= AnyEvent::Socket::tcp_connect "www.example.net", 80, sub { |
|
|
1026 | delete $self->{connect_guard}; |
|
|
1027 | ... |
|
|
1028 | }; |
|
|
1029 | |
|
|
1030 | Imagine that this function could instantly call the callback, for |
|
|
1031 | example, because it detects an obvious error such as a negative port |
|
|
1032 | number. Invoking the callback before the function returns causes problems |
|
|
1033 | however: the callback will be called and will try to delete the guard |
|
|
1034 | object. But since the function hasn't returned yet, there is nothing to |
|
|
1035 | delete. When the function eventually returns it will assign the guard |
|
|
1036 | object to C<< $self->{connect_guard} >>, where it will likely never be |
|
|
1037 | deleted, so the program thinks it is still trying to connect. |
|
|
1038 | |
|
|
1039 | This is where C<AnyEvent::postpone> should be used. Instead of calling the |
|
|
1040 | callback directly on error: |
|
|
1041 | |
|
|
1042 | $cb->(undef), return # signal error to callback, BAD! |
|
|
1043 | if $some_error_condition; |
|
|
1044 | |
|
|
1045 | It should use C<postpone>: |
|
|
1046 | |
|
|
1047 | AnyEvent::postpone { $cb->(undef) }, return # signal error to callback, later |
|
|
1048 | if $some_error_condition; |
|
|
1049 | |
1010 | =back |
1050 | =back |
1011 | |
1051 | |
1012 | =head1 WHAT TO DO IN A MODULE |
1052 | =head1 WHAT TO DO IN A MODULE |
1013 | |
1053 | |
1014 | As a module author, you should C<use AnyEvent> and call AnyEvent methods |
1054 | As a module author, you should C<use AnyEvent> and call AnyEvent methods |
… | |
… | |
1047 | modules might create watchers when they are loaded, and AnyEvent will |
1087 | modules might create watchers when they are loaded, and AnyEvent will |
1048 | decide on the event model to use as soon as it creates watchers, and it |
1088 | decide on the event model to use as soon as it creates watchers, and it |
1049 | might choose the wrong one unless you load the correct one yourself. |
1089 | might choose the wrong one unless you load the correct one yourself. |
1050 | |
1090 | |
1051 | You can chose to use a pure-perl implementation by loading the |
1091 | You can chose to use a pure-perl implementation by loading the |
1052 | C<AnyEvent::Impl::Perl> module, which gives you similar behaviour |
1092 | C<AnyEvent::Loop> module, which gives you similar behaviour |
1053 | everywhere, but letting AnyEvent chose the model is generally better. |
1093 | everywhere, but letting AnyEvent chose the model is generally better. |
1054 | |
1094 | |
1055 | =head2 MAINLOOP EMULATION |
1095 | =head2 MAINLOOP EMULATION |
1056 | |
1096 | |
1057 | Sometimes (often for short test scripts, or even standalone programs who |
1097 | Sometimes (often for short test scripts, or even standalone programs who |
… | |
… | |
1195 | $PROTOCOL{$_} = ++$idx |
1235 | $PROTOCOL{$_} = ++$idx |
1196 | for reverse split /\s*,\s*/, |
1236 | for reverse split /\s*,\s*/, |
1197 | $ENV{PERL_ANYEVENT_PROTOCOLS} || "ipv4,ipv6"; |
1237 | $ENV{PERL_ANYEVENT_PROTOCOLS} || "ipv4,ipv6"; |
1198 | } |
1238 | } |
1199 | |
1239 | |
|
|
1240 | our @post_detect; |
|
|
1241 | |
|
|
1242 | sub post_detect(&) { |
|
|
1243 | my ($cb) = @_; |
|
|
1244 | |
|
|
1245 | push @post_detect, $cb; |
|
|
1246 | |
|
|
1247 | defined wantarray |
|
|
1248 | ? bless \$cb, "AnyEvent::Util::postdetect" |
|
|
1249 | : () |
|
|
1250 | } |
|
|
1251 | |
|
|
1252 | sub AnyEvent::Util::postdetect::DESTROY { |
|
|
1253 | @post_detect = grep $_ != ${$_[0]}, @post_detect; |
|
|
1254 | } |
|
|
1255 | |
|
|
1256 | our $POSTPONE_W; |
|
|
1257 | our @POSTPONE; |
|
|
1258 | |
|
|
1259 | sub _postpone_exec { |
|
|
1260 | undef $POSTPONE_W; |
|
|
1261 | |
|
|
1262 | &{ shift @POSTPONE } |
|
|
1263 | while @POSTPONE; |
|
|
1264 | } |
|
|
1265 | |
|
|
1266 | sub postpone(&) { |
|
|
1267 | push @POSTPONE, shift; |
|
|
1268 | |
|
|
1269 | $POSTPONE_W ||= AE::timer (0, 0, \&_postpone_exec); |
|
|
1270 | |
|
|
1271 | () |
|
|
1272 | } |
|
|
1273 | |
1200 | my @models = ( |
1274 | our @models = ( |
1201 | [EV:: => AnyEvent::Impl::EV:: , 1], |
1275 | [EV:: => AnyEvent::Impl::EV:: , 1], |
1202 | [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl:: , 1], |
1276 | [AnyEvent::Loop:: => AnyEvent::Impl::Perl:: , 1], |
1203 | # everything below here will not (normally) be autoprobed |
1277 | # everything below here will not (normally) be autoprobed |
1204 | # as the pureperl backend should work everywhere |
1278 | # as the pure perl backend should work everywhere |
1205 | # and is usually faster |
1279 | # and is usually faster |
1206 | [Event:: => AnyEvent::Impl::Event::, 1], |
1280 | [Event:: => AnyEvent::Impl::Event::, 1], |
1207 | [Glib:: => AnyEvent::Impl::Glib:: , 1], # becomes extremely slow with many watchers |
1281 | [Glib:: => AnyEvent::Impl::Glib:: , 1], # becomes extremely slow with many watchers |
1208 | [Event::Lib:: => AnyEvent::Impl::EventLib::], # too buggy |
1282 | [Event::Lib:: => AnyEvent::Impl::EventLib::], # too buggy |
1209 | [Irssi:: => AnyEvent::Impl::Irssi::], # Irssi has a bogus "Event" package |
1283 | [Irssi:: => AnyEvent::Impl::Irssi::], # Irssi has a bogus "Event" package |
1210 | [Tk:: => AnyEvent::Impl::Tk::], # crashes with many handles |
1284 | [Tk:: => AnyEvent::Impl::Tk::], # crashes with many handles |
1211 | [Qt:: => AnyEvent::Impl::Qt::], # requires special main program |
1285 | [Qt:: => AnyEvent::Impl::Qt::], # requires special main program |
1212 | [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza |
1286 | [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza |
1213 | [Wx:: => AnyEvent::Impl::POE::], |
1287 | [Wx:: => AnyEvent::Impl::POE::], |
1214 | [Prima:: => AnyEvent::Impl::POE::], |
1288 | [Prima:: => AnyEvent::Impl::POE::], |
1215 | [IO::Async::Loop:: => AnyEvent::Impl::IOAsync::], |
1289 | [IO::Async::Loop:: => AnyEvent::Impl::IOAsync::], # a bitch to autodetect |
1216 | [Cocoa::EventLoop:: => AnyEvent::Impl::Cocoa::], |
1290 | [Cocoa::EventLoop:: => AnyEvent::Impl::Cocoa::], |
1217 | [FLTK:: => AnyEvent::Impl::FLTK::], |
1291 | [FLTK:: => AnyEvent::Impl::FLTK::], |
1218 | ); |
1292 | ); |
1219 | |
1293 | |
1220 | our %method = map +($_ => 1), |
1294 | our %method = map +($_ => 1), |
1221 | qw(io timer time now now_update signal child idle condvar one_event DESTROY); |
1295 | qw(io timer time now now_update signal child idle condvar DESTROY); |
1222 | |
|
|
1223 | our @post_detect; |
|
|
1224 | |
|
|
1225 | sub post_detect(&) { |
|
|
1226 | my ($cb) = @_; |
|
|
1227 | |
|
|
1228 | push @post_detect, $cb; |
|
|
1229 | |
|
|
1230 | defined wantarray |
|
|
1231 | ? bless \$cb, "AnyEvent::Util::postdetect" |
|
|
1232 | : () |
|
|
1233 | } |
|
|
1234 | |
|
|
1235 | sub AnyEvent::Util::postdetect::DESTROY { |
|
|
1236 | @post_detect = grep $_ != ${$_[0]}, @post_detect; |
|
|
1237 | } |
|
|
1238 | |
1296 | |
1239 | sub detect() { |
1297 | sub detect() { |
1240 | # free some memory |
1298 | # free some memory |
1241 | *detect = sub () { $MODEL }; |
1299 | *detect = sub () { $MODEL }; |
1242 | |
1300 | |
1243 | local $!; # for good measure |
1301 | local $!; # for good measure |
1244 | local $SIG{__DIE__}; |
1302 | local $SIG{__DIE__}; |
1245 | |
1303 | |
1246 | if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z]+)$/) { |
1304 | if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z0-9:]+)$/) { |
1247 | my $model = "AnyEvent::Impl::$1"; |
1305 | my $model = $1; |
|
|
1306 | $model = "AnyEvent::Impl::$model" unless $model =~ s/::$//; |
1248 | if (eval "require $model") { |
1307 | if (eval "require $model") { |
1249 | $MODEL = $model; |
1308 | $MODEL = $model; |
1250 | warn "AnyEvent: loaded model '$model' (forced by \$ENV{PERL_ANYEVENT_MODEL}), using it.\n" if $VERBOSE >= 2; |
1309 | warn "AnyEvent: loaded model '$model' (forced by \$ENV{PERL_ANYEVENT_MODEL}), using it.\n" if $VERBOSE >= 2; |
1251 | } else { |
1310 | } else { |
1252 | warn "AnyEvent: unable to load model '$model' (from \$ENV{PERL_ANYEVENT_MODEL}):\n$@" if $VERBOSE; |
1311 | warn "AnyEvent: unable to load model '$model' (from \$ENV{PERL_ANYEVENT_MODEL}):\n$@" if $VERBOSE; |
… | |
… | |
1285 | $MODEL |
1344 | $MODEL |
1286 | or die "AnyEvent: backend autodetection failed - did you properly install AnyEvent?\n"; |
1345 | or die "AnyEvent: backend autodetection failed - did you properly install AnyEvent?\n"; |
1287 | } |
1346 | } |
1288 | } |
1347 | } |
1289 | |
1348 | |
1290 | @models = (); # free probe data |
1349 | # free memory only needed for probing |
|
|
1350 | undef @models; |
|
|
1351 | undef @REGISTRY; |
1291 | |
1352 | |
1292 | push @{"$MODEL\::ISA"}, "AnyEvent::Base"; |
1353 | push @{"$MODEL\::ISA"}, "AnyEvent::Base"; |
1293 | unshift @ISA, $MODEL; |
1354 | unshift @ISA, $MODEL; |
1294 | |
1355 | |
1295 | # now nuke some methods that are overridden by the backend. |
1356 | # now nuke some methods that are overridden by the backend. |
1296 | # SUPER is not allowed. |
1357 | # SUPER usage is not allowed in these. |
1297 | for (qw(time signal child idle)) { |
1358 | for (qw(time signal child idle)) { |
1298 | undef &{"AnyEvent::Base::$_"} |
1359 | undef &{"AnyEvent::Base::$_"} |
1299 | if defined &{"$MODEL\::$_"}; |
1360 | if defined &{"$MODEL\::$_"}; |
1300 | } |
1361 | } |
1301 | |
1362 | |
… | |
… | |
1304 | warn "AnyEvent: cannot load AnyEvent::Strict: $@" |
1365 | warn "AnyEvent: cannot load AnyEvent::Strict: $@" |
1305 | if $@ && $VERBOSE; |
1366 | if $@ && $VERBOSE; |
1306 | } |
1367 | } |
1307 | |
1368 | |
1308 | (shift @post_detect)->() while @post_detect; |
1369 | (shift @post_detect)->() while @post_detect; |
|
|
1370 | undef @post_detect; |
1309 | |
1371 | |
1310 | *post_detect = sub(&) { |
1372 | *post_detect = sub(&) { |
1311 | shift->(); |
1373 | shift->(); |
1312 | |
1374 | |
1313 | undef |
1375 | undef |
1314 | }; |
1376 | }; |
1315 | |
1377 | |
|
|
1378 | # recover a few more bytes |
|
|
1379 | postpone { |
|
|
1380 | undef &AUTOLOAD; |
|
|
1381 | }; |
|
|
1382 | |
1316 | $MODEL |
1383 | $MODEL |
1317 | } |
1384 | } |
|
|
1385 | |
|
|
1386 | our %method = map +($_ => 1), |
|
|
1387 | qw(io timer time now now_update signal child idle condvar DESTROY); |
1318 | |
1388 | |
1319 | sub AUTOLOAD { |
1389 | sub AUTOLOAD { |
1320 | (my $func = $AUTOLOAD) =~ s/.*://; |
1390 | (my $func = $AUTOLOAD) =~ s/.*://; |
1321 | |
1391 | |
1322 | $method{$func} |
1392 | $method{$func} |
1323 | or Carp::croak "$func: not a valid AnyEvent class method"; |
1393 | or Carp::croak "$func: not a valid AnyEvent class method"; |
|
|
1394 | |
|
|
1395 | # free some memory |
|
|
1396 | undef %method; |
1324 | |
1397 | |
1325 | detect; |
1398 | detect; |
1326 | |
1399 | |
1327 | my $class = shift; |
1400 | my $class = shift; |
1328 | $class->$func (@_); |
1401 | $class->$func (@_); |
… | |
… | |
1357 | |
1430 | |
1358 | package AE; |
1431 | package AE; |
1359 | |
1432 | |
1360 | our $VERSION = $AnyEvent::VERSION; |
1433 | our $VERSION = $AnyEvent::VERSION; |
1361 | |
1434 | |
|
|
1435 | |
|
|
1436 | sub _reset() { |
|
|
1437 | eval q{ |
1362 | # fall back to the main API by default - backends and AnyEvent::Base |
1438 | # fall back to the main API by default - backends and AnyEvent::Base |
1363 | # implementations can overwrite these. |
1439 | # implementations can overwrite these. |
1364 | |
1440 | |
1365 | sub io($$$) { |
1441 | sub io($$$) { |
1366 | AnyEvent->io (fh => $_[0], poll => $_[1] ? "w" : "r", cb => $_[2]) |
1442 | AnyEvent->io (fh => $_[0], poll => $_[1] ? "w" : "r", cb => $_[2]) |
1367 | } |
1443 | } |
1368 | |
1444 | |
1369 | sub timer($$$) { |
1445 | sub timer($$$) { |
1370 | AnyEvent->timer (after => $_[0], interval => $_[1], cb => $_[2]) |
1446 | AnyEvent->timer (after => $_[0], interval => $_[1], cb => $_[2]) |
1371 | } |
1447 | } |
1372 | |
1448 | |
1373 | sub signal($$) { |
1449 | sub signal($$) { |
1374 | AnyEvent->signal (signal => $_[0], cb => $_[1]) |
1450 | AnyEvent->signal (signal => $_[0], cb => $_[1]) |
1375 | } |
1451 | } |
1376 | |
1452 | |
1377 | sub child($$) { |
1453 | sub child($$) { |
1378 | AnyEvent->child (pid => $_[0], cb => $_[1]) |
1454 | AnyEvent->child (pid => $_[0], cb => $_[1]) |
1379 | } |
1455 | } |
1380 | |
1456 | |
1381 | sub idle($) { |
1457 | sub idle($) { |
1382 | AnyEvent->idle (cb => $_[0]) |
1458 | AnyEvent->idle (cb => $_[0]) |
1383 | } |
1459 | } |
1384 | |
1460 | |
1385 | sub cv(;&) { |
1461 | sub cv(;&) { |
1386 | AnyEvent->condvar (@_ ? (cb => $_[0]) : ()) |
1462 | AnyEvent->condvar (@_ ? (cb => $_[0]) : ()) |
1387 | } |
1463 | } |
1388 | |
1464 | |
1389 | sub now() { |
1465 | sub now() { |
1390 | AnyEvent->now |
1466 | AnyEvent->now |
1391 | } |
1467 | } |
1392 | |
1468 | |
1393 | sub now_update() { |
1469 | sub now_update() { |
1394 | AnyEvent->now_update |
1470 | AnyEvent->now_update |
1395 | } |
1471 | } |
1396 | |
1472 | |
1397 | sub time() { |
1473 | sub time() { |
1398 | AnyEvent->time |
1474 | AnyEvent->time |
|
|
1475 | } |
|
|
1476 | |
|
|
1477 | *postpone = \&AnyEvent::postpone; |
|
|
1478 | }; |
|
|
1479 | die if $@; |
1399 | } |
1480 | } |
|
|
1481 | |
|
|
1482 | BEGIN { _reset } |
1400 | |
1483 | |
1401 | package AnyEvent::Base; |
1484 | package AnyEvent::Base; |
1402 | |
1485 | |
1403 | # default implementations for many methods |
1486 | # default implementations for many methods |
1404 | |
1487 | |
… | |
… | |
1423 | |
1506 | |
1424 | *now = \&time; |
1507 | *now = \&time; |
1425 | |
1508 | |
1426 | sub now_update { } |
1509 | sub now_update { } |
1427 | |
1510 | |
|
|
1511 | sub _poll { |
|
|
1512 | Carp::croak "$AnyEvent::MODEL does not support blocking waits. Caught"; |
|
|
1513 | } |
|
|
1514 | |
1428 | # default implementation for ->condvar |
1515 | # default implementation for ->condvar |
|
|
1516 | # in fact, the default should not be overwritten |
1429 | |
1517 | |
1430 | sub condvar { |
1518 | sub condvar { |
1431 | eval q{ # poor man's autoloading {} |
1519 | eval q{ # poor man's autoloading {} |
1432 | *condvar = sub { |
1520 | *condvar = sub { |
1433 | bless { @_ == 3 ? (_ae_cb => $_[2]) : () }, "AnyEvent::CondVar" |
1521 | bless { @_ == 3 ? (_ae_cb => $_[2]) : () }, "AnyEvent::CondVar" |
… | |
… | |
1603 | : sysread $SIGPIPE_R, (my $dummy), 9; |
1691 | : sysread $SIGPIPE_R, (my $dummy), 9; |
1604 | |
1692 | |
1605 | while (%SIG_EV) { |
1693 | while (%SIG_EV) { |
1606 | for (keys %SIG_EV) { |
1694 | for (keys %SIG_EV) { |
1607 | delete $SIG_EV{$_}; |
1695 | delete $SIG_EV{$_}; |
1608 | $_->() for values %{ $SIG_CB{$_} || {} }; |
1696 | &$_ for values %{ $SIG_CB{$_} || {} }; |
1609 | } |
1697 | } |
1610 | } |
1698 | } |
1611 | }; |
1699 | }; |
1612 | }; |
1700 | }; |
1613 | die if $@; |
1701 | die if $@; |
… | |
… | |
1640 | }; |
1728 | }; |
1641 | |
1729 | |
1642 | *child = sub { |
1730 | *child = sub { |
1643 | my (undef, %arg) = @_; |
1731 | my (undef, %arg) = @_; |
1644 | |
1732 | |
1645 | defined (my $pid = $arg{pid} + 0) |
1733 | my $pid = $arg{pid}; |
1646 | or Carp::croak "required option 'pid' is missing"; |
1734 | my $cb = $arg{cb}; |
1647 | |
1735 | |
1648 | $PID_CB{$pid}{$arg{cb}} = $arg{cb}; |
1736 | $PID_CB{$pid}{$cb+0} = $cb; |
1649 | |
1737 | |
1650 | unless ($CHLD_W) { |
1738 | unless ($CHLD_W) { |
1651 | $CHLD_W = AE::signal CHLD => \&_sigchld; |
1739 | $CHLD_W = AE::signal CHLD => \&_sigchld; |
1652 | # child could be a zombie already, so make at least one round |
1740 | # child could be a zombie already, so make at least one round |
1653 | &_sigchld; |
1741 | &_sigchld; |
1654 | } |
1742 | } |
1655 | |
1743 | |
1656 | bless [$pid, $arg{cb}], "AnyEvent::Base::child" |
1744 | bless [$pid, $cb+0], "AnyEvent::Base::child" |
1657 | }; |
1745 | }; |
1658 | |
1746 | |
1659 | *AnyEvent::Base::child::DESTROY = sub { |
1747 | *AnyEvent::Base::child::DESTROY = sub { |
1660 | my ($pid, $cb) = @{$_[0]}; |
1748 | my ($pid, $icb) = @{$_[0]}; |
1661 | |
1749 | |
1662 | delete $PID_CB{$pid}{$cb}; |
1750 | delete $PID_CB{$pid}{$icb}; |
1663 | delete $PID_CB{$pid} unless keys %{ $PID_CB{$pid} }; |
1751 | delete $PID_CB{$pid} unless keys %{ $PID_CB{$pid} }; |
1664 | |
1752 | |
1665 | undef $CHLD_W unless keys %PID_CB; |
1753 | undef $CHLD_W unless keys %PID_CB; |
1666 | }; |
1754 | }; |
1667 | }; |
1755 | }; |
… | |
… | |
1739 | |
1827 | |
1740 | sub _send { |
1828 | sub _send { |
1741 | # nop |
1829 | # nop |
1742 | } |
1830 | } |
1743 | |
1831 | |
|
|
1832 | sub _wait { |
|
|
1833 | AnyEvent->_poll until $_[0]{_ae_sent}; |
|
|
1834 | } |
|
|
1835 | |
1744 | sub send { |
1836 | sub send { |
1745 | my $cv = shift; |
1837 | my $cv = shift; |
1746 | $cv->{_ae_sent} = [@_]; |
1838 | $cv->{_ae_sent} = [@_]; |
1747 | (delete $cv->{_ae_cb})->($cv) if $cv->{_ae_cb}; |
1839 | (delete $cv->{_ae_cb})->($cv) if $cv->{_ae_cb}; |
1748 | $cv->_send; |
1840 | $cv->_send; |
… | |
… | |
1755 | |
1847 | |
1756 | sub ready { |
1848 | sub ready { |
1757 | $_[0]{_ae_sent} |
1849 | $_[0]{_ae_sent} |
1758 | } |
1850 | } |
1759 | |
1851 | |
1760 | sub _wait { |
|
|
1761 | $WAITING |
|
|
1762 | and !$_[0]{_ae_sent} |
|
|
1763 | and Carp::croak "AnyEvent::CondVar: recursive blocking wait detected"; |
|
|
1764 | |
|
|
1765 | local $WAITING = 1; |
|
|
1766 | AnyEvent->one_event while !$_[0]{_ae_sent}; |
|
|
1767 | } |
|
|
1768 | |
|
|
1769 | sub recv { |
1852 | sub recv { |
|
|
1853 | unless ($_[0]{_ae_sent}) { |
|
|
1854 | $WAITING |
|
|
1855 | and Carp::croak "AnyEvent::CondVar: recursive blocking wait attempted"; |
|
|
1856 | |
|
|
1857 | local $WAITING = 1; |
1770 | $_[0]->_wait; |
1858 | $_[0]->_wait; |
|
|
1859 | } |
1771 | |
1860 | |
1772 | Carp::croak $_[0]{_ae_croak} if $_[0]{_ae_croak}; |
1861 | $_[0]{_ae_croak} |
1773 | wantarray ? @{ $_[0]{_ae_sent} } : $_[0]{_ae_sent}[0] |
1862 | and Carp::croak $_[0]{_ae_croak}; |
|
|
1863 | |
|
|
1864 | wantarray |
|
|
1865 | ? @{ $_[0]{_ae_sent} } |
|
|
1866 | : $_[0]{_ae_sent}[0] |
1774 | } |
1867 | } |
1775 | |
1868 | |
1776 | sub cb { |
1869 | sub cb { |
1777 | my $cv = shift; |
1870 | my $cv = shift; |
1778 | |
1871 | |
… | |
… | |
1794 | &{ $_[0]{_ae_end_cb} || sub { $_[0]->send } }; |
1887 | &{ $_[0]{_ae_end_cb} || sub { $_[0]->send } }; |
1795 | } |
1888 | } |
1796 | |
1889 | |
1797 | # undocumented/compatibility with pre-3.4 |
1890 | # undocumented/compatibility with pre-3.4 |
1798 | *broadcast = \&send; |
1891 | *broadcast = \&send; |
1799 | *wait = \&_wait; |
1892 | *wait = \&recv; |
1800 | |
1893 | |
1801 | =head1 ERROR AND EXCEPTION HANDLING |
1894 | =head1 ERROR AND EXCEPTION HANDLING |
1802 | |
1895 | |
1803 | In general, AnyEvent does not do any error handling - it relies on the |
1896 | In general, AnyEvent does not do any error handling - it relies on the |
1804 | caller to do that if required. The L<AnyEvent::Strict> module (see also |
1897 | caller to do that if required. The L<AnyEvent::Strict> module (see also |
… | |
… | |
1859 | can be very useful, however. |
1952 | can be very useful, however. |
1860 | |
1953 | |
1861 | =item C<PERL_ANYEVENT_MODEL> |
1954 | =item C<PERL_ANYEVENT_MODEL> |
1862 | |
1955 | |
1863 | This can be used to specify the event model to be used by AnyEvent, before |
1956 | This can be used to specify the event model to be used by AnyEvent, before |
1864 | auto detection and -probing kicks in. It must be a string consisting |
1957 | auto detection and -probing kicks in. |
1865 | entirely of ASCII letters. The string C<AnyEvent::Impl::> gets prepended |
1958 | |
|
|
1959 | It normally is a string consisting entirely of ASCII letters (e.g. C<EV> |
|
|
1960 | or C<IOAsync>). The string C<AnyEvent::Impl::> gets prepended and the |
1866 | and the resulting module name is loaded and if the load was successful, |
1961 | resulting module name is loaded and - if the load was successful - used as |
1867 | used as event model. If it fails to load AnyEvent will proceed with |
1962 | event model backend. If it fails to load then AnyEvent will proceed with |
1868 | auto detection and -probing. |
1963 | auto detection and -probing. |
1869 | |
1964 | |
1870 | This functionality might change in future versions. |
1965 | If the string ends with C<::> instead (e.g. C<AnyEvent::Impl::EV::>) then |
|
|
1966 | nothing gets prepended and the module name is used as-is (hint: C<::> at |
|
|
1967 | the end of a string designates a module name and quotes it appropriately). |
1871 | |
1968 | |
1872 | For example, to force the pure perl model (L<AnyEvent::Impl::Perl>) you |
1969 | For example, to force the pure perl model (L<AnyEvent::Loop::Perl>) you |
1873 | could start your program like this: |
1970 | could start your program like this: |
1874 | |
1971 | |
1875 | PERL_ANYEVENT_MODEL=Perl perl ... |
1972 | PERL_ANYEVENT_MODEL=Perl perl ... |
1876 | |
1973 | |
1877 | =item C<PERL_ANYEVENT_PROTOCOLS> |
1974 | =item C<PERL_ANYEVENT_PROTOCOLS> |
… | |
… | |
2576 | |
2673 | |
2577 | =item L<Time::HiRes> |
2674 | =item L<Time::HiRes> |
2578 | |
2675 | |
2579 | This module is part of perl since release 5.008. It will be used when the |
2676 | This module is part of perl since release 5.008. It will be used when the |
2580 | chosen event library does not come with a timing source of its own. The |
2677 | chosen event library does not come with a timing source of its own. The |
2581 | pure-perl event loop (L<AnyEvent::Impl::Perl>) will additionally use it to |
2678 | pure-perl event loop (L<AnyEvent::Loop>) will additionally load it to |
2582 | try to use a monotonic clock for timing stability. |
2679 | try to use a monotonic clock for timing stability. |
2583 | |
2680 | |
2584 | =back |
2681 | =back |
2585 | |
2682 | |
2586 | |
2683 | |
… | |
… | |
2654 | |
2751 | |
2655 | FAQ: L<AnyEvent::FAQ>. |
2752 | FAQ: L<AnyEvent::FAQ>. |
2656 | |
2753 | |
2657 | Utility functions: L<AnyEvent::Util>. |
2754 | Utility functions: L<AnyEvent::Util>. |
2658 | |
2755 | |
2659 | Event modules: L<EV>, L<EV::Glib>, L<Glib::EV>, L<Event>, L<Glib::Event>, |
2756 | Event modules: L<AnyEvent::Loop>, L<EV>, L<EV::Glib>, L<Glib::EV>, |
2660 | L<Glib>, L<Tk>, L<Event::Lib>, L<Qt>, L<POE>. |
2757 | L<Event>, L<Glib::Event>, L<Glib>, L<Tk>, L<Event::Lib>, L<Qt>, L<POE>. |
2661 | |
2758 | |
2662 | Implementations: L<AnyEvent::Impl::EV>, L<AnyEvent::Impl::Event>, |
2759 | Implementations: L<AnyEvent::Impl::EV>, L<AnyEvent::Impl::Event>, |
2663 | L<AnyEvent::Impl::Glib>, L<AnyEvent::Impl::Tk>, L<AnyEvent::Impl::Perl>, |
2760 | L<AnyEvent::Impl::Glib>, L<AnyEvent::Impl::Tk>, L<AnyEvent::Impl::Perl>, |
2664 | L<AnyEvent::Impl::EventLib>, L<AnyEvent::Impl::Qt>, |
2761 | L<AnyEvent::Impl::EventLib>, L<AnyEvent::Impl::Qt>, |
2665 | L<AnyEvent::Impl::POE>, L<AnyEvent::Impl::IOAsync>, L<Anyevent::Impl::Irssi>. |
2762 | L<AnyEvent::Impl::POE>, L<AnyEvent::Impl::IOAsync>, L<Anyevent::Impl::Irssi>. |