blib/lib/POE/Kernel.pm

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