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

I need to lock() a variable in an Inline::C function. Does anyone know how?

Replies are listed 'Best First'.
Re: threads::shared locking via C API
by zentara (Cardinal) on Aug 24, 2011 at 17:22 UTC
      I need to use the lock in the Inline::C parts and the regular perl parts. If the implementation uses pthreads that's great, but I still need to identify the actual mutexes used by the interpreter.
Re: threads::shared locking via C API
by BrowserUk (Patriarch) on Aug 24, 2011 at 18:24 UTC

    The function you need to call is Perl_sharedsv_locksv(pTHX_ SV *sv) found in threads::shared.xs.

    But good luck in working out how to link to it.

    Another mystery that has long defeated me is quite where the perl-level lock() function (keyword or operator) lives and how it gets translated into a call to the above.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

        Yes, thanks. But that doesn't get you very far.

        c:\test\perl-5.14.0-RC1>findstr SvLOCK *.? pp.c: SvLOCK(sv); sv.h:=for apidoc Am|void|SvLOCK|SV* sv sv.h:#define SvLOCK(sv) PL_lockhook(aTHX_ sv) c:\test\perl-5.14.0-RC1>findstr PL_lockhook *.? embedvar.h:#define PL_lockhook (vTHX->Ilockhook) embedvar.h:#define PL_Ilockhook PL_lockhook sv.c: PL_lockhook = proto_perl->Ilockhook; sv.h:#define SvLOCK(sv) PL_lockhook(aTHX_ sv) c:\test\perl-5.14.0-RC1>findstr Ilockhook *.? embedvar.h:#define PL_lockhook (vTHX->Ilockhook) embedvar.h:#define PL_Ilockhook PL_lockhook intrpvar.h:PERLVARI(Ilockhook, share_proc_t, Perl_sv_nosharing) sv.c: PL_lockhook = proto_perl->Ilockhook;

        The whole 'lock locks the variable if threading is enable but is a silent noop otherwise', is the single most pointless piece of logic ever. (After the over-elaborate feature mechanism that is.)


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: threads::shared locking via C API
by duyet (Friar) on Aug 24, 2011 at 16:15 UTC
    Not sure what you meant by lock a var, but if you don't want a var to be changed, defined it as constant.