Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating Threaded callbacks in XS
    text
    copied!<p>EDIT: I have created a <a href="http://sdlperl.ath.cx/projects/SDLPerl/ticket/63" rel="nofollow noreferrer">ticket</a> for this which has data on an alternative to this way of doing things. </p> <p>I have <a href="http://github.com/kthakore/SDL_perl/commit/e5aede73e86d54da85f621790492344de47f0580" rel="nofollow noreferrer">updated the code</a> in an attempt to use MY_CXT's callback as gcxt was not storing across threads. However this segfaults at ENTER.</p> <pre><code>#include "EXTERN.h" #include "perl.h" #include "XSUB.h" #ifndef aTHX_ #define aTHX_ #endif #ifdef USE_THREADS #define HAVE_TLS_CONTEXT #endif /* For windows */ #ifndef SDL_PERL_DEFINES_H #define SDL_PERL_DEFINES_H #ifdef HAVE_TLS_CONTEXT PerlInterpreter *parent_perl = NULL; extern PerlInterpreter *parent_perl; #define GET_TLS_CONTEXT parent_perl = PERL_GET_CONTEXT; #define ENTER_TLS_CONTEXT \ PerlInterpreter *current_perl = PERL_GET_CONTEXT; \ PERL_SET_CONTEXT(parent_perl); { \ PerlInterpreter *my_perl = parent_perl; #define LEAVE_TLS_CONTEXT \ } PERL_SET_CONTEXT(current_perl); #else #define GET_TLS_CONTEXT /* TLS context not enabled */ #define ENTER_TLS_CONTEXT /* TLS context not enabled */ #define LEAVE_TLS_CONTEXT /* TLS context not enabled */ #endif #endif #include &lt;SDL.h&gt; #define MY_CXT_KEY "SDL::Time::_guts" XS_VERSION typedef struct { void* data; SV* callback; Uint32 retval; } my_cxt_t; static my_cxt_t gcxt; START_MY_CXT static Uint32 add_timer_cb ( Uint32 interval, void* param ) { ENTER_TLS_CONTEXT dMY_CXT; dSP; int back; ENTER; //SEGFAULTS RIGHT HERE! SAVETMPS; PUSHMARK(SP); XPUSHs(sv_2mortal(newSViv(interval))); PUTBACK; if (0 != (back = call_sv(MY_CXT.callback,G_SCALAR))) { SPAGAIN; if (back != 1 ) Perl_croak (aTHX_ "Timer Callback failed!"); MY_CXT.retval = POPi; } else { Perl_croak(aTHX_ "Timer Callback failed!"); } FREETMPS; LEAVE; LEAVE_TLS_CONTEXT dMY_CXT; return MY_CXT.retval; } MODULE = SDL::Time PACKAGE = SDL::Time PREFIX = time_ BOOT: { MY_CXT_INIT; } SDL_TimerID time_add_timer ( interval, cmd ) Uint32 interval void *cmd PREINIT: dMY_CXT; CODE: MY_CXT.callback=cmd; gcxt = MY_CXT; RETVAL = SDL_AddTimer(interval,add_timer_cb,(void *)cmd); OUTPUT: RETVAL void CLONE(...) CODE: MY_CXT_CLONE; </code></pre> <p>This segfaults as soon as I go into ENTER for the callback.</p> <pre><code>use SDL; use SDL::Time; SDL::init(SDL_INIT_TIMER); my $time = 0; SDL::Timer::add_timer(100, sub { $time++; return $_[0]} ); sleep(10); print "Never Prints"; </code></pre> <p>Output is</p> <pre><code>$ </code></pre> <p>it should be </p> <pre><code>$ Never Prints </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload