http://www.perlmonks.org?node_id=1228037


in reply to XS callback to mpv_set_wakeup_callback

I suspect your problem lies in some random foreign thread calling the callback. The dTHX macro in your callback function expands (depending on platform and configuration) to something like
PerlInterpreter* my_perl = pthread_getspecific(PL_thr_key);
This means that it attempts to set the current interpreter (my_perl) to whatever value is stored in thread-local storage for that thread. For a perl thread, that value will point to the interpreter associated with that thread. For a foreign thread, it's likely to be NULL.

The question is, if a foreign thread calls the callback, what interpreter structure should it use? Once you have decided that, then you'll need to do

PERL_SET_CONTEXT(some_perl);
at some point. See perlguts and perlembed.

The question of what interpreter to use depends on your setup - how threads are created and managed - but in any case, you need to avoid two separate threads sharing the same interpreter.

Dave.