#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "ppport.h"
CV* callback_ref;
int runCallback(const char* param)
{
int count;
int answer;
dSP;
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSVpv(param, 0)));
PUTBACK;
count = call_sv((SV*)callback_ref, G_SCALAR);
SPAGAIN;
if (count != 1)
croak("Uncknown return value format. Returned %d params instid 1", count);
answer = POPi;
FREETMPS;
LEAVE;
return answer;
}
MODULE = Callback PACKAGE = Callback
void
registCallback(cb)
CV* cb;
PROTOTYPE: $
PPCODE:
callback_ref = cb;
void
runCallback(param)
const char* param;
PROTOTYPE: $
PPCODE:
XPUSHs(sv_2mortal( newSViv(runCallback(param)) ));
####
package Callback;
use 5.010000;
use threads;
use threads::shared;
use strict;
use warnings;
require Exporter;
use AutoLoader qw(AUTOLOAD);
our @ISA = qw(Exporter);
our $VERSION = '0.01';
require XSLoader;
XSLoader::load('Callback', $VERSION);
sub new
{
my $invocant = shift;
my $callback_ref = shift;
$invocant = ref $invocant if ref $invocant;
registCallback($callback_ref);
my $self = &share({});
return bless $self, $invocant;
}
sub run
{
my $self = shift;
my $param = shift;
runCallback($param);
}
1;
####
#!/usr/bin/env perl
use strict;
use lib qw(./Callback/build/lib/perl5/i386-linux-thread-multi/);
use threads;
use threads::shared;
use Data::Dumper;
use Callback;
use constant MESSAGE => "Hellow world!!!\n";
my $obj = new Callback (\&printHellow);
my $thread = threads->create(sub {$obj->run(&MESSAGE)})
or exit(1);
$thread->join();
sub printHellow
{
my $message = shift;
print "MESSAGE: $message\n";
}