tfoertsch has asked for the wisdom of the Perl Monks concerning the following question:

While working on the interpreter management of mod_perl I saw various invocations of PERL_SET_CONTEXT(aTHX).

For example

if (r || c) { interp = modperl_interp_select(r, c, s); aTHX = interp->perl; } else { /* Child{Init,Exit}, OpenLogs */ aTHX = scfg->mip->parent->perl; PERL_SET_CONTEXT(aTHX); }

So, what does PERL_SET_CONTEXT do? Why is it called in the else branch but not in the if? Is that maybe an error?

When do I have to call PERL_SET_CONTEXT? Is it an expensive operation? Wouldn't it be right to call it each time an interpreter is selected for a certain task?

Thanks,
Torsten

Replies are listed 'Best First'.
Re: What is PERL_SET_CONTEXT for?
by kyle (Abbot) on Sep 26, 2007 at 16:48 UTC

    See perlembed (GIYF):

    Note the calls to PERL_SET_CONTEXT(). These are necessary to initialize the global state that tracks which interpreter is the "current" one on the particular process or thread that may be running it. It should always be used if you have more than one interpreter and are making perl API calls on both interpreters in an interleaved fashion.

    PERL_SET_CONTEXT(interp) should also be called whenever interp is used by a thread that did not create it (using either perl_alloc(), or the more esoteric perl_clone()).

    In this case, it looks to me as if PERL_SET_CONTEXT is being used because the interpreter is being used by a thread that didn't create it.