4

Disclaimer: asked over at perlmonks.

I hope I'm describing and depicting my issue properly... In XS, I'm trying to send a callback into an external library's function, where the callback has Perl specific functions. The XSUB is passed as a function pointer to an external C function. The XSUB callback being sent in turn calls back to a sub in the `main` perl application:

void callback(){
    dSP;
    PUSHMARK(SP);
    call_pv("p_callback", G_DISCARD|G_NOARGS);
}

// example extern call

externFunc(&callback);

This segfaults. I think it's because the external library doesn't understand the perl functions that are being called. Things work fine if I call the C `callback()` function directly though.

Is there some magic that I can do to make the external library "see" the Perl C functions, or am I doing something wrong?

Here's the code I'm testing with:

use warnings;
use strict;

use Inline ('C' => 'DATA', libs => '-lwiringPi');

init();
setInterrupt(27, 3);

# direct call

callback();

# on() triggers the external function and sends
# it the callback

on(27);

sub p_callback {
    print "in perl callback\n";
}

__DATA__
__C__

#include <stdlib.h>
#include <stdio.h>
#include <wiringPi.h>

void init();
void on(int pin);
void off(int pin);
void setInterrupt(int pin, int edge);
void callback();

void init(){
    printf("in init\n");
    wiringPiSetup();
}
void on(int pin){
    pinMode(pin, 1);
    digitalWrite(pin, 1);
}

void off(int pin){
    digitalWrite(pin, 0);
    pinMode(pin, 0);
}

void setInterrupt(int pin, int edge){
    wiringPiISR(pin, edge, &callback);
}

void callback(){
    dSP;
    PUSHMARK(SP);
    call_pv("p_callback", G_DISCARD|G_NOARGS);
}

Output:

in init
in perl callback
Segmentation fault

If I remove the perl specific C calls from within the callback and just do a `printf()` or other pure-C work, things proceed without a segfault.

stevieb
  • 9,065
  • 3
  • 26
  • 36
  • 2
    Calling into Perl from an ISR seems like a bad idea. There are a lot of things that could go wrong. For example: Are you running a threaded Perl and is the ISR called from a different thread? – nwellnhof Aug 15 '16 at 00:06
  • @nwellnhof I'm brand new to interrupt handling, and also a C newb so this is a big learning curve :) You've pointed out that I need to do some research here before I proceed. Thanks! – stevieb Aug 15 '16 at 13:55

1 Answers1

1

Just came across this question and thought I'd give it my own answer as I did resolve it some time ago.

There were some important bits I was missing to set the Perl context, as well as within the C exec_perl_callback() function.

use warnings;
use strict;

use Inline 'C';
use Inline 'NoClean';

sub p_callback {
    print "hello, world from perl!\n";
}

exec_perl_callback('p_callback');

__END__
__C__

#define PERL_NO_GET_CONTEXT

PerlInterpreter * mine;

void callback(char* perl_callback){
    PERL_SET_CONTEXT(mine);

    dSP;
    ENTER;
    SAVETMPS;
    PUSHMARK(SP);
    PUTBACK;

    exec_perl_callback(perl_callback, G_DISCARD|G_NOARGS);

    FREETMPS;
    LEAVE;
}

Output:

hello world, from perl!
stevieb
  • 9,065
  • 3
  • 26
  • 36