PyInterpreterState *next;
int64_t id;
- int64_t id_refcount;
+ Py_ssize_t id_refcount;
int requires_idref;
- PyThread_type_lock id_mutex;
#define _PyInterpreterState_WHENCE_NOTSET -1
#define _PyInterpreterState_WHENCE_UNKNOWN 0
PyAPI_FUNC(int64_t) _PyInterpreterState_ObjectToID(PyObject *);
PyAPI_FUNC(PyInterpreterState *) _PyInterpreterState_LookUpID(int64_t);
PyAPI_FUNC(PyInterpreterState *) _PyInterpreterState_LookUpIDObject(PyObject *);
-PyAPI_FUNC(int) _PyInterpreterState_IDInitref(PyInterpreterState *);
-PyAPI_FUNC(int) _PyInterpreterState_IDIncref(PyInterpreterState *);
+PyAPI_FUNC(void) _PyInterpreterState_IDIncref(PyInterpreterState *);
PyAPI_FUNC(void) _PyInterpreterState_IDDecref(PyInterpreterState *);
PyAPI_FUNC(int) _PyInterpreterState_IsReady(PyInterpreterState *interp);
_PyTypes_AfterFork();
- /* bpo-42540: id_mutex is freed by _PyInterpreterState_Delete, which does
- * not force the default allocator. */
- if (_PyThread_at_fork_reinit(&runtime->interpreters.main->id_mutex) < 0) {
- return _PyStatus_ERR("Failed to reinitialize runtime locks");
- }
-
PyStatus status = gilstate_tss_reinit(runtime);
if (_PyStatus_EXCEPTION(status)) {
return status;
assert(id > 0 || (id == 0 && interp == runtime->interpreters.main));
interp->id = id;
+ interp->id_refcount = 0;
+
assert(runtime->interpreters.head == interp);
assert(next != NULL || (interp == runtime->interpreters.main));
interp->next = next;
}
HEAD_UNLOCK(runtime);
- if (interp->id_mutex != NULL) {
- PyThread_free_lock(interp->id_mutex);
- }
-
_Py_qsbr_fini(interp);
_PyObject_FiniState(interp);
// the "current" tstate to be set?
PyInterpreterState_Clear(interp); // XXX must activate?
zapthreads(interp);
- if (interp->id_mutex != NULL) {
- PyThread_free_lock(interp->id_mutex);
- }
PyInterpreterState *prev_interp = interp;
interp = interp->next;
free_interpreter(prev_interp);
PyObject *
_PyInterpreterState_GetIDObject(PyInterpreterState *interp)
{
- if (_PyInterpreterState_IDInitref(interp) != 0) {
- return NULL;
- };
int64_t interpid = interp->id;
if (interpid < 0) {
return NULL;
}
-int
-_PyInterpreterState_IDInitref(PyInterpreterState *interp)
-{
- if (interp->id_mutex != NULL) {
- return 0;
- }
- interp->id_mutex = PyThread_allocate_lock();
- if (interp->id_mutex == NULL) {
- PyErr_SetString(PyExc_RuntimeError,
- "failed to create init interpreter ID mutex");
- return -1;
- }
- interp->id_refcount = 0;
- return 0;
-}
-
-int
+void
_PyInterpreterState_IDIncref(PyInterpreterState *interp)
{
- if (_PyInterpreterState_IDInitref(interp) < 0) {
- return -1;
- }
-
- PyThread_acquire_lock(interp->id_mutex, WAIT_LOCK);
- interp->id_refcount += 1;
- PyThread_release_lock(interp->id_mutex);
- return 0;
+ _Py_atomic_add_ssize(&interp->id_refcount, 1);
}
void
_PyInterpreterState_IDDecref(PyInterpreterState *interp)
{
- assert(interp->id_mutex != NULL);
_PyRuntimeState *runtime = interp->runtime;
- PyThread_acquire_lock(interp->id_mutex, WAIT_LOCK);
- assert(interp->id_refcount != 0);
- interp->id_refcount -= 1;
- int64_t refcount = interp->id_refcount;
- PyThread_release_lock(interp->id_mutex);
+ Py_ssize_t refcount = _Py_atomic_add_ssize(&interp->id_refcount, -1);
- if (refcount == 0 && interp->requires_idref) {
+ if (refcount == 1 && interp->requires_idref) {
PyThreadState *tstate =
_PyThreadState_NewBound(interp, _PyThreadState_WHENCE_FINI);