| 1 |
/* |
| 2 |
Logic for a better replacement of PyGILState_Ensure(). |
| 3 |
|
| 4 |
This version is ready to handle the case of a non-Python-started |
| 5 |
thread in which we do a large number of calls to CFFI callbacks. If |
| 6 |
we were to rely on PyGILState_Ensure() for that, we would constantly |
| 7 |
be creating and destroying PyThreadStates---it is slow, and |
| 8 |
PyThreadState_Delete() will actually walk the list of all thread |
| 9 |
states, making it O(n). :-( |
| 10 |
|
| 11 |
This version only creates one PyThreadState object the first time we |
| 12 |
see a given thread, and keep it alive until the thread is really |
| 13 |
shut down, using a destructor on the tls key. |
| 14 |
*/ |
| 15 |
#ifndef CFFI_MISC_THREAD_POSIX_H |
| 16 |
#define CFFI_MISC_THREAD_POSIX_H |
| 17 |
|
| 18 |
#include <pthread.h> |
| 19 |
#include "misc_thread_common.h" |
| 20 |
|
| 21 |
|
| 22 |
static pthread_key_t cffi_tls_key; |
| 23 |
|
| 24 |
static void init_cffi_tls(void) |
| 25 |
{ |
| 26 |
if (pthread_key_create(&cffi_tls_key, &cffi_thread_shutdown) != 0) |
| 27 |
PyErr_SetString(PyExc_OSError, "pthread_key_create() failed"); |
| 28 |
} |
| 29 |
|
| 30 |
static struct cffi_tls_s *_make_cffi_tls(void) |
| 31 |
{ |
| 32 |
void *p = calloc(1, sizeof(struct cffi_tls_s)); |
| 33 |
if (p == NULL) |
| 34 |
return NULL; |
| 35 |
if (pthread_setspecific(cffi_tls_key, p) != 0) { |
| 36 |
free(p); |
| 37 |
return NULL; |
| 38 |
} |
| 39 |
return p; |
| 40 |
} |
| 41 |
|
| 42 |
static struct cffi_tls_s *get_cffi_tls(void) |
| 43 |
{ |
| 44 |
void *p = pthread_getspecific(cffi_tls_key); |
| 45 |
if (p == NULL) |
| 46 |
p = _make_cffi_tls(); |
| 47 |
return (struct cffi_tls_s *)p; |
| 48 |
} |
| 49 |
|
| 50 |
#define save_errno save_errno_only |
| 51 |
#define restore_errno restore_errno_only |
| 52 |
|
| 53 |
#ifdef Py_GIL_DISABLED |
| 54 |
# ifndef __ATOMIC_SEQ_CST |
| 55 |
# error "The free threading build needs atomic support" |
| 56 |
# endif |
| 57 |
|
| 58 |
/* Minimal atomic support */ |
| 59 |
static void *cffi_atomic_load(void **ptr) |
| 60 |
{ |
| 61 |
return __atomic_load_n(ptr, __ATOMIC_SEQ_CST); |
| 62 |
} |
| 63 |
|
| 64 |
static void cffi_atomic_store(void **ptr, void *value) |
| 65 |
{ |
| 66 |
__atomic_store_n(ptr, value, __ATOMIC_SEQ_CST); |
| 67 |
} |
| 68 |
|
| 69 |
static uint8_t cffi_atomic_load_uint8(uint8_t *ptr) |
| 70 |
{ |
| 71 |
return __atomic_load_n(ptr, __ATOMIC_SEQ_CST); |
| 72 |
} |
| 73 |
|
| 74 |
static void cffi_atomic_store_uint8(uint8_t *ptr, uint8_t value) |
| 75 |
{ |
| 76 |
__atomic_store_n(ptr, value, __ATOMIC_SEQ_CST); |
| 77 |
} |
| 78 |
|
| 79 |
static Py_ssize_t cffi_atomic_load_ssize(Py_ssize_t *ptr) |
| 80 |
{ |
| 81 |
return __atomic_load_n(ptr, __ATOMIC_SEQ_CST); |
| 82 |
} |
| 83 |
|
| 84 |
static void cffi_atomic_store_ssize(Py_ssize_t *ptr, Py_ssize_t value) |
| 85 |
{ |
| 86 |
__atomic_store_n(ptr, value, __ATOMIC_SEQ_CST); |
| 87 |
} |
| 88 |
|
| 89 |
#endif |
| 90 |
|
| 91 |
#endif /* CFFI_MISC_THREAD_POSIX_H */ |