| 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 |
|
|
=over 4 |
| 27 |
|
|
|
| 28 |
|
|
=cut |
| 29 |
|
|
|
| 30 |
|
|
package Async::Interrupt; |
| 31 |
|
|
|
| 32 |
|
|
BEGIN { |
| 33 |
|
|
$VERSION = '0.02'; |
| 34 |
|
|
|
| 35 |
|
|
require XSLoader; |
| 36 |
|
|
XSLoader::load Async::Interrupt::, $VERSION; |
| 37 |
|
|
} |
| 38 |
|
|
|
| 39 |
|
|
=item $async = new Async::Interrupt key => value... |
| 40 |
|
|
|
| 41 |
|
|
Creates a new Async::Interrupt object. You may only use async |
| 42 |
|
|
notifications on this object while it exists, so you need to keep a |
| 43 |
|
|
reference to it at all times while it is used. |
| 44 |
|
|
|
| 45 |
|
|
Optional constructor arguments include (normally you would specify at |
| 46 |
|
|
least one of C<cb> or C<c_cb>). |
| 47 |
|
|
|
| 48 |
|
|
=over 4 |
| 49 |
|
|
|
| 50 |
|
|
=item cb => $coderef->($value) |
| 51 |
|
|
|
| 52 |
|
|
Registers a perl callback to be invoked whenever the async interrupt is |
| 53 |
|
|
signalled. |
| 54 |
|
|
|
| 55 |
|
|
Note that, since this callback can be invoked at basically any time, it |
| 56 |
|
|
must not modify any well-known global variables such as C<$/>, C<$@> or |
| 57 |
|
|
C<$!>, without restoring them again before returning. |
| 58 |
|
|
|
| 59 |
|
|
=item c_cb => [$c_func, $c_data] |
| 60 |
|
|
|
| 61 |
|
|
Registers a C callback the be invoked whenever the async interrupt is |
| 62 |
|
|
signalled. |
| 63 |
|
|
|
| 64 |
|
|
The C callback must have the following prototype: |
| 65 |
|
|
|
| 66 |
|
|
void c_func (pTHX_ void *c_data, int value); |
| 67 |
|
|
|
| 68 |
|
|
Both C<$c_func> and C<$c_data> must be specified as integers/IVs. |
| 69 |
|
|
|
| 70 |
|
|
Note that, because the callback can be invoked at almost any time, you |
| 71 |
|
|
have to be careful at saving and restoring global variables that Perl |
| 72 |
|
|
might use, most notably C<errno>. The callback itself runs as part of the |
| 73 |
|
|
perl context, so you can call any perl functions and modify any perl data |
| 74 |
|
|
structures. |
| 75 |
|
|
|
| 76 |
|
|
=item fh => $fileno_or_fh |
| 77 |
|
|
|
| 78 |
|
|
Specifies a file descriptor (or file handle) that should be signalled |
| 79 |
|
|
whenever the async interrupt is signalled. This means a single octet will |
| 80 |
|
|
be written to it, and before the callback is being invoked, it will be |
| 81 |
|
|
read again. Due to races, it is unlikely but possible that multiple octets |
| 82 |
|
|
are written, therefore, it is recommended that the file handle is in |
| 83 |
|
|
nonblocking mode. |
| 84 |
|
|
|
| 85 |
|
|
(You can get a portable pipe and set non-blocking mode portably by using |
| 86 |
|
|
e.g. L<AnyEvent::Util> from the L<AnyEvent> distro). |
| 87 |
|
|
|
| 88 |
|
|
The object will keep a reference to the file handle. |
| 89 |
|
|
|
| 90 |
|
|
This can be used to ensure that async notifications will interrupt event |
| 91 |
|
|
frameworks as well. |
| 92 |
|
|
|
| 93 |
|
|
=back |
| 94 |
|
|
|
| 95 |
|
|
=cut |
| 96 |
|
|
|
| 97 |
|
|
sub new { |
| 98 |
|
|
my ($class, %arg) = @_; |
| 99 |
|
|
|
| 100 |
|
|
my $self = _alloc $arg{cb}, @{$arg{c_cb}}[0,1], $arg{fh}; |
| 101 |
|
|
bless \$self, $class |
| 102 |
|
|
} |
| 103 |
|
|
|
| 104 |
|
|
=item ($signal_func, $signal_arg) = $async->signal_cb |
| 105 |
|
|
|
| 106 |
|
|
Returns the address of a function to call asynchronously. The function has |
| 107 |
|
|
the following prototype and needs to be passed the specified C<$c_arg>, |
| 108 |
|
|
which is a C<void *> cast to C<IV>: |
| 109 |
|
|
|
| 110 |
|
|
void (*signal_func) (void *signal_arg, int value) |
| 111 |
|
|
|
| 112 |
|
|
An example call would look like: |
| 113 |
|
|
|
| 114 |
|
|
signal_func (signal_arg, 0); |
| 115 |
|
|
|
| 116 |
|
|
The function is safe toc all from within signal and thread contexts, at |
| 117 |
|
|
any time. The specified C<value> is passed to both C and Perl callback. |
| 118 |
|
|
|
| 119 |
|
|
If the function is called while the Async::Interrupt object is already |
| 120 |
|
|
signaled but before the callbacks are being executed, then the stored |
| 121 |
|
|
C<value> is being overwritten. Due to the asynchronous nature of the code, |
| 122 |
|
|
the C<value> can even be passed to two consecutive invocations of the |
| 123 |
|
|
callback. |
| 124 |
|
|
|
| 125 |
|
|
=item $async->signal ($value=0) |
| 126 |
|
|
|
| 127 |
|
|
This signals the given async object from Perl code. Semi-obviously, this |
| 128 |
|
|
will instantly trigger the callback invocation. |
| 129 |
|
|
|
| 130 |
|
|
=cut |
| 131 |
|
|
|
| 132 |
|
|
1; |
| 133 |
|
|
|
| 134 |
|
|
=back |
| 135 |
|
|
|
| 136 |
|
|
=head1 AUTHOR |
| 137 |
|
|
|
| 138 |
|
|
Marc Lehmann <schmorp@schmorp.de> |
| 139 |
|
|
http://home.schmorp.de/ |
| 140 |
|
|
|
| 141 |
|
|
=cut |
| 142 |
|
|
|