Using multiple perl interpreters
am 05.11.2009 05:49:15 von Sri
Hi,
I've read several posts on this forum on similar topic, but not able to figure out the problem in my specific case. So hoping someone can help me here. Essentially, its a seg fault when using a thread-specific interpreter.
I'm trying to provide a Perl interface to a C library (event-driven). 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). I do NOT have any code to create/initiaze this instance in my api wrappers. My library's 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.
I tried to use perl_clone instead of perl_alloc, but that method crashed too.
---using perl 5.8.8, compiled with multiplicity, implicit context (see below)---
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
parts from perl -V
cc='gcc', ccflags ='-D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS -DDEBUGGING -fno-strict-aliasing -pipe -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/include/gdbm',
Characteristics of this binary (from libperl):
Compile-time options: DEBUGGING MULTIPLICITY PERL_IMPLICIT_CONTEXT
PERL_MALLOC_WRAP THREADS_HAVE_PIDS USE_64_BIT_ALL
USE_64_BIT_INT USE_ITHREADS USE_LARGE_FILES
USE_PERLIO USE_REENTRANT_API
Re: Using multiple perl interpreters
am 20.11.2009 10:26:58 von Sri
I've posted an update/resolution reg this issue to perl5-porters. FYI
http://markmail.org/message/cjbhybjfikuirvud
--- On Wed, 11/4/09, Sri wrote:
> From: Sri
> Subject: Using multiple perl interpreters
> To: modperl@perl.apache.org
> Date: Wednesday, November 4, 2009, 8:49 PM
> Hi,
>=20
> I've read several posts on this forum on similar topic, but
> not able to figure out the problem in my specific case. So
> hoping someone can help me here.=A0 Essentially, its a
> seg fault when using a thread-specific interpreter.
>=20
> I'm trying to provide a Perl interface to a C library
> (event-driven).=A0 The C library has APIs, ofcourse, but
> it also allows perl-subroutines to be registered as
> callbacks into the C library.=A0 The C library has a
> thread that monitors external events and invokes the
> perl-sub callbacks.
>=20
> The default perl interpreter is executing the script
> (interpreter instance one).=A0 I do NOT have any code to
> create/initiaze this instance in my api
> wrappers. =A0My library's thread, creates a
> new interpreter.=A0 But, crashes in the call_sv().=A0
> If I execute the same code (call_handler below, unmodified)
> as part of an API call invoked from perl-script, it works
> just fine.=A0 So, I feel I'm not initializing/using the
> 2nd instance correctly or "coordinating" between the two
> instances properly.=A0 If anyone has pointers or other
> forums that may specialize in this topic, it would be great
> help.
>=20
> I tried to use perl_clone instead of perl_alloc, but that
> method crashed too.
>=20
> ---using perl 5.8.8, compiled with multiplicity, implicit
> context (see below)---
>=20
> void mythread_fn(void)
> {
>=20
> =A0 =A0 =A0 =A0static
> PerlInterpreter *my_perl=3DNULL;
>=20
> =A0 =A0 =A0 =A0my_perl =3D
> perl_alloc();
> =A0 =A0 =A0
> =A0PERL_SET_CONTEXT(my_perl);
> =A0 =A0 =A0
> =A0perl_construct(my_perl);
> =A0 =A0 =A0 =A0perl_run(my_perl);
>=20
> =A0 =A0 =A0 =A0while (1)
> =A0 =A0 =A0 =A0{
>=20
> =A0 =A0 =A0 =A0 =A0 =A0/*
> monitor for events */
> =A0 =A0 =A0 =A0 =A0 =A0if
> (event == xyz)
> =A0 =A0 =A0 =A0 =A0 =A0 =A0
> =A0callback_handler(event);
> =A0 =A0 =A0 =A0}
>=20
> =A0 =A0 =A0 =A0 perl_destruct(my_perl);
> =A0 =A0 =A0 =A0 perl_free(my_perl);
> }
>=20
> void callback_handler(int event)
> {
> =A0 =A0 =A0 dTHX;
> =A0 =A0 =A0 dSP;
>=20
> =A0 =A0 =A0 ENTER;
> =A0 =A0 =A0 SAVETMPS;
>=20
> =A0 =A0 =A0 SV * sv =3D disc_callback;=A0 /*
> saved earlier in an API call */
> =A0 =A0 =A0 if (sv == (SV*)NULL)
> =A0 =A0 =A0 =A0croak("Internal
> error...\n");
>=20
> =A0 =A0 =A0 PUSHMARK(SP);
> =A0 =A0 =A0 XPUSHs(sv_2mortal(newSViv(event)));
> =A0 =A0 =A0 PUTBACK;
>=20
> =A0 =A0 =A0 /* Call the Perl sub */
> =A0 =A0 =A0 call_sv(sv, G_DISCARD);=A0 /*
> crashes as shown below in gdb */
>=20
> =A0 =A0 =A0 FREETMPS;
> =A0 =A0 =A0 LEAVE;
> }
>=20
> Part of gdb stacktrace.
>=20
> #0=A0 Perl_pp_entersub (my_perl=3D0x6587b0) at
> pp_hot.c:2945
> #1=A0 0x00000000004224f3 in S_call_body
> (my_perl=3D0x6587b0, myop=3D0x65a6e0,
> =A0 =A0 is_eval=3D1 '\001') at perl.c:2728
> #2=A0 0x00000000004223c4 in Perl_call_sv
> (my_perl=3D0x6587b0, sv=3D0x635420, flags=3D2)
> =A0 =A0 at perl.c:2607
>=20
> parts from perl -V
>=20
> =A0 =A0 cc=3D'gcc', ccflags =3D'-D_REENTRANT
> -D_GNU_SOURCE -DTHREADS_HAVE_PIDS -DDEBUGGING
> -fno-strict-aliasing -pipe -I/usr/local/include
> -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=3D64
> -I/usr/include/gdbm',
>=20
> Characteristics of this binary (from libperl):
> =A0 Compile-time options: DEBUGGING MULTIPLICITY
> PERL_IMPLICIT_CONTEXT
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0
> =A0 =A0 =A0 =A0 PERL_MALLOC_WRAP
> THREADS_HAVE_PIDS USE_64_BIT_ALL
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0
> =A0 =A0 =A0 =A0 USE_64_BIT_INT USE_ITHREADS
> USE_LARGE_FILES
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0
> =A0 =A0 =A0 =A0 USE_PERLIO USE_REENTRANT_API
>=20
>=20
>=20
>=20
> =A0 =A0
>
=0A