Skip to content

Commit 2839ee1

Browse files
kumaraditya303vstinner
authored andcommitted
gh-142518: add thread safety docs for set C-APIs (GH-146562)
(cherry picked from commit dd88e77fad887aaf00ead1a3ba655fc00fd1dc25) Co-authored-by: Kumar Aditya <kumaraditya@python.org> Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent f36da66 commit 2839ee1

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

Doc/c-api/set.rst

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ the constructor functions work with any iterable Python object.
9292
actually iterable. The constructor is also useful for copying a set
9393
(``c=set(s)``).
9494
95+
.. note::
96+
97+
The operation is atomic on :term:`free threading <free-threaded build>`
98+
when *iterable* is a :class:`set`, :class:`frozenset`, :class:`dict` or :class:`frozendict`.
99+
95100
96101
.. c:function:: PyObject* PyFrozenSet_New(PyObject *iterable)
97102
@@ -100,6 +105,11 @@ the constructor functions work with any iterable Python object.
100105
set on success or ``NULL`` on failure. Raise :exc:`TypeError` if *iterable* is
101106
not actually iterable.
102107
108+
.. note::
109+
110+
The operation is atomic on :term:`free threading <free-threaded build>`
111+
when *iterable* is a :class:`set`, :class:`frozenset`, :class:`dict` or :class:`frozendict`.
112+
103113
104114
The following functions and macros are available for instances of :class:`set`
105115
or :class:`frozenset` or instances of their subtypes.
@@ -127,6 +137,10 @@ or :class:`frozenset` or instances of their subtypes.
127137
the *key* is unhashable. Raise :exc:`SystemError` if *anyset* is not a
128138
:class:`set`, :class:`frozenset`, or an instance of a subtype.
129139
140+
.. note::
141+
142+
The operation is atomic on :term:`free threading <free-threaded build>`
143+
when *key* is :class:`str`, :class:`int`, :class:`float`, :class:`bool` or :class:`bytes`.
130144
131145
.. c:function:: int PySet_Add(PyObject *set, PyObject *key)
132146
@@ -138,6 +152,12 @@ or :class:`frozenset` or instances of their subtypes.
138152
:exc:`SystemError` if *set* is not an instance of :class:`set` or its
139153
subtype.
140154
155+
.. note::
156+
157+
The operation is atomic on :term:`free threading <free-threaded build>`
158+
when *key* is :class:`str`, :class:`int`, :class:`float`, :class:`bool` or :class:`bytes`.
159+
160+
141161
142162
The following functions are available for instances of :class:`set` or its
143163
subtypes but not for instances of :class:`frozenset` or its subtypes.
@@ -152,6 +172,11 @@ subtypes but not for instances of :class:`frozenset` or its subtypes.
152172
temporary frozensets. Raise :exc:`SystemError` if *set* is not an
153173
instance of :class:`set` or its subtype.
154174
175+
.. note::
176+
177+
The operation is atomic on :term:`free threading <free-threaded build>`
178+
when *key* is :class:`str`, :class:`int`, :class:`float`, :class:`bool` or :class:`bytes`.
179+
155180
156181
.. c:function:: PyObject* PySet_Pop(PyObject *set)
157182
@@ -167,6 +192,12 @@ subtypes but not for instances of :class:`frozenset` or its subtypes.
167192
success. Return ``-1`` and raise :exc:`SystemError` if *set* is not an instance of
168193
:class:`set` or its subtype.
169194
195+
.. note::
196+
197+
In the :term:`free-threaded build`, the set is emptied before its entries
198+
are cleared, so other threads will observe an empty set rather than
199+
intermediate states.
200+
170201
171202
Deprecated API
172203
^^^^^^^^^^^^^^

Doc/data/threadsafety.dat

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,29 @@ PyByteArray_GET_SIZE:atomic:
125125
PyByteArray_AsString:compatible:
126126
PyByteArray_AS_STRING:compatible:
127127

128+
# Creation - may iterate the iterable argument, calling arbitrary code.
129+
# Atomic for sets, frozensets, dicts, and frozendicts.
130+
PySet_New:shared:
131+
PyFrozenSet_New:shared:
132+
133+
# Size - uses atomic load on free-threaded builds
134+
PySet_Size:atomic:
135+
PySet_GET_SIZE:atomic:
136+
137+
# Contains - lock-free, atomic with simple types
138+
PySet_Contains:shared:
139+
140+
# Mutations - hold per-object lock for duration
141+
# atomic with simple types
142+
PySet_Add:shared:
143+
PySet_Discard:shared:
144+
145+
# Pop - hold per-object lock for duration
146+
PySet_Pop:atomic:
147+
148+
# Clear - empties the set before clearing
149+
PySet_Clear:atomic:
150+
128151
# Capsule objects (Doc/c-api/capsule.rst)
129152

130153
# Type check - read ob_type pointer, always safe

0 commit comments

Comments
 (0)