mirror of
https://github.com/python/cpython.git
synced 2025-12-31 12:33:28 +00:00
gh-143057: avoid locking in tracemalloc C-APIs when it is not enabled (#143065)
This commit is contained in:
parent
9ded3dd4e9
commit
e728b006de
3 changed files with 16 additions and 3 deletions
|
|
@ -850,7 +850,7 @@ _PyTraceMalloc_Start(int max_nframe)
|
|||
|
||||
/* everything is ready: start tracing Python memory allocations */
|
||||
TABLES_LOCK();
|
||||
tracemalloc_config.tracing = 1;
|
||||
_Py_atomic_store_int_relaxed(&tracemalloc_config.tracing, 1);
|
||||
TABLES_UNLOCK();
|
||||
|
||||
return 0;
|
||||
|
|
@ -867,7 +867,7 @@ _PyTraceMalloc_Stop(void)
|
|||
}
|
||||
|
||||
/* stop tracing Python memory allocations */
|
||||
tracemalloc_config.tracing = 0;
|
||||
_Py_atomic_store_int_relaxed(&tracemalloc_config.tracing, 0);
|
||||
|
||||
/* unregister the hook on memory allocators */
|
||||
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &allocators.raw);
|
||||
|
|
@ -1207,6 +1207,10 @@ int
|
|||
PyTraceMalloc_Track(unsigned int domain, uintptr_t ptr,
|
||||
size_t size)
|
||||
{
|
||||
if (_Py_atomic_load_int_relaxed(&tracemalloc_config.tracing) == 0) {
|
||||
/* tracemalloc is not tracing: do nothing */
|
||||
return -2;
|
||||
}
|
||||
PyGILState_STATE gil_state = PyGILState_Ensure();
|
||||
TABLES_LOCK();
|
||||
|
||||
|
|
@ -1228,6 +1232,11 @@ PyTraceMalloc_Track(unsigned int domain, uintptr_t ptr,
|
|||
int
|
||||
PyTraceMalloc_Untrack(unsigned int domain, uintptr_t ptr)
|
||||
{
|
||||
if (_Py_atomic_load_int_relaxed(&tracemalloc_config.tracing) == 0) {
|
||||
/* tracemalloc is not tracing: do nothing */
|
||||
return -2;
|
||||
}
|
||||
|
||||
TABLES_LOCK();
|
||||
|
||||
int result;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue