Be on the safe side
[p5sagit/Devel-GlobalDestruction.git] / lib / Devel / GlobalDestruction.pm
1 package Devel::GlobalDestruction;
2
3 use strict;
4 use warnings;
5
6 our $VERSION = '0.08';
7
8 use Sub::Exporter::Progressive -setup => {
9   exports => [ qw(in_global_destruction) ],
10   groups  => { default => [ -all ] },
11 };
12
13 # we run 5.14+ - everything is in core
14 #
15 if (defined ${^GLOBAL_PHASE}) {
16   eval 'sub in_global_destruction () { ${^GLOBAL_PHASE} eq q[DESTRUCT] }; 1'
17     or die $@;
18 }
19 # try to load the xs version if it was compiled
20 #
21 elsif (eval {
22   require XSLoader;
23   XSLoader::load(__PACKAGE__, $VERSION);
24   1;
25 }) {
26   # the eval already installed everything, nothing to do
27 }
28 # Not core nor XS
29 # The whole thing is in an eval to prevent perl from parsing it in the
30 # first place under perls where none of this is needed
31 #
32 else {
33   eval <<'PP_IGD' or die $@;
34
35 # SpeedyCGI runs END blocks every cycle but somehow keeps object instances
36 # hence DIAF
37 die("The pure-perl version of @{[__PACKAGE__]} can not function correctly under CGI::SpeedyCGI. "
38   . "Please ensure you have a working compiler, and reinstall @{[__PACKAGE__]} to enable the XS "
39   . "codepath.\n"
40 ) if $CGI::SpeedyCGI::i_am_speedy;
41
42 my ($in_global_destruction, $before_is_installed);
43
44 sub in_global_destruction () { $in_global_destruction }
45
46 # end_av trick suggested by liz++
47 require B;
48 my $add_endblock = sub {
49   push @{ B::end_av()->object_2svref }, sub { $in_global_destruction = 1 };
50 };
51
52 # This block will fire towards the end of the program execution
53 # Use it to inject an END block which is guaranteed to run last
54 # (as long as something else doesn't inject yet another block in
55 # the same manner afterwards, at which point it hardly matters
56 # anyway)
57 #
58 END { $add_endblock->() }
59
60 # threads do not execute the global ENDs (it would be stupid). However
61 # one can register a new thread-local END from within a thread, and
62 # achieve the same result. A logical place to do this would be CLONE, which
63 # is claimed to run in the context of the new thread. However this does
64 # not really seem to be the case - any END inserted in a CLONE is ignored :(
65 # Hence blatantly hooking threads::create
66 #
67 if ($INC{'threads.pm'}) {
68   require Scalar::Util;
69
70   my $orig_create = threads->can('create');
71   no warnings 'redefine';
72
73   *threads::create = sub {
74     my $class = shift;
75     my $target = shift;
76
77     unless ( (Scalar::Util::reftype($target)||'') eq 'CODE' ) {
78       no strict 'refs';
79       $target = \&{ caller() . "::$target" };
80     }
81
82     @_ = (
83       $class,
84       sub { $add_endblock->(); goto $target },
85       @_,
86     );
87
88     goto $orig_create;
89   };
90
91   $before_is_installed = 1;
92 }
93
94 # just in case threads got loaded after us (silly)
95 sub CLONE {
96   unless ($before_is_installed) {
97     require Carp;
98     Carp::croak("You must load the 'threads' module before @{[ __PACKAGE__ ]}");
99   }
100 }
101
102 1;  # keep eval happy
103
104 PP_IGD
105
106 }
107
108 1;  # keep require happy
109
110
111 __END__
112
113 =head1 NAME
114
115 Devel::GlobalDestruction - Expose the flag which marks global
116 destruction.
117
118 =head1 SYNOPSIS
119
120     package Foo;
121     use Devel::GlobalDestruction;
122
123     use namespace::clean; # to avoid having an "in_global_destruction" method
124
125     sub DESTROY {
126         return if in_global_destruction;
127
128         do_something_a_little_tricky();
129     }
130
131 =head1 DESCRIPTION
132
133 Perl's global destruction is a little tricky to deal with WRT finalizers
134 because it's not ordered and objects can sometimes disappear.
135
136 Writing defensive destructors is hard and annoying, and usually if global
137 destruction is happenning you only need the destructors that free up non
138 process local resources to actually execute.
139
140 For these constructors you can avoid the mess by simply bailing out if global
141 destruction is in effect.
142
143 =head1 EXPORTS
144
145 This module uses L<Sub::Exporter::Progressive> so the exports may be renamed,
146 aliased, etc. if L<Sub::Exporter> is present.
147
148 =over 4
149
150 =item in_global_destruction
151
152 Returns true if the interpreter is in global destruction. In perl 5.14+, this
153 returns C<${^GLOBAL_PHASE} eq 'DESTRUCT'>, and on earlier perls, it returns the
154 current value of C<PL_dirty>.
155
156 =back
157
158 =head1 AUTHORS
159
160 Yuval Kogman E<lt>nothingmuch@woobling.orgE<gt>
161
162 Florian Ragwitz E<lt>rafl@debian.orgE<gt>
163
164 Jesse Luehrs E<lt>doy@tozt.netE<gt>
165
166 Peter Rabbitson E<lt>ribasushi@cpan.orgE<gt>
167
168 Arthur Axel 'fREW' Schmidt E<lt>frioux@gmail.comE<gt>
169
170 Elizabeth Mattijsen E<lt>liz@dijkmat.nlE<gt>
171
172 =head1 COPYRIGHT
173
174     Copyright (c) 2008 Yuval Kogman. All rights reserved
175     This program is free software; you can redistribute
176     it and/or modify it under the same terms as Perl itself.
177
178 =cut