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.

In reply to Threaded Callbacks in XS by kthakore

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.