blib/lib/POE/Kernel.pm

filter: Statementstmt 90.0% 706 / 784 Branchbran 84.4% 255 / 302 Conditioncond 65.6% 63 / 96 MC/DCmcdc 37.2% 22 / 59 Subroutinesub 87.8% 101 / 115 Podpod 100% 49 / 49 totaltotal 85.1% 1196 / 1405 CC 286 SCAR 19.7
CC
286
Cov
87%
BEGIN
21.7
_dispatch_event
21.0
call
17.6
CRAP 7.2
« blib/lib/POE/Wheel/Curses.pm ↑ Summary blib/lib/POE/Filter/Block.pm »
1 package POE::Kernel;
2  
3 201 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 201 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1402 $VERSION = '1.367'; # NOTE - Should be #.### (three decimal places)
7  
8 201 ▶use POE::Resource::Clock qw( monotime sleep mono2wall wall2mono walltime time );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 201 ▶use POSIX qw(uname);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 201 ▶use Errno qw(ESRCH EINTR ECHILD EPERM EINVAL EEXIST EAGAIN EWOULDBLOCK);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 201 ▶use Carp qw(carp croak confess cluck);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 201 ▶use Sys::Hostname qw(hostname);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 201 ▶use IO::Handle ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 201 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 #use Time::HiRes qw(time sleep);
17  
18 # People expect these to be lexical.
19  
20 201 ▶use vars qw($poe_kernel $poe_main_window);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 #------------------------------------------------------------------------------
23 # A cheezy exporter to avoid using Exporter.
24  
25 68 my $queue_class;
26  
27 BEGIN {
28 201 ▶ eval {
Subroutine BEGIN: called CC=2 SCAR=7.5
29 201 require POE::XS::Queue::Array;
30 0 POE::XS::Queue::Array->import();
31 0 $queue_class = "POE::XS::Queue::Array";
32 };
33 201 ▶ unless ($queue_class) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($queue_class) 201 0 201
34 201 require POE::Queue::Array;
35 201 POE::Queue::Array->import();
36 201 $queue_class = "POE::Queue::Array";
37 }
38 }
39  
40 sub import {
41 555 ▶ my ($class, $args) = ($poe_kernel, @_[1..$#_]);
Subroutine import: called CC=9 SCAR=22.0
42 555 my $package = caller();
43  
44 555 ▶ croak "POE::Kernel expects its arguments in a hash ref"
Branch: 1 branch
BranchTrueFalseTotal
if $args and ref $args ne "HASH" 1 554 555
Condition100%3/3
$args and ref $args ne "HASH"
!ll&&!rl&&r
54681
MC/DC100%2/2
$args and ref $args ne "HASH"
$args ref $args ne "HASH"
Truth table100%3/3
A and B
A= $argsB= ref $args ne "HASH"
ABresult
0X0
100
111
45 if ($args && ref($args) ne 'HASH');
46  
47 {
48 201 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
49 554 *{ $package . '::poe_kernel' } = \$poe_kernel;
50 554 *{ $package . '::poe_main_window' } = \$poe_main_window;
51 }
52  
53 # Extract the import arguments we're interested in here.
54  
55 554 ▶ my $loop = delete $args->{loop} || $ENV{POE_EVENT_LOOP};
Condition100%3/3
delete $args->{'loop'} || $ENV{'POE_EVENT_LOOP'}
l!l&&r!l&&!r
72545
MC/DC100%2/2
delete $args->{'loop'} || $ENV{'POE_EVENT_LOOP'}
delete $args->{'loop'} $ENV{'POE_EVENT_LOOP'}
Truth table100%3/3
A || B
A= delete $args->{'loop'}B= $ENV{'POE_EVENT_LOOP'}
ABresult
1X1
011
000
56  
57 # Don't accept unknown/mistyped arguments.
58  
59 554 my @unknown = sort keys %$args;
60 554 ▶ croak "Unknown POE::Kernel import arguments: @unknown" if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 1 553 554
61  
62 # Now do things with them.
63  
64 553 ▶ unless (UNIVERSAL::can('POE::Kernel', 'poe_kernel_loop')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::can("POE::Kernel", "poe_kernel_loop")) 200 353 553
65 200 ▶ if (defined $loop) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $loop) 6 194 200
66 6 $loop =~ s/^(POE::)?(XS::)?(Loop::)?//;
67 6 ▶ if (defined $2) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $2) { } 0 6 6
68 0 $loop = "POE::XS::Loop::$loop";
69 }
70 else {
71 6 $loop = "POE::Loop::$loop";
72 }
73 }
74 200 _test_loop($loop);
75 # Bootstrap the kernel. This is inherited from a time when multiple
76 # kernels could be present in the same Perl process.
77 198 ▶ POE::Kernel->new() if UNIVERSAL::can('POE::Kernel', 'poe_kernel_loop');
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::can("POE::Kernel", "poe_kernel_loop") 198 0 198
78 }
79 }
80  
81 #------------------------------------------------------------------------------
82 # Perform some optional setup.
83  
84 BEGIN {
85 201 ▶ local $SIG{'__DIE__'} = 'DEFAULT';
Subroutine BEGIN: called CC=2 SCAR=7.4
86  
87 {
88 201 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
89 201 ▶ if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 201 201
90 0 ▶ *{ __PACKAGE__ . '::RUNNING_IN_HELL' } = sub { 1 };
Subroutine __ANON__: not called CC=1 SCAR=6.9
91 } else {
92 201 ▶ *{ __PACKAGE__ . '::RUNNING_IN_HELL' } = sub { 0 };
Subroutine __ANON__: called CC=1 SCAR=0.0
93 }
94 }
95 }
96  
97 BEGIN {
98 # The entire BEGIN block is a no-strict-refs zone.
99  
100 201 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
101  
102 # Set up a constant that lets the user deactivate automatic
103 # exception handling.
104  
105 201 ▶ unless (defined &CATCH_EXCEPTIONS) {
Subroutine BEGIN: called CC=14 SCAR=30.8
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &POE::Kernel::CATCH_EXCEPTIONS) 193 8 201
106 my $catch_exceptions = (
107 (exists $ENV{POE_CATCH_EXCEPTIONS})
108 ? $ENV{POE_CATCH_EXCEPTIONS}
109 193 ▶ : 1
Branch: 1 branch
BranchTrueFalseTotal
exists $ENV{'POE_CATCH_EXCEPTIONS'} ? : 1 192 193
110 );
111  
112 193 ▶ if ($catch_exceptions) {
Branch: 1 branch
BranchTrueFalseTotal
if ($catch_exceptions) { } 192 1 193
113 192 *CATCH_EXCEPTIONS = sub () { 1 };
114 }
115 else {
116 1 *CATCH_EXCEPTIONS = sub () { 0 };
117 }
118 }
119  
120 201 ▶ unless (defined &CHILD_POLLING_INTERVAL) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &POE::Kernel::CHILD_POLLING_INTERVAL) 201 0 201
121 # That's one second, not a true value.
122 201 *CHILD_POLLING_INTERVAL = sub () { 1 };
123 }
124  
125 201 ▶ unless (defined &USE_SIGCHLD) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &POE::Kernel::USE_SIGCHLD) 185 16 201
126 # Perl >= 5.7.3 has safe signals support
127 # perlipc.pod#Deferred_Signals_(Safe_Signals)
128 # We decided to target 5.8.1 just to be safe :)
129 185 ▶ if ( $] >= 5.008001 and not RUNNING_IN_HELL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= 5.008001 and not RUNNING_IN_HELL()) { } 185 0 185
Condition33%1/3
$] >= "5.008001" and not RUNNING_IN_HELL()
!ll&&!rl&&r
00185
MC/DC0%0/2
$] >= "5.008001" and not RUNNING_IN_HELL()
$] >= "5.008001" not RUNNING_IN_HELL()
Truth table33%1/3
A and not(B)
A= $] >= "5.008001"B= not RUNNING_IN_HELL()
ABresult
0X0
100
111
130 185 *USE_SIGCHLD = sub () { 1 };
131 } else {
132 0 *USE_SIGCHLD = sub () { 0 };
133 }
134 }
135  
136 201 ▶ unless (defined &USE_SIGNAL_PIPE) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &POE::Kernel::USE_SIGNAL_PIPE) 198 3 201
137 198 my $use_signal_pipe;
138 198 ▶ if ( exists $ENV{POE_USE_SIGNAL_PIPE} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ENV{"POE_USE_SIGNAL_PIPE"}) 0 198 198
139 0 $use_signal_pipe = $ENV{POE_USE_SIGNAL_PIPE};
140 }
141  
142 198 ▶ if (RUNNING_IN_HELL) {
Branch: 1 branch
BranchTrueFalseTotal
if (RUNNING_IN_HELL()) 0 198 198
143 0 ▶ if ($use_signal_pipe) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_signal_pipe) 0 0 0
144 0 _warn(
145 "Sorry, disabling USE_SIGNAL_PIPE on $^O.\n",
146 "Programs are reported to hang when it's enabled.\n",
147 );
148 }
149  
150 # Must be defined to supersede the default.
151 0 $use_signal_pipe = 0;
152 }
153  
154 198 ▶ if ($use_signal_pipe or not defined $use_signal_pipe) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_signal_pipe or not defined $use_signal_pipe) { } 198 0 198
Condition33%1/3
$use_signal_pipe or not defined $use_signal_pipe
l!l&&r!l&&!r
01980
MC/DC0%0/2
$use_signal_pipe or not defined $use_signal_pipe
$use_signal_pipe not defined $use_signal_pipe
Truth table33%1/3
A or not(B)
A= $use_signal_pipeB= not defined $use_signal_pipe
ABresult
1X1
011
000
155 198 *USE_SIGNAL_PIPE = sub () { 1 };
156 }
157 else {
158 0 *USE_SIGNAL_PIPE = sub () { 0 };
159 }
160 }
161 }
162  
163 #==============================================================================
164 # Globals, or at least package-scoped things. Data structures were
165 # moved into lexicals in 0.1201.
166  
167 # A reference to the currently active session. Used throughout the
168 # functions that act on the current session.
169 my $kr_active_session;
170 68 my $kr_active_event;
171 200 my $kr_active_event_type;
172  
173 # Needs to be lexical so that POE::Resource::Events can see it
174 # change. TODO - Something better? Maybe we call a method in
175 # POE::Resource::Events to trigger the exception there?
176 201 ▶use vars qw($kr_exception);
Subroutine BEGIN: called CC=1 SCAR=0.0
177  
178 # The Kernel's master queue.
179 200 my $kr_queue;
180  
181 # The current PID, to detect when it changes
182 200 my $kr_pid;
183  
184 # Filehandle activity modes. They are often used as list indexes.
185 sub MODE_RD () { 0 } # read
186 sub MODE_WR () { 1 } # write
187 sub MODE_EX () { 2 } # exception/expedite
188  
189 #------------------------------------------------------------------------------
190 # Kernel structure. This is the root of a large data tree. Dumping
191 # $poe_kernel with Data::Dumper or something will show most of the
192 # data that POE keeps track of. The exceptions to this are private
193 # storage in some of the leaf objects, such as POE::Wheel. All its
194 # members are described in detail further on.
195  
196 200 my $kr_id_seq = 0;
197  
198 sub KR_SESSIONS () { 0 } # [ \%kr_sessions,
199 sub KR_FILENOS () { 1 } # \%kr_filenos,
200 sub KR_SIGNALS () { 2 } # \%kr_signals,
201 sub KR_ALIASES () { 3 } # \%kr_aliases,
202 sub KR_ACTIVE_SESSION () { 4 } # \$kr_active_session,
203 sub KR_QUEUE () { 5 } # \$kr_queue,
204 sub KR_ID () { 6 } # $unique_kernel_id,
205 sub KR_SESSION_IDS () { 7 } # \%kr_session_ids,
206 sub KR_SID_SEQ () { 8 } # \$kr_sid_seq,
207 sub KR_EXTRA_REFS () { 9 } # \$kr_extra_refs,
208 sub KR_SIZE () { 10 } # XXX UNUSED ???
209 sub KR_RUN () { 11 } # \$kr_run_warning
210 sub KR_ACTIVE_EVENT () { 12 } # \$kr_active_event
211 sub KR_PIDS () { 13 } # \%kr_pids_to_events
212 sub KR_ACTIVE_EVENT_TYPE () { 14 } # \$kr_active_event_type
213 # ]
214  
215 # This flag indicates that POE::Kernel's run() method was called.
216 # It's used to warn about forgetting $poe_kernel->run().
217  
218 sub KR_RUN_CALLED () { 0x01 } # $kernel->run() called
219 sub KR_RUN_SESSION () { 0x02 } # sessions created
220 sub KR_RUN_DONE () { 0x04 } # run returned
221 200 my $kr_run_warning = 0;
222  
223 #------------------------------------------------------------------------------
224 # Events themselves.
225  
226 sub EV_SESSION () { 0 } # [ $destination_session,
227 sub EV_SOURCE () { 1 } # $sender_session,
228 sub EV_NAME () { 2 } # $event_name,
229 sub EV_TYPE () { 3 } # $event_type,
230 sub EV_ARGS () { 4 } # \@event_parameters_arg0_etc,
231 #
232 # (These fields go towards the end
233 # because they are optional in some
234 # cases. TODO: Is this still true?)
235 #
236 sub EV_OWNER_FILE () { 5 } # $caller_filename_where_enqueued,
237 sub EV_OWNER_LINE () { 6 } # $caller_line_where_enqueued,
238 sub EV_FROMSTATE () { 7 } # $fromstate
239 sub EV_SEQ () { 8 } # Maintained by POE::Queue (unique event ID)
240 sub EV_WALLTIME () { 9 } # Walltime when event was created (for alarms)
241 sub EV_DELTA () { 10 } # Seconds past walltime for event (for alarms)
242 # ]
243  
244 # These are the names of POE's internal events. They're in constants
245 # so we don't mistype them again.
246  
247 sub EN_CHILD () { '_child' }
248 sub EN_GC () { '_garbage_collect' }
249 sub EN_PARENT () { '_parent' }
250 sub EN_SCPOLL () { '_sigchld_poll' }
251 sub EN_SIGNAL () { '_signal' }
252 sub EN_START () { '_start' }
253 sub EN_STOP () { '_stop' }
254  
255 # These are POE's event classes (types). They often shadow the event
256 # names themselves, but they can encompass a large group of events.
257 # For example, ET_ALARM describes anything enqueued as by an alarm
258 # call. Types are preferred over names because bitmask tests are
259 # faster than string equality tests.
260  
261 sub ET_POST () { 0x0001 } # User events (posted, yielded).
262 sub ET_CALL () { 0x0002 } # User events that weren't enqueued.
263 sub ET_START () { 0x0004 } # _start
264 sub ET_STOP () { 0x0008 } # _stop
265 sub ET_SIGNAL () { 0x0010 } # _signal
266 sub ET_GC () { 0x0020 } # _garbage_collect
267 sub ET_PARENT () { 0x0040 } # _parent
268 sub ET_CHILD () { 0x0080 } # _child
269 sub ET_SCPOLL () { 0x0100 } # _sigchild_poll
270 sub ET_ALARM () { 0x0200 } # Alarm events.
271 sub ET_SELECT () { 0x0400 } # File activity events.
272 sub ET_SIGCLD () { 0x0800 } # sig_child() events.
273 sub ET_SIGDIE () { 0x1000 } # SIGDIE exception events.
274  
275 # A mask for all events generated by/for users.
276 sub ET_MASK_USER () { ~(ET_GC | ET_SCPOLL) }
277  
278 # A mask for all events that are delayed by a dispatch time.
279 sub ET_MASK_DELAYED () { ET_ALARM | ET_SCPOLL }
280  
281 # Temporary signal subtypes, used during signal dispatch semantics
282 # deprecation and reformation.
283  
284 sub ET_SIGNAL_RECURSIVE () { 0x2000 } # Explicitly requested signal.
285  
286 # A hash of reserved names. It's used to test whether someone is
287 # trying to use an internal event directly.
288  
289 200 my %poes_own_events = (
290 +EN_CHILD => 1,
291 +EN_GC => 1,
292 +EN_PARENT => 1,
293 +EN_SCPOLL => 1,
294 +EN_SIGNAL => 1,
295 +EN_START => 1,
296 +EN_STOP => 1,
297 +EN_STAT => 1,
298 );
299  
300 # These are ways a child may come or go.
301 # TODO - It would be useful to split 'lose' into two types. One to
302 # indicate that the child has stopped, and one to indicate that it was
303 # given away.
304  
305 sub CHILD_GAIN () { 'gain' } # The session was inherited from another.
306 sub CHILD_LOSE () { 'lose' } # The session is no longer this one's child.
307 sub CHILD_CREATE () { 'create' } # The session was created as a child of this.
308  
309 # Argument offsets for different types of internally generated events.
310 # TODO Exporting (EXPORT_OK) these would let people stop depending on
311 # positions for them.
312  
313 sub EA_SEL_HANDLE () { 0 }
314 sub EA_SEL_MODE () { 1 }
315 sub EA_SEL_ARGS () { 2 }
316  
317 #------------------------------------------------------------------------------
318 # Debugging and configuration constants.
319  
320 # Shorthand for defining a trace constant.
321 sub _define_trace {
322 201 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
323 200 ▶ foreach my $name (@_) {
Subroutine _define_trace: called CC=3 SCAR=11.3
324 1600 ▶ next if defined *{"TRACE_$name"}{CODE};
Branch: 1 branch
BranchTrueFalseTotal
if defined *{"TRACE_$name";}{"CODE"} 0 1600 1600
325 1600 my $trace_value = &TRACE_DEFAULT;
326 1600 my $trace_name = "TRACE_$name";
327 1600 ▶ *$trace_name = sub () { $trace_value };
Subroutine __ANON__: not called CC=1 SCAR=1.2
328 }
329 }
330  
331 # Debugging flags for subsystems. They're done as double evals here
332 # so that someone may define them before using POE::Kernel (or POE),
333 # and the pre-defined value will take precedence over the defaults
334 # here.
335  
336 200 my $trace_file_handle;
337  
338 BEGIN {
339 # Shorthand for defining an assert constant.
340 sub _define_assert {
341 201 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
342 200 ▶ foreach my $name (@_) {
Subroutine _define_assert: called CC=3 SCAR=11.0
343 1000 ▶ next if defined *{"ASSERT_$name"}{CODE};
Branch: 1 branch
BranchTrueFalseTotal
if defined *{"ASSERT_$name";}{"CODE"} 3 997 1000
344 997 my $assert_value = &ASSERT_DEFAULT;
345 997 my $assert_name = "ASSERT_$name";
346 997 ▶ *$assert_name = sub () { $assert_value };
Subroutine __ANON__: not called CC=1 SCAR=1.2
347 }
348 }
349  
350 # Assimilate POE_TRACE_* and POE_ASSERT_* environment variables.
351 # Environment variables override everything else.
352 201 ▶ while (my ($var, $val) = each %ENV) {
Subroutine BEGIN: called CC=11 SCAR=24.5
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %ENV) 5955 200 6155
353 5955 ▶ next unless $var =~ /^POE_([A-Z_]+)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $var =~ /^POE_([A-Z_]+)$/ 5944 11 5955
354  
355 11 my $const = $1;
356  
357 201 ▶ next unless $const =~ /^(?:TRACE|ASSERT)_/ or do { no strict 'refs'; defined &$const };
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless $const =~ /^(?:TRACE|ASSERT)_/ or do { no strict 'refs'; defined &$const } 10 1 11
Condition66%2/3
$const =~ /^(?:TRACE|ASSERT)_/ or do { no strict 'refs'; defined &$const }
l!l&&r!l&&!r
0110
MC/DC50%1/2
$const =~ /^(?:TRACE|ASSERT)_/ or do { no strict 'refs'; defined &$const }
$const =~ /^(?:TRACE|ASSERT)_/ do { no strict 'refs'; defined &$const }
Truth table66%2/3
A or B
A= $const =~ /^(?:TRACE|ASSERT)_/B= do { no strict 'refs'; defined &$const }
ABresult
1X1
011
000
358  
359 # Copy so we don't hurt our environment.
360 1 my $value = $val;
361 1 ($value) = ($value =~ /^([-\@\w.]+)$/); # Untaint per rt.cpan.org 81550
362 1 $value =~ tr['"][]d;
363 1 ▶ $value = 0 + $value if $value =~ /^\s*-?\d+(?:\.\d+)?\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /^\s*-?\d+(?:\.\d+)?\s*$/ 1 0 1
364  
365 201 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
366 1 local $^W = 0;
367 1 ▶ local $SIG{__WARN__} = sub { }; # redefine
Subroutine __ANON__: not called CC=1 SCAR=0.0
368 1 ▶ *$const = sub () { $value };
Subroutine __ANON__: not called CC=1 SCAR=1.2
369 }
370  
371 # TRACE_FILENAME is special.
372 {
373 201 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
374 200 ▶ my $trace_filename = TRACE_FILENAME() if defined &TRACE_FILENAME;
Branch: 1 branch
BranchTrueFalseTotal
if defined &POE::Kernel::TRACE_FILENAME 0 200 200
375 200 ▶ if (defined $trace_filename) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $trace_filename) 0 200 200
376 0 ▶ open $trace_file_handle, ">$trace_filename"
Branch: 1 branch
BranchTrueFalseTotal
open $trace_file_handle, ">$trace_filename" or die "can't open trace file `${trace_filename}': $!" 0 0 0
377 or die "can't open trace file `$trace_filename': $!";
378 0 CORE::select((CORE::select($trace_file_handle), $| = 1)[0]);
379 }
380 }
381 # TRACE_DEFAULT changes the default value for other TRACE_*
382 # constants. Since define_trace() uses TRACE_DEFAULT internally, it
383 # can't be used to define TRACE_DEFAULT itself.
384  
385 200 ▶ defined &TRACE_DEFAULT or *TRACE_DEFAULT = sub () { 0 };
Branch: 1 branch
BranchTrueFalseTotal
defined &POE::Kernel::TRACE_DEFAULT or *TRACE_DEFAULT = sub () { 0 } 88 112 200
386  
387 200 _define_trace qw(
388 EVENTS FILES PROFILE REFCNT RETVALS SESSIONS SIGNALS STATISTICS
389 );
390  
391 # See the notes for TRACE_DEFAULT, except read ASSERT and assert
392 # where you see TRACE and trace.
393  
394 200 ▶ defined &ASSERT_DEFAULT or *ASSERT_DEFAULT = sub () { 0 };
Branch: 1 branch
BranchTrueFalseTotal
defined &POE::Kernel::ASSERT_DEFAULT or *ASSERT_DEFAULT = sub () { 0 } 68 132 200
395  
396 200 _define_assert qw(DATA EVENTS FILES RETVALS USAGE);
397 }
398  
399 # An "idle" POE::Kernel may still have events enqueued. These events
400 # regulate polling for signals, profiling, and perhaps other aspects of
401 # POE::Kernel's internal workings.
402 #
403 # XXX - There must be a better mechanism.
404 #
405 my $idle_queue_size;
406  
407 3 ▶sub _idle_queue_grow { $idle_queue_size++; }
Subroutine _idle_queue_grow: called CC=1 SCAR=0.0
408 2 ▶sub _idle_queue_shrink { $idle_queue_size--; }
Subroutine _idle_queue_shrink: called CC=1 SCAR=0.0
409 5 ▶sub _idle_queue_size { $idle_queue_size; }
Subroutine _idle_queue_size: called CC=1 SCAR=0.0
410 196 ▶sub _idle_queue_reset { $idle_queue_size = 0; }
Subroutine _idle_queue_reset: called CC=1 SCAR=0.0
411  
412 #------------------------------------------------------------------------------
413 # Helpers to carp, croak, confess, cluck, warn and die with whatever
414 # trace file we're using today. _trap is reserved for internal
415 # errors.
416  
417 sub _trap {
418 24 ▶ local $Carp::CarpLevel = $Carp::CarpLevel + 1;
Subroutine _trap: called CC=2 SCAR=7.6
419 24 ▶ local *STDERR = $trace_file_handle || *STDERR;
Condition33%1/3
$trace_file_handle || *STDERR
l!l&&r!l&&!r
0240
MC/DC0%0/2
$trace_file_handle || *STDERR
$trace_file_handle *STDERR
Truth table33%1/3
A || B
A= $trace_file_handleB= *STDERR
ABresult
1X1
011
000
420  
421 24 confess(
422 "=== $$ === Please address any warnings or errors above this message,\n",
423 "=== $$ === and try again. If there are no previous messages, or they\n",
424 "=== $$ === are from within POE, then please mail them along with the\n",
425 "=== $$ === following information to bug-POE\@rt.cpan.org:\n",
426 "---\n@_\n-----\n"
427 );
428 }
429  
430 sub _croak {
431 1 ▶ local $Carp::CarpLevel = $Carp::CarpLevel + 1;
Subroutine _croak: called CC=2 SCAR=7.2
432 1 ▶ local *STDERR = $trace_file_handle || *STDERR;
Condition33%1/3
$trace_file_handle || *STDERR
l!l&&r!l&&!r
010
MC/DC0%0/2
$trace_file_handle || *STDERR
$trace_file_handle *STDERR
Truth table33%1/3
A || B
A= $trace_file_handleB= *STDERR
ABresult
1X1
011
000
433 1 my $message = join("", @_);
434 1 $message =~ s/^/=== $$ === /mg;
435 1 croak $message;
436 }
437  
438 sub _confess {
439 101 ▶ local $Carp::CarpLevel = $Carp::CarpLevel + 1;
Subroutine _confess: called CC=2 SCAR=7.2
440 101 ▶ local *STDERR = $trace_file_handle || *STDERR;
Condition33%1/3
$trace_file_handle || *STDERR
l!l&&r!l&&!r
01010
MC/DC0%0/2
$trace_file_handle || *STDERR
$trace_file_handle *STDERR
Truth table33%1/3
A || B
A= $trace_file_handleB= *STDERR
ABresult
1X1
011
000
441 101 my $message = join("", @_);
442 101 $message =~ s/^/=== $$ === /mg;
443 101 confess $message;
444 }
445  
446 sub _cluck {
447 5106 ▶ local $Carp::CarpLevel = $Carp::CarpLevel + 1;
Subroutine _cluck: called CC=2 SCAR=7.2
448 5106 ▶ local *STDERR = $trace_file_handle || *STDERR;
Condition33%1/3
$trace_file_handle || *STDERR
l!l&&r!l&&!r
051060
MC/DC0%0/2
$trace_file_handle || *STDERR
$trace_file_handle *STDERR
Truth table33%1/3
A || B
A= $trace_file_handleB= *STDERR
ABresult
1X1
011
000
449 5106 my $message = join("", @_);
450 5106 $message =~ s/^/=== $$ === /mg;
451 5106 cluck $message;
452 }
453  
454 sub _carp {
455 4803 ▶ local $Carp::CarpLevel = $Carp::CarpLevel + 1;
Subroutine _carp: called CC=2 SCAR=7.2
456 4803 ▶ local *STDERR = $trace_file_handle || *STDERR;
Condition33%1/3
$trace_file_handle || *STDERR
l!l&&r!l&&!r
048030
MC/DC0%0/2
$trace_file_handle || *STDERR
$trace_file_handle *STDERR
Truth table33%1/3
A || B
A= $trace_file_handleB= *STDERR
ABresult
1X1
011
000
457 4803 my $message = join("", @_);
458 4803 $message =~ s/^/=== $$ === /mg;
459 4803 carp $message;
460 }
461  
462 sub _warn {
463 56887 ▶ my ($package, $file, $line) = caller();
Subroutine _warn: called CC=2 SCAR=6.9
464 56887 my $message = join("", @_);
465 56887 ▶ $message .= " at $file line $line\n" unless $message =~ /\n$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $message =~ /\n$/ 30463 26424 56887
466 56887 $message =~ s/^/=== $$ === /mg;
467 56887 warn $message;
468 }
469  
470 sub _die {
471 1 ▶ my ($package, $file, $line) = caller();
Subroutine _die: called CC=3 SCAR=11.6
472 1 my $message = join("", @_);
473 1 ▶ $message .= " at $file line $line\n" unless $message =~ /\n$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $message =~ /\n$/ 1 0 1
474 1 $message =~ s/^/=== $$ === /mg;
475 1 ▶ local *STDERR = $trace_file_handle || *STDERR;
Condition33%1/3
$trace_file_handle || *STDERR
l!l&&r!l&&!r
010
MC/DC0%0/2
$trace_file_handle || *STDERR
$trace_file_handle *STDERR
Truth table33%1/3
A || B
A= $trace_file_handleB= *STDERR
ABresult
1X1
011
000
476 1 die $message;
477 }
478  
479 #------------------------------------------------------------------------------
480 # Adapt POE::Kernel's personality to whichever event loop is present.
481  
482 200 my @has_poe_loop;
483 sub _find_loop {
484 53499 ▶ my ($mod) = @_;
Subroutine _find_loop: called CC=5 SCAR=16.1
485  
486 # Turns O(M*N) into O(M+N). I've seen the old way take over 30
487 # seconds according to Devel::NYTProf, with egregiously long @INCs.
488 53499 ▶ unless (@has_poe_loop) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@has_poe_loop) 194 53305 53499
489 @has_poe_loop = (
490 194 ▶ grep { (-d "$_/POE/Loop") || (-d "$_/POE/XS/Loop") }
Branch: 1 branch
BranchTrueFalseTotal
-d "$_/POE/Loop" or -d "$_/POE/XS/Loop" 1266 432 1698
491 @INC
492 );
493 }
494  
495 53499 foreach my $dir (@has_poe_loop) {
496 117445 ▶ return 1 if (-r "$dir/$mod");
Branch: 1 branch
BranchTrueFalseTotal
if -r "$dir/$mod" 51 117394 117445
497 }
498  
499 53448 return 0;
500 }
501  
502 sub _load_loop {
503 200 ▶ my $loop = shift;
Subroutine _load_loop: called CC=3 SCAR=11.0
504  
505 200 ▶ *poe_kernel_loop = sub { return "$loop" };
Subroutine __ANON__: called CC=1 SCAR=0.0
506  
507 # Modules can die with "not really dying" if they've loaded
508 # something else. This exception prevents the rest of the
509 # originally used module from being parsed, so the module it's
510 # handed off to takes over.
511 200 eval "require $loop";
512 200 ▶ if ($@ and $@ !~ /not really dying/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ and not $@ =~ /not really dying/) 2 198 200
Condition66%2/3
$@ and not $@ =~ /not really dying/
!ll&&!rl&&r
19802
MC/DC50%1/2
$@ and not $@ =~ /not really dying/
$@ not $@ =~ /not really dying/
Truth table66%2/3
A and not(B)
A= $@B= not $@ =~ /not really dying/
ABresult
0X0
100
111
513 2 die(
514 "*\n",
515 "* POE can't use $loop:\n",
516 "* $@\n",
517 "*\n",
518 );
519 }
520 }
521  
522 sub _test_loop {
523 200 ▶ my $used_first = shift;
Subroutine _test_loop: called CC=11 SCAR=24.4
524 200 local $SIG{__DIE__};
525  
526 # First see if someone wants to load a POE::Loop or XS version
527 # explicitly.
528 200 ▶ if (defined $used_first) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $used_first) 6 194 200
529 6 _load_loop($used_first);
530 4 return;
531 }
532  
533 194 foreach my $file (keys %INC) {
534 27066 ▶ next if (substr ($file, -3) ne '.pm');
Branch: 1 branch
BranchTrueFalseTotal
if substr($file, -3) ne ".pm" 388 26678 27066
535 26678 my @split_dirs = File::Spec->splitdir($file);
536  
537 # Create a module name by replacing the path separators with
538 # underscores and removing ".pm"
539 26678 my $module = join("_", @split_dirs);
540 26678 substr($module, -3) = "";
541  
542 # Skip the module name if it isn't legal.
543 26678 ▶ next if $module =~ /[^\w\.]/;
Branch: 1 branch
BranchTrueFalseTotal
if $module =~ /[^\w\.]/ 0 26678 26678
544  
545 # Try for the XS version first. If it fails, try the plain
546 # version. If that fails, we're up a creek.
547 26678 $module = "POE/XS/Loop/$module.pm";
548 26678 ▶ unless (_find_loop($module)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_find_loop($module)) 26678 0 26678
549 26678 $module =~ s|XS/||;
550 26678 ▶ next unless (_find_loop($module));
Branch: 1 branch
BranchTrueFalseTotal
unless _find_loop($module) 26627 51 26678
551 }
552  
553 51 ▶ if (defined $used_first and $used_first ne $module) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $used_first and $used_first ne $module) 0 51 51
Condition33%1/3
defined $used_first and $used_first ne $module
!ll&&!rl&&r
5100
MC/DC0%0/2
defined $used_first and $used_first ne $module
defined $used_first $used_first ne $module
Truth table33%1/3
A and B
A= defined $used_firstB= $used_first ne $module
ABresult
0X0
100
111
554 0 die(
555 "*\n",
556 "* POE can't use multiple event loops at once.\n",
557 "* You used $used_first and $module.\n",
558 "* Specify the loop you want as an argument to POE\n",
559 "* use POE qw(Loop::Select);\n",
560 "* or;\n",
561 "* use POE::Kernel { loop => 'Select' };\n",
562 "*\n",
563 );
564 }
565  
566 51 $used_first = $module;
567 }
568  
569 # No loop found. Default to our internal select() loop.
570 194 ▶ unless (defined $used_first) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $used_first) 143 51 194
571 143 $used_first = "POE/XS/Loop/Select.pm";
572 143 ▶ unless (_find_loop($used_first)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_find_loop($used_first)) 143 0 143
573 143 $used_first =~ s/XS\///;
574 }
575 }
576  
577 194 substr($used_first, -3) = "";
578 194 $used_first =~ s|/|::|g;
579 194 _load_loop($used_first);
580 }
581  
582 #------------------------------------------------------------------------------
583 # Include resource modules here. Later, when we have the option of XS
584 # versions, we'll adapt this to include them if they're available.
585  
586 200 ▶use POE::Resources;
Subroutine BEGIN: called CC=1 SCAR=0.0
587  
588 ###############################################################################
589 # Helpers.
590  
591 ### Resolve $whatever into a session reference, trying every method we
592 ### can until something succeeds.
593  
594 sub _resolve_session {
595 4069 ▶ my ($self, $whatever) = @_;
Subroutine _resolve_session: called CC=5 SCAR=16.1
596 4069 my $session;
597  
598 # Resolve against sessions.
599 4069 $session = $self->_data_ses_resolve($whatever);
600 4069 ▶ return $session if defined $session;
Branch: 1 branch
BranchTrueFalseTotal
if defined $session 3862 207 4069
601  
602 # Resolve against IDs.
603 207 $session = $self->_data_sid_resolve($whatever);
604 207 ▶ return $session if defined $session;
Branch: 1 branch
BranchTrueFalseTotal
if defined $session 83 124 207
605  
606 # Resolve against aliases.
607 124 $session = $self->_data_alias_resolve($whatever);
608 124 ▶ return $session if defined $session;
Branch: 1 branch
BranchTrueFalseTotal
if defined $session 118 6 124
609  
610 # Resolve against the Kernel itself. Use "eq" instead of "==" here
611 # because $whatever is often a string.
612 6 ▶ return $whatever if $whatever eq $self;
Branch: 1 branch
BranchTrueFalseTotal
if $whatever eq $self 0 6 6
613  
614 # We don't know what it is.
615 6 return undef;
616 }
617  
618 ### Test whether POE has become idle.
619  
620 sub _test_if_kernel_is_idle {
621 2856 ▶ my $self = shift;
Subroutine _test_if_kernel_is_idle: called CC=7 SCAR=19.5
622  
623 2856 if (TRACE_REFCNT) {
624 _warn(
625 "<rc> ,----- Kernel Activity -----\n",
626 "<rc> | Events : ", $kr_queue->get_item_count(),
627 " (vs. idle size = ", $idle_queue_size, ")\n",
628 "<rc> | Files : ", $self->_data_handle_count(), "\n",
629 "<rc> | Extra : ", $self->_data_extref_count(), "\n",
630 "<rc> | Procs : ", $self->_data_sig_kernel_awaits_pids(), "\n",
631 "<rc> | Sess : ", $self->_data_ses_count(), "\n",
632 "<rc> `---------------------------\n",
633 "<rc> ..."
634 );
635 }
636  
637 2856 if( ASSERT_DATA ) {
638 if( $kr_pid != $$ ) {
639 _trap(
640 "New process detected. " .
641 "You must call ->has_forked() in the child process."
642 );
643 }
644 }
645  
646 # Not yet idle, or SO idle that there's nothing to receive the
647 # event. Try to order these from most to least likely to be true so
648 # that the tests short-circuit quickly.
649  
650 return if (
651 2856 ▶ $kr_queue->get_item_count() > $idle_queue_size or
Branch: 1 branch
BranchTrueFalseTotal
if $kr_queue->get_item_count > $idle_queue_size or $self->_data_handle_count or $self->_data_extref_count or $self->_data_sig_kernel_awaits_pids or not $self->_data_ses_count 0 1891 1891
Condition91%11/12
$kr_queue->get_item_count > $idle_queue_size or $self->_data_handle_count or $self->_data_extref_count or $self->_data_sig_kernel_awaits_pids or not $self->_data_ses_count
l!l&&r!l&&!r
26540202
$kr_queue->get_item_count > $idle_queue_size or $self->_data_handle_count or $self->_data_extref_count or $self->_data_sig_kernel_awaits_pids
l!l&&r!l&&!r
26468202
$kr_queue->get_item_count > $idle_queue_size or $self->_data_handle_count or $self->_data_extref_count
l!l&&r!l&&!r
263214210
$kr_queue->get_item_count > $idle_queue_size or $self->_data_handle_count
l!l&&r!l&&!r
2089543224
MC/DC80%4/5
$kr_queue->get_item_count > $idle_queue_size or $self->_data_handle_count or $self->_data_extref_count or $self->_data_sig_kernel_awaits_pids or not $self->_data_ses_count
$kr_queue->get_item_count > $idle_queue_size $self->_data_handle_count $self->_data_extref_count $self->_data_sig_kernel_awaits_pids not $self->_data_ses_count
Truth table83%5/6
A or B or C or D or not(E)
A= $kr_queue->get_item_count > $idle_queue_sizeB= $self->_data_handle_countC= $self->_data_extref_countD= $self->_data_sig_kernel_awaits_pidsE= not $self->_data_ses_count
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
652 $self->_data_handle_count() or
653 $self->_data_extref_count() or
654 $self->_data_sig_kernel_awaits_pids() or
655 !$self->_data_ses_count()
656 );
657  
658 1328 $self->_data_ev_enqueue(
659 $self, $self, EN_SIGNAL, ET_SIGNAL, [ 'IDLE' ],
660 __FILE__, __LINE__, undef
661 );
662 }
663  
664 ### Explain why a session could not be resolved.
665  
666 sub _explain_resolve_failure {
667 569 ▶ my ($self, $whatever, $nonfatal) = @_;
Subroutine _explain_resolve_failure: called CC=2 SCAR=7.6
668 1381 local $Carp::CarpLevel = 2;
669  
670 87 if (ASSERT_DATA and !$nonfatal) {
671 _trap "<dt> Cannot resolve ``$whatever'' into a session reference";
672 }
673  
674 4 $! = ESRCH;
675 0 TRACE_RETVALS and _carp "<rv> session not resolved: $!";
676 0 ASSERT_RETVALS and _carp "<rv> session not resolved: $!";
677 }
678  
679 ### Explain why a function is returning unsuccessfully.
680  
681 sub _explain_return {
682 15 ▶ my ($self, $message) = @_;
Subroutine _explain_return: called CC=1 SCAR=0.0
683 15 local $Carp::CarpLevel = 2;
684  
685 15 ASSERT_RETVALS and _confess "<rv> $message";
686 6 TRACE_RETVALS and _carp "<rv> $message";
687 }
688  
689 ### Explain how the user made a mistake calling a function.
690  
691 sub _explain_usage {
692 25 ▶ my ($self, $message) = @_;
Subroutine _explain_usage: called CC=1 SCAR=0.0
693 25 local $Carp::CarpLevel = 2;
694  
695 25 ASSERT_USAGE and _confess "<us> $message";
696 13 ASSERT_RETVALS and _confess "<rv> $message";
697 1 TRACE_RETVALS and _carp "<rv> $message";
698 }
699  
700 #==============================================================================
701 # SIGNALS
702 #==============================================================================
703  
704 #------------------------------------------------------------------------------
705 # Register or remove signals.
706  
707 # Public interface for adding or removing signal handlers.
708  
709 sub sig {
710 280 ▶ my ($self, $signal, $event_name, @args) = ($poe_kernel, @_[1..$#_]);
Subroutine sig: called CC=6 SCAR=17.9
Pod documented
711  
712 280 if (ASSERT_USAGE) {
713 _confess "<us> must call sig() from a running session"
714 if $kr_active_session == $self;
715 _confess "<us> undefined signal in sig()" unless defined $signal;
716 _carp(
717 "<us> The '$event_name' event is one of POE's own. Its " .
718 "effect cannot be achieved assigning it to a signal"
719 ) if defined($event_name) and exists($poes_own_events{$event_name});
720 };
721  
722 280 ▶ if (defined $event_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $event_name) { } 16 443 459
723 278 $self->_data_sig_add($kr_active_session, $signal, $event_name, \@args);
724 }
725 else {
726 275 $self->_data_sig_remove($kr_active_session->ID, $signal);
727 }
728 }
729  
730 # Public interface for posting signal events.
731 # TODO - Like post(), signal() should return
732  
733 sub signal {
734 295 ▶ my ($self, $dest_session, $signal, @etc) = ($poe_kernel, @_[1..$#_]);
Subroutine signal: called CC=4 SCAR=13.9
Pod documented
735  
736 175 if (ASSERT_USAGE) {
737 _confess "<us> undefined destination in signal()"
738 unless defined $dest_session;
739 _confess "<us> undefined signal in signal()" unless defined $signal;
740 };
741  
742 141 my $session = $self->_resolve_session($dest_session);
743 20 ▶ unless (defined $session) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $session) 3 293 296
744 17 $self->_explain_resolve_failure($dest_session);
745 16 return;
746 }
747  
748 $self->_data_ev_enqueue(
749 2 $session, $kr_active_session,
750 EN_SIGNAL, ET_SIGNAL, [ $signal, @etc ],
751 (caller)[1,2], $kr_active_event
752 );
753 2 return 1;
754 }
755  
756 # Public interface for flagging signals as handled. This will replace
757 # the handlers' return values as an implicit flag. Returns undef so
758 # it may be used as the last function in an event handler.
759  
760 sub sig_handled {
761 140 ▶ my $self = $poe_kernel;
Subroutine sig_handled: called CC=2 SCAR=7.0
Pod documented
762 140 $self->_data_sig_handled();
763  
764 124 ▶ if ($kr_active_event eq EN_SIGNAL) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kr_active_event eq "_signal") 1 414 415
765 0 _die(
766 ",----- DEPRECATION ERROR -----\n",
767 "| ", $self->_data_alias_loggable($kr_active_session->ID), ":\n",
768 "| handled a _signal event. You must register a handler with sig().\n",
769 "`-----------------------------\n",
770 );
771 }
772 }
773  
774 # Attach a window or widget's destroy/closure to the UIDESTROY signal.
775  
776 sub signal_ui_destroy {
777 0 ▶ my ($self, $window) = @_;
Subroutine signal_ui_destroy: not called CC=1 SCAR=6.9
Pod documented
778 0 $self->loop_attach_uidestroy($window);
779 }
780  
781 # Handle child PIDs being reaped. Added 2006-09-15.
782  
783 sub sig_child {
784 231 ▶ my ($self, $pid, $event_name, @args) = ($poe_kernel, @_[1..$#_]);
Subroutine sig_child: called CC=7 SCAR=19.6
Pod documented
785  
786 231 if (ASSERT_USAGE) {
787 _confess "<us> must call sig_chld() from a running session"
788 if $kr_active_session == $self;
789 _confess "<us> undefined process ID in sig_chld()" unless defined $pid;
790 _carp(
791 "<us> The '$event_name' event is one of POE's own. Its " .
792 "effect cannot be achieved assigning it to a signal"
793 ) if defined($event_name) and exists($poes_own_events{$event_name});
794 };
795  
796 231 ▶ if (defined $event_name) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $event_name) { } 252 269 521
elsif ($self->_data_sig_pids_is_ses_watching($kr_active_session->ID, $pid)) { } 0 133 133
797 231 $self->_data_sig_pid_watch($kr_active_session, $pid, $event_name, \@args);
798 }
799 elsif ($self->_data_sig_pids_is_ses_watching($kr_active_session->ID, $pid)) {
800 133 $self->_data_sig_pid_ignore($kr_active_session->ID, $pid);
801 }
802 }
803  
804 #==============================================================================
805 # KERNEL
806 #==============================================================================
807  
808 sub new {
809 331 ▶ my $type = shift;
Subroutine new: called CC=2 SCAR=6.9
Pod documented
810  
811 # Prevent multiple instances, no matter how many times it's called.
812 # This is a backward-compatibility enhancement for programs that
813 # have used versions prior to 0.06. It also provides a convenient
814 # single entry point into the entirety of POE's state: point a
815 # Dumper module at it, and you'll see a hideous tree of knowledge.
816 # Be careful, though. Its apples bite back.
817 330 ▶ unless (defined $poe_kernel) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $POE::Kernel::poe_kernel) 198 133 331
818  
819 # Create our master queue.
820 198 $kr_queue = $queue_class->new();
821  
822 # Remember the PID
823 198 $kr_pid = $$;
824  
825 # TODO - Should KR_ACTIVE_SESSIONS and KR_ACTIVE_EVENT be handled
826 # by POE::Resource::Sessions?
827 # TODO - Should the subsystems be split off into separate real
828 # objects, such as KR_QUEUE is?
829  
830 198 my $self = $poe_kernel = bless [
831 undef, # KR_SESSIONS - from POE::Resource::Sessions
832 undef, # KR_FILENOS - from POE::Resource::FileHandles
833 undef, # KR_SIGNALS - from POE::Resource::Signals
834 undef, # KR_ALIASES - from POE::Resource::Aliases
835 \$kr_active_session, # KR_ACTIVE_SESSION
836 $kr_queue, # KR_QUEUE - reference to an object
837 undef, # KR_ID
838 undef, # KR_SESSION_IDS - from POE::Resource::SIDS
839 undef, # KR_SID_SEQ - from POE::Resource::SIDS
840 undef, # KR_EXTRA_REFS
841 undef, # KR_SIZE
842 \$kr_run_warning, # KR_RUN
843 \$kr_active_event, # KR_ACTIVE_EVENT
844 undef, # KR_PIDS
845 \$kr_active_event_type, # KR_ACTIVE_EVENT_TYPE
846 ], $type;
847  
848 198 POE::Resources->load();
849  
850 198 $self->_recalc_id();
851 198 $self->_data_sid_set($self->[KR_ID], $self);
852  
853 # Initialize subsystems. The order is important.
854  
855 # We need events before sessions, and the kernel's session before
856 # it can start polling for signals.
857 198 $self->_data_ev_initialize($kr_queue);
858 198 $self->_initialize_kernel_session();
859 197 $self->_data_sig_initialize();
860 196 $self->_data_alias_initialize();
861  
862 # These other subsystems don't have strange interactions.
863 196 $self->_data_handle_initialize($kr_queue);
864  
865 196 _idle_queue_reset();
866 }
867  
868 # Return the global instance.
869 196 $poe_kernel;
870 }
871  
872 sub CLONE {
873 0 ▶ _data_ses_clone();
Subroutine CLONE: not called CC=1 SCAR=6.9
874 }
875  
876 #------------------------------------------------------------------------------
877 # Send an event to a session right now. Used by _disp_select to
878 # expedite select() events, and used by run() to deliver posted events
879 # from the queue.
880  
881 # Dispatch an event to its session. A lot of work goes on here.
882  
883 259 ▶sub _dummy_sigdie_handler { 1 }
Subroutine _dummy_sigdie_handler: called CC=1 SCAR=0.0
884  
885 sub _dispatch_signal_event {
886 my (
887 494 ▶ $self,
Subroutine _dispatch_signal_event: called CC=14 SCAR=26.4
888 $session, $source_session, $event, $type, $etc,
889 $file, $line, $fromstate, $priority, $seq
890 ) = @_;
891  
892 # TODO - Regrettably, duplicate checking code in:
893 # _dispatch_signal_event(), _dispatch_event().
894  
895 494 if (ASSERT_EVENTS) {
896 _confess "<ev> undefined dest session" unless defined $session;
897 _confess "<ev> undefined source session" unless defined $source_session;
898 };
899  
900 494 if (TRACE_EVENTS) {
901 my $log_session = $session;
902 $log_session = $self->_data_alias_loggable($session->ID) unless (
903 $type & ET_START
904 );
905 my $string_etc = join(" ", map { defined() ? $_ : "(undef)" } @$etc);
906 _warn(
907 "<ev> Dispatching event $seq ``$event'' ($string_etc) from ",
908 $self->_data_alias_loggable($source_session->ID), " to $log_session"
909 );
910 }
911  
912 494 my $signal = $etc->[0];
913  
914 494 if (TRACE_SIGNALS) {
915 _warn(
916 "<sg> dispatching ET_SIGNAL ($signal) to ",
917 $self->_data_alias_loggable($session->ID)
918 );
919 }
920  
921 # Step 1a: Reset the handled-signal flags.
922  
923 494 local @POE::Kernel::kr_signaled_sessions;
924 494 local $POE::Kernel::kr_signal_total_handled;
925 494 local $POE::Kernel::kr_signal_type;
926  
927 637 $self->_data_sig_reset_handled($signal);
928  
929 # Step 1b: Collect a list of sessions to receive the signal.
930  
931 494 my @touched_sessions = ($session);
932 494 my $touched_index = 0;
933 494 ▶ while ($touched_index < @touched_sessions) {
Branch: 1 branch
BranchTrueFalseTotal
if ($touched_index < @touched_sessions) 673 492 1165
934 905 my $next_target = $touched_sessions[$touched_index]->ID;
935 905 push @touched_sessions, $self->_data_ses_get_children($next_target);
936 1059 $touched_index++;
937 }
938  
939 # Step 1c: The DIE signal propagates up through parents, too.
940  
941 648 ▶ if ($signal eq "DIE") {
Branch: 1 branch
BranchTrueFalseTotal
if ($signal eq "DIE") 6 489 495
942 524 my $next_target = $self->_data_ses_get_parent($session->ID);
943 370 ▶ while (defined($next_target) and $next_target != $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $next_target and $next_target != $self) 521 171 692
Condition100%3/3
defined $next_target and $next_target != $self
!ll&&!rl&&r
1212901
MC/DC100%2/2
defined $next_target and $next_target != $self
defined $next_target $next_target != $self
Truth table100%3/3
A and B
A= defined $next_targetB= $next_target != $self
ABresult
0X0
100
111
944 202 unshift @touched_sessions, $next_target;
945 202 $next_target = $self->_data_ses_get_parent($next_target->ID);
946 }
947 }
948  
949 # Step 2: Propagate the signal to the explicit watchers in the
950 # child tree. Ensure the full tree is touched regardless
951 # whether there are explicit watchers.
952  
953 819 ▶ if ($self->_data_sig_explicitly_watched($signal)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_data_sig_explicitly_watched($signal)) { } 378 259 637
954 728 my %signal_watchers = $self->_data_sig_watchers($signal);
955  
956 890 $touched_index = @touched_sessions;
957 400 ▶ while ($touched_index--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($touched_index--) 733 235 968
958 210 my $target_session = $touched_sessions[$touched_index];
959 210 $self->_data_sig_touched_session($target_session);
960  
961 367 my $target_sid = $target_session->ID;
962 367 ▶ next unless exists $signal_watchers{$target_sid};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $signal_watchers{$target_sid} 177 234 411
963 557 my ($target_event, $target_etc) = @{$signal_watchers{$target_sid}};
964  
965 364 if (TRACE_SIGNALS) {
966 _warn(
967 "<sg> propagating explicit signal $target_event ($signal) ",
968 "(@$target_etc) to ", $self->_data_alias_loggable($target_sid)
969 );
970 }
971  
972 # ET_SIGNAL_RECURSIVE is used here to avoid repropagating
973 # the signal ad nauseam.
974 $self->_dispatch_event(
975 364 $target_session, $self,
976 $target_event, ET_SIGNAL_RECURSIVE | $type, [ @$etc, @$target_etc ],
977 $file, $line, $fromstate, monotime(), -__LINE__
978 );
979 }
980 }
981 else {
982 654 $touched_index = @touched_sessions;
983 654 ▶ while ($touched_index--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($touched_index--) 817 260 1077
984 891 $self->_data_sig_touched_session($touched_sessions[$touched_index]);
985 }
986 }
987  
988 # Step 3: Check to see if the signal was handled.
989  
990 524 $self->_data_sig_free_terminated_sessions();
991  
992 # If the signal was SIGDIE, then propagate the exception.
993  
994 298 my $handled_session_count = (_data_sig_handled_status())[0];
995 463 ▶ if ($signal eq "DIE" and !$handled_session_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($signal eq "DIE" and not $handled_session_count) 314 334 648
Condition100%3/3
$signal eq "DIE" and not $handled_session_count
!ll&&!rl&&r
49022
MC/DC100%2/2
$signal eq "DIE" and not $handled_session_count
$signal eq "DIE" not $handled_session_count
Truth table100%3/3
A and not(B)
A= $signal eq "DIE"B= not $handled_session_count
ABresult
0X0
100
111
996 $kr_exception = $etc->[1]{error_str} . (
997 338 ▶ (defined $kr_exception)
Branch: 1 branch
BranchTrueFalseTotal
defined $POE::Kernel::kr_exception ? : 395 160 555
998 ? "Additional error thrown in handler for previous error:\n$kr_exception"
999 : ''
1000 );
1001 }
1002  
1003 # Signal completely dispatched. Thanks for flying!
1004 460 return;
1005 }
1006  
1007 sub _dispatch_event {
1008 my (
1009 8478 ▶ $self,
Subroutine _dispatch_event: called CC=21 SCAR=30.5
1010 $session, $source_session, $event, $type, $etc,
1011 $file, $line, $fromstate, $priority, $seq
1012 ) = @_;
1013  
1014 8515 if (ASSERT_EVENTS) {
1015 _confess "<ev> undefined dest session" unless defined $session;
1016 _confess "<ev> undefined source session" unless defined $source_session;
1017 };
1018  
1019 8353 if (TRACE_EVENTS) {
1020 my $log_session = $session;
1021 $log_session = $self->_data_alias_loggable($session->ID) unless (
1022 $type & ET_START
1023 );
1024 my $string_etc = join(" ", map { defined() ? $_ : "(undef)" } @$etc);
1025 _warn(
1026 "<ev> Dispatching event $seq ``$event'' ($string_etc) from ",
1027 $self->_data_alias_loggable($source_session->ID), " to $log_session"
1028 );
1029 }
1030  
1031 ### Pre-dispatch processing.
1032  
1033 # Some sessions don't do anything in _start and expect their
1034 # creators to provide a start-up event. This means we can't
1035 # &_collect_garbage at _start time. Instead, an ET_GC event is
1036 # posted as part of session allocation. Simply dispatching it
1037 # will trigger a GC sweep.
1038  
1039 8609 ▶ return 0 if $type & ET_GC;
Branch: 1 branch
BranchTrueFalseTotal
if $type & 32 381 8491 8872
1040  
1041 # Preprocess signals. This is where _signal is translated into
1042 # its registered handler's event name, if there is one.
1043  
1044 8377 if (TRACE_EVENTS) {
1045 _warn(
1046 "<ev> dispatching event $seq ``$event'' to ",
1047 $self->_data_alias_loggable($session->ID)
1048 );
1049 if ($event eq EN_SIGNAL) {
1050 _warn("<ev> signal($etc->[0])");
1051 }
1052 }
1053  
1054 # Prepare to call the appropriate handler. Push the current active
1055 # session on Perl's call stack.
1056  
1057 8374 my ($hold_active_session, $hold_active_event, $hold_active_event_type) = (
1058 $kr_active_session, $kr_active_event, $kr_active_event_type
1059 );
1060 (
1061 8153 $kr_active_session, $kr_active_event, $kr_active_event_type
1062 ) = ($session, $event, $type);
1063  
1064 # We only care about the return value and calling context if it's
1065 # ET_CALL.
1066  
1067 7940 my $return;
1068 9633 my $wantarray = wantarray();
1069  
1070 7940 ▶ confess 'please report this stacktrace to bug-poe@rt.cpan.org' unless (
Branch: 1 branch
BranchTrueFalseTotal
unless defined $session 341 8181 8522
1071 defined $session
1072 );
1073  
1074 # Quiet SIGDIE if it's DEFAULT. If it's something special, then
1075 # someone had better know what they're doing.
1076 # 'DEFAULT', undef and '' are all the same.
1077  
1078 7940 my $old_sig_die = $SIG{__DIE__};
1079 7654 ▶ $SIG{__DIE__} = \&_dummy_sigdie_handler if (
Branch: 1 branch
BranchTrueFalseTotal
if not defined $old_sig_die or $old_sig_die eq "DEFAULT" or $old_sig_die eq "" 4069 4273 8342
Condition66%4/6
not defined $old_sig_die or $old_sig_die eq "DEFAULT" or $old_sig_die eq ""
l!l&&r!l&&!r
15007504
not defined $old_sig_die or $old_sig_die eq "DEFAULT"
l!l&&r!l&&!r
15007504
MC/DC33%1/3
not defined $old_sig_die or $old_sig_die eq "DEFAULT" or $old_sig_die eq ""
not defined $old_sig_die $old_sig_die eq "DEFAULT" $old_sig_die eq ""
Truth table50%2/4
not(A) or B or C
A= not defined $old_sig_dieB= $old_sig_die eq "DEFAULT"C= $old_sig_die eq ""
ABCresult
1XX1
01X1
0011
0000
1080 not defined $old_sig_die or $old_sig_die eq 'DEFAULT' or $old_sig_die eq ''
1081 );
1082  
1083 7623 eval {
1084 7654 ▶ if ($wantarray) {
Branch: 2 branches
BranchTrueFalseTotal
if ($wantarray) { } 5495 3939 9434
elsif (defined $wantarray) { } 542 7368 7910
1085 2043 $return = [
1086 $session->_invoke_state(
1087 $source_session, $event, $etc, $file, $line, $fromstate
1088 )
1089 ];
1090 }
1091 elsif (defined $wantarray) {
1092 6169 $return = $session->_invoke_state(
1093 $source_session, $event, $etc, $file, $line, $fromstate
1094 );
1095 }
1096 else {
1097 5471 $session->_invoke_state(
1098 $source_session, $event, $etc, $file, $line, $fromstate
1099 );
1100 }
1101 };
1102  
1103 # An exception happened?
1104 # It was intially thrown under the $SIG{__DIE__} conditions that the
1105 # user wanted. Any formatting, logging, etc. is already done.
1106  
1107 6288 ▶ if (ref($@) or $@ ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $@ or $@ ne "") 88 7538 7626
Condition66%2/3
ref $@ or $@ ne ""
l!l&&r!l&&!r
0137566
MC/DC50%1/2
ref $@ or $@ ne ""
ref $@ $@ ne ""
Truth table66%2/3
A or B
A= ref $@B= $@ ne ""
ABresult
1X1
011
000
1108 5333 if (CATCH_EXCEPTIONS) {
1109 5887 if (TRACE_EVENTS) {
1110 _warn(
1111 "<ev> exception occurred in $event when invoked on ",
1112 $self->_data_alias_loggable($session->ID)
1113 );
1114 }
1115  
1116 # Exceptions in _stop are rethrown unconditionally.
1117 # We can't enqueue them--the session is about to go away.
1118 # Also if the active session has been forced back to $self via
1119 # POE::Kernel->stop().
1120 4004 ▶ if ($type & (ET_STOP | ET_SIGDIE) or $kr_active_session eq $self) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type & 4104 or $kr_active_session eq $self) { } 588 5337 5925
Condition100%3/3
$type & 4104 or $kr_active_session eq $self
l!l&&r!l&&!r
21584
MC/DC50%1/2
$type & 4104 or $kr_active_session eq $self
$type & 4104 $kr_active_session eq $self
Truth table66%2/3
A or B
A= $type & 4104B= $kr_active_session eq $self
ABresult
1X1
011
000
1121 # Propagate the exception up to the safe rethrow point.
1122 3994 $kr_exception = $@;
1123 }
1124 else {
1125 4075 $self->_data_ev_enqueue(
1126 $session, $self, EN_SIGNAL, ET_SIGDIE, [
1127 'DIE' => {
1128 source_session => $source_session,
1129 dest_session => $session,
1130 event => $event,
1131 file => $file,
1132 line => $line,
1133 from_state => $fromstate,
1134 error_str => $@,
1135 },
1136 ], __FILE__, __LINE__, undef
1137 );
1138 }
1139 }
1140 else {
1141 # Propagate the exception up to the safe rethrow point.
1142 $kr_exception = $@;
1143 }
1144 }
1145  
1146 # Global $sig{__DIE__} changed? For shame!
1147 # TODO - This warning is only needed if a SIGDIE handler is active.
1148 # TODO - Likewise, setting a SIGDIE with a __DIE__ handler in play
1149 # will be tricky or impossible. There should be some message.
1150  
1151 5691 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (!defined($old_sig_die) || $old_sig_die eq "DEFAULT" and $SIG{"__DIE__"} ne \&POE::Kernel::_dummy_sigdie_handler) 23 7489 7512
Condition100%6/6
!defined($old_sig_die) || $old_sig_die eq "DEFAULT" and $SIG{"__DIE__"} ne \&POE::Kernel::_dummy_sigdie_handler
!ll&&!rl&&r
7238134207
not defined $old_sig_die or $old_sig_die eq "DEFAULT"
l!l&&r!l&&!r
319307230
MC/DC0%0/3
!defined($old_sig_die) || $old_sig_die eq "DEFAULT" and $SIG{"__DIE__"} ne \&POE::Kernel::_dummy_sigdie_handler
not defined $old_sig_die $old_sig_die eq "DEFAULT" $SIG{"__DIE__"} ne \&POE::Kernel::_dummy_sigdie_handler
Truth table40%2/5
not(A) or B and C
A= not defined $old_sig_dieB= $old_sig_die eq "DEFAULT"C= $SIG{"__DIE__"} ne \&POE::Kernel::_dummy_sigdie_handler
ABCresult
00X0
1X00
0100
1X11
0111
1152 (not defined $old_sig_die or $old_sig_die eq 'DEFAULT') and
1153 $SIG{__DIE__} ne \&_dummy_sigdie_handler
1154 ) {
1155 24 _warn(
1156 "<sg> Event handler redefined global __DIE__ signal handler.\n",
1157 "<sg> This may conflict with CATCH_EXCEPTIONS handling.\n",
1158 "<sg> If global redefinition is necessary, do it in global code.\n",
1159 );
1160  
1161 2782 $SIG{__DIE__} = $old_sig_die;
1162 }
1163  
1164 # Clear out the event arguments list, in case there are POE-ish
1165 # things in it. This allows them to destruct happily before we set
1166 # the current session back.
1167  
1168 4929 @$etc = ( );
1169  
1170 # Stringify the handler's return value if it belongs in the POE
1171 # namespace. $return's scope exists beyond the post-dispatch
1172 # processing, which includes POE's garbage collection. The scope
1173 # bleed was known to break determinism in surprising ways.
1174  
1175 5650 ▶ if (defined $return and substr(ref($return), 0, 5) eq 'POE::') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $return and substr(ref $return, 0, 5) eq "POE::") 10 5812 5822
Condition100%3/3
defined $return and substr(ref $return, 0, 5) eq "POE::"
!ll&&!rl&&r
6261101687
MC/DC100%2/2
defined $return and substr(ref $return, 0, 5) eq "POE::"
defined $return substr(ref $return, 0, 5) eq "POE::"
Truth table100%3/3
A and B
A= defined $returnB= substr(ref $return, 0, 5) eq "POE::"
ABresult
0X0
100
111
1176 1903 $return = "$return";
1177 }
1178  
1179 # Pop the active session and event, now that they're no longer
1180 # active.
1181  
1182 3608 ($kr_active_session, $kr_active_event, $kr_active_event_type) = (
1183 $hold_active_session, $hold_active_event, $hold_active_event_type
1184 );
1185  
1186 1993 if (TRACE_EVENTS) {
1187 my $string_ret = $return;
1188 $string_ret = "undef" unless defined $string_ret;
1189 _warn("<ev> event $seq ``$event'' returns ($string_ret)\n");
1190 }
1191  
1192 # Return doesn't matter unless ET_CALL, ET_START or ET_STOP.
1193 3510 ▶ return unless $type & (ET_CALL | ET_START | ET_STOP);
Branch: 1 branch
BranchTrueFalseTotal
unless $type & 14 2314 5245 7559
1194  
1195 # Return what the handler did. This is used for call().
1196 2063 ▶ return( $wantarray ? @$return : $return );
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 171 5940 6111
1197 }
1198  
1199 #------------------------------------------------------------------------------
1200 # POE's main loop! Now with Tk and Event support!
1201  
1202 # Do pre-run start-up. Initialize the event loop, and allocate a
1203 # session structure to represent the Kernel.
1204  
1205 sub _initialize_kernel_session {
1206 2168 ▶ my $self = shift;
Subroutine _initialize_kernel_session: called CC=1 SCAR=0.0
1207  
1208 4779 $self->loop_initialize();
1209  
1210 325 $kr_exception = undef;
1211 419 $kr_active_session = $self;
1212 4225 $self->_data_ses_allocate($self, $self->[KR_ID], undef);
1213 }
1214  
1215 # Do post-run cleanup.
1216  
1217 sub _finalize_kernel {
1218 4170 ▶ my $self = shift;
Subroutine _finalize_kernel: called CC=2 SCAR=6.9
1219  
1220 # Disable signal watching since there's now no place for them to go.
1221 342 foreach ($self->_data_sig_get_safe_signals()) {
1222 10634 $self->loop_ignore_signal($_);
1223 }
1224  
1225 # Remove the kernel session's signal watcher.
1226 4170 $self->_data_sig_remove($self->ID, "IDLE");
1227  
1228 # The main loop is done, no matter which event library ran it.
1229 # sig before loop so that it clears the signal_pipe file handler
1230 4094 $self->_data_sig_finalize();
1231 4076 $self->loop_finalize();
1232 4059 $self->_data_extref_finalize();
1233 4042 $self->_data_sid_finalize();
1234 1067 $self->_data_alias_finalize();
1235 202 $self->_data_handle_finalize();
1236 202 $self->_data_ev_finalize();
1237 202 $self->_data_ses_finalize();
1238 }
1239  
1240 sub run_while {
1241 0 ▶ my ($self, $scalar_ref) = ($poe_kernel, @_[1..$#_]);
Subroutine run_while: not called CC=3 SCAR=18.4
Pod documented
1242 0 ▶ 1 while $$scalar_ref and $self->run_one_timeslice();
Branch: 1 branch
BranchTrueFalseTotal
$$scalar_ref and $self->run_one_timeslice and do { 1 } 1228 701 1929
Condition0%0/3
$$scalar_ref and $self->run_one_timeslice
!ll&&!rl&&r
000
MC/DC0%0/2
$$scalar_ref and $self->run_one_timeslice
$$scalar_ref $self->run_one_timeslice
Truth table0%0/3
A and B
A= $$scalar_refB= $self->run_one_timeslice
ABresult
0X0
100
111
1243 }
1244  
1245 sub run_one_timeslice {
1246 0 ▶ my $self = $poe_kernel;
Subroutine run_one_timeslice: not called CC=4 SCAR=20.1
Pod documented
1247  
1248 0 ▶ unless ($self->_data_ses_count()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_data_ses_count) 17 4504 4521
1249 0 $self->_finalize_kernel();
1250 0 $kr_run_warning |= KR_RUN_DONE;
1251 0 ▶ $kr_exception and $self->_rethrow_kr_exception();
Branch: 1 branch
BranchTrueFalseTotal
$POE::Kernel::kr_exception and $self->_rethrow_kr_exception 105 3863 3968
1252 0 return;
1253 }
1254  
1255 0 $self->loop_do_timeslice();
1256 0 ▶ $kr_exception and $self->_rethrow_kr_exception();
Branch: 1 branch
BranchTrueFalseTotal
$POE::Kernel::kr_exception and $self->_rethrow_kr_exception 3262 689 3951
1257  
1258 0 return 1;
1259 }
1260  
1261 sub run {
1262 # So run() can be called as a class method.
1263 213 ▶ POE::Kernel->new unless defined $poe_kernel;
Subroutine run: called CC=5 SCAR=16.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless defined $POE::Kernel::poe_kernel 2975 1096 4071
1264 213 my $self = $poe_kernel;
1265  
1266 # Flag that run() was called.
1267 213 $kr_run_warning |= KR_RUN_CALLED;
1268  
1269 # TODO is this check expensive? ( do people run() more than 1 time? )
1270 213 ▶ if( $kr_pid != $$ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kr_pid != $$) 3 1075 1078
1271 3 if ( ASSERT_USAGE ) {
1272 _warn "Detected a fork, automatically calling ->has_forked()";
1273 }
1274 3 $self->has_forked;
1275 }
1276  
1277 # Don't run the loop if we have no sessions.
1278 # Loop::Event will blow up, so we're doing this sanity check.
1279 # It may never trigger, however: See rt.cpan.org 101227.
1280 46 ▶ if ( $self->_data_ses_count() == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_data_ses_count == 0) { } 0 213 213
1281 # Emit noise only if we are under debug mode
1282 167 if ( ASSERT_DATA ) {
1283 _warn("Not running the event loop because we have no sessions!\n");
1284 }
1285 } else {
1286 # All signals must be explicitly watched now. We do it here because
1287 # it's too early in initialize_kernel_session.
1288 45 $self->_data_sig_add($self, "IDLE", EN_SIGNAL);
1289  
1290 # Run the loop!
1291 47 $self->loop_run();
1292  
1293 # Cleanup
1294 202 $self->_finalize_kernel();
1295 }
1296  
1297 # Clean up afterwards.
1298 202 $kr_run_warning |= KR_RUN_DONE;
1299  
1300 202 ▶ $kr_exception and $self->_rethrow_kr_exception();
Branch: 1 branch
BranchTrueFalseTotal
$POE::Kernel::kr_exception and $self->_rethrow_kr_exception 10 192 202
1301 }
1302  
1303 sub _rethrow_kr_exception {
1304 178 ▶ my $self = shift;
Subroutine _rethrow_kr_exception: called CC=1 SCAR=0.0
1305  
1306 # It's quite common to see people wrap POE::Kernel->run() in an eval
1307 # block and start things again if an exception is caught.
1308 #
1309 # This little lexical dance is actually important. It allows
1310 # $kr_exception to be cleared if the die() is caught.
1311  
1312 176 my $exception = $kr_exception;
1313 10 $kr_exception = undef;
1314  
1315 # The die is cast.
1316 10 die $exception;
1317 }
1318  
1319 # Stops the kernel cold. XXX Experimental!
1320 # No events happen as a result of this, all structures are cleaned up
1321 # except the kernel's. Even the current session and POE::Kernel are
1322 # cleaned up, which may introduce inconsistencies in the current
1323 # session... as _dispatch_event() attempts to clean up for a defunct
1324 # session.
1325  
1326 sub stop {
1327 # So stop() can be called as a class method.
1328 16 ▶ my $self = $poe_kernel;
Subroutine stop: called CC=5 SCAR=16.1
Pod documented
1329  
1330 # May be called when the kernel's already stopped. Avoid problems
1331 # trying to find child sessions when the kernel isn't registered.
1332 16 ▶ if ($self->_data_ses_exists($self->ID)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_data_ses_exists($self->ID)) 6 10 16
1333 6 my @children = ($self);
1334 6 foreach my $session (@children) {
1335 17 push @children, $self->_data_ses_get_children($session->ID);
1336 }
1337  
1338 # Don't stop believin'. Nor the POE::Kernel singleton.
1339 6 shift @children;
1340  
1341 # Walk backwards to avoid inconsistency errors.
1342 6 foreach my $session (reverse @children) {
1343 11 $self->_data_ses_stop($session->ID);
1344 }
1345 }
1346  
1347 # Roll back whether sessions were started.
1348 16 $kr_run_warning &= ~KR_RUN_SESSION;
1349  
1350 # So new sessions will not be child of the current defunct session.
1351 16 $kr_active_session = $self;
1352  
1353 # The GC mark list may prevent sessions from DESTROYing.
1354 # Clean it up.
1355 16 $self->_data_ses_gc_sweep();
1356  
1357 # Running stop() is recommended in a POE::Wheel::Run coderef
1358 # Program, before setting up for the next POE::Kernel->run(). When
1359 # the PID has changed, imply _data_sig_has_forked() during stop().
1360  
1361 16 ▶ $poe_kernel->has_forked() if $kr_pid != $$;
Branch: 1 branch
BranchTrueFalseTotal
if $kr_pid != $$ 0 16 16
1362  
1363 # TODO - If we're polling for signals, then the reset gets it wrong.
1364 # The reset doesn't count sigchld polling. If we must put this
1365 # back, it MUST account for all internal events currently in play,
1366 # or the child process will stall if it reruns POE::Kernel's loop.
1367 #_idle_queue_reset();
1368  
1369 16 return;
1370 }
1371  
1372 # Less invasive form of ->stop() + ->run()
1373 sub has_forked {
1374 13 ▶ if( $kr_pid == $$ ) {
Subroutine has_forked: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if ($kr_pid == $$) 3 10 13
1375 3 if ( ASSERT_USAGE ) {
1376 _warn "You should only call ->has_forked() from the child process.";
1377 }
1378 3 return;
1379 }
1380  
1381 # So has_forked() can be called as a class method.
1382 6 my $self = $poe_kernel;
1383  
1384 10 $kr_pid = $$;
1385 10 $self->_recalc_id();
1386  
1387 # reset some stuff for the signals
1388 10 $poe_kernel->_data_sig_has_forked;
1389 }
1390  
1391 #------------------------------------------------------------------------------
1392  
1393 sub DESTROY {
1394 4 ▶ my $self = shift;
Subroutine DESTROY: not called CC=3 SCAR=22.0
1395  
1396 # Warn that a session never had the opportunity to run if one was
1397 # created but run() was never called.
1398  
1399 0 ▶ unless ($kr_run_warning & KR_RUN_CALLED) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($kr_run_warning & 1) 0 0 0
1400 0 ▶ if ($kr_run_warning & KR_RUN_SESSION) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kr_run_warning & 2) 0 0 0
1401 0 _warn(
1402 "Sessions were started, but POE::Kernel's run() method was never\n",
1403 "called to execute them. This usually happens because an error\n",
1404 "occurred before POE::Kernel->run() could be called. Please fix\n",
1405 "any errors above this notice, and be sure that POE::Kernel->run()\n",
1406 "is called. See documentation for POE::Kernel's run() method for\n",
1407 "another way to disable this warning.\n",
1408 );
1409 }
1410 }
1411 }
1412  
1413 #------------------------------------------------------------------------------
1414 # _invoke_state is what _dispatch_event calls to dispatch a transition
1415 # event. This is the kernel's _invoke_state so it can receive events.
1416 # These are mostly signals, which are propagated down in
1417 # _dispatch_event.
1418  
1419 sub _invoke_state {
1420 1686 ▶ my ($self, $source_session, $event, $etc) = @_;
Subroutine _invoke_state: called CC=6 SCAR=18.6
1421  
1422 # This is an event loop to poll for child processes without needing
1423 # to catch SIGCHLD.
1424  
1425 1686 ▶ if ($event eq EN_SCPOLL) {
Branch: 2 branches
BranchTrueFalseTotal
if ($event eq '_sigchld_poll') { } 377 1309 1686
elsif ($event eq '_signal') { } 202 1107 1309
1426 377 $self->_data_sig_handle_poll_event($etc->[0]);
1427 }
1428  
1429 # A signal was posted. Because signals propagate depth-first, this
1430 # _invoke_state is called last in the dispatch. If the signal was
1431 # SIGIDLE, then post a SIGZOMBIE if the main queue is still idle.
1432  
1433 elsif ($event eq EN_SIGNAL) {
1434 202 ▶ if ($etc->[0] eq 'IDLE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($etc->[0] eq "IDLE") 202 0 202
1435 202 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless ($kr_queue->get_item_count > $idle_queue_size or $self->_data_handle_count) 202 0 202
Condition33%1/3
$kr_queue->get_item_count > $idle_queue_size or $self->_data_handle_count
l!l&&r!l&&!r
00202
MC/DC0%0/2
$kr_queue->get_item_count > $idle_queue_size or $self->_data_handle_count
$kr_queue->get_item_count > $idle_queue_size $self->_data_handle_count
Truth table33%1/3
A or B
A= $kr_queue->get_item_count > $idle_queue_sizeB= $self->_data_handle_count
ABresult
1X1
011
000
1436 $kr_queue->get_item_count() > $idle_queue_size or
1437 $self->_data_handle_count()
1438 ) {
1439 202 $self->_data_ev_enqueue(
1440 $self, $self, EN_SIGNAL, ET_SIGNAL, [ 'ZOMBIE' ],
1441 __FILE__, __LINE__, undef
1442 );
1443 }
1444 }
1445 }
1446  
1447 1686 return 0;
1448 }
1449  
1450 #==============================================================================
1451 # SESSIONS
1452 #==============================================================================
1453  
1454 # Dispatch _start to a session, allocating it in the kernel's data
1455 # structures as a side effect.
1456  
1457 sub session_alloc {
1458 838 ▶ my ($self, $session, @args) = ($poe_kernel, @_[1..$#_]);
Subroutine session_alloc: called CC=5 SCAR=16.1
Pod documented
1459  
1460 # If we already returned, then we must reinitialize. This is so
1461 # $poe_kernel->run() will work correctly more than once.
1462 838 ▶ if ($kr_run_warning & KR_RUN_DONE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kr_run_warning & 4) 94 744 838
1463 94 $kr_run_warning &= ~KR_RUN_DONE;
1464 94 $self->_initialize_kernel_session();
1465 94 $self->_data_sig_initialize();
1466 }
1467  
1468 838 if (ASSERT_DATA) {
1469 if (defined $session->ID) {
1470 _trap(
1471 "<ss> ", $self->_data_alias_loggable($session->ID),
1472 " already allocated\a"
1473 );
1474 }
1475 }
1476  
1477 # Register that a session was created.
1478 838 $kr_run_warning |= KR_RUN_SESSION;
1479  
1480 # Allocate the session's data structure. This must be done before
1481 # we dispatch anything regarding the new session.
1482 167 my $new_sid = $self->_data_sid_allocate();
1483 837 $session->_set_id($new_sid);
1484 837 $self->_data_ses_allocate($session, $new_sid, $kr_active_session->ID);
1485  
1486 837 my $loggable = $self->_data_alias_loggable($new_sid);
1487  
1488 # Tell the new session that it has been created. Catch the _start
1489 # state's return value so we can pass it to the parent with the
1490 # _child create.
1491 #
1492 # TODO - Void the context if the parent has no _child handler?
1493  
1494 837 my $return = $self->_dispatch_event(
1495 $session, $kr_active_session,
1496 EN_START, ET_START, \@args,
1497 __FILE__, __LINE__, undef, monotime(), -__LINE__
1498 );
1499  
1500 823 ▶ unless($self->_data_ses_exists($new_sid)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_data_ses_exists($new_sid)) 1 823 824
1501 671 if(TRACE_SESSIONS) {
1502 _warn("<ss> ", $loggable, " disappeared during ", EN_START);
1503 }
1504 621 return $return;
1505 }
1506  
1507 # If the child has not detached itself---that is, if its parent is
1508 # the currently active session---then notify the parent with a
1509 # _child create event. Otherwise skip it, since we'd otherwise
1510 # throw a create without a lose.
1511 $self->_dispatch_event(
1512 138 $self->_data_ses_get_parent($session->ID), $self,
1513 EN_CHILD, ET_CHILD, [ CHILD_CREATE, $session, $return ],
1514 __FILE__, __LINE__, undef, monotime(), -__LINE__
1515 );
1516  
1517 152 ▶ unless ($self->_data_ses_exists($new_sid)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_data_ses_exists($new_sid)) 0 773 773
1518 249 if (TRACE_SESSIONS) {
1519 _warn("<ss> ", $loggable, " disappeared during ", EN_CHILD, " dispatch");
1520 }
1521 621 return $return;
1522 }
1523  
1524 # Enqueue a delayed garbage-collection event so the session has time
1525 # to do its thing before it goes.
1526 $self->_data_ev_enqueue(
1527 524 $session, $session, EN_GC, ET_GC, [],
1528 __FILE__, __LINE__, undef
1529 );
1530 }
1531  
1532 # Detach a session from its parent. This breaks the parent/child
1533 # relationship between the current session and its parent. Basically,
1534 # the current session is given to the Kernel session. Unlike with
1535 # _stop, the current session's children follow their parent.
1536  
1537 sub detach_myself {
1538 11 ▶ my $self = $poe_kernel;
Subroutine detach_myself: called CC=4 SCAR=14.2
Pod documented
1539  
1540 260 if (ASSERT_USAGE) {
1541 _confess "<us> must call detach_myself() from a running session"
1542 if $kr_active_session == $self;
1543 }
1544  
1545 # Can't detach from the kernel.
1546 11 ▶ if ($self->_data_ses_get_parent($kr_active_session->ID) == $self) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_data_ses_get_parent($kr_active_session->ID) == $self) 1 631 632
1547 396 $! = EPERM;
1548 0 return;
1549 }
1550  
1551 0 my $old_parent = $self->_data_ses_get_parent($kr_active_session->ID);
1552  
1553 # Tell the old parent session that the child is departing.
1554 # But not if the active event is ET_START, since that would generate
1555 # a CHILD_LOSE without a CHILD_CREATE.
1556 10 ▶ $self->_dispatch_event(
Branch: 1 branch
BranchTrueFalseTotal
unless $kr_active_event_type & 4 0 10 10
1557 $old_parent, $self,
1558 EN_CHILD, ET_CHILD, [ CHILD_LOSE, $kr_active_session, undef ],
1559 (caller)[1,2], undef, monotime(), -__LINE__
1560 )
1561 unless $kr_active_event_type & ET_START;
1562  
1563 # Tell the new parent (kernel) that it's gaining a child.
1564 # (Actually it doesn't care, so we don't do that here, but this is
1565 # where the code would go if it ever does in the future.)
1566  
1567 # Tell the current session that its parentage is changing.
1568 10 $self->_dispatch_event(
1569 $kr_active_session, $self,
1570 EN_PARENT, ET_PARENT, [ $old_parent, $self ],
1571 (caller)[1,2], undef, monotime(), -__LINE__
1572 );
1573  
1574 10 $self->_data_ses_move_child($kr_active_session->ID, $self->ID);
1575  
1576 # Success!
1577 10 return 1;
1578 }
1579  
1580 # Detach a child from this, the parent. The session being detached
1581 # must be a child of the current session.
1582  
1583 sub detach_child {
1584 20 ▶ my ($self, $child) = ($poe_kernel, @_[1..$#_]);
Subroutine detach_child: called CC=5 SCAR=18.2
Pod documented
1585  
1586 10 if (ASSERT_USAGE) {
1587 _confess "<us> must call detach_child() from a running session"
1588 if $kr_active_session == $self;
1589 }
1590  
1591 10 my $child_session = $self->_resolve_session($child);
1592 9 ▶ unless (defined $child_session) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $child_session) 11 9 20
1593 9 $self->_explain_resolve_failure($child);
1594 0 return;
1595 }
1596  
1597 # Can't detach if it belongs to the kernel. TODO We shouldn't need
1598 # to check for this.
1599 0 ▶ if ($kr_active_session == $self) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kr_active_session == $self) 0 8 8
1600 8 $! = EPERM;
1601 0 return;
1602 }
1603  
1604 # Can't detach if it's not a child of the current session.
1605 0 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_data_ses_is_child($kr_active_session->ID, $child_session->ID)) 0 8 8
1606 $self->_data_ses_is_child($kr_active_session->ID, $child_session->ID)
1607 ) {
1608 8 $! = EPERM;
1609 0 return;
1610 }
1611  
1612 # Tell the current session that the child is departing.
1613 $self->_dispatch_event(
1614 0 $kr_active_session, $self,
1615 EN_CHILD, ET_CHILD, [ CHILD_LOSE, $child_session, undef ],
1616 (caller)[1,2], undef, monotime(), -__LINE__
1617 );
1618  
1619 # Tell the new parent (kernel) that it's gaining a child.
1620 # (Actually it doesn't care, so we don't do that here, but this is
1621 # where the code would go if it ever does in the future.)
1622  
1623 # Tell the child session that its parentage is changing.
1624 8 $self->_dispatch_event(
1625 $child_session, $self,
1626 EN_PARENT, ET_PARENT, [ $kr_active_session, $self ],
1627 (caller)[1,2], undef, monotime(), -__LINE__
1628 );
1629  
1630 8 $self->_data_ses_move_child($child_session->ID, $self->ID);
1631  
1632 # Success!
1633 8 return 1;
1634 }
1635  
1636 ### Helpful accessors.
1637  
1638 sub get_active_session {
1639 1051 ▶ return $kr_active_session;
Subroutine get_active_session: called CC=1 SCAR=0.0
Pod documented
1640 }
1641  
1642 sub get_active_event {
1643 0 ▶ return $kr_active_event;
Subroutine get_active_event: not called CC=1 SCAR=6.9
Pod documented
1644 }
1645  
1646 # FIXME - Should this exist?
1647 sub get_event_count {
1648 0 ▶ return $kr_queue->get_item_count();
Subroutine get_event_count: not called CC=1 SCAR=6.9
Pod documented
1649 }
1650  
1651 # FIXME - Should this exist?
1652 sub get_next_event_time {
1653 0 ▶ return $kr_queue->get_next_priority();
Subroutine get_next_event_time: not called CC=1 SCAR=6.9
Pod documented
1654 }
1655  
1656 #==============================================================================
1657 # EVENTS
1658 #==============================================================================
1659  
1660 #------------------------------------------------------------------------------
1661 # Post an event to the queue.
1662  
1663 sub post {
1664 326 ▶ my ($self, $dest_session, $event_name, @etc) = ($poe_kernel, @_[1..$#_]);
Subroutine post: called CC=5 SCAR=16.1
Pod documented
1665  
1666 326 if (ASSERT_USAGE) {
1667 _confess "<us> destination is undefined in post()"
1668 unless defined $dest_session;
1669 _confess "<us> event is undefined in post()" unless defined $event_name;
1670 _carp(
1671 "<us> The '$event_name' event is one of POE's own. Its " .
1672 "effect cannot be achieved by posting it"
1673 ) if exists $poes_own_events{$event_name};
1674 };
1675  
1676 # Attempt to resolve the destination session reference against
1677 # various things.
1678  
1679 326 my $session = $self->_resolve_session($dest_session);
1680 325 ▶ unless (defined $session) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $session) 2 332 334
1681 317 $self->_explain_resolve_failure($dest_session);
1682 316 return;
1683 }
1684  
1685 # Enqueue the event for "now", which simulates FIFO in our
1686 # time-ordered queue.
1687  
1688 $self->_data_ev_enqueue(
1689 323 $session, $kr_active_session, $event_name, ET_POST, \@etc,
1690 (caller)[1,2], $kr_active_event
1691 );
1692 7 return 1;
1693 }
1694  
1695 #------------------------------------------------------------------------------
1696 # Post an event to the queue for the current session.
1697  
1698 sub yield {
1699 763 ▶ my ($self, $event_name, @etc) = ($poe_kernel, @_[1..$#_]);
Subroutine yield: called CC=4 SCAR=13.9
Pod documented
1700  
1701 1079 if (ASSERT_USAGE) {
1702 _confess "<us> must call yield() from a running session"
1703 if $kr_active_session == $self;
1704 _confess "<us> event name is undefined in yield()"
1705 unless defined $event_name;
1706 _carp(
1707 "<us> The '$event_name' event is one of POE's own. Its " .
1708 "effect cannot be achieved by yielding it"
1709 ) if exists $poes_own_events{$event_name};
1710 };
1711  
1712 1079 $self->_data_ev_enqueue(
1713 $kr_active_session, $kr_active_session, $event_name, ET_POST, \@etc,
1714 (caller)[1,2], $kr_active_event
1715 );
1716  
1717 762 undef;
1718 }
1719  
1720 #------------------------------------------------------------------------------
1721 # Call an event handler directly.
1722  
1723 sub call {
1724 4394 ▶ my ($self, $dest_session, $event_name, @etc) = ($poe_kernel, @_[1..$#_]);
Subroutine call: called CC=13 SCAR=28.7
Pod documented
1725  
1726 4394 if (ASSERT_USAGE) {
1727 _confess "<us> destination is undefined in call()"
1728 unless defined $dest_session;
1729 _confess "<us> event is undefined in call()" unless defined $event_name;
1730 _carp(
1731 "<us> The '$event_name' event is one of POE's own. Its " .
1732 "effect cannot be achieved by calling it"
1733 ) if exists $poes_own_events{$event_name};
1734 };
1735  
1736 # Attempt to resolve the destination session reference against
1737 # various things.
1738  
1739 4394 my $session = $self->_resolve_session($dest_session);
1740 3700 ▶ unless (defined $session) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $session) 4 4709 4713
1741 1190 $self->_explain_resolve_failure($dest_session);
1742 1189 return;
1743 }
1744  
1745 # Dispatch the event right now, bypassing the queue altogether.
1746 # This tends to be a Bad Thing to Do.
1747  
1748 # TODO The difference between synchronous and asynchronous events
1749 # should be made more clear in the documentation, so that people
1750 # have a tendency not to abuse them. I discovered in xws that
1751 # mixing the two types makes it harder than necessary to write
1752 # deterministic programs, but the difficulty can be ameliorated if
1753 # programmers set some base rules and stick to them.
1754  
1755 3698 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 2 4707 4709
1756 0 ▶ my @return_value = (
Branch: 1 branch
BranchTrueFalseTotal
$session == $kr_active_session ? : 0 2198 2198
1757 ($session == $kr_active_session)
1758 ? $session->_invoke_state(
1759 $session, $event_name, \@etc, (caller)[1,2],
1760 $kr_active_event
1761 )
1762 : $self->_dispatch_event(
1763 $session, $kr_active_session,
1764 $event_name, ET_CALL, \@etc,
1765 (caller)[1,2], $kr_active_event, monotime(), -__LINE__
1766 )
1767 );
1768  
1769 0 ▶ $kr_exception and $self->_rethrow_kr_exception();
Branch: 1 branch
BranchTrueFalseTotal
$POE::Kernel::kr_exception and $self->_rethrow_kr_exception 0 1189 1189
1770  
1771 1189 $! = 0;
1772 0 return @return_value;
1773 }
1774  
1775 2509 ▶ if (defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) 0 3698 3698
1776 0 ▶ my $return_value = (
Branch: 1 branch
BranchTrueFalseTotal
$session == $kr_active_session ? : 0 0 0
1777 $session == $kr_active_session
1778 ? $session->_invoke_state(
1779 $session, $event_name, \@etc, (caller)[1,2],
1780 $kr_active_event
1781 )
1782 : $self->_dispatch_event(
1783 $session, $kr_active_session,
1784 $event_name, ET_CALL, \@etc,
1785 (caller)[1,2], $kr_active_event, monotime(), -__LINE__
1786 )
1787 );
1788  
1789 0 ▶ $kr_exception and $self->_rethrow_kr_exception();
Branch: 1 branch
BranchTrueFalseTotal
$POE::Kernel::kr_exception and $self->_rethrow_kr_exception 0 0 0
1790  
1791 1189 $! = 0;
1792 165 return $return_value;
1793 }
1794  
1795 2674 ▶ if ($session == $kr_active_session) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session == $kr_active_session) { } 2665 1033 3698
1796 2665 $session->_invoke_state(
1797 $session, $event_name, \@etc, (caller)[1,2],
1798 $kr_active_event
1799 );
1800 }
1801 else {
1802 174 $self->_dispatch_event(
1803 $session, $kr_active_session,
1804 $event_name, ET_CALL, \@etc,
1805 (caller)[1,2], $kr_active_event, monotime(), -__LINE__
1806 );
1807 }
1808  
1809 3522 ▶ $kr_exception and $self->_rethrow_kr_exception();
Branch: 1 branch
BranchTrueFalseTotal
$POE::Kernel::kr_exception and $self->_rethrow_kr_exception 101 2562 2663
1810  
1811 3456 $! = 0;
1812 2564 return;
1813 }
1814  
1815 #==============================================================================
1816 # DELAYED EVENTS
1817 #==============================================================================
1818  
1819 sub alarm {
1820 3727 ▶ my ($self, $event_name, $time, @etc) = ($poe_kernel, @_[1..$#_]);
Subroutine alarm: called CC=7 SCAR=19.5
Pod documented
1821  
1822 3727 if (ASSERT_USAGE) {
1823 _confess "<us> must call alarm() from a running session"
1824 if $kr_active_session == $self;
1825 _confess "<us> event name is undefined in alarm()"
1826 unless defined $event_name;
1827 _carp(
1828 "<us> The '$event_name' event is one of POE's own. Its " .
1829 "effect cannot be achieved by setting an alarm for it"
1830 ) if exists $poes_own_events{$event_name};
1831 };
1832  
1833 3727 ▶ unless (defined $event_name) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $event_name) 2 2866 2868
1834 2541 $self->_explain_return("invalid parameter to alarm() call");
1835 2539 return EINVAL;
1836 }
1837  
1838 2700 $self->_data_ev_clear_alarm_by_name($kr_active_session->ID(), $event_name);
1839  
1840 # Add the new alarm if it includes a time. Calling _data_ev_enqueue
1841 # directly is faster than calling alarm_set to enqueue it.
1842 161 ▶ if (defined $time) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $time) { } 959 2766 3725
1843 0 $self->_data_ev_enqueue
1844 ( $kr_active_session, $kr_active_session,
1845 $event_name, ET_ALARM, [ @etc ],
1846 (caller)[1,2], $kr_active_event, $time,
1847 );
1848 }
1849 else {
1850 # The event queue has become empty? Stop the time watcher.
1851 2700 ▶ $self->loop_pause_time_watcher() unless $kr_queue->get_item_count();
Branch: 1 branch
BranchTrueFalseTotal
unless $kr_queue->get_item_count 79 3645 3724
1852 }
1853  
1854 2700 return 0;
1855 }
1856  
1857 # Add an alarm without clobbering previous alarms of the same name.
1858 sub alarm_add {
1859 1258 ▶ my ($self, $event_name, $time, @etc) = ($poe_kernel, @_[1..$#_]);
Subroutine alarm_add: called CC=7 SCAR=19.5
Pod documented
1860  
1861 1313 if (ASSERT_USAGE) {
1862 _confess "<us> must call alarm_add() from a running session"
1863 if $kr_active_session == $self;
1864 _confess "<us> undefined event name in alarm_add()"
1865 unless defined $event_name;
1866 _confess "<us> undefined time in alarm_add()" unless defined $time;
1867 _carp(
1868 "<us> The '$event_name' event is one of POE's own. Its " .
1869 "effect cannot be achieved by adding an alarm for it"
1870 ) if exists $poes_own_events{$event_name};
1871 };
1872  
1873 2555 ▶ unless (defined $event_name and defined $time) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $event_name and defined $time) 2 2553 2555
Condition66%2/3
defined $event_name and defined $time
!ll&&!rl&&r
1012
MC/DC50%1/2
defined $event_name and defined $time
defined $event_name defined $time
Truth table66%2/3
A and B
A= defined $event_nameB= defined $time
ABresult
0X0
100
111
1874 15 $self->_explain_return("invalid parameter to alarm_add() call");
1875 13 return EINVAL;
1876 }
1877  
1878 $self->_data_ev_enqueue
1879 12 ( $kr_active_session, $kr_active_session,
1880 $event_name, ET_ALARM, [ @etc ],
1881 (caller)[1,2], $kr_active_event, $time,
1882 );
1883  
1884 12 return 0;
1885 }
1886  
1887 # Add a delay, which is like an alarm relative to the current time.
1888 sub delay {
1889 976 ▶ my ($self, $event_name, $delay, @etc) = ($poe_kernel, @_[1..$#_]);
Subroutine delay: called CC=6 SCAR=17.9
Pod documented
1890 976 my $pri = monotime();
1891  
1892 988 if (ASSERT_USAGE) {
1893 _confess "<us> must call delay() from a running session"
1894 if $kr_active_session == $self;
1895 _confess "<us> undefined event name in delay()" unless defined $event_name;
1896 _carp(
1897 "<us> The '$event_name' event is one of POE's own. Its " .
1898 "effect cannot be achieved by setting a delay for it"
1899 ) if exists $poes_own_events{$event_name};
1900 };
1901  
1902 988 ▶ unless (defined $event_name) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $event_name) 1245 2284 3529
1903 293 $self->_explain_return("invalid parameter to delay() call");
1904 291 return EINVAL;
1905 }
1906  
1907 973 ▶ if (defined $delay) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $delay) { } 536 1748 2284
1908 521 $self->_data_ev_clear_alarm_by_name($kr_active_session->ID(), $event_name);
1909  
1910 # Add the new alarm if it includes a time. Calling _data_ev_enqueue
1911 # directly is faster than calling alarm_set to enqueue it.
1912 521 $self->_data_ev_enqueue
1913 ( $kr_active_session, $kr_active_session,
1914 $event_name, ET_ALARM, [ @etc ],
1915 (caller)[1,2], $kr_active_event, undef, $delay, $pri+$delay
1916 );
1917 }
1918 else {
1919 452 $self->alarm($event_name);
1920 }
1921  
1922 918 return 0;
1923 }
1924  
1925 # Add a delay without clobbering previous delays of the same name.
1926 sub delay_add {
1927 246 ▶ my ($self, $event_name, $delay, @etc) = ($poe_kernel, @_[1..$#_]);
Subroutine delay_add: called CC=7 SCAR=19.5
Pod documented
1928 65 my $pri = monotime();
1929  
1930 301 if (ASSERT_USAGE) {
1931 _confess "<us> must call delay_add() from a running session"
1932 if $kr_active_session == $self;
1933 _confess "<us> undefined event name in delay_add()"
1934 unless defined $event_name;
1935 _confess "<us> undefined time in delay_add()" unless defined $delay;
1936 _carp(
1937 "<us> The '$event_name' event is one of POE's own. Its " .
1938 "effect cannot be achieved by adding a delay for it"
1939 ) if exists $poes_own_events{$event_name};
1940 };
1941  
1942 10 ▶ unless (defined $event_name and defined $delay) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $event_name and defined $delay) 2 311 313
Condition66%2/3
defined $event_name and defined $delay
!ll&&!rl&&r
106
MC/DC50%1/2
defined $event_name and defined $delay
defined $event_name defined $delay
Truth table66%2/3
A and B
A= defined $event_nameB= defined $delay
ABresult
0X0
100
111
1943 9 $self->_explain_return("invalid parameter to delay_add() call");
1944 7 return EINVAL;
1945 }
1946  
1947 $self->_data_ev_enqueue
1948 6 ( $kr_active_session, $kr_active_session,
1949 $event_name, ET_ALARM, [ @etc ],
1950 (caller)[1,2], $kr_active_event, undef, $delay, $pri+$delay
1951 );
1952  
1953 6 return 0;
1954 }
1955  
1956 #------------------------------------------------------------------------------
1957 # New style alarms.
1958  
1959 # Set an alarm. This does more *and* less than plain alarm(). It
1960 # only sets alarms (that's the less part), but it also returns an
1961 # alarm ID (that's the more part).
1962  
1963 sub alarm_set {
1964 241 ▶ my ($self, $event_name, $time, @etc) = ($poe_kernel, @_[1..$#_]);
Subroutine alarm_set: called CC=5 SCAR=16.4
Pod documented
1965  
1966 241 if (ASSERT_USAGE) {
1967 _confess "<us> must call alarm_set() from a running session"
1968 if $kr_active_session == $self;
1969 }
1970  
1971 247 ▶ unless (defined $event_name) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $event_name) 3 549 552
1972 245 $self->_explain_usage("undefined event name in alarm_set()");
1973 1 $! = EINVAL;
1974 0 return;
1975 }
1976  
1977 1 ▶ unless (defined $time) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $time) 239 298 537
1978 238 $self->_explain_usage("undefined time in alarm_set()");
1979 1 $! = EINVAL;
1980 0 return;
1981 }
1982  
1983 0 if (ASSERT_USAGE) {
1984 _carp(
1985 "<us> The '$event_name' event is one of POE's own. Its " .
1986 "effect cannot be achieved by setting an alarm for it"
1987 ) if exists $poes_own_events{$event_name};
1988 }
1989  
1990 236 return $self->_data_ev_enqueue
1991 ( $kr_active_session, $kr_active_session, $event_name, ET_ALARM, [ @etc ],
1992 (caller)[1,2], $kr_active_event, $time,
1993 );
1994 }
1995  
1996 # Remove an alarm by its ID. TODO Now that alarms and events have
1997 # been recombined, this will remove an event by its ID. However,
1998 # nothing returns an event ID, so nobody knows what to remove.
1999  
2000 sub alarm_remove {
2001 466 ▶ my ($self, $alarm_id) = ($poe_kernel, @_[1..$#_]);
Subroutine alarm_remove: called CC=6 SCAR=17.9
Pod documented
2002  
2003 466 if (ASSERT_USAGE) {
2004 _confess "<us> must call alarm_remove() from a running session"
2005 if $kr_active_session == $self;
2006 }
2007  
2008 230 ▶ unless (defined $alarm_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $alarm_id) 3 470 473
2009 221 $self->_explain_usage("undefined alarm id in alarm_remove()");
2010 1 $! = EINVAL;
2011 0 return;
2012 }
2013  
2014 8 my ($time, $event) =
2015 $self->_data_ev_clear_alarm_by_id($kr_active_session->ID(), $alarm_id);
2016 227 ▶ return unless defined $time;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $time 1 469 470
2017  
2018 # In a list context, return the alarm that was removed. In a scalar
2019 # context, return a reference to the alarm that was removed. In a
2020 # void context, return nothing. Either way this returns a defined
2021 # value when someone needs something useful from it.
2022  
2023 227 ▶ return unless defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 8 219 227
2024 219 ▶ return ( $event->[EV_NAME], $time, $event->[EV_ARGS] ) if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 215 4 219
2025 4 return [ $event->[EV_NAME], $time, $event->[EV_ARGS] ];
2026 }
2027  
2028 # Move an alarm to a new time. This virtually removes the alarm and
2029 # re-adds it somewhere else. In reality, adjust_priority() is
2030 # optimized for this sort of thing.
2031  
2032 sub alarm_adjust {
2033 37 ▶ my ($self, $alarm_id, $delta) = ($poe_kernel, @_[1..$#_]);
Subroutine alarm_adjust: called CC=4 SCAR=14.1
Pod documented
2034  
2035 35 if (ASSERT_USAGE) {
2036 _confess "<us> must call alarm_adjust() from a running session"
2037 if $kr_active_session == $self;
2038 }
2039  
2040 35 ▶ unless (defined $alarm_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $alarm_id) 4 35 39
2041 33 $self->_explain_usage("undefined alarm id in alarm_adjust()");
2042 1 $! = EINVAL;
2043 0 return;
2044 }
2045  
2046 1 ▶ unless (defined $delta) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $delta) 2 31 33
2047 32 $self->_explain_usage("undefined alarm delta in alarm_adjust()");
2048 1 $! = EINVAL;
2049 0 return;
2050 }
2051  
2052 my $my_alarm = sub {
2053 3788 ▶ $_[0]->[EV_SESSION] == $kr_active_session;
Subroutine __ANON__: called CC=1 SCAR=0.0
2054 0 };
2055
2056 30 return $self->_data_ev_adjust( $alarm_id, $my_alarm, undef, $delta );
2057 }
2058  
2059 # A convenient function for setting alarms relative to now. It also
2060 # uses whichever time() POE::Kernel can find, which may be
2061 # Time::HiRes'.
2062  
2063 sub delay_set {
2064 # Always always always grab time() ASAP, so that the eventual
2065 # time we set the delay for is as close as possible to the time
2066 # at which they ASKED for the delay, not when we actually set it.
2067 20 ▶ my $t = walltime();
Subroutine delay_set: called CC=5 SCAR=16.1
Pod documented
2068 20 my $pri = monotime();
2069  
2070 # And now continue as normal
2071 20 my ($self, $event_name, $seconds, @etc) = ($poe_kernel, @_[1..$#_]);
2072  
2073 20 if (ASSERT_USAGE) {
2074 _confess "<us> must call delay_set() from a running session"
2075 if $kr_active_session == $self;
2076 }
2077  
2078 20 ▶ unless (defined $event_name) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $event_name) 3 48 51
2079 10 $self->_explain_usage("undefined event name in delay_set()");
2080 1 $! = EINVAL;
2081 0 return;
2082 }
2083  
2084 9 if (ASSERT_USAGE) {
2085 _carp(
2086 "<us> The '$event_name' event is one of POE's own. Its " .
2087 "effect cannot be achieved by setting a delay for it"
2088 ) if exists $poes_own_events{$event_name};
2089 }
2090  
2091 17 ▶ unless (defined $seconds) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $seconds) 2 16 18
2092 9 $self->_explain_usage("undefined seconds in delay_set()");
2093 8 $! = EINVAL;
2094 1 return;
2095 }
2096  
2097 8 return $self->_data_ev_enqueue
2098 ( $kr_active_session, $kr_active_session, $event_name, ET_ALARM, [ @etc ],
2099 (caller)[1,2], $kr_active_event, $t, $seconds, $pri+$seconds
2100 );
2101 }
2102  
2103 # Move a delay to a new offset from time(). As with alarm_adjust(),
2104 # this is optimized internally for this sort of activity.
2105  
2106 sub delay_adjust {
2107 # Always always always grab time() ASAP, so that the eventual
2108 # time we set the delay for is as close as possible to the time
2109 # at which they ASKED for the delay, not when we actually set it.
2110 5 ▶ my $t = walltime();
Subroutine delay_adjust: called CC=4 SCAR=14.9
Pod documented
2111 12 my $pri = monotime();
2112  
2113 # And now continue as normal
2114 5 my ($self, $alarm_id, $seconds) = ($poe_kernel, @_[1..$#_]);
2115  
2116 5 if (ASSERT_USAGE) {
2117 _confess "<us> must call delay_adjust() from a running session"
2118 if $kr_active_session == $self;
2119 }
2120  
2121 5 ▶ unless (defined $alarm_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $alarm_id) 2 11 13
2122 3 $self->_explain_usage("undefined delay id in delay_adjust()");
2123 1 $! = EINVAL;
2124 0 return;
2125 }
2126  
2127 1 ▶ unless (defined $seconds) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $seconds) 3 8 11
2128 2 $self->_explain_usage("undefined delay seconds in delay_adjust()");
2129 1 $! = EINVAL;
2130 0 return;
2131 }
2132  
2133 my $my_delay = sub {
2134 0 ▶ $_[0]->[EV_SESSION] == $kr_active_session;
Subroutine __ANON__: not called CC=1 SCAR=6.9
2135 0 };
2136  
2137 0 if (TRACE_EVENTS) {
2138 _warn("<ev> adjusted event $alarm_id by $seconds seconds from $t");
2139 }
2140  
2141 0 return $self->_data_ev_set($alarm_id, $my_delay, $t, $pri, $seconds );
2142 }
2143  
2144 # Remove all alarms for the current session.
2145  
2146 sub alarm_remove_all {
2147 4 ▶ my $self = $poe_kernel;
Subroutine alarm_remove_all: called CC=5 SCAR=16.7
Pod documented
2148  
2149 4 if (ASSERT_USAGE) {
2150 _confess "<us> must call alarm_remove_all() from a running session"
2151 if $kr_active_session == $self;
2152 }
2153  
2154 # This should never happen, actually.
2155 4 ▶ _trap "unknown session in alarm_remove_all call" unless (
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_data_ses_exists($kr_active_session->ID) 3 2 5
2156 $self->_data_ses_exists($kr_active_session->ID)
2157 );
2158  
2159 # Free every alarm owned by the session. This code is ripped off
2160 # from the _stop code to flush everything.
2161  
2162 2 my @removed = $self->_data_ev_clear_alarm_by_session(
2163 $kr_active_session->ID()
2164 );
2165  
2166 2 ▶ return unless defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 2 2
2167 2 ▶ return @removed if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 2 0 2
2168 0 return \@removed;
2169 }
2170  
2171 #==============================================================================
2172 # SELECTS
2173 #==============================================================================
2174  
2175 sub _internal_select {
2176 2844 ▶ my ($self, $session, $handle, $event_name, $mode, $args) = @_;
Subroutine _internal_select: called CC=2 SCAR=6.9
2177  
2178 # If an event is included, then we're defining a filehandle watcher.
2179  
2180 2844 ▶ if ($event_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($event_name) { } 1224 1620 2844
2181 1224 $self->_data_handle_add($handle, $mode, $session, $event_name, $args);
2182 }
2183 else {
2184 1620 $self->_data_handle_remove($handle, $mode, $session->ID);
2185 }
2186 }
2187  
2188 # A higher-level select() that manipulates read, write and expedite
2189 # selects together.
2190  
2191 sub select {
2192 113 ▶ my ($self, $handle, $event_r, $event_w, $event_e, @args) = (
Subroutine select: called CC=7 SCAR=19.5
Pod documented
2193 $poe_kernel, @_[1..$#_]
2194 );
2195  
2196 113 if (ASSERT_USAGE) {
2197 _confess "<us> must call select() from a running session"
2198 if $kr_active_session == $self;
2199 _confess "<us> undefined filehandle in select()" unless defined $handle;
2200 _confess "<us> invalid filehandle in select()"
2201 unless defined fileno($handle);
2202 foreach ($event_r, $event_w, $event_e) {
2203 next unless defined $_;
2204 _carp(
2205 "<us> The '$_' event is one of POE's own. Its " .
2206 "effect cannot be achieved by setting a file watcher to it"
2207 ) if exists($poes_own_events{$_});
2208 }
2209 }
2210  
2211 $self->_internal_select(
2212 113 $kr_active_session, $handle, $event_r, MODE_RD, \@args
2213 );
2214 112 $self->_internal_select(
2215 $kr_active_session, $handle, $event_w, MODE_WR, \@args
2216 );
2217 111 $self->_internal_select(
2218 $kr_active_session, $handle, $event_e, MODE_EX, \@args
2219 );
2220 110 return 0;
2221 }
2222  
2223 # Only manipulate the read select.
2224 sub select_read {
2225 1870 ▶ my ($self, $handle, $event_name, @args) = ($poe_kernel, @_[1..$#_]);
Subroutine select_read: called CC=6 SCAR=17.9
Pod documented
2226  
2227 1590 if (ASSERT_USAGE) {
2228 _confess "<us> must call select_read() from a running session"
2229 if $kr_active_session == $self;
2230 _confess "<us> undefined filehandle in select_read()"
2231 unless defined $handle;
2232 _confess "<us> invalid filehandle in select_read()"
2233 unless defined fileno($handle);
2234 _carp(
2235 "<us> The '$event_name' event is one of POE's own. Its " .
2236 "effect cannot be achieved by setting a file watcher to it"
2237 ) if defined($event_name) and exists($poes_own_events{$event_name});
2238 };
2239  
2240 1682 $self->_internal_select(
2241 $kr_active_session, $handle, $event_name, MODE_RD, \@args
2242 );
2243 1681 return 0;
2244 }
2245  
2246 # Only manipulate the write select.
2247 sub select_write {
2248 1955 ▶ my ($self, $handle, $event_name, @args) = ($poe_kernel, @_[1..$#_]);
Subroutine select_write: called CC=6 SCAR=17.9
Pod documented
2249  
2250 1950 if (ASSERT_USAGE) {
2251 _confess "<us> must call select_write() from a running session"
2252 if $kr_active_session == $self;
2253 _confess "<us> undefined filehandle in select_write()"
2254 unless defined $handle;
2255 _confess "<us> invalid filehandle in select_write()"
2256 unless defined fileno($handle);
2257 _carp(
2258 "<us> The '$event_name' event is one of POE's own. Its " .
2259 "effect cannot be achieved by setting a file watcher to it"
2260 ) if defined($event_name) and exists($poes_own_events{$event_name});
2261 };
2262  
2263 1856 $self->_internal_select(
2264 $kr_active_session, $handle, $event_name, MODE_WR, \@args
2265 );
2266 1855 return 0;
2267 }
2268  
2269 # Only manipulate the expedite select.
2270 sub select_expedite {
2271 708 ▶ my ($self, $handle, $event_name, @args) = ($poe_kernel, @_[1..$#_]);
Subroutine select_expedite: called CC=6 SCAR=17.9
Pod documented
2272  
2273 707 if (ASSERT_USAGE) {
2274 _confess "<us> must call select_expedite() from a running session"
2275 if $kr_active_session == $self;
2276 _confess "<us> undefined filehandle in select_expedite()"
2277 unless defined $handle;
2278 _confess "<us> invalid filehandle in select_expedite()"
2279 unless defined fileno($handle);
2280 _carp(
2281 "<us> The '$event_name' event is one of POE's own. Its " .
2282 "effect cannot be achieved by setting a file watcher to it"
2283 ) if defined($event_name) and exists($poes_own_events{$event_name});
2284 };
2285  
2286 707 $self->_internal_select(
2287 $kr_active_session, $handle, $event_name, MODE_EX, \@args
2288 );
2289 706 return 0;
2290 }
2291  
2292 # Turn off a handle's write mode bit without doing
2293 # garbage-collection things.
2294 sub select_pause_write {
2295 875 ▶ my ($self, $handle) = ($poe_kernel, @_[1..$#_]);
Subroutine select_pause_write: called CC=5 SCAR=16.1
Pod documented
2296  
2297 874 if (ASSERT_USAGE) {
2298 _confess "<us> must call select_pause_write() from a running session"
2299 if $kr_active_session == $self;
2300 _confess "<us> undefined filehandle in select_pause_write()"
2301 unless defined $handle;
2302 _confess "<us> invalid filehandle in select_pause_write()"
2303 unless defined fileno($handle);
2304 };
2305  
2306 874 ▶ return 0 unless $self->_data_handle_is_good($handle, MODE_WR);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_data_handle_is_good($handle, 1) 5 2603 2608
2307  
2308 873 $self->_data_handle_pause($handle, MODE_WR);
2309  
2310 872 return 1;
2311 }
2312  
2313 # Turn on a handle's write mode bit without doing garbage-collection
2314 # things.
2315 sub select_resume_write {
2316 1217 ▶ my ($self, $handle) = ($poe_kernel, @_[1..$#_]);
Subroutine select_resume_write: called CC=5 SCAR=16.1
Pod documented
2317  
2318 1217 if (ASSERT_USAGE) {
2319 _confess "<us> must call select_resume_write() from a running session"
2320 if $kr_active_session == $self;
2321 _confess "<us> undefined filehandle in select_resume_write()"
2322 unless defined $handle;
2323 _confess "<us> invalid filehandle in select_resume_write()"
2324 unless defined fileno($handle);
2325 };
2326  
2327 1217 ▶ return 0 unless $self->_data_handle_is_good($handle, MODE_WR);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_data_handle_is_good($handle, 1) 6 2943 2949
2328  
2329 543 $self->_data_handle_resume($handle, MODE_WR);
2330  
2331 542 return 1;
2332 }
2333  
2334 # Turn off a handle's read mode bit without doing garbage-collection
2335 # things.
2336 sub select_pause_read {
2337 522 ▶ my ($self, $handle) = ($poe_kernel, @_[1..$#_]);
Subroutine select_pause_read: called CC=5 SCAR=16.1
Pod documented
2338  
2339 522 if (ASSERT_USAGE) {
2340 _confess "<us> must call select_pause_read() from a running session"
2341 if $kr_active_session == $self;
2342 _confess "<us> undefined filehandle in select_pause_read()"
2343 unless defined $handle;
2344 _confess "<us> invalid filehandle in select_pause_read()"
2345 unless defined fileno($handle);
2346 };
2347  
2348 522 ▶ return 0 unless $self->_data_handle_is_good($handle, MODE_RD);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_data_handle_is_good($handle, 0) 11 2913 2924
2349  
2350 72 $self->_data_handle_pause($handle, MODE_RD);
2351  
2352 71 return 1;
2353 }
2354  
2355 # Turn on a handle's read mode bit without doing garbage-collection
2356 # things.
2357 sub select_resume_read {
2358 141 ▶ my ($self, $handle) = ($poe_kernel, @_[1..$#_]);
Subroutine select_resume_read: called CC=5 SCAR=16.1
Pod documented
2359  
2360 141 if (ASSERT_USAGE) {
2361 _confess "<us> must call select_resume_read() from a running session"
2362 if $kr_active_session == $self;
2363 _confess "<us> undefined filehandle in select_resume_read()"
2364 unless defined $handle;
2365 _confess "<us> invalid filehandle in select_resume_read()"
2366 unless defined fileno($handle);
2367 };
2368  
2369 141 ▶ return 0 unless $self->_data_handle_is_good($handle, MODE_RD);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_data_handle_is_good($handle, 0) 283 2889 3172
2370  
2371 70 $self->_data_handle_resume($handle, MODE_RD);
2372  
2373 69 return 1;
2374 }
2375  
2376 #==============================================================================
2377 # Aliases: These functions expose the internal alias accessors with
2378 # extra fun parameter/return value checking.
2379 #==============================================================================
2380  
2381 ### Set an alias in the current session.
2382  
2383 sub alias_set {
2384 264 ▶ my ($self, $name) = ($poe_kernel, @_[1..$#_]);
Subroutine alias_set: called CC=4 SCAR=13.9
Pod documented
2385  
2386 264 if (ASSERT_USAGE) {
2387 _confess "<us> undefined alias in alias_set()" unless defined $name;
2388 };
2389  
2390 # Don't overwrite another session's alias.
2391 264 my $existing_session = $self->_data_alias_resolve($name);
2392 195 ▶ if (defined $existing_session) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $existing_session) 8 780 788
2393 183 ▶ if ($existing_session != $kr_active_session) {
Branch: 1 branch
BranchTrueFalseTotal
if ($existing_session != $kr_active_session) 43 279 322
2394 42 $self->_explain_usage("alias '$name' is in use by another session");
2395 1 return EEXIST;
2396 }
2397 4 return 0;
2398 }
2399  
2400 52 $self->_data_alias_add($kr_active_session, $name);
2401 149 return 0;
2402 }
2403  
2404 ### Remove an alias from the current session.
2405  
2406 sub alias_remove {
2407 204 ▶ my ($self, $name) = ($poe_kernel, @_[1..$#_]);
Subroutine alias_remove: called CC=4 SCAR=13.9
Pod documented
2408  
2409 67 if (ASSERT_USAGE) {
2410 _confess "<us> undefined alias in alias_remove()" unless defined $name;
2411 };
2412  
2413 67 my $existing_session = $self->_data_alias_resolve($name);
2414  
2415 66 ▶ unless (defined $existing_session) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $existing_session) 4 172 176
2416 56 $self->_explain_usage("alias '$name' does not exist");
2417 2 return ESRCH;
2418 }
2419  
2420 10 ▶ if ($existing_session != $kr_active_session) {
Branch: 1 branch
BranchTrueFalseTotal
if ($existing_session != $kr_active_session) 2 62 64
2421 54 $self->_explain_usage("alias '$name' does not belong to current session");
2422 1 return EPERM;
2423 }
2424  
2425 9 $self->_data_alias_remove($kr_active_session, $name);
2426 61 return 0;
2427 }
2428  
2429 ### Resolve an alias into a session.
2430  
2431 sub alias_resolve {
2432 63 ▶ my ($self, $name) = ($poe_kernel, @_[1..$#_]);
Subroutine alias_resolve: called CC=2 SCAR=6.9
Pod documented
2433  
2434 11 if (ASSERT_USAGE) {
2435 _confess "<us> undefined alias in alias_resolve()" unless defined $name;
2436 };
2437  
2438 11 return $self->_resolve_session($name);
2439 }
2440  
2441 ### List the aliases for a given session.
2442  
2443 sub alias_list {
2444 18 ▶ my ($self, $search_session) = ($poe_kernel, @_[1..$#_]);
Subroutine alias_list: called CC=4 SCAR=14.2
Pod documented
2445 8 ▶ my $session =
Condition66%2/3
$search_session || $kr_active_session
l!l&&r!l&&!r
7408920
MC/DC0%0/2
$search_session || $kr_active_session
$search_session $kr_active_session
Truth table66%2/3
A || B
A= $search_sessionB= $kr_active_session
ABresult
1X1
011
000
2446 $self->_resolve_session($search_session || $kr_active_session);
2447  
2448 8 ▶ unless (defined $session) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $session) 2 70 72
2449 0 $self->_explain_resolve_failure($search_session, "nonfatal");
2450 0 return;
2451 }
2452  
2453 # Return whatever can be found.
2454 8 my @alias_list = $self->_data_alias_list($session->ID);
2455 8 ▶ return wantarray() ? @alias_list : $alias_list[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 4 8
2456 }
2457  
2458 #==============================================================================
2459 # Kernel and Session IDs
2460 #==============================================================================
2461  
2462 # Return the Kernel's "unique" ID. There's only so much uniqueness
2463 # available; machines on separate private 10/8 networks may have
2464 # identical kernel IDs. The chances of a collision are vanishingly
2465 # small.
2466  
2467 # The Kernel and Session IDs are based on Philip Gwyn's code. I hope
2468 # he still can recognize it.
2469  
2470 sub _recalc_id {
2471 208 ▶ my $self = shift;
Subroutine _recalc_id: called CC=3 SCAR=11.0
2472  
2473 208 my $old_id = $self->[KR_ID];
2474  
2475 208 my $hostname = eval { (uname)[1] };
2476 208 ▶ $hostname = hostname() unless defined $hostname;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hostname 0 208 208
2477  
2478 my $new_id = $self->[KR_ID] = join(
2479 "-", $hostname,
2480 624 map { unpack "H*", $_ }
2481 208 map { pack "N", $_ }
2482 (monotime(), $$, ++$kr_id_seq)
2483 );
2484  
2485 208 ▶ if (defined $old_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $old_id) 10 198 208
2486 10 $self->_data_sig_relocate_kernel_id($old_id, $new_id);
2487 10 $self->_data_ses_relocate_kernel_id($old_id, $new_id);
2488 10 $self->_data_sid_relocate_kernel_id($old_id, $new_id);
2489 10 $self->_data_handle_relocate_kernel_id($old_id, $new_id);
2490 10 $self->_data_ev_relocate_kernel_id($old_id, $new_id);
2491 10 $self->_data_alias_relocate_kernel_id($old_id, $new_id);
2492 }
2493 }
2494  
2495 15667 ▶sub ID { $poe_kernel->[KR_ID] }
Subroutine ID: called CC=1 SCAR=0.0
Pod documented
2496  
2497 # Resolve an ID to a session reference. This function is virtually
2498 # moot now that _resolve_session does it too. This explicit call will
2499 # be faster, though, so it's kept for things that can benefit from it.
2500  
2501 sub ID_id_to_session {
2502 6 ▶ my ($self, $id) = ($poe_kernel, @_[1..$#_]);
Subroutine ID_id_to_session: called CC=3 SCAR=11.3
Pod documented
2503  
2504 6 if (ASSERT_USAGE) {
2505 _confess "<us> undefined ID in ID_id_to_session()" unless defined $id;
2506 };
2507  
2508 6 my $session = $self->_data_sid_resolve($id);
2509 5 ▶ return $session if defined $session;
Branch: 1 branch
BranchTrueFalseTotal
if defined $session 1 5 6
2510  
2511 5 $self->_explain_return("ID does not exist");
2512 0 $! = ESRCH;
2513 0 return;
2514 }
2515  
2516 # Resolve a session reference to its corresponding ID.
2517  
2518 sub ID_session_to_id {
2519 33 ▶ my ($self, $session) = ($poe_kernel, @_[1..$#_]);
Subroutine ID_session_to_id: called CC=3 SCAR=11.2
Pod documented
2520  
2521 33 if (ASSERT_USAGE) {
2522 _confess "<us> undefined session in ID_session_to_id()"
2523 unless defined $session;
2524 };
2525  
2526 33 my $id = $self->_data_ses_resolve_to_id($session);
2527 32 ▶ if (defined $id) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id) 5 32 37
2528 31 $! = 0;
2529 31 return $id;
2530 }
2531  
2532 32 $self->_explain_return("session ($session) does not exist");
2533 0 $! = ESRCH;
2534 0 return;
2535 }
2536  
2537 #==============================================================================
2538 # Extra reference counts, to keep sessions alive when things occur.
2539 # They take session IDs because they may be called from resources at
2540 # times where the session reference is otherwise unknown.
2541 #==============================================================================
2542  
2543 sub refcount_increment {
2544 104 ▶ my ($self, $session_id, $tag) = ($poe_kernel, @_[1..$#_]);
Subroutine refcount_increment: called CC=4 SCAR=13.9
Pod documented
2545  
2546 104 if (ASSERT_USAGE) {
2547 _confess "<us> undefined session ID in refcount_increment()"
2548 unless defined $session_id;
2549 _confess "<us> undefined reference count tag in refcount_increment()"
2550 unless defined $tag;
2551 };
2552  
2553 104 ▶ unless ($self->_data_ses_exists($session_id)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_data_ses_exists($session_id)) 33 102 135
2554 102 $self->_explain_return("session id $session_id does not exist");
2555 100 $! = ESRCH;
2556 0 return;
2557 }
2558  
2559 1 my $refcount = $self->_data_extref_inc($session_id, $tag);
2560 # TODO trace it here
2561 1 return $refcount;
2562 }
2563  
2564 sub refcount_decrement {
2565 204 ▶ my ($self, $session_id, $tag) = ($poe_kernel, @_[1..$#_]);
Subroutine refcount_decrement: called CC=4 SCAR=13.9
Pod documented
2566  
2567 204 if (ASSERT_USAGE) {
2568 _confess "<us> undefined session ID in refcount_decrement()"
2569 unless defined $session_id;
2570 _confess "<us> undefined reference count tag in refcount_decrement()"
2571 unless defined $tag;
2572 };
2573  
2574 104 ▶ unless ($self->_data_ses_exists($session_id)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_data_ses_exists($session_id)) 3 202 205
2575 102 $self->_explain_return("session id $session_id does not exist");
2576 100 $! = ESRCH;
2577 0 return;
2578 }
2579  
2580 1 my $refcount = $self->_data_extref_dec($session_id, $tag);
2581  
2582 # TODO trace it here
2583 1 return $refcount;
2584 }
2585  
2586 #==============================================================================
2587 # HANDLERS
2588 #==============================================================================
2589  
2590 # Add or remove event handlers from sessions.
2591 sub state {
2592 2234 ▶ my ($self, $event, $state_code, $state_alias) = ($poe_kernel, @_[1..$#_]);
Subroutine state: called CC=7 SCAR=19.5
Pod documented
2593 2234 ▶ $state_alias = $event unless defined $state_alias;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $state_alias 2135 200 2335
2594  
2595 2134 if (ASSERT_USAGE) {
2596 _confess "<us> must call state() from a running session"
2597 if $kr_active_session == $self;
2598 _confess "<us> undefined event name in state()" unless defined $event;
2599 _confess "<us> can't call state() outside a session" if (
2600 $kr_active_session == $self
2601 );
2602 };
2603  
2604 2134 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (ref $kr_active_session ne "" and ref $kr_active_session ne "POE::Kernel") 792 1442 2234
Condition66%2/3
ref $kr_active_session ne "" and ref $kr_active_session ne "POE::Kernel"
!ll&&!rl&&r
072125
MC/DC50%1/2
ref $kr_active_session ne "" and ref $kr_active_session ne "POE::Kernel"
ref $kr_active_session ne "" ref $kr_active_session ne "POE::Kernel"
Truth table66%2/3
A and B
A= ref $kr_active_session ne ""B= ref $kr_active_session ne "POE::Kernel"
ABresult
0X0
100
111
2605 (ref($kr_active_session) ne '') &&
2606 (ref($kr_active_session) ne 'POE::Kernel')
2607 ) {
2608 2126 $kr_active_session->_register_state($event, $state_code, $state_alias);
2609 2125 return 0;
2610 }
2611  
2612 # TODO A terminal signal (such as UIDESTROY) kills a session. The
2613 # Kernel deallocates the session, which cascades destruction to its
2614 # HEAP. That triggers a Wheel's destruction, which calls
2615 # $kernel->state() to remove a state from the session. The session,
2616 # though, is already gone. If TRACE_RETVALS and/or ASSERT_RETVALS
2617 # is set, this causes a warning or fatal error.
2618  
2619 1341 $self->_explain_return("session ($kr_active_session) does not exist");
2620 1340 return ESRCH;
2621 }
2622  
2623 200 1;
2624  
2625 __END__
« blib/lib/POE/Wheel/Curses.pm ↑ Summary blib/lib/POE/Filter/Block.pm »