4

免责声明:在perlmonks 询问

我希望我能正确地描述和描述我的问题......在 XS 中,我试图将回调发送到外部库的函数中,其中回调具有 Perl 特定的函数。XSUB 作为函数指针传递给外部 C 函数。依次发送的 XSUB 回调回调到 `main` perl 应用程序中的子程序:

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

// example extern call

externFunc(&callback);

这是段错误。我认为这是因为外部库不理解被调用的 perl 函数。不过,如果我直接调用 C `callback()` 函数,一切正常。

我可以做一些魔术来使外部库“看到” Perl C 函数,还是我做错了什么?

这是我正在测试的代码:

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);
}

输出:

in init
in perl callback
Segmentation fault

如果我从回调中删除 perl 特定的 C 调用并只执行“printf()”或其他纯 C 工作,那么事情会继续进行而不会出现段错误。

4

1 回答 1

1

刚刚遇到这个问题,并认为我会给出自己的答案,就像我前段时间解决它一样。

在设置 Perl 上下文以及在 Cexec_perl_callback()函数中,我缺少一些重要的位。

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;
}

输出:

hello world, from perl!
于 2017-06-06T19:23:27.150 回答