1 package Devel::GlobalDestruction;
8 use Sub::Exporter::Progressive -setup => {
9 exports => [ qw(in_global_destruction) ],
10 groups => { default => [ -all ] },
13 # we run 5.14+ - everything is in core
15 if (defined ${^GLOBAL_PHASE}) {
16 eval 'sub in_global_destruction () { ${^GLOBAL_PHASE} eq q[DESTRUCT] }; 1'
19 # try to load the xs version if it was compiled
23 XSLoader::load(__PACKAGE__, $VERSION);
26 # the eval already installed everything, nothing to do
28 # We need pure-perl and we are running under -c
29 # None of the END-block trickery will work, use a global scope guard instead,
30 # as it is more than adequate in this situation
31 # The whole thing is in an eval to prevent perl from parsing it in the
32 # first place where none of this is needed
35 eval <<'PP_IGD' or die $@;
37 my $in_global_destruction;
39 sub in_global_destruction () { $in_global_destruction }
42 package Devel::GlobalDestgruction::_MinusC::ScopeGuard;
43 sub DESTROY { shift->[0]->() };
47 $Devel::GlobalDestgruction::_MinusC::guard = bless [sub {
48 $in_global_destruction = 1;
49 }], 'Devel::GlobalDestgruction::_MinusC::ScopeGuard';
56 # The whole thing is in an eval to prevent perl from parsing it in the
57 # first place under perls where none of this is needed
60 eval <<'PP_IGD' or die $@;
62 # SpeedyCGI runs END blocks every cycle but somehow keeps object instances
64 die("The pure-perl version of @{[__PACKAGE__]} can not function correctly under CGI::SpeedyCGI. "
65 . "Please ensure you have a working compiler, and reinstall @{[__PACKAGE__]} to enable the XS "
67 ) if $CGI::SpeedyCGI::i_am_speedy;
69 my ($in_global_destruction, $before_is_installed);
71 sub in_global_destruction () { $in_global_destruction }
73 # end_av trick suggested by liz++
75 my $add_endblock = sub {
76 push @{ B::end_av()->object_2svref }, sub { $in_global_destruction = 1 };
79 # This block will fire towards the end of the program execution
80 # Use it to inject an END block which is guaranteed to run last
81 # (as long as something else doesn't inject yet another block in
82 # the same manner afterwards, at which point it hardly matters
85 END { $add_endblock->() }
87 # threads do not execute the global ENDs (it would be stupid). However
88 # one can register a new thread-local END from within a thread, and
89 # achieve the same result. A logical place to do this would be CLONE, which
90 # is claimed to run in the context of the new thread. However this does
91 # not really seem to be the case - any END inserted in a CLONE is ignored :(
92 # Hence blatantly hooking threads::create
94 if ($INC{'threads.pm'}) {
97 my $orig_create = threads->can('create');
98 no warnings 'redefine';
100 *threads::create = sub {
104 unless ( (Scalar::Util::reftype($target)||'') eq 'CODE' ) {
106 $target = \&{ caller() . "::$target" };
112 # Perls compiled with THREADS_HAVE_PIDS do not copy end_av properly
113 # between threads, so B::end_av ends up returning a B::SPECIAL and it
114 # goes downhill from there
115 # Install a noop END just to be on the safe side
116 { local $@; eval 'END {}' }
126 $before_is_installed = 1;
129 # just in case threads got loaded after us (silly)
131 unless ($before_is_installed) {
133 Carp::croak("You must load the 'threads' module before @{[ __PACKAGE__ ]}");
143 1; # keep require happy
150 Devel::GlobalDestruction - Expose the flag which marks global
156 use Devel::GlobalDestruction;
158 use namespace::clean; # to avoid having an "in_global_destruction" method
161 return if in_global_destruction;
163 do_something_a_little_tricky();
168 Perl's global destruction is a little tricky to deal with WRT finalizers
169 because it's not ordered and objects can sometimes disappear.
171 Writing defensive destructors is hard and annoying, and usually if global
172 destruction is happenning you only need the destructors that free up non
173 process local resources to actually execute.
175 For these constructors you can avoid the mess by simply bailing out if global
176 destruction is in effect.
180 This module uses L<Sub::Exporter::Progressive> so the exports may be renamed,
181 aliased, etc. if L<Sub::Exporter> is present.
185 =item in_global_destruction
187 Returns true if the interpreter is in global destruction. In perl 5.14+, this
188 returns C<${^GLOBAL_PHASE} eq 'DESTRUCT'>, and on earlier perls, it returns the
189 current value of C<PL_dirty>.
195 Yuval Kogman E<lt>nothingmuch@woobling.orgE<gt>
197 Florian Ragwitz E<lt>rafl@debian.orgE<gt>
199 Jesse Luehrs E<lt>doy@tozt.netE<gt>
201 Peter Rabbitson E<lt>ribasushi@cpan.orgE<gt>
203 Arthur Axel 'fREW' Schmidt E<lt>frioux@gmail.comE<gt>
205 Elizabeth Mattijsen E<lt>liz@dijkmat.nlE<gt>
209 Copyright (c) 2008 Yuval Kogman. All rights reserved
210 This program is free software; you can redistribute
211 it and/or modify it under the same terms as Perl itself.