| 1 | =head1 NAME | ||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||
| 3 | AnyEvent::Loop - AnyEvent's Pure-Perl event loop | ||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||
| 5 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||
| 7 | use AnyEvent; | ||||||||||||||||||||||||||||||||||||
| 8 | # use AnyEvent::Loop; | ||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||
| 10 | # this module gets loaded automatically when no other loop can be found | ||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||
| 12 | # Explicit use: | ||||||||||||||||||||||||||||||||||||
| 13 | use AnyEvent::Loop; | ||||||||||||||||||||||||||||||||||||
| 14 | use AnyEvent; | ||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||
| 16 | ... | ||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||
| 18 | AnyEvent::Loop::run; # run the event loop | ||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||
| 20 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||
| 22 | This module provides an event loop for AnyEvent in case no other event | ||||||||||||||||||||||||||||||||||||
| 23 | loop could be found or loaded. You don't have to do anything to make it | ||||||||||||||||||||||||||||||||||||
| 24 | work with AnyEvent except by possibly loading it before creating the first | ||||||||||||||||||||||||||||||||||||
| 25 | AnyEvent watcher. | ||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||
| 27 | This module is I<not> some loop abstracion used by AnyEvent, but just | ||||||||||||||||||||||||||||||||||||
| 28 | another event loop like EV or Glib, just written in pure perl and | ||||||||||||||||||||||||||||||||||||
| 29 | delivered with AnyEvent, so AnyEvent always works, even in the absence of | ||||||||||||||||||||||||||||||||||||
| 30 | any other backend. | ||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||
| 32 | If you want to use this module instead of autoloading a potentially better | ||||||||||||||||||||||||||||||||||||
| 33 | event loop you can simply load it (and no other event loops) before | ||||||||||||||||||||||||||||||||||||
| 34 | creating the first watcher. | ||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||
| 36 | As for performance, this module is on par with (and usually faster than) | ||||||||||||||||||||||||||||||||||||
| 37 | most select/poll-based C event modules such as Event or Glib (it does not | ||||||||||||||||||||||||||||||||||||
| 38 | even come close to EV, though), with respect to I/O watchers. Timers are | ||||||||||||||||||||||||||||||||||||
| 39 | handled less optimally, but for many common tasks, it is still on par with | ||||||||||||||||||||||||||||||||||||
| 40 | event loops written in C. | ||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||
| 42 | This event loop has been optimised for the following use cases: | ||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||
| 44 | =over 4 | ||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||
| 46 | =item monotonic clock is available | ||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||
| 48 | This module will use the POSIX monotonic clock option (if it can be | ||||||||||||||||||||||||||||||||||||
| 49 | detected at runtime) or the POSIX C<times> function (if the resolution | ||||||||||||||||||||||||||||||||||||
| 50 | is at least 100Hz), in which case it will not suffer adversely from time | ||||||||||||||||||||||||||||||||||||
| 51 | jumps. | ||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||
| 53 | If no monotonic clock is available, this module will not attempt to | ||||||||||||||||||||||||||||||||||||
| 54 | correct for time jumps in any way. | ||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||
| 56 | The clock chosen will be reported if the environment variable | ||||||||||||||||||||||||||||||||||||
| 57 | C<$PERL_ANYEVENT_VERBOSE> is set to 8 or higher. | ||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||
| 59 | =item any number of watchers on one fd | ||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||
| 61 | Supporting a large number of watchers per fd is purely a dirty benchmark | ||||||||||||||||||||||||||||||||||||
| 62 | optimisation not relevant in practise. The more common case of having one | ||||||||||||||||||||||||||||||||||||
| 63 | watcher per fd/poll combo is special-cased, however, and therefore fast, | ||||||||||||||||||||||||||||||||||||
| 64 | too. | ||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||
| 66 | =item relatively few active fds per C<select> call | ||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||
| 68 | This module expects that only a tiny amount of fds is active at any one | ||||||||||||||||||||||||||||||||||||
| 69 | time. This is relatively typical of larger servers (but not the case where | ||||||||||||||||||||||||||||||||||||
| 70 | C<select> traditionally is fast), at the expense of the "dense activity | ||||||||||||||||||||||||||||||||||||
| 71 | case" where most of the fds are active (which suits C<select>). | ||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||
| 73 | The optimal implementation of the "dense" case is not much faster, though, | ||||||||||||||||||||||||||||||||||||
| 74 | so the module should behave very well in most cases, subject to the bad | ||||||||||||||||||||||||||||||||||||
| 75 | scalability of C<select> in the presence of a large number of inactive | ||||||||||||||||||||||||||||||||||||
| 76 | file descriptors. | ||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||
| 78 | =item lots of timer changes/iteration, or none at all | ||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||
| 80 | This module sorts the timer list using perl's C<sort>, even though a total | ||||||||||||||||||||||||||||||||||||
| 81 | ordering is not required for timers internally. | ||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||
| 83 | This sorting is expensive, but means sorting can be avoided unless the | ||||||||||||||||||||||||||||||||||||
| 84 | timer list has changed in a way that requires a new sort. | ||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||
| 86 | This means that adding lots of timers is very efficient, as well as not | ||||||||||||||||||||||||||||||||||||
| 87 | changing the timers. Advancing timers (e.g. recreating a timeout watcher | ||||||||||||||||||||||||||||||||||||
| 88 | on activity) is also relatively efficient, for example, if you have a | ||||||||||||||||||||||||||||||||||||
| 89 | large number of timeout watchers that time out after 10 seconds, then the | ||||||||||||||||||||||||||||||||||||
| 90 | timer list will be sorted only once every 10 seconds. | ||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||
| 92 | This should not have much of an impact unless you have hundreds or | ||||||||||||||||||||||||||||||||||||
| 93 | thousands of timers, though, or your timers have very small timeouts. | ||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||
| 95 | =back | ||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||
| 97 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||
| 99 | The only user-visible functions provided by this module loop related - | ||||||||||||||||||||||||||||||||||||
| 100 | watchers are created via the normal AnyEvent mechanisms. | ||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||
| 102 | =over 4 | ||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||
| 104 | =item AnyEvent::Loop::run | ||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||
| 106 | Run the event loop, usually the last thing done in the main program when | ||||||||||||||||||||||||||||||||||||
| 107 | you want to use the pure-perl backend. | ||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||
| 109 | =item AnyEvent::Loop::one_event | ||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||
| 111 | Blocks until at least one new event has been received by the operating | ||||||||||||||||||||||||||||||||||||
| 112 | system, whether or not it was AnyEvent-related. | ||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||
| 114 | =back | ||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||
| 116 | =cut | ||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||
| 118 | package AnyEvent::Loop; | ||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||
| 120 | 3 | ▶ | use Scalar::Util qw(weaken); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 121 | 3 | ▶ | use List::Util (); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||
| 123 | 3 | ▶ | use AnyEvent (); BEGIN { AnyEvent::common_sense } | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 124 | 3 | ▶ | use AnyEvent::Util (); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||
| 126 | 3 | our $VERSION = $AnyEvent::VERSION; | |||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||
| 128 | 3 | our ($NOW, $MNOW); | |||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||
| 130 | sub MAXWAIT() { 3600 } # never sleep for longer than this many seconds | ||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||
| 132 | 3 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=8 SCAR=30.9
| |||||||||||||||||||||||||||||||||||||
| 133 | 3 | local $SIG{__DIE__}; # protect us against the many broken __DIE__ handlers out there | |||||||||||||||||||||||||||||||||||
| 134 | 3 | ▶ | my $time_hires = eval "use Time::HiRes (); 1"; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 135 | 3 | ▶ | my $clk_tck = eval "use POSIX (); POSIX::sysconf (POSIX::_SC_CLK_TCK ())"; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 136 | 3 | my $round; # actual granularity | |||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||
| 138 | 3 | ▶ | if ($time_hires && eval "&Time::HiRes::clock_gettime (Time::HiRes::CLOCK_MONOTONIC ())") { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition33%1/3
$time_hires and eval "&Time::HiRes::clock_gettime (Time::HiRes::CLOCK_MONOTONIC ())"
MC/DC0%0/2
$time_hires and eval "&Time::HiRes::clock_gettime (Time::HiRes::CLOCK_MONOTONIC ())"
$time_hires
eval "&Time::HiRes::clock_gettime (Time::HiRes::CLOCK_MONOTONIC ())"
Truth table33%1/3
A and B
A= $time_hiresB= eval "&Time::HiRes::clock_gettime (Time::HiRes::CLOCK_MONOTONIC ())"
| |||||||||||||||||||||||||||||||||||||
| 139 | 3 | AE::log 8 => "Using CLOCK_MONOTONIC as timebase."; | |||||||||||||||||||||||||||||||||||
| 140 | 3 | ▶ | *_update_clock = sub { | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 141 | 3 | $NOW = &Time::HiRes::time; | |||||||||||||||||||||||||||||||||||
| 142 | 3 | $MNOW = Time::HiRes::clock_gettime (&Time::HiRes::CLOCK_MONOTONIC); | |||||||||||||||||||||||||||||||||||
| 143 | 3 | }; | |||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||
| 145 | 0 | ▶ | } elsif (100 <= $clk_tck && $clk_tck <= 1000000 && eval { (POSIX::times ())[0] != -1 }) { # -1 is also a valid return value :/ | ||||||||||||||||||||||||||||||||||
|
Condition0%0/6
100 <= $clk_tck and $clk_tck <= 1000000 and eval {
do {
(POSIX::times())[0] != -1
}
}
100 <= $clk_tck and $clk_tck <= 1000000
MC/DC0%0/3
100 <= $clk_tck and $clk_tck <= 1000000 and eval {
do {
(POSIX::times())[0] != -1
}
}
100 <= $clk_tck
$clk_tck <= 1000000
eval {
do {
(POSIX::times())[0] != -1
}
}
Truth table0%0/4
A and B and C
A= 100 <= $clk_tckB= $clk_tck <= 1000000C= eval {
do {
(POSIX::times())[0] != -1
}
}
| |||||||||||||||||||||||||||||||||||||
| 146 | 0 | AE::log 8 => "Using POSIX::times (monotonic) as timebase."; | |||||||||||||||||||||||||||||||||||
| 147 | 0 | my $HZ1 = 1 / $clk_tck; | |||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||
| 149 | 0 | my $last = (POSIX::times ())[0]; | |||||||||||||||||||||||||||||||||||
| 150 | 0 | my $next; | |||||||||||||||||||||||||||||||||||
| 151 | 0 | ▶ | *_update_clock = sub { | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||
| 152 | 0 | $NOW = time; # d'oh | |||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||
| 154 | 0 | $next = (POSIX::times ())[0]; | |||||||||||||||||||||||||||||||||||
| 155 | # we assume 32 bit signed on wrap but 64 bit will never wrap | ||||||||||||||||||||||||||||||||||||
| 156 | 0 | ▶ | $last -= 4294967296 if $last > $next; # 0x100000000, but perl has problems with big hex constants | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 157 | 0 | $MNOW += ($next - $last) * $HZ1; | |||||||||||||||||||||||||||||||||||
| 158 | 0 | $last = $next; | |||||||||||||||||||||||||||||||||||
| 159 | 0 | }; | |||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||
| 161 | 0 | $round = $HZ1; | |||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||
| 163 | } elsif (eval "use Time::HiRes (); 1") { | ||||||||||||||||||||||||||||||||||||
| 164 | 0 | AE::log 8 => "Using Time::HiRes::time (non-monotonic) clock as timebase."; | |||||||||||||||||||||||||||||||||||
| 165 | 0 | ▶ | *_update_clock = sub { | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||
| 166 | 0 | $NOW = $MNOW = &Time::HiRes::time; | |||||||||||||||||||||||||||||||||||
| 167 | 0 | }; | |||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||
| 169 | } else { | ||||||||||||||||||||||||||||||||||||
| 170 | 0 | AE::log fatal => "Unable to find sub-second time source (is this really perl 5.8.0 or later?)"; | |||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||
| 173 | 3 | ▶ | $round = 0.001 if $round < 0.001; # 1ms is enough for us | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 174 | 3 | $round -= $round * 1e-2; # 0.1 => 0.099 | |||||||||||||||||||||||||||||||||||
| 175 | 3 | eval "sub ROUNDUP() { $round }"; | |||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||
| 178 | 3 | _update_clock; | |||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||
| 180 | # rely on AnyEvent:Base::time to provide time | ||||||||||||||||||||||||||||||||||||
| 181 | 0 | ▶ | sub now () { $NOW } | ||||||||||||||||||||||||||||||||||
|
Subroutine
now: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 182 | 0 | ▶ | sub now_update() { _update_clock } | ||||||||||||||||||||||||||||||||||
|
Subroutine
now_update: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||
| 184 | # fds[0] is for read, fds[1] is for write watchers | ||||||||||||||||||||||||||||||||||||
| 185 | # fds[poll][V] is the bitmask for select | ||||||||||||||||||||||||||||||||||||
| 186 | # fds[poll][W][fd] contains a list of i/o watchers | ||||||||||||||||||||||||||||||||||||
| 187 | # an I/O watcher is a blessed arrayref containing [fh, poll(0/1), callback, queue-index] | ||||||||||||||||||||||||||||||||||||
| 188 | # the queue-index is simply the index in the [W] array, which is only used to improve | ||||||||||||||||||||||||||||||||||||
| 189 | # benchmark results in the synthetic "many watchers on one fd" benchmark. | ||||||||||||||||||||||||||||||||||||
| 190 | 3 | my @fds = ([], []); | |||||||||||||||||||||||||||||||||||
| 191 | sub V() { 0 } | ||||||||||||||||||||||||||||||||||||
| 192 | sub W() { 1 } | ||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||
| 194 | 3 | my $need_sort = 1e300; # when to re-sort timer list | |||||||||||||||||||||||||||||||||||
| 195 | 3 | my @timer; # list of [ abs-timeout, Timer::[callback] ] | |||||||||||||||||||||||||||||||||||
| 196 | my @idle; # list of idle callbacks | ||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||
| 198 | # the pure perl mainloop | ||||||||||||||||||||||||||||||||||||
| 199 | 0 | ▶ | sub one_event { | ||||||||||||||||||||||||||||||||||
|
Subroutine
one_event: not called CC=23 SCAR=63.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 200 | 0 | _update_clock; | |||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||
| 202 | # first sort timers if required (slow) | ||||||||||||||||||||||||||||||||||||
| 203 | 0 | ▶ | if ($MNOW >= $need_sort) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 204 | 0 | $need_sort = 1e300; | |||||||||||||||||||||||||||||||||||
| 205 | 0 | @timer = sort { $a->[0] <=> $b->[0] } @timer; | |||||||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||
| 208 | # handle all pending timers | ||||||||||||||||||||||||||||||||||||
| 209 | 0 | ▶ | if (@timer && $timer[0][0] <= $MNOW) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@timer and $timer[0][0] <= $AnyEvent::Loop::MNOW
MC/DC0%0/2
@timer and $timer[0][0] <= $AnyEvent::Loop::MNOW
@timer
$timer[0][0] <= $AnyEvent::Loop::MNOW
Truth table0%0/3
A and B
A= @timerB= $timer[0][0] <= $AnyEvent::Loop::MNOW
| |||||||||||||||||||||||||||||||||||||
| 210 | 0 | ▶ | do { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@timer and $timer[0][0] <= $AnyEvent::Loop::MNOW
MC/DC0%0/2
@timer and $timer[0][0] <= $AnyEvent::Loop::MNOW
@timer
$timer[0][0] <= $AnyEvent::Loop::MNOW
Truth table0%0/3
A and B
A= @timerB= $timer[0][0] <= $AnyEvent::Loop::MNOW
| |||||||||||||||||||||||||||||||||||||
| 211 | 0 | my $timer = shift @timer; | |||||||||||||||||||||||||||||||||||
| 212 | 0 | ▶ | $timer->[1] && $timer->[1]($timer); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 213 | } while @timer && $timer[0][0] <= $MNOW; | ||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||
| 215 | } else { | ||||||||||||||||||||||||||||||||||||
| 216 | # poll for I/O events, we do not do this when there | ||||||||||||||||||||||||||||||||||||
| 217 | # were any pending timers to ensure that one_event returns | ||||||||||||||||||||||||||||||||||||
| 218 | # quickly when some timers have been handled | ||||||||||||||||||||||||||||||||||||
| 219 | 0 | ▶ | my ($wait, @vec, $fds) | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@timer and $timer[0][0] < $need_sort
MC/DC0%0/2
@timer and $timer[0][0] < $need_sort
@timer
$timer[0][0] < $need_sort
Truth table0%0/3
A and B
A= @timerB= $timer[0][0] < $need_sort
| |||||||||||||||||||||||||||||||||||||
| 220 | = (@timer && $timer[0][0] < $need_sort ? $timer[0][0] : $need_sort) - $MNOW; | ||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||
| 222 | 0 | ▶ | $wait = $wait < MAXWAIT ? $wait + ROUNDUP : MAXWAIT; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 223 | 0 | ▶ | $wait = 0 if @idle; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||
| 225 | 0 | $fds = CORE::select | |||||||||||||||||||||||||||||||||||
| 226 | $vec[0] = $fds[0][V], | ||||||||||||||||||||||||||||||||||||
| 227 | $vec[1] = $fds[1][V], | ||||||||||||||||||||||||||||||||||||
| 228 | AnyEvent::WIN32 ? $vec[2] = $fds[1][V] : undef, | ||||||||||||||||||||||||||||||||||||
| 229 | $wait; | ||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||
| 231 | 0 | _update_clock; | |||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||
| 233 | 0 | ▶ | if ($fds > 0) { | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||
| 234 | # buggy microshit windows errornously sets exceptfds instead of writefds | ||||||||||||||||||||||||||||||||||||
| 235 | 0 | $vec[1] |= $vec[2] if AnyEvent::WIN32; | |||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||
| 237 | # prefer write watchers, because they might reduce memory pressure. | ||||||||||||||||||||||||||||||||||||
| 238 | 0 | for (1, 0) { | |||||||||||||||||||||||||||||||||||
| 239 | 0 | my $fds = $fds[$_]; | |||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||
| 241 | # we parse the bitmask by first expanding it into | ||||||||||||||||||||||||||||||||||||
| 242 | # a string of bits | ||||||||||||||||||||||||||||||||||||
| 243 | 0 | for (unpack "b*", $vec[$_]) { | |||||||||||||||||||||||||||||||||||
| 244 | # and then repeatedly matching a regex against it | ||||||||||||||||||||||||||||||||||||
| 245 | 0 | ▶ | while (/1/g) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 246 | # and use the resulting string position as fd | ||||||||||||||||||||||||||||||||||||
| 247 | $_ && $_->[2]() | ||||||||||||||||||||||||||||||||||||
| 248 | 0 | ▶ | for @{ $fds->[W][(pos) - 1] || [] }; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$fds->[1][pos($_) - 1] || []
MC/DC0%0/1
$fds->[1][pos($_) - 1] || []
$fds->[1][pos($_) - 1]
Truth table0%0/2
A
A= $fds->[1][pos($_) - 1]
| |||||||||||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||||||
| 252 | } elsif (AnyEvent::WIN32 && $fds && $! == AnyEvent::Util::WSAEINVAL) { | ||||||||||||||||||||||||||||||||||||
| 253 | # buggy microshit windoze asks us to route around it | ||||||||||||||||||||||||||||||||||||
| 254 | CORE::select undef, undef, undef, $wait if $wait; | ||||||||||||||||||||||||||||||||||||
| 255 | ▶ | } elsif (!@timer || $timer[0][0] > $MNOW && !$fds) { | |||||||||||||||||||||||||||||||||||
|
Condition0%0/6
not @timer or $timer[0][0] > $AnyEvent::Loop::MNOW and not $fds
$timer[0][0] > $AnyEvent::Loop::MNOW and not $fds
MC/DC0%0/3
not @timer or $timer[0][0] > $AnyEvent::Loop::MNOW and not $fds
not @timer
$timer[0][0] > $AnyEvent::Loop::MNOW
not $fds
Truth table0%0/4
not(A) or B and not(C)
A= not @timerB= $timer[0][0] > $AnyEvent::Loop::MNOWC= not $fds
| |||||||||||||||||||||||||||||||||||||
| 256 | 0 | ▶ | $$$_ && $$$_->() for @idle = grep $$$_, @idle; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||
| 261 | 0 | ▶ | sub run { | ||||||||||||||||||||||||||||||||||
|
Subroutine
run: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 262 | 0 | one_event while 1; | |||||||||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||
| 265 | 0 | ▶ | sub io($$$) { | ||||||||||||||||||||||||||||||||||
|
Subroutine
io: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 266 | 0 | my ($fd, $write, $cb) = @_; | |||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||
| 268 | 0 | ▶ | defined ($fd = fileno $fd) | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 269 | or $fd = $_[0]; | ||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||
| 271 | 0 | my $self = bless [ | |||||||||||||||||||||||||||||||||||
| 272 | $fd, | ||||||||||||||||||||||||||||||||||||
| 273 | $write, | ||||||||||||||||||||||||||||||||||||
| 274 | $cb, | ||||||||||||||||||||||||||||||||||||
| 275 | # q-idx | ||||||||||||||||||||||||||||||||||||
| 276 | ], "AnyEvent::Loop::io"; | ||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||
| 278 | 0 | my $fds = $fds[$self->[1]]; | |||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||
| 280 | # add watcher to fds structure | ||||||||||||||||||||||||||||||||||||
| 281 | 0 | ▶ | my $q = $fds->[W][$fd] ||= []; | ||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$fds->[1][$fd] ||= []
MC/DC0%0/1
$fds->[1][$fd] ||= []
$fds->[1][$fd]
Truth table0%0/2
A
A= $fds->[1][$fd]
| |||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||
| 283 | 0 | (vec $fds->[V], $fd, 1) = 1; | |||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||
| 285 | 0 | $self->[3] = @$q; | |||||||||||||||||||||||||||||||||||
| 286 | 0 | push @$q, $self; | |||||||||||||||||||||||||||||||||||
| 287 | 0 | weaken $q->[-1]; | |||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||
| 289 | 0 | $self | |||||||||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||
| 292 | 0 | ▶ | sub AnyEvent::Loop::io::DESTROY { | ||||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||
| 293 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||
| 295 | 0 | my $fds = $fds[$self->[1]]; | |||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||
| 297 | # remove watcher from fds structure | ||||||||||||||||||||||||||||||||||||
| 298 | 0 | my $fd = $self->[0]; | |||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||||
| 300 | 0 | ▶ | if (@{ $fds->[W][$fd] } == 1) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 301 | 0 | delete $fds->[W][$fd]; | |||||||||||||||||||||||||||||||||||
| 302 | 0 | (vec $fds->[V], $fd, 1) = 0; | |||||||||||||||||||||||||||||||||||
| 303 | } else { | ||||||||||||||||||||||||||||||||||||
| 304 | 0 | my $q = $fds->[W][$fd]; | |||||||||||||||||||||||||||||||||||
| 305 | 0 | my $last = pop @$q; | |||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||
| 307 | 0 | ▶ | if ($last != $self) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 308 | 0 | weaken ($q->[$self->[3]] = $last); | |||||||||||||||||||||||||||||||||||
| 309 | 0 | $last->[3] = $self->[3]; | |||||||||||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||
| 314 | 1 | ▶ | sub timer($$$) { | ||||||||||||||||||||||||||||||||||
|
Subroutine
timer: called CC=3 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 315 | 1 | my ($after, $interval, $cb) = @_; | |||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||
| 317 | 1 | my $self; | |||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||
| 319 | 1 | ▶ | if ($interval) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 320 | 0 | ▶ | $self = [$MNOW + $after , sub { | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||
| 321 | 0 | $_[0][0] = List::Util::max $_[0][0] + $interval, $MNOW; | |||||||||||||||||||||||||||||||||||
| 322 | 0 | push @timer, $_[0]; | |||||||||||||||||||||||||||||||||||
| 323 | 0 | weaken $timer[-1]; | |||||||||||||||||||||||||||||||||||
| 324 | 0 | ▶ | $need_sort = $_[0][0] if $_[0][0] < $need_sort; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 325 | 0 | &$cb; | |||||||||||||||||||||||||||||||||||
| 326 | 1 | }]; | |||||||||||||||||||||||||||||||||||
| 327 | } else { | ||||||||||||||||||||||||||||||||||||
| 328 | 0 | $self = [$MNOW + $after, $cb]; | |||||||||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||
| 331 | 1 | push @timer, $self; | |||||||||||||||||||||||||||||||||||
| 332 | 1 | weaken $timer[-1]; | |||||||||||||||||||||||||||||||||||
| 333 | 1 | ▶ | $need_sort = $self->[0] if $self->[0] < $need_sort; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||
| 335 | 1 | $self | |||||||||||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||
| 338 | 0 | ▶ | sub idle($) { | ||||||||||||||||||||||||||||||||||
|
Subroutine
idle: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 339 | 0 | my $cb = shift; | |||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||
| 341 | 0 | push @idle, \\$cb; | |||||||||||||||||||||||||||||||||||
| 342 | 0 | weaken ${$idle[-1]}; | |||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||
| 344 | 0 | ${$idle[-1]} | |||||||||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||
| 347 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||||
| 349 | L<AnyEvent>. | ||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||
| 351 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||
| 353 | Marc Lehmann <schmorp@schmorp.de> | ||||||||||||||||||||||||||||||||||||
| 354 | http://anyevent.schmorp.de | ||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||
| 356 | =cut | ||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||
| 358 | 3 | 1 | |||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||