[pypy-commit] pypy stm-thread-2: Hacks to get Boehm to translate and run. Crashes, but that may be

arigo noreply at buildbot.pypy.org
Thu Sep 6 15:38:23 CEST 2012


Author: Armin Rigo <arigo at tunes.org>
Branch: stm-thread-2
Changeset: r57175:e4a73bd95338
Date: 2012-09-06 15:38 +0200
http://bitbucket.org/pypy/pypy/changeset/e4a73bd95338/

Log:	Hacks to get Boehm to translate and run. Crashes, but that may be a
	genuine bug.

diff --git a/pypy/rpython/memory/gctransform/boehm.py b/pypy/rpython/memory/gctransform/boehm.py
--- a/pypy/rpython/memory/gctransform/boehm.py
+++ b/pypy/rpython/memory/gctransform/boehm.py
@@ -57,7 +57,7 @@
             self.mixlevelannotator.finish()   # for now
             self.mixlevelannotator.backend_optimize()
 
-    def gcheader_initdata(self, hdr):
+    def gcheader_initdata(self, hdr, ptr):
         pass
 
     def push_alive_nopyobj(self, var, llops):
diff --git a/pypy/rpython/memory/gctransform/boehmstm.py b/pypy/rpython/memory/gctransform/boehmstm.py
--- a/pypy/rpython/memory/gctransform/boehmstm.py
+++ b/pypy/rpython/memory/gctransform/boehmstm.py
@@ -1,5 +1,5 @@
 from pypy.rpython.memory.gctransform.boehm import BoehmGCTransformer
-from pypy.rpython.lltypesystem import lltype
+from pypy.rpython.lltypesystem import lltype, llmemory
 from pypy.rlib.rarithmetic import r_uint, LONG_BIT
 
 
@@ -13,9 +13,18 @@
 
 class BoehmSTMGCTransformer(BoehmGCTransformer):
     HDR = lltype.Struct("header", ("hash", lltype.Signed),
+                                  ("size", lltype.Signed),
                                   ("tid", lltype.Signed),    # for flags only
                                   ("revision", lltype.Unsigned))
 
-    def gcheader_initdata(self, hdr):
+    def gcheader_initdata(self, hdr, ptr):
+        ptr = lltype.normalizeptr(ptr)
+        TYPE = lltype.typeOf(ptr).TO
+        if TYPE._is_varsize():
+            while isinstance(ptr._T, lltype.Struct):
+                ptr = getattr(ptr, ptr._T._arrayfld)
+            hdr.size = llmemory.sizeof(TYPE, len(ptr))
+        else:
+            hdr.size = llmemory.sizeof(TYPE)
         hdr.tid = GCFLAG_PREBUILT
         hdr.revision = REV_INITIAL
diff --git a/pypy/translator/c/gc.py b/pypy/translator/c/gc.py
--- a/pypy/translator/c/gc.py
+++ b/pypy/translator/c/gc.py
@@ -207,9 +207,10 @@
 
     def common_gcheader_initdata(self, defnode):
         if defnode.db.gctransformer is not None:
+            ptr = defnode.obj._as_ptr()
             hdr = lltype.malloc(defnode.db.gctransformer.HDR, immortal=True)
-            hdr.hash = lltype.identityhash_nocache(defnode.obj._as_ptr())
-            defnode.db.gctransformer.gcheader_initdata(hdr)
+            hdr.hash = lltype.identityhash_nocache(ptr)
+            defnode.db.gctransformer.gcheader_initdata(hdr, ptr)
             return hdr._obj
         return None
 
diff --git a/pypy/translator/c/genc.py b/pypy/translator/c/genc.py
--- a/pypy/translator/c/genc.py
+++ b/pypy/translator/c/genc.py
@@ -473,8 +473,12 @@
         from pypy.rpython.lltypesystem import rffi
         from pypy.rpython.annlowlevel import MixLevelHelperAnnotator
         entrypoint = self.entrypoint
+        stm = self.config.translation.stm
         #
         def entrypoint_wrapper(argc, argv):
+            if stm:
+                from pypy.translator.stm.funcgen import _stm_init_function
+                _stm_init_function()
             list = [""] * argc
             i = 0
             while i < argc:
diff --git a/pypy/translator/c/src/mem.h b/pypy/translator/c/src/mem.h
--- a/pypy/translator/c/src/mem.h
+++ b/pypy/translator/c/src/mem.h
@@ -178,16 +178,13 @@
 /* #define BOEHM_MALLOC_0_1   GC_MALLOC_IGNORE_OFF_PAGE */
 /* #define BOEHM_MALLOC_1_1   GC_MALLOC_ATOMIC_IGNORE_OFF_PAGE */
 
-#ifndef _BOEHM_ALLOCATED
-#  define _BOEHM_ALLOCATED(r)    /* nothing */
-#endif
-
+#ifndef OP_BOEHM_ZERO_MALLOC    /* may be defined already by src_stm/et.h */
 #define OP_BOEHM_ZERO_MALLOC(size, r, restype, is_atomic, is_varsize)   {             \
 	r = (restype) BOEHM_MALLOC_ ## is_atomic ## _ ## is_varsize (size);    \
 	if (r && is_atomic)  /* the non-atomic versions return cleared memory */ \
                 memset((void*) r, 0, size);				\
-        _BOEHM_ALLOCATED(r);                                            \
   }
+#endif
 
 #define OP_BOEHM_DISAPPEARING_LINK(link, obj, r)			   \
 	if (GC_base(obj) == NULL)					   \
diff --git a/pypy/translator/stm/funcgen.py b/pypy/translator/stm/funcgen.py
--- a/pypy/translator/stm/funcgen.py
+++ b/pypy/translator/stm/funcgen.py
@@ -1,4 +1,5 @@
 from pypy.translator.c.support import c_string_constant
+from pypy.translator.stm.stmgcintf import StmOperations
 
 
 def stm_start_transaction(funcgen, op):
@@ -38,6 +39,11 @@
     XXX
     return funcgen.OP_DIRECT_CALL(op)
 
+def _stm_init_function():
+    """Called at process start-up."""
+    StmOperations.descriptor_init()
+    StmOperations.begin_inevitable_transaction()
+
 
 def op_stm(funcgen, op):
     func = globals()[op.opname]
diff --git a/pypy/translator/stm/src_stm/et.h b/pypy/translator/stm/src_stm/et.h
--- a/pypy/translator/stm/src_stm/et.h
+++ b/pypy/translator/stm/src_stm/et.h
@@ -113,9 +113,10 @@
 # define OP_GC_ADR_OF_ROOT_STACK_TOP(r)   r = NULL
 void stm_boehm_start_transaction(void);
 void stm_boehm_stop_transaction(void);
-void stm_boehm_allocated(gcptr);
-# undef _BOEHM_ALLOCATED
-# define _BOEHM_ALLOCATED(r) stm_boehm_allocated((gcptr)(r))
+gcptr stm_boehm_allocate(size_t);
+# undef OP_BOEHM_ZERO_MALLOC
+# define OP_BOEHM_ZERO_MALLOC(size, r, restype, is_atomic, is_varsize)  \
+    r = (restype) stm_boehm_allocate(size)
 #endif
 
 #endif  /* _ET_H */
diff --git a/pypy/translator/stm/src_stm/rpyintf.c b/pypy/translator/stm/src_stm/rpyintf.c
--- a/pypy/translator/stm/src_stm/rpyintf.c
+++ b/pypy/translator/stm/src_stm/rpyintf.c
@@ -123,7 +123,9 @@
   void **volatile v_saved_value;
   long volatile v_atomic = thread_descriptor->atomic;
   assert((!thread_descriptor->active) == (!v_atomic));
+#ifndef USING_BOEHM_GC
   v_saved_value = *(void***)save_and_restore;
+#endif
   /***/
   setjmp(_jmpbuf);
   /* After setjmp(), the local variables v_* are preserved because they
@@ -133,6 +135,7 @@
   void **restore_value;
   counter = v_counter;
   d->atomic = v_atomic;
+#ifndef USING_BOEHM_GC
   restore_value = v_saved_value;
   if (!d->atomic)
     {
@@ -143,6 +146,7 @@
       *restore_value++ = END_MARKER;
     }
   *(void***)save_and_restore = restore_value;
+#endif
 
   do
     {
@@ -169,7 +173,9 @@
   if (d->atomic && d->setjmp_buf == &_jmpbuf)
     BecomeInevitable("perform_transaction left with atomic");
 
+#ifndef USING_BOEHM_GC
   *(void***)save_and_restore = v_saved_value;
+#endif
 }
 
 void stm_abort_and_retry(void)
@@ -181,32 +187,47 @@
 static __thread gcptr stm_boehm_chained_list;
 void stm_boehm_start_transaction(void)
 {
+    GC_init();
     stm_boehm_chained_list = NULL;
 }
-void stm_boehm_allocated(gcptr W)
+gcptr stm_boehm_allocate(size_t size)
 {
+    gcptr W = GC_local_malloc(size);
+    memset((void*) W, 0, size);
+    W->h_size = size;
     W->h_revision = (revision_t)stm_boehm_chained_list;
     stm_boehm_chained_list = W;
+    return W;
 }
 void stm_boehm_stop_transaction(void)
 {
+    struct gcroot_s *gcroots;
     gcptr W = stm_boehm_chained_list;
     stm_boehm_chained_list = NULL;
     while (W) {
         gcptr W_next = (gcptr)W->h_revision;
-        assert(W->h_tid & (GCFLAG_GLOBAL |
-                           GCFLAG_NOT_WRITTEN |
-                           GCFLAG_LOCAL_COPY) == 0);
+        assert((W->h_tid & (GCFLAG_GLOBAL |
+                            GCFLAG_NOT_WRITTEN |
+                            GCFLAG_LOCAL_COPY)) == 0);
         W->h_tid |= GCFLAG_GLOBAL | GCFLAG_NOT_WRITTEN;
         W->h_revision = 1;
         W = W_next;
     }
-    FindRootsForLocalCollect();
+
+    gcroots = FindRootsForLocalCollect();
+    while (gcroots->R != NULL) {
+        W = gcroots->L;
+        assert((W->h_tid & (GCFLAG_GLOBAL |
+                            GCFLAG_NOT_WRITTEN |
+                            GCFLAG_LOCAL_COPY)) == GCFLAG_LOCAL_COPY);
+        W->h_tid |= GCFLAG_GLOBAL | GCFLAG_NOT_WRITTEN;
+        gcroots++;
+    }
 }
 void *pypy_g__stm_duplicate(void *src)
 {
-    size_t size = GC_size(src);
-    void *result = GC_MALLOC(size);
+    size_t size = ((gcptr)src)->h_size;
+    void *result = GC_local_malloc(size);
     memcpy(result, src, size);
     return result;
 }


More information about the pypy-commit mailing list