
w2learn at yahoo
Nov 20, 2009, 1:07 AM
Post #2 of 2
(119 views)
Permalink
|
|
Re: Multiple embed interpreters : seg fault due to incorrect use?
[In reply to]
|
|
Hello, I have got this problem resolved by trying few ideas I got searchig archives, googling etc. Posting my solution here for others. There maybe better solutions and I suspect this may still have other issues (eg do I need to cleanup sv_dup return?) a) I saved the original perl-interpreter during startup. In SWIG, I specify %init %{ my_init(); %} %inline %{ static PerlInterpreter *orig_perl=NULL; void my_init(void) { orig_perl = PERL_GET_CONTEXT; } } b) modified mythread_fn like this instead of perl_alloc(). This change essentially got rid of the core dump. But, the args were empty when the perl sub was entered. PERL_SET_CONTEXT(orig_perl); perl_for_cb = perl_clone(orig_perl, CLONEf_KEEP_PTR_TABLE); PERL_SET_CONTEXT(perl_for_cb); c) added this code to the callback_handler before invoking the perl-sub via call_sv(). With this sv cloning, the arguments are fine when sub was entered. CLONE_PARAMS clone_param; clone_param.stashes = NULL; clone_param.flags = 0; clone_param.proto_perl = perl_for_cb; sv = sv_dup(sv, &clone_param); d) Also, added this to the callback registration function ("coderef" is the ref to the perl sub). SvSHARE(coderef); The problem I now face is that a global variable updated in the perl callback (ie. executing in cloned interpreter?) isnt getting reflected in the "main" of the perl script (orig interpreter). Adding this to the script throws a run-time error for a scalar that I did NOT declare/intend to be shared ("Invalid value for shared scalar at") use threads; use threads::shared; #updated in the perl callback..but update is not reflected in main, so #try sharing it. my $my_flag :shared =0; Thanks --- On Tue, 11/3/09, Sri <w2learn [at] yahoo> wrote: > From: Sri <w2learn [at] yahoo> > Subject: Multiple embed interpreters : seg fault due to incorrect use? > To: perl5-porters [at] perl > Date: Tuesday, November 3, 2009, 2:46 AM > Hi, > > I'm trying to provide a Perl interface to a C library > (event-driven). I've used SWIG (www.swig.org) to > create perl wrappings for this library. I'm getting a > seg fault in the depths of perl and I'm at a loss reg how to > proceed. I feel its related to multiple interpreter instance > usage. > > The C library has APIs, ofcourse, but it also allows > perl-subroutines to be registered as callbacks into the C > library. The C library has a thread that monitors > external events and invokes the perl-sub callbacks. > > The default perl interpreter is executing the script > (interpreter instance one). Neither SWIG nor I have > any code to create/initiaze this instance, as far as I can > see. My library thread, creates a new > interpreter. But, crashes in the call_sv(). If I > execute the same code (call_handler below, unmodified) as > part of an API call invoked from perl-script, it works just > fine. So, I feel I'm not initializing/using the 2nd > instance correctly or "coordinating" between the two > instances properly. If anyone has pointers or other > forums that may specialize in this topic, it would be great > help. > > ---using perl 5.8.8, compiled with multiplicity, > implicit_context--- > > void mythread_fn(void) > { > > static > PerlInterpreter *my_perl=NULL; > > my_perl = > perl_alloc(); > > PERL_SET_CONTEXT(my_perl); > > perl_construct(my_perl); > perl_run(my_perl); > > while (1) > { > > /* > monitor for events */ > if > (event == xyz) > > callback_handler(event); > } > > perl_destruct(my_perl); > perl_free(my_perl); > } > > void callback_handler(int event) > { > dTHX; > dSP; > > ENTER; > SAVETMPS; > > SV * sv = disc_callback; /* > saved earlier in an API call */ > if (sv == (SV*)NULL) > croak("Internal > error...\n"); > > PUSHMARK(SP); > XPUSHs(sv_2mortal(newSViv(event))); > PUTBACK; > > /* Call the Perl sub */ > call_sv(sv, G_DISCARD); /* > crashes as shown below in gdb */ > > FREETMPS; > LEAVE; > } > > Part of gdb stacktrace. > > #0 Perl_pp_entersub (my_perl=0x6587b0) at > pp_hot.c:2945 > #1 0x00000000004224f3 in S_call_body > (my_perl=0x6587b0, myop=0x65a6e0, > is_eval=1 '\001') at perl.c:2728 > #2 0x00000000004223c4 in Perl_call_sv > (my_perl=0x6587b0, sv=0x635420, flags=2) > at perl.c:2607 > > > > >
|