1 |
root |
1.1 |
=head1 NAME |
2 |
|
|
|
3 |
|
|
Async::Interrupt - allow C/XS libraries to interrupt perl asynchronously |
4 |
|
|
|
5 |
|
|
=head1 SYNOPSIS |
6 |
|
|
|
7 |
|
|
use Async::Interrupt; |
8 |
|
|
|
9 |
|
|
=head1 DESCRIPTION |
10 |
|
|
|
11 |
|
|
This module implements a single feature only of interest to advanced perl |
12 |
|
|
modules, namely asynchronous interruptions (think "unix signals", which |
13 |
|
|
are very similar). |
14 |
|
|
|
15 |
|
|
Sometimes, modules wish to run code asynchronously (in another thread), |
16 |
|
|
and then signal the perl interpreter on certain events. One common way is |
17 |
|
|
to write some data to a pipe and use an event handling toolkit to watch |
18 |
|
|
for I/O events. Another way is to send a signal. Those methods are slow, |
19 |
|
|
and in the case of a pipe, also not asynchronous - it won't interrupt a |
20 |
|
|
running perl interpreter. |
21 |
|
|
|
22 |
|
|
This module implements asynchronous notifications that enable you to |
23 |
|
|
signal running perl code form another thread, asynchronously, without |
24 |
|
|
issuing syscalls. |
25 |
|
|
|
26 |
root |
1.2 |
It works by creating an C<Async::Interrupt> object for each such use. This |
27 |
|
|
object stores a perl and/or a C-level callback that is invoked when the |
28 |
|
|
C<Async::Interrupt> object gets signalled. It is executed at the next time |
29 |
|
|
the perl interpreter is running (i.e. it will interrupt a computation, but |
30 |
|
|
not an XS function or a syscall). |
31 |
|
|
|
32 |
|
|
You can signal the C<Async::Interrupt> object either by calling it's C<< |
33 |
|
|
->signal >> method, or, more commonly, by calling a C function. |
34 |
|
|
|
35 |
|
|
The C<< ->signal_func >> returns the address of the C function that is to |
36 |
|
|
be called (plus an argument to be used during the call). The signalling |
37 |
|
|
function also takes an integer argument in the range SIG_ATOMIC_MIN to |
38 |
|
|
SIG_ATOMIC_MAX (guaranteed to allow at least 0..127). |
39 |
|
|
|
40 |
|
|
Since this kind of interruption is fast, but can only interrupt a |
41 |
|
|
I<running> interpreter, there is optional support for also signalling a |
42 |
|
|
pipe - that means you can also wait for the pipe to become readable while |
43 |
|
|
|
44 |
root |
1.1 |
=over 4 |
45 |
|
|
|
46 |
|
|
=cut |
47 |
|
|
|
48 |
|
|
package Async::Interrupt; |
49 |
|
|
|
50 |
root |
1.2 |
no warnings; |
51 |
|
|
|
52 |
root |
1.1 |
BEGIN { |
53 |
|
|
$VERSION = '0.02'; |
54 |
|
|
|
55 |
|
|
require XSLoader; |
56 |
|
|
XSLoader::load Async::Interrupt::, $VERSION; |
57 |
|
|
} |
58 |
|
|
|
59 |
root |
1.2 |
our $DIED = sub { warn "$@" }; |
60 |
|
|
|
61 |
root |
1.1 |
=item $async = new Async::Interrupt key => value... |
62 |
|
|
|
63 |
|
|
Creates a new Async::Interrupt object. You may only use async |
64 |
|
|
notifications on this object while it exists, so you need to keep a |
65 |
|
|
reference to it at all times while it is used. |
66 |
|
|
|
67 |
|
|
Optional constructor arguments include (normally you would specify at |
68 |
|
|
least one of C<cb> or C<c_cb>). |
69 |
|
|
|
70 |
|
|
=over 4 |
71 |
|
|
|
72 |
|
|
=item cb => $coderef->($value) |
73 |
|
|
|
74 |
|
|
Registers a perl callback to be invoked whenever the async interrupt is |
75 |
|
|
signalled. |
76 |
|
|
|
77 |
|
|
Note that, since this callback can be invoked at basically any time, it |
78 |
root |
1.2 |
must not modify any well-known global variables such as C<$/> without |
79 |
|
|
restoring them again before returning. |
80 |
|
|
|
81 |
|
|
The exceptions are C<$!> and C<$@>, which are saved and restored by |
82 |
|
|
Async::Interrupt. |
83 |
root |
1.1 |
|
84 |
root |
1.2 |
If the callback should throw an exception, then it will be caught, |
85 |
|
|
and C<$Async::Interrupt::DIED> will be called with C<$@> containing |
86 |
|
|
the exception. The default will simply C<warn> about the message and |
87 |
|
|
continue. |
88 |
|
|
|
89 |
|
|
=item c_cb => [$c_func, $c_arg] |
90 |
root |
1.1 |
|
91 |
|
|
Registers a C callback the be invoked whenever the async interrupt is |
92 |
|
|
signalled. |
93 |
|
|
|
94 |
|
|
The C callback must have the following prototype: |
95 |
|
|
|
96 |
root |
1.2 |
void c_func (pTHX_ void *c_arg, int value); |
97 |
root |
1.1 |
|
98 |
root |
1.2 |
Both C<$c_func> and C<$c_arg> must be specified as integers/IVs, and |
99 |
|
|
C<$value> is the C<value> passed to some earlier call to either C<$signal> |
100 |
|
|
or the C<signal_func> function. |
101 |
root |
1.1 |
|
102 |
|
|
Note that, because the callback can be invoked at almost any time, you |
103 |
|
|
have to be careful at saving and restoring global variables that Perl |
104 |
root |
1.2 |
might use (the excetpion is C<errno>, which is aved and restored by |
105 |
|
|
Async::Interrupt). The callback itself runs as part of the perl context, |
106 |
|
|
so you can call any perl functions and modify any perl data structures (in |
107 |
|
|
which case the requireemnts set out for C<cb> apply as well). |
108 |
root |
1.1 |
|
109 |
root |
1.2 |
=item pipe => [$fileno_or_fh_for_reading, $fileno_or_fh_for_writing] |
110 |
root |
1.1 |
|
111 |
root |
1.2 |
Specifies two file descriptors (or file handles) that should be signalled |
112 |
root |
1.1 |
whenever the async interrupt is signalled. This means a single octet will |
113 |
|
|
be written to it, and before the callback is being invoked, it will be |
114 |
|
|
read again. Due to races, it is unlikely but possible that multiple octets |
115 |
root |
1.2 |
are written. It is required that the file handles are both in nonblocking |
116 |
|
|
mode. |
117 |
root |
1.1 |
|
118 |
|
|
(You can get a portable pipe and set non-blocking mode portably by using |
119 |
|
|
e.g. L<AnyEvent::Util> from the L<AnyEvent> distro). |
120 |
|
|
|
121 |
root |
1.2 |
The object will keep a reference to the file handles. |
122 |
root |
1.1 |
|
123 |
|
|
This can be used to ensure that async notifications will interrupt event |
124 |
|
|
frameworks as well. |
125 |
|
|
|
126 |
|
|
=back |
127 |
|
|
|
128 |
|
|
=cut |
129 |
|
|
|
130 |
|
|
sub new { |
131 |
|
|
my ($class, %arg) = @_; |
132 |
|
|
|
133 |
root |
1.2 |
bless \(_alloc $arg{cb}, @{$arg{c_cb}}[0,1], @{$arg{pipe}}[0,1]), $class |
134 |
root |
1.1 |
} |
135 |
|
|
|
136 |
root |
1.2 |
=item ($signal_func, $signal_arg) = $async->signal_func |
137 |
root |
1.1 |
|
138 |
|
|
Returns the address of a function to call asynchronously. The function has |
139 |
|
|
the following prototype and needs to be passed the specified C<$c_arg>, |
140 |
|
|
which is a C<void *> cast to C<IV>: |
141 |
|
|
|
142 |
|
|
void (*signal_func) (void *signal_arg, int value) |
143 |
|
|
|
144 |
|
|
An example call would look like: |
145 |
|
|
|
146 |
|
|
signal_func (signal_arg, 0); |
147 |
|
|
|
148 |
root |
1.2 |
The function is safe to call from within signal and thread contexts, at |
149 |
root |
1.1 |
any time. The specified C<value> is passed to both C and Perl callback. |
150 |
|
|
|
151 |
root |
1.2 |
C<$value> must be in the valid range for a C<sig_atomic_t> (0..127 is |
152 |
|
|
portable). |
153 |
|
|
|
154 |
root |
1.1 |
If the function is called while the Async::Interrupt object is already |
155 |
|
|
signaled but before the callbacks are being executed, then the stored |
156 |
root |
1.2 |
C<value> is either the old or the new one. Due to the asynchronous |
157 |
|
|
nature of the code, the C<value> can even be passed to two consecutive |
158 |
|
|
invocations of the callback. |
159 |
root |
1.1 |
|
160 |
|
|
=item $async->signal ($value=0) |
161 |
|
|
|
162 |
|
|
This signals the given async object from Perl code. Semi-obviously, this |
163 |
|
|
will instantly trigger the callback invocation. |
164 |
|
|
|
165 |
root |
1.2 |
C<$value> must be in the valid range for a C<sig_atomic_t> (0..127 is |
166 |
|
|
portable). |
167 |
|
|
|
168 |
|
|
=item $async->block |
169 |
|
|
|
170 |
|
|
Sometimes you need a "critical section" of code where |
171 |
|
|
|
172 |
|
|
=item $async->unblock |
173 |
|
|
|
174 |
root |
1.1 |
=cut |
175 |
|
|
|
176 |
|
|
1; |
177 |
|
|
|
178 |
|
|
=back |
179 |
|
|
|
180 |
root |
1.2 |
=head1 EXAMPLE |
181 |
|
|
|
182 |
|
|
#TODO |
183 |
|
|
|
184 |
|
|
=head1 IMPLEMENTATION DETAILS AND LIMITATIONS |
185 |
|
|
|
186 |
|
|
This module works by "hijacking" SIGKILL, which is guarenteed to be always |
187 |
|
|
available in perl, but also cannot be caught, so is always available. |
188 |
|
|
|
189 |
|
|
Basically, this module fakes the receive of a SIGKILL signal and |
190 |
|
|
then catches it. This makes normal signal handling slower (probably |
191 |
|
|
unmeasurably), but has the advantage of not requiring a special runops nor |
192 |
|
|
slowing down normal perl execution a bit. |
193 |
|
|
|
194 |
|
|
It assumes that C<sig_atomic_t> and C<int> are both exception-safe to |
195 |
|
|
modify (C<sig_atomic_> is used by this module, and perl itself uses |
196 |
|
|
C<int>, so we can assume that this is quite portbale, at least w.r.t. |
197 |
|
|
signals). |
198 |
|
|
|
199 |
root |
1.1 |
=head1 AUTHOR |
200 |
|
|
|
201 |
|
|
Marc Lehmann <schmorp@schmorp.de> |
202 |
|
|
http://home.schmorp.de/ |
203 |
|
|
|
204 |
|
|
=cut |
205 |
|
|
|