[pypy-svn] r24263 - in pypy/dist/pypy/translator: c c/src tool

pedronis at codespeak.net pedronis at codespeak.net
Sun Mar 12 15:02:54 CET 2006


Author: pedronis
Date: Sun Mar 12 15:02:51 2006
New Revision: 24263

Added:
   pypy/dist/pypy/translator/tool/__thread_test.c   (contents, props changed)
Modified:
   pypy/dist/pypy/translator/c/genc.py
   pypy/dist/pypy/translator/c/src/stack.h
   pypy/dist/pypy/translator/c/src/thread.h
   pypy/dist/pypy/translator/tool/cbuild.py
Log:
use __thread with gcc when possible.

At the moment doesn't seem to make any significant difference for our speed.



Modified: pypy/dist/pypy/translator/c/genc.py
==============================================================================
--- pypy/dist/pypy/translator/c/genc.py	(original)
+++ pypy/dist/pypy/translator/c/genc.py	Sun Mar 12 15:02:51 2006
@@ -7,6 +7,7 @@
 from pypy.translator.tool.cbuild import compile_c_module
 from pypy.translator.tool.cbuild import build_executable, CCompiler
 from pypy.translator.tool.cbuild import import_module_from_directory
+from pypy.translator.tool.cbuild import check_under_under_thread
 from pypy.rpython.lltypesystem import lltype
 from pypy.tool.udir import udir
 from pypy.tool import isolate
@@ -56,6 +57,8 @@
         db.complete()
         return db
 
+    have___thread = None
+
     def generate_source(self, db=None):
         assert self.c_source_filename is None
         translator = self.translator
@@ -70,6 +73,10 @@
         self.targetdir = targetdir
         defines = {}
         # defines={'COUNT_OP_MALLOCS': 1}
+        if CBuilder.have___thread is None:
+            CBuilder.have___thread = check_under_under_thread()
+        if CBuilder.have___thread:
+            defines['HAVE___THREAD'] = 1
         if not self.standalone:
             from pypy.translator.c.symboltable import SymbolTable
             self.symboltable = SymbolTable()

Modified: pypy/dist/pypy/translator/c/src/stack.h
==============================================================================
--- pypy/dist/pypy/translator/c/src/stack.h	(original)
+++ pypy/dist/pypy/translator/c/src/stack.h	Sun Mar 12 15:02:51 2006
@@ -37,7 +37,7 @@
 	static volatile char *stack_base_pointer = NULL;
 	static long stack_min = 0;
 	static long stack_max = 0;
-	static RPyThreadTLS stack_base_pointer_key;
+	static RPyThreadStaticTLS stack_base_pointer_key;
 	/* Check that the stack is less than MAX_STACK_SIZE bytes bigger
 	   than the value recorded in stack_base_pointer.  The base
 	   pointer is updated to the current value if it is still NULL
@@ -58,7 +58,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 = RPyThreadTLS_Create(&stack_base_pointer_key);
+		char *errmsg = RPyThreadStaticTLS_Create(&stack_base_pointer_key);
 		if (errmsg) {
 			/* XXX should we exit the process? */
 			fprintf(stderr, "Internal PyPy error: %s\n", errmsg);
@@ -70,7 +70,7 @@
 			stack_min = -MAX_STACK_SIZE;
 	}
 
-	baseptr = (char *) RPyThreadTLS_Get(stack_base_pointer_key);
+	baseptr = (char *) RPyThreadStaticTLS_Get(stack_base_pointer_key);
 	if (baseptr != NULL) {
 		diff = &local - baseptr;
 		if (stack_min <= diff && diff <= stack_max) {
@@ -92,7 +92,7 @@
 
 	/* update the stack base pointer to the current value */
 	baseptr = &local;
-	RPyThreadTLS_Set(stack_base_pointer_key, baseptr);
+	RPyThreadStaticTLS_Set(stack_base_pointer_key, baseptr);
 	stack_base_pointer = baseptr;
 	return 0;
 }

Modified: pypy/dist/pypy/translator/c/src/thread.h
==============================================================================
--- pypy/dist/pypy/translator/c/src/thread.h	(original)
+++ pypy/dist/pypy/translator/c/src/thread.h	Sun Mar 12 15:02:51 2006
@@ -17,3 +17,21 @@
 #ifdef NT_THREADS
 #include "thread_nt.h"
 #endif
+
+#ifdef HAVE___THREAD
+
+#define RPyThreadStaticTLS                  __thread void *
+#define RPyThreadStaticTLS_Create(tls)      NULL
+#define RPyThreadStaticTLS_Get(tls)         tls
+#define RPyThreadStaticTLS_Set(tls, value)  tls = value
+
+#endif
+
+#ifndef RPyThreadStaticTLS
+
+#define RPyThreadStaticTLS             RPyThreadTLS
+#define RPyThreadStaticTLS_Create(key) RPyThreadTLS_Create(key)
+#define RPyThreadStaticTLS_Get(key)    RPyThreadTLS_Get(key)
+#define RPyThreadStaticTLS_Set(key, value) RPyThreadTLS_Set(key, value)
+
+#endif

Added: pypy/dist/pypy/translator/tool/__thread_test.c
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/tool/__thread_test.c	Sun Mar 12 15:02:51 2006
@@ -0,0 +1,35 @@
+#include <unistd.h>
+#include <stdio.h>
+
+#define N 100000000
+
+#if defined(__GNUC__) && defined(_POSIX_THREADS)
+#include <pthread.h>
+
+void * th_f(void *x) {
+        volatile int * n = (volatile int *)x;
+        static __thread int i = 0;
+        int  delta;
+        delta = *n;
+        for (; i < N; i++) {
+                *n += delta;
+        }
+        return NULL;
+}
+
+int nbs[] = {2, 3};
+
+int main() {
+        pthread_t t0, t1;
+        pthread_create(&t0, NULL, th_f, &nbs[0]);
+        pthread_create(&t1, NULL, th_f, &nbs[1]);
+        pthread_join(t0, NULL);
+        pthread_join(t1, NULL);
+        printf("1= %d\n", nbs[0]);
+        printf("2= %d\n", nbs[1]);
+        return !(nbs[0] == (N+1)*2 && nbs[1] == (N+1)*3);
+}
+
+#else
+#error "Meaningful only with GCC (+ POSIX Threads)"
+#endif

Modified: pypy/dist/pypy/translator/tool/cbuild.py
==============================================================================
--- pypy/dist/pypy/translator/tool/cbuild.py	(original)
+++ pypy/dist/pypy/translator/tool/cbuild.py	Sun Mar 12 15:02:51 2006
@@ -354,3 +354,24 @@
         return False
     else:
         return True
+
+def check_under_under_thread():
+    from pypy.tool.udir import udir
+    cfile = py.path.local(autopath.this_dir).join('__thread_test.c')
+    fsource = cfile.open('r')
+    source = fsource.read()
+    fsource.close()
+    cfile = udir.join('__thread_test.c')
+    fsource = cfile.open('w')
+    fsource.write(source)
+    fsource.close()
+    try:
+       exe = build_executable([str(cfile)], 
+                              noerr=True)
+       py.process.cmdexec(exe)
+    except (KeyboardInterrupt, SystemExit):
+        raise
+    except:
+        return False
+    else:
+        return True



More information about the Pypy-commit mailing list