mirror of https://github.com/python/cpython
[3.13] gh-121546: Disable contextvar caching on free-threading build (GH-121740) (#121808)
gh-121546: Disable contextvar caching on free-threading build (GH-121740)
(cherry picked from commit e904300882
)
Co-authored-by: Ken Jin <kenjin@python.org>
This commit is contained in:
parent
b506de4eb5
commit
0a634e3702
|
@ -35,9 +35,11 @@ struct _pycontextvarobject {
|
||||||
PyObject_HEAD
|
PyObject_HEAD
|
||||||
PyObject *var_name;
|
PyObject *var_name;
|
||||||
PyObject *var_default;
|
PyObject *var_default;
|
||||||
|
#ifndef Py_GIL_DISABLED
|
||||||
PyObject *var_cached;
|
PyObject *var_cached;
|
||||||
uint64_t var_cached_tsid;
|
uint64_t var_cached_tsid;
|
||||||
uint64_t var_cached_tsver;
|
uint64_t var_cached_tsver;
|
||||||
|
#endif
|
||||||
Py_hash_t var_hash;
|
Py_hash_t var_hash;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -203,6 +203,7 @@ PyContextVar_Get(PyObject *ovar, PyObject *def, PyObject **val)
|
||||||
goto not_found;
|
goto not_found;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifndef Py_GIL_DISABLED
|
||||||
if (var->var_cached != NULL &&
|
if (var->var_cached != NULL &&
|
||||||
var->var_cached_tsid == ts->id &&
|
var->var_cached_tsid == ts->id &&
|
||||||
var->var_cached_tsver == ts->context_ver)
|
var->var_cached_tsver == ts->context_ver)
|
||||||
|
@ -210,6 +211,7 @@ PyContextVar_Get(PyObject *ovar, PyObject *def, PyObject **val)
|
||||||
*val = var->var_cached;
|
*val = var->var_cached;
|
||||||
goto found;
|
goto found;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
assert(PyContext_CheckExact(ts->context));
|
assert(PyContext_CheckExact(ts->context));
|
||||||
PyHamtObject *vars = ((PyContext *)ts->context)->ctx_vars;
|
PyHamtObject *vars = ((PyContext *)ts->context)->ctx_vars;
|
||||||
|
@ -221,9 +223,11 @@ PyContextVar_Get(PyObject *ovar, PyObject *def, PyObject **val)
|
||||||
}
|
}
|
||||||
if (res == 1) {
|
if (res == 1) {
|
||||||
assert(found != NULL);
|
assert(found != NULL);
|
||||||
|
#ifndef Py_GIL_DISABLED
|
||||||
var->var_cached = found; /* borrow */
|
var->var_cached = found; /* borrow */
|
||||||
var->var_cached_tsid = ts->id;
|
var->var_cached_tsid = ts->id;
|
||||||
var->var_cached_tsver = ts->context_ver;
|
var->var_cached_tsver = ts->context_ver;
|
||||||
|
#endif
|
||||||
|
|
||||||
*val = found;
|
*val = found;
|
||||||
goto found;
|
goto found;
|
||||||
|
@ -723,8 +727,10 @@ PyTypeObject PyContext_Type = {
|
||||||
static int
|
static int
|
||||||
contextvar_set(PyContextVar *var, PyObject *val)
|
contextvar_set(PyContextVar *var, PyObject *val)
|
||||||
{
|
{
|
||||||
|
#ifndef Py_GIL_DISABLED
|
||||||
var->var_cached = NULL;
|
var->var_cached = NULL;
|
||||||
PyThreadState *ts = _PyThreadState_GET();
|
PyThreadState *ts = _PyThreadState_GET();
|
||||||
|
#endif
|
||||||
|
|
||||||
PyContext *ctx = context_get();
|
PyContext *ctx = context_get();
|
||||||
if (ctx == NULL) {
|
if (ctx == NULL) {
|
||||||
|
@ -739,16 +745,20 @@ contextvar_set(PyContextVar *var, PyObject *val)
|
||||||
|
|
||||||
Py_SETREF(ctx->ctx_vars, new_vars);
|
Py_SETREF(ctx->ctx_vars, new_vars);
|
||||||
|
|
||||||
|
#ifndef Py_GIL_DISABLED
|
||||||
var->var_cached = val; /* borrow */
|
var->var_cached = val; /* borrow */
|
||||||
var->var_cached_tsid = ts->id;
|
var->var_cached_tsid = ts->id;
|
||||||
var->var_cached_tsver = ts->context_ver;
|
var->var_cached_tsver = ts->context_ver;
|
||||||
|
#endif
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
contextvar_del(PyContextVar *var)
|
contextvar_del(PyContextVar *var)
|
||||||
{
|
{
|
||||||
|
#ifndef Py_GIL_DISABLED
|
||||||
var->var_cached = NULL;
|
var->var_cached = NULL;
|
||||||
|
#endif
|
||||||
|
|
||||||
PyContext *ctx = context_get();
|
PyContext *ctx = context_get();
|
||||||
if (ctx == NULL) {
|
if (ctx == NULL) {
|
||||||
|
@ -823,9 +833,11 @@ contextvar_new(PyObject *name, PyObject *def)
|
||||||
|
|
||||||
var->var_default = Py_XNewRef(def);
|
var->var_default = Py_XNewRef(def);
|
||||||
|
|
||||||
|
#ifndef Py_GIL_DISABLED
|
||||||
var->var_cached = NULL;
|
var->var_cached = NULL;
|
||||||
var->var_cached_tsid = 0;
|
var->var_cached_tsid = 0;
|
||||||
var->var_cached_tsver = 0;
|
var->var_cached_tsver = 0;
|
||||||
|
#endif
|
||||||
|
|
||||||
if (_PyObject_GC_MAY_BE_TRACKED(name) ||
|
if (_PyObject_GC_MAY_BE_TRACKED(name) ||
|
||||||
(def != NULL && _PyObject_GC_MAY_BE_TRACKED(def)))
|
(def != NULL && _PyObject_GC_MAY_BE_TRACKED(def)))
|
||||||
|
@ -863,9 +875,11 @@ contextvar_tp_clear(PyContextVar *self)
|
||||||
{
|
{
|
||||||
Py_CLEAR(self->var_name);
|
Py_CLEAR(self->var_name);
|
||||||
Py_CLEAR(self->var_default);
|
Py_CLEAR(self->var_default);
|
||||||
|
#ifndef Py_GIL_DISABLED
|
||||||
self->var_cached = NULL;
|
self->var_cached = NULL;
|
||||||
self->var_cached_tsid = 0;
|
self->var_cached_tsid = 0;
|
||||||
self->var_cached_tsver = 0;
|
self->var_cached_tsver = 0;
|
||||||
|
#endif
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue