Skip to content

Commit d06bc5c

Browse files
mpageSonicField
authored andcommitted
pythongh-117657: Disable the function/code cache in free-threaded builds (python#118301)
This is only used by the specializing interpreter and the tier 2 optimizer, both of which are disabled in free-threaded builds.
1 parent 4670d66 commit d06bc5c

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

Include/internal/pycore_function.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
extern "C" {
55
#endif
66

7+
#include "pycore_lock.h"
8+
79
#ifndef Py_BUILD_CORE
810
# error "this header requires Py_BUILD_CORE define"
911
#endif
@@ -24,6 +26,11 @@ struct _func_version_cache_item {
2426
};
2527

2628
struct _py_func_state {
29+
#ifdef Py_GIL_DISABLED
30+
// Protects next_version
31+
PyMutex mutex;
32+
#endif
33+
2734
uint32_t next_version;
2835
// Borrowed references to function and code objects whose
2936
// func_version % FUNC_VERSION_CACHE_SIZE

Objects/codeobject.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,10 +416,16 @@ init_code(PyCodeObject *co, struct _PyCodeConstructor *con)
416416
co->co_ncellvars = ncellvars;
417417
co->co_nfreevars = nfreevars;
418418
PyInterpreterState *interp = _PyInterpreterState_GET();
419+
#ifdef Py_GIL_DISABLED
420+
PyMutex_Lock(&interp->func_state.mutex);
421+
#endif
419422
co->co_version = interp->func_state.next_version;
420423
if (interp->func_state.next_version != 0) {
421424
interp->func_state.next_version++;
422425
}
426+
#ifdef Py_GIL_DISABLED
427+
PyMutex_Unlock(&interp->func_state.mutex);
428+
#endif
423429
co->_co_monitoring = NULL;
424430
co->_co_instrumentation_version = 0;
425431
/* not set */

Objects/funcobject.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ functions is running.
287287
void
288288
_PyFunction_SetVersion(PyFunctionObject *func, uint32_t version)
289289
{
290+
#ifndef Py_GIL_DISABLED
290291
PyInterpreterState *interp = _PyInterpreterState_GET();
291292
if (func->func_version != 0) {
292293
struct _func_version_cache_item *slot =
@@ -297,19 +298,23 @@ _PyFunction_SetVersion(PyFunctionObject *func, uint32_t version)
297298
// Leave slot->code alone, there may be use for it.
298299
}
299300
}
301+
#endif
300302
func->func_version = version;
303+
#ifndef Py_GIL_DISABLED
301304
if (version != 0) {
302305
struct _func_version_cache_item *slot =
303306
interp->func_state.func_version_cache
304307
+ (version % FUNC_VERSION_CACHE_SIZE);
305308
slot->func = func;
306309
slot->code = func->func_code;
307310
}
311+
#endif
308312
}
309313

310314
void
311315
_PyFunction_ClearCodeByVersion(uint32_t version)
312316
{
317+
#ifndef Py_GIL_DISABLED
313318
PyInterpreterState *interp = _PyInterpreterState_GET();
314319
struct _func_version_cache_item *slot =
315320
interp->func_state.func_version_cache
@@ -322,11 +327,15 @@ _PyFunction_ClearCodeByVersion(uint32_t version)
322327
slot->func = NULL;
323328
}
324329
}
330+
#endif
325331
}
326332

327333
PyFunctionObject *
328334
_PyFunction_LookupByVersion(uint32_t version, PyObject **p_code)
329335
{
336+
#ifdef Py_GIL_DISABLED
337+
return NULL;
338+
#else
330339
PyInterpreterState *interp = _PyInterpreterState_GET();
331340
struct _func_version_cache_item *slot =
332341
interp->func_state.func_version_cache
@@ -346,6 +355,7 @@ _PyFunction_LookupByVersion(uint32_t version, PyObject **p_code)
346355
return slot->func;
347356
}
348357
return NULL;
358+
#endif
349359
}
350360

351361
uint32_t

Tools/tsan/suppressions_free_threading.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ race:_add_to_weak_set
1515
race:_in_weak_set
1616
race:_mi_heap_delayed_free_partial
1717
race:_PyEval_EvalFrameDefault
18-
race:_PyFunction_SetVersion
1918
race:_PyImport_AcquireLock
2019
race:_PyImport_ReleaseLock
2120
race:_PyInterpreterState_SetNotRunningMain

0 commit comments

Comments
 (0)