diff --git a/Include/internal/pycore_lock.h b/Include/internal/pycore_lock.h index 2a18bb7644725f..7b6412b7771882 100644 --- a/Include/internal/pycore_lock.h +++ b/Include/internal/pycore_lock.h @@ -13,6 +13,8 @@ extern "C" { # error "this header requires Py_BUILD_CORE define" #endif +#include "pycore_pythread.h" // for PyThread_ident_t + //_Py_UNLOCKED is defined as 0 and _Py_LOCKED as 1 in Include/cpython/lock.h #define _Py_HAS_PARKED 2 #define _Py_ONCE_INITIALIZED 4 @@ -154,7 +156,7 @@ _PyOnceFlag_CallOnce(_PyOnceFlag *flag, _Py_once_fn_t *fn, void *arg) // A recursive mutex. The mutex should zero-initialized. typedef struct { PyMutex mutex; - unsigned long long thread; // i.e., PyThread_get_thread_ident_ex() + PyThread_ident_t thread; // i.e., PyThread_get_thread_ident_ex() size_t level; } _PyRecursiveMutex; diff --git a/Include/internal/pycore_pythread.h b/Include/internal/pycore_pythread.h index 3610c6254db6af..d3cb04182050e0 100644 --- a/Include/internal/pycore_pythread.h +++ b/Include/internal/pycore_pythread.h @@ -116,7 +116,7 @@ PyAPI_FUNC(PyLockStatus) PyThread_acquire_lock_timed_with_retries( PyThread_type_lock, PY_TIMEOUT_T microseconds); -typedef unsigned long long PyThread_ident_t; +typedef size_t PyThread_ident_t; typedef Py_uintptr_t PyThread_handle_t; #define PY_FORMAT_THREAD_IDENT_T "llu" diff --git a/Modules/_threadmodule.c b/Modules/_threadmodule.c index 39d309729d88b8..a7535b86e93c41 100644 --- a/Modules/_threadmodule.c +++ b/Modules/_threadmodule.c @@ -1023,8 +1023,8 @@ rlock_dealloc(rlockobject *self) static bool rlock_is_owned_by(rlockobject *self, PyThread_ident_t tid) { - PyThread_ident_t owner_tid = - _Py_atomic_load_ullong_relaxed(&self->rlock_owner); + PyThread_ident_t owner_tid = (PyThread_ident_t) + _Py_atomic_load_ssize_relaxed((const Py_ssize_t *) &self->rlock_owner); return owner_tid == tid && self->rlock_count > 0; } @@ -1052,7 +1052,7 @@ rlock_acquire(rlockobject *self, PyObject *args, PyObject *kwds) r = acquire_timed(self->rlock_lock, timeout); if (r == PY_LOCK_ACQUIRED) { assert(self->rlock_count == 0); - _Py_atomic_store_ullong_relaxed(&self->rlock_owner, tid); + _Py_atomic_store_ssize_relaxed((Py_ssize_t *) &self->rlock_owner, tid); self->rlock_count = 1; } else if (r == PY_LOCK_INTR) { @@ -1096,7 +1096,7 @@ rlock_release(rlockobject *self, PyObject *Py_UNUSED(ignored)) return NULL; } if (--self->rlock_count == 0) { - _Py_atomic_store_ullong_relaxed(&self->rlock_owner, 0); + _Py_atomic_store_ssize_relaxed((Py_ssize_t *) &self->rlock_owner, 0); PyThread_release_lock(self->rlock_lock); } Py_RETURN_NONE; @@ -1142,7 +1142,7 @@ rlock_acquire_restore(rlockobject *self, PyObject *args) return NULL; } assert(self->rlock_count == 0); - _Py_atomic_store_ullong_relaxed(&self->rlock_owner, owner); + _Py_atomic_store_ssize_relaxed((Py_ssize_t *) &self->rlock_owner, owner); self->rlock_count = count; Py_RETURN_NONE; } @@ -1168,7 +1168,7 @@ rlock_release_save(rlockobject *self, PyObject *Py_UNUSED(ignored)) owner = self->rlock_owner; count = self->rlock_count; self->rlock_count = 0; - _Py_atomic_store_ullong_relaxed(&self->rlock_owner, 0); + _Py_atomic_store_ssize_relaxed((Py_ssize_t *) &self->rlock_owner, 0); PyThread_release_lock(self->rlock_lock); return Py_BuildValue("k" Py_PARSE_THREAD_IDENT_T, count, owner); } @@ -1183,8 +1183,8 @@ static PyObject * rlock_recursion_count(rlockobject *self, PyObject *Py_UNUSED(ignored)) { PyThread_ident_t tid = PyThread_get_thread_ident_ex(); - PyThread_ident_t owner = - _Py_atomic_load_ullong_relaxed(&self->rlock_owner); + PyThread_ident_t owner = (PyThread_ident_t) + _Py_atomic_load_ssize_relaxed((const Py_ssize_t *) &self->rlock_owner); return PyLong_FromUnsignedLong(owner == tid ? self->rlock_count : 0UL); } @@ -1234,8 +1234,8 @@ rlock_new(PyTypeObject *type, PyObject *args, PyObject *kwds) static PyObject * rlock_repr(rlockobject *self) { - PyThread_ident_t owner = - _Py_atomic_load_ullong_relaxed(&self->rlock_owner); + PyThread_ident_t owner = (PyThread_ident_t) + _Py_atomic_load_ssize_relaxed((const Py_ssize_t *) &self->rlock_owner); return PyUnicode_FromFormat( "<%s %s object owner=%" PY_FORMAT_THREAD_IDENT_T " count=%lu at %p>", self->rlock_count ? "locked" : "unlocked", @@ -2331,7 +2331,7 @@ thread__make_thread_handle(PyObject *module, PyObject *identobj) PyErr_SetString(PyExc_TypeError, "ident must be an integer"); return NULL; } - PyThread_ident_t ident = PyLong_AsUnsignedLongLong(identobj); + PyThread_ident_t ident = (PyThread_ident_t) PyLong_AsSsize_t(identobj); if (PyErr_Occurred()) { return NULL; } diff --git a/Python/lock.c b/Python/lock.c index 57675fe1873fa2..f8768b30fcd437 100644 --- a/Python/lock.c +++ b/Python/lock.c @@ -355,7 +355,8 @@ _PyOnceFlag_CallOnceSlow(_PyOnceFlag *flag, _Py_once_fn_t *fn, void *arg) static int recursive_mutex_is_owned_by(_PyRecursiveMutex *m, PyThread_ident_t tid) { - return _Py_atomic_load_ullong_relaxed(&m->thread) == tid; + return tid == (PyThread_ident_t) _Py_atomic_load_ssize_relaxed( + (const Py_ssize_t *) &m->thread); } int @@ -373,7 +374,7 @@ _PyRecursiveMutex_Lock(_PyRecursiveMutex *m) return; } PyMutex_Lock(&m->mutex); - _Py_atomic_store_ullong_relaxed(&m->thread, thread); + _Py_atomic_store_ssize_relaxed((Py_ssize_t*) &m->thread, thread); assert(m->level == 0); } @@ -390,7 +391,7 @@ _PyRecursiveMutex_Unlock(_PyRecursiveMutex *m) return; } assert(m->level == 0); - _Py_atomic_store_ullong_relaxed(&m->thread, 0); + _Py_atomic_store_ssize_relaxed((Py_ssize_t *) &m->thread, 0); PyMutex_Unlock(&m->mutex); }