| 1 | =head1 NAME | ||
| 2 | |||
| 3 | AnyEvent::Impl::IOAsync - AnyEvent adaptor for IO::Async | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | use AnyEvent; | ||
| 8 | |||
| 9 | use IO::ASync::Loop; | ||
| 10 | use AnyEvent::Impl::IOAsync; | ||
| 11 | |||
| 12 | my $loop = new IO::Async::Loop; | ||
| 13 | |||
| 14 | AnyEvent::Impl::IOAsync::set_loop $loop; | ||
| 15 | |||
| 16 | =head1 DESCRIPTION | ||
| 17 | |||
| 18 | This module provides support for IO::Async as AnyEvent backend. Due to | ||
| 19 | the rather sad state of L<IO::Async>, support is only available partially | ||
| 20 | (only timers and I/O watchers are supported, signals and child watchers | ||
| 21 | are emulated by AnyEvent itself (fighting with IO::Async, so you cannot | ||
| 22 | use both), idle watchers are being emulated, I/O watchers need to dup | ||
| 23 | their fh. | ||
| 24 | |||
| 25 | =head1 PROBLEMS WITH IO::Async | ||
| 26 | |||
| 27 | There have been multiple attempts at providing an AnyEvent interface to | ||
| 28 | IO::Async, and the effort is ongoing. Supporting IO::Async is hard. Here's | ||
| 29 | why: | ||
| 30 | |||
| 31 | =over 4 | ||
| 32 | |||
| 33 | =item IO::Async integration cannot be automatic - no default loop | ||
| 34 | |||
| 35 | IO::Async doesn't offer an interface suitable for independent usage of | ||
| 36 | event sources: there is no standard way to share the main event loop | ||
| 37 | between modules - modules have to somehow agree on how to do this. | ||
| 38 | |||
| 39 | For AnyEvent to work with IO::Async, the IO::Async main program has to | ||
| 40 | call C<AnyEvent::Impl::IOAsync::set_loop> with the C<IO::Async::Loop> | ||
| 41 | object that AnyEvent is to use, see the SYNOPSIS section for an example. | ||
| 42 | |||
| 43 | It is possible to get a copy of the loop by reading | ||
| 44 | C<$AnyEvent::Impl::IOAsync::LOOP>, so AnyEvent could be used as a central | ||
| 45 | place to store the default L<IO::Async::Loop> object, also for other | ||
| 46 | modules, if there is any need for that. | ||
| 47 | |||
| 48 | =item Broken child watchers | ||
| 49 | |||
| 50 | IO::Async is the only module that requires you to call a special function | ||
| 51 | before even forking your child program, while the AnyEvent API itself | ||
| 52 | (which works with other event loops) works as long as it has been | ||
| 53 | initialised (AnyEvent's own pure perl implementation doesn't even suffer | ||
| 54 | from these limitations and just works). | ||
| 55 | |||
| 56 | Worse, IO::Async does not let you install multiple child watchers, does | ||
| 57 | not let you watch for any child, and apparently an interface to unregister | ||
| 58 | child watchers has been forgotten as well. | ||
| 59 | |||
| 60 | As a result, AnyEvent::Impl::IOAsync has to fall back on it's own child | ||
| 61 | management, which makes it impossible to watch for child processes via | ||
| 62 | both AnyEvent and IO::Async. Hooking and Patching IO::Async has been | ||
| 63 | considered, but is considerable work. | ||
| 64 | |||
| 65 | =item No support for multiple watchers per event | ||
| 66 | |||
| 67 | In most (all? documentation?) cases you cannot have multiple watchers | ||
| 68 | for the same event (what's the point of having all these fancy notifier | ||
| 69 | classes when you cannot have multiple notifiers for the same event? That's | ||
| 70 | like only allowing one timer per second or so...). | ||
| 71 | |||
| 72 | This makes signal watchers almost useless (You could just hook them | ||
| 73 | yourself, you can't share any of them, as would make sense for | ||
| 74 | e.g. SIGTERM, SIGTSTP, SIGPWR, SIGUSR1 etc.). | ||
| 75 | |||
| 76 | As a result, AnyEvent falls back to it's own signal handling (it is | ||
| 77 | pointless to somehow share the IO::Async watcher, as it doesn't matter if | ||
| 78 | AnyEvent blocks the signal via IO::Async or directly, and AnyEvents signal | ||
| 79 | handling is race-free). | ||
| 80 | |||
| 81 | For I/O watchers, AnyEvent has to dup() every file handle, as IO::Async | ||
| 82 | fails to support the same or different file handles pointing to the same | ||
| 83 | fd (this is at least documented, but why not fix it instead?). | ||
| 84 | |||
| 85 | =back | ||
| 86 | |||
| 87 | Apart from these fatal flaws, there are a number of unpleasent properties | ||
| 88 | that just need some mentioning: | ||
| 89 | |||
| 90 | =over 4 | ||
| 91 | |||
| 92 | =item Confusing and misleading name | ||
| 93 | |||
| 94 | Another rather negative point about this module family is its name, | ||
| 95 | which is deeply confusing: Despite the "async" in the name, L<IO::Async> | ||
| 96 | only does I<synchronous> I/O, there is nothing "asynchronous" about it | ||
| 97 | whatsoever (when I first heard about it, I thought, "wow, a second async | ||
| 98 | I/O module, what does it do compared to L<IO::AIO>", and was somehow set | ||
| 99 | back when I learned that the only "async" aspect of it is the name). | ||
| 100 | |||
| 101 | =item Inconsistent, incomplete and convoluted API | ||
| 102 | |||
| 103 | Implementing AnyEvent's rather simple timers on top of IO::Async's timers | ||
| 104 | was a nightmare (try implementing a timer with configurable interval and | ||
| 105 | delay value...). | ||
| 106 | |||
| 107 | How to actually get I/O events in L<IO::Async::Handle> is undocumented: | ||
| 108 | read events are apparently automatic, for write events, you have to | ||
| 109 | explicitly request C<want_writeready>, and specifying C<want_readready> is | ||
| 110 | apparently a usage bug (it doesn't exist). All this must be deduced from | ||
| 111 | reading the sources. | ||
| 112 | |||
| 113 | You can't stop child watchers. Even reading the sources I found no way to | ||
| 114 | stop them. It must have been forgotten. | ||
| 115 | |||
| 116 | The method naming is chaotic: C<watch_child> creates a child watcher, | ||
| 117 | but C<watch_io> is an internal method; C<detach_signal> removes a signal | ||
| 118 | watcher, but C<detach_child> forks a subprocess and so on). | ||
| 119 | |||
| 120 | IO::Async has weird checks - passing in a callable reference is sometimes | ||
| 121 | forbidden (of course, this is checked on every invocation, not when the | ||
| 122 | callback is registered, so you have no idea where in your code you passed | ||
| 123 | it in), as the code checks explicitly for code references, disallowing | ||
| 124 | callable objects. | ||
| 125 | |||
| 126 | =item Unpleasant surprises on GNU/Linux | ||
| 127 | |||
| 128 | When you develop your program on FreeBSD and run it on GNU/Linux, you | ||
| 129 | might have unpleasant surprises, as IO::Async::Loop will by default use | ||
| 130 | L<IO::Async::Loop::Epoll>, which is incompatible with C<fork>, so your | ||
| 131 | network server will run into spurious and very hard to debug problems | ||
| 132 | under heavy load, as IO::Async forks a lot of processes, e.g. for DNS | ||
| 133 | resolution. It would be better if IO::Async would only load "safe" | ||
| 134 | backends by default (or fix the epoll backend to work in the presence of | ||
| 135 | fork, which admittedly is hard - EV does it for you, and also does not use | ||
| 136 | unsafe backends by default). | ||
| 137 | |||
| 138 | =item Exiting considered harmful | ||
| 139 | |||
| 140 | (in cleanup) Can't call method "parent" on an undefined value | ||
| 141 | at IO/Async/Loop.pm line 297 during global destruction. | ||
| 142 | |||
| 143 | IO::Async just hates global destruction. Calling C<exit> will easily give | ||
| 144 | you one such line per watcher. | ||
| 145 | |||
| 146 | The problem is that L<IO::Async::Loop> is itself not warning-free, but | ||
| 147 | actually enables warnings for itself. | ||
| 148 | |||
| 149 | (Ok, the real bug is of course perl's broken mark & sweep garbage | ||
| 150 | collector that corrupts data structures). | ||
| 151 | |||
| 152 | =back | ||
| 153 | |||
| 154 | On the positive side, performance with IO::Async is quite good even in my | ||
| 155 | very demanding eyes. | ||
| 156 | |||
| 157 | =cut | ||
| 158 | |||
| 159 | package AnyEvent::Impl::IOAsync; | ||
| 160 | |||
| 161 | use AnyEvent (); BEGIN { AnyEvent::common_sense } | ||
| 162 | |||
| 163 | use Time::HiRes; | ||
| 164 | use Scalar::Util; | ||
| 165 | |||
| 166 | BEGIN { | ||
| 167 | # IO::Async enables warnings but is itself not warning-free, we | ||
| 168 | # try our best to silence it. | ||
| 169 | require warnings; | ||
| 170 | local *warnings::import = sub { }; | ||
| 171 | require IO::Async::Loop; | ||
| 172 | } | ||
| 173 | |||
| 174 | use IO::Async::Handle; | ||
| 175 | |||
| 176 | our $VERSION = $AnyEvent::VERSION; | ||
| 177 | our $LOOP; | ||
| 178 | |||
| 179 | sub set_loop($) { | ||
| 180 | $LOOP = $_[0]; | ||
| 181 | #$LOOP->enable_childmanager; | ||
| 182 | } | ||
| 183 | |||
| 184 | sub timer { | ||
| 185 | my ($class, %arg) = @_; | ||
| 186 | |||
| 187 | # IO::Async has problems with overloaded objects | ||
| 188 | my $cb = $arg{cb}; | ||
| 189 | |||
| 190 | my $id; | ||
| 191 | |||
| 192 | if (my $ival = $arg{interval}) { | ||
| 193 | my $ival_cb; $ival_cb = sub { | ||
| 194 | $id = $LOOP->enqueue_timer (delay => $ival, code => $ival_cb); | ||
| 195 | &$cb; | ||
| 196 | }; | ||
| 197 | $id = $LOOP->enqueue_timer (delay => $arg{after}, code => $ival_cb); | ||
| 198 | |||
| 199 | # we have to weaken afterwards, but when enqueue dies, we have a memleak. | ||
| 200 | # still, we do anything for speed... | ||
| 201 | Scalar::Util::weaken $ival_cb; | ||
| 202 | |||
| 203 | } else { | ||
| 204 | $id = $LOOP->enqueue_timer (delay => $arg{after}, code => sub { &$cb }); | ||
| 205 | } | ||
| 206 | |||
| 207 | bless \\$id, "AnyEvent::Impl::IOAsync::timer" | ||
| 208 | } | ||
| 209 | |||
| 210 | sub AnyEvent::Impl::IOAsync::timer::DESTROY { | ||
| 211 | $LOOP->cancel_timer (${${$_[0]}}); | ||
| 212 | } | ||
| 213 | |||
| 214 | sub io { | ||
| 215 | my ($class, %arg) = @_; | ||
| 216 | |||
| 217 | # we need to dup(), as IO::Async only allows one watcher per | ||
| 218 | # underlying fd. | ||
| 219 | my ($fh) = AnyEvent::_dupfh $arg{poll}, $arg{fh}; | ||
| 220 | |||
| 221 | # there is no want_readready, but want_writeready is obligatory :/ | ||
| 222 | # I just love undocumented and illogical interfaces... | ||
| 223 | my $id = new IO::Async::Handle | ||
| 224 | $arg{poll} eq "r" | ||
| 225 | ? (read_handle => $fh, on_read_ready => $arg{cb}) | ||
| 226 | : (write_handle => $fh, on_write_ready => $arg{cb}, want_writeready => 1), | ||
| 227 | ; | ||
| 228 | $LOOP->add ($id); | ||
| 229 | |||
| 230 | bless \\$id, "AnyEvent::Impl::IOAsync::io" | ||
| 231 | } | ||
| 232 | |||
| 233 | sub AnyEvent::Impl::IOAsync::io::DESTROY { | ||
| 234 | $LOOP->remove (${${$_[0]}}); | ||
| 235 | } | ||
| 236 | |||
| 237 | #sub signal { | ||
| 238 | # my ($class, %arg) = @_; | ||
| 239 | # | ||
| 240 | # my $signal = $arg{signal}; | ||
| 241 | # | ||
| 242 | # my $id = $LOOP->attach_signal ($arg{signal}, $arg{cb}); | ||
| 243 | # bless [$signal, $id], "AnyEvent::Impl::IOAsync::signal"; | ||
| 244 | #} | ||
| 245 | # | ||
| 246 | #sub AnyEvent::Impl::IOAsync::signal::DESTROY { | ||
| 247 | # $LOOP->detach_signal (@{ $_[0] }); | ||
| 248 | #} | ||
| 249 | |||
| 250 | sub one_event { | ||
| 251 | $LOOP->loop_once; | ||
| 252 | } | ||
| 253 | |||
| 254 | sub loop { | ||
| 255 | $LOOP->loop_forever; | ||
| 256 | } | ||
| 257 | |||
| 258 | 1; | ||
| 259 | |||
| 260 | =head1 SEE ALSO | ||
| 261 | |||
| 262 | L<AnyEvent>, L<EV>. | ||
| 263 | |||
| 264 | =head1 AUTHOR | ||
| 265 | |||
| 266 | Marc Lehmann <schmorp@schmorp.de> | ||
| 267 | http://home.schmorp.de/ | ||
| 268 | |||
| 269 | =cut | ||
| 270 |