Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.13] gh-117721: use PyMutex in _thread.lock (#125110) #125116

Merged
merged 2 commits into from
Oct 8, 2024
Merged
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
54 changes: 10 additions & 44 deletions Modules/_threadmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -701,9 +701,7 @@ static PyType_Spec ThreadHandle_Type_spec = {

typedef struct {
PyObject_HEAD
PyThread_type_lock lock_lock;
PyObject *in_weakreflist;
char locked; /* for sanity checking */
PyMutex lock;
} lockobject;

static int
Expand All @@ -717,15 +715,7 @@ static void
lock_dealloc(lockobject *self)
{
PyObject_GC_UnTrack(self);
if (self->in_weakreflist != NULL) {
PyObject_ClearWeakRefs((PyObject *) self);
}
if (self->lock_lock != NULL) {
/* Unlock the lock so it's safe to free it */
if (self->locked)
PyThread_release_lock(self->lock_lock);
PyThread_free_lock(self->lock_lock);
}
PyObject_ClearWeakRefs((PyObject *) self);
PyTypeObject *tp = Py_TYPE(self);
tp->tp_free((PyObject*)self);
Py_DECREF(tp);
Expand Down Expand Up @@ -790,13 +780,12 @@ lock_PyThread_acquire_lock(lockobject *self, PyObject *args, PyObject *kwds)
if (lock_acquire_parse_args(args, kwds, &timeout) < 0)
return NULL;

PyLockStatus r = acquire_timed(self->lock_lock, timeout);
PyLockStatus r = _PyMutex_LockTimed(&self->lock, timeout,
_PY_LOCK_HANDLE_SIGNALS | _PY_LOCK_DETACH);
if (r == PY_LOCK_INTR) {
return NULL;
}

if (r == PY_LOCK_ACQUIRED)
self->locked = 1;
return PyBool_FromLong(r == PY_LOCK_ACQUIRED);
}

Expand Down Expand Up @@ -827,13 +816,11 @@ static PyObject *
lock_PyThread_release_lock(lockobject *self, PyObject *Py_UNUSED(ignored))
{
/* Sanity check: the lock must be locked */
if (!self->locked) {
if (_PyMutex_TryUnlock(&self->lock) < 0) {
PyErr_SetString(ThreadError, "release unlocked lock");
return NULL;
}

self->locked = 0;
PyThread_release_lock(self->lock_lock);
Py_RETURN_NONE;
}

Expand All @@ -860,7 +847,7 @@ Release the lock.");
static PyObject *
lock_locked_lock(lockobject *self, PyObject *Py_UNUSED(ignored))
{
return PyBool_FromLong((long)self->locked);
return PyBool_FromLong(PyMutex_IsLocked(&self->lock));
}

PyDoc_STRVAR(locked_doc,
Expand All @@ -879,20 +866,14 @@ static PyObject *
lock_repr(lockobject *self)
{
return PyUnicode_FromFormat("<%s %s object at %p>",
self->locked ? "locked" : "unlocked", Py_TYPE(self)->tp_name, self);
PyMutex_IsLocked(&self->lock) ? "locked" : "unlocked", Py_TYPE(self)->tp_name, self);
}

#ifdef HAVE_FORK
static PyObject *
lock__at_fork_reinit(lockobject *self, PyObject *Py_UNUSED(args))
{
if (_PyThread_at_fork_reinit(&self->lock_lock) < 0) {
PyErr_SetString(ThreadError, "failed to reinitialize lock at fork");
return NULL;
}

self->locked = 0;

_PyMutex_at_fork_reinit(&self->lock);
Py_RETURN_NONE;
}
#endif /* HAVE_FORK */
Expand Down Expand Up @@ -958,18 +939,12 @@ A lock is not owned by the thread that locked it; another thread may\n\
unlock it. A thread attempting to lock a lock that it has already locked\n\
will block until another thread unlocks it. Deadlocks may ensue.");

static PyMemberDef lock_type_members[] = {
{"__weaklistoffset__", Py_T_PYSSIZET, offsetof(lockobject, in_weakreflist), Py_READONLY},
{NULL},
};

static PyType_Slot lock_type_slots[] = {
{Py_tp_dealloc, (destructor)lock_dealloc},
{Py_tp_repr, (reprfunc)lock_repr},
{Py_tp_doc, (void *)lock_doc},
{Py_tp_methods, lock_methods},
{Py_tp_traverse, lock_traverse},
{Py_tp_members, lock_type_members},
{Py_tp_new, lock_new},
{0, 0}
};
Expand All @@ -978,7 +953,7 @@ static PyType_Spec lock_type_spec = {
.name = "_thread.lock",
.basicsize = sizeof(lockobject),
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Py_TPFLAGS_IMMUTABLETYPE),
Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_MANAGED_WEAKREF),
.slots = lock_type_slots,
};

Expand Down Expand Up @@ -1320,16 +1295,7 @@ newlockobject(PyObject *module)
if (self == NULL) {
return NULL;
}

self->lock_lock = PyThread_allocate_lock();
self->locked = 0;
self->in_weakreflist = NULL;

if (self->lock_lock == NULL) {
Py_DECREF(self);
PyErr_SetString(ThreadError, "can't allocate lock");
return NULL;
}
self->lock = (PyMutex){0};
return self;
}

Expand Down
Loading