summaryrefslogtreecommitdiff
path: root/Modules/_tracemalloc.c
diff options
context:
space:
mode:
Diffstat (limited to 'Modules/_tracemalloc.c')
-rw-r--r--Modules/_tracemalloc.c25
1 files changed, 8 insertions, 17 deletions
diff --git a/Modules/_tracemalloc.c b/Modules/_tracemalloc.c
index 386f2f11ba..af2a2fa4d5 100644
--- a/Modules/_tracemalloc.c
+++ b/Modules/_tracemalloc.c
@@ -167,14 +167,7 @@ tracemalloc_error(const char *format, ...)
#if defined(TRACE_RAW_MALLOC)
#define REENTRANT_THREADLOCAL
-/* If your OS does not provide native thread local storage, you can implement
- it manually using a lock. Functions of thread.c cannot be used because
- they use PyMem_RawMalloc() which leads to a reentrant call. */
-#if !(defined(_POSIX_THREADS) || defined(NT_THREADS))
-# error "need native thread local storage (TLS)"
-#endif
-
-static int tracemalloc_reentrant_key = -1;
+static Py_tss_t tracemalloc_reentrant_key = Py_tss_NEEDS_INIT;
/* Any non-NULL pointer can be used */
#define REENTRANT Py_True
@@ -184,8 +177,8 @@ get_reentrant(void)
{
void *ptr;
- assert(tracemalloc_reentrant_key != -1);
- ptr = PyThread_get_key_value(tracemalloc_reentrant_key);
+ assert(PyThread_tss_is_created(&tracemalloc_reentrant_key));
+ ptr = PyThread_tss_get(&tracemalloc_reentrant_key);
if (ptr != NULL) {
assert(ptr == REENTRANT);
return 1;
@@ -198,15 +191,15 @@ static void
set_reentrant(int reentrant)
{
assert(reentrant == 0 || reentrant == 1);
- assert(tracemalloc_reentrant_key != -1);
+ assert(PyThread_tss_is_created(&tracemalloc_reentrant_key));
if (reentrant) {
assert(!get_reentrant());
- PyThread_set_key_value(tracemalloc_reentrant_key, REENTRANT);
+ PyThread_tss_set(&tracemalloc_reentrant_key, REENTRANT);
}
else {
assert(get_reentrant());
- PyThread_set_key_value(tracemalloc_reentrant_key, NULL);
+ PyThread_tss_set(&tracemalloc_reentrant_key, NULL);
}
}
@@ -975,8 +968,7 @@ tracemalloc_init(void)
PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &allocators.raw);
#ifdef REENTRANT_THREADLOCAL
- tracemalloc_reentrant_key = PyThread_create_key();
- if (tracemalloc_reentrant_key == -1) {
+ if (PyThread_tss_create(&tracemalloc_reentrant_key) != 0) {
#ifdef MS_WINDOWS
PyErr_SetFromWindowsErr(0);
#else
@@ -1061,8 +1053,7 @@ tracemalloc_deinit(void)
#endif
#ifdef REENTRANT_THREADLOCAL
- PyThread_delete_key(tracemalloc_reentrant_key);
- tracemalloc_reentrant_key = -1;
+ PyThread_tss_delete(&tracemalloc_reentrant_key);
#endif
Py_XDECREF(unknown_filename);