mirror of
https://github.com/python/cpython.git
synced 2025-10-12 01:43:12 +00:00
gh-114271: Make PyInterpreterState.threads.count
thread-safe in free-threaded builds (gh-115093)
Use atomics to mutate PyInterpreterState.threads.count.
This commit is contained in:
parent
879f4546bf
commit
de7d67b19b
2 changed files with 4 additions and 4 deletions
|
@ -112,7 +112,7 @@ struct _is {
|
||||||
/* The thread currently executing in the __main__ module, if any. */
|
/* The thread currently executing in the __main__ module, if any. */
|
||||||
PyThreadState *main;
|
PyThreadState *main;
|
||||||
/* Used in Modules/_threadmodule.c. */
|
/* Used in Modules/_threadmodule.c. */
|
||||||
long count;
|
Py_ssize_t count;
|
||||||
/* Support for runtime thread stack size tuning.
|
/* Support for runtime thread stack size tuning.
|
||||||
A value of 0 means using the platform's default stack size
|
A value of 0 means using the platform's default stack size
|
||||||
or the size specified by the THREAD_STACK_SIZE macro. */
|
or the size specified by the THREAD_STACK_SIZE macro. */
|
||||||
|
|
|
@ -1244,7 +1244,7 @@ thread_run(void *boot_raw)
|
||||||
|
|
||||||
_PyThreadState_Bind(tstate);
|
_PyThreadState_Bind(tstate);
|
||||||
PyEval_AcquireThread(tstate);
|
PyEval_AcquireThread(tstate);
|
||||||
tstate->interp->threads.count++;
|
_Py_atomic_add_ssize(&tstate->interp->threads.count, 1);
|
||||||
|
|
||||||
PyObject *res = PyObject_Call(boot->func, boot->args, boot->kwargs);
|
PyObject *res = PyObject_Call(boot->func, boot->args, boot->kwargs);
|
||||||
if (res == NULL) {
|
if (res == NULL) {
|
||||||
|
@ -1262,7 +1262,7 @@ thread_run(void *boot_raw)
|
||||||
|
|
||||||
thread_bootstate_free(boot, 1);
|
thread_bootstate_free(boot, 1);
|
||||||
|
|
||||||
tstate->interp->threads.count--;
|
_Py_atomic_add_ssize(&tstate->interp->threads.count, -1);
|
||||||
PyThreadState_Clear(tstate);
|
PyThreadState_Clear(tstate);
|
||||||
_PyThreadState_DeleteCurrent(tstate);
|
_PyThreadState_DeleteCurrent(tstate);
|
||||||
|
|
||||||
|
@ -1539,7 +1539,7 @@ static PyObject *
|
||||||
thread__count(PyObject *self, PyObject *Py_UNUSED(ignored))
|
thread__count(PyObject *self, PyObject *Py_UNUSED(ignored))
|
||||||
{
|
{
|
||||||
PyInterpreterState *interp = _PyInterpreterState_GET();
|
PyInterpreterState *interp = _PyInterpreterState_GET();
|
||||||
return PyLong_FromLong(interp->threads.count);
|
return PyLong_FromSsize_t(_Py_atomic_load_ssize(&interp->threads.count));
|
||||||
}
|
}
|
||||||
|
|
||||||
PyDoc_STRVAR(_count_doc,
|
PyDoc_STRVAR(_count_doc,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue