Hi BrowserUk:

I got back in town and got your original and a modified version working (mostly changes like VOID to void * etc were needed). Ofcourse, this worked just as in your experience. So, didnt post that version here. However, I got two other versions that do not work and fail similar to my real project. I've posted them below. I really hope you will be able to shed some light. Thanks

cback_test.pl & cback_test.pm : In this variant, both CCBs are invoked from same thread. I tried to structure the files closer to how my project files are structured (i.e separate pm). Please see addl comments in the code too.

The 2nd version is posted further below

#! perl -slw use strict; use cback_test; use Inline C => Config => BUILD_NOISY => 1; use Inline C => <<'END_C', NAME => '_867652', CLEAN_AFTER_BUILD => 0; PerlInterpreter *saved1; SV *cback1; PerlInterpreter *saved2; SV *cback2; void cbProc1( unsigned int time ) { //this always returns "0" PerlInterpreter *temp = Perl_get_context(); printf( "Time in C-Callback1: %u. Prior ctx %lud\n", time , temp); PERL_SET_CONTEXT( saved1 ); { dSP; ENTER; SAVETMPS; PUSHMARK( SP ); XPUSHs( sv_2mortal( newSVuv( (UV)time ) ) ); PUTBACK; call_sv( cback1, G_DISCARD ); FREETMPS; LEAVE; } PERL_SET_CONTEXT( temp ); return; } void cbProc2( unsigned int time ) { //this always returns "0" PerlInterpreter *temp = Perl_get_context(); printf( "Time in C-Callback2: %u. Prior ctx %lud\n", time , temp) +; PERL_SET_CONTEXT( saved2 ); { dSP; ENTER; SAVETMPS; PUSHMARK( SP ); XPUSHs( sv_2mortal( newSVuv( (UV)time ) ) ); PUTBACK; //the error msg shows up after calling this call_sv( cback2, G_DISCARD ); FREETMPS; LEAVE; } PERL_SET_CONTEXT( temp ); return; } void* thread1( void *arg ) { int i=0; while( i++<5 ) { sleep( 5 ); printf( "Here in thread1..calling CCB1\n" ); cbProc1( time(NULL) ); } i=0; while( i++<5 ) { sleep( 5 ); printf( "Here in thread1..calling CCB2\n" ); cbProc2( time(NULL) ); } } void* thread2( void *arg ) { while( sleep( 7 ), 1 ) { printf( "Here in thread2\n" ); cbProc2( time(NULL) ); } } void setCallback1( SV* cv ) { pthread_t tid; //my code did not have the dTHX here, but adding it made no difference + (i.e saved1 the same). //dTHX; //saved1 and saved2 are always the same too. saved1 = Perl_get_context(); cback1 = cv; printf("saved1 = %lud\n", saved1); // Start callback timer thread pthread_create( &tid, NULL, thread1, NULL ); return; } void setCallback2( SV* cv ) { pthread_t tid; //my code did not have the dTHX here, but adding it made no difference + (i.e saved2 is the same). //dTHX; saved2 = Perl_get_context(); cback2 = cv; printf("saved2 = %lud\n", saved2); // Start callback timer thread //pthread_create( &tid, NULL, thread2, NULL ); return; } END_C $|++; print "Setting callback1..\n"; #Registering cback rhis gives a coredump or one or several other error +s #when CCB calls PCB #setCallback1(\&cback_test::callback1); #this syntax works as expected in this program, but this doesnt #work (the pcb ref is not valid or null) when my typemap using SWIG ru +ns setCallback1('cback_test::callback1'); while ($cback_test::cb1_cnt != 4) { print "Waiting for callback1 to finish..\n"; sleep(1); } print "Setting callback2..\n"; setCallback2(\&cback_test::callback2); while ($cback_test::cb2_cnt != 4) { print "Waiting for callback2 to finish..\n"; sleep(1); } sleep (15); __END__

package cback_test; $cb1_cnt = 0; sub callback1 { print "Timer value in callback1 is: $_[0]"; $cb1_cnt++; return; } $cb2_cnt = 0; sub callback2 { print "Timer value in callback2 is: $_[0]"; $cb2_cnt++; return; } 1;

Here in thread1..calling CCB2 Time in C-Callback2: 1289205658. Prior ctx 0d Use of inherited AUTOLOAD for non-method main::1289205658() is depreca +ted at cback_test3.pl line 137. Segmentation fault

myModule.xs and test.pl : These are from the a "fake" module I created using  h2xs -AX myModule. I then had to  ln -s lib/myModule.pm . and  ln -s blib/arch/auto/myModule/myModule.so . to successfully run the test.pl (I'm sure there is a better way, but this worked).

#include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include "ppport.h" #include <pthread.h> static PerlInterpreter *orig_perl=NULL; SV* cb_ptr1 = NULL; SV* cb_ptr2 = NULL; void InvokeCB () { static int val = 0; SV * sv; val++; Perl_set_context(orig_perl); printf("curr_ctx is %lud\n", Perl_get_context()); dSP; ENTER; SAVETMPS; PUSHMARK(SP); if (val >= 5) sv = cb_ptr2; else sv = cb_ptr1; printf("invoking %lud\n", sv); XPUSHs(sv_2mortal(newSViv(val))); + PUTBACK; call_sv(sv, G_DISCARD); FREETMPS; LEAVE; } void * BGThread(void * dontcare) { while (1) { sleep(5); InvokeCB(); } } MODULE = myModule PACKAGE = myModule int RegisterCB1 (SV *SubRef) CODE: orig_perl = Perl_get_context(); printf("orig_ctx is %lud\n", orig_perl); pthread_t tid; pthread_create(&tid, NULL, BGThread, NULL); orig_perl = PERL_GET_CONTEXT; cb_ptr1 = SubRef; printf("registered %lud\n", cb_ptr1); RETVAL = 1; OUTPUT: RETVAL int RegisterCB2 (SV *SubRef) CODE: cb_ptr2 = SubRef; printf("registered %lud\n", cb_ptr1); RETVAL = 1; OUTPUT: RETVAL
#! /usr/local/bin/perl use myModule; use warnings; $cb_done1 = 0; $cb_done2 = 0; @results = (); sub cb_one { ($value) = @_; print "cb_onr called. val received : ", $value, "\n"; $results[scalar(@results)] = $value; if ($value == 5) { print "cb_done changed to one.\n"; $cb_done1 = 1; } } sub cb_two { ($value) = @_; print "cb_two called. val received : ", $value, "\n"; $results[scalar(@results)] = $value; if ($value == 10) { print "cb_done2 changed to one.\n"; $cb_done2 = 1; } } print "Registering CB1...\n"; $status = myModule::RegisterCB1(\&main::cb_one); do { print "Waiting for CB1 to be done...\n"; sleep (1); } until ($cb_done1 == 1); print "CB1 was invoked : $cb_done1\n"; print "results are : @results \n"; $status = myModule::RegisterCB2(\&main::cb_two); do { print "Waiting for CB2 to be done...\n"; sleep (1); } until ($cb_done2 == 1); print "CB2 was invoked : $cb_done2\n"; print "results are : @results \n";
Registering CB1... orig_ctx is 151171080d registered 151189632d Waiting for CB1 to be done... Waiting for CB1 to be done... Waiting for CB1 to be done... Waiting for CB1 to be done... Waiting for CB1 to be done... curr_ctx is 151171080d invoking 151189632d Undefined subroutine &main::1 called at test.pl line 42. make: *** [test_dynamic] Error 255

In reply to Re^15: Perl crash during perl_clone by perlmonk1729
in thread Perl crash during perl_clone by perlmonk1729

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.