blib/lib/Mojo/Reactor/Prima.pm

filter: Statementstmt 26.5% 31 / 117 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 29 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 36.6% 11 / 30 Podpod n/a totaltotal 18.6% 42 / 225 CC 29 SCAR 28.1
CC
29
Cov
17%
remove
42.0
watch
42.0
DESTROY
30.0
CRAP 16.6
↑ Summary
1 package Mojo::Reactor::Prima;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
6 0 ▶use Prima qw(Utils);
Subroutine BEGIN: called CC=1 SCAR=0.4
Subroutine BEGIN: called CC=1 SCAR=0.4
7 1 ▶use Mojo::Base 'Mojo::Reactor';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Mojo::Util qw(md5_sum steady_time);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 our $VERSION = '1.02';
11  
12 $ENV{MOJO_REACTOR} ||= 'Mojo::Reactor::Prima';
13  
14 my ($refcnt, $destroy, %loops, $current);
15  
16 sub new
17 0 ▶{
Subroutine new: not called CC=3 SCAR=24.8
18 0 my $self = shift->SUPER::new;
19 0 $refcnt++;
20 0 ▶ unless ( $::application ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($main::application) 0 0 0
21 0 $::application = Prima::Application->new;
22 0 $destroy = 1;
23 }
24 0 $self->{timers} = {};
25 0 $self->{io} = {};
26 0 $loops{"$self"} = $self;
27 0 weaken $loops{"$self"};
28 0 ▶ $current //= "$self";
Condition0%0/2
$current //= "$self"
l!l
00
MC/DC0%0/1
$current //= "$self"
$current
Truth table0%0/2
A
A= $current
Aresult
11
00
29 0 return $self;
30 }
31  
32 sub DESTROY
33 0 ▶{
Subroutine DESTROY: not called CC=5 SCAR=34.0
34 0 my $self = shift;
35 0 delete $loops{"$self"};
36 0 ▶ if (0 == --$refcnt && $destroy && $::application) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == --$refcnt and $destroy and $main::application) 0 0 0
Condition0%0/6
0 == --$refcnt and $destroy and $main::application
!ll&&!rl&&r
000
0 == --$refcnt and $destroy
!ll&&!rl&&r
000
MC/DC0%0/3
0 == --$refcnt and $destroy and $main::application
0 == --$refcnt $destroy $main::application
Truth table0%0/4
A and B and C
A= 0 == --$refcntB= $destroyC= $main::application
ABCresult
0XX0
10X0
1100
1111
37 0 ▶ $::application->destroy if $::application->alive;
Branch: 1 branch
BranchTrueFalseTotal
if $main::application->alive 0 0 0
38 0 $::application = undef;
39 }
40 }
41  
42 sub again
43 0 ▶{
Subroutine again: not called CC=3 SCAR=24.8
44 0 my $self = shift;
45 0 ▶ croak 'Timer not active' unless my $timer = $self->{timers}{shift()};
Branch: 1 branch
BranchTrueFalseTotal
unless my $timer = $self->{"timers"}{shift()} 0 0 0
46 0 $timer->{watcher}->stop;
47 0 ▶ $timer->{watcher}->start if $self->_is_active;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_active 0 0 0
48 }
49  
50 sub io
51 0 ▶{
Subroutine io: not called CC=2 SCAR=17.9
52 0 my ($self, $handle, $cb) = @_;
53 0 ▶ $self->{io}{fileno($handle) // croak 'Handle is closed'} = {cb => $cb};
Condition0%0/2
fileno $handle // croak("Handle is closed")
l!l
00
MC/DC0%0/1
fileno $handle // croak("Handle is closed")
fileno $handle
Truth table0%0/2
A
A= fileno $handle
Aresult
11
00
54 0 return $self->watch($handle, 1, 1);
55 }
56  
57 0 ▶sub is_running { !!shift->{running} }
Subroutine is_running: not called CC=1 SCAR=6.9
58  
59 sub _next
60 0 ▶{
Subroutine _next: not called CC=2 SCAR=17.9
61 0 my $self = shift;
62 0 delete $self->{next_timer};
63 0 ▶ while (my $cb = shift @{$self->{next_tick}}) { $self->$cb() }
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = shift @{$$self{"next_tick"};}) 0 0 0
64 }
65  
66 sub next_tick
67 0 ▶{
Subroutine next_tick: not called CC=2 SCAR=17.9
68 0 my ($self, $cb) = @_;
69 0 push @{$self->{next_tick}}, $cb;
70 0 ▶ $self->{next_timer} //= $self->timer(0 => \&_next);
Condition0%0/3
$self->{"next_timer"} //= $self->timer(0, \&Mojo::Reactor::Prima::_next)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"next_timer"} //= $self->timer(0, \&Mojo::Reactor::Prima::_next)
$self->{"next_timer"} $self->timer(0, \&Mojo::Reactor::Prima::_next)
Truth table0%0/3
A //= B
A= $self->{"next_timer"}B= $self->timer(0, \&Mojo::Reactor::Prima::_next)
ABresult
1X1
011
000
71 0 return undef;
72 }
73  
74 sub one_tick
75 0 ▶{
Subroutine one_tick: not called CC=4 SCAR=30.0
76 0 my $self = shift;
77 0 ▶ return $self->stop unless keys %{ $self->{io} } || keys %{ $self->{timers} };
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$$self{"io"};} or keys %{$$self{"timers"};} 0 0 0
Condition0%0/3
keys %{$$self{"io"};} or keys %{$$self{"timers"};}
l!l&&r!l&&!r
000
MC/DC0%0/2
keys %{$$self{"io"};} or keys %{$$self{"timers"};}
keys %{$$self{"io"};} keys %{$$self{"timers"};}
Truth table0%0/3
A or B
A= keys %{$$self{"io"};}B= keys %{$$self{"timers"};}
ABresult
1X1
011
000
78 0 ▶ local $self->{running} = 1 unless $self->{running};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"running"} 0 0 0
79 0 $self-> _select;
80 0 $::application->yield(1);
81 }
82  
83 0 ▶sub recurring { shift->_timer(1, @_) }
Subroutine recurring: not called CC=1 SCAR=6.9
84  
85 sub remove
86 0 ▶{
Subroutine remove: not called CC=6 SCAR=37.4
87 0 my ($self, $remove) = @_;
88 0 my $obj;
89 0 ▶ return 0 unless defined $remove;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $remove 0 0 0
90 0 ▶ if ( ref($remove)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $remove) { } 0 0 0
91 0 ▶ $obj = delete $self->{io}{fileno($remove) // croak 'Handle is closed'};
Condition0%0/2
fileno $remove // croak("Handle is closed")
l!l
00
MC/DC0%0/1
fileno $remove // croak("Handle is closed")
fileno $remove
Truth table0%0/2
A
A= fileno $remove
Aresult
11
00
92 } else {
93 0 $obj = delete $self->{timers}{$remove};
94 }
95 0 ▶ $obj->{watcher}->destroy if $obj && $obj->{watcher};
Branch: 1 branch
BranchTrueFalseTotal
if $obj and $obj->{"watcher"} 0 0 0
Condition0%0/3
$obj and $obj->{"watcher"}
!ll&&!rl&&r
000
MC/DC0%0/2
$obj and $obj->{"watcher"}
$obj $obj->{"watcher"}
Truth table0%0/3
A and B
A= $objB= $obj->{"watcher"}
ABresult
0X0
100
111
96 0 return !!$obj;
97 }
98  
99 sub reset
100 0 ▶{
Subroutine reset: not called CC=2 SCAR=17.9
101 0 my $self = shift;
102 0 $_->destroy for grep { defined } map { $_->{watcher} }
103 0 values (%{ $self->{io} }),
104 0 values (%{ $self->{timers} });
105 ;
106 0 delete @{$self}{qw(io next_tick next_timer timers events)}
107 }
108  
109 sub start
110 0 ▶{
Subroutine start: not called CC=4 SCAR=30.0
111 0 my $self = shift;
112 0 ▶ return unless keys %{ $self->{io} } || keys %{ $self->{timers} };
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$$self{"io"};} or keys %{$$self{"timers"};} 0 0 0
Condition0%0/3
keys %{$$self{"io"};} or keys %{$$self{"timers"};}
l!l&&r!l&&!r
000
MC/DC0%0/2
keys %{$$self{"io"};} or keys %{$$self{"timers"};}
keys %{$$self{"io"};} keys %{$$self{"timers"};}
Truth table0%0/3
A or B
A= keys %{$$self{"io"};}B= keys %{$$self{"timers"};}
ABresult
1X1
011
000
113 0 ▶ local $self->{running} = ($self->{running} || 0) + 1;
Condition0%0/2
$self->{'running'} || 0
l!l
00
MC/DC0%0/1
$self->{'running'} || 0
$self->{'running'}
Truth table0%0/2
A
A= $self->{'running'}
Aresult
11
00
114 0 $self-> _select;
115 0 $::application->go;
116 }
117  
118 sub stop
119 0 ▶{
Subroutine stop: not called CC=1 SCAR=6.9
120 0 delete shift->{running};
121 0 $::application->stop;
122 }
123  
124 0 ▶sub timer { shift->_timer(0, @_) }
Subroutine timer: not called CC=1 SCAR=6.9
125  
126 sub _watch_read_cb
127 0 ▶{
Subroutine _watch_read_cb: not called CC=1 SCAR=6.9
128 0 my ($self, $obj, $fd) = @_;
129 0 $self->_try('I/O watcher', $self->{io}{$fd}{cb}, 0);
130 }
131  
132 sub _watch_write_cb
133 0 ▶{
Subroutine _watch_write_cb: not called CC=1 SCAR=6.9
134 0 my ($self, $obj, $fd) = @_;
135 0 $self->_try('I/O watcher', $self->{io}{$fd}{cb}, 1);
136 }
137  
138 sub watch
139 0 ▶{
Subroutine watch: not called CC=6 SCAR=37.4
140 0 my ($self, $handle, $read, $write) = @_;
141  
142 0 my $fd = fileno $handle;
143 0 ▶ croak 'I/O watcher not active' unless my $io = $self->{io}{$fd};
Branch: 1 branch
BranchTrueFalseTotal
unless my $io = $self->{"io"}{$fd} 0 0 0
144  
145 0 my $mode = 0;
146 0 ▶ $mode |= fe::Read if $read;
Branch: 1 branch
BranchTrueFalseTotal
if $read 0 0 0
147 0 ▶ $mode |= fe::Write if $write;
Branch: 1 branch
BranchTrueFalseTotal
if $write 0 0 0
148 my $obj = $io->{watcher} //= Prima::File->new(
149 fd => $fd,
150 0 ▶ onRead => sub { $self->_try('I/O watcher', $self->{io}{$fd}{cb}, 0) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
151 0 ▶ onWrite => sub { $self->_try('I/O watcher', $self->{io}{$fd}{cb}, 1) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
152 0 ▶ );
Condition0%0/3
$io->{"watcher"} //= "Prima::File"->new("fd", $fd, "onRead", sub { $self->_try("I/O watcher", $self->{"io"}{$fd}{"cb"}, 0); } , "onWrite", sub { $self->_try("I/O watcher", $self->{"io"}{$fd}{"cb"}, 1); } )
l!l&&r!l&&!r
000
MC/DC0%0/2
$io->{"watcher"} //= "Prima::File"->new("fd", $fd, "onRead", sub { $self->_try("I/O watcher", $self->{"io"}{$fd}{"cb"}, 0); } , "onWrite", sub { $self->_try("I/O watcher", $self->{"io"}{$fd}{"cb"}, 1); } )
$io->{"watcher"} "Prima::File"->new("fd", $fd, "onRead", sub { $self->_try("I/O watcher", $self->{"io"}{$fd}{"cb"}, 0); } , "onWrite", sub { $self->_try("I/O watcher", $self->{"io"}{$fd}{"cb"}, 1); } )
Truth table0%0/3
A //= B
A= $io->{"watcher"}B= "Prima::File"->new("fd", $fd, "onRead", sub { $self->_try("I/O watcher", $self->{"io"}{$fd}{"cb"}, 0); } , "onWrite", sub { $self->_try("I/O watcher", $self->{"io"}{$fd}{"cb"}, 1); } )
ABresult
1X1
011
000
153 0 $io->{mask} = $mode;
154 0 ▶ $obj->mask($mode) if $self->_is_active;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_active 0 0 0
155  
156 0 return $self;
157 }
158  
159 sub _id
160 {
161 my $self = shift;
162 my $id;
163 do { $id = md5_sum 't' . steady_time . rand } while $self->{timers}{$id};
164 return $id;
165 }
166  
167 sub _timer
168 {
169 my ($self, $recurring, $after, $cb) = @_;
170 $after ||= 0.0001;
171 my $id = $self->_id;
172  
173 my $t = $self->{timers}{$id}{watcher} = Prima::Timer->new(
174 timeout => $after * 1000,
175 onTick => sub {
176 unless ($recurring) {
177 $_[0]->destroy;
178 delete $self->{timers}{$id};
179 }
180 $self->_try('Timer', $cb);
181 },
182 );
183 $t->start if $self->_is_active;
184 return $id;
185 }
186  
187 sub _try
188 {
189 my ($self, $what, $cb) = (shift,shift,shift);
190 eval { $self->$cb(@_); 1 } or $self->emit(error => "$what failed: $@");
191 $self->stop unless keys %{ $self->{io} } || keys %{ $self->{timers} };
192 }
193  
194 sub _is_active { $_[0] eq $current || $_[0]->{running } }
195  
196 sub _select
197 {
198 my $self = shift;
199 return if $current eq "$self";
200 $current = "$self";
201 for my $loop ( values %loops ) {
202 if ( $self eq $loop ) {
203 $_->{watcher}->start for values %{ $loop->{timers} };
204 $_->{watcher}->mask( $_->{mask} ) for values %{ $loop->{io} };
205 } elsif ( ! $loop->{running} ) {
206 $_->{watcher}->stop for values %{ $loop->{timers} };
207 $_->{watcher}->mask( 0 ) for values %{ $loop->{io} };
208 }
209 }
210 }
211  
212 1;
213  
214 =pod
215  
216 =head1 NAME
217  
218 Mojo::Reactor::Prima - Prima event loop backend for Mojo::Reactor
219  
220 =head1 DESCRIPTION
221  
222 L<Mojo::Reactor::Prima> is an event reactor for L<Mojo::IOLoop> that uses
223 L<Prima>. The usage is exactly the same as other L<Mojo::Reactor>
224 implementations such as L<Mojo::Reactor::Poll>. L<Mojo::Reactor::Prima> will be
225 used as the default backend for L<Mojo::IOLoop> if it is loaded before
226 L<Mojo::IOLoop> or any module using the loop. However, when invoking a
227 L<Mojolicious> application through L<morbo> or L<hypnotoad>, the reactor must
228 be set as the default by setting the C<MOJO_REACTOR> environment variable to
229 C<Mojo::Reactor::Prima>.
230  
231 =head1 AUTHOR
232  
233 Dmitry Karasik E<lt>dmitry@karasik.eu.orgE<gt>
234  
235 =head1 LICENSE
236  
237 This library is free software; you may redistribute it and/or modify it under
238 the terms of the Artistic License version 2.0.
239  
240 =head1 SEE ALSO
241  
242 L<Mojolicious>, L<Mojo::IOLoop>, L<Prima>
243  
244 =cut
↑ Summary