@@ -4536,53 +4536,71 @@ This class might be used like this:
45364536
45374537=head2 Safely Storing Static Data in XS
45384538
4539- Starting with Perl 5.8, a macro framework has been defined to allow
4540- static data to be safely stored in XS modules that will be accessed from
4541- a multi-threaded Perl.
4539+ You should generally avoid declaring static variables and data within an
4540+ XS file. The Perl interpreter binary is commonly configured to allow
4541+ multiple interpreter structures, with a complete set of interpreter state
4542+ per interpreter struct. In this case, you usually need your "static" data
4543+ to be per-interpreter rather than a single shared per-process value.
45424544
4543- Although primarily designed for use with multi-threaded Perl, the macros
4544- have been designed so that they will work with non-threaded Perl as well.
4545+ This becomes more important in the presence of multiple threads; either
4546+ via C<use threads> or where the Perl interpreter is embedded within
4547+ another application (such as a web server) which may manage its own
4548+ threads and allocate interpreters to threads as it sees fit.
45454549
4546- It is therefore strongly recommended that these macros be used by all
4547- XS modules that make use of static data.
4550+ A macro framework is available to XS code to allow a single C struct to be
4551+ declared and safely accessed. Behind the scenes, the struct will be
4552+ allocated per interpreter or thread; on non-threaded Perl interpreter
4553+ builds, the macros gracefully degrade to a single global instance. These
4554+ macros have C<MY_CXT> ("my context") as part of their names.
45484555
4549- The easiest way to get a template set of macros to use is by specifying
4550- the C<-g> (C<--global>) option with h2xs (see L<h2xs>) .
4556+ It is therefore strongly recommended that these macros be used by all XS
4557+ modules that make use of static data .
45514558
4552- Below is an example module that makes use of the macros.
4559+ When creating a new skeleton F<Foo.xs> file, you can use the C<--global>
4560+ option of F<h2xs> to also include a skeleton set of macros, e.g.
4561+
4562+ h2xs -A --global -n Foo::Bar
4563+
4564+ Below is a complete example module that makes use of the macros. It tracks
4565+ the names of up to three blind mice.
45534566
45544567 #define PERL_NO_GET_CONTEXT
45554568 #include "EXTERN.h"
45564569 #include "perl.h"
45574570 #include "XSUB.h"
4571+ #include "ppport.h"
4572+
4573+ #define MAX_NAME_LEN 100
45584574
45594575 /* Global Data */
45604576
45614577 #define MY_CXT_KEY "BlindMice::_guts" XS_VERSION
45624578
45634579 typedef struct {
45644580 int count;
4565- char name[3][100 ];
4581+ char name[3][MAX_NAME_LEN+1 ];
45664582 } my_cxt_t;
45674583
45684584 START_MY_CXT
45694585
45704586 MODULE = BlindMice PACKAGE = BlindMice
45714587
4588+ PROTOTYPES: DISABLE
4589+
45724590 BOOT:
45734591 {
45744592 MY_CXT_INIT;
45754593 MY_CXT.count = 0;
4576- strcpy(MY_CXT.name[0], "None");
4577- strcpy(MY_CXT.name[1], "None");
4578- strcpy(MY_CXT.name[2], "None");
45794594 }
45804595
45814596 int
4582- newMouse (char *name)
4597+ AddMouse (char *name)
45834598 PREINIT:
45844599 dMY_CXT;
45854600 CODE:
4601+ if (strlen(name) > MAX_NAME_LEN)
4602+ croak("Mouse name too long\n");
4603+
45864604 if (MY_CXT.count >= 3) {
45874605 warn("Already have 3 blind mice");
45884606 RETVAL = 0;
@@ -4595,13 +4613,12 @@ Below is an example module that makes use of the macros.
45954613 RETVAL
45964614
45974615 char *
4598- get_mouse_name(index)
4599- int index
4616+ get_mouse_name(int index)
46004617 PREINIT:
46014618 dMY_CXT;
46024619 CODE:
46034620 if (index > MY_CXT.count)
4604- croak("There are only 3 blind mice.");
4621+ croak("There are only %d blind mice.", MY_CXT.count );
46054622 else
46064623 RETVAL = MY_CXT.name[index - 1];
46074624 OUTPUT:
@@ -4612,43 +4629,78 @@ Below is an example module that makes use of the macros.
46124629 CODE:
46134630 MY_CXT_CLONE;
46144631
4615- =head3 MY_CXT REFERENCE
4632+ The main points from this example are:
4633+
4634+ =over
4635+
4636+ =item *
4637+
4638+ The C<my_cxt_t> struct will hold all your "static" data.
4639+
4640+ =item *
4641+
4642+ The C<MY_CXT_KEY> and C<START_MY_CXT> are boilerplate to make the macro
4643+ system work. The former is a string which should be unique to your module.
4644+
4645+ =item *
4646+
4647+ The C<MY_CXT_INIT> in the C<BOOT> section allocates the struct when the
4648+ module is loaded. You can add further boot code which does any
4649+ initialisation you require (such as setting C<count>). C<BOOT> is called
4650+ at most once per interpreter, when code in that interpreter instance first
4651+ does C<use BlindMice>.
4652+
4653+ =item *
4654+
4655+ Each XSUB includes a C<dMY_CXT> declaration, which retrieves a pointer to
4656+ the struct associated with the current interpreter and saves it in a
4657+ hidden auto variable; C<MY_CXT> allows you to access fields within this
4658+ structure.
4659+
4660+ =item *
4661+
4662+ C<MY_CXT_CLONE> creates a byte-for-byte copy of the current struct. This
4663+ is called from the special C<CLONE> XSUB, to ensure that each new thread
4664+ gets its own copy of the data which is otherwise shared by default.
4665+
4666+ =back
4667+
4668+ =head3 MY_CXT macros reference
46164669
46174670=over 5
46184671
46194672=item MY_CXT_KEY
46204673
4621- This macro is used to define a unique key to refer to the static data
4622- for an XS module. The suggested naming scheme, as used by h2xs, is to
4623- use a string that consists of the module name, the string "::_guts"
4624- and the module version number.
4674+ This macro is used to define a unique key to refer to the static data for
4675+ an XS module. The suggested naming scheme, as used by F< h2xs> , is to use a
4676+ string that consists of a concatenation of the module name, the string
4677+ C<::_guts> and the module version number:
46254678
46264679 #define MY_CXT_KEY "MyModule::_guts" XS_VERSION
46274680
4628- =item typedef my_cxt_t
4629-
4630- This struct typedef I<must> always be called C<my_cxt_t>. The other
4631- C<CXT*> macros assume the existence of the C<my_cxt_t> typedef name.
4681+ =item my_cxt_t
46324682
4633- Declare a typedef named C<my_cxt_t> that is a structure that contains
4634- all the data that needs to be interpreter-local.
4683+ The "static" values should be stored within a struct typedef which I<must>
4684+ always be called C<my_cxt_t>. The other C<*MY_CXT*> macros assume the
4685+ existence of the C<my_cxt_t> typedef name. For example:
46354686
46364687 typedef struct {
46374688 int some_value;
4689+ int some_other_value;
46384690 } my_cxt_t;
46394691
46404692=item START_MY_CXT
46414693
4642- Always place the START_MY_CXT macro directly after the declaration
4643- of C<my_cxt_t>.
4694+ This macro contains hidden boilerplate code. Always place the
4695+ C<START_MY_CXT> macro directly after the declaration of C<my_cxt_t>.
46444696
46454697=for apidoc Amnh||START_MY_CXT
46464698
46474699=item MY_CXT_INIT
46484700
4649- The MY_CXT_INIT macro initializes storage for the C<my_cxt_t> struct.
4701+ The C< MY_CXT_INIT> macro initializes storage for the C<my_cxt_t> struct.
46504702
4651- It I< must> be called exactly once, typically in a BOOT: section. If you
4703+ It must be called I< exactly once> , typically in a BOOT: section. If you
46524704are maintaining multiple interpreters, it should be called once in each
46534705interpreter instance, except for interpreters cloned from existing ones.
46544706(But see L</MY_CXT_CLONE> below.)
@@ -4657,21 +4709,21 @@ interpreter instance, except for interpreters cloned from existing ones.
46574709
46584710=item dMY_CXT
46594711
4660- Use the dMY_CXT macro (a declaration) in all the functions that access
4661- MY_CXT.
4712+ Use the C< dMY_CXT> macro (a declaration) at the start of all the XSUBs
4713+ (and other functions) that access C< MY_CXT> .
46624714
46634715=for apidoc Amnh||dMY_CXT
46644716
46654717=item MY_CXT
46664718
4667- Use the MY_CXT macro to access members of the C<my_cxt_t> struct. For
4719+ Use the C< MY_CXT> macro to access members of the C<my_cxt_t> struct. For
46684720example, if C<my_cxt_t> is
46694721
46704722 typedef struct {
46714723 int index;
46724724 } my_cxt_t;
46734725
4674- then use this to access the C<index> member
4726+ then use this to access the C<index> member:
46754727
46764728 dMY_CXT;
46774729 MY_CXT.index = 2;
@@ -4680,7 +4732,8 @@ then use this to access the C<index> member
46804732
46814733C<dMY_CXT> may be quite expensive to calculate, and to avoid the overhead
46824734of invoking it in each function it is possible to pass the declaration
4683- onto other functions using the C<aMY_CXT>/C<pMY_CXT> macros, eg
4735+ onto other functions using the argument/parameter C<aMY_CXT>/C<pMY_CXT>
4736+ macros, e.g.:
46844737
46854738=for apidoc Amnh||_aMY_CXT
46864739=for apidoc Amnh||aMY_CXT
@@ -4700,32 +4753,39 @@ onto other functions using the C<aMY_CXT>/C<pMY_CXT> macros, eg
47004753 MY_CXT.index = 2;
47014754 }
47024755
4703- Analogously to C<pTHX>, there are equivalent forms for when the macro is the
4704- first or last in multiple arguments, where an underscore represents a
4705- comma, i.e. C<_aMY_CXT>, C<aMY_CXT_>, C<_pMY_CXT> and C<pMY_CXT_>.
4756+ Analogously to C<pTHX>, there are equivalent forms for when the macro is
4757+ the first or last in multiple arguments, where an underscore is expanded
4758+ to a comma where appropriate, i.e. C<_aMY_CXT>, C<aMY_CXT_>, C<_pMY_CXT>
4759+ and C<pMY_CXT_>. These allow for the possibility that those macros might
4760+ optimise away any actual argument without leaving a stray comma.
47064761
47074762=item MY_CXT_CLONE
47084763
4709- By default, when a new interpreter is created as a copy of an existing one
4710- (eg via C<< threads->create() >>), both interpreters share the same physical
4711- my_cxt_t structure. Calling C<MY_CXT_CLONE> (typically via the package's
4712- C<CLONE()> function), causes a byte-for-byte copy of the structure to be
4713- taken, and any future dMY_CXT will cause the copy to be accessed instead.
4764+ When a new interpreter is created as a copy of an existing one (e.g. via
4765+ C<< threads->create() >>), then by default, both interpreters share the
4766+ same physical my_cxt_t structure. Calling C<MY_CXT_CLONE> (typically via
4767+ the package's C<CLONE()> function), causes a byte-for-byte copy of the
4768+ structure to be taken (but not a deep copy) and any future C<dMY_CXT> will
4769+ cause the copy to be accessed instead.
4770+
4771+ This is typically used within the C<CLONE> method which is called each
4772+ time an interpreter is copied (usually when creating a new thread). Other
4773+ code can be added to C<CLONE()> to deep copy items within the structure.
47144774
47154775=for apidoc Amnh||MY_CXT_CLONE
47164776
47174777=item MY_CXT_INIT_INTERP(my_perl)
47184778
47194779=item dMY_CXT_INTERP(my_perl)
47204780
4721- These are versions of the macros which take an explicit interpreter as an
4722- argument.
4781+ These are variants of the C<MY_CXT_INIT> and C<dMY_CXT> macros which take
4782+ an explicit perl interpreter as an argument.
47234783
47244784=back
47254785
47264786Note that these macros will only work together within the I<same> source
4727- file; that is, a dMY_CTX in one source file will access a different structure
4728- than a dMY_CTX in another source file.
4787+ file; that is, a C<dMY_CXT> in one source file will access a different
4788+ structure than a C<dMY_CXT> in another source file.
47294789
47304790=head1 EXAMPLES
47314791
0 commit comments