in reply to SOLVED: XS: Passing an external library's function a Perl XS callback

For completeness sake, I'm posting the relevant code that now has fixed my original problem. I can now use an external library that has ISR (Interrupt Service Routine) capability.

What I needed to do was allow a user to send in a Perl code reference, and then have the C interrupt handler dynamically call that user-supplied sub when the interrupt occurred.

XS code that sets up the interrupt through the external lib:

void interruptHandler(); int setInterrupt(int pin, int edge, char *callback); char *perl_callback; PerlInterpreter *mine; void interruptHandler(){ PERL_SET_CONTEXT(mine); dSP; PUSHMARK(SP); PUTBACK; call_pv(perl_callback, G_DISCARD|G_NOARGS); FREETMPS; LEAVE; } int setInterrupt(int pin, int edge, char *callback){ perl_callback = callback; int interrupt = wiringPiISR(pin, edge, &interruptHandler); return interrupt; } # XS int setInterrupt(pin, edge, callback) int pin int edge char *callback void interruptHandler()

Perl module code that calls into the XS:

sub set_interrupt { shift if @_ == 4; my ($pin, $edge, $callback) = @_; setInterrupt($pin, $edge, $callback); }

...and finally a basic test script:

use warnings; use strict; use RPi::WiringPi; use RPi::WiringPi::Constant qw(:all); my $pi = RPi::WiringPi->new(); my $pin = $pi->pin(27); RPi::WiringPi::Core::set_interrupt( $pin->num, EDGE_RISING, 'pin_27_edge_rise' ); $pin->mode(INPUT); sleep 10; # manually change the pin state here to trigger # interrupt $pi->cleanup; sub pin_27_edge_rise { print "pin 27 edge rise callback...\n"; # do other stuff }

Replies are listed 'Best First'.
Re^2: XS: Passing an external library's function a Perl XS callback
by RonW (Parson) on Aug 16, 2016 at 20:09 UTC

    I see where you declare PerlInterpreter *mine to hold the Perl context, but where you assign it?

    In his linked example, BrowserUk has:

    void setCallback( SV *cb1, SV* cb2, SV *cb3, SV *cb4 ) { saved = Perl_get_context();

    but I don't see an equivalent in your code.

      I can confirm that you have to run
      mine = Perl_get_context();
      before you can use this. The run of
      dSP;
      just segfaults if
      mine
      have not been set via
      mine = Perl_get_context();
      I call on initialisation of my module a XS function which does does do
      mine = Perl_get_context();
      and verify that mine is not NULL before trying to call a perl function.

      All I needed is the following to get it working, and call the function "functionname" within of the module I called "setcallback" from.
      The use in C:
      void dorunperlfunction() { if (mine) { PERL_SET_CONTEXT(mine); dSP; ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; call_pv("functionname", G_DISCARD|G_NOARGS); FREETMPS; LEAVE; }
      The initialisation in XS(!):
      PerlInterpreter * mine = NULL; ... void setcallback() CODE: ... mine = Perl_get_context(); ...