#endif
#ifndef dMY_CXT
-# define MY_CXT vmg_globaldata
# define dMY_CXT
+# undef MY_CXT
+# define MY_CXT vmg_globaldata
+# undef START_MY_CXT
# define START_MY_CXT STATIC my_cxt_t MY_CXT;
+# undef MY_CXT_INIT
# define MY_CXT_INIT
+# undef MY_CXT_CLONE
+# undef aMY_CXT
+# undef pMY_CXT
+# define VMG_THREADSAFE 0
+#else
+# define VMG_THREADSAFE 1
#endif
#ifndef PERL_MAGIC_ext
START_MY_CXT
+STATIC void vmg_cxt_init
+#if defined(pMY_CXT) && defined(aMY_CXT)
+ (pTHX_ pMY_CXT) {
+# define vmg_cxt_init() vmg_cxt_init(aTHX_ aMY_CXT)
+#else
+ (pTHX) {
+ dMY_CXT;
+# define vmg_cxt_init() vmg_cxt_init(aTHX)
+#endif
+ MY_CXT.wizz = newHV();
+#ifdef USE_ITHREADS
+ HvSHAREKEYS_off(MY_CXT.wizz);
+#endif
+ MY_CXT.count = 0;
+ return;
+}
+
/* --- Signatures ---------------------------------------------------------- */
#define SIG_MIN ((U16) (1u << 8))
{
HV *stash;
MY_CXT_INIT;
- MY_CXT.wizz = newHV();
- MY_CXT.count = 0;
+ vmg_cxt_init();
stash = gv_stashpv(__PACKAGE__, 1);
newCONSTSUB(stash, "SIG_MIN", newSVuv(SIG_MIN));
newCONSTSUB(stash, "SIG_MAX", newSVuv(SIG_MAX));
newCONSTSUB(stash, "VMG_COMPAT_SCALAR_LENGTH_NOLEN",
newSVuv(VMG_COMPAT_SCALAR_LENGTH_NOLEN));
newCONSTSUB(stash, "VMG_PERL_PATCHLEVEL", newSVuv(VMG_PERL_PATCHLEVEL));
+ newCONSTSUB(stash, "VMG_THREADSAFE", newSVuv(VMG_THREADSAFE));
}
+void
+CLONE(...)
+PROTOTYPE: DISABLE
+CODE:
+#ifdef MY_CXT_CLONE
+ MY_CXT_CLONE;
+ vmg_cxt_init();
+#endif
+
SV *_wizard(...)
PROTOTYPE: DISABLE
PREINIT:
hv_store(MY_CXT.wizz, buf, sprintf(buf, "%u", sig), sv, 0);
++MY_CXT.count;
-
+
RETVAL = newRV_noinc(sv);
OUTPUT:
RETVAL
The perl patchlevel this module was built with, or C<0> for non-debugging perls.
+=head2 C<VMG_THREADSAFE>
+
+True iff this module could have been built with thread-safety features enabled.
+
=head1 FUNCTIONS
=cut
'consts' => [ qw/SIG_MIN SIG_MAX SIG_NBR MGf_COPY MGf_DUP MGf_LOCAL VMG_UVAR/,
qw/VMG_COMPAT_ARRAY_PUSH_NOLEN VMG_COMPAT_ARRAY_UNDEF_CLEAR/,
qw/VMG_COMPAT_SCALAR_LENGTH_NOLEN/,
- qw/VMG_PERL_PATCHLEVEL/ ]
+ qw/VMG_PERL_PATCHLEVEL/,
+ qw/VMG_THREADSAFE/ ]
);
our @EXPORT_OK = map { @$_ } values %EXPORT_TAGS;
$EXPORT_TAGS{'all'} = [ @EXPORT_OK ];
use strict;
use warnings;
-use Test::More tests => 17;
+use Test::More tests => 18;
require Variable::Magic;
-for (qw/wizard gensig getsig cast getdata dispell SIG_MIN SIG_MAX SIG_NBR MGf_COPY MGf_DUP MGf_LOCAL VMG_UVAR VMG_COMPAT_ARRAY_PUSH_NOLEN VMG_COMPAT_ARRAY_UNDEF_CLEAR VMG_COMPAT_SCALAR_LENGTH_NOLEN VMG_PERL_PATCHLEVEL/) {
+for (qw/wizard gensig getsig cast getdata dispell SIG_MIN SIG_MAX SIG_NBR MGf_COPY MGf_DUP MGf_LOCAL VMG_UVAR VMG_COMPAT_ARRAY_PUSH_NOLEN VMG_COMPAT_ARRAY_UNDEF_CLEAR VMG_COMPAT_SCALAR_LENGTH_NOLEN VMG_PERL_PATCHLEVEL VMG_THREADSAFE/) {
eval { Variable::Magic->import($_) };
is($@, '', 'import ' . $_);
}
--- /dev/null
+#!perl -T
+
+use strict;
+use warnings;
+
+use threads; # Before Test::More
+use threads::shared;
+
+use Test::More;
+
+use Variable::Magic qw/wizard cast dispell getdata VMG_THREADSAFE/;
+
+if (VMG_THREADSAFE) {
+ plan tests => 2 * 16 + 1;
+} else {
+ plan skip_all => 'This Variable::Magic isn\'t thread safe';
+}
+
+my $destroyed : shared = 0;
+
+sub try {
+ my $tid = threads->tid();
+ my $c = 0;
+ my $wiz = eval {
+ wizard get => sub { ++$c },
+ data => sub { $_[1] + $tid },
+ free => sub { ++$destroyed };
+ };
+ is($@, '', "wizard in thread $tid doesn't croak");
+ isnt($wiz, undef, "wizard in thread $tid is defined");
+ is($c, 0, "wizard in thread $tid doesn't trigger magic");
+ my $a = 3;
+ my $res = eval { cast $a, $wiz, sub { 5 }->() };
+ is($@, '', "cast in thread $tid doesn't croak");
+ is($c, 0, "cast in thread $tid doesn't trigger magic");
+ my $b;
+ eval { $b = $a };
+ is($@, '', "get in thread $tid doesn't croak");
+ is($b, 3, "get in thread $tid returns the right thing");
+ is($c, 1, "get in thread $tid triggers magic");
+ my $d = eval { getdata $a, $wiz };
+ is($@, '', "getdata in thread $tid doesn't croak");
+ is($d, 5 + $tid, "getdata in thread $tid returns the right thing");
+ is($c, 1, "getdata in thread $tid doesn't trigger magic");
+ $res = eval { dispell $a, $wiz };
+ is($@, '', "dispell in thread $tid doesn't croak");
+ is($c, 1, "dispell in thread $tid doesn't trigger magic");
+ undef $b;
+ eval { $b = $a };
+ is($@, '', "get in thread $tid after dispell doesn't croak");
+ is($b, 3, "get in thread $tid after dispell returns the right thing");
+ is($c, 1, "get in thread $tid after dispell doesn't trigger magic");
+ return;
+}
+
+my @t = map { threads->create(\&try) } 1 .. 2;
+$t[0]->join;
+$t[1]->join;
+
+is($destroyed, 0, 'destructors didn\'t fired');