1 ################################################################################
5 ## $Date: 2006/01/14 18:08:03 +0100 $
7 ################################################################################
9 ## Version 3.x, Copyright (C) 2004-2006, Marcus Holland-Moritz.
10 ## Version 2.x, Copyright (C) 2001, Paul Marquess.
11 ## Version 1.x, Copyright (C) 1999, Kenneth Albanowski.
13 ## This program is free software; you can redistribute it and/or
14 ## modify it under the same terms as Perl itself.
16 ################################################################################
36 * Boilerplate macros for initializing and accessing interpreter-local
37 * data from C. All statics in extensions should be reworked to use
38 * this, if you want to make the extension thread-safe. See ext/re/re.xs
39 * for an example of the use of these macros.
41 * Code that uses these macros is responsible for the following:
42 * 1. #define MY_CXT_KEY to a unique string, e.g. "DynaLoader_guts"
43 * 2. Declare a typedef named my_cxt_t that is a structure that contains
44 * all the data that needs to be interpreter-local.
45 * 3. Use the START_MY_CXT macro after the declaration of my_cxt_t.
46 * 4. Use the MY_CXT_INIT macro such that it is called exactly once
47 * (typically put in the BOOT: section).
48 * 5. Use the members of the my_cxt_t structure everywhere as
50 * 6. Use the dMY_CXT macro (a declaration) in all the functions that
54 #if defined(MULTIPLICITY) || defined(PERL_OBJECT) || \
55 defined(PERL_CAPI) || defined(PERL_IMPLICIT_CONTEXT)
59 /* This must appear in all extensions that define a my_cxt_t structure,
60 * right after the definition (i.e. at file scope). The non-threads
61 * case below uses it to declare the data as static. */
64 #if { VERSION < 5.004_68 }
65 /* Fetches the SV that keeps the per-interpreter data. */
67 SV *my_cxt_sv = get_sv(MY_CXT_KEY, FALSE)
68 #else /* >= perl5.004_68 */
70 SV *my_cxt_sv = *hv_fetch(PL_modglobal, MY_CXT_KEY, \
71 sizeof(MY_CXT_KEY)-1, TRUE)
72 #endif /* < perl5.004_68 */
74 /* This declaration should be used within all functions that use the
75 * interpreter-local data. */
78 my_cxt_t *my_cxtp = INT2PTR(my_cxt_t*,SvUV(my_cxt_sv))
80 /* Creates and zeroes the per-interpreter data.
81 * (We allocate my_cxtp in a Perl SV so that it will be released when
82 * the interpreter goes away.) */
85 /* newSV() allocates one more than needed */ \
86 my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1));\
87 Zero(my_cxtp, 1, my_cxt_t); \
88 sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
90 /* This macro must be used to access members of the my_cxt_t structure.
91 * e.g. MYCXT.some_data */
92 #define MY_CXT (*my_cxtp)
94 /* Judicious use of these macros can reduce the number of times dMY_CXT
95 * is used. Use is similar to pTHX, aTHX etc. */
96 #define pMY_CXT my_cxt_t *my_cxtp
97 #define pMY_CXT_ pMY_CXT,
98 #define _pMY_CXT ,pMY_CXT
99 #define aMY_CXT my_cxtp
100 #define aMY_CXT_ aMY_CXT,
101 #define _aMY_CXT ,aMY_CXT
103 #endif /* START_MY_CXT */
106 /* Clones the per-interpreter data. */
107 #define MY_CXT_CLONE \
109 my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1));\
110 Copy(INT2PTR(my_cxt_t*, SvUV(my_cxt_sv)), my_cxtp, 1, my_cxt_t);\
111 sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
114 #else /* single interpreter */
118 #define START_MY_CXT static my_cxt_t my_cxt;
119 #define dMY_CXT_SV dNOOP
120 #define dMY_CXT dNOOP
121 #define MY_CXT_INIT NOOP
122 #define MY_CXT my_cxt
131 #endif /* START_MY_CXT */
134 #define MY_CXT_CLONE NOOP
141 #define MY_CXT_KEY "Devel::PPPort::_guts" XS_VERSION
144 /* Put Global Data in here */
154 /* If any of the fields in the my_cxt_t struct need
155 * to be initialised, do it here.
166 RETVAL = MY_CXT.dummy == 42;
175 RETVAL = MY_CXT.dummy == 43;
189 ok(&Devel::PPPort::MY_CXT_1());
190 ok(&Devel::PPPort::MY_CXT_2());
191 ok(&Devel::PPPort::MY_CXT_CLONE());