How to get threads->tid() value in XS code?

1.2k views Asked by At

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

2

There are 2 answers

4
ikegami On BEST ANSWER

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;
 }
1
Borodin On

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.