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

gh-123983: Redefine PyThread_ident_t as a size_t, and also use it. #123984

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion Include/internal/pycore_lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion Include/internal/pycore_pythread.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
22 changes: 11 additions & 11 deletions Modules/_threadmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
}
Expand All @@ -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);
}

Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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;
}
Expand Down
7 changes: 4 additions & 3 deletions Python/lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}

Expand All @@ -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);
}

Expand Down
Loading