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.


In reply to xs wrapper for a threaded library by fng

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.