ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent.pm
(Generate patch)

Comparing AnyEvent/lib/AnyEvent.pm (file contents):
Revision 1.241 by root, Fri Jul 17 18:08:35 2009 UTC vs.
Revision 1.248 by root, Sat Jul 18 22:27:10 2009 UTC

361invocation, and callback invocation will be synchronous. Synchronous means 361invocation, and callback invocation will be synchronous. Synchronous means
362that it might take a while until the signal gets handled by the process, 362that it might take a while until the signal gets handled by the process,
363but it is guaranteed not to interrupt any other callbacks. 363but it is guaranteed not to interrupt any other callbacks.
364 364
365The main advantage of using these watchers is that you can share a signal 365The main advantage of using these watchers is that you can share a signal
366between multiple watchers. 366between multiple watchers, and AnyEvent will ensure that signals will not
367interrupt your program at bad times.
367 368
368This watcher might use C<%SIG>, so programs overwriting those signals 369This watcher might use C<%SIG> (depending on the event loop used),
369directly will likely not work correctly. 370so programs overwriting those signals directly will likely not work
371correctly.
370 372
371Example: exit on SIGINT 373Example: exit on SIGINT
372 374
373 my $w = AnyEvent->signal (signal => "INT", cb => sub { exit 1 }); 375 my $w = AnyEvent->signal (signal => "INT", cb => sub { exit 1 });
376
377=head3 Signal Races, Delays and Workarounds
378
379Many event loops (e.g. Glib, Tk, Qt, IO::Async) do not support attaching
380callbacks to signals in a generic way, which is a pity, as you cannot do
381race-free signal handling in perl. AnyEvent will try to do it's best, but
382in some cases, signals will be delayed. The maximum time a signal might
383be delayed is specified in C<$AnyEvent::MAX_SIGNAL_LATENCY> (default: 10
384seconds). This variable can be changed only before the first signal
385watcher is created, and should be left alone otherwise. Higher values
386will cause fewer spurious wake-ups, which is better for power and CPU
387saving. All these problems can be avoided by installing the optional
388L<Async::Interrupt> module. This will not work with inherently broken
389event loops such as L<Event> or L<Event::Lib> (and not with L<POE>
390currently, as POE does it's own workaround with one-second latency). With
391those, you just have to suffer the delays.
374 392
375=head2 CHILD PROCESS WATCHERS 393=head2 CHILD PROCESS WATCHERS
376 394
377You can also watch on a child process exit and catch its exit status. 395You can also watch on a child process exit and catch its exit status.
378 396
403 421
404This means you cannot create a child watcher as the very first 422This means you cannot create a child watcher as the very first
405thing in an AnyEvent program, you I<have> to create at least one 423thing in an AnyEvent program, you I<have> to create at least one
406watcher before you C<fork> the child (alternatively, you can call 424watcher before you C<fork> the child (alternatively, you can call
407C<AnyEvent::detect>). 425C<AnyEvent::detect>).
426
427As most event loops do not support waiting for child events, they will be
428emulated by AnyEvent in most cases, in which the latency and race problems
429mentioned in the description of signal watchers apply.
408 430
409Example: fork a process and wait for it 431Example: fork a process and wait for it
410 432
411 my $done = AnyEvent->condvar; 433 my $done = AnyEvent->condvar;
412 434
1024 1046
1025=cut 1047=cut
1026 1048
1027package AnyEvent; 1049package AnyEvent;
1028 1050
1051# basically a tuned-down version of common::sense
1052sub common_sense {
1029no warnings; 1053 # no warnings
1054 ${^WARNING_BITS} ^= ${^WARNING_BITS};
1030use strict qw(vars subs); 1055 # use strict vars subs
1056 $^H |= 0x00000600;
1057}
1058
1059BEGIN { AnyEvent::common_sense }
1031 1060
1032use Carp (); 1061use Carp ();
1033 1062
1034our $VERSION = 4.83; 1063our $VERSION = 4.85;
1035our $MODEL; 1064our $MODEL;
1036 1065
1037our $AUTOLOAD; 1066our $AUTOLOAD;
1038our @ISA; 1067our @ISA;
1039 1068
1040our @REGISTRY; 1069our @REGISTRY;
1041 1070
1042our $WIN32; 1071our $WIN32;
1072
1073our $VERBOSE;
1043 1074
1044BEGIN { 1075BEGIN {
1045 eval "sub WIN32(){ " . (($^O =~ /mswin32/i)*1) ." }"; 1076 eval "sub WIN32(){ " . (($^O =~ /mswin32/i)*1) ." }";
1046 eval "sub TAINT(){ " . (${^TAINT}*1) . " }"; 1077 eval "sub TAINT(){ " . (${^TAINT}*1) . " }";
1047 1078
1048 delete @ENV{grep /^PERL_ANYEVENT_/, keys %ENV} 1079 delete @ENV{grep /^PERL_ANYEVENT_/, keys %ENV}
1049 if ${^TAINT}; 1080 if ${^TAINT};
1050}
1051 1081
1052our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; 1082 $VERBOSE = $ENV{PERL_ANYEVENT_VERBOSE}*1;
1083
1084}
1085
1086our $MAX_SIGNAL_LATENCY = 10;
1053 1087
1054our %PROTOCOL; # (ipv4|ipv6) => (1|2), higher numbers are preferred 1088our %PROTOCOL; # (ipv4|ipv6) => (1|2), higher numbers are preferred
1055 1089
1056{ 1090{
1057 my $idx; 1091 my $idx;
1108 @post_detect = grep $_ != ${$_[0]}, @post_detect; 1142 @post_detect = grep $_ != ${$_[0]}, @post_detect;
1109} 1143}
1110 1144
1111sub detect() { 1145sub detect() {
1112 unless ($MODEL) { 1146 unless ($MODEL) {
1113 no strict 'refs';
1114 local $SIG{__DIE__}; 1147 local $SIG{__DIE__};
1115 1148
1116 if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z]+)$/) { 1149 if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z]+)$/) {
1117 my $model = "AnyEvent::Impl::$1"; 1150 my $model = "AnyEvent::Impl::$1";
1118 if (eval "require $model") { 1151 if (eval "require $model") {
1119 $MODEL = $model; 1152 $MODEL = $model;
1120 warn "AnyEvent: loaded model '$model' (forced by \$ENV{PERL_ANYEVENT_MODEL}), using it.\n" if $verbose > 1; 1153 warn "AnyEvent: loaded model '$model' (forced by \$ENV{PERL_ANYEVENT_MODEL}), using it.\n" if $VERBOSE >= 2;
1121 } else { 1154 } else {
1122 warn "AnyEvent: unable to load model '$model' (from \$ENV{PERL_ANYEVENT_MODEL}):\n$@" if $verbose; 1155 warn "AnyEvent: unable to load model '$model' (from \$ENV{PERL_ANYEVENT_MODEL}):\n$@" if $VERBOSE;
1123 } 1156 }
1124 } 1157 }
1125 1158
1126 # check for already loaded models 1159 # check for already loaded models
1127 unless ($MODEL) { 1160 unless ($MODEL) {
1128 for (@REGISTRY, @models) { 1161 for (@REGISTRY, @models) {
1129 my ($package, $model) = @$_; 1162 my ($package, $model) = @$_;
1130 if (${"$package\::VERSION"} > 0) { 1163 if (${"$package\::VERSION"} > 0) {
1131 if (eval "require $model") { 1164 if (eval "require $model") {
1132 $MODEL = $model; 1165 $MODEL = $model;
1133 warn "AnyEvent: autodetected model '$model', using it.\n" if $verbose > 1; 1166 warn "AnyEvent: autodetected model '$model', using it.\n" if $VERBOSE >= 2;
1134 last; 1167 last;
1135 } 1168 }
1136 } 1169 }
1137 } 1170 }
1138 1171
1143 my ($package, $model) = @$_; 1176 my ($package, $model) = @$_;
1144 if (eval "require $package" 1177 if (eval "require $package"
1145 and ${"$package\::VERSION"} > 0 1178 and ${"$package\::VERSION"} > 0
1146 and eval "require $model") { 1179 and eval "require $model") {
1147 $MODEL = $model; 1180 $MODEL = $model;
1148 warn "AnyEvent: autoprobed model '$model', using it.\n" if $verbose > 1; 1181 warn "AnyEvent: autoprobed model '$model', using it.\n" if $VERBOSE >= 2;
1149 last; 1182 last;
1150 } 1183 }
1151 } 1184 }
1152 1185
1153 $MODEL 1186 $MODEL
1198 1231
1199package AnyEvent::Base; 1232package AnyEvent::Base;
1200 1233
1201# default implementations for many methods 1234# default implementations for many methods
1202 1235
1203BEGIN { 1236sub _time {
1237 # probe for availability of Time::HiRes
1204 if (eval "use Time::HiRes (); Time::HiRes::time (); 1") { 1238 if (eval "use Time::HiRes (); Time::HiRes::time (); 1") {
1239 warn "AnyEvent: using Time::HiRes for sub-second timing accuracy.\n" if $VERBOSE >= 8;
1205 *_time = \&Time::HiRes::time; 1240 *_time = \&Time::HiRes::time;
1206 # if (eval "use POSIX (); (POSIX::times())... 1241 # if (eval "use POSIX (); (POSIX::times())...
1207 } else { 1242 } else {
1243 warn "AnyEvent: using built-in time(), WARNING, no sub-second resolution!\n" if $VERBOSE;
1208 *_time = sub { time }; # epic fail 1244 *_time = sub { time }; # epic fail
1209 } 1245 }
1246
1247 &_time
1210} 1248}
1211 1249
1212sub time { _time } 1250sub time { _time }
1213sub now { _time } 1251sub now { _time }
1214sub now_update { } 1252sub now_update { }
1219 bless { @_ == 3 ? (_ae_cb => $_[2]) : () }, "AnyEvent::CondVar" 1257 bless { @_ == 3 ? (_ae_cb => $_[2]) : () }, "AnyEvent::CondVar"
1220} 1258}
1221 1259
1222# default implementation for ->signal 1260# default implementation for ->signal
1223 1261
1262our $HAVE_ASYNC_INTERRUPT;
1224our ($SIGPIPE_R, $SIGPIPE_W, %SIG_CB, %SIG_EV, $SIG_IO); 1263our ($SIGPIPE_R, $SIGPIPE_W, %SIG_CB, %SIG_EV, $SIG_IO);
1264our (%SIG_ASY, %SIG_ASY_W);
1265our ($SIG_COUNT, $SIG_TW);
1225 1266
1226sub _signal_exec { 1267sub _signal_exec {
1268 $HAVE_ASYNC_INTERRUPT
1269 ? $SIGPIPE_R->drain
1227 sysread $SIGPIPE_R, my $dummy, 4; 1270 : sysread $SIGPIPE_R, my $dummy, 9;
1228 1271
1229 while (%SIG_EV) { 1272 while (%SIG_EV) {
1230 for (keys %SIG_EV) { 1273 for (keys %SIG_EV) {
1231 delete $SIG_EV{$_}; 1274 delete $SIG_EV{$_};
1232 $_->() for values %{ $SIG_CB{$_} || {} }; 1275 $_->() for values %{ $SIG_CB{$_} || {} };
1233 } 1276 }
1234 } 1277 }
1235} 1278}
1236 1279
1280# install a dumym wakeupw atcher to reduce signal catching latency
1281sub _sig_add() {
1282 unless ($SIG_COUNT++) {
1283 # try to align timer on a full-second boundary, if possible
1284 my $NOW = AnyEvent->now;
1285
1286 $SIG_TW = AnyEvent->timer (
1287 after => $MAX_SIGNAL_LATENCY - ($NOW - int $NOW),
1288 interval => $MAX_SIGNAL_LATENCY,
1289 cb => sub { }, # just for the PERL_ASYNC_CHECK
1290 );
1291 }
1292}
1293
1294sub _sig_del {
1295 undef $SIG_TW
1296 unless --$SIG_COUNT;
1297}
1298
1299sub _signal {
1300 my (undef, %arg) = @_;
1301
1302 my $signal = uc $arg{signal}
1303 or Carp::croak "required option 'signal' is missing";
1304
1305 $SIG_CB{$signal}{$arg{cb}} = $arg{cb};
1306
1307 if ($HAVE_ASYNC_INTERRUPT) {
1308 # async::interrupt
1309
1310 $SIG_ASY{$signal} ||= do {
1311 my $asy = new Async::Interrupt
1312 cb => sub { undef $SIG_EV{$signal} },
1313 signal => $signal,
1314 pipe => [$SIGPIPE_R->filenos],
1315 ;
1316 $asy->pipe_autodrain (0);
1317
1318 $asy
1319 };
1320
1321 } else {
1322 # pure perl
1323
1324 $SIG{$signal} ||= sub {
1325 local $!;
1326 syswrite $SIGPIPE_W, "\x00", 1 unless %SIG_EV;
1327 undef $SIG_EV{$signal};
1328 };
1329
1330 # can't do signal processing without introducing races in pure perl,
1331 # so limit the signal latency.
1332 _sig_add;
1333 }
1334
1335 bless [$signal, $arg{cb}], "AnyEvent::Base::signal"
1336}
1337
1237sub signal { 1338sub signal {
1238 my (undef, %arg) = @_; 1339 # probe for availability of Async::Interrupt
1340 if (!$ENV{PERL_ANYEVENT_AVOID_ASYNC_INTERRUPT} && eval "use Async::Interrupt 0.6 (); 1") {
1341 warn "AnyEvent: using Async::Interrupt for race-free signal handling.\n" if $VERBOSE >= 8;
1239 1342
1240 unless ($SIGPIPE_R) { 1343 $HAVE_ASYNC_INTERRUPT = 1;
1344 $SIGPIPE_R = new Async::Interrupt::EventPipe;
1345 $SIG_IO = AnyEvent->io (fh => $SIGPIPE_R->fileno, poll => "r", cb => \&_signal_exec);
1346
1347 } else {
1348 warn "AnyEvent: using emulated perl signal handling with latency timer.\n" if $VERBOSE >= 8;
1349
1241 require Fcntl; 1350 require Fcntl;
1242 1351
1243 if (AnyEvent::WIN32) { 1352 if (AnyEvent::WIN32) {
1244 require AnyEvent::Util; 1353 require AnyEvent::Util;
1245 1354
1260 or Carp::croak "AnyEvent: unable to create a signal reporting pipe: $!\n"; 1369 or Carp::croak "AnyEvent: unable to create a signal reporting pipe: $!\n";
1261 1370
1262 $SIG_IO = AnyEvent->io (fh => $SIGPIPE_R, poll => "r", cb => \&_signal_exec); 1371 $SIG_IO = AnyEvent->io (fh => $SIGPIPE_R, poll => "r", cb => \&_signal_exec);
1263 } 1372 }
1264 1373
1265 my $signal = uc $arg{signal} 1374 *signal = \&_signal;
1266 or Carp::croak "required option 'signal' is missing"; 1375 &signal
1267
1268 $SIG_CB{$signal}{$arg{cb}} = $arg{cb};
1269 $SIG{$signal} ||= sub {
1270 local $!;
1271 syswrite $SIGPIPE_W, "\x00", 1 unless %SIG_EV;
1272 undef $SIG_EV{$signal};
1273 };
1274
1275 bless [$signal, $arg{cb}], "AnyEvent::Base::signal"
1276} 1376}
1277 1377
1278sub AnyEvent::Base::signal::DESTROY { 1378sub AnyEvent::Base::signal::DESTROY {
1279 my ($signal, $cb) = @{$_[0]}; 1379 my ($signal, $cb) = @{$_[0]};
1280 1380
1381 _sig_del;
1382
1281 delete $SIG_CB{$signal}{$cb}; 1383 delete $SIG_CB{$signal}{$cb};
1282 1384
1385 $HAVE_ASYNC_INTERRUPT
1386 ? delete $SIG_ASY{$signal}
1283 # delete doesn't work with older perls - they then 1387 : # delete doesn't work with older perls - they then
1284 # print weird messages, or just unconditionally exit 1388 # print weird messages, or just unconditionally exit
1285 # instead of getting the default action. 1389 # instead of getting the default action.
1390 undef $SIG{$signal}
1286 undef $SIG{$signal} unless keys %{ $SIG_CB{$signal} }; 1391 unless keys %{ $SIG_CB{$signal} };
1287} 1392}
1288 1393
1289# default implementation for ->child 1394# default implementation for ->child
1290 1395
1291our %PID_CB; 1396our %PID_CB;
1293our $CHLD_DELAY_W; 1398our $CHLD_DELAY_W;
1294our $WNOHANG; 1399our $WNOHANG;
1295 1400
1296sub _sigchld { 1401sub _sigchld {
1297 while (0 < (my $pid = waitpid -1, $WNOHANG)) { 1402 while (0 < (my $pid = waitpid -1, $WNOHANG)) {
1403 $_->($pid, $?)
1298 $_->($pid, $?) for (values %{ $PID_CB{$pid} || {} }), 1404 for values %{ $PID_CB{$pid} || {} },
1299 (values %{ $PID_CB{0} || {} }); 1405 values %{ $PID_CB{0} || {} };
1300 } 1406 }
1301} 1407}
1302 1408
1303sub child { 1409sub child {
1304 my (undef, %arg) = @_; 1410 my (undef, %arg) = @_;
1306 defined (my $pid = $arg{pid} + 0) 1412 defined (my $pid = $arg{pid} + 0)
1307 or Carp::croak "required option 'pid' is missing"; 1413 or Carp::croak "required option 'pid' is missing";
1308 1414
1309 $PID_CB{$pid}{$arg{cb}} = $arg{cb}; 1415 $PID_CB{$pid}{$arg{cb}} = $arg{cb};
1310 1416
1417 # WNOHANG is almost cetrainly 1 everywhere
1418 $WNOHANG ||= $^O =~ /^(?:openbsd|netbsd|linux|freebsd|cygwin|MSWin32)$/
1419 ? 1
1311 $WNOHANG ||= eval { local $SIG{__DIE__}; require POSIX; &POSIX::WNOHANG } || 1; 1420 : eval { local $SIG{__DIE__}; require POSIX; &POSIX::WNOHANG } || 1;
1312 1421
1313 unless ($CHLD_W) { 1422 unless ($CHLD_W) {
1314 $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_sigchld); 1423 $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_sigchld);
1315 # child could be a zombie already, so make at least one round 1424 # child could be a zombie already, so make at least one round
1316 &_sigchld; 1425 &_sigchld;
1368 1477
1369our @ISA = AnyEvent::CondVar::Base::; 1478our @ISA = AnyEvent::CondVar::Base::;
1370 1479
1371package AnyEvent::CondVar::Base; 1480package AnyEvent::CondVar::Base;
1372 1481
1373use overload 1482#use overload
1374 '&{}' => sub { my $self = shift; sub { $self->send (@_) } }, 1483# '&{}' => sub { my $self = shift; sub { $self->send (@_) } },
1375 fallback => 1; 1484# fallback => 1;
1485
1486# save 300+ kilobytes by dirtily hardcoding overloading
1487${"AnyEvent::CondVar::Base::OVERLOAD"}{dummy}++; # Register with magic by touching.
1488*{'AnyEvent::CondVar::Base::()'} = sub { }; # "Make it findable via fetchmethod."
1489*{'AnyEvent::CondVar::Base::(&{}'} = sub { my $self = shift; sub { $self->send (@_) } }; # &{}
1490${'AnyEvent::CondVar::Base::()'} = 1; # fallback
1376 1491
1377our $WAITING; 1492our $WAITING;
1378 1493
1379sub _send { 1494sub _send {
1380 # nop 1495 # nop
1471C<PERL_ANYEVENT_MODEL>. 1586C<PERL_ANYEVENT_MODEL>.
1472 1587
1473When set to C<2> or higher, cause AnyEvent to report to STDERR which event 1588When set to C<2> or higher, cause AnyEvent to report to STDERR which event
1474model it chooses. 1589model it chooses.
1475 1590
1591When set to C<8> or higher, then AnyEvent will report extra information on
1592which optional modules it loads and how it implements certain features.
1593
1476=item C<PERL_ANYEVENT_STRICT> 1594=item C<PERL_ANYEVENT_STRICT>
1477 1595
1478AnyEvent does not do much argument checking by default, as thorough 1596AnyEvent does not do much argument checking by default, as thorough
1479argument checking is very costly. Setting this variable to a true value 1597argument checking is very costly. Setting this variable to a true value
1480will cause AnyEvent to load C<AnyEvent::Strict> and then to thoroughly 1598will cause AnyEvent to load C<AnyEvent::Strict> and then to thoroughly
1481check the arguments passed to most method calls. If it finds any problems, 1599check the arguments passed to most method calls. If it finds any problems,
1482it will croak. 1600it will croak.
1483 1601
1484In other words, enables "strict" mode. 1602In other words, enables "strict" mode.
1485 1603
1486Unlike C<use strict>, it is definitely recommended to keep it off in 1604Unlike C<use strict> (or it's modern cousin, C<< use L<common::sense>
1487production. Keeping C<PERL_ANYEVENT_STRICT=1> in your environment while 1605>>, it is definitely recommended to keep it off in production. Keeping
1488developing programs can be very useful, however. 1606C<PERL_ANYEVENT_STRICT=1> in your environment while developing programs
1607can be very useful, however.
1489 1608
1490=item C<PERL_ANYEVENT_MODEL> 1609=item C<PERL_ANYEVENT_MODEL>
1491 1610
1492This can be used to specify the event model to be used by AnyEvent, before 1611This can be used to specify the event model to be used by AnyEvent, before
1493auto detection and -probing kicks in. It must be a string consisting 1612auto detection and -probing kicks in. It must be a string consisting
1555 1674
1556When neither C<ca_file> nor C<ca_path> was specified during 1675When neither C<ca_file> nor C<ca_path> was specified during
1557L<AnyEvent::TLS> context creation, and either of these environment 1676L<AnyEvent::TLS> context creation, and either of these environment
1558variables exist, they will be used to specify CA certificate locations 1677variables exist, they will be used to specify CA certificate locations
1559instead of a system-dependent default. 1678instead of a system-dependent default.
1679
1680=item C<PERL_ANYEVENT_AVOID_GUARD> and C<PERL_ANYEVENT_AVOID_ASYNC_INTERRUPT>
1681
1682When these are set to C<1>, then the respective modules are not
1683loaded. Mostly good for testing AnyEvent itself.
1560 1684
1561=back 1685=back
1562 1686
1563=head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE 1687=head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE
1564 1688
2132 if $SIG{CHLD} eq 'IGNORE'; 2256 if $SIG{CHLD} eq 'IGNORE';
2133 2257
2134$SIG{PIPE} = sub { } 2258$SIG{PIPE} = sub { }
2135 unless defined $SIG{PIPE}; 2259 unless defined $SIG{PIPE};
2136 2260
2261=head1 RECOMMENDED/OPTIONAL MODULES
2262
2263One of AnyEvent's main goals is to be 100% Pure-Perl(tm): only perl (and
2264it's built-in modules) are required to use it.
2265
2266That does not mean that AnyEvent won't take advantage of some additional
2267modules if they are installed.
2268
2269This section epxlains which additional modules will be used, and how they
2270affect AnyEvent's operetion.
2271
2272=over 4
2273
2274=item L<Async::Interrupt>
2275
2276This slightly arcane module is used to implement fast signal handling: To
2277my knowledge, there is no way to do completely race-free and quick
2278signal handling in pure perl. To ensure that signals still get
2279delivered, AnyEvent will start an interval timer to wake up perl (and
2280catch the signals) with some delay (default is 10 seconds, look for
2281C<$AnyEvent::MAX_SIGNAL_LATENCY>).
2282
2283If this module is available, then it will be used to implement signal
2284catching, which means that signals will not be delayed, and the event loop
2285will not be interrupted regularly, which is more efficient (And good for
2286battery life on laptops).
2287
2288This affects not just the pure-perl event loop, but also other event loops
2289that have no signal handling on their own (e.g. Glib, Tk, Qt).
2290
2291Some event loops (POE, Event, Event::Lib) offer signal watchers natively,
2292and either employ their own workarounds (POE) or use AnyEvent's workaround
2293(using C<$AnyEvent::MAX_SIGNAL_LATENCY>). Installing L<Async::Interrupt>
2294does nothing for those backends.
2295
2296=item L<EV>
2297
2298This module isn't really "optional", as it is simply one of the backend
2299event loops that AnyEvent can use. However, it is simply the best event
2300loop available in terms of features, speed and stability: It supports
2301the AnyEvent API optimally, implements all the watcher types in XS, does
2302automatic timer adjustments even when no monotonic clock is available,
2303can take avdantage of advanced kernel interfaces such as C<epoll> and
2304C<kqueue>, and is the fastest backend I<by far>. You can even embed
2305L<Glib>/L<Gtk2> in it (or vice versa, see L<EV::Glib> and L<Glib::EV>).
2306
2307=item L<Guard>
2308
2309The guard module, when used, will be used to implement
2310C<AnyEvent::Util::guard>. This speeds up guards considerably (and uses a
2311lot less memory), but otherwise doesn't affect guard operation much. It is
2312purely used for performance.
2313
2314=item L<JSON> and L<JSON::XS>
2315
2316This module is required when you want to read or write JSON data via
2317L<AnyEvent::Handle>. It is also written in pure-perl, but can take
2318advantage of the ultra-high-speed L<JSON::XS> module when it is installed.
2319
2320In fact, L<AnyEvent::Handle> will use L<JSON::XS> by default if it is
2321installed.
2322
2323=item L<Net::SSLeay>
2324
2325Implementing TLS/SSL in Perl is certainly interesting, but not very
2326worthwhile: If this module is installed, then L<AnyEvent::Handle> (with
2327the help of L<AnyEvent::TLS>), gains the ability to do TLS/SSL.
2328
2329=item L<Time::HiRes>
2330
2331This module is part of perl since release 5.008. It will be used when the
2332chosen event library does not come with a timing source on it's own. The
2333pure-perl event loop (L<AnyEvent::Impl::Perl>) will additionally use it to
2334try to use a monotonic clock for timing stability.
2335
2336=back
2337
2338
2137=head1 FORK 2339=head1 FORK
2138 2340
2139Most event libraries are not fork-safe. The ones who are usually are 2341Most event libraries are not fork-safe. The ones who are usually are
2140because they rely on inefficient but fork-safe C<select> or C<poll> 2342because they rely on inefficient but fork-safe C<select> or C<poll>
2141calls. Only L<EV> is fully fork-aware. 2343calls. Only L<EV> is fully fork-aware.
2142 2344
2143If you have to fork, you must either do so I<before> creating your first 2345If you have to fork, you must either do so I<before> creating your first
2144watcher OR you must not use AnyEvent at all in the child. 2346watcher OR you must not use AnyEvent at all in the child OR you must do
2347something completely out of the scope of AnyEvent.
2145 2348
2146 2349
2147=head1 SECURITY CONSIDERATIONS 2350=head1 SECURITY CONSIDERATIONS
2148 2351
2149AnyEvent can be forced to load any event model via 2352AnyEvent can be forced to load any event model via

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines