Skip to content

Commit

Permalink
code review 1
Browse files Browse the repository at this point in the history
  • Loading branch information
kumaraditya303 authored Oct 8, 2024
1 parent a569964 commit 34e9ba0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
14 changes: 9 additions & 5 deletions Modules/_threadmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,8 @@ rlock_acquire_restore(PyObject *op, PyObject *args)
return NULL;

_PyRecursiveMutex_Lock(&self->lock);
self->lock.thread = owner;
self->lock.level = count - 1;
Py_RETURN_NONE;
}

Expand All @@ -1090,9 +1092,11 @@ rlock_release_save(PyObject *op, PyObject *Py_UNUSED(ignored))
{
rlockobject *self = (rlockobject*)op;

PyThread_ident_t owner = _Py_atomic_load_ullong_relaxed(&self->lock.thread);
size_t count = _Py_atomic_load_ulong_relaxed(&self->lock.level);
if (_PyRecursiveMutex_TryUnlock(&self->lock) < 0) {
PyThread_ident_t owner = self->lock.thread;
size_t count = self->lock.level + 1;

if (_PyRecursiveMutex_TryUnlock(&self->lock) < 0)
{
PyErr_SetString(PyExc_RuntimeError,
"cannot release un-acquired lock");
return NULL;
Expand Down Expand Up @@ -1150,8 +1154,8 @@ static PyObject *
rlock_repr(PyObject *op)
{
rlockobject *self = (rlockobject*)op;
PyThread_ident_t owner = _Py_atomic_load_ullong_relaxed(&self->lock.thread);
size_t count = _Py_atomic_load_ulong_relaxed(&self->lock.level);
PyThread_ident_t owner = self->lock.thread;
size_t count = self->lock.level + 1;
return PyUnicode_FromFormat(
"<%s %s object owner=%" PY_FORMAT_THREAD_IDENT_T " count=%zu at %p>",
owner ? "locked" : "unlocked",
Expand Down
12 changes: 7 additions & 5 deletions Python/lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -397,11 +397,13 @@ void
_PyRecursiveMutex_Unlock(_PyRecursiveMutex *m)
{
if (_PyRecursiveMutex_TryUnlock(m) < 0) {
Py_FatalError("cannot unlock un-acquired lock");
Py_FatalError("unlocking a recursive mutex that is not "
"owned by the current thread");
}
}

int _PyRecursiveMutex_TryUnlock(_PyRecursiveMutex *m)
int
_PyRecursiveMutex_TryUnlock(_PyRecursiveMutex *m)
{
PyThread_ident_t thread = PyThread_get_thread_ident_ex();
if (!recursive_mutex_is_owned_by(m, thread)) {
Expand All @@ -412,10 +414,10 @@ int _PyRecursiveMutex_TryUnlock(_PyRecursiveMutex *m)
return 0;
}
assert(m->level == 0);
if (_PyMutex_TryUnlock(&m->mutex) < 0){
return -1;
};
_Py_atomic_store_ullong_relaxed(&m->thread, 0);
if (_PyMutex_TryUnlock(&m->mutex) < 0) {
return -1;
}
return 0;
}

Expand Down

0 comments on commit 34e9ba0

Please sign in to comment.