develooper Front page | perl.ithreads | Postings from August 2008

Re: XS callbacks from different threads.

Thread Previous | Thread Next
From:
Shmuel Fomberg
Date:
August 5, 2008 13:04
Subject:
Re: XS callbacks from different threads.
Message ID:
4898B22E.8090905@012.net.il
Josef Wolf wrote:

>> For this one you need a real expert. It is possible to call into Perl 
>> from an XS sub. but to call into Perl from other threads while it is 
>> inside a XS call? I have no idea.
> 
> As far as I understand the corresponding section in perlguts, it should
> be possible _if_ PERL_SET_CONTEXT() is called before.  But for some
> reason it keeps crashing :-(

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.

>>> 2. I need to create a second perl interpreter (or clone the first one).
>>>   Can somebody point me to example code how to do that?
>> That's actually no big deal. look at perldoc page perlembed, there are 
>> examples.
> What I am missing from those examples is the explanation how the
> different interpreters would communicate.

There are two ways to communicate between two thread: using Perl's 
mechanizem, or using C. either possible. just stick to one and don't mix.
For example, if you choose to use C communication, then after initizing 
the lib, the master perl should call some C function that waits until 
the lib finished working / need restart / etc. The lib will use the same 
system to release the master from it's sleep, either directly or even an 
XS sub that the perl code will call. (C calling Perl calling C...)

> And there's one more complication: the low-level library don't even
> state how many threads will call my callback.  Therefore I would
> prefer to have only _one_ interpreter and use a mutex to keep the
> threads from entering the interpreter simultaneously.

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.

> static SV *callback_ref = (SV*)NULL;

Reality check: you do know that "static" means here, right?

> 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? 
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...

Good luck, anyway.

Shmuel.


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About