| 1 | =head1 NAME | ||
| 2 | |||
| 3 | AnyEvent::Impl::IOAsync - AnyEvent adaptor for IO::Async | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | use AnyEvent; | ||
| 8 | use IO::Async::Loop; | ||
| 9 | |||
| 10 | # optionally set another event loop | ||
| 11 | use AnyEvent::Impl::IOAsync; | ||
| 12 | my $loop = new IO::Async::Loop; | ||
| 13 | AnyEvent::Impl::IOAsync::set_loop $loop; | ||
| 14 | |||
| 15 | =head1 DESCRIPTION | ||
| 16 | |||
| 17 | This module provides support for IO::Async as AnyEvent backend. It supports | ||
| 18 | I/O, timers, signals and child process watchers. Idle watchers are emulated. | ||
| 19 | I/O watchers need to dup their fh because IO::Async only supports IO handles, | ||
| 20 | not plain file descriptors. | ||
| 21 | |||
| 22 | =head1 FUNCTIONS AND VARIABLES | ||
| 23 | |||
| 24 | The only user-servicible part in this module is the C<set_loop> function | ||
| 25 | and C<$LOOP> variable: | ||
| 26 | |||
| 27 | =over 4 | ||
| 28 | |||
| 29 | =item AnyEvent::Impl::IOAsync::set_loop $new_loop | ||
| 30 | |||
| 31 | Unfortunately, IO::Async has no concept of a default loop. Modules using | ||
| 32 | IO::Async must be told by their caller which loop to use, which makes it | ||
| 33 | impossible to transparently use IO::Async from a module. | ||
| 34 | |||
| 35 | This module is no exception. It creates a new IO::Async::Loop object when | ||
| 36 | it is loaded. This might not be the right loop object, though, and thus | ||
| 37 | you can replace it by a call to this function with the loop object of your | ||
| 38 | choice. | ||
| 39 | |||
| 40 | Note that switching loops while watchers are already initialised can have | ||
| 41 | unexpected effects, and is not supported unless you can live witht he | ||
| 42 | consequences. | ||
| 43 | |||
| 44 | =item $AnyEvent::Impl::IOAsync::LOOP | ||
| 45 | |||
| 46 | This variable always contains the IO::Async::Loop object used by this | ||
| 47 | AnyEvent backend. See above for more info. | ||
| 48 | |||
| 49 | Storing the "default" loop makes this module a possible arbiter for other | ||
| 50 | modules that want to use IO::Async transparently. It's advised to directly | ||
| 51 | refer to this variable each time you want to use it, without making a | ||
| 52 | local copy. | ||
| 53 | |||
| 54 | =back | ||
| 55 | |||
| 56 | =head1 PROBLEMS WITH IO::Async | ||
| 57 | |||
| 58 | This section had a long list of problems and shortcomings that made it | ||
| 59 | almost impossible to support L<IO::Async>. With version 0.33 of IO::Async, | ||
| 60 | however, most of these have been fixed, so L<IO::Async> can now be used as | ||
| 61 | easily as many other loops. | ||
| 62 | |||
| 63 | There are a few remaining problems that require emulation or workarounds: | ||
| 64 | |||
| 65 | =over 4 | ||
| 66 | |||
| 67 | =item No support for multiple watchers per event | ||
| 68 | |||
| 69 | In most (all? documentation?) cases you cannot have multiple watchers | ||
| 70 | for the same event (what's the point of having all these fancy notifier | ||
| 71 | classes when you cannot have multiple notifiers for the same event? That's | ||
| 72 | like only allowing one timer per second or so...). | ||
| 73 | |||
| 74 | For I/O watchers, AnyEvent has to dup() every file handle, as IO::Async | ||
| 75 | fails to support the same or different file handles pointing to the same | ||
| 76 | fd (the good thing is that it is documented, but why not fix it instead?). | ||
| 77 | |||
| 78 | =back | ||
| 79 | |||
| 80 | Apart from these fatal flaws, there are a number of unpleasent properties | ||
| 81 | that just need some mentioning: | ||
| 82 | |||
| 83 | =over 4 | ||
| 84 | |||
| 85 | =item Confusing and misleading names | ||
| 86 | |||
| 87 | Another rather negative point about this module family is its name, | ||
| 88 | which is deeply confusing: Despite the "async" in the name, L<IO::Async> | ||
| 89 | only does I<synchronous> I/O, there is nothing "asynchronous" about it | ||
| 90 | whatsoever (when I first heard about it, I thought, "wow, a second async | ||
| 91 | I/O module, what does it do compared to L<IO::AIO>", and was somehow set | ||
| 92 | back when I learned that the only "async" aspect of it is the name). | ||
| 93 | |||
| 94 | =item Inconsistent, incomplete and convoluted API | ||
| 95 | |||
| 96 | Implementing AnyEvent's rather simple timers on top of IO::Async's timers | ||
| 97 | was a nightmare (try implementing a timer with configurable interval and | ||
| 98 | delay value...). | ||
| 99 | |||
| 100 | The method naming is chaotic: C<watch_child> creates a child watcher, | ||
| 101 | but C<watch_io> is an internal method; C<detach_signal> removes a signal | ||
| 102 | watcher, but C<detach_child> forks a subprocess and so on). | ||
| 103 | |||
| 104 | =item Unpleasant surprises on GNU/Linux | ||
| 105 | |||
| 106 | When you develop your program on FreeBSD and run it on GNU/Linux, you | ||
| 107 | might have unpleasant surprises, as IO::Async::Loop will by default use | ||
| 108 | L<IO::Async::Loop::Epoll>, which is incompatible with C<fork>, so your | ||
| 109 | network server will run into spurious and very hard to debug problems | ||
| 110 | under heavy load, as IO::Async forks a lot of processes, e.g. for DNS | ||
| 111 | resolution. It would be better if IO::Async would only load "safe" | ||
| 112 | backends by default (or fix the epoll backend to work in the presence of | ||
| 113 | fork, which admittedly is hard - EV does it for you, and also does not use | ||
| 114 | unsafe backends by default). | ||
| 115 | |||
| 116 | =back | ||
| 117 | |||
| 118 | On the positive side, performance with IO::Async is quite good even in my | ||
| 119 | very demanding eyes. | ||
| 120 | |||
| 121 | =cut | ||
| 122 | |||
| 123 | package AnyEvent::Impl::IOAsync; | ||
| 124 | |||
| 125 | use AnyEvent (); BEGIN { AnyEvent::common_sense } | ||
| 126 | |||
| 127 | use Time::HiRes (); | ||
| 128 | use Scalar::Util (); | ||
| 129 | |||
| 130 | use IO::Async::Loop 0.33; | ||
| 131 | |||
| 132 | our $LOOP = new IO::Async::Loop; | ||
| 133 | |||
| 134 | sub set_loop($) { | ||
| 135 | $LOOP = $_[0]; | ||
| 136 | } | ||
| 137 | |||
| 138 | sub timer { | ||
| 139 | my ($class, %arg) = @_; | ||
| 140 | |||
| 141 | my $cb = $arg{cb}; | ||
| 142 | |||
| 143 | my $id; | ||
| 144 | |||
| 145 | if (my $ival = $arg{interval}) { | ||
| 146 | my $ival_cb; $ival_cb = sub { | ||
| 147 | $id = $LOOP->enqueue_timer (delay => $ival, code => $ival_cb); | ||
| 148 | &$cb; | ||
| 149 | }; | ||
| 150 | $id = $LOOP->enqueue_timer (delay => $arg{after}, code => $ival_cb); | ||
| 151 | |||
| 152 | # we have to weaken afterwards, but when enqueue dies, we have a memleak. | ||
| 153 | # still, we do anything for speed... | ||
| 154 | Scalar::Util::weaken $ival_cb; | ||
| 155 | |||
| 156 | } else { | ||
| 157 | # IO::Async has problems with overloaded objects | ||
| 158 | $id = $LOOP->enqueue_timer (delay => $arg{after}, code => sub { | ||
| 159 | undef $id; # IO::Async <= 0.43 bug workaround | ||
| 160 | &$cb; | ||
| 161 | }); | ||
| 162 | } | ||
| 163 | |||
| 164 | bless \\$id, "AnyEvent::Impl::IOAsync::timer" | ||
| 165 | } | ||
| 166 | |||
| 167 | sub AnyEvent::Impl::IOAsync::timer::DESTROY { | ||
| 168 | # Need to be well-behaved during global destruction | ||
| 169 | $LOOP->cancel_timer (${${$_[0]}}) | ||
| 170 | if defined ${${$_[0]}}; # IO::Async <= 0.43 bug workaround | ||
| 171 | } | ||
| 172 | |||
| 173 | sub io { | ||
| 174 | my ($class, %arg) = @_; | ||
| 175 | |||
| 176 | # Ensure we have a real IO handle, and not just a UNIX fd integer | ||
| 177 | my ($fh) = AnyEvent::_dupfh $arg{poll}, $arg{fh}; | ||
| 178 | |||
| 179 | my $event = $arg{poll} eq "r" ? "on_read_ready" : "on_write_ready"; | ||
| 180 | |||
| 181 | $LOOP->watch_io ( | ||
| 182 | handle => $fh, | ||
| 183 | $event => $arg{cb}, | ||
| 184 | ); | ||
| 185 | |||
| 186 | bless [$fh, $event], "AnyEvent::Impl::IOAsync::io" | ||
| 187 | } | ||
| 188 | |||
| 189 | sub AnyEvent::Impl::IOAsync::io::DESTROY { | ||
| 190 | $LOOP->unwatch_io ( | ||
| 191 | handle => $_[0][0], | ||
| 192 | $_[0][1] => 1, | ||
| 193 | ); | ||
| 194 | } | ||
| 195 | |||
| 196 | sub signal { | ||
| 197 | my ($class, %arg) = @_; | ||
| 198 | |||
| 199 | my $signal = $arg{signal}; | ||
| 200 | |||
| 201 | my $id = $LOOP->attach_signal ($arg{signal}, $arg{cb}); | ||
| 202 | bless [$signal, $id], "AnyEvent::Impl::IOAsync::signal" | ||
| 203 | } | ||
| 204 | |||
| 205 | sub AnyEvent::Impl::IOAsync::signal::DESTROY { | ||
| 206 | $LOOP->detach_signal (@{ $_[0] }); | ||
| 207 | } | ||
| 208 | |||
| 209 | our %pid_cb; | ||
| 210 | |||
| 211 | sub child { | ||
| 212 | my ($class, %arg) = @_; | ||
| 213 | |||
| 214 | my $pid = $arg{pid}; | ||
| 215 | |||
| 216 | $LOOP->watch_child ($pid, $arg{cb}); | ||
| 217 | bless [$pid], "AnyEvent::Impl::IOAsync::child" | ||
| 218 | } | ||
| 219 | |||
| 220 | sub child { | ||
| 221 | my ($class, %arg) = @_; | ||
| 222 | |||
| 223 | my $pid = $arg{pid}; | ||
| 224 | my $cb = $arg{cb}; | ||
| 225 | |||
| 226 | unless (%{ $pid_cb{$pid} }) { | ||
| 227 | $LOOP->watch_child ($pid, sub { | ||
| 228 | $_->($_[0], $_[1]) | ||
| 229 | for values %{ $pid_cb{$pid} }; | ||
| 230 | }); | ||
| 231 | } | ||
| 232 | |||
| 233 | $pid_cb{$pid}{$cb+0} = $cb; | ||
| 234 | |||
| 235 | bless [$pid, $cb+0], "AnyEvent::Impl::IOAsync::child" | ||
| 236 | } | ||
| 237 | |||
| 238 | sub AnyEvent::Impl::IOAsync::child::DESTROY { | ||
| 239 | my ($pid, $icb) = @{ $_[0] }; | ||
| 240 | |||
| 241 | delete $pid_cb{$pid}{$icb}; | ||
| 242 | |||
| 243 | unless (%{ $pid_cb{$pid} }) { | ||
| 244 | delete $pid_cb{$pid}; | ||
| 245 | $LOOP->unwatch_child ($pid); | ||
| 246 | } | ||
| 247 | } | ||
| 248 | |||
| 249 | #sub loop { | ||
| 250 | # $LOOP->loop_forever; | ||
| 251 | #} | ||
| 252 | |||
| 253 | sub _poll { | ||
| 254 | $LOOP->loop_once; | ||
| 255 | } | ||
| 256 | |||
| 257 | sub AnyEvent::CondVar::Base::_wait { | ||
| 258 | $LOOP->loop_once until exists $_[0]{_ae_sent}; | ||
| 259 | } | ||
| 260 | |||
| 261 | =head1 SEE ALSO | ||
| 262 | |||
| 263 | L<AnyEvent>, L<IO::Async>. | ||
| 264 | |||
| 265 | =head1 AUTHOR | ||
| 266 | |||
| 267 | Marc Lehmann <schmorp@schmorp.de> | ||
| 268 | http://anyevent.schmorp.de | ||
| 269 | |||
| 270 | Paul Evans <leonerd@leonerd.org.uk> | ||
| 271 | Rewrote the backend for IO::Async version 0.33. | ||
| 272 | |||
| 273 | =cut | ||
| 274 | |||
| 275 | 1 | ||
| 276 |