Async-Interrupt
view release on metacpan or search on metacpan
Interrupt.pm view on Meta::CPAN
Specifies two file descriptors (or file handles) that should be signalled
whenever the async interrupt is signalled. This means a single octet will
be written to it, and before the callback is being invoked, it will be
read again. Due to races, it is unlikely but possible that multiple octets
are written. It is required that the file handles are both in nonblocking
mode.
The object will keep a reference to the file handles.
This can be used to ensure that async notifications will interrupt event
frameworks as well.
Note that C<Async::Interrupt> will create a suitable signal fd
automatically when your program requests one, so you don't have to specify
this argument when all you want is an extra file descriptor to watch.
If you want to share a single event pipe between multiple Async::Interrupt
objects, you can use the C<Async::Interrupt::EventPipe> class to manage
those.
=item pipe_autodrain => $boolean
Sets the initial autodrain state, see the C<pipe_autodrain> method, below.
=back
=cut
sub new {
my ($class, %arg) = @_;
my $self = bless \(_alloc $arg{cb}, @{$arg{c_cb}}[0,1], @{$arg{pipe}}[0,1], $arg{signal}, $arg{var}), $class;
# urgs, reminds me of Event
for my $attr (qw(pipe_autodrain signal_hysteresis)) {
$self->$attr ($arg{$attr}) if exists $arg{$attr};
}
$self
}
=item ($signal_func, $signal_arg) = $async->signal_func
Returns the address of a function to call asynchronously. The function
has the following prototype and needs to be passed the specified
C<$signal_arg>, which is a C<void *> cast to C<IV>:
void (*signal_func) (void *signal_arg, int value)
An example call would look like:
signal_func (signal_arg, 0);
The function is safe to call from within signal and thread contexts, at
any time. The specified C<value> is passed to both C and Perl callback.
C<$value> must be in the valid range for a C<sig_atomic_t>, except C<0>
(1..127 is portable).
If the function is called while the Async::Interrupt object is already
signaled but before the callbacks are being executed, then the stored
C<value> is either the old or the new one. Due to the asynchronous
nature of the code, the C<value> can even be passed to two consecutive
invocations of the callback.
=item $address = $async->c_var
Returns the address (cast to IV) of an C<IV> variable. The variable is set
to C<0> initially and gets set to the passed value whenever the object
gets signalled, and reset to C<0> once the interrupt has been handled.
Note that it is often beneficial to just call C<PERL_ASYNC_CHECK ()> to
handle any interrupts.
Example: call some XS function to store the address, then show C code
waiting for it.
my_xs_func $async->c_var;
static IV *valuep;
void
my_xs_func (void *addr)
CODE:
valuep = (IV *)addr;
// code in a loop, waiting
while (!*valuep)
; // do something
=item $async->signal ($value=1)
This signals the given async object from Perl code. Semi-obviously, this
will instantly trigger the callback invocation (it does not, as the name
might imply, do anything with POSIX signals).
C<$value> must be in the valid range for a C<sig_atomic_t>, except C<0>
(1..127 is portable).
=item $async->handle
Calls the callback if the object is pending.
This method does not need to be called normally, as it will be invoked
automatically. However, it can be used to force handling of outstanding
interrupts while the object is blocked.
One reason why one might want to do that is when you want to switch
from asynchronous interruptions to synchronous one, using e.g. an event
loop. To do that, one would first C<< $async->block >> the interrupt
object, then register a read watcher on the C<pipe_fileno> that calls C<<
$async->handle >>.
This disables asynchronous interruptions, but ensures that interrupts are
handled by the event loop.
=item $async->signal_hysteresis ($enable)
Enables or disables signal hysteresis (default: disabled). If a POSIX
signal is used as a signal source for the interrupt object, then enabling
signal hysteresis causes Async::Interrupt to reset the signal action to
( run in 0.810 second using v1.01-cache-2.11-cpan-39bf76dae61 )