Skip to content

Commit 34e9ba0

Browse files
code review 1
1 parent a569964 commit 34e9ba0

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

Modules/_threadmodule.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,8 @@ rlock_acquire_restore(PyObject *op, PyObject *args)
10761076
return NULL;
10771077

10781078
_PyRecursiveMutex_Lock(&self->lock);
1079+
self->lock.thread = owner;
1080+
self->lock.level = count - 1;
10791081
Py_RETURN_NONE;
10801082
}
10811083

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

1093-
PyThread_ident_t owner = _Py_atomic_load_ullong_relaxed(&self->lock.thread);
1094-
size_t count = _Py_atomic_load_ulong_relaxed(&self->lock.level);
1095-
if (_PyRecursiveMutex_TryUnlock(&self->lock) < 0) {
1095+
PyThread_ident_t owner = self->lock.thread;
1096+
size_t count = self->lock.level + 1;
1097+
1098+
if (_PyRecursiveMutex_TryUnlock(&self->lock) < 0)
1099+
{
10961100
PyErr_SetString(PyExc_RuntimeError,
10971101
"cannot release un-acquired lock");
10981102
return NULL;
@@ -1150,8 +1154,8 @@ static PyObject *
11501154
rlock_repr(PyObject *op)
11511155
{
11521156
rlockobject *self = (rlockobject*)op;
1153-
PyThread_ident_t owner = _Py_atomic_load_ullong_relaxed(&self->lock.thread);
1154-
size_t count = _Py_atomic_load_ulong_relaxed(&self->lock.level);
1157+
PyThread_ident_t owner = self->lock.thread;
1158+
size_t count = self->lock.level + 1;
11551159
return PyUnicode_FromFormat(
11561160
"<%s %s object owner=%" PY_FORMAT_THREAD_IDENT_T " count=%zu at %p>",
11571161
owner ? "locked" : "unlocked",

Python/lock.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -397,11 +397,13 @@ void
397397
_PyRecursiveMutex_Unlock(_PyRecursiveMutex *m)
398398
{
399399
if (_PyRecursiveMutex_TryUnlock(m) < 0) {
400-
Py_FatalError("cannot unlock un-acquired lock");
400+
Py_FatalError("unlocking a recursive mutex that is not "
401+
"owned by the current thread");
401402
}
402403
}
403404

404-
int _PyRecursiveMutex_TryUnlock(_PyRecursiveMutex *m)
405+
int
406+
_PyRecursiveMutex_TryUnlock(_PyRecursiveMutex *m)
405407
{
406408
PyThread_ident_t thread = PyThread_get_thread_ident_ex();
407409
if (!recursive_mutex_is_owned_by(m, thread)) {
@@ -412,10 +414,10 @@ int _PyRecursiveMutex_TryUnlock(_PyRecursiveMutex *m)
412414
return 0;
413415
}
414416
assert(m->level == 0);
415-
if (_PyMutex_TryUnlock(&m->mutex) < 0){
416-
return -1;
417-
};
418417
_Py_atomic_store_ullong_relaxed(&m->thread, 0);
418+
if (_PyMutex_TryUnlock(&m->mutex) < 0) {
419+
return -1;
420+
}
419421
return 0;
420422
}
421423

0 commit comments

Comments
 (0)