kthakore has asked for the wisdom of the Perl Monks concerning the following question:

I am currently working on updating the SDL bindings for Perl. I need to do some callbacks for functions that need function pointers as parameters. Some functions do not need threaded callbacks and I have been able to get these to work. SDL_EventFilter implementation .
/* Static Memory for event filter call back */ static SV * eventfiltersv; int eventfilter_cb( const void * event) { dSP; int count; int filter_signal; SV * eventref = newSV( sizeof(SDL_Event *) ); void * copyEvent = safemalloc( sizeof(SDL_Event) ); memcpy( copyEvent, event, sizeof(SDL_Event) ); ENTER; SAVETMPS; PUSHMARK(SP); XPUSHs( sv_setref_pv( eventref, "SDL::Event", (void *)copyEvent) ) +; PUTBACK; count = call_sv(eventfiltersv, G_SCALAR); SPAGAIN; if (count != 1 ) croak("callback returned more than 1 value\n"); + filter_signal = POPi; FREETMPS; LEAVE; return filter_signal; } void events_set_event_filter(callback) SV* callback CODE: eventfiltersv = callback; SDL_SetEventFilter( (SDL_EventFilter *)eventfilter_cb);
However this same method falls apart for functions which may be threaded. The Threaded callbacks are used in time_set_timer and time_add_timer. This is what I have tried
#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 <SDL.h> SV* set_timersv; Uint32 set_timer_cb( Uint32 interval) { Uint32 retval; int back; SV* cmd; ENTER_TLS_CONTEXT dSP; cmd = (SV*)set_timersv; ENTER; SAVETMPS; PUSHMARK(SP); XPUSHs(sv_2mortal(newSViv(interval))); PUTBACK; if (0 != (back = call_sv(cmd,G_SCALAR))) { SPAGAIN; if (back != 1 ) Perl_croak (aTHX_ "Timer Callback failed!"); retval = POPi; } else { Perl_croak(aTHX_ "Timer Callback failed!"); } FREETMPS; LEAVE; LEAVE_TLS_CONTEXT return retval; } int time_set_timer ( interval, callback ) Uint32 interval SV* callback CODE: set_timersv = callback; RETVAL = SDL_SetTimer(interval, set_timer_cb); OUTPUT: RETVAL
To build the new SDL Perl redesign code please refer to this link . My goal is to be able to do this
use SDL; use SDL::Time; SDL::init(SDL_INIT_TIMER); my $time = 0; SDL::Timer::set_timer(100, sub { $time++; return $_[0]} ); print 'this will not print!'
Currently this will die silently even with valgrind.