diff options
author | AN Long <aisk@users.noreply.github.com> | 2024-07-01 19:11:39 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-07-01 19:11:39 (GMT) |
commit | 294e72496439da984cb8dba9100d3613c8cc8a6d (patch) | |
tree | 8b205b9af78fdd7469bb462b81b7a0b51b748754 /Include | |
parent | 9bcb7d8c6f8277c4e76145ec4c834213167e3387 (diff) | |
download | cpython-294e72496439da984cb8dba9100d3613c8cc8a6d.zip cpython-294e72496439da984cb8dba9100d3613c8cc8a6d.tar.gz cpython-294e72496439da984cb8dba9100d3613c8cc8a6d.tar.bz2 |
gh-117657: Fix data races reported by TSAN in some set methods (#120914)
Refactor the fast Unicode hash check into `_PyObject_HashFast` and use relaxed
atomic loads in the free-threaded build.
After this change, the TSAN doesn't report data races for this method.
Diffstat (limited to 'Include')
-rw-r--r-- | Include/internal/pycore_object.h | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/Include/internal/pycore_object.h b/Include/internal/pycore_object.h index 9c963d8..fa78961 100644 --- a/Include/internal/pycore_object.h +++ b/Include/internal/pycore_object.h @@ -613,6 +613,20 @@ _PyObject_IS_GC(PyObject *obj) && (type->tp_is_gc == NULL || type->tp_is_gc(obj))); } +// Fast inlined version of PyObject_Hash() +static inline Py_hash_t +_PyObject_HashFast(PyObject *op) +{ + if (PyUnicode_CheckExact(op)) { + Py_hash_t hash = FT_ATOMIC_LOAD_SSIZE_RELAXED( + _PyASCIIObject_CAST(op)->hash); + if (hash != -1) { + return hash; + } + } + return PyObject_Hash(op); +} + // Fast inlined version of PyType_IS_GC() #define _PyType_IS_GC(t) _PyType_HasFeature((t), Py_TPFLAGS_HAVE_GC) |