Visitar URL original
gh-112075: Iterating a dict shouldn't require locks by DinoV · Pull Request #115108 · python/cpython · GitHub
Skip to content
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
Prev Previous commit
Next Next commit
Use has_unique_reference
  • Loading branch information
DinoV committed Feb 22, 2024
commit 0ba2f5d439f693ee2dd8085de9e6eaef649e10e7
28 changes: 13 additions & 15 deletions Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -5428,13 +5428,23 @@ dictiter_iternext_threadsafe(PyDictObject *d, PyObject *self,

#endif

static bool
has_unique_reference(PyObject *op)
{
#ifdef Py_GIL_DISABLED
return (_Py_IsOwnedByCurrentThread(op) &&
op->ob_ref_local == 1 &&
_Py_atomic_load_ssize_relaxed(&op->ob_ref_shared) == 0);
#else
return Py_REFCNT(op) == 1;
#endif
}

static bool
acquire_iter_result(PyObject *result)
{
#ifdef Py_GIL_DISABLED
if (_Py_IsOwnedByCurrentThread(result) &&
result->ob_ref_local == 1 &&
_Py_atomic_load_ssize_relaxed(&result->ob_ref_shared) == 0) {
if (has_unique_reference(result)) {
#else
if (Py_REFCNT(result) == 1) {
#endif
Expand Down Expand Up @@ -6647,18 +6657,6 @@ _PyObject_MakeDictFromInstanceAttributes(PyObject *obj, PyDictValues *values)
return make_dict_from_instance_attributes(interp, keys, values);
}

static bool
has_unique_reference(PyObject *op)
{
#ifdef Py_GIL_DISABLED
return (_Py_IsOwnedByCurrentThread(op) &&
op->ob_ref_local == 1 &&
_Py_atomic_load_ssize_relaxed(&op->ob_ref_shared) == 0);
#else
return Py_REFCNT(op) == 1;
#endif
}

// Return true if the dict was dematerialized, false otherwise.
bool
_PyObject_MakeInstanceAttributesFromDict(PyObject *obj, PyDictOrValues *dorv)
Expand Down