develooper Front page | perl.perl5.porters | Postings from October 2001

Re: [PATCH h2xs] Use of static data in XS modules

Thread Previous | Thread Next
From:
Gurusamy Sarathy
Date:
October 28, 2001 17:20
Subject:
Re: [PATCH h2xs] Use of static data in XS modules
Message ID:
200110290119.f9T1JA107524@smtp3.ActiveState.com
On Sun, 28 Oct 2001 22:02:52 GMT, Tim Bunce wrote:
>I think it needs some more words of explanation (or pointers) in
>the docs and/or generated code as to why it's needed and how the fields
>in the struct are accessed.

Yes, ideally, there would be documentation and examples in perlxs.pod.
Paul said he might look into that.  Right now, perl.h has the following
by way of comments, which should be enough for someone to write the
perlxs.pod additions:

: /*
:  * Boilerplate macros for initializing and accessing interpreter-local
:  * data from C.  All statics in extensions should be reworked to use
:  * this, if you want to make the extension thread-safe.  See ext/re/re.xs
:  * for an example of the use of these macros.
:  *
:  * Code that uses these macros is responsible for the following:
:  * 1. #define MY_CXT_KEY to a unique string, e.g. "DynaLoader_guts"
:  * 2. Declare a typedef named my_cxt_t that is a structure that contains
:  *    all the data that needs to be interpreter-local.
:  * 3. Use the START_MY_CXT macro after the declaration of my_cxt_t.
:  * 4. Use the MY_CXT_INIT macro such that it is called exactly once
:  *    (typically put in the BOOT: section).
:  * 5. Use the members of the my_cxt_t structure everywhere as
:  *    MY_CXT.member.
:  * 6. Use the dMY_CXT macro (a declaration) in all the functions that
:  *    access MY_CXT.
:  */
: 
: #if defined(PERL_IMPLICIT_CONTEXT)
: 
: /* This must appear in all extensions that define a my_cxt_t structure,
:  * right after the definition (i.e. at file scope).  The non-threads
:  * case below uses it to declare the data as static. */
: #define START_MY_CXT
: 
: /* Fetches the SV that keeps the per-interpreter data. */
: #define dMY_CXT_SV \
:         SV *my_cxt_sv = *hv_fetch(PL_modglobal, MY_CXT_KEY,             \
:                                   sizeof(MY_CXT_KEY)-1, TRUE)
: 
: /* This declaration should be used within all functions that use the
:  * interpreter-local data. */
: #define dMY_CXT \
:         dMY_CXT_SV;                                                     \
:         my_cxt_t *my_cxtp = (my_cxt_t*)SvUV(my_cxt_sv)
: 
: /* Creates and zeroes the per-interpreter data.
:  * (We allocate my_cxtp in a Perl SV so that it will be released when
:  * the interpreter goes away.) */
: #define MY_CXT_INIT \
:         dMY_CXT_SV;                                                     \
:         /* newSV() allocates one more than needed */                    \
:         my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1));\
:         Zero(my_cxtp, 1, my_cxt_t);                                     \
:         sv_setuv(my_cxt_sv, (UV)my_cxtp)
: 
: /* This macro must be used to access members of the my_cxt_t structure.
:  * e.g. MYCXT.some_data */
: #define MY_CXT          (*my_cxtp)
: 
: /* Judicious use of these macros can reduce the number of times dMY_CXT
:  * is used.  Use is similar to pTHX, aTHX etc. */
: #define pMY_CXT         my_cxt_t *my_cxtp
: #define pMY_CXT_        pMY_CXT,
: #define _pMY_CXT        ,pMY_CXT
: #define aMY_CXT         my_cxtp
: #define aMY_CXT_        aMY_CXT,
: #define _aMY_CXT        ,aMY_CXT
: 
: #else /* USE_ITHREADS */
: 
: #define START_MY_CXT    static my_cxt_t my_cxt;
: #define dMY_CXT_SV      dNOOP
: #define dMY_CXT         dNOOP
: #define MY_CXT_INIT     NOOP
: #define MY_CXT          my_cxt
: 
: #define pMY_CXT         void
: #define pMY_CXT_
: #define _pMY_CXT
: #define aMY_CXT
: #define aMY_CXT_
: #define _aMY_CXT
: 
: #endif /* !defined(USE_ITHREADS) */


Sarathy
gsar@ActiveState.com

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