Add emulation layer for Thread/Semaphore and Thread/Queue
[p5sagit/p5-mst-13.2.git] / ext / Thread / Thread / Signal.pm
CommitLineData
3aeed370 1package Thread::Signal;
2use Thread qw(async);
3
28b605d8 4our $VERSION = '1.00';
5
3aeed370 6=head1 NAME
7
8Thread::Signal - Start a thread which runs signal handlers reliably
9
10=head1 SYNOPSIS
11
12 use Thread::Signal;
13
14 $SIG{HUP} = \&some_handler;
15
16=head1 DESCRIPTION
17
18The C<Thread::Signal> module starts up a special signal handler thread.
19All signals to the process are delivered to it and it runs the
20associated C<$SIG{FOO}> handlers for them. Without this module,
21signals arriving at inopportune moments (such as when perl's internals
22are in the middle of updating critical structures) cause the perl
23code of the handler to be run unsafely which can cause memory corruption
24or worse.
25
26=head1 BUGS
27
28This module changes the semantics of signal handling slightly in that
29the signal handler is run separately from the main thread (and in
30parallel with it). This means that tricks such as calling C<die> from
31a signal handler behave differently (and, in particular, can't be
32used to exit directly from a system call).
33
34=cut
35
36if (!init_thread_signals()) {
37 require Carp;
38 Carp::croak("init_thread_signals failed: $!");
39}
40
41async {
42 my $sig;
43 while ($sig = await_signal()) {
44 &$sig();
45 }
46};
47
48END {
49 kill_sighandler_thread();
50}
51
521;