| 1 | =head1 NAME | ||
| 2 | |||
| 3 | AnyEvent::Impl::UV - AnyEvent adaptor for UV | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | use AnyEvent; | ||
| 8 | use UV; | ||
| 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 UV work with AnyEvent except by loading UV before | ||
| 16 | creating the first AnyEvent watcher. | ||
| 17 | |||
| 18 | =cut | ||
| 19 | |||
| 20 | package AnyEvent::Impl::UV; | ||
| 21 | |||
| 22 | use AnyEvent (); BEGIN { AnyEvent::common_sense } | ||
| 23 | use UV 0.24; | ||
| 24 | use Scalar::Util qw(weaken); | ||
| 25 | |||
| 26 | sub warnlog { | ||
| 27 | my $err = UV::last_error; | ||
| 28 | |||
| 29 | AnyEvent::log warn => "returned $_[0]: " | ||
| 30 | . UV::err_name ($err) . "($err): " | ||
| 31 | . UV::strerror ($err); | ||
| 32 | |||
| 33 | @_ | ||
| 34 | } | ||
| 35 | |||
| 36 | # https://github.com/joyent/libuv/issues/680 | ||
| 37 | # https://github.com/joyent/libuv/blob/dc1ea27c736f0d21c7160c790bcd1b113d20abd9/include/uv.h#L1277 | ||
| 38 | my %io_watchers; | ||
| 39 | |||
| 40 | sub io_watcher_cb { | ||
| 41 | my $slaves = shift; | ||
| 42 | my (undef, $events) = @_; | ||
| 43 | return unless defined $slaves; | ||
| 44 | |||
| 45 | foreach my $entry (keys %$slaves) { | ||
| 46 | my $slave = $slaves->{$entry}; | ||
| 47 | $slave->{cb}(@_) if $slave->{mode} & $events; | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | sub AnyEvent::Impl::UV::io_slave::new { | ||
| 52 | bless { parent => $_[1] }, $_[0] | ||
| 53 | } | ||
| 54 | |||
| 55 | sub AnyEvent::Impl::UV::io_slave::DESTROY { | ||
| 56 | my $self = $_[0]; | ||
| 57 | my $master = $self->{parent}; | ||
| 58 | |||
| 59 | delete $master->{slaves}{$self}; | ||
| 60 | if (keys %{$master->{slaves}} == 0) { | ||
| 61 | if (defined $master->{w}) { | ||
| 62 | my $rc = UV::poll_stop $master->{w}; | ||
| 63 | warnlog $rc if $rc; | ||
| 64 | } | ||
| 65 | delete $io_watchers{$master->{fd}}; | ||
| 66 | return; | ||
| 67 | } | ||
| 68 | |||
| 69 | my $mode = 0; | ||
| 70 | foreach my $entry (keys %{$master->{slaves}}) { | ||
| 71 | $mode |= $master->{slaves}{$entry}{mode}; | ||
| 72 | } | ||
| 73 | |||
| 74 | if ($master->{mode} != $mode) { | ||
| 75 | $master->{mode} = $mode; | ||
| 76 | my $rc = UV::poll_start $master->{w}, $master->{mode}, sub { | ||
| 77 | io_watcher_cb $master->{slaves}, @_; | ||
| 78 | }; | ||
| 79 | warnlog $rc if $rc; | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | sub io { | ||
| 84 | my ($class, %arg) = @_; | ||
| 85 | my $fd = fileno $arg{fh}; | ||
| 86 | defined $fd or $fd = $arg{fh}; | ||
| 87 | |||
| 88 | my $master = $io_watchers{$fd} ||= { fd => $fd }; | ||
| 89 | |||
| 90 | unless (defined $master->{w}) { | ||
| 91 | $master->{w} = UV::poll_init $fd; | ||
| 92 | return warnlog $master->{w} unless defined $master->{w}; | ||
| 93 | $master->{slaves} = {}; | ||
| 94 | } | ||
| 95 | |||
| 96 | my $slave = AnyEvent::Impl::UV::io_slave->new ($master); | ||
| 97 | weaken ($master->{slaves}->{$slave} = $slave); | ||
| 98 | |||
| 99 | $slave->{mode} = $arg{poll} eq "r" ? UV::READABLE : UV::WRITABLE; | ||
| 100 | $master->{mode} = 0 unless defined $master->{mode}; | ||
| 101 | $slave->{cb} = $arg{cb}; | ||
| 102 | |||
| 103 | unless ($master->{mode} & $slave->{mode}) { | ||
| 104 | $master->{mode} |= $slave->{mode}; | ||
| 105 | my $rc = UV::poll_start $master->{w}, $master->{mode}, sub { | ||
| 106 | io_watcher_cb $master->{slaves}, @_; | ||
| 107 | }; | ||
| 108 | warnlog $rc if $rc; | ||
| 109 | } | ||
| 110 | |||
| 111 | $slave | ||
| 112 | } | ||
| 113 | |||
| 114 | sub AnyEvent::Impl::UV::handle::new { | ||
| 115 | my ($class, $w, $start, $stop, @args) = @_; | ||
| 116 | return warnlog $w unless defined $w; | ||
| 117 | |||
| 118 | my $rc = $start->($w, @args); | ||
| 119 | warnlog $rc if $rc; | ||
| 120 | |||
| 121 | bless { w => $w, stop => $stop }, $class | ||
| 122 | } | ||
| 123 | |||
| 124 | sub AnyEvent::Impl::UV::handle::DESTROY { | ||
| 125 | my $h = $_[0]; | ||
| 126 | return unless $h->{w}; | ||
| 127 | my $rc = $h->{stop}($h->{w}); | ||
| 128 | warnlog $rc if $rc; | ||
| 129 | UV::close $h->{w}; | ||
| 130 | } | ||
| 131 | |||
| 132 | sub idle { | ||
| 133 | my ($class, %arg) = @_; | ||
| 134 | |||
| 135 | AnyEvent::Impl::UV::handle->new ( | ||
| 136 | UV::timer_init, | ||
| 137 | \&UV::idle_start, | ||
| 138 | \&UV::idle_stop, | ||
| 139 | $arg{cb} | ||
| 140 | ); | ||
| 141 | } | ||
| 142 | |||
| 143 | sub timer { | ||
| 144 | my ($class, %arg) = @_; | ||
| 145 | |||
| 146 | AnyEvent::Impl::UV::handle->new ( | ||
| 147 | UV::timer_init, | ||
| 148 | \&UV::timer_start, | ||
| 149 | \&UV::timer_stop, | ||
| 150 | $arg{after} * 1000, $arg{interval} * 1000, $arg{cb} | ||
| 151 | ); | ||
| 152 | } | ||
| 153 | |||
| 154 | sub now { UV::now } | ||
| 155 | |||
| 156 | sub _poll { | ||
| 157 | UV::run UV::RUN_ONCE; | ||
| 158 | } | ||
| 159 | |||
| 160 | sub AnyEvent::CondVar::Base::_wait { | ||
| 161 | UV::run UV::RUN_NOWAIT until exists $_[0]{_ae_sent}; | ||
| 162 | } | ||
| 163 | |||
| 164 | =head1 SEE ALSO | ||
| 165 | |||
| 166 | L<AnyEvent>, L<UV>. | ||
| 167 | |||
| 168 | =head1 AUTHOR | ||
| 169 | |||
| 170 | Mike Lowell <mikedotlowell@gmail.com> | ||
| 171 | |||
| 172 | =cut | ||
| 173 | |||
| 174 | 1 | ||
| 175 |