… | |
… | |
38 | |
38 | |
39 | You can use this module to bind a signal to a callback while at the same |
39 | You can use this module to bind a signal to a callback while at the same |
40 | time activating an event pipe that you can C<select> on, fixing the race |
40 | time activating an event pipe that you can C<select> on, fixing the race |
41 | completely. |
41 | completely. |
42 | |
42 | |
43 | This can be used to implement the signal hadling in event loops, |
43 | This can be used to implement the signal handling in event loops, |
44 | e.g. L<AnyEvent>, L<POE>, L<IO::Async::Loop> and so on. |
44 | e.g. L<AnyEvent>, L<POE>, L<IO::Async::Loop> and so on. |
45 | |
45 | |
46 | =item Background threads want speedy reporting |
46 | =item Background threads want speedy reporting |
47 | |
47 | |
48 | Assume you want very exact timing, and you can spare an extra cpu core |
48 | Assume you want very exact timing, and you can spare an extra cpu core |
… | |
… | |
114 | callback just sets a global variable, as we are only interested in |
114 | callback just sets a global variable, as we are only interested in |
115 | synchronous signals (i.e. when the event loop polls), which is why the |
115 | synchronous signals (i.e. when the event loop polls), which is why the |
116 | pipe draining is not done automatically. |
116 | pipe draining is not done automatically. |
117 | |
117 | |
118 | my $interrupt = new Async::Interrupt |
118 | my $interrupt = new Async::Interrupt |
119 | cb => sub { undef $SIGNAL_RECEIVED{$signum} } |
119 | cb => sub { undef $SIGNAL_RECEIVED{$signum} }, |
120 | signal => $signum, |
120 | signal => $signum, |
121 | pipe => [$SIGPIPE->filenos], |
121 | pipe => [$SIGPIPE->filenos], |
122 | pipe_autodrain => 0, |
122 | pipe_autodrain => 0, |
123 | ; |
123 | ; |
124 | |
124 | |
… | |
… | |
158 | _attach $self, $asy->signal_func; |
158 | _attach $self, $asy->signal_func; |
159 | |
159 | |
160 | So to repeat: first the XS object is created, then it is queried for the |
160 | So to repeat: first the XS object is created, then it is queried for the |
161 | callback that should be called when the Interrupt object gets signalled. |
161 | callback that should be called when the Interrupt object gets signalled. |
162 | |
162 | |
163 | Then the interrupt object is queried for the callback fucntion that the |
163 | Then the interrupt object is queried for the callback function that the |
164 | thread should call to signal the Interrupt object, and this callback is |
164 | thread should call to signal the Interrupt object, and this callback is |
165 | then attached to the thread. |
165 | then attached to the thread. |
166 | |
166 | |
167 | You have to be careful that your new thread is not signalling before the |
167 | You have to be careful that your new thread is not signalling before the |
168 | signal function was configured, for example by starting the background |
168 | signal function was configured, for example by starting the background |
… | |
… | |
236 | # the next line forces initialisation of internal |
236 | # the next line forces initialisation of internal |
237 | # signal handling variables, otherwise, PL_sig_pending |
237 | # signal handling variables, otherwise, PL_sig_pending |
238 | # etc. might be null pointers. |
238 | # etc. might be null pointers. |
239 | $SIG{KILL} = sub { }; |
239 | $SIG{KILL} = sub { }; |
240 | |
240 | |
241 | our $VERSION = '1.05'; |
241 | our $VERSION = 1.26; |
242 | |
242 | |
243 | require XSLoader; |
243 | require XSLoader; |
244 | XSLoader::load ("Async::Interrupt", $VERSION); |
244 | XSLoader::load ("Async::Interrupt", $VERSION); |
245 | } |
245 | } |
246 | |
246 | |
… | |
… | |
419 | might imply, do anything with POSIX signals). |
419 | might imply, do anything with POSIX signals). |
420 | |
420 | |
421 | C<$value> must be in the valid range for a C<sig_atomic_t>, except C<0> |
421 | C<$value> must be in the valid range for a C<sig_atomic_t>, except C<0> |
422 | (1..127 is portable). |
422 | (1..127 is portable). |
423 | |
423 | |
|
|
424 | =item $async->handle |
|
|
425 | |
|
|
426 | Calls the callback if the object is pending. |
|
|
427 | |
|
|
428 | This method does not need to be called normally, as it will be invoked |
|
|
429 | automatically. However, it can be used to force handling of outstanding |
|
|
430 | interrupts while the object is blocked. |
|
|
431 | |
|
|
432 | One reason why one might want to do that is when you want to switch |
|
|
433 | from asynchronous interruptions to synchronous one, using e.g. an event |
|
|
434 | loop. To do that, one would first C<< $async->block >> the interrupt |
|
|
435 | object, then register a read watcher on the C<pipe_fileno> that calls C<< |
|
|
436 | $async->handle >>. |
|
|
437 | |
|
|
438 | This disables asynchronous interruptions, but ensures that interrupts are |
|
|
439 | handled by the event loop. |
|
|
440 | |
424 | =item $async->signal_hysteresis ($enable) |
441 | =item $async->signal_hysteresis ($enable) |
425 | |
442 | |
426 | Enables or disables signal hysteresis (default: disabled). If a POSIX |
443 | Enables or disables signal hysteresis (default: disabled). If a POSIX |
427 | signal is used as a signal source for the interrupt object, then enabling |
444 | signal is used as a signal source for the interrupt object, then enabling |
428 | signal hysteresis causes Async::Interrupt to reset the signal action to |
445 | signal hysteresis causes Async::Interrupt to reset the signal action to |
… | |
… | |
432 | When you expect a lot of signals (e.g. when using SIGIO), then enabling |
449 | When you expect a lot of signals (e.g. when using SIGIO), then enabling |
433 | signal hysteresis can reduce the number of handler invocations |
450 | signal hysteresis can reduce the number of handler invocations |
434 | considerably, at the cost of two extra syscalls. |
451 | considerably, at the cost of two extra syscalls. |
435 | |
452 | |
436 | Note that setting the signal to C<SIG_IGN> can have unintended side |
453 | Note that setting the signal to C<SIG_IGN> can have unintended side |
437 | effects when you fork and exec other programs, as often they do nto expect |
454 | effects when you fork and exec other programs, as often they do not expect |
438 | signals to be ignored by default. |
455 | signals to be ignored by default. |
439 | |
456 | |
440 | =item $async->block |
457 | =item $async->block |
441 | |
458 | |
442 | =item $async->unblock |
459 | =item $async->unblock |
… | |
… | |
495 | =item $fileno = $async->pipe_fileno |
512 | =item $fileno = $async->pipe_fileno |
496 | |
513 | |
497 | Returns the reading side of the signalling pipe. If no signalling pipe is |
514 | Returns the reading side of the signalling pipe. If no signalling pipe is |
498 | currently attached to the object, it will dynamically create one. |
515 | currently attached to the object, it will dynamically create one. |
499 | |
516 | |
500 | Note that the only valid oepration on this file descriptor is to wait |
517 | Note that the only valid operation on this file descriptor is to wait |
501 | until it is readable. The fd might belong currently to a pipe, a tcp |
518 | until it is readable. The fd might belong currently to a pipe, a tcp |
502 | socket, or an eventfd, depending on the platform, and is guaranteed to be |
519 | socket, or an eventfd, depending on the platform, and is guaranteed to be |
503 | C<select>able. |
520 | C<select>able. |
504 | |
521 | |
505 | =item $async->pipe_autodrain ($enable) |
522 | =item $async->pipe_autodrain ($enable) |
… | |
… | |
510 | draining. |
527 | draining. |
511 | |
528 | |
512 | This is useful when you want to share one pipe among many Async::Interrupt |
529 | This is useful when you want to share one pipe among many Async::Interrupt |
513 | objects. |
530 | objects. |
514 | |
531 | |
|
|
532 | =item $async->pipe_drain |
|
|
533 | |
|
|
534 | Drains the pipe manually, for example, when autodrain is disabled. Does |
|
|
535 | nothing when no pipe is enabled. |
|
|
536 | |
515 | =item $async->post_fork |
537 | =item $async->post_fork |
516 | |
538 | |
517 | The object will not normally be usable after a fork (as the pipe fd is |
539 | The object will not normally be usable after a fork (as the pipe fd is |
518 | shared between processes). Calling this method after a fork in the child |
540 | shared between processes). Calling this method after a fork in the child |
519 | ensures that the object will work as expected again. It only needs to be |
541 | ensures that the object will work as expected again. It only needs to be |
… | |
… | |
595 | |
617 | |
596 | c_func (c_arg); |
618 | c_func (c_arg); |
597 | |
619 | |
598 | =item $epipe->renew |
620 | =item $epipe->renew |
599 | |
621 | |
600 | Recreates the pipe (useful after a fork). The reading side will not change |
622 | Recreates the pipe (usually required in the child after a fork). The |
601 | it's file descriptor number, but the writing side might. |
623 | reading side will not change it's file descriptor number, but the writing |
|
|
624 | side might. |
602 | |
625 | |
603 | =item $epipe->wait |
626 | =item $epipe->wait |
604 | |
627 | |
605 | This method blocks the process until there are events on the pipe. This is |
628 | This method blocks the process until there are events on the pipe. This is |
606 | not a very event-based or ncie way of usign an event pipe, but it can be |
629 | not a very event-based or ncie way of usign an event pipe, but it can be |
… | |
… | |
615 | =head1 IMPLEMENTATION DETAILS AND LIMITATIONS |
638 | =head1 IMPLEMENTATION DETAILS AND LIMITATIONS |
616 | |
639 | |
617 | This module works by "hijacking" SIGKILL, which is guaranteed to always |
640 | This module works by "hijacking" SIGKILL, which is guaranteed to always |
618 | exist, but also cannot be caught, so is always available. |
641 | exist, but also cannot be caught, so is always available. |
619 | |
642 | |
620 | Basically, this module fakes the occurance of a SIGKILL signal and |
643 | Basically, this module fakes the occurence of a SIGKILL signal and |
621 | then intercepts the interpreter handling it. This makes normal signal |
644 | then intercepts the interpreter handling it. This makes normal signal |
622 | handling slower (probably unmeasurably, though), but has the advantage |
645 | handling slower (probably unmeasurably, though), but has the advantage |
623 | of not requiring a special runops function, nor slowing down normal perl |
646 | of not requiring a special runops function, nor slowing down normal perl |
624 | execution a bit. |
647 | execution a bit. |
625 | |
648 | |