summaryrefslogtreecommitdiffstats
path: root/Modules/_tracemalloc.c
diff options
context:
space:
mode:
authorMasayuki Yamamoto <ma3yuki.8mamo10@gmail.com>2017-10-06 10:41:34 (GMT)
committerNick Coghlan <ncoghlan@gmail.com>2017-10-06 10:41:34 (GMT)
commit731e18901484c75b60167a06a0ba0719a6d4827d (patch)
treefc9b8afc6eb8453729c130a146b838228ab103c6 /Modules/_tracemalloc.c
parentb8ab9d3fc816f85f4d6dbef12b7414e6dc10e4dd (diff)
downloadcpython-731e18901484c75b60167a06a0ba0719a6d4827d.zip
cpython-731e18901484c75b60167a06a0ba0719a6d4827d.tar.gz
cpython-731e18901484c75b60167a06a0ba0719a6d4827d.tar.bz2
bpo-25658: Implement PEP 539 for Thread Specific Storage (TSS) API (GH-1362)
See PEP 539 for details. Highlights of changes: - Add Thread Specific Storage (TSS) API - Document the Thread Local Storage (TLS) API as deprecated - Update code that used TLS API to use TSS API
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 386f2f1..af2a2fa 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);