| 1 | package Mojo::Reactor; | ||||||||||||||||||||||||||||
| 2 | 33 | ▶ | use Mojo::Base 'Mojo::EventEmitter'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 33 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 33 | ▶ | use Config; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 33 | ▶ | use Mojo::Loader qw(load_class); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 33 | my %DETECTED; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | sub again { croak 'Method "again" not implemented by subclass' } | ||||||||||||||||||||||||||
|
Subroutine
again: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 54 | ▶ | sub detect { | ||||||||||||||||||||||||||
|
Subroutine
detect: called CC=5 SCAR=16.7
Pod
documented
| |||||||||||||||||||||||||||||
| 13 | 54 | ▶ | my $default = 'Mojo::Reactor::' . ($Config{d_pseudofork} ? 'Poll' : 'EV'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 14 | 54 | ▶ | my $try = $ENV{MOJO_REACTOR} || $default; | ||||||||||||||||||||||||||
|
Condition66%2/3
$ENV{'MOJO_REACTOR'} || $default
MC/DC0%0/2
$ENV{'MOJO_REACTOR'} || $default
$ENV{'MOJO_REACTOR'}
$default
Truth table66%2/3
A || B
A= $ENV{'MOJO_REACTOR'}B= $default
| |||||||||||||||||||||||||||||
| 15 | 54 | ▶ | return $DETECTED{$try} ||= load_class($try) ? 'Mojo::Reactor::Poll' : $try; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$DETECTED{$try} ||= load_class($try) ? "Mojo::Reactor::Poll" : $try
MC/DC0%0/2
$DETECTED{$try} ||= load_class($try) ? "Mojo::Reactor::Poll" : $try
$DETECTED{$try}
load_class($try) ? "Mojo::Reactor::Poll" : $try
Truth table66%2/3
A ||= B
A= $DETECTED{$try}B= load_class($try) ? "Mojo::Reactor::Poll" : $try
| |||||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 1 | ▶ | sub io { croak 'Method "io" not implemented by subclass' } | ||||||||||||||||||||||||||
|
Subroutine
io: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 19 | 1 | ▶ | sub is_running { croak 'Method "is_running" not implemented by subclass' } | ||||||||||||||||||||||||||
|
Subroutine
is_running: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | 1 | ▶ | sub next_tick { croak 'Method "next_tick" not implemented by subclass' } | ||||||||||||||||||||||||||
|
Subroutine
next_tick: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 21 | 1 | ▶ | sub one_tick { croak 'Method "one_tick" not implemented by subclass' } | ||||||||||||||||||||||||||
|
Subroutine
one_tick: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | 1 | ▶ | sub recurring { croak 'Method "recurring" not implemented by subclass' } | ||||||||||||||||||||||||||
|
Subroutine
recurring: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 23 | 1 | ▶ | sub remove { croak 'Method "remove" not implemented by subclass' } | ||||||||||||||||||||||||||
|
Subroutine
remove: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | 1 | ▶ | sub reset { croak 'Method "reset" not implemented by subclass' } | ||||||||||||||||||||||||||
|
Subroutine
reset: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 25 | 1 | ▶ | sub start { croak 'Method "start" not implemented by subclass' } | ||||||||||||||||||||||||||
|
Subroutine
start: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 26 | 1 | ▶ | sub stop { croak 'Method "stop" not implemented by subclass' } | ||||||||||||||||||||||||||
|
Subroutine
stop: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | 1 | ▶ | sub timer { croak 'Method "timer" not implemented by subclass' } | ||||||||||||||||||||||||||
|
Subroutine
timer: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 28 | 1 | ▶ | sub watch { croak 'Method "watch" not implemented by subclass' } | ||||||||||||||||||||||||||
|
Subroutine
watch: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 33 | 1; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =encoding utf8 | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | =head1 NAME | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | Mojo::Reactor - Low-level event reactor base class | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | package Mojo::Reactor::MyEventLoop; | ||||||||||||||||||||||||||||
| 41 | use Mojo::Base 'Mojo::Reactor'; | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | sub again {...} | ||||||||||||||||||||||||||||
| 44 | sub io {...} | ||||||||||||||||||||||||||||
| 45 | sub is_running {...} | ||||||||||||||||||||||||||||
| 46 | sub next_tick {...} | ||||||||||||||||||||||||||||
| 47 | sub one_tick {...} | ||||||||||||||||||||||||||||
| 48 | sub recurring {...} | ||||||||||||||||||||||||||||
| 49 | sub remove {...} | ||||||||||||||||||||||||||||
| 50 | sub reset {...} | ||||||||||||||||||||||||||||
| 51 | sub start {...} | ||||||||||||||||||||||||||||
| 52 | sub stop {...} | ||||||||||||||||||||||||||||
| 53 | sub timer {...} | ||||||||||||||||||||||||||||
| 54 | sub watch {...} | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | L<Mojo::Reactor> is an abstract base class for low-level event reactors, like L<Mojo::Reactor::EV> and | ||||||||||||||||||||||||||||
| 59 | L<Mojo::Reactor::Poll>. | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =head1 EVENTS | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | L<Mojo::Reactor> inherits all events from L<Mojo::EventEmitter> and can emit the following new ones. | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =head2 error | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | $reactor->on(error => sub ($reactor, $err) {...}); | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | Emitted for exceptions caught in callbacks, fatal if unhandled. Note that if this event is unhandled or fails it might | ||||||||||||||||||||||||||||
| 70 | kill your program, so you need to be careful. | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | $reactor->on(error => sub ($reactor, $err) { say "Something very bad happened: $err" }); | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =head1 METHODS | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | L<Mojo::Reactor> inherits all methods from L<Mojo::EventEmitter> and implements the following new ones. | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =head2 again | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | $reactor->again($id); | ||||||||||||||||||||||||||||
| 81 | $reactor->again($id, 0.5); | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | Restart timer and optionally change the invocation time. Meant to be overloaded in a subclass. Note that this method | ||||||||||||||||||||||||||||
| 84 | requires an active timer. | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =head2 detect | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | my $class = Mojo::Reactor->detect; | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | Detect and load the best reactor implementation available, will try the value of the C<MOJO_REACTOR> environment | ||||||||||||||||||||||||||||
| 91 | variable, L<Mojo::Reactor::EV> or L<Mojo::Reactor::Poll>. | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # Instantiate best reactor implementation available | ||||||||||||||||||||||||||||
| 94 | my $reactor = Mojo::Reactor->detect->new; | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =head2 io | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | $reactor = $reactor->io($handle => sub {...}); | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | Watch handle for I/O events, invoking the callback whenever handle becomes readable or writable. Meant to be overloaded | ||||||||||||||||||||||||||||
| 101 | in a subclass. | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | # Callback will be executed twice if handle becomes readable and writable | ||||||||||||||||||||||||||||
| 104 | $reactor->io($handle => sub ($reactor, $writable) { | ||||||||||||||||||||||||||||
| 105 | say $writable ? 'Handle is writable' : 'Handle is readable'; | ||||||||||||||||||||||||||||
| 106 | }); | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =head2 is_running | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | my $bool = $reactor->is_running; | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | Check if reactor is running. Meant to be overloaded in a subclass. | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =head2 next_tick | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | my $undef = $reactor->next_tick(sub {...}); | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | Execute callback as soon as possible, but not before returning or other callbacks that have been registered with this | ||||||||||||||||||||||||||||
| 119 | method, always returns C<undef>. Meant to be overloaded in a subclass. | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | =head2 one_tick | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | $reactor->one_tick; | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | Run reactor until an event occurs. Note that this method can recurse back into the reactor, so you need to be careful. | ||||||||||||||||||||||||||||
| 126 | Meant to be overloaded in a subclass. | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | # Don't block longer than 0.5 seconds | ||||||||||||||||||||||||||||
| 129 | my $id = $reactor->timer(0.5 => sub {}); | ||||||||||||||||||||||||||||
| 130 | $reactor->one_tick; | ||||||||||||||||||||||||||||
| 131 | $reactor->remove($id); | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | =head2 recurring | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | my $id = $reactor->recurring(0.25 => sub {...}); | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | Create a new recurring timer, invoking the callback repeatedly after a given amount of time in seconds. Meant to be | ||||||||||||||||||||||||||||
| 138 | overloaded in a subclass. | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =head2 remove | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | my $bool = $reactor->remove($handle); | ||||||||||||||||||||||||||||
| 143 | my $bool = $reactor->remove($id); | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | Remove handle or timer. Meant to be overloaded in a subclass. | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | =head2 reset | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | $reactor->reset; | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | Remove all handles and timers. Meant to be overloaded in a subclass. | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =head2 start | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | $reactor->start; | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | Start watching for I/O and timer events, this will block until L</"stop"> is called. Note that some reactors stop | ||||||||||||||||||||||||||||
| 158 | automatically if there are no events being watched anymore. Meant to be overloaded in a subclass. | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | # Start reactor only if it is not running already | ||||||||||||||||||||||||||||
| 161 | $reactor->start unless $reactor->is_running; | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =head2 stop | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | $reactor->stop; | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | Stop watching for I/O and timer events. Meant to be overloaded in a subclass. | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =head2 timer | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | my $id = $reactor->timer(0.5 => sub {...}); | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | Create a new timer, invoking the callback after a given amount of time in seconds. Meant to be overloaded in a | ||||||||||||||||||||||||||||
| 174 | subclass. | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | =head2 watch | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | $reactor = $reactor->watch($handle, $readable, $writable); | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | Change I/O events to watch handle for with true and false values. Meant to be overloaded in a subclass. Note that this | ||||||||||||||||||||||||||||
| 181 | method requires an active I/O watcher. | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | # Watch only for readable events | ||||||||||||||||||||||||||||
| 184 | $reactor->watch($handle, 1, 0); | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | # Watch only for writable events | ||||||||||||||||||||||||||||
| 187 | $reactor->watch($handle, 0, 1); | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | # Watch for readable and writable events | ||||||||||||||||||||||||||||
| 190 | $reactor->watch($handle, 1, 1); | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | # Pause watching for events | ||||||||||||||||||||||||||||
| 193 | $reactor->watch($handle, 0, 0); | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>. | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | =cut | ||||||||||||||||||||||||||||