| 1 | =head1 NAME | ||
| 2 | |||
| 3 | AnyEvent::Impl::EventLib - AnyEvent adaptor for Event::Lib | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | use AnyEvent; | ||
| 8 | use Event::Lib; | ||
| 9 | |||
| 10 | # this module gets loaded automatically as required | ||
| 11 | |||
| 12 | =head1 DESCRIPTION | ||
| 13 | |||
| 14 | This module provides transparent support for AnyEvent. You don't have to | ||
| 15 | do anything to make Event work with AnyEvent except by loading Event::Lib | ||
| 16 | before creating the first AnyEvent watcher. | ||
| 17 | |||
| 18 | The L<Event::Lib> module suffers from the same limitations and bugs | ||
| 19 | as libevent, most notably it kills already-installed watchers on a | ||
| 20 | file descriptor and it is unable to support fork. It has many other bugs | ||
| 21 | such as taking references to file handles and callbacks instead of making | ||
| 22 | a copy. Only Tk rivals it in its brokenness. | ||
| 23 | |||
| 24 | This adaptor module employs the same workaround around the watcher problem | ||
| 25 | as Tk and should therefore be avoided. (This was done for simplicity, one | ||
| 26 | could in theory work around the problems with lower overhead by managing | ||
| 27 | our own watchers). | ||
| 28 | |||
| 29 | Event::Lib also leaks file handles and memory and tends to just exit on | ||
| 30 | problems. | ||
| 31 | |||
| 32 | It also doesn't work around the Windows bug of not signalling TCP | ||
| 33 | connection failures. | ||
| 34 | |||
| 35 | Event::Lib does not support idle watchers. They could be emulated using | ||
| 36 | low-priority timers but as the priority range (and availability) is not | ||
| 37 | queryable nor guaranteed, and the default priority is likely the lowest | ||
| 38 | one, this module cannot use them. | ||
| 39 | |||
| 40 | Avoid Event::Lib if you can. | ||
| 41 | |||
| 42 | =cut | ||
| 43 | |||
| 44 | package AnyEvent::Impl::EventLib; | ||
| 45 | |||
| 46 | use AnyEvent (); BEGIN { AnyEvent::common_sense } | ||
| 47 | |||
| 48 | use Event::Lib; | ||
| 49 | |||
| 50 | # Event::Lib doesn't always take a reference to the callback, so closures | ||
| 51 | # cause memory corruption and segfaults. it also has an issue actually | ||
| 52 | # calling callbacks, so this exists as workaround. | ||
| 53 | sub ccb { | ||
| 54 | # Event:Lib accesses $_[0] after the callback, when it might be freed, | ||
| 55 | # so we keep it referenced until after the callback. This still accesses | ||
| 56 | # a freed scalar, but at least it'll not crash. | ||
| 57 | my $keep_it = $_[0]; | ||
| 58 | |||
| 59 | $_[2](); | ||
| 60 | } | ||
| 61 | |||
| 62 | my $ccb = \&ccb; | ||
| 63 | |||
| 64 | sub io { | ||
| 65 | my (undef, %arg) = @_; | ||
| 66 | |||
| 67 | # work around these bugs in Event::Lib: | ||
| 68 | # - adding a callback might destroy other callbacks | ||
| 69 | # - only one callback per fd/poll combination | ||
| 70 | my ($fh, $mode) = AnyEvent::_dupfh $arg{poll}, $arg{fh}, EV_READ, EV_WRITE; | ||
| 71 | |||
| 72 | # event_new errornously takes a reference to fh and cb instead of making a copy | ||
| 73 | # fortunately, going through %arg/_dupfh already makes a copy, so it happpens to work | ||
| 74 | my $w = event_new $fh, $mode | EV_PERSIST, $ccb, $arg{cb}; | ||
| 75 | event_add $w; | ||
| 76 | bless \\$w, __PACKAGE__ | ||
| 77 | } | ||
| 78 | |||
| 79 | sub timer { | ||
| 80 | my (undef, %arg) = @_; | ||
| 81 | |||
| 82 | my $ival = $arg{interval}; | ||
| 83 | my $cb = $arg{cb}; | ||
| 84 | |||
| 85 | my $w; $w = timer_new $ccb, | ||
| 86 | $ival | ||
| 87 | ? sub { event_add $w, $ival; &$cb } | ||
| 88 | : sub { undef $w ; &$cb }; | ||
| 89 | |||
| 90 | event_add $w, $arg{after} || 1e-10; # work around 0-bug in Event::Lib | ||
| 91 | |||
| 92 | bless \\$w, __PACKAGE__ | ||
| 93 | } | ||
| 94 | |||
| 95 | sub DESTROY { | ||
| 96 | local $@; | ||
| 97 | ${${$_[0]}}->remove; | ||
| 98 | } | ||
| 99 | |||
| 100 | sub signal { | ||
| 101 | my (undef, %arg) = @_; | ||
| 102 | |||
| 103 | my $w = signal_new AnyEvent::Base::sig2num $arg{signal}, $ccb, $arg{cb}; | ||
| 104 | event_add $w; | ||
| 105 | AnyEvent::Base::_sig_add; | ||
| 106 | bless \\$w, "AnyEvent::Impl::EventLib::signal" | ||
| 107 | } | ||
| 108 | |||
| 109 | sub AnyEvent::Impl::EventLib::signal::DESTROY { | ||
| 110 | AnyEvent::Base::_sig_del; | ||
| 111 | local $@; | ||
| 112 | ${${$_[0]}}->remove; | ||
| 113 | } | ||
| 114 | |||
| 115 | sub one_event { | ||
| 116 | event_one_loop; | ||
| 117 | } | ||
| 118 | |||
| 119 | sub loop { | ||
| 120 | event_mainloop; | ||
| 121 | } | ||
| 122 | |||
| 123 | 1; | ||
| 124 | |||
| 125 | =head1 SEE ALSO | ||
| 126 | |||
| 127 | L<AnyEvent>, L<Event::Lib>. | ||
| 128 | |||
| 129 | =head1 AUTHOR | ||
| 130 | |||
| 131 | Marc Lehmann <schmorp@schmorp.de> | ||
| 132 | http://home.schmorp.de/ | ||
| 133 | |||
| 134 | =cut | ||
| 135 |