From 2fea9b961d6ea1041ace66037513fcad1fc2173d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Valur=20J=C3=B3nsson?= Date: Mon, 20 Sep 2010 02:11:49 +0000 Subject: [PATCH] issue 9786 Native TLS support for pthreads PyThread_create_key now has a failure mode that the applicatino can detect. --- Python/pystate.c | 2 ++ Python/thread_nt.h | 5 ++++- Python/thread_pthread.h | 43 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/Python/pystate.c b/Python/pystate.c index 77534fc12f8..d5d98b0c6d6 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -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); diff --git a/Python/thread_nt.h b/Python/thread_nt.h index 2fd709817c9..9de9e0dfbec 100644 --- a/Python/thread_nt.h +++ b/Python/thread_nt.h @@ -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 diff --git a/Python/thread_pthread.h b/Python/thread_pthread.h index 5e52b3977ea..a529b7a7eb1 100644 --- a/Python/thread_pthread.h +++ b/Python/thread_pthread.h @@ -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) +{}