X-Git-Url: http://git.vpit.fr/?p=perl%2Fmodules%2FLexical-Types.git;a=blobdiff_plain;f=Types.xs;h=375d771176f12c374f8b84b8fd09a94756d17b0b;hp=338201462e072b96920c9450c3c68305c44ad392;hb=c530c61ff04bdbd8bc101a3e88273fc4219d963e;hpb=802bedc36b8f65e33ea579641fe0dc70b4e59e9d diff --git a/Types.xs b/Types.xs index 3382014..375d771 100644 --- a/Types.xs +++ b/Types.xs @@ -23,6 +23,10 @@ # endif #endif +#ifndef LT_WORKAROUND_REQUIRE_PROPAGATION +# define LT_WORKAROUND_REQUIRE_PROPAGATION !LT_HAS_PERL(5, 10, 1) +#endif + #ifndef HvNAME_get # define HvNAME_get(H) HvNAME(H) #endif @@ -31,8 +35,8 @@ # define HvNAMELEN_get(H) strlen(HvNAME_get(H)) #endif -#ifndef SvIS_FREED -# define SvIS_FREED(sv) ((sv)->sv_flags == SVTYPEMASK) +#ifndef SvREFCNT_inc_simple_NN +# define SvREFCNT_inc_simple_NN SvREFCNT_inc #endif /* ... Thread safety and multiplicity ...................................... */ @@ -59,16 +63,63 @@ # endif #else # define LT_THREADSAFE 0 +# undef dMY_CXT +# define dMY_CXT dNOOP +# undef MY_CXT +# define MY_CXT lt_globaldata +# undef START_MY_CXT +# define START_MY_CXT STATIC my_cxt_t MY_CXT; +# undef MY_CXT_INIT +# define MY_CXT_INIT NOOP +# undef MY_CXT_CLONE +# define MY_CXT_CLONE NOOP +# undef pMY_CXT +# define pMY_CXT +# undef pMY_CXT_ +# define pMY_CXT_ +# undef aMY_CXT +# define aMY_CXT +# undef aMY_CXT_ +# define aMY_CXT_ #endif /* --- Helpers ------------------------------------------------------------- */ /* ... Thread-safe hints ................................................... */ +#if LT_WORKAROUND_REQUIRE_PROPAGATION + +typedef struct { + SV *code; + IV require_tag; +} lt_hint_t; + +#define LT_HINT_STRUCT 1 + +#define LT_HINT_CODE(H) ((H)->code) + +#define LT_HINT_FREE(H) { \ + lt_hint_t *h = (H); \ + SvREFCNT_dec(h->code); \ + PerlMemShared_free(h); \ +} + +#else /* LT_WORKAROUND_REQUIRE_PROPAGATION */ + +typedef SV lt_hint_t; + +#define LT_HINT_STRUCT 0 + +#define LT_HINT_CODE(H) (H) + +#define LT_HINT_FREE(H) SvREFCNT_dec(H); + +#endif /* !LT_WORKAROUND_REQUIRE_PROPAGATION */ + #if LT_THREADSAFE #define PTABLE_NAME ptable_hints -#define PTABLE_VAL_FREE(V) if ((V) && !SvIS_FREED((SV *) (V))) SvREFCNT_dec(V) +#define PTABLE_VAL_FREE(V) LT_HINT_FREE(V) #define pPTBL pTHX #define pPTBL_ pTHX_ @@ -80,122 +131,207 @@ #define ptable_hints_store(T, K, V) ptable_hints_store(aTHX_ (T), (K), (V)) #define ptable_hints_free(T) ptable_hints_free(aTHX_ (T)) +#endif /* LT_THREADSAFE */ + +/* ... Global data ......................................................... */ + #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION typedef struct { - ptable *tbl; +#if LT_THREADSAFE + ptable *tbl; /* It really is a ptable_hints */ tTHX owner; +#endif + SV *default_meth; + OP * (*pp_padsv_saved)(pTHX); } my_cxt_t; START_MY_CXT -STATIC void lt_ptable_hints_clone(pTHX_ ptable_ent *ent, void *ud_) { - my_cxt_t *ud = ud_; - SV *val = ent->val; - - if (ud->owner != aTHX) { - CLONE_PARAMS param; - AV *stashes = (SvTYPE(val) == SVt_PVHV && HvNAME_get(val)) ? newAV() : NULL; - param.stashes = stashes; - param.flags = 0; - param.proto_perl = ud->owner; - val = sv_dup(val, ¶m); - if (stashes) { - av_undef(stashes); - SvREFCNT_dec(stashes); - } +/* ... Cloning global data ................................................. */ + +#if LT_THREADSAFE + +STATIC SV *lt_clone(pTHX_ SV *sv, tTHX owner) { +#define lt_clone(S, O) lt_clone(aTHX_ (S), (O)) + CLONE_PARAMS param; + AV *stashes = NULL; + SV *dupsv; + + if (SvTYPE(sv) == SVt_PVHV && HvNAME_get(sv)) + stashes = newAV(); + + param.stashes = stashes; + param.flags = 0; + param.proto_perl = owner; + + dupsv = sv_dup(sv, ¶m); + + if (stashes) { + av_undef(stashes); + SvREFCNT_dec(stashes); } - ptable_hints_store(ud->tbl, ent->key, val); - SvREFCNT_inc(val); + return SvREFCNT_inc(dupsv); } -STATIC void lt_thread_cleanup(pTHX_ void *); +STATIC void lt_ptable_hints_clone(pTHX_ ptable_ent *ent, void *ud_) { + my_cxt_t *ud = ud_; + lt_hint_t *h1 = ent->val; + lt_hint_t *h2; -STATIC void lt_thread_cleanup(pTHX_ void *ud) { - int *level = ud; - SV *id; + if (ud->owner == aTHX) + return; - if (*level) { - *level = 0; - LEAVE; - SAVEDESTRUCTOR_X(lt_thread_cleanup, level); - ENTER; - } else { - dMY_CXT; - PerlMemShared_free(level); - ptable_hints_free(MY_CXT.tbl); - } +#if LT_HINT_STRUCT + + h2 = PerlMemShared_malloc(sizeof *h2); + h2->code = lt_clone(h1->code, ud->owner); + SvREFCNT_inc(h2->code); +#if LT_WORKAROUND_REQUIRE_PROPAGATION + h2->require_tag = PTR2IV(lt_clone(INT2PTR(SV *, h1->require_tag), ud->owner)); +#endif + +#else /* LT_HINT_STRUCT */ + + h2 = lt_clone(h1, ud->owner); + SvREFCNT_inc(h2); + +#endif /* !LT_HINT_STRUCT */ + + ptable_hints_store(ud->tbl, ent->key, h2); } -STATIC SV *lt_tag(pTHX_ SV *value) { -#define lt_tag(V) lt_tag(aTHX_ (V)) - dMY_CXT; +#include "reap.h" - value = SvOK(value) && SvROK(value) ? SvRV(value) : NULL; - /* We only need for the key to be an unique tag for looking up the value later. - * Allocated memory provides convenient unique identifiers, so that's why we - * use the value pointer as the key itself. */ - ptable_hints_store(MY_CXT.tbl, value, value); - SvREFCNT_inc(value); +STATIC void lt_thread_cleanup(pTHX_ void *ud) { + dMY_CXT; - return newSVuv(PTR2UV(value)); + ptable_hints_free(MY_CXT.tbl); } -STATIC SV *lt_detag(pTHX_ const SV *hint) { -#define lt_detag(H) lt_detag(aTHX_ (H)) - void *tag; - SV *value; +#endif /* LT_THREADSAFE */ - if (!hint || !SvOK(hint) || !SvIOK(hint)) - croak("Wrong hint"); +/* ... Hint tags ........................................................... */ + +#if LT_WORKAROUND_REQUIRE_PROPAGATION +STATIC IV lt_require_tag(pTHX) { +#define lt_require_tag() lt_require_tag(aTHX) + const CV *cv, *outside; + + cv = PL_compcv; + + if (!cv) { + /* If for some reason the pragma is operational at run-time, try to discover + * the current cv in use. */ + const PERL_SI *si; + + for (si = PL_curstackinfo; si; si = si->si_prev) { + I32 cxix; + + for (cxix = si->si_cxix; cxix >= 0; --cxix) { + const PERL_CONTEXT *cx = si->si_cxstack + cxix; + + switch (CxTYPE(cx)) { + case CXt_SUB: + case CXt_FORMAT: + /* The propagation workaround is only needed up to 5.10.0 and at that + * time format and sub contexts were still identical. And even later the + * cv members offsets should have been kept the same. */ + cv = cx->blk_sub.cv; + goto get_enclosing_cv; + case CXt_EVAL: + cv = cx->blk_eval.cv; + goto get_enclosing_cv; + default: + break; + } + } + } - tag = INT2PTR(void *, SvIVX(hint)); - { - dMY_CXT; - value = ptable_fetch(MY_CXT.tbl, tag); + cv = PL_main_cv; } - return value; -} +get_enclosing_cv: + for (outside = CvOUTSIDE(cv); outside; outside = CvOUTSIDE(cv)) + cv = outside; -#else + return PTR2IV(cv); +} +#endif /* LT_WORKAROUND_REQUIRE_PROPAGATION */ STATIC SV *lt_tag(pTHX_ SV *value) { #define lt_tag(V) lt_tag(aTHX_ (V)) - UV tag = 0; + lt_hint_t *h; + SV *code = NULL; + dMY_CXT; - if (SvOK(value) && SvROK(value)) { + if (SvROK(value)) { value = SvRV(value); - SvREFCNT_inc(value); - tag = PTR2UV(value); + if (SvTYPE(value) >= SVt_PVCV) { + code = value; + SvREFCNT_inc_simple_NN(code); + } } - return newSVuv(tag); +#if LT_HINT_STRUCT + h = PerlMemShared_malloc(sizeof *h); + h->code = code; +# if LT_WORKAROUND_REQUIRE_PROPAGATION + h->require_tag = lt_require_tag(); +# endif /* LT_WORKAROUND_REQUIRE_PROPAGATION */ +#else /* LT_HINT_STRUCT */ + h = code; +#endif /* !LT_HINT_STRUCT */ + +#if LT_THREADSAFE + /* We only need for the key to be an unique tag for looking up the value later. + * Allocated memory provides convenient unique identifiers, so that's why we + * use the hint as the key itself. */ + ptable_hints_store(MY_CXT.tbl, h, h); +#endif /* LT_THREADSAFE */ + + return newSViv(PTR2IV(h)); } -#define lt_detag(H) INT2PTR(SV *, SvUVX(H)) +STATIC SV *lt_detag(pTHX_ const SV *hint) { +#define lt_detag(H) lt_detag(aTHX_ (H)) + lt_hint_t *h; + dMY_CXT; + + if (!(hint && SvIOK(hint))) + return NULL; + h = INT2PTR(lt_hint_t *, SvIVX(hint)); +#if LT_THREADSAFE + h = ptable_fetch(MY_CXT.tbl, h); #endif /* LT_THREADSAFE */ +#if LT_WORKAROUND_REQUIRE_PROPAGATION + if (lt_require_tag() != h->require_tag) + return NULL; +#endif /* LT_WORKAROUND_REQUIRE_PROPAGATION */ + + return LT_HINT_CODE(h); +} STATIC U32 lt_hash = 0; STATIC SV *lt_hint(pTHX) { #define lt_hint() lt_hint(aTHX) - SV *id; -#if LT_HAS_PERL(5, 10, 0) - id = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash, - NULL, - __PACKAGE__, __PACKAGE_LEN__, - 0, - lt_hash); + SV *hint; +#if LT_HAS_PERL(5, 9, 5) + hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash, + NULL, + __PACKAGE__, __PACKAGE_LEN__, + 0, + lt_hash); #else SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__, lt_hash); if (!val) return 0; - id = *val; + hint = *val; #endif - return (id && SvOK(id)) ? id : NULL; + return lt_detag(hint); } /* ... op => info map ...................................................... */ @@ -215,14 +351,19 @@ STATIC perl_mutex lt_op_map_mutex; #endif typedef struct { +#ifdef MULTIPLICITY + STRLEN buf_size, orig_pkg_len, type_pkg_len, type_meth_len; + char *buf; +#else /* MULTIPLICITY */ SV *orig_pkg; SV *type_pkg; SV *type_meth; - OP *(*pp_padsv)(pTHX); +#endif /* !MULTIPLICITY */ + OP *(*old_pp_padsv)(pTHX); } lt_op_info; -STATIC void lt_map_store(pPTBLMS_ const OP *o, SV *orig_pkg, SV *type_pkg, SV *type_meth, OP *(*pp_padsv)(pTHX)) { -#define lt_map_store(O, OP, TP, TM, PP) lt_map_store(aPTBLMS_ (O), (OP), (TP), (TM), (PP)) +STATIC void lt_map_store(pTHX_ const OP *o, SV *orig_pkg, SV *type_pkg, SV *type_meth, OP *(*old_pp_padsv)(pTHX)) { +#define lt_map_store(O, OP, TP, TM, PP) lt_map_store(aTHX_ (O), (OP), (TP), (TM), (PP)) lt_op_info *oi; #ifdef USE_ITHREADS @@ -232,12 +373,49 @@ STATIC void lt_map_store(pPTBLMS_ const OP *o, SV *orig_pkg, SV *type_pkg, SV *t if (!(oi = ptable_fetch(lt_op_map, o))) { oi = PerlMemShared_malloc(sizeof *oi); ptable_map_store(lt_op_map, o, oi); +#ifdef MULTIPLICITY + oi->buf = NULL; + oi->buf_size = 0; +#else /* MULTIPLICITY */ + } else { + SvREFCNT_dec(oi->orig_pkg); + SvREFCNT_dec(oi->type_pkg); + SvREFCNT_dec(oi->type_meth); +#endif /* !MULTIPLICITY */ } +#ifdef MULTIPLICITY + { + STRLEN op_len = SvCUR(orig_pkg); + STRLEN tp_len = SvCUR(type_pkg); + STRLEN tm_len = SvCUR(type_meth); + STRLEN new_buf_size = op_len + tp_len + tm_len; + char *buf; + if (new_buf_size > oi->buf_size) { + PerlMemShared_free(oi->buf); + oi->buf = PerlMemShared_malloc(new_buf_size); + oi->buf_size = new_buf_size; + } + buf = oi->buf; + Copy(SvPVX(orig_pkg), buf, op_len, char); + buf += op_len; + Copy(SvPVX(type_pkg), buf, tp_len, char); + buf += tp_len; + Copy(SvPVX(type_meth), buf, tm_len, char); + oi->orig_pkg_len = op_len; + oi->type_pkg_len = tp_len; + oi->type_meth_len = tm_len; + SvREFCNT_dec(orig_pkg); + SvREFCNT_dec(type_pkg); + SvREFCNT_dec(type_meth); + } +#else /* MULTIPLICITY */ oi->orig_pkg = orig_pkg; oi->type_pkg = type_pkg; oi->type_meth = type_meth; - oi->pp_padsv = pp_padsv; +#endif /* !MULTIPLICITY */ + + oi->old_pp_padsv = old_pp_padsv; #ifdef USE_ITHREADS MUTEX_UNLOCK(<_op_map_mutex); @@ -264,6 +442,19 @@ STATIC const lt_op_info *lt_map_fetch(const OP *o, lt_op_info *oi) { return val; } +STATIC void lt_map_delete(pTHX_ const OP *o) { +#define lt_map_delete(O) lt_map_delete(aTHX_ (O)) +#ifdef USE_ITHREADS + MUTEX_LOCK(<_op_map_mutex); +#endif + + ptable_map_store(lt_op_map, o, NULL); + +#ifdef USE_ITHREADS + MUTEX_UNLOCK(<_op_map_mutex); +#endif +} + /* --- Hooks --------------------------------------------------------------- */ /* ... Our pp_padsv ........................................................ */ @@ -276,20 +467,40 @@ STATIC OP *lt_pp_padsv(pTHX) { SV *sv = PAD_SVl(targ); if (sv) { + SV *orig_pkg, *type_pkg, *type_meth; int items; dSP; ENTER; SAVETMPS; +#ifdef MULTIPLICITY + { + STRLEN op_len = oi.orig_pkg_len, tp_len = oi.type_pkg_len; + char *buf = oi.buf; + orig_pkg = sv_2mortal(newSVpvn(buf, op_len)); + SvREADONLY_on(orig_pkg); + buf += op_len; + type_pkg = sv_2mortal(newSVpvn(buf, tp_len)); + SvREADONLY_on(type_pkg); + buf += tp_len; + type_meth = sv_2mortal(newSVpvn(buf, oi.type_meth_len)); + SvREADONLY_on(type_meth); + } +#else /* MULTIPLICITY */ + orig_pkg = oi.orig_pkg; + type_pkg = oi.type_pkg; + type_meth = oi.type_meth; +#endif /* !MULTIPLICITY */ + PUSHMARK(SP); EXTEND(SP, 3); - PUSHs(oi.type_pkg); + PUSHs(type_pkg); PUSHs(sv); - PUSHs(oi.orig_pkg); + PUSHs(orig_pkg); PUTBACK; - items = call_sv(oi.type_meth, G_ARRAY | G_METHOD); + items = call_sv(type_meth, G_ARRAY | G_METHOD); SPAGAIN; switch (items) { @@ -307,31 +518,33 @@ STATIC OP *lt_pp_padsv(pTHX) { LEAVE; } - return CALL_FPTR(oi.pp_padsv)(aTHX); + return CALL_FPTR(oi.old_pp_padsv)(aTHX); } return CALL_FPTR(PL_ppaddr[OP_PADSV])(aTHX); } -STATIC OP *(*lt_pp_padsv_saved)(pTHX) = 0; - -STATIC void lt_pp_padsv_save(void) { - if (lt_pp_padsv_saved) +STATIC void lt_pp_padsv_save(pMY_CXT) { +#define lt_pp_padsv_save() lt_pp_padsv_save(aMY_CXT) + if (MY_CXT.pp_padsv_saved) return; - lt_pp_padsv_saved = PL_ppaddr[OP_PADSV]; - PL_ppaddr[OP_PADSV] = lt_pp_padsv; + MY_CXT.pp_padsv_saved = PL_ppaddr[OP_PADSV]; + PL_ppaddr[OP_PADSV] = lt_pp_padsv; } -STATIC void lt_pp_padsv_restore(OP *o) { - if (!lt_pp_padsv_saved) +STATIC void lt_pp_padsv_restore(pMY_CXT_ OP *o) { +#define lt_pp_padsv_restore(O) lt_pp_padsv_restore(aMY_CXT_ (O)) + OP *(*saved)(pTHX) = MY_CXT.pp_padsv_saved; + + if (!saved) return; if (o->op_ppaddr == lt_pp_padsv) - o->op_ppaddr = lt_pp_padsv_saved; + o->op_ppaddr = saved; - PL_ppaddr[OP_PADSV] = lt_pp_padsv_saved; - lt_pp_padsv_saved = 0; + PL_ppaddr[OP_PADSV] = saved; + MY_CXT.pp_padsv_saved = 0; } /* ... Our ck_pad{any,sv} .................................................. */ @@ -344,69 +557,67 @@ STATIC void lt_pp_padsv_restore(OP *o) { * pp_padsv, but much less than if we would have set PL_ppaddr[OP_PADSV] * globally. */ -STATIC SV *lt_default_meth = NULL; - STATIC OP *(*lt_old_ck_padany)(pTHX_ OP *) = 0; STATIC OP *lt_ck_padany(pTHX_ OP *o) { HV *stash; - SV *hint; + SV *code; + dMY_CXT; lt_pp_padsv_restore(o); o = CALL_FPTR(lt_old_ck_padany)(aTHX_ o); stash = PL_in_my_stash; - if (stash && (hint = lt_hint())) { + if (stash && (code = lt_hint())) { SV *orig_pkg = newSVpvn(HvNAME_get(stash), HvNAMELEN_get(stash)); - SV *orig_meth = lt_default_meth; + SV *orig_meth = MY_CXT.default_meth; SV *type_pkg = NULL; SV *type_meth = NULL; - SV *code = lt_detag(hint); + int items; - SvREADONLY_on(orig_pkg); + dSP; - if (code) { - int items; - dSP; + SvREADONLY_on(orig_pkg); - ENTER; - SAVETMPS; + ENTER; + SAVETMPS; - PUSHMARK(SP); - EXTEND(SP, 2); - PUSHs(orig_pkg); - PUSHs(orig_meth); - PUTBACK; + PUSHMARK(SP); + EXTEND(SP, 2); + PUSHs(orig_pkg); + PUSHs(orig_meth); + PUTBACK; - items = call_sv(code, G_ARRAY); + items = call_sv(code, G_ARRAY); - SPAGAIN; - if (items > 2) - croak(__PACKAGE__ " mangler should return zero, one or two scalars, but got %d", items); - if (items == 0) { - SvREFCNT_dec(orig_pkg); - goto skip; - } else { - SV *rsv; - if (items > 1) { - rsv = POPs; - if (SvOK(rsv)) { - type_meth = newSVsv(rsv); - SvREADONLY_on(type_meth); - } - } + SPAGAIN; + if (items > 2) + croak(__PACKAGE__ " mangler should return zero, one or two scalars, but got %d", items); + if (items == 0) { + SvREFCNT_dec(orig_pkg); + FREETMPS; + LEAVE; + goto skip; + } else { + SV *rsv; + if (items > 1) { rsv = POPs; if (SvOK(rsv)) { - type_pkg = newSVsv(rsv); - SvREADONLY_on(type_pkg); + type_meth = newSVsv(rsv); + SvREADONLY_on(type_meth); } } - PUTBACK; - - FREETMPS; - LEAVE; + rsv = POPs; + if (SvOK(rsv)) { + type_pkg = newSVsv(rsv); + SvREADONLY_on(type_pkg); + } } + PUTBACK; + + FREETMPS; + LEAVE; if (!type_pkg) { type_pkg = orig_pkg; @@ -420,57 +631,108 @@ STATIC OP *lt_ck_padany(pTHX_ OP *o) { lt_pp_padsv_save(); - lt_map_store(o, orig_pkg, type_pkg, type_meth, lt_pp_padsv_saved); + lt_map_store(o, orig_pkg, type_pkg, type_meth, MY_CXT.pp_padsv_saved); + } else { +skip: + lt_map_delete(o); } -skip: return o; } STATIC OP *(*lt_old_ck_padsv)(pTHX_ OP *) = 0; STATIC OP *lt_ck_padsv(pTHX_ OP *o) { + dMY_CXT; + lt_pp_padsv_restore(o); + lt_map_delete(o); + return CALL_FPTR(lt_old_ck_padsv)(aTHX_ o); } STATIC U32 lt_initialized = 0; +STATIC void lt_teardown(pTHX_ void *root) { + dMY_CXT; + + if (!lt_initialized) + return; + +#if LT_MULTIPLICITY + if (aTHX != root) + return; +#endif + +#if LT_THREADSAFE + ptable_hints_free(MY_CXT.tbl); +#endif + SvREFCNT_dec(MY_CXT.default_meth); + + PL_check[OP_PADANY] = MEMBER_TO_FPTR(lt_old_ck_padany); + lt_old_ck_padany = 0; + PL_check[OP_PADSV] = MEMBER_TO_FPTR(lt_old_ck_padsv); + lt_old_ck_padsv = 0; + + lt_initialized = 0; +} + +STATIC void lt_setup(pTHX) { +#define lt_setup() lt_setup(aTHX) + if (lt_initialized) + return; + + { + MY_CXT_INIT; +#if LT_THREADSAFE + MY_CXT.tbl = ptable_new(); + MY_CXT.owner = aTHX; +#endif + MY_CXT.pp_padsv_saved = 0; + MY_CXT.default_meth = newSVpvn("TYPEDSCALAR", 11); + SvREADONLY_on(MY_CXT.default_meth); + } + + lt_old_ck_padany = PL_check[OP_PADANY]; + PL_check[OP_PADANY] = MEMBER_TO_FPTR(lt_ck_padany); + lt_old_ck_padsv = PL_check[OP_PADSV]; + PL_check[OP_PADSV] = MEMBER_TO_FPTR(lt_ck_padsv); + +#if LT_MULTIPLICITY + call_atexit(lt_teardown, aTHX); +#else + call_atexit(lt_teardown, NULL); +#endif + + lt_initialized = 1; +} + +STATIC U32 lt_booted = 0; + /* --- XS ------------------------------------------------------------------ */ MODULE = Lexical::Types PACKAGE = Lexical::Types PROTOTYPES: ENABLE -BOOT: -{ - if (!lt_initialized++) { +BOOT: +{ + if (!lt_booted++) { HV *stash; -#if LT_THREADSAFE - MY_CXT_INIT; - MY_CXT.tbl = ptable_new(); - MY_CXT.owner = aTHX; -#endif lt_op_map = ptable_new(); #ifdef USE_ITHREADS MUTEX_INIT(<_op_map_mutex); #endif - lt_default_meth = newSVpvn("TYPEDSCALAR", 11); - SvREADONLY_on(lt_default_meth); - PERL_HASH(lt_hash, __PACKAGE__, __PACKAGE_LEN__); - lt_old_ck_padany = PL_check[OP_PADANY]; - PL_check[OP_PADANY] = MEMBER_TO_FPTR(lt_ck_padany); - lt_old_ck_padsv = PL_check[OP_PADSV]; - PL_check[OP_PADSV] = MEMBER_TO_FPTR(lt_ck_padsv); - stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1); newCONSTSUB(stash, "LT_THREADSAFE", newSVuv(LT_THREADSAFE)); } + + lt_setup(); } #if LT_THREADSAFE @@ -480,27 +742,25 @@ CLONE(...) PROTOTYPE: DISABLE PREINIT: ptable *t; - int *level; -CODE: + SV *cloned_default_meth; +PPCODE: { my_cxt_t ud; dMY_CXT; ud.tbl = t = ptable_new(); ud.owner = MY_CXT.owner; ptable_walk(MY_CXT.tbl, lt_ptable_hints_clone, &ud); + cloned_default_meth = lt_clone(MY_CXT.default_meth, MY_CXT.owner); } { MY_CXT_CLONE; - MY_CXT.tbl = t; - MY_CXT.owner = aTHX; - } - { - level = PerlMemShared_malloc(sizeof *level); - *level = 1; - LEAVE; - SAVEDESTRUCTOR_X(lt_thread_cleanup, level); - ENTER; + MY_CXT.tbl = t; + MY_CXT.owner = aTHX; + MY_CXT.pp_padsv_saved = 0; + MY_CXT.default_meth = cloned_default_meth; } + reap(3, lt_thread_cleanup, NULL); + XSRETURN(0); #endif