… | |
… | |
389 | |
389 | |
390 | The instrument to do that is called a "condition variable", so called |
390 | The instrument to do that is called a "condition variable", so called |
391 | because they represent a condition that must become true. |
391 | because they represent a condition that must become true. |
392 | |
392 | |
393 | Condition variables can be created by calling the C<< AnyEvent->condvar |
393 | Condition variables can be created by calling the C<< AnyEvent->condvar |
394 | |
|
|
395 | >> method, usually without arguments. The only argument pair allowed is |
394 | >> method, usually without arguments. The only argument pair allowed is |
396 | |
395 | |
397 | C<cb>, which specifies a callback to be called when the condition variable |
396 | C<cb>, which specifies a callback to be called when the condition variable |
398 | becomes true, with the condition variable as the first argument (but not |
397 | becomes true, with the condition variable as the first argument (but not |
399 | the results). |
398 | the results). |
… | |
… | |
851 | =cut |
850 | =cut |
852 | |
851 | |
853 | package AnyEvent; |
852 | package AnyEvent; |
854 | |
853 | |
855 | no warnings; |
854 | no warnings; |
856 | use strict; |
855 | use strict qw(vars subs); |
857 | |
856 | |
858 | use Carp; |
857 | use Carp; |
859 | |
858 | |
860 | our $VERSION = 4.22; |
859 | our $VERSION = 4.233; |
861 | our $MODEL; |
860 | our $MODEL; |
862 | |
861 | |
863 | our $AUTOLOAD; |
862 | our $AUTOLOAD; |
864 | our @ISA; |
863 | our @ISA; |
865 | |
864 | |
… | |
… | |
1017 | |
1016 | |
1018 | package AnyEvent::Base; |
1017 | package AnyEvent::Base; |
1019 | |
1018 | |
1020 | # default implementation for now and time |
1019 | # default implementation for now and time |
1021 | |
1020 | |
1022 | use Time::HiRes (); |
1021 | BEGIN { |
|
|
1022 | if (eval "use Time::HiRes (); time (); 1") { |
|
|
1023 | *_time = \&Time::HiRes::time; |
|
|
1024 | # if (eval "use POSIX (); (POSIX::times())... |
|
|
1025 | } else { |
|
|
1026 | *_time = \&CORE::time; # epic fail |
|
|
1027 | } |
|
|
1028 | } |
1023 | |
1029 | |
1024 | sub time { Time::HiRes::time } |
1030 | sub time { _time } |
1025 | sub now { Time::HiRes::time } |
1031 | sub now { _time } |
1026 | |
1032 | |
1027 | # default implementation for ->condvar |
1033 | # default implementation for ->condvar |
1028 | |
1034 | |
1029 | sub condvar { |
1035 | sub condvar { |
1030 | bless { @_ == 3 ? (_ae_cb => $_[2]) : () }, AnyEvent::CondVar:: |
1036 | bless { @_ == 3 ? (_ae_cb => $_[2]) : () }, AnyEvent::CondVar:: |
… | |
… | |
1169 | |
1175 | |
1170 | # undocumented/compatibility with pre-3.4 |
1176 | # undocumented/compatibility with pre-3.4 |
1171 | *broadcast = \&send; |
1177 | *broadcast = \&send; |
1172 | *wait = \&_wait; |
1178 | *wait = \&_wait; |
1173 | |
1179 | |
|
|
1180 | =head1 ERROR AND EXCEPTION HANDLING |
|
|
1181 | |
|
|
1182 | In general, AnyEvent does not do any error handling - it relies on the |
|
|
1183 | caller to do that if required. The L<AnyEvent::Strict> module (see also |
|
|
1184 | the C<PERL_ANYEVENT_STRICT> environment variable, below) provides strict |
|
|
1185 | checking of all AnyEvent methods, however, which is highly useful during |
|
|
1186 | development. |
|
|
1187 | |
|
|
1188 | As for exception handling (i.e. runtime errors and exceptions thrown while |
|
|
1189 | executing a callback), this is not only highly event-loop specific, but |
|
|
1190 | also not in any way wrapped by this module, as this is the job of the main |
|
|
1191 | program. |
|
|
1192 | |
|
|
1193 | The pure perl event loop simply re-throws the exception (usually |
|
|
1194 | within C<< condvar->recv >>), the L<Event> and L<EV> modules call C<< |
|
|
1195 | $Event/EV::DIED->() >>, L<Glib> uses C<< install_exception_handler >> and |
|
|
1196 | so on. |
|
|
1197 | |
|
|
1198 | =head1 ENVIRONMENT VARIABLES |
|
|
1199 | |
|
|
1200 | The following environment variables are used by this module or its |
|
|
1201 | submodules: |
|
|
1202 | |
|
|
1203 | =over 4 |
|
|
1204 | |
|
|
1205 | =item C<PERL_ANYEVENT_VERBOSE> |
|
|
1206 | |
|
|
1207 | By default, AnyEvent will be completely silent except in fatal |
|
|
1208 | conditions. You can set this environment variable to make AnyEvent more |
|
|
1209 | talkative. |
|
|
1210 | |
|
|
1211 | When set to C<1> or higher, causes AnyEvent to warn about unexpected |
|
|
1212 | conditions, such as not being able to load the event model specified by |
|
|
1213 | C<PERL_ANYEVENT_MODEL>. |
|
|
1214 | |
|
|
1215 | When set to C<2> or higher, cause AnyEvent to report to STDERR which event |
|
|
1216 | model it chooses. |
|
|
1217 | |
|
|
1218 | =item C<PERL_ANYEVENT_STRICT> |
|
|
1219 | |
|
|
1220 | AnyEvent does not do much argument checking by default, as thorough |
|
|
1221 | argument checking is very costly. Setting this variable to a true value |
|
|
1222 | will cause AnyEvent to load C<AnyEvent::Strict> and then to thoroughly |
|
|
1223 | check the arguments passed to most method calls. If it finds any problems |
|
|
1224 | it will croak. |
|
|
1225 | |
|
|
1226 | In other words, enables "strict" mode. |
|
|
1227 | |
|
|
1228 | Unlike C<use strict>, it is definitely recommended ot keep it off in |
|
|
1229 | production. Keeping C<PERL_ANYEVENT_STRICT=1> in your environment while |
|
|
1230 | developing programs can be very useful, however. |
|
|
1231 | |
|
|
1232 | =item C<PERL_ANYEVENT_MODEL> |
|
|
1233 | |
|
|
1234 | This can be used to specify the event model to be used by AnyEvent, before |
|
|
1235 | auto detection and -probing kicks in. It must be a string consisting |
|
|
1236 | entirely of ASCII letters. The string C<AnyEvent::Impl::> gets prepended |
|
|
1237 | and the resulting module name is loaded and if the load was successful, |
|
|
1238 | used as event model. If it fails to load AnyEvent will proceed with |
|
|
1239 | auto detection and -probing. |
|
|
1240 | |
|
|
1241 | This functionality might change in future versions. |
|
|
1242 | |
|
|
1243 | For example, to force the pure perl model (L<AnyEvent::Impl::Perl>) you |
|
|
1244 | could start your program like this: |
|
|
1245 | |
|
|
1246 | PERL_ANYEVENT_MODEL=Perl perl ... |
|
|
1247 | |
|
|
1248 | =item C<PERL_ANYEVENT_PROTOCOLS> |
|
|
1249 | |
|
|
1250 | Used by both L<AnyEvent::DNS> and L<AnyEvent::Socket> to determine preferences |
|
|
1251 | for IPv4 or IPv6. The default is unspecified (and might change, or be the result |
|
|
1252 | of auto probing). |
|
|
1253 | |
|
|
1254 | Must be set to a comma-separated list of protocols or address families, |
|
|
1255 | current supported: C<ipv4> and C<ipv6>. Only protocols mentioned will be |
|
|
1256 | used, and preference will be given to protocols mentioned earlier in the |
|
|
1257 | list. |
|
|
1258 | |
|
|
1259 | This variable can effectively be used for denial-of-service attacks |
|
|
1260 | against local programs (e.g. when setuid), although the impact is likely |
|
|
1261 | small, as the program has to handle connection errors already- |
|
|
1262 | |
|
|
1263 | Examples: C<PERL_ANYEVENT_PROTOCOLS=ipv4,ipv6> - prefer IPv4 over IPv6, |
|
|
1264 | but support both and try to use both. C<PERL_ANYEVENT_PROTOCOLS=ipv4> |
|
|
1265 | - only support IPv4, never try to resolve or contact IPv6 |
|
|
1266 | addresses. C<PERL_ANYEVENT_PROTOCOLS=ipv6,ipv4> support either IPv4 or |
|
|
1267 | IPv6, but prefer IPv6 over IPv4. |
|
|
1268 | |
|
|
1269 | =item C<PERL_ANYEVENT_EDNS0> |
|
|
1270 | |
|
|
1271 | Used by L<AnyEvent::DNS> to decide whether to use the EDNS0 extension |
|
|
1272 | for DNS. This extension is generally useful to reduce DNS traffic, but |
|
|
1273 | some (broken) firewalls drop such DNS packets, which is why it is off by |
|
|
1274 | default. |
|
|
1275 | |
|
|
1276 | Setting this variable to C<1> will cause L<AnyEvent::DNS> to announce |
|
|
1277 | EDNS0 in its DNS requests. |
|
|
1278 | |
|
|
1279 | =item C<PERL_ANYEVENT_MAX_FORKS> |
|
|
1280 | |
|
|
1281 | The maximum number of child processes that C<AnyEvent::Util::fork_call> |
|
|
1282 | will create in parallel. |
|
|
1283 | |
|
|
1284 | =back |
|
|
1285 | |
1174 | =head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE |
1286 | =head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE |
1175 | |
1287 | |
1176 | This is an advanced topic that you do not normally need to use AnyEvent in |
1288 | This is an advanced topic that you do not normally need to use AnyEvent in |
1177 | a module. This section is only of use to event loop authors who want to |
1289 | a module. This section is only of use to event loop authors who want to |
1178 | provide AnyEvent compatibility. |
1290 | provide AnyEvent compatibility. |
… | |
… | |
1211 | |
1323 | |
1212 | I<rxvt-unicode> also cheats a bit by not providing blocking access to |
1324 | I<rxvt-unicode> also cheats a bit by not providing blocking access to |
1213 | condition variables: code blocking while waiting for a condition will |
1325 | condition variables: code blocking while waiting for a condition will |
1214 | C<die>. This still works with most modules/usages, and blocking calls must |
1326 | C<die>. This still works with most modules/usages, and blocking calls must |
1215 | not be done in an interactive application, so it makes sense. |
1327 | not be done in an interactive application, so it makes sense. |
1216 | |
|
|
1217 | =head1 ENVIRONMENT VARIABLES |
|
|
1218 | |
|
|
1219 | The following environment variables are used by this module: |
|
|
1220 | |
|
|
1221 | =over 4 |
|
|
1222 | |
|
|
1223 | =item C<PERL_ANYEVENT_VERBOSE> |
|
|
1224 | |
|
|
1225 | By default, AnyEvent will be completely silent except in fatal |
|
|
1226 | conditions. You can set this environment variable to make AnyEvent more |
|
|
1227 | talkative. |
|
|
1228 | |
|
|
1229 | When set to C<1> or higher, causes AnyEvent to warn about unexpected |
|
|
1230 | conditions, such as not being able to load the event model specified by |
|
|
1231 | C<PERL_ANYEVENT_MODEL>. |
|
|
1232 | |
|
|
1233 | When set to C<2> or higher, cause AnyEvent to report to STDERR which event |
|
|
1234 | model it chooses. |
|
|
1235 | |
|
|
1236 | =item C<PERL_ANYEVENT_STRICT> |
|
|
1237 | |
|
|
1238 | AnyEvent does not do much argument checking by default, as thorough |
|
|
1239 | argument checking is very costly. Setting this variable to a true value |
|
|
1240 | will cause AnyEvent to load C<AnyEvent::Strict> and then to thoroughly |
|
|
1241 | check the arguments passed to most method calls. If it finds any problems |
|
|
1242 | it will croak. |
|
|
1243 | |
|
|
1244 | In other words, enables "strict" mode. |
|
|
1245 | |
|
|
1246 | Unlike C<use strict> it is definitely recommended ot keep it off in |
|
|
1247 | production. |
|
|
1248 | |
|
|
1249 | =item C<PERL_ANYEVENT_MODEL> |
|
|
1250 | |
|
|
1251 | This can be used to specify the event model to be used by AnyEvent, before |
|
|
1252 | auto detection and -probing kicks in. It must be a string consisting |
|
|
1253 | entirely of ASCII letters. The string C<AnyEvent::Impl::> gets prepended |
|
|
1254 | and the resulting module name is loaded and if the load was successful, |
|
|
1255 | used as event model. If it fails to load AnyEvent will proceed with |
|
|
1256 | auto detection and -probing. |
|
|
1257 | |
|
|
1258 | This functionality might change in future versions. |
|
|
1259 | |
|
|
1260 | For example, to force the pure perl model (L<AnyEvent::Impl::Perl>) you |
|
|
1261 | could start your program like this: |
|
|
1262 | |
|
|
1263 | PERL_ANYEVENT_MODEL=Perl perl ... |
|
|
1264 | |
|
|
1265 | =item C<PERL_ANYEVENT_PROTOCOLS> |
|
|
1266 | |
|
|
1267 | Used by both L<AnyEvent::DNS> and L<AnyEvent::Socket> to determine preferences |
|
|
1268 | for IPv4 or IPv6. The default is unspecified (and might change, or be the result |
|
|
1269 | of auto probing). |
|
|
1270 | |
|
|
1271 | Must be set to a comma-separated list of protocols or address families, |
|
|
1272 | current supported: C<ipv4> and C<ipv6>. Only protocols mentioned will be |
|
|
1273 | used, and preference will be given to protocols mentioned earlier in the |
|
|
1274 | list. |
|
|
1275 | |
|
|
1276 | This variable can effectively be used for denial-of-service attacks |
|
|
1277 | against local programs (e.g. when setuid), although the impact is likely |
|
|
1278 | small, as the program has to handle connection errors already- |
|
|
1279 | |
|
|
1280 | Examples: C<PERL_ANYEVENT_PROTOCOLS=ipv4,ipv6> - prefer IPv4 over IPv6, |
|
|
1281 | but support both and try to use both. C<PERL_ANYEVENT_PROTOCOLS=ipv4> |
|
|
1282 | - only support IPv4, never try to resolve or contact IPv6 |
|
|
1283 | addresses. C<PERL_ANYEVENT_PROTOCOLS=ipv6,ipv4> support either IPv4 or |
|
|
1284 | IPv6, but prefer IPv6 over IPv4. |
|
|
1285 | |
|
|
1286 | =item C<PERL_ANYEVENT_EDNS0> |
|
|
1287 | |
|
|
1288 | Used by L<AnyEvent::DNS> to decide whether to use the EDNS0 extension |
|
|
1289 | for DNS. This extension is generally useful to reduce DNS traffic, but |
|
|
1290 | some (broken) firewalls drop such DNS packets, which is why it is off by |
|
|
1291 | default. |
|
|
1292 | |
|
|
1293 | Setting this variable to C<1> will cause L<AnyEvent::DNS> to announce |
|
|
1294 | EDNS0 in its DNS requests. |
|
|
1295 | |
|
|
1296 | =item C<PERL_ANYEVENT_MAX_FORKS> |
|
|
1297 | |
|
|
1298 | The maximum number of child processes that C<AnyEvent::Util::fork_call> |
|
|
1299 | will create in parallel. |
|
|
1300 | |
|
|
1301 | =back |
|
|
1302 | |
1328 | |
1303 | =head1 EXAMPLE PROGRAM |
1329 | =head1 EXAMPLE PROGRAM |
1304 | |
1330 | |
1305 | The following program uses an I/O watcher to read data from STDIN, a timer |
1331 | The following program uses an I/O watcher to read data from STDIN, a timer |
1306 | to display a message once per second, and a condition variable to quit the |
1332 | to display a message once per second, and a condition variable to quit the |