summaryrefslogtreecommitdiff
path: root/perl.h
diff options
context:
space:
mode:
authorJarkko Hietaniemi <jhi@iki.fi>2001-10-25 18:07:58 +0000
committerJarkko Hietaniemi <jhi@iki.fi>2001-10-25 18:07:58 +0000
commit89ca4ac7af707d34da190bf469b8073ada175d9b (patch)
treee9338f678f71d31acd664a643de302ea3bd3289f /perl.h
parent4e1a6d261e266112da00fd82e73dbe2a0631027c (diff)
downloadperl-89ca4ac7af707d34da190bf469b8073ada175d9b.tar.gz
Integrate changes #12652 and #12653 from maintperl;
more tweaks to change#12626 * move the boilerplate code over to perl.h and make DynaLoader use it * make re, Opcode, File::Glob and B threadsafe * re.xs needed s/deinstall/uninstall/ (guess nobody uses C<no re;> anywhere) include XS_VERSION in MY_CXT_KEY (tweak for change#12652) File::Glob required a bit more work in bleadperl because of ExtUtils::Constant (see the Makefile.PL change) p4raw-link: @12652 on //depot/maint-5.6/perl: 3bc8871b91a24662eada2114d9a016153718b1c4 p4raw-link: @12626 on //depot/maint-5.6/perl: 512dcce54ea4db665708f91609bdd0a6126d1acd p4raw-id: //depot/perl@12654 p4raw-integrated: from //depot/maint-5.6/perl@12650 'edit in' ext/B/B.xs ext/DynaLoader/dlutils.c (@12652..) 'merge in' perl.h (@12597..) ext/File/Glob/Glob.xs ext/Opcode/Opcode.xs ext/re/re.xs (@12652..)
Diffstat (limited to 'perl.h')
-rw-r--r--perl.h77
1 files changed, 77 insertions, 0 deletions
diff --git a/perl.h b/perl.h
index 2959df463f..659d8b6862 100644
--- a/perl.h
+++ b/perl.h
@@ -3776,6 +3776,83 @@ typedef struct am_table_short AMTS;
# endif
#endif
+/*
+ * 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(USE_ITHREADS)
+
+/* 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) */
+
#ifdef I_FCNTL
# include <fcntl.h>
#endif