blib/lib/POE/Resource/Sessions.pm

filter: Statementstmt 93.3% 154 / 165 Branchbran 85.4% 41 / 48 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 95.6% 22 / 23 Podpod n/a totaltotal 90.6% 223 / 246 CC 46 SCAR 15.2
CC
46
Cov
91%
_data_ses_finalize
8.3
_data_ses_free
8.0
_data_ses_stop
6.0
CRAP 4.6
« mylib/gen-tests.perl ↑ Summary blib/lib/POE/Wheel/ListenAccept.pm »
1 # Manage session data structures on behalf of POE::Kernel.
2  
3 package POE::Resource::Sessions;
4  
5 200 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 471 $VERSION = '1.370'; # NOTE - Should be #.### (three decimal places)
7  
8 # These methods are folded into POE::Kernel;
9 package POE::Kernel;
10  
11 200 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # Map stringy sessions to their references for _data_ses_resolve.
14 485 my %kr_session_refs;
15 # { $session_ref => $blessed, ... }
16  
17 ### Session structure.
18 my %kr_sessions;
19 # { $session_id =>
20 # [ $blessed_session, SS_SESSION
21 # $total_reference_count, SS_REFCOUNT
22 # $parent_session, SS_PARENT
23 # { $child_session_id => $blessed_ref, SS_CHILDREN
24 # ...,
25 # },
26 # { $process_id => $placeholder_value, SS_PROCESSES
27 # ...,
28 # },
29 # ],
30 # ...,
31 # };
32  
33 sub SS_SESSION () { 0 }
34 sub SS_REFCOUNT () { 1 }
35 sub SS_PARENT () { 2 }
36 sub SS_CHILDREN () { 3 }
37 sub SS_PROCESSES () { 4 }
38  
39 200 ▶BEGIN { $POE::Kernel::poe_kernel->[KR_SESSIONS] = \%kr_sessions; }
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 10 ▶sub _data_ses_relocate_kernel_id {
Subroutine _data_ses_relocate_kernel_id: called CC=4 SCAR=14.1
42 10 my ($self, $old_id, $new_id) = @_;
43  
44 10 ▶ while (my ($sid, $ses_rec) = each %kr_sessions) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %kr_sessions) 17 10 27
45 17 my $children = $ses_rec->[SS_CHILDREN];
46 $children->{$new_id} = delete $children->{$old_id}
47 17 ▶ if exists $children->{$old_id};
Branch: 1 branch
BranchTrueFalseTotal
if exists $children->{$old_id} 0 17 17
48 }
49  
50 $kr_sessions{$new_id} = delete $kr_sessions{$old_id}
51 10 ▶ if exists $kr_sessions{$old_id};
Branch: 1 branch
BranchTrueFalseTotal
if exists $kr_sessions{$old_id} 10 0 10
52 }
53  
54 ### End-run leak checking.
55  
56 0 ▶sub _data_ses_clone {
Subroutine _data_ses_clone: not called CC=2 SCAR=17.9
57 0 %kr_session_refs = ();
58 0 foreach my $ses_ref (map { $_->[SS_SESSION] } values %kr_sessions) {
59 0 $kr_session_refs{$ses_ref} = $ses_ref;
60 }
61 }
62  
63 204 ▶sub _data_ses_finalize {
Subroutine _data_ses_finalize: called CC=4 SCAR=21.2
64 204 my $finalized_ok = 1;
65  
66 204 ▶ while (my ($sid, $ses_rec) = each %kr_sessions) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %kr_sessions) 0 204 204
67 0 $finalized_ok = 0;
68 _warn(
69 "!!! Leaked session: $sid\n",
70 "!!!\trefcnt = $ses_rec->[SS_REFCOUNT]\n",
71 "!!!\tparent = $ses_rec->[SS_PARENT]\n",
72 0 "!!!\tchilds = ", join("; ", keys(%{$ses_rec->[SS_CHILDREN]})), "\n",
73 0 "!!!\tprocs = ", join("; ", keys(%{$ses_rec->[SS_PROCESSES]})),"\n",
74 );
75 }
76  
77 204 ▶ while (my ($stringy, $blessed) = each %kr_session_refs) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %kr_session_refs) 0 204 204
78 0 $finalized_ok = 0;
79 0 _warn "!!! Leaked stringy session $stringy = $blessed\n";
80 0 ▶ _warn "!!!\tBad clone detected, while we're at it.\n" if (
Branch: 1 branch
BranchTrueFalseTotal
if $stringy ne "$blessed" 0 0 0
81 $stringy ne "$blessed"
82 );
83 }
84  
85 204 return $finalized_ok;
86 }
87  
88 ### Enter a new session into the back-end stuff.
89  
90 200 my %kr_marked_for_gc;
91 200 my @kr_marked_for_gc;
92  
93 1141 ▶sub _data_ses_allocate {
Subroutine _data_ses_allocate: called CC=6 SCAR=17.9
94 1141 my ($self, $session, $sid, $parent_id) = @_;
95  
96 1141 my $parent;
97 1141 ▶ if (defined $parent_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parent_id) 848 293 1141
98 _trap "parent session $parent_id does not exist" unless (
99 848 ▶ exists $kr_sessions{$parent_id}
Branch: 1 branch
BranchTrueFalseTotal
unless exists $kr_sessions{$parent_id} 1 847 848
100 );
101  
102 847 $parent = $kr_sessions{$parent_id}[SS_SESSION];
103  
104 847 ▶ _trap "session $session is already allocated" if exists $kr_sessions{$sid};
Branch: 1 branch
BranchTrueFalseTotal
if exists $kr_sessions{$sid} 1 846 847
105 }
106  
107 1139 TRACE_REFCNT and _warn "<rc> allocating $session";
108  
109 1139 $kr_sessions{$sid} =
110 [ $session, # SS_SESSION
111 0, # SS_REFCOUNT
112 $parent, # SS_PARENT
113 { }, # SS_CHILDREN
114 { }, # SS_PROCESSES
115 ];
116  
117 # For the ID to session reference lookup.
118 1139 $self->_data_sid_set($sid, $session);
119  
120 # For the stringy to blessed session reference lookup.
121 1139 $kr_session_refs{$session} = $session;
122  
123 # Manage parent/child relationship.
124 1139 ▶ if (defined $parent_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parent_id) 846 293 1139
125 846 if (TRACE_SESSIONS) {
126 _warn(
127 "<ss> ",
128 $self->_data_alias_loggable($sid), " has parent ",
129 $self->_data_alias_loggable($parent_id)
130 );
131 }
132  
133 846 $kr_sessions{$parent_id}->[SS_CHILDREN]->{$sid} = $session;
134 846 $self->_data_ses_refcount_inc($parent_id);
135 }
136  
137 1015 TRACE_REFCNT and _warn "<rc> $session marked for gc";
138 1139 ▶ unless ($sid eq $self->ID) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sid eq $self->ID) 846 293 1139
139 970 push @kr_marked_for_gc, $sid;
140 846 $kr_marked_for_gc{$sid} = $sid;
141 }
142 }
143  
144 # Release a session's resources, and remove it. This doesn't do
145 # garbage collection for the session itself because that should
146 # already have happened.
147 #
148 # TODO This is yet another place where resources will need to register
149 # a function. Every resource's _data_???_clear_session is called
150 # here.
151  
152 816 ▶sub _data_ses_free {
Subroutine _data_ses_free: called CC=8 SCAR=20.8
153 1242 my ($self, $sid) = @_;
154  
155 816 TRACE_REFCNT and do {
156 _warn "<rc> freeing session $sid";
157 _trap("!!! free defunct session $sid?!\n") unless (
158 $self->_data_ses_exists($sid)
159 );
160 };
161  
162 816 if (TRACE_SESSIONS) {
163 _warn "<ss> freeing ", $self->_data_alias_loggable($sid);
164 }
165  
166 # Manage parent/child relationships.
167  
168 816 my $parent = $kr_sessions{$sid}->[SS_PARENT];
169 816 my @children = $self->_data_ses_get_children($sid);
170  
171 816 ▶ if (defined $parent) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parent) { } 307 509 816
172 698 my $parent_id = $parent->ID;
173  
174 698 if (ASSERT_DATA) {
175 _trap "session is its own parent" if $parent_id eq $sid;
176 _trap "session's parent ($parent_id) doesn't exist"
177 unless exists $kr_sessions{$parent_id};
178  
179 unless ($self->_data_ses_is_child($parent_id, $sid)) {
180 _trap(
181 $self->_data_alias_loggable($sid), " isn't a child of ",
182 $self->_data_alias_loggable($parent_id), " (it's a child of ",
183 $self->_data_alias_loggable($self->_data_ses_get_parent($sid)->ID),
184 ")"
185 );
186 }
187 }
188  
189 # Remove the departing session from its parent.
190  
191 _trap "internal inconsistency ($parent_id/$sid)"
192 698 ▶ unless delete $kr_sessions{$parent_id}->[SS_CHILDREN]->{$sid};
Branch: 1 branch
BranchTrueFalseTotal
unless delete $kr_sessions{$parent_id}[3]{$sid} 303 395 698
193  
194 610 $kr_sessions{$sid}->[SS_PARENT] = undef;
195  
196 610 if (TRACE_SESSIONS) {
197 _cluck(
198 "<ss> removed ",
199 $self->_data_alias_loggable($sid), " from ",
200 $self->_data_alias_loggable($parent_id)
201 );
202 }
203  
204 375 $self->_data_ses_refcount_dec($parent_id);
205  
206 # Move the departing session's children to its parent.
207  
208 610 foreach (@children) {
209 538 $self->_data_ses_move_child($_->ID, $parent_id)
210 }
211 }
212 elsif (ASSERT_DATA) {
213 _trap "no parent to give children to" if @children;
214 }
215  
216 341 my $session = $kr_sessions{$sid}[SS_SESSION];
217  
218 # Things which do not hold reference counts.
219  
220 644 $self->_data_sid_clear($sid); # Remove from SID tables.
221 644 $self->_data_sig_clear_session($sid); # Remove all leftover signals.
222  
223 # Things which do hold reference counts.
224  
225 409 $self->_data_alias_clear_session($sid); # Remove all leftover aliases.
226 495 $self->_data_extref_clear_session($sid); # Remove all leftover extrefs.
227 730 $self->_data_handle_clear_session($sid); # Remove all leftover handles.
228  
229 730 $self->_data_ev_clear_session($sid); # Remove all leftover events.
230  
231 # Remove the session itself.
232  
233 428 delete $kr_marked_for_gc{$sid};
234 513 delete $kr_sessions{$sid};
235 815 delete $kr_session_refs{$session};
236 }
237  
238 ### Move a session to a new parent.
239  
240 22 ▶sub _data_ses_move_child {
Subroutine _data_ses_move_child: called CC=4 SCAR=13.9
241 731 my ($self, $sid, $new_parent_id) = @_;
242  
243 731 if (ASSERT_DATA) {
244 _trap("moving nonexistent child to another parent")
245 unless exists $kr_sessions{$sid};
246 _trap("moving child to a nonexistent parent")
247 unless exists $kr_sessions{$new_parent_id};
248 }
249  
250 731 if (TRACE_SESSIONS) {
251 _warn(
252 "<ss> moving ",
253 $self->_data_alias_loggable($sid), " to ",
254 $self->_data_alias_loggable($new_parent_id)
255 );
256 }
257  
258 730 my $old_parent_id = $self->_data_ses_get_parent($sid)->ID;
259  
260 727 if (ASSERT_DATA) {
261 _trap("moving child from a nonexistent parent")
262 unless exists $kr_sessions{$old_parent_id};
263 }
264  
265 # Remove the session from its old parent.
266 408 delete $kr_sessions{$old_parent_id}->[SS_CHILDREN]->{$sid};
267  
268 408 if (TRACE_SESSIONS) {
269 _warn(
270 "<ss> removed ",
271 $self->_data_alias_loggable($sid), " from ",
272 $self->_data_alias_loggable($old_parent_id)
273 );
274 }
275  
276 408 $self->_data_ses_refcount_dec($old_parent_id);
277  
278 # Change the session's parent.
279 408 $kr_sessions{$sid}->[SS_PARENT] = $kr_sessions{$new_parent_id}[SS_SESSION];
280  
281 20 if (TRACE_SESSIONS) {
282 _warn(
283 "<ss> changed parent of ",
284 $self->_data_alias_loggable($sid), " to ",
285 $self->_data_alias_loggable($new_parent_id)
286 );
287 }
288  
289 # Add the current session to the new parent's children.
290 $kr_sessions{$new_parent_id}->[SS_CHILDREN]->{$sid} = (
291 20 $kr_sessions{$sid}[SS_SESSION]
292 );
293  
294 20 if (TRACE_SESSIONS) {
295 _warn(
296 "<ss> added ",
297 $self->_data_alias_loggable($sid), " as child of ",
298 $self->_data_alias_loggable($new_parent_id)
299 );
300 }
301  
302 20 $self->_data_ses_refcount_inc($new_parent_id);
303  
304 # We do not call _data_ses_collect_garbage() here. This function is
305 # called in batch for a departing session, to move its children to
306 # its parent. The GC test would be superfluous here. Rather, it's
307 # up to the caller to do the proper GC test after moving things
308 # around.
309 }
310  
311 ### Get a session's parent.
312  
313 1641 ▶sub _data_ses_get_parent {
Subroutine _data_ses_get_parent: called CC=3 SCAR=11.0
314 1661 my ($self, $sid) = @_;
315 1661 if (ASSERT_DATA || ASSERT_USAGE) {
316 _trap("undefined session ID") unless defined $sid;
317 _trap("retrieving parent of a nonexistent session")
318 unless exists $kr_sessions{$sid};
319 }
320 1661 return $kr_sessions{$sid}->[SS_PARENT];
321 }
322  
323 ### Get a session's children.
324  
325 3211 ▶sub _data_ses_get_children {
Subroutine _data_ses_get_children: called CC=2 SCAR=6.9
326 4621 my ($self, $sid) = @_;
327 4620 if (ASSERT_DATA) {
328 _trap("retrieving children of a nonexistent session")
329 unless exists $kr_sessions{$sid};
330 }
331 3231 return values %{$kr_sessions{$sid}->[SS_CHILDREN]};
332 }
333  
334 ### Is a session a child of another?
335  
336 550 ▶sub _data_ses_is_child {
Subroutine _data_ses_is_child: called CC=3 SCAR=12.0
337 2996 my ($self, $parent_id, $child_id) = @_;
338 550 if (ASSERT_DATA) {
339 _trap("testing is-child of a nonexistent parent session")
340 unless exists $kr_sessions{$parent_id};
341 }
342 return(
343 exists $kr_sessions{$parent_id} &&
344 550 ▶ exists $kr_sessions{$parent_id}->[SS_CHILDREN]->{$child_id}
Condition33%1/3
exists $kr_sessions{$parent_id} && exists $kr_sessions{$parent_id}[3]{$child_id}
!ll&&!rl&&r
00549
MC/DC0%0/2
exists $kr_sessions{$parent_id} && exists $kr_sessions{$parent_id}[3]{$child_id}
exists $kr_sessions{$parent_id} exists $kr_sessions{$parent_id}[3]{$child_id}
Truth table33%1/3
A && B
A= exists $kr_sessions{$parent_id}B= exists $kr_sessions{$parent_id}[3]{$child_id}
ABresult
0X0
100
111
345 );
346 }
347  
348 ### Determine whether a session exists. We should only need to verify
349 ### this for sessions provided by the outside. Internally, our code
350 ### should be so clean it's not necessary.
351  
352 6674 ▶sub _data_ses_exists {
Subroutine _data_ses_exists: called CC=1 SCAR=0.0
353 7223 my ($self, $sid) = @_;
354 6674 return exists $kr_sessions{$sid};
355 }
356  
357 ### Resolve a session into its reference.
358  
359 3895 ▶sub _data_ses_resolve {
Subroutine _data_ses_resolve: called CC=2 SCAR=6.9
360 3895 my ($self, $session) = @_;
361 3895 ▶ return undef unless exists $kr_session_refs{$session}; # Prevents autoviv.
Branch: 1 branch
BranchTrueFalseTotal
unless exists $kr_session_refs{$session} 213 8629 8842
362 3685 return $kr_session_refs{$session};
363 }
364  
365 ### Resolve a session ID into its reference.
366  
367 34 ▶sub _data_ses_resolve_to_id {
Subroutine _data_ses_resolve_to_id: called CC=2 SCAR=6.9
368 34 my ($self, $session) = @_;
369 34 $session = $self->_data_ses_resolve($session);
370 34 ▶ return undef unless defined $session;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $session 4 1979 1983
371 32 return $session->ID;
372 }
373  
374 ### Sweep the GC marks.
375  
376 3810 ▶sub _data_ses_gc_sweep {
Subroutine _data_ses_gc_sweep: called CC=4 SCAR=13.9
377 3810 my $self = shift;
378  
379 3810 TRACE_REFCNT and _warn "<rc> trying sweep";
380 3810 ▶ while (@kr_marked_for_gc) {
Branch: 1 branch
BranchTrueFalseTotal
if (@kr_marked_for_gc) 837 4368 5205
381 837 my %temp_marked = %kr_marked_for_gc;
382 837 %kr_marked_for_gc = ();
383  
384 837 my @todo = reverse @kr_marked_for_gc;
385 837 @kr_marked_for_gc = ();
386  
387 # Never GC the POE::Kernel singleton.
388 837 delete $temp_marked{$self->ID};
389  
390 837 foreach my $sid (@todo) {
391 1550 ▶ next unless delete $temp_marked{$sid};
Branch: 1 branch
BranchTrueFalseTotal
unless delete $temp_marked{$sid} 1053 884 1937
392 497 $self->_data_ses_stop($sid);
393 }
394 }
395 }
396  
397 ### Decrement a session's main reference count. This is called by
398 ### each watcher when the last thing it watches for the session goes
399 ### away. In other words, a session's reference count should only
400 ### enumerate the different types of things being watched; not the
401 ### number of each.
402  
403 4598 ▶sub _data_ses_refcount_dec {
Subroutine _data_ses_refcount_dec: called CC=5 SCAR=16.1
404 4598 my ($self, $sid) = @_;
405  
406 4598 if (ASSERT_DATA) {
407 _trap("decrementing refcount of a nonexistent session")
408 unless exists $kr_sessions{$sid};
409 }
410  
411 4598 if (TRACE_REFCNT) {
412 _cluck(
413 "<rc> decrementing refcount for ",
414 $self->_data_alias_loggable($sid)
415 );
416 }
417  
418 4597 ▶ if (--$kr_sessions{$sid}->[SS_REFCOUNT] < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$kr_sessions{$sid}[1] < 1) 258 4426 4684
419 3790 TRACE_REFCNT and _warn "<rc> session $sid marked for gc";
420 2907 ▶ unless ($sid eq $self->ID) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sid eq $self->ID) 1347 2443 3790
421 1351 push @kr_marked_for_gc, $sid;
422 1099 $kr_marked_for_gc{$sid} = $sid;
423 }
424 }
425  
426 1792 $self->_data_ses_dump_refcounts($sid) if TRACE_REFCNT;
427  
428 2927 if (ASSERT_DATA and $kr_sessions{$sid}->[SS_REFCOUNT] < 0) {
429 _trap(
430 $self->_data_alias_loggable($sid),
431 " reference count went below zero"
432 );
433 }
434 }
435  
436 ### Increment a session's main reference count.
437  
438 5238 ▶sub _data_ses_refcount_inc {
Subroutine _data_ses_refcount_inc: called CC=3 SCAR=11.0
439 8776 my ($self, $sid) = @_;
440  
441 7391 if (ASSERT_DATA) {
442 _trap("incrementing refcount for nonexistent session")
443 unless exists $kr_sessions{$sid};
444 }
445  
446 5239 if (TRACE_REFCNT) {
447 _cluck(
448 "<rc> incrementing refcount for ",
449 $self->_data_alias_loggable($sid)
450 );
451 }
452  
453 5237 ▶ if (++$kr_sessions{$sid}->[SS_REFCOUNT] > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$kr_sessions{$sid}[1] > 0) { } 1926 4505 6431
454 5237 TRACE_REFCNT and _warn "<rc> session $sid unmarked for gc";
455 5237 delete $kr_marked_for_gc{$sid};
456 }
457 elsif (TRACE_REFCNT) {
458 _warn(
459 "??? session $sid refcount = $kr_sessions{$sid}->[SS_REFCOUNT]"
460 );
461 }
462  
463 5236 $self->_data_ses_dump_refcounts($sid) if TRACE_REFCNT;
464 }
465  
466 4807 ▶sub _data_ses_dump_refcounts {
Subroutine _data_ses_dump_refcounts: called CC=3 SCAR=11.0
467 8851 my ($self, $sid) = @_;
468  
469 4842 my $ss = $kr_sessions{$sid};
470  
471 _warn(
472 "<rc> +----- GC test for ", $self->_data_alias_loggable($sid), "-----\n",
473 "<rc> | total refcnt : ", $ss->[SS_REFCOUNT], "\n",
474 "<rc> | event count : ", $self->_data_ev_get_count_to($sid), "\n",
475 "<rc> | post count : ", $self->_data_ev_get_count_from($sid), "\n",
476 7423 "<rc> | child sessions: ", scalar(keys(%{$ss->[SS_CHILDREN]})), "\n",
477 "<rc> | handles in use: ", $self->_data_handle_count_ses($sid), "\n",
478 "<rc> | aliases in use: ", $self->_data_alias_count_ses($sid), "\n",
479 "<rc> | extra refs : ", $self->_data_extref_count_ses($sid), "\n",
480 "<rc> | pid count : ", $self->_data_sig_session_awaits_pids($sid), "\n",
481 "<rc> +---------------------------------------------------\n",
482 );
483  
484 4807 ▶ unless ($ss->[SS_REFCOUNT] and $self->_data_sig_session_awaits_pids($sid)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ss->[1] and $self->_data_sig_session_awaits_pids($sid)) 8654 3732 12386
Condition100%3/3
$ss->[1] and $self->_data_sig_session_awaits_pids($sid)
!ll&&!rl&&r
6993910198
MC/DC100%2/2
$ss->[1] and $self->_data_sig_session_awaits_pids($sid)
$ss->[1] $self->_data_sig_session_awaits_pids($sid)
Truth table100%3/3
A and B
A= $ss->[1]B= $self->_data_sig_session_awaits_pids($sid)
ABresult
0X0
100
111
485 4609 _warn(
486 "<rc> | ", $self->_data_alias_loggable($sid),
487 " is eligible for garbage collection.\n",
488 "<rc> +---------------------------------------------------\n",
489 );
490 }
491  
492 4807 _carp "<rc> | called";
493 }
494  
495 # Query a session's reference count. Added for testing purposes.
496  
497 43 ▶sub _data_ses_refcount {
Subroutine _data_ses_refcount: called CC=1 SCAR=0.0
498 43 my ($self, $sid) = @_;
499 43 return $kr_sessions{$sid}->[SS_REFCOUNT];
500 }
501  
502 ### Compatibility function to do a GC sweep on attempted garbage
503 ### collection. The tests still try to call this.
504  
505 1 ▶sub _data_ses_collect_garbage {
Subroutine _data_ses_collect_garbage: called CC=1 SCAR=0.0
506 1 my ($self, $sid) = @_;
507 # TODO - Deprecation warning.
508 1 $self->_data_ses_gc_sweep();
509 }
510  
511 ### Return the number of sessions we know about.
512  
513 4416 ▶sub _data_ses_count {
Subroutine _data_ses_count: called CC=1 SCAR=0.0
514 4416 return scalar keys %kr_sessions;
515 }
516  
517 ### Close down a session by force.
518  
519 # Stop a session, dispatching _stop, _parent, and _child as necessary.
520 #
521 # Dispatch _stop to a session, removing it from the kernel's data
522 # structures as a side effect.
523  
524 200 my %already_stopping;
525  
526 814 ▶sub _data_ses_stop {
Subroutine _data_ses_stop: called CC=6 SCAR=17.9
527 814 my ($self, $sid) = @_;
528  
529 # Don't stop a session that's already in the throes of stopping.
530 # This can happen with exceptions, during die() in _stop. It can
531 # probably be removed if exceptions are.
532  
533 814 ▶ return if exists $already_stopping{$sid};
Branch: 1 branch
BranchTrueFalseTotal
if exists $already_stopping{$sid} 0 814 814
534 814 $already_stopping{$sid} = 1;
535  
536 814 TRACE_REFCNT and _warn "<rc> stopping session $sid";
537  
538 814 if (ASSERT_DATA) {
539 _trap("stopping a nonexistent session") unless exists $kr_sessions{$sid};
540 }
541  
542 814 if (TRACE_SESSIONS) {
543 _warn("<ss> stopping ", $self->_data_alias_loggable($sid));
544 }
545  
546 # Maintain referential integrity between parents and children.
547 # First move the children of the stopping session up to its parent.
548 813 my $parent = $self->_data_ses_get_parent($sid);
549  
550 813 foreach my $child ($self->_data_ses_get_children($sid)) {
551 707 $self->_dispatch_event(
552 $parent, $self,
553 EN_CHILD, ET_CHILD, [ CHILD_GAIN, $child ],
554 __FILE__, __LINE__, undef, monotime(), -__LINE__
555 );
556 386 $self->_dispatch_event(
557 $child, $self,
558 EN_PARENT, ET_PARENT,
559 [ $self->_data_ses_get_parent($child->ID), $parent, ],
560 __FILE__, __LINE__, undef, monotime(), -__LINE__
561 );
562 }
563  
564 # Referential integrity has been dealt with. Now notify the session
565 # that it has been stopped.
566  
567 107 my $session = $kr_sessions{$sid}[SS_SESSION];
568 428 my $stop_return = $self->_dispatch_event(
569 $session, $self->get_active_session(),
570 EN_STOP, ET_STOP, [],
571 __FILE__, __LINE__, undef, monotime(), -__LINE__
572 );
573  
574 # If the departing session has a parent, notify it that the session
575 # is being lost.
576  
577 813 ▶ if (defined $parent) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parent) 73 741 814
578 779 $self->_dispatch_event(
579 $parent, $self,
580 EN_CHILD, ET_CHILD, [ CHILD_LOSE, $session, $stop_return ],
581 __FILE__, __LINE__, undef, monotime(), -__LINE__
582 );
583 }
584  
585 # Deallocate the session.
586  
587 727 $self->_data_ses_free($sid);
588  
589 # Stop the main loop if everything is gone.
590 # XXX - Under Tk this is called twice. Why? WHY is it called twice?
591 729 ▶ unless (keys %kr_sessions) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %kr_sessions) 573 240 813
592 741 $self->loop_halt();
593 }
594  
595 578 delete $already_stopping{$sid};
596 }
597  
598 200 1;
599  
600 __END__
« mylib/gen-tests.perl ↑ Summary blib/lib/POE/Wheel/ListenAccept.pm »