gh-117657: Disable the function/code cache in free-threaded builds by mpage · Pull Request #118301 · python/cpython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Include/internal/pycore_function.h
6 changes: 6 additions & 0 deletions Objects/codeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -416,10 +416,16 @@ init_code(PyCodeObject *co, struct _PyCodeConstructor *con)
co->co_ncellvars = ncellvars;
co->co_nfreevars = nfreevars;
PyInterpreterState *interp = _PyInterpreterState_GET();
#ifdef Py_GIL_DISABLED
PyMutex_Lock(&interp->func_state.mutex);
#endif
co->co_version = interp->func_state.next_version;
if (interp->func_state.next_version != 0) {
interp->func_state.next_version++;
}
#ifdef Py_GIL_DISABLED
PyMutex_Unlock(&interp->func_state.mutex);
#endif
co->_co_monitoring = NULL;
co->_co_instrumentation_version = 0;
/* not set */
Expand Down
10 changes: 10 additions & 0 deletions Objects/funcobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ functions is running.
void
_PyFunction_SetVersion(PyFunctionObject *func, uint32_t version)
{
#ifndef Py_GIL_DISABLED
PyInterpreterState *interp = _PyInterpreterState_GET();
if (func->func_version != 0) {
struct _func_version_cache_item *slot =
Expand All @@ -297,19 +298,23 @@ _PyFunction_SetVersion(PyFunctionObject *func, uint32_t version)
// Leave slot->code alone, there may be use for it.
}
}
#endif
func->func_version = version;
#ifndef Py_GIL_DISABLED
if (version != 0) {
struct _func_version_cache_item *slot =
interp->func_state.func_version_cache
+ (version % FUNC_VERSION_CACHE_SIZE);
slot->func = func;
slot->code = func->func_code;
}
#endif
}

void
_PyFunction_ClearCodeByVersion(uint32_t version)
{
#ifndef Py_GIL_DISABLED
PyInterpreterState *interp = _PyInterpreterState_GET();
struct _func_version_cache_item *slot =
interp->func_state.func_version_cache
Expand All @@ -322,11 +327,15 @@ _PyFunction_ClearCodeByVersion(uint32_t version)
slot->func = NULL;
}
}
#endif
}

PyFunctionObject *
_PyFunction_LookupByVersion(uint32_t version, PyObject **p_code)
{
#ifdef Py_GIL_DISABLED
return NULL;
#else
PyInterpreterState *interp = _PyInterpreterState_GET();
struct _func_version_cache_item *slot =
interp->func_state.func_version_cache
Expand All @@ -346,6 +355,7 @@ _PyFunction_LookupByVersion(uint32_t version, PyObject **p_code)
return slot->func;
}
return NULL;
#endif
}

uint32_t
Expand Down
1 change: 0 additions & 1 deletion Tools/tsan/suppressions_free_threading.txt