I am writing an XS wrapper to a library that makes its own threads and also expects C style callbacks. The intent is to use this library in a perl application, not in a C application with embedded perl. My design hinges on being able to call a perl sub within the C-style callback, allowing this library to throw events at me and for me to handle these events in perl. The problem is that the library in question is multi-threaded, and I never know what thread it will be executing in when it calls the C-style callback. From there I cannot call any perl API functions because I have no interpreter context. I realize that this is because perl is meant to start the threads so it can clone the interpreter at that point; because the library manages its own threads I end up unable to look up an interpreter.
So, my question is, what should I do? I can try to start a perl interpreter within the callback, but I have none to clone and I am nervous about managing the collection of cloned interpreters myself. I could try to handle the callback by passing info about the events back to one of the perl-capable threads in some pure-C based manner (zmq, is my first guess). I'm not sure if there is a better option, any suggestions or advice would be appreciated.