summaryrefslogtreecommitdiffstats
path: root/Doc/c-api
diff options
context:
space:
mode:
authorSam Gross <colesbury@gmail.com>2024-06-24 18:15:15 (GMT)
committerGitHub <noreply@github.com>2024-06-24 18:15:15 (GMT)
commit375b723d5873f948696c7e85a97f4778d9e00ff0 (patch)
tree081f3b0fffac79d58c7b1e3cc81e8a9bdc2e3373 /Doc/c-api
parentdee63cb35971b87a09ddda5d6f29cd941f570720 (diff)
downloadcpython-375b723d5873f948696c7e85a97f4778d9e00ff0.zip
cpython-375b723d5873f948696c7e85a97f4778d9e00ff0.tar.gz
cpython-375b723d5873f948696c7e85a97f4778d9e00ff0.tar.bz2
gh-120858: PyDict_Next should not lock the dict (#120859)
PyDict_Next no longer locks the dictionary in the free-threaded build. Locking around individual PyDict_Next calls is not sufficient because the function returns borrowed references and because it allows concurrent modifications during the iteraiton loop. The internal locking also interferes with correct external synchronization because it may suspend outer critical sections created by the caller.
Diffstat (limited to 'Doc/c-api')
-rw-r--r--Doc/c-api/dict.rst11
1 files changed, 11 insertions, 0 deletions
diff --git a/Doc/c-api/dict.rst b/Doc/c-api/dict.rst
index 49a7858..b4fdf47 100644
--- a/Doc/c-api/dict.rst
+++ b/Doc/c-api/dict.rst
@@ -290,6 +290,17 @@ Dictionary Objects
Py_DECREF(o);
}
+ The function is not thread-safe in the :term:`free-threaded <free threading>`
+ build without external synchronization. You can use
+ :c:macro:`Py_BEGIN_CRITICAL_SECTION` to lock the dictionary while iterating
+ over it::
+
+ Py_BEGIN_CRITICAL_SECTION(self->dict);
+ while (PyDict_Next(self->dict, &pos, &key, &value)) {
+ ...
+ }
+ Py_END_CRITICAL_SECTION();
+
.. c:function:: int PyDict_Merge(PyObject *a, PyObject *b, int override)