1 package Devel::GlobalDestruction;
8 use Sub::Exporter -setup => {
9 exports => [ qw(in_global_destruction) ],
10 groups => { default => [ -all ] },
13 if (defined ${^GLOBAL_PHASE}) {
14 eval 'sub in_global_destruction () { ${^GLOBAL_PHASE} eq q[DESTRUCT] }';
18 XSLoader::load(__PACKAGE__, $VERSION);
21 # the eval already installed everything, nothing to do
24 eval <<'PP_IGD' or die $@;
26 my ($in_global_destruction, $before_is_installed);
28 sub in_global_destruction { $in_global_destruction }
31 # SpeedyCGI runs END blocks every cycle but somehow keeps object instances
32 # hence lying about it seems reasonable...ish
33 $in_global_destruction = 1 unless $CGI::SpeedyCGI::i_am_speedy;
36 # threads do not execute the global ENDs (it would be stupid). However
37 # one can register a new END via simple string eval within a thread, and
38 # achieve the same result. A logical place to do this would be CLONE, which
39 # is claimed to run in the context of the new thread. However this does
40 # not really seem to be the case - any END evaled in a CLONE is ignored :(
41 # Hence blatantly hooking threads::create
43 if ($INC{'threads.pm'}) {
44 my $orig_create = threads->can('create');
45 no warnings 'redefine';
46 *threads::create = sub {
47 { local $@; eval 'END { $in_global_destruction = 1 }' };
50 $before_is_installed = 1;
53 # just in case threads got loaded after us (silly)
55 unless ($before_is_installed) {
57 Carp::croak("You must load the 'threads' module before @{[ __PACKAGE__ ]}");
67 1; # keep require happy
74 Devel::GlobalDestruction - Expose the flag which marks global
80 use Devel::GlobalDestruction;
82 use namespace::clean; # to avoid having an "in_global_destruction" method
85 return if in_global_destruction;
87 do_something_a_little_tricky();
92 Perl's global destruction is a little tricky to deal with WRT finalizers
93 because it's not ordered and objects can sometimes disappear.
95 Writing defensive destructors is hard and annoying, and usually if global
96 destruction is happenning you only need the destructors that free up non
97 process local resources to actually execute.
99 For these constructors you can avoid the mess by simply bailing out if global
100 destruction is in effect.
104 This module uses L<Sub::Exporter> so the exports may be renamed, aliased, etc.
108 =item in_global_destruction
110 Returns true if the interpreter is in global destruction. In perl 5.14+, this
111 returns C<${^GLOBAL_PHASE} eq 'DESTRUCT'>, and on earlier perls, it returns the
112 current value of C<PL_dirty>.
118 Yuval Kogman E<lt>nothingmuch@woobling.orgE<gt>
120 Florian Ragwitz E<lt>rafl@debian.orgE<gt>
122 Jesse Luehrs E<lt>doy@tozt.netE<gt>
124 Peter Rabbitson E<lt>ribasushi@cpan.orgE<gt>
128 Copyright (c) 2008 Yuval Kogman. All rights reserved
129 This program is free software; you can redistribute
130 it and/or modify it under the same terms as Perl itself.