]> git.vpit.fr Git - perl/modules/Thread-Cleanup.git/blob - lib/Thread/Cleanup.pm
f7f815b60e7192b45ff3b3aa091056ece88280a6
[perl/modules/Thread-Cleanup.git] / lib / Thread / Cleanup.pm
1 package Thread::Cleanup;
2
3 use 5.008;
4
5 use strict;
6 use warnings;
7
8 =head1 NAME
9
10 Thread::Cleanup - Hook thread destruction.
11
12 =head1 VERSION
13
14 Version 0.01
15
16 =cut
17
18 our $VERSION;
19
20 BEGIN {
21  $VERSION = '0.01';
22  require XSLoader;
23  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<threads>.
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 =head1 FUNCTIONS
44
45 =head2 C<register BLOCK>
46
47 Specify that the C<BLOCK> will have to be called (in void context, without arguments) every time a thread finishes is job.
48 More precisely,
49
50 =over 4
51
52 =item *
53
54 it will always be called before the join for joined threads ;
55
56 =item *
57
58 it will be called for detached threads only if they terminate before the main thread, and the hook will then fire at C<END> time ;
59
60 =item *
61
62 it won't trigger for the the destruction of the main thread.
63
64 =back
65
66 =cut
67
68 my @callbacks;
69
70 sub register (&) { push @callbacks, shift }
71
72 sub _CLEANUP { $_->() for @callbacks }
73
74 =head1 EXPORT
75
76 None.
77
78 =head1 DEPENDENCIES
79
80 C<perl> 5.8.
81
82 C<threads> 1.07.
83
84 C<XSLoader>.
85
86 =head1 AUTHOR
87
88 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
89
90 =head1 BUGS
91
92 Please report any bugs or feature requests to C<bug-thread-cleanup at rt.cpan.org>, or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Thread-Cleanup>.  I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
93
94 =head1 SUPPORT
95
96 You can find documentation for this module with the perldoc command.
97
98     perldoc Thread::Cleanup
99
100 =head1 ACKNOWLEDGEMENTS
101
102 Inspired by a question from TonyC on #p5p.
103
104 =head1 COPYRIGHT & LICENSE
105
106 Copyright 2009 Vincent Pit, all rights reserved.
107
108 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
109
110 =cut
111
112 1; # End of Thread::Cleanup