Skip to content

Commit 2fea9b9

Browse files
author
Kristján Valur Jónsson
committed
issue 9786 Native TLS support for pthreads
PyThread_create_key now has a failure mode that the applicatino can detect.
1 parent 3d8580f commit 2fea9b9

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

Python/pystate.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,8 @@ _PyGILState_Init(PyInterpreterState *i, PyThreadState *t)
569569
{
570570
assert(i && t); /* must init with valid states */
571571
autoTLSkey = PyThread_create_key();
572+
if (autoTLSkey == -1)
573+
Py_FatalError("Could not allocate TLS entry");
572574
autoInterpreterState = i;
573575
assert(PyThread_get_key_value(autoTLSkey) == NULL);
574576
assert(t->gilstate_counter == 0);

Python/thread_nt.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,10 @@ _pythread_nt_set_stacksize(size_t size)
315315
int
316316
PyThread_create_key(void)
317317
{
318-
return (int) TlsAlloc();
318+
DWORD result= TlsAlloc();
319+
if (result == TLS_OUT_OF_INDEXES)
320+
return -1;
321+
return (int)result;
319322
}
320323

321324
void

Python/thread_pthread.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,3 +558,46 @@ _pythread_pthread_set_stacksize(size_t size)
558558
}
559559

560560
#define THREAD_SET_STACKSIZE(x) _pythread_pthread_set_stacksize(x)
561+
562+
#define Py_HAVE_NATIVE_TLS
563+
564+
int
565+
PyThread_create_key(void)
566+
{
567+
pthread_key_t key;
568+
int fail = pthread_key_create(&key, NULL);
569+
return fail ? -1 : key;
570+
}
571+
572+
void
573+
PyThread_delete_key(int key)
574+
{
575+
pthread_key_delete(key);
576+
}
577+
578+
void
579+
PyThread_delete_key_value(int key)
580+
{
581+
pthread_setspecific(key, NULL);
582+
}
583+
584+
int
585+
PyThread_set_key_value(int key, void *value)
586+
{
587+
int fail;
588+
void *oldValue = pthread_getspecific(key);
589+
if (oldValue != NULL)
590+
return 0;
591+
fail = pthread_setspecific(key, value);
592+
return fail ? -1 : 0;
593+
}
594+
595+
void *
596+
PyThread_get_key_value(int key)
597+
{
598+
return pthread_getspecific(key);
599+
}
600+
601+
void
602+
PyThread_ReInitTLS(void)
603+
{}

0 commit comments

Comments
 (0)