ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/Async-Interrupt/Interrupt.pm
(Generate patch)

Comparing cvsroot/Async-Interrupt/Interrupt.pm (file contents):
Revision 1.12 by root, Tue Jul 14 19:51:24 2009 UTC vs.
Revision 1.16 by root, Fri Jul 17 21:02:18 2009 UTC

88I<running> interpreter, there is optional support for signalling a pipe 88I<running> interpreter, there is optional support for signalling a pipe
89- that means you can also wait for the pipe to become readable (e.g. via 89- that means you can also wait for the pipe to become readable (e.g. via
90L<EV> or L<AnyEvent>). This, of course, incurs the overhead of a C<read> 90L<EV> or L<AnyEvent>). This, of course, incurs the overhead of a C<read>
91and C<write> syscall. 91and C<write> syscall.
92 92
93=head1 THE Async::Interrupt CLASS
94
93=over 4 95=over 4
94 96
95=cut 97=cut
96 98
97package Async::Interrupt; 99package Async::Interrupt;
101BEGIN { 103BEGIN {
102 # the next line forces initialisation of internal 104 # the next line forces initialisation of internal
103 # signal handling # variables 105 # signal handling # variables
104 $SIG{KILL} = sub { }; 106 $SIG{KILL} = sub { };
105 107
106 our $VERSION = '0.042'; 108 our $VERSION = '0.6';
107 109
108 require XSLoader; 110 require XSLoader;
109 XSLoader::load ("Async::Interrupt", $VERSION); 111 XSLoader::load ("Async::Interrupt", $VERSION);
110} 112}
111 113
157might use (the exception is C<errno>, which is saved and restored by 159might use (the exception is C<errno>, which is saved and restored by
158Async::Interrupt). The callback itself runs as part of the perl context, 160Async::Interrupt). The callback itself runs as part of the perl context,
159so you can call any perl functions and modify any perl data structures (in 161so you can call any perl functions and modify any perl data structures (in
160which case the requirements set out for C<cb> apply as well). 162which case the requirements set out for C<cb> apply as well).
161 163
164=item var => $scalar_ref
165
166When specified, then the given argument must be a reference to a
167scalar. The scalar will be set to C<0> intiially. Signalling the interrupt
168object will set it to the passed value, handling the interrupt will reset
169it to C<0> again.
170
171Note that the only thing you are legally allowed to do is to is to check
172the variable in a boolean or integer context (e.g. comparing it with a
173string, or printing it, will I<destroy> it and might cause your program to
174crash or worse).
175
162=item signal => $signame_or_value 176=item signal => $signame_or_value
163 177
164When this parameter is specified, then the Async::Interrupt will hook the 178When this parameter is specified, then the Async::Interrupt will hook the
165given signal, that is, it will effectively call C<< ->signal (0) >> each time 179given signal, that is, it will effectively call C<< ->signal (0) >> each time
166the given signal is caught by the process. 180the given signal is caught by the process.
175be written to it, and before the callback is being invoked, it will be 189be written to it, and before the callback is being invoked, it will be
176read again. Due to races, it is unlikely but possible that multiple octets 190read again. Due to races, it is unlikely but possible that multiple octets
177are written. It is required that the file handles are both in nonblocking 191are written. It is required that the file handles are both in nonblocking
178mode. 192mode.
179 193
180You can get a portable pipe and set non-blocking mode portably by using
181e.g. L<AnyEvent::Util> from the L<AnyEvent> distribution.
182
183It is also possible to pass in a linux eventfd as both read and write
184handle (which is faster than a pipe).
185
186The object will keep a reference to the file handles. 194The object will keep a reference to the file handles.
187 195
188This can be used to ensure that async notifications will interrupt event 196This can be used to ensure that async notifications will interrupt event
189frameworks as well. 197frameworks as well.
190 198
199Note that C<Async::Interrupt> will create a suitable signal fd
200automatically when your program requests one, so you don't have to specify
201this argument when all you want is an extra file descriptor to watch.
202
203If you want to share a single event pipe between multiple Async::Interrupt
204objects, you can use the C<Async::Interrupt::EventPipe> class to manage
205those.
206
191=back 207=back
192 208
193=cut 209=cut
194 210
195sub new { 211sub new {
196 my ($class, %arg) = @_; 212 my ($class, %arg) = @_;
197 213
198 bless \(_alloc $arg{cb}, @{$arg{c_cb}}[0,1], @{$arg{pipe}}[0,1], $arg{signal}), $class 214 bless \(_alloc $arg{cb}, @{$arg{c_cb}}[0,1], @{$arg{pipe}}[0,1], $arg{signal}, $arg{var}), $class
199} 215}
200 216
201=item ($signal_func, $signal_arg) = $async->signal_func 217=item ($signal_func, $signal_arg) = $async->signal_func
202 218
203Returns the address of a function to call asynchronously. The function has 219Returns the address of a function to call asynchronously. The function has
211 signal_func (signal_arg, 0); 227 signal_func (signal_arg, 0);
212 228
213The function is safe to call from within signal and thread contexts, at 229The function is safe to call from within signal and thread contexts, at
214any time. The specified C<value> is passed to both C and Perl callback. 230any time. The specified C<value> is passed to both C and Perl callback.
215 231
216C<$value> must be in the valid range for a C<sig_atomic_t> (0..127 is 232C<$value> must be in the valid range for a C<sig_atomic_t>, except C<0>
217portable). 233(1..127 is portable).
218 234
219If the function is called while the Async::Interrupt object is already 235If the function is called while the Async::Interrupt object is already
220signaled but before the callbacks are being executed, then the stored 236signaled but before the callbacks are being executed, then the stored
221C<value> is either the old or the new one. Due to the asynchronous 237C<value> is either the old or the new one. Due to the asynchronous
222nature of the code, the C<value> can even be passed to two consecutive 238nature of the code, the C<value> can even be passed to two consecutive
223invocations of the callback. 239invocations of the callback.
224 240
241=item $address = $async->c_var
242
243Returns the address (cast to IV) of an C<IV> variable. The variable is set
244to C<0> initially and gets set to the passed value whenever the object
245gets signalled, and reset to C<0> once the interrupt has been handled.
246
247Note that it is often beneficial to just call C<PERL_ASYNC_CHECK ()> to
248handle any interrupts.
249
250Example: call some XS function to store the address, then show C code
251waiting for it.
252
253 my_xs_func $async->c_var;
254
255 static IV *valuep;
256
257 void
258 my_xs_func (void *addr)
259 CODE:
260 valuep = (IV *)addr;
261
262 // code in a loop, waiting
263 while (!*valuep)
264 ; // do something
265
225=item $async->signal ($value=0) 266=item $async->signal ($value=1)
226 267
227This signals the given async object from Perl code. Semi-obviously, this 268This signals the given async object from Perl code. Semi-obviously, this
228will instantly trigger the callback invocation. 269will instantly trigger the callback invocation.
229 270
230C<$value> must be in the valid range for a C<sig_atomic_t> (0..127 is 271C<$value> must be in the valid range for a C<sig_atomic_t>, except C<0>
231portable). 272(1..127 is portable).
232 273
233=item $async->block 274=item $async->block
234 275
235=item $async->unblock 276=item $async->unblock
236 277
262enabled). Writing to a pipe is relatively expensive, so it can be disabled 303enabled). Writing to a pipe is relatively expensive, so it can be disabled
263when you know you are not waiting for it (for example, with L<EV> you 304when you know you are not waiting for it (for example, with L<EV> you
264could disable the pipe in a check watcher, and enable it in a prepare 305could disable the pipe in a check watcher, and enable it in a prepare
265watcher). 306watcher).
266 307
267Note that when C<fd_disable> is in effect, no attempt to read from the 308Note that currently, while C<pipe_disable> is in effect, no attempt to
268pipe will be done. 309read from the pipe will be done when handling events. This might change as
310soon as I realize why this is a mistake.
311
312=item $fileno = $async->pipe_fileno
313
314Returns the reading side of the signalling pipe. If no signalling pipe is
315currently attached to the object, it will dynamically create one.
316
317Note that the only valid oepration on this file descriptor is to wait
318until it is readable. The fd might belong currently to a pipe, a tcp
319socket, or an eventfd, depending on the platform, and is guaranteed to be
320C<select>able.
321
322=item $async->pipe_autodrain ($enable)
323
324Enables (C<1>) or disables (C<0>) automatic draining of the pipe (default:
325enabled). When automatic draining is enabled, then Async::Interrupt will
326automatically clear the pipe. Otherwise the user is responsible for this
327draining.
328
329This is useful when you want to share one pipe among many Async::Interrupt
330objects.
331
332=item $async->post_fork
333
334The object will not normally be usable after a fork (as the pipe fd is
335shared between processes). Calling this method after a fork in the child
336ensures that the object will work as expected again. It only needs to be
337called when the async object is used in the child.
338
339This only works when the pipe was created by Async::Interrupt.
340
341Async::Interrupt ensures that the reading file descriptor does not change
342it's value.
343
344=back
345
346=head1 THE Async::Interrupt::EventPipe CLASS
347
348Pipes are the predominent utility to make asynchronous signals
349synchronous. However, pipes are hard to come by: they don't exist on the
350broken windows platform, and on GNU/Linux systems, you might want to use
351an C<eventfd> instead.
352
353This class creates selectable event pipes in a portable fashion: on
354windows, it will try to create a tcp socket pair, on GNU/Linux, it will
355try to create an eventfd and everywhere else it will try to use a normal
356pipe.
357
358=over 4
359
360=item $epipe = new Async::Interrupt::EventPipe
361
362This creates and returns an eventpipe object. This object is simply a
363blessed array reference:
364
365=item ($r_fd, $w_fd) = $epipe->filenos
366
367Returns the read-side file descriptor and the write-side file descriptor.
368
369Example: pass an eventpipe object as pipe to the Async::Interrupt
370constructor, and create an AnyEvent watcher for the read side.
371
372 my $epipe = new Async::Interrupt::EventPipe;
373 my $asy = new Async::Interrupt pipe => [$epipe->filenos];
374 my $iow = AnyEvent->io (fh => $epipe->fileno, poll => 'r', cb => sub { });
375
376=item $r_fd = $epipe->fileno
377
378Return only the reading/listening side.
379
380=item $epipe->signal
381
382Write something to the pipe, in a portable fashion.
383
384=item $epipe->drain
385
386Drain (empty) the pipe.
387
388=item $epipe->renew
389
390Recreates the pipe (useful after a fork). The reading side will not change
391it's file descriptor number, but the writing side might.
392
393=back
269 394
270=cut 395=cut
271 396
2721; 3971;
273
274=back
275 398
276=head1 EXAMPLE 399=head1 EXAMPLE
277 400
278There really should be a complete C/XS example. Bug me about it. Better 401There really should be a complete C/XS example. Bug me about it. Better
279yet, create one. 402yet, create one.
287then intercepts the interpreter handling it. This makes normal signal 410then intercepts the interpreter handling it. This makes normal signal
288handling slower (probably unmeasurably, though), but has the advantage 411handling slower (probably unmeasurably, though), but has the advantage
289of not requiring a special runops function, nor slowing down normal perl 412of not requiring a special runops function, nor slowing down normal perl
290execution a bit. 413execution a bit.
291 414
292It assumes that C<sig_atomic_t> and C<int> are both async-safe to modify 415It assumes that C<sig_atomic_t>, C<int> and C<IV> are all async-safe to
293(C<sig_atomic_> is used by this module, and perl itself uses C<int>, so we 416modify.
294can assume that this is quite portable, at least w.r.t. signals).
295 417
296=head1 AUTHOR 418=head1 AUTHOR
297 419
298 Marc Lehmann <schmorp@schmorp.de> 420 Marc Lehmann <schmorp@schmorp.de>
299 http://home.schmorp.de/ 421 http://home.schmorp.de/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines