8000 [3.13] gh-117721: use PyMutex in `_thread.lock` (#125110) by kumaraditya303 · Pull Request #125116 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

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

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

Merged
merged 2 commits into from
Oct 8, 2024
Merged
Changes from 1 commit
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
Next Next commit
gh-117721: use PyMutex in _thread.lock (#125110)
(cherry picked from commit fca5529)
  • Loading branch information
kumaraditya303 committed Oct 8, 2024
commit 99899b0dd710bf38541be320b311f5284ae19ace
56 changes: 12 additions & 44 deletions Modules/_threadmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -701,9 +701,7 @@

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 @@
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 @@
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 @@
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,8 @@
static PyObject *
lock_locked_lock(lockobject *self, PyObject *Py_UNUSED(ignored))
{
return PyBool_FromLong((long)self->locked);
lockobject *self = (lockobject*)op;

Check failure on line 850 in Modules/_threadmodule.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test (ubuntu-22.04)

‘self’ redeclared as different kind of symbol

Check failure on line 850 in Modules/_threadmodule.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test (ubuntu-22.04)

‘op’ undeclared (first use in this function)

Check failure on line 850 in Modules/_threadmodule.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (ubuntu-22.04, 3.0.15)

‘self’ redeclared as different kind of symbol

Check failure on line 850 in Modules/_threadmodule.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (ubuntu-22.04, 3.0.15)

‘op’ undeclared (first use in this function)

Check failure on line 850 in Modules/_threadmodule.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (ubuntu-22.04, 3.1.7)

‘self’ redeclared as different kind of symbol

Check failure on line 850 in Modules/_threadmodule.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (ubuntu-22.04, 3.1.7)

‘op’ undeclared (first use in this function)

Check failure on line 850 in Modules/_threadmodule.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (ubuntu-22.04, 3.3.2)

‘self’ redeclared as different kind of symbol

Check failure on line 850 in Modules/_threadmodule.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (ubuntu-22.04, 3.3.2)

‘op’ undeclared (first use in this function)

Check failure on line 850 in Modules/_threadmodule.c

View workflow job for this annotation

GitHub Actions / Hypothesis tests on Ubuntu

‘self’ redeclared as different kind of symbol

Check failure on line 850 in Modules/_threadmodule.c

View workflow job for this annotation

GitHub Actions / Hypothesis tests on Ubuntu

‘op’ undeclared (first use in this function)

Check failure on line 850 in Modules/_threadmodule.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (ubuntu-22.04, 3.2.3)

‘self’ redeclared as different kind of symbol

Check failure on line 850 in Modules/_threadmodule.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (ubuntu-22.04, 3.2.3)

‘op’ undeclared (first use in this function)

Check failure on line 850 in Modules/_threadmodule.c

View workflow job for this annotation

GitHub Actions / Address sanitizer

‘self’ redeclared as different kind of symbol

Check failure on line 850 in Modules/_threadmodule.c

View workflow job for this annotation

GitHub Actions / Address sanitizer

‘op’ undeclared (first use in this function)

Check failure on line 850 in Modules/_threadmodule.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / build (arm64)

redefinition of formal parameter 'self' [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check failure on line 850 in Modules/_threadmodule.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / build (arm64)

'op': undeclared identifier [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 850 in Modules/_threadmodule.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / build (arm64)

'type cast': conversion from 'int' to 'lockobject *' of greater size [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check failure on line 850 in Modules/_threadmodule.c

View workflow job for this annotation

GitHub Actions / Windows / build (arm64)

redefinition of formal parameter 'self' [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check failure on line 850 in Modules/_threadmodule.c

View workflow job for this annotation

GitHub Actions / Windows / build (arm64)

'op': undeclared identifier [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 850 in Modules/_threadmodule.c

View workflow job for this annotation

GitHub Actions / Windows / build (arm64)

'type cast': conversion from 'int' to 'lockobject *' of greater size [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check failure on line 850 in Modules/_threadmodule.c

View workflow job for this annotation

GitHub Actions / Windows / build and test (x64)

redefinition of formal parameter 'self' [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check failure on line 850 in Modules/_threadmodule.c

View workflow job for this annotation

GitHub Actions / Windows / build and test (x64)

'op': undeclared identifier [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 850 in Modules/_threadmodule.c

View workflow job for this annotation

GitHub Actions / Windows / build and test (x64)

'type cast': conversion from 'int' to 'lockobject *' of greater size [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check failure on line 850 in Modules/_threadmodule.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / build and test (x64)

redefinition of formal parameter 'self' [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check failure on line 850 in Modules/_threadmodule.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / build and test (x64)

'op': undeclared identifier [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 850 in Modules/_threadmodule.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / build and test (x64)

'type cast': conversion from 'int' to 'lockobject *' of greater size [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]
return PyBool_FromLong(PyMutex_IsLocked(&self->lock));
}

PyDoc_STRVAR(locked_doc,
Expand All @@ -879,20 +867,15 @@
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;

lockobject *self = (lockobject *)op;

Check failure on line 877 in Modules/_threadmodule.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test (ubuntu-22.04)

‘self’ redeclared as different kind of symbol

Check failure on line 877 in Modules/_threadmodule.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test (ubuntu-22.04)

‘op’ undeclared (first use in this function)

Check failure on line 877 in Modules/_threadmodule.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (ubuntu-22.04, 3.0.15)

‘self’ redeclared as different kind of symbol

Check failure on line 877 in Modules/_threadmodule.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (ubuntu-22.04, 3.0.15)

‘op’ undeclared (first use in this function)

Check failure on line 877 in Modules/_threadmodule.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (ubuntu-22.04, 3.1.7)

‘self’ redeclared as different kind of symbol

Check failure on line 877 in Modules/_threadmodule.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (ubuntu-22.04, 3.1.7)

‘op’ undeclared (first use in this function)

Check failure on line 877 in Modules/_threadmodule.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (ubuntu-22.04, 3.3.2)

‘self’ redeclared as different kind of symbol

Check failure on line 877 in Modules/_threadmodule.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (ubuntu-22.04, 3.3.2)

‘op’ undeclared (first use in this function)

Check failure on line 877 in Modules/_threadmodule.c

View workflow job for this annotation

GitHub Actions / Hypothesis tests on Ubuntu

‘self’ redeclared as different kind of symbol

Check failure on line 877 in Modules/_threadmodule.c

View workflow job for this annotation

GitHub Actions / Hypothesis tests on Ubuntu

‘op’ undeclared (first use in this function)

Check failure on line 877 in Modules/_threadmodule.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (ubuntu-22.04, 3.2.3)

‘self’ redeclared as different kind of symbol

Check failure on line 877 in Modules/_threadmodule.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (ubuntu-22.04, 3.2.3)

‘op’ undeclared (first use in this function)

Check failure on line 877 in Modules/_threadmodule.c

View workflow job for this annotation

GitHub Actions / Address sanitizer

‘self’ redeclared as different kind of symbol

Check failure on line 877 in Modules/_threadmodule.c

View workflow job for this annotation

GitHub Actions / Address sanitizer

‘op’ undeclared (first use in this function)
_PyMutex_at_fork_reinit(&self->lock);
Py_RETURN_NONE;
}
#endif /* HAVE_FORK */
Expand Down Expand Up @@ -958,18 +941,12 @@
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 +955,7 @@
.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 +1297,7 @@
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
0