Author: Armin Rigo <[email protected]>
Branch: ec-threadlocal
Changeset: r72134:3f04bb5349f6
Date: 2014-06-22 17:47 +0200
http://bitbucket.org/pypy/pypy/changeset/3f04bb5349f6/

Log:    Hack around until ThreadLocalReference translates

diff --git a/rpython/rlib/rthread.py b/rpython/rlib/rthread.py
--- a/rpython/rlib/rthread.py
+++ b/rpython/rlib/rthread.py
@@ -281,14 +281,18 @@
 
 class ThreadLocalReference(object):
     _COUNT = 1
+    OPAQUEID = lltype.OpaqueType("ThreadLocalRef",
+                                 hints={"threadlocalref": True})
 
     def __init__(self, Cls):
         "NOT_RPYTHON: must be prebuilt"
         import thread
         self.Cls = Cls
         self.local = thread._local()      # <- NOT_RPYTHON
-        self.unique_id = ThreadLocalReference._COUNT
+        unique_id = ThreadLocalReference._COUNT
         ThreadLocalReference._COUNT += 1
+        self.opaque_id = lltype.opaqueptr(ThreadLocalReference.OPAQUEID,
+                                          'tlref%d' % unique_id)
 
     def _freeze_(self):
         return True
@@ -298,7 +302,7 @@
         if we_are_translated():
             from rpython.rtyper.lltypesystem import rclass
             from rpython.rtyper.annlowlevel import cast_base_ptr_to_instance
-            ptr = llop.threadlocalref_get(rclass.OBJECTPTR, self.unique_id)
+            ptr = llop.threadlocalref_get(rclass.OBJECTPTR, self.opaque_id)
             return cast_base_ptr_to_instance(self.Cls, ptr)
         else:
             return getattr(self.local, 'value', None)
@@ -310,6 +314,6 @@
         if we_are_translated():
             from rpython.rtyper.annlowlevel import cast_instance_to_base_ptr
             ptr = cast_instance_to_base_ptr(value)
-            llop.threadlocalref_set(lltype.Void, self.unique_id, ptr)
+            llop.threadlocalref_set(lltype.Void, self.opaque_id, ptr)
         else:
             self.local.value = value
diff --git a/rpython/translator/c/node.py b/rpython/translator/c/node.py
--- a/rpython/translator/c/node.py
+++ b/rpython/translator/c/node.py
@@ -959,12 +959,30 @@
                 args.append('0')
         yield 'RPyOpaque_SETUP_%s(%s);' % (T.tag, ', '.join(args))
 
+class ThreadLocalRefOpaqueNode(ContainerNode):
+    nodekind = 'tlrefopaque'
+
+    def basename(self):
+        return self.obj._name
+
+    def enum_dependencies(self):
+        return []
+
+    def initializationexpr(self, decoration=''):
+        return ['{ NULL }']
+
+    def startupcode(self):
+        p = self.getptrname()
+        yield 'RPyThreadStaticTLS_Create(%s);' % (p,)
+
 
 def opaquenode_factory(db, T, obj):
     if T == RuntimeTypeInfo:
         return db.gcpolicy.rtti_node_factory()(db, T, obj)
     if T.hints.get("render_structure", False):
         return ExtType_OpaqueNode(db, T, obj)
+    if T.hints.get("threadlocalref", False):
+        return ThreadLocalRefOpaqueNode(db, T, obj)
     raise Exception("don't know about %r" % (T,))
 
 
diff --git a/rpython/translator/c/src/g_include.h 
b/rpython/translator/c/src/g_include.h
--- a/rpython/translator/c/src/g_include.h
+++ b/rpython/translator/c/src/g_include.h
@@ -19,6 +19,7 @@
 #include "src/address.h"
 #include "src/unichar.h"
 #include "src/llgroup.h"
+#include "src/threadlocal.h"
 
 #include "src/instrument.h"
 #include "src/asm.h"
diff --git a/rpython/translator/c/src/stack.c b/rpython/translator/c/src/stack.c
--- a/rpython/translator/c/src/stack.c
+++ b/rpython/translator/c/src/stack.c
@@ -32,12 +32,7 @@
                /* XXX We assume that initialization is performed early,
                   when there is still only one thread running.  This
                   allows us to ignore race conditions here */
-               char *errmsg = RPyThreadStaticTLS_Create(&end_tls_key);
-               if (errmsg) {
-                       /* XXX should we exit the process? */
-                       fprintf(stderr, "Internal PyPy error: %s\n", errmsg);
-                       return 1;
-               }
+               RPyThreadStaticTLS_Create(&end_tls_key);
        }
 
        baseptr = (char *) RPyThreadStaticTLS_Get(end_tls_key);
diff --git a/rpython/translator/c/src/threadlocal.c 
b/rpython/translator/c/src/threadlocal.c
--- a/rpython/translator/c/src/threadlocal.c
+++ b/rpython/translator/c/src/threadlocal.c
@@ -2,23 +2,25 @@
 
 #ifdef _WIN32
 
-char *RPyThreadTLS_Create(RPyThreadTLS *result)
+void RPyThreadTLS_Create(RPyThreadTLS *result)
 {
     *result = TlsAlloc();
-    if (*result == TLS_OUT_OF_INDEXES)
-        return "out of thread-local storage indexes";
-    else
-        return NULL;
+    if (*result == TLS_OUT_OF_INDEXES) {
+        fprintf(stderr, "Internal RPython error: "
+                        "out of thread-local storage indexes");
+        abort();
+    }
 }
 
 #else
 
-char *RPyThreadTLS_Create(RPyThreadTLS *result)
+void RPyThreadTLS_Create(RPyThreadTLS *result)
 {
-    if (pthread_key_create(result, NULL) != 0)
-        return "out of thread-local storage keys";
-    else
-        return NULL;
+    if (pthread_key_create(result, NULL) != 0) {
+        fprintf(stderr, "Internal RPython error: "
+                        "out of thread-local storage keys");
+        abort();
+    }
 }
 
 #endif
diff --git a/rpython/translator/c/src/threadlocal.h 
b/rpython/translator/c/src/threadlocal.h
--- a/rpython/translator/c/src/threadlocal.h
+++ b/rpython/translator/c/src/threadlocal.h
@@ -1,4 +1,7 @@
 /* Thread-local storage */
+#ifndef _SRC_THREADLOCAL_H
+#define _SRC_THREADLOCAL_H
+
 
 #ifdef _WIN32
 
@@ -22,7 +25,7 @@
 #ifdef USE___THREAD
 
 #define RPyThreadStaticTLS                  __thread void *
-#define RPyThreadStaticTLS_Create(tls)      NULL
+#define RPyThreadStaticTLS_Create(tls)      (void)0
 #define RPyThreadStaticTLS_Get(tls)         tls
 #define RPyThreadStaticTLS_Set(tls, value)  tls = value
 
@@ -34,7 +37,15 @@
 #define RPyThreadStaticTLS_Create(key) RPyThreadTLS_Create(key)
 #define RPyThreadStaticTLS_Get(key)    RPyThreadTLS_Get(key)
 #define RPyThreadStaticTLS_Set(key, value) RPyThreadTLS_Set(key, value)
-char *RPyThreadTLS_Create(RPyThreadTLS *result);
+void RPyThreadTLS_Create(RPyThreadTLS *result);
 
 #endif
 
+
+struct pypy_opaque_ThreadLocalRef { void *gcref; };
+
+#define OP_THREADLOCALREF_SET(tlref, ptr, _)   tlref->gcref = ptr
+#define OP_THREADLOCALREF_GET(tlref, ptr)      ptr = tlref->gcref
+
+
+#endif /* _SRC_THREADLOCAL_H */
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to