# define SvREFCNT_inc_simple_void_NN(S) ((void) SvREFCNT_inc(S))
#endif
+#ifdef DEBUGGING
+# define LT_ASSERT(C) assert(C)
+#else
+# define LT_ASSERT(C)
+#endif
+
/* ... Thread safety and multiplicity ...................................... */
/* Safe unless stated otherwise in Makefile.PL */
# define aMY_CXT_
#endif
+#if LT_THREADSAFE
+/* We must use preexistent global mutexes or we will never be able to destroy
+ * them. */
+# if LT_HAS_PERL(5, 9, 3)
+# define LT_LOADED_LOCK MUTEX_LOCK(&PL_my_ctx_mutex)
+# define LT_LOADED_UNLOCK MUTEX_UNLOCK(&PL_my_ctx_mutex)
+# else
+# define LT_LOADED_LOCK OP_REFCNT_LOCK
+# define LT_LOADED_UNLOCK OP_REFCNT_UNLOCK
+# endif
+#else
+# define LT_LOADED_LOCK NOOP
+# define LT_LOADED_UNLOCK NOOP
+#endif
+
#if defined(OP_CHECK_MUTEX_LOCK) && defined(OP_CHECK_MUTEX_UNLOCK)
-# define LT_CHECK_MUTEX_LOCK OP_CHECK_MUTEX_LOCK
-# define LT_CHECK_MUTEX_UNLOCK OP_CHECK_MUTEX_UNLOCK
+# define LT_CHECK_LOCK OP_CHECK_MUTEX_LOCK
+# define LT_CHECK_UNLOCK OP_CHECK_MUTEX_UNLOCK
+#elif LT_HAS_PERL(5, 9, 3)
+# define LT_CHECK_LOCK OP_REFCNT_LOCK
+# define LT_CHECK_UNLOCK OP_REFCNT_UNLOCK
#else
-# define LT_CHECK_MUTEX_LOCK OP_REFCNT_LOCK
-# define LT_CHECK_MUTEX_UNLOCK OP_REFCNT_UNLOCK
+/* Before perl 5.9.3, lt_ck_*() calls are already protected by the LT_LOADED
+ * mutex, which falls back to the OP_REFCNT mutex. Make sure we don't lock it
+ * twice. */
+# define LT_CHECK_LOCK NOOP
+# define LT_CHECK_UNLOCK NOOP
#endif
typedef OP *(*lt_ck_t)(pTHX_ OP *);
static void lt_ck_replace(pTHX_ OPCODE type, lt_ck_t new_ck, lt_ck_t *old_ck_p){
#define lt_ck_replace(T, NC, OCP) lt_ck_replace(aTHX_ (T), (NC), (OCP))
- LT_CHECK_MUTEX_LOCK;
+ LT_CHECK_LOCK;
if (!*old_ck_p) {
*old_ck_p = PL_check[type];
PL_check[type] = new_ck;
}
- LT_CHECK_MUTEX_UNLOCK;
+ LT_CHECK_UNLOCK;
}
#endif
static void lt_ck_restore(pTHX_ OPCODE type, lt_ck_t *old_ck_p) {
#define lt_ck_restore(T, OCP) lt_ck_restore(aTHX_ (T), (OCP))
- LT_CHECK_MUTEX_LOCK;
+ LT_CHECK_LOCK;
if (*old_ck_p) {
PL_check[type] = *old_ck_p;
*old_ck_p = 0;
}
- LT_CHECK_MUTEX_UNLOCK;
+ LT_CHECK_UNLOCK;
}
/* --- Helpers ------------------------------------------------------------- */
+/* ... Check if the module is loaded ....................................... */
+
+static I32 lt_loaded = 0;
+
+#if LT_THREADSAFE
+
+#define PTABLE_NAME ptable_loaded
+#define PTABLE_NEED_DELETE 1
+#define PTABLE_NEED_WALK 0
+
+#include "ptable.h"
+
+#define ptable_loaded_store(T, K, V) ptable_loaded_store(aPTBLMS_ (T), (K), (V))
+#define ptable_loaded_delete(T, K) ptable_loaded_delete(aPTBLMS_ (T), (K))
+#define ptable_loaded_free(T) ptable_loaded_free(aPTBLMS_ (T))
+
+static ptable *lt_loaded_cxts = NULL;
+
+#ifdef DEBUGGING
+
+static int lt_is_loaded(pTHX_ void *cxt) {
+#define lt_is_loaded(C) lt_is_loaded(aTHX_ (C))
+ int res = 0;
+
+ LT_LOADED_LOCK;
+ if (lt_loaded_cxts && ptable_fetch(lt_loaded_cxts, cxt))
+ res = 1;
+ LT_LOADED_UNLOCK;
+
+ return res;
+}
+
+#endif /* DEBUGGING */
+
+static int lt_set_loaded_locked(pTHX_ void *cxt) {
+#define lt_set_loaded_locked(C) lt_set_loaded_locked(aTHX_ (C))
+ int global_setup = 0;
+
+ if (lt_loaded <= 0) {
+ LT_ASSERT(lt_loaded == 0);
+ LT_ASSERT(!lt_loaded_cxts);
+ lt_loaded_cxts = ptable_new();
+ global_setup = 1;
+ }
+ ++lt_loaded;
+ LT_ASSERT(lt_loaded_cxts);
+ ptable_loaded_store(lt_loaded_cxts, cxt, cxt);
+
+ return global_setup;
+}
+
+static int lt_clear_loaded_locked(pTHX_ void *cxt) {
+#define lt_clear_loaded_locked(C) lt_clear_loaded_locked(aTHX_ (C))
+ int global_teardown = 0;
+
+ if (lt_loaded > 1) {
+ LT_ASSERT(lt_loaded_cxts);
+ ptable_loaded_delete(lt_loaded_cxts, cxt);
+ --lt_loaded;
+ } else if (lt_loaded_cxts) {
+ LT_ASSERT(lt_loaded == 1);
+ ptable_loaded_free(lt_loaded_cxts);
+ lt_loaded_cxts = NULL;
+ lt_loaded = 0;
+ global_teardown = 1;
+ }
+
+ return global_teardown;
+}
+
+#else
+
+#define lt_is_loaded(C) (lt_loaded > 0)
+#define lt_set_loaded_locked(C) ((lt_loaded++ <= 0) ? 1 : 0)
+#define lt_clear_loaded_locked(C) ((--lt_loaded <= 0) ? 1 : 0)
+
+#endif
+
/* ... Thread-safe hints ................................................... */
#if LT_WORKAROUND_REQUIRE_PROPAGATION
#define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
typedef struct {
+ peep_t old_peep;
+ ptable *seen; /* It really is a ptable_seen */
#if LT_THREADSAFE
- ptable *tbl; /* It really is a ptable_hints */
+ ptable *tbl; /* It really is a ptable_hints */
tTHX owner;
#endif
- ptable *seen; /* It really is a ptable_seen */
SV *default_meth;
} my_cxt_t;
ptable_hints_store(ud->tbl, ent->key, h2);
}
-static void lt_thread_cleanup(pTHX_ void *ud) {
- dMY_CXT;
-
- ptable_hints_free(MY_CXT.tbl);
- MY_CXT.tbl = NULL;
- ptable_seen_free(MY_CXT.seen);
- MY_CXT.seen = NULL;
- SvREFCNT_dec(MY_CXT.default_meth);
- MY_CXT.default_meth = NULL;
-}
-
-static int lt_endav_free(pTHX_ SV *sv, MAGIC *mg) {
- SAVEDESTRUCTOR_X(lt_thread_cleanup, NULL);
-
- return 0;
-}
-
-static MGVTBL lt_endav_vtbl = {
- 0,
- 0,
- 0,
- 0,
- lt_endav_free
-#if MGf_COPY
- , 0
-#endif
-#if MGf_DUP
- , 0
-#endif
-#if MGf_LOCAL
- , 0
-#endif
-};
-
#endif /* LT_THREADSAFE */
/* ... Hint tags ........................................................... */
return LT_HINT_CODE(h);
}
-static U32 lt_hash = 0;
+static VOL U32 lt_hash = 0;
static SV *lt_hint(pTHX) {
#define lt_hint() lt_hint(aTHX)
#endif
-static peep_t lt_old_peep = 0; /* This is actually the rpeep past 5.13.5 */
-
static void lt_peep_rec(pTHX_ OP *o, ptable *seen) {
#define lt_peep_rec(O) lt_peep_rec(aTHX_ (O), seen)
for (; o; o = o->op_next) {
}
static void lt_peep(pTHX_ OP *o) {
+ ptable *seen;
dMY_CXT;
- ptable *seen = MY_CXT.seen;
- lt_old_peep(aTHX_ o);
+ LT_ASSERT(lt_is_loaded(&MY_CXT));
+
+ MY_CXT.old_peep(aTHX_ o);
+ seen = MY_CXT.seen;
if (seen) {
ptable_seen_clear(seen);
lt_peep_rec(o);
}
}
-/* --- Interpreter setup/teardown ------------------------------------------ */
-
-static U32 lt_initialized = 0;
+/* --- Module setup/teardown ----------------------------------------------- */
static void lt_teardown(pTHX_ void *root) {
- if (!lt_initialized)
- return;
+ dMY_CXT;
-#if LT_MULTIPLICITY
- if (aTHX != root)
- return;
+ LT_LOADED_LOCK;
+
+ if (lt_clear_loaded_locked(&MY_CXT)) {
+ lt_ck_restore(OP_PADANY, <_old_ck_padany);
+ lt_ck_restore(OP_PADSV, <_old_ck_padsv);
+
+ ptable_map_free(lt_op_padxv_map);
+ lt_op_padxv_map = NULL;
+
+#if LT_HAS_PERL(5, 17, 6)
+ ptable_map_free(lt_op_padrange_map);
+ lt_op_padrange_map = NULL;
#endif
- {
- dMY_CXT;
-#if LT_THREADSAFE
- ptable_hints_free(MY_CXT.tbl);
- MY_CXT.tbl = NULL;
+#ifdef USE_ITHREADS
+ MUTEX_DESTROY(<_op_map_mutex);
#endif
- ptable_seen_free(MY_CXT.seen);
- MY_CXT.seen = NULL;
- SvREFCNT_dec(MY_CXT.default_meth);
- MY_CXT.default_meth = NULL;
}
- lt_ck_restore(OP_PADANY, <_old_ck_padany);
- lt_ck_restore(OP_PADSV, <_old_ck_padsv);
+ LT_LOADED_UNLOCK;
+ if (MY_CXT.old_peep) {
#if LT_HAS_RPEEP
- PL_rpeepp = lt_old_peep;
+ PL_rpeepp = MY_CXT.old_peep;
#else
- PL_peepp = lt_old_peep;
+ PL_peepp = MY_CXT.old_peep;
+#endif
+ MY_CXT.old_peep = 0;
+ }
+
+ ptable_seen_free(MY_CXT.seen);
+ MY_CXT.seen = NULL;
+
+#if LT_THREADSAFE
+ ptable_hints_free(MY_CXT.tbl);
+ MY_CXT.tbl = NULL;
#endif
- lt_old_peep = 0;
- lt_initialized = 0;
+ SvREFCNT_dec(MY_CXT.default_meth);
+ MY_CXT.default_meth = NULL;
+
+ return;
}
static void lt_setup(pTHX) {
#define lt_setup() lt_setup(aTHX)
- if (lt_initialized)
- return;
+ MY_CXT_INIT; /* Takes/release PL_my_ctx_mutex */
- {
- MY_CXT_INIT;
-#if LT_THREADSAFE
- MY_CXT.tbl = ptable_new();
- MY_CXT.owner = aTHX;
+ LT_LOADED_LOCK;
+
+ if (lt_set_loaded_locked(&MY_CXT)) {
+ PERL_HASH(lt_hash, __PACKAGE__, __PACKAGE_LEN__);
+
+ lt_op_padxv_map = ptable_new();
+#if LT_HAS_PERL(5, 17, 6)
+ lt_op_padrange_map = ptable_new();
#endif
- MY_CXT.seen = ptable_new();
- MY_CXT.default_meth = newSVpvn("TYPEDSCALAR", 11);
- SvREADONLY_on(MY_CXT.default_meth);
+
+#ifdef USE_ITHREADS
+ MUTEX_INIT(<_op_map_mutex);
+#endif
+
+ lt_ck_replace(OP_PADANY, lt_ck_padany, <_old_ck_padany);
+ lt_ck_replace(OP_PADSV, lt_ck_padsv, <_old_ck_padsv);
}
- lt_ck_replace(OP_PADANY, lt_ck_padany, <_old_ck_padany);
- lt_ck_replace(OP_PADSV, lt_ck_padsv, <_old_ck_padsv);
+ LT_LOADED_UNLOCK;
+
+ {
+ HV *stash;
+
+ stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
+ newCONSTSUB(stash, "LT_THREADSAFE", newSVuv(LT_THREADSAFE));
+ newCONSTSUB(stash, "LT_FORKSAFE", newSVuv(LT_FORKSAFE));
+ }
#if LT_HAS_RPEEP
- lt_old_peep = PL_rpeepp;
- PL_rpeepp = lt_peep;
+ if (PL_rpeepp != lt_peep) {
+ MY_CXT.old_peep = PL_rpeepp;
+ PL_rpeepp = lt_peep;
+ }
#else
- lt_old_peep = PL_peepp;
- PL_peepp = lt_peep;
+ if (PL_peepp != lt_peep) {
+ MY_CXT.old_peep = PL_peepp;
+ PL_peepp = lt_peep;
+ }
#endif
+ else {
+ MY_CXT.old_peep = 0;
+ }
-#if LT_MULTIPLICITY
- call_atexit(lt_teardown, aTHX);
-#else
- call_atexit(lt_teardown, NULL);
+ MY_CXT.seen = ptable_new();
+
+#if LT_THREADSAFE
+ MY_CXT.tbl = ptable_new();
+ MY_CXT.owner = aTHX;
#endif
- lt_initialized = 1;
-}
+ MY_CXT.default_meth = newSVpvn("TYPEDSCALAR", 11);
+ SvREADONLY_on(MY_CXT.default_meth);
-static U32 lt_booted = 0;
+ call_atexit(lt_teardown, NULL);
+
+ return;
+}
/* --- XS ------------------------------------------------------------------ */
BOOT:
{
- if (!lt_booted++) {
- HV *stash;
-
- lt_op_padxv_map = ptable_new();
-#if LT_HAS_PERL(5, 17, 6)
- lt_op_padrange_map = ptable_new();
-#endif
-#ifdef USE_ITHREADS
- MUTEX_INIT(<_op_map_mutex);
-#endif
-
- PERL_HASH(lt_hash, __PACKAGE__, __PACKAGE_LEN__);
-
- stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
- newCONSTSUB(stash, "LT_THREADSAFE", newSVuv(LT_THREADSAFE));
- newCONSTSUB(stash, "LT_FORKSAFE", newSVuv(LT_FORKSAFE));
- }
-
lt_setup();
}
ptable *t;
ptable *s;
SV *cloned_default_meth;
- GV *gv;
PPCODE:
{
{
}
{
MY_CXT_CLONE;
+ MY_CXT.seen = s;
MY_CXT.tbl = t;
MY_CXT.owner = aTHX;
- MY_CXT.seen = s;
MY_CXT.default_meth = cloned_default_meth;
+ {
+ int global_setup;
+ LT_LOADED_LOCK;
+ global_setup = lt_set_loaded_locked(&MY_CXT);
+ LT_ASSERT(!global_setup);
+ LT_LOADED_UNLOCK;
+ }
}
- gv = gv_fetchpv(__PACKAGE__ "::_THREAD_CLEANUP", 0, SVt_PVCV);
- if (gv) {
- CV *cv = GvCV(gv);
- if (!PL_endav)
- PL_endav = newAV();
- SvREFCNT_inc(cv);
- if (!av_store(PL_endav, av_len(PL_endav) + 1, (SV *) cv))
- SvREFCNT_dec(cv);
- sv_magicext((SV *) PL_endav, NULL, PERL_MAGIC_ext, <_endav_vtbl, NULL, 0);
- }
- XSRETURN(0);
-
-void
-_THREAD_CLEANUP(...)
-PROTOTYPE: DISABLE
-PPCODE:
- lt_thread_cleanup(aTHX_ NULL);
XSRETURN(0);
#endif
--- /dev/null
+#!perl
+
+use strict;
+use warnings;
+
+my ($module, $thread_safe_var);
+BEGIN {
+ $module = 'Lexical::Types';
+ $thread_safe_var = 'Lexical::Types::LT_THREADSAFE()';
+}
+
+sub Int::TYPEDSCALAR { 123 }
+
+sub load_test {
+ my $res;
+ if (defined &Lexical::Types::import) {
+ local $@;
+ $res = eval 'BEGIN { Lexical::Types->import } my Int $x';
+ $res = $@ if $@;
+ }
+ if (not defined $res) {
+ return 0;
+ } elsif (not ref $res and $res == 123) {
+ return 1;
+ } else {
+ return "$res";
+ }
+}
+
+# Keep the rest of the file untouched
+
+use lib 't/lib';
+use VPIT::TestHelpers threads => [ $module, $thread_safe_var ];
+
+my $could_not_create_thread = 'Could not create thread';
+
+use Test::Leaner;
+
+sub is_loaded {
+ my ($affirmative, $desc) = @_;
+
+ my $res = load_test();
+
+ my $expected;
+ if ($affirmative) {
+ $expected = 1;
+ $desc = "$desc: module loaded";
+ } else {
+ $expected = 0;
+ $desc = "$desc: module not loaded";
+ }
+
+ unless (is $res, $expected, $desc) {
+ $res = defined $res ? "'$res'" : 'undef';
+ $expected = "'$expected'";
+ diag("Test '$desc' failed: got $res, expected $expected");
+ }
+
+ return;
+}
+
+BEGIN {
+ local $@;
+ my $code = eval "sub { require $module }";
+ die $@ if $@;
+ *do_load = $code;
+}
+
+is_loaded 0, 'main body, beginning';
+
+# Test serial loadings
+
+SKIP: {
+ my $thr = spawn(sub {
+ my $here = "first serial thread";
+ is_loaded 0, "$here, beginning";
+
+ do_load;
+ is_loaded 1, "$here, after loading";
+
+ return;
+ });
+
+ skip "$could_not_create_thread (serial 1)" => 2 unless defined $thr;
+
+ $thr->join;
+ if (my $err = $thr->error) {
+ die $err;
+ }
+}
+
+is_loaded 0, 'main body, in between serial loadings';
+
+SKIP: {
+ my $thr = spawn(sub {
+ my $here = "second serial thread";
+ is_loaded 0, "$here, beginning";
+
+ do_load;
+ is_loaded 1, "$here, after loading";
+
+ return;
+ });
+
+ skip "$could_not_create_thread (serial 2)" => 2 unless defined $thr;
+
+ $thr->join;
+ if (my $err = $thr->error) {
+ die $err;
+ }
+}
+
+is_loaded 0, 'main body, after serial loadings';
+
+# Test nested loadings
+
+SKIP: {
+ my $parent = spawn(sub {
+ my $here = 'parent thread';
+ is_loaded 0, "$here, beginning";
+
+ SKIP: {
+ my $kid = spawn(sub {
+ my $here = 'child thread';
+ is_loaded 0, "$here, beginning";
+
+ do_load;
+ is_loaded 1, "$here, after loading";
+
+ return;
+ });
+
+ skip "$could_not_create_thread (nested child)" => 2 unless defined $kid;
+
+ $kid->join;
+ if (my $err = $kid->error) {
+ die "in child thread: $err\n";
+ }
+ }
+
+ is_loaded 0, "$here, after child terminated";
+
+ do_load;
+ is_loaded 1, "$here, after loading";
+
+ return;
+ });
+
+ skip "$could_not_create_thread (nested parent)" => (3 + 2)
+ unless defined $parent;
+
+ $parent->join;
+ if (my $err = $parent->error) {
+ die $err;
+ }
+}
+
+is_loaded 0, 'main body, after nested loadings';
+
+# Test parallel loadings
+
+use threads;
+use threads::shared;
+
+my $sync_points = 7;
+
+my @locks_down = (1) x $sync_points;
+my @locks_up = (0) x $sync_points;
+share($_) for @locks_down, @locks_up;
+
+my $default_peers = 2;
+
+sub sync_master {
+ my ($id, $peers) = @_;
+
+ $peers = $default_peers unless defined $peers;
+
+ {
+ lock $locks_down[$id];
+ $locks_down[$id] = 0;
+ cond_broadcast $locks_down[$id];
+ }
+
+ LOCK: {
+ lock $locks_up[$id];
+ my $timeout = time() + 10;
+ until ($locks_up[$id] == $peers) {
+ if (cond_timedwait $locks_up[$id], $timeout) {
+ last LOCK;
+ } else {
+ return 0;
+ }
+ }
+ }
+
+ return 1;
+}
+
+sub sync_slave {
+ my ($id) = @_;
+
+ {
+ lock $locks_down[$id];
+ cond_wait $locks_down[$id] until $locks_down[$id] == 0;
+ }
+
+ {
+ lock $locks_up[$id];
+ $locks_up[$id]++;
+ cond_signal $locks_up[$id];
+ }
+
+ return 1;
+}
+
+for my $first_thread_ends_first (0, 1) {
+ for my $id (0 .. $sync_points - 1) {
+ {
+ lock $locks_down[$id];
+ $locks_down[$id] = 1;
+ }
+ {
+ lock $locks_up[$id];
+ $locks_up[$id] = 0;
+ }
+ }
+
+ my $thr1_end = 'finishes first';
+ my $thr2_end = 'finishes last';
+
+ ($thr1_end, $thr2_end) = ($thr2_end, $thr1_end)
+ unless $first_thread_ends_first;
+
+ SKIP: {
+ my $thr1 = spawn(sub {
+ my $here = "first simultaneous thread ($thr1_end)";
+ sync_slave 0;
+
+ is_loaded 0, "$here, beginning";
+ sync_slave 1;
+
+ do_load;
+ is_loaded 1, "$here, after loading";
+ sync_slave 2;
+ sync_slave 3;
+
+ sync_slave 4;
+ is_loaded 1, "$here, still loaded while also loaded in the other thread";
+ sync_slave 5;
+
+ sync_slave 6 unless $first_thread_ends_first;
+
+ is_loaded 1, "$here, end";
+
+ return 1;
+ });
+
+ skip "$could_not_create_thread (parallel 1)" => (4 * 2) unless defined $thr1;
+
+ my $thr2 = spawn(sub {
+ my $here = "second simultaneous thread ($thr2_end)";
+ sync_slave 0;
+
+ is_loaded 0, "$here, beginning";
+ sync_slave 1;
+
+ sync_slave 2;
+ sync_slave 3;
+ is_loaded 0, "$here, loaded in other thread but not here";
+
+ do_load;
+ is_loaded 1, "$here, after loading";
+ sync_slave 4;
+ sync_slave 5;
+
+ sync_slave 6 if $first_thread_ends_first;
+
+ is_loaded 1, "$here, end";
+
+ return 1;
+ });
+
+ sync_master($_) for 0 .. 5;
+
+ if (defined $thr2) {
+ ($thr2, $thr1) = ($thr1, $thr2) unless $first_thread_ends_first;
+
+ $thr1->join;
+ if (my $err = $thr1->error) {
+ die $err;
+ }
+
+ sync_master(6, 1);
+
+ $thr2->join;
+ if (my $err = $thr1->error) {
+ die $err;
+ }
+ } else {
+ sync_master(6, 1) unless $first_thread_ends_first;
+
+ $thr1->join;
+ if (my $err = $thr1->error) {
+ die $err;
+ }
+
+ skip "$could_not_create_thread (parallel 2)" => (4 * 1);
+ }
+ }
+
+ is_loaded 0, 'main body, after simultaneous threads';
+}
+
+# Test simple clone
+
+SKIP: {
+ my $parent = spawn(sub {
+ my $here = 'simple clone, parent thread';
+ is_loaded 0, "$here, beginning";
+
+ do_load;
+ is_loaded 1, "$here, after loading";
+
+ SKIP: {
+ my $kid = spawn(sub {
+ my $here = 'simple clone, child thread';
+
+ is_loaded 1, "$here, beginning";
+
+ return;
+ });
+
+ skip "$could_not_create_thread (simple clone child)" => 1
+ unless defined $kid;
+
+ $kid->join;
+ if (my $err = $kid->error) {
+ die "in child thread: $err\n";
+ }
+ }
+
+ is_loaded 1, "$here, after child terminated";
+
+ return;
+ });
+
+ skip "$could_not_create_thread (simple clone parent)" => (3 + 1)
+ unless defined $parent;
+
+ $parent->join;
+ if (my $err = $parent->error) {
+ die $err;
+ }
+}
+
+is_loaded 0, 'main body, after simple clone';
+
+# Test clone outliving its parent
+
+SKIP: {
+ my $kid_done;
+ share($kid_done);
+
+ my $parent = spawn(sub {
+ my $here = 'outliving clone, parent thread';
+ is_loaded 0, "$here, beginning";
+
+ do_load;
+ is_loaded 1, "$here, after loading";
+
+ my $kid_tid;
+
+ SKIP: {
+ my $kid = spawn(sub {
+ my $here = 'outliving clone, child thread';
+
+ is_loaded 1, "$here, beginning";
+
+ {
+ lock $kid_done;
+ cond_wait $kid_done until $kid_done;
+ }
+
+ is_loaded 1, "$here, end";
+
+ return 1;
+ });
+
+ if (defined $kid) {
+ $kid_tid = $kid->tid;
+ } else {
+ $kid_tid = 0;
+ skip "$could_not_create_thread (outliving clone child)" => 2;
+ }
+ }
+
+ is_loaded 1, "$here, end";
+
+ return $kid_tid;
+ });
+
+ skip "$could_not_create_thread (outliving clone parent)" => (3 + 2)
+ unless defined $parent;
+
+ my $kid_tid = $parent->join;
+ if (my $err = $parent->error) {
+ die $err;
+ }
+
+ if ($kid_tid) {
+ my $kid = threads->object($kid_tid);
+ if (defined $kid) {
+ if ($kid->is_running) {
+ lock $kid_done;
+ $kid_done = 1;
+ cond_signal $kid_done;
+ }
+
+ $kid->join;
+ }
+ }
+}
+
+is_loaded 0, 'main body, after outliving clone';
+
+do_load;
+is_loaded 1, 'main body, loaded at end';
+
+done_testing();