| 1 | =head1 NAME | ||
| 2 | |||
| 3 | AnyEvent::Impl::Event - AnyEvent adaptor for Event | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | use AnyEvent; | ||
| 8 | use Event; | ||
| 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 before | ||
| 16 | creating the first AnyEvent watcher. | ||
| 17 | |||
| 18 | The event module is reasonably efficient and generally works correctly | ||
| 19 | even with many watchers, except that it's signal handling is inherently | ||
| 20 | racy and requires the wake-up-frequently workaround. | ||
| 21 | |||
| 22 | =cut | ||
| 23 | |||
| 24 | package AnyEvent::Impl::Event; | ||
| 25 | |||
| 26 | use AnyEvent (); BEGIN { AnyEvent::common_sense } | ||
| 27 | use Event qw(unloop); # we have to import something to make Event use Time::HiRes | ||
| 28 | |||
| 29 | sub io { | ||
| 30 | my (undef, %arg) = @_; | ||
| 31 | $arg{fd} = delete $arg{fh}; | ||
| 32 | $arg{poll} .= "e" if AnyEvent::WIN32; # work around windows connect bug | ||
| 33 | my $cb = $arg{cb}; $arg{cb} = sub { &$cb }; # event doesn't like callable objects | ||
| 34 | bless \(Event->io (%arg)), __PACKAGE__ | ||
| 35 | } | ||
| 36 | |||
| 37 | sub timer { | ||
| 38 | my (undef, %arg) = @_; | ||
| 39 | $arg{after} = 0 if $arg{after} < 0; | ||
| 40 | my $cb = $arg{cb}; $arg{cb} = sub { &$cb }; # event doesn't like callable objects | ||
| 41 | bless \Event->timer (%arg, repeat => $arg{interval}), __PACKAGE__ | ||
| 42 | } | ||
| 43 | |||
| 44 | sub idle { | ||
| 45 | my (undef, %arg) = @_; | ||
| 46 | my $cb = $arg{cb}; $arg{cb} = sub { &$cb }; # event doesn't like callable objects | ||
| 47 | bless \Event->idle (repeat => 1, min => 0, %arg), __PACKAGE__ | ||
| 48 | } | ||
| 49 | |||
| 50 | sub DESTROY { | ||
| 51 | ${$_[0]}->cancel; | ||
| 52 | } | ||
| 53 | |||
| 54 | sub signal { | ||
| 55 | my (undef, %arg) = @_; | ||
| 56 | |||
| 57 | my $cb = $arg{cb}; | ||
| 58 | my $w = Event->signal ( | ||
| 59 | signal => AnyEvent::Base::sig2name $arg{signal}, | ||
| 60 | cb => sub { &$cb }, # event doesn't like callable objects | ||
| 61 | ); | ||
| 62 | |||
| 63 | AnyEvent::Base::_sig_add; | ||
| 64 | bless \$w, "AnyEvent::Impl::Event::signal" | ||
| 65 | } | ||
| 66 | |||
| 67 | sub AnyEvent::Impl::Event::signal::DESTROY { | ||
| 68 | AnyEvent::Base::_sig_del; | ||
| 69 | ${$_[0]}->cancel; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub one_event { | ||
| 73 | Event::one_event; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub loop { | ||
| 77 | Event::loop; | ||
| 78 | } | ||
| 79 | |||
| 80 | 1; | ||
| 81 | |||
| 82 | =head1 SEE ALSO | ||
| 83 | |||
| 84 | L<AnyEvent>, L<Event>. | ||
| 85 | |||
| 86 | =head1 AUTHOR | ||
| 87 | |||
| 88 | Marc Lehmann <schmorp@schmorp.de> | ||
| 89 | http://home.schmorp.de/ | ||
| 90 | |||
| 91 | =cut | ||
| 92 |