| 1 | # The data necessary to manage tagged extra/external reference counts | ||||||||||
| 2 | # on sessions, and the accessors to get at them sanely from other | ||||||||||
| 3 | # files. | ||||||||||
| 4 | |||||||||||
| 5 | package POE::Resource::Extrefs; | ||||||||||
| 6 | |||||||||||
| 7 | 200 | ▶ | use vars qw($VERSION); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 200 | $VERSION = '1.370'; # NOTE - Should be #.### (three decimal places) | |||||||||
| 9 | |||||||||||
| 10 | # These methods are folded into POE::Kernel; | ||||||||||
| 11 | package POE::Kernel; | ||||||||||
| 12 | |||||||||||
| 13 | 200 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | ### The count of all extra references used in the system. | ||||||||||
| 16 | |||||||||||
| 17 | 200 | my %kr_extra_refs; | |||||||||
| 18 | # ( $session_id => | ||||||||||
| 19 | # { $tag => $count, | ||||||||||
| 20 | # ..., | ||||||||||
| 21 | # }, | ||||||||||
| 22 | # ..., | ||||||||||
| 23 | # ); | ||||||||||
| 24 | |||||||||||
| 25 | 0 | ▶ | sub _data_extref_relocate_kernel_id { | ||||||||
|
Subroutine
_data_extref_relocate_kernel_id: not called CC=2 SCAR=17.9
| |||||||||||
| 26 | 0 | my ($self, $old_id, $new_id) = @_; | |||||||||
| 27 | 0 | ▶ | return unless exists $kr_extra_refs{$old_id}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 28 | 0 | $kr_extra_refs{$new_id} = delete $kr_extra_refs{$old_id}; | |||||||||
| 29 | } | ||||||||||
| 30 | |||||||||||
| 31 | ### End-run leak checking. | ||||||||||
| 32 | |||||||||||
| 33 | 204 | ▶ | sub _data_extref_finalize { | ||||||||
|
Subroutine
_data_extref_finalize: called CC=3 SCAR=16.5
| |||||||||||
| 34 | 204 | my $finalized_ok = 1; | |||||||||
| 35 | 204 | foreach my $session_id (keys %kr_extra_refs) { | |||||||||
| 36 | 0 | $finalized_ok = 0; | |||||||||
| 37 | 0 | _warn "!!! Leaked extref: $session_id\n"; | |||||||||
| 38 | 0 | foreach my $tag (keys %{$kr_extra_refs{$session_id}}) { | |||||||||
| 39 | 0 | _warn "!!!\t`$tag' = $kr_extra_refs{$session_id}->{$tag}\n"; | |||||||||
| 40 | } | ||||||||||
| 41 | } | ||||||||||
| 42 | 204 | return $finalized_ok; | |||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | # Increment a session's tagged reference count. If this is the first | ||||||||||
| 46 | # time the tag is used in the session, then increment the session's | ||||||||||
| 47 | # reference count as well. Returns the tag's new reference count. | ||||||||||
| 48 | # | ||||||||||
| 49 | # TODO Allows incrementing reference counts on sessions that don't | ||||||||||
| 50 | # exist, but the public interface catches that. | ||||||||||
| 51 | # | ||||||||||
| 52 | # TODO Need to track extref ownership for signal-based session | ||||||||||
| 53 | # termination. One problem seen is that signals terminate sessions | ||||||||||
| 54 | # out of order. Owners think extra refcounts exist for sessions that | ||||||||||
| 55 | # are no longer around. Ownership trees give us a few benefits: We | ||||||||||
| 56 | # can make sure sessions destruct in a cleaner order. We can detect | ||||||||||
| 57 | # refcount loops and possibly prevent that. | ||||||||||
| 58 | |||||||||||
| 59 | 109 | ▶ | sub _data_extref_inc { | ||||||||
|
Subroutine
_data_extref_inc: called CC=2 SCAR=6.9
| |||||||||||
| 60 | 109 | my ($self, $sid, $tag) = @_; | |||||||||
| 61 | 109 | my $refcount = ++$kr_extra_refs{$sid}->{$tag}; | |||||||||
| 62 | |||||||||||
| 63 | # TODO We could probably get away with only incrementing the | ||||||||||
| 64 | # session's master refcount once, as long as any extra refcount is | ||||||||||
| 65 | # positive. Then the session reference count would be a flag | ||||||||||
| 66 | # instead of a counter. | ||||||||||
| 67 | 109 | ▶ | $self->_data_ses_refcount_inc($sid) if $refcount == 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | |||||||||||
| 69 | 109 | if (TRACE_REFCNT) { | |||||||||
| 70 | _warn( | ||||||||||
| 71 | "<rc> incremented extref ``$tag'' (now $refcount) for ", | ||||||||||
| 72 | $self->_data_alias_loggable($sid) | ||||||||||
| 73 | ); | ||||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | 109 | return $refcount; | |||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | # Decrement a session's tagged reference count, removing it outright | ||||||||||
| 80 | # if the count reaches zero. Return the new reference count or undef | ||||||||||
| 81 | # if the tag doesn't exist. | ||||||||||
| 82 | # | ||||||||||
| 83 | # TODO Allows negative reference counts, and the resulting hilarity. | ||||||||||
| 84 | # Hopefully the public interface won't allow it. | ||||||||||
| 85 | |||||||||||
| 86 | 106 | ▶ | sub _data_extref_dec { | ||||||||
|
Subroutine
_data_extref_dec: called CC=4 SCAR=13.9
| |||||||||||
| 87 | 138 | my ($self, $sid, $tag) = @_; | |||||||||
| 88 | |||||||||||
| 89 | 106 | if (ASSERT_DATA) { | |||||||||
| 90 | # Prevents autoviv. | ||||||||||
| 91 | _trap("<dt> decrementing extref for session without any") | ||||||||||
| 92 | unless exists $kr_extra_refs{$sid}; | ||||||||||
| 93 | |||||||||||
| 94 | unless (exists $kr_extra_refs{$sid}->{$tag}) { | ||||||||||
| 95 | _trap( | ||||||||||
| 96 | "<dt> decrementing extref for nonexistent tag ``$tag'' in ", | ||||||||||
| 97 | $self->_data_alias_loggable($sid) | ||||||||||
| 98 | ); | ||||||||||
| 99 | } | ||||||||||
| 100 | } | ||||||||||
| 101 | |||||||||||
| 102 | 106 | my $refcount = --$kr_extra_refs{$sid}->{$tag}; | |||||||||
| 103 | |||||||||||
| 104 | 105 | if (TRACE_REFCNT) { | |||||||||
| 105 | _warn( | ||||||||||
| 106 | "<rc> decremented extref ``$tag'' (now $refcount) for ", | ||||||||||
| 107 | $self->_data_alias_loggable($sid) | ||||||||||
| 108 | ); | ||||||||||
| 109 | } | ||||||||||
| 110 | |||||||||||
| 111 | 2 | ▶ | $self->_data_extref_remove($sid, $tag) unless $refcount; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 112 | 104 | return $refcount; | |||||||||
| 113 | } | ||||||||||
| 114 | |||||||||||
| 115 | ### Remove an extra reference from a session, regardless of its count. | ||||||||||
| 116 | |||||||||||
| 117 | 101 | ▶ | sub _data_extref_remove { | ||||||||
|
Subroutine
_data_extref_remove: called CC=4 SCAR=13.9
| |||||||||||
| 118 | 204 | my ($self, $sid, $tag) = @_; | |||||||||
| 119 | |||||||||||
| 120 | 204 | if (ASSERT_DATA) { | |||||||||
| 121 | # Prevents autoviv. | ||||||||||
| 122 | _trap("<dt> removing extref from session without any") | ||||||||||
| 123 | unless exists $kr_extra_refs{$sid}; | ||||||||||
| 124 | unless (exists $kr_extra_refs{$sid}->{$tag}) { | ||||||||||
| 125 | _trap( | ||||||||||
| 126 | "<dt> removing extref for nonexistent tag ``$tag'' in ", | ||||||||||
| 127 | $self->_data_alias_loggable($sid) | ||||||||||
| 128 | ); | ||||||||||
| 129 | } | ||||||||||
| 130 | } | ||||||||||
| 131 | |||||||||||
| 132 | 204 | delete $kr_extra_refs{$sid}->{$tag}; | |||||||||
| 133 | 127 | ▶ | delete $kr_extra_refs{$sid} unless scalar keys %{$kr_extra_refs{$sid}}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 134 | 99 | $self->_data_ses_refcount_dec($sid); | |||||||||
| 135 | } | ||||||||||
| 136 | |||||||||||
| 137 | ### Clear all the extra references from a session. | ||||||||||
| 138 | |||||||||||
| 139 | 817 | ▶ | sub _data_extref_clear_session { | ||||||||
|
Subroutine
_data_extref_clear_session: called CC=4 SCAR=13.9
| |||||||||||
| 140 | 915 | my ($self, $sid) = @_; | |||||||||
| 141 | |||||||||||
| 142 | # TODO - Should there be a _trap here if the session doesn't exist? | ||||||||||
| 143 | |||||||||||
| 144 | 915 | ▶ | return unless exists $kr_extra_refs{$sid}; # avoid autoviv | ||||||||
|
Branch: 1 branch
| |||||||||||
| 145 | 99 | foreach (keys %{$kr_extra_refs{$sid}}) { | |||||||||
| 146 | 2 | $self->_data_extref_remove($sid, $_); | |||||||||
| 147 | } | ||||||||||
| 148 | |||||||||||
| 149 | 1 | if (ASSERT_DATA) { | |||||||||
| 150 | if (exists $kr_extra_refs{$sid}) { | ||||||||||
| 151 | _trap( | ||||||||||
| 152 | "<dt> extref clear did not remove session ", | ||||||||||
| 153 | $self->_data_alias_loggable($sid) | ||||||||||
| 154 | ); | ||||||||||
| 155 | } | ||||||||||
| 156 | } | ||||||||||
| 157 | } | ||||||||||
| 158 | |||||||||||
| 159 | # Fetch the number of sessions with extra references held in the | ||||||||||
| 160 | # entire system. | ||||||||||
| 161 | |||||||||||
| 162 | 1422 | ▶ | sub _data_extref_count { | ||||||||
|
Subroutine
_data_extref_count: called CC=1 SCAR=0.0
| |||||||||||
| 163 | 1423 | return scalar keys %kr_extra_refs; | |||||||||
| 164 | } | ||||||||||
| 165 | |||||||||||
| 166 | # Fetch whether a session has extra references. | ||||||||||
| 167 | |||||||||||
| 168 | 4812 | ▶ | sub _data_extref_count_ses { | ||||||||
|
Subroutine
_data_extref_count_ses: called CC=2 SCAR=6.9
| |||||||||||
| 169 | 4812 | my ($self, $sid) = @_; | |||||||||
| 170 | 4812 | ▶ | return 0 unless exists $kr_extra_refs{$sid}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 171 | 70 | return scalar keys %{$kr_extra_refs{$sid}}; | |||||||||
| 172 | } | ||||||||||
| 173 | |||||||||||
| 174 | 200 | 1; | |||||||||
| 175 | |||||||||||
| 176 | __END__ | ||||||||||