How to get threads->tid() value in XS code?
Question
I need to get current perl thread id in a C function inside *.XS part of a perl module.
In pure perl (*.pm part) I would simply do:
$id = threads->tid();
But what is a recommended way to get this value in XS?
Unfortunately http://perldoc.perl.org/perlthrtut.html and http://perldoc.perl.org/threads.html do not talk about dealing with threads in XS.
Thanks in advance
-- kmx
Solution
To call a method, one uses call_method
.
UV get_tid() {
dSP;
UV tid;
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSVpv("threads", 0)));
PUTBACK;
count = call_method("tid", G_SCALAR|G_EVAL);
SPAGAIN;
if (SvTRUE(ERRSV) || count != 1)
tid = 0;
else
tid = (UV)POPi;
PUTBACK;
FREETMPS;
LEAVE;
return tid;
}
OTHER TIPS
See how threads itself does it! I suggest you download threads
from CPAN, unpack it and take a look at threads.xs
. The part you need is a function called ithread_tid
.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow