]> git.vpit.fr Git - perl/modules/Lexical-Types.git/commitdiff
Fix the thread destructor trick for 5.13.1
authorVincent Pit <vince@profvince.com>
Tue, 1 Jun 2010 16:36:55 +0000 (18:36 +0200)
committerVincent Pit <vince@profvince.com>
Tue, 1 Jun 2010 16:36:55 +0000 (18:36 +0200)
MANIFEST
Types.xs
reap.h [new file with mode: 0644]

index 40ec6eaa779469a057ddec88d3fb25ebcc2b533f..53ad9cebf33eb0950e59b5dc260ace4d7aa3751c 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -6,6 +6,7 @@ README
 Types.xs
 lib/Lexical/Types.pm
 ptable.h
+reap.h
 samples/basic.pl
 t/00-load.t
 t/10-base.t
index 0a13cdb88caf7c94517d61d6188349c1e15a604c..a8fb284c35a89b6f8379f04553f51f7f676a54fa 100644 (file)
--- a/Types.xs
+++ b/Types.xs
@@ -216,21 +216,12 @@ STATIC void lt_ptable_hints_clone(pTHX_ ptable_ent *ent, void *ud_) {
  ptable_hints_store(ud->tbl, ent->key, h2);
 }
 
-STATIC void lt_thread_cleanup(pTHX_ void *);
+#include "reap.h"
 
 STATIC void lt_thread_cleanup(pTHX_ void *ud) {
int *level = ud;
dMY_CXT;
 
- if (*level) {
-  *level = 0;
-  LEAVE;
-  SAVEDESTRUCTOR_X(lt_thread_cleanup, level);
-  ENTER;
- } else {
-  dMY_CXT;
-  PerlMemShared_free(level);
-  ptable_hints_free(MY_CXT.tbl);
- }
+ ptable_hints_free(MY_CXT.tbl);
 }
 
 #endif /* LT_THREADSAFE */
@@ -739,7 +730,6 @@ CLONE(...)
 PROTOTYPE: DISABLE
 PREINIT:
  ptable *t;
- int    *level;
  SV     *cloned_default_meth;
 PPCODE:
  {
@@ -757,13 +747,7 @@ PPCODE:
   MY_CXT.pp_padsv_saved = 0;
   MY_CXT.default_meth   = cloned_default_meth;
  }
- {
-  level = PerlMemShared_malloc(sizeof *level);
-  *level = 1;
-  LEAVEn("sub");
-  SAVEDESTRUCTOR_X(lt_thread_cleanup, level);
-  ENTERn("sub");
- }
+ reap(3, lt_thread_cleanup, NULL);
  XSRETURN(0);
 
 #endif
diff --git a/reap.h b/reap.h
new file mode 100644 (file)
index 0000000..82329c5
--- /dev/null
+++ b/reap.h
@@ -0,0 +1,81 @@
+/* This file is part of the Lexical::Types Perl module.
+ * See http://search.cpan.org/dist/Lexical-Types/ */
+
+/* This header provides a specialized version of Scope::Upper::reap that can be
+ * called directly from XS.
+ * See http://search.cpan.org/dist/Scope-Upper/ for details. */
+
+#ifndef REAP_H
+#define REAP_H 1
+
+#define REAP_DESTRUCTOR_SIZE 3
+
+typedef struct {
+ I32    depth;
+ I32   *origin;
+ void (*cb)(pTHX_ void *);
+ void  *ud;
+ char  *dummy;
+} reap_ud;
+
+STATIC void reap_pop(pTHX_ void *);
+
+STATIC void reap_pop(pTHX_ void *ud_) {
+ reap_ud *ud = ud_;
+ I32 depth, *origin, mark, base;
+
+ depth  = ud->depth;
+ origin = ud->origin;
+ mark   = origin[depth];
+ base   = origin[depth - 1];
+
+ if (base < mark) {
+  PL_savestack_ix = mark;
+  leave_scope(base);
+ }
+ PL_savestack_ix = base;
+
+ if ((ud->depth = --depth) > 0) {
+  SAVEDESTRUCTOR_X(reap_pop, ud);
+ } else {
+  void (*cb)(pTHX_ void *) = ud->cb;
+  void  *cb_ud             = ud->ud;
+
+  PerlMemShared_free(ud->origin);
+  PerlMemShared_free(ud);
+
+  SAVEDESTRUCTOR_X(cb, cb_ud);
+ }
+}
+
+STATIC void reap(pTHX_ I32 depth, void (*cb)(pTHX_ void *), void *cb_ud) {
+#define reap(D, CB, UD) reap(aTHX_ (D), (CB), (UD))
+ reap_ud *ud;
+ I32 i;
+
+ if (depth > PL_scopestack_ix)
+  depth = PL_scopestack_ix;
+
+ ud         = PerlMemShared_malloc(sizeof *ud);
+ ud->depth  = depth;
+ ud->origin = PerlMemShared_malloc((depth + 1) * sizeof *ud->origin);
+ ud->cb     = cb;
+ ud->ud     = cb_ud;
+ ud->dummy  = NULL;
+
+ for (i = depth; i >= 1; --i) {
+  I32 j = PL_scopestack_ix - i;
+  ud->origin[depth - i] = PL_scopestack[j];
+  PL_scopestack[j] += REAP_DESTRUCTOR_SIZE;
+ }
+ ud->origin[depth] = PL_savestack_ix;
+
+ while (PL_savestack_ix + REAP_DESTRUCTOR_SIZE
+                                       <= PL_scopestack[PL_scopestack_ix - 1]) {
+  save_pptr(&ud->dummy);
+ }
+
+ SAVEDESTRUCTOR_X(reap_pop, ud);
+}
+
+#endif /* REAP_H */