File Coverage

blib/lib/Thread/Cleanup.pm
Criterion Covered Total %
statement 12 14 85.7
branch n/a
condition n/a
subroutine 4 6 66.6
pod 1 1 100.0
total 17 21 80.9


line stmt bran cond sub pod time code
1             package Thread::Cleanup;
2              
3 2     2   30748 use 5.008;
  2         6  
  2         59  
4              
5 2     2   8 use strict;
  2         3  
  2         50  
6 2     2   7 use warnings;
  2         7  
  2         105  
7              
8             =head1 NAME
9              
10             Thread::Cleanup - Hook thread destruction.
11              
12             =head1 VERSION
13              
14             Version 0.06
15              
16             =cut
17              
18             our $VERSION;
19              
20             BEGIN {
21 2     2   4 $VERSION = '0.06';
22 2         8 require XSLoader;
23 2         928 XSLoader::load(__PACKAGE__, $VERSION);
24             }
25              
26             =head1 SYNOPSIS
27              
28             use Thread::Cleanup;
29              
30             use threads;
31              
32             Thread::Cleanup::register {
33             my $tid = threads->tid();
34             warn "Thread $tid finished\n";
35             };
36              
37             =head1 DESCRIPTION
38              
39             This module allows you to hook thread destruction without fiddling with the internals of L.
40              
41             It acts globally on all the threads that may spawn anywhere in your program, with the exception of the main thread.
42              
43             The hook will also be called when pseudo-forks (i.e. processes spawn on Windows for the C emulation) terminate.
44              
45             =head1 FUNCTIONS
46              
47             =head2 C
48              
49             register { ... };
50             ®ister($coderef);
51              
52             Specify that the given block or code reference C<$coderef> will have to be called (in void context, without arguments) every time a thread or a pseudo-fork terminates.
53             More precisely :
54              
55             =over 4
56              
57             =item *
58              
59             For joined threads, it will be called when C succeeds, after any C block local to the spawn thread ;
60              
61             =item *
62              
63             For detached threads, it will be called if and only if the thread terminates before the main thread, and the hook will then fire at global C time ;
64              
65             =item *
66              
67             For pseudo-forks, it will be called when C succeeds, after any C block local to the spawn process but before any global C block ;
68              
69             =item *
70              
71             It will never trigger for the destruction of the main thread.
72              
73             =back
74              
75             =cut
76              
77             my @callbacks;
78              
79 0     0 1   sub register (&) { push @callbacks, shift }
80              
81 0     0     sub _CLEANUP { $_->() for @callbacks }
82              
83             =head1 EXPORT
84              
85             None.
86              
87             =head1 DEPENDENCIES
88              
89             L 5.8.
90              
91             A C compiler.
92             This module may happen to build with a C++ compiler as well, but don't rely on it, as no guarantee is made in this regard.
93              
94             L 1.07.
95              
96             L.
97              
98             =head1 AUTHOR
99              
100             Vincent Pit, C<< >>, L.
101              
102             =head1 BUGS
103              
104             Please report any bugs or feature requests to C, or through the web interface at L. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
105              
106             =head1 SUPPORT
107              
108             You can find documentation for this module with the perldoc command.
109              
110             perldoc Thread::Cleanup
111              
112             =head1 ACKNOWLEDGEMENTS
113              
114             Inspired by a question from TonyC on #p5p.
115              
116             =head1 COPYRIGHT & LICENSE
117              
118             Copyright 2009,2010,2013,2014 Vincent Pit, all rights reserved.
119              
120             This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
121              
122             =cut
123              
124             1; # End of Thread::Cleanup