Thanks for your help and your passience, Shmuel!

On Tue, Aug 05, 2008 at 11:03:58PM +0300, Shmuel Fomberg wrote:

> Well, maybe, but there is already one thread that is passing through 
> perl, so calling PERL_SET_CONTEXT for this perl on other thread is 
> pulling the rug under the first thread's legs. it can't be good.

I've got it working now.  The solution is to PERL_SET_CONTEXT()
_before_ declaring the context with dTHX.  The code looks like this now:


static PerlInterpreter *orig_perl;
static SV *callback_ref = (SV*)NULL;

static int do_call_perl (size_t argc, char *argv[])
{
    dTHX;
    dSP;
    I32 ax;
 /* ... snipped here ... */
}

static int call_perl_argv (size_t argc, char *argv[])
{
    int ret;
    static pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;

    pthread_mutex_lock(&mutex);
    PERL_SET_CONTEXT(orig_perl);

    ret = do_call_perl (argc, argv);

    pthread_mutex_unlock(&mutex);

    return ret;
}

void register_callback (SV *callback)
{
    dTHX;

    if (callback_ref == (SV*)NULL) {
        callback_ref = newSVsv (callback); /* first time, create new SV */
    } else {
        SvSetSV (callback_ref, callback);  /* been here, overwrite */
    }

    orig_perl = my_perl;

    do {
        sleep (3);
    } while (call_perl (1, "check"));

    shutdown_threads ();
}

> Maybe you should use two threads: one of the calling perl, and one slave 
> perl. because as said two paragraphs ago, I don't think that using one 
> perl interpreter will work.

Now I have multiple C-threads calling one perl interpreter.  Multiple
entries into the interpreter are protected by a mutex.  That's exactly
what I've tried to achieve (at least for now).

> >static SV *callback_ref = (SV*)NULL;
> 
> Reality check: you do know that "static" means here, right?

I think so: I have exactly one instance of this variable.  Do you see
any problems with this?

> >int call_perl (int cnt, ...)
> >{
> >    dTHX;
> >    PERL_SET_CONTEXT(my_perl);
> 
> I hate the "my_perl" parameter. Who defined it? what is it set to? 

AFAIK, this is created by dTHX.

> Especially if you have more then one perl interpreter in you code. I used:
>       dTHXa(ph->perl);
>       PERL_SET_CONTEXT(ph->perl);
> where ph was some struct that held the perl interpreter for me...

Ah, I did not know about dTHXa().

Reply via email to