Commit 4a066475 authored by Victor Stinner's avatar Victor Stinner

Add assertions on tracemalloc_reentrant_key

Issue #26588.
parent 0cfc058d
...@@ -167,7 +167,7 @@ tracemalloc_error(const char *format, ...) ...@@ -167,7 +167,7 @@ tracemalloc_error(const char *format, ...)
# error "need native thread local storage (TLS)" # error "need native thread local storage (TLS)"
#endif #endif
static int tracemalloc_reentrant_key; static int tracemalloc_reentrant_key = -1;
/* Any non-NULL pointer can be used */ /* Any non-NULL pointer can be used */
#define REENTRANT Py_True #define REENTRANT Py_True
...@@ -175,7 +175,10 @@ static int tracemalloc_reentrant_key; ...@@ -175,7 +175,10 @@ static int tracemalloc_reentrant_key;
static int static int
get_reentrant(void) get_reentrant(void)
{ {
void *ptr = PyThread_get_key_value(tracemalloc_reentrant_key); void *ptr;
assert(tracemalloc_reentrant_key != -1);
ptr = PyThread_get_key_value(tracemalloc_reentrant_key);
if (ptr != NULL) { if (ptr != NULL) {
assert(ptr == REENTRANT); assert(ptr == REENTRANT);
return 1; return 1;
...@@ -188,6 +191,8 @@ static void ...@@ -188,6 +191,8 @@ static void
set_reentrant(int reentrant) set_reentrant(int reentrant)
{ {
assert(reentrant == 0 || reentrant == 1); assert(reentrant == 0 || reentrant == 1);
assert(tracemalloc_reentrant_key != -1);
if (reentrant) { if (reentrant) {
assert(!get_reentrant()); assert(!get_reentrant());
PyThread_set_key_value(tracemalloc_reentrant_key, REENTRANT); PyThread_set_key_value(tracemalloc_reentrant_key, REENTRANT);
...@@ -1018,6 +1023,7 @@ DEBUG("tracemalloc_deinit(): exit (not initialized)"); ...@@ -1018,6 +1023,7 @@ DEBUG("tracemalloc_deinit(): exit (not initialized)");
DEBUG("tracemalloc_deinit(): delete reentrant key"); DEBUG("tracemalloc_deinit(): delete reentrant key");
#ifdef REENTRANT_THREADLOCAL #ifdef REENTRANT_THREADLOCAL
PyThread_delete_key(tracemalloc_reentrant_key); PyThread_delete_key(tracemalloc_reentrant_key);
tracemalloc_reentrant_key = -1;
#endif #endif
Py_XDECREF(unknown_filename); Py_XDECREF(unknown_filename);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment