mirror of
https://github.com/python/cpython.git
synced 2025-08-31 14:07:50 +00:00
gh-128923: Use zero to indicate unassigned unique id (#128925)
In the free threading build, the per thread reference counting uses a unique id for some objects to index into the local reference count table. Use 0 instead of -1 to indicate that the id is not assigned. This avoids bugs where zero-initialized heap type objects look like they have a unique id assigned.
This commit is contained in:
parent
767c89ba7c
commit
d66c08aa75
9 changed files with 110 additions and 33 deletions
|
@ -1659,6 +1659,9 @@ _PyDict_EnablePerThreadRefcounting(PyObject *op)
|
|||
assert(PyDict_Check(op));
|
||||
#ifdef Py_GIL_DISABLED
|
||||
Py_ssize_t id = _PyObject_AssignUniqueId(op);
|
||||
if (id == _Py_INVALID_UNIQUE_ID) {
|
||||
return;
|
||||
}
|
||||
if ((uint64_t)id >= (uint64_t)DICT_UNIQUE_ID_MAX) {
|
||||
_PyObject_ReleaseUniqueId(id);
|
||||
return;
|
||||
|
@ -1666,8 +1669,7 @@ _PyDict_EnablePerThreadRefcounting(PyObject *op)
|
|||
|
||||
PyDictObject *mp = (PyDictObject *)op;
|
||||
assert((mp->_ma_watcher_tag >> DICT_UNIQUE_ID_SHIFT) == 0);
|
||||
// Plus 1 so that _ma_watcher_tag=0 represents an unassigned id
|
||||
mp->_ma_watcher_tag += ((uint64_t)id + 1) << DICT_UNIQUE_ID_SHIFT;
|
||||
mp->_ma_watcher_tag += (uint64_t)id << DICT_UNIQUE_ID_SHIFT;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue