]> granicus.if.org Git - python/commitdiff
issue 9786 Native TLS support for pthreads
authorKristján Valur Jónsson <kristjan@ccpgames.com>
Mon, 20 Sep 2010 02:11:49 +0000 (02:11 +0000)
committerKristján Valur Jónsson <kristjan@ccpgames.com>
Mon, 20 Sep 2010 02:11:49 +0000 (02:11 +0000)
PyThread_create_key now has a failure mode that the applicatino can detect.

Python/pystate.c
Python/thread_nt.h
Python/thread_pthread.h

index 77534fc12f86e6c1660a7d3c09fb87b92d344458..d5d98b0c6d6d81de18a757aec259eca1d0a03985 100644 (file)
@@ -569,6 +569,8 @@ _PyGILState_Init(PyInterpreterState *i, PyThreadState *t)
 {
     assert(i && t); /* must init with valid states */
     autoTLSkey = PyThread_create_key();
+    if (autoTLSkey == -1)
+        Py_FatalError("Could not allocate TLS entry");
     autoInterpreterState = i;
     assert(PyThread_get_key_value(autoTLSkey) == NULL);
     assert(t->gilstate_counter == 0);
index 2fd709817c974b54c79ebbb6281bebae5ed5108c..9de9e0dfbec0c8ec9f76e4492b64847c347e4b18 100644 (file)
@@ -315,7 +315,10 @@ _pythread_nt_set_stacksize(size_t size)
 int
 PyThread_create_key(void)
 {
-    return (int) TlsAlloc();
+    DWORD result= TlsAlloc();
+    if (result == TLS_OUT_OF_INDEXES)
+        return -1;
+    return (int)result;
 }
 
 void
index 5e52b3977eada8abf129954edc0c576443130b2f..a529b7a7eb191cd1778605c7a5cd10269fd3bb26 100644 (file)
@@ -558,3 +558,46 @@ _pythread_pthread_set_stacksize(size_t size)
 }
 
 #define THREAD_SET_STACKSIZE(x) _pythread_pthread_set_stacksize(x)
+
+#define Py_HAVE_NATIVE_TLS
+
+int
+PyThread_create_key(void)
+{
+    pthread_key_t key;
+    int fail = pthread_key_create(&key, NULL);
+    return fail ? -1 : key;
+}
+
+void
+PyThread_delete_key(int key)
+{
+    pthread_key_delete(key);
+}
+
+void
+PyThread_delete_key_value(int key)
+{
+    pthread_setspecific(key, NULL);
+}
+
+int
+PyThread_set_key_value(int key, void *value)
+{
+    int fail;
+    void *oldValue = pthread_getspecific(key);
+    if (oldValue != NULL)
+        return 0;
+    fail = pthread_setspecific(key, value);
+    return fail ? -1 : 0;
+}
+
+void *
+PyThread_get_key_value(int key)
+{
+    return pthread_getspecific(key);
+}
+
+void
+PyThread_ReInitTLS(void)
+{}