Skip to content

Commit fe8c4f1

Browse files
Deploy preview for PR 1211 🛫
1 parent cae9e7f commit fe8c4f1

File tree

584 files changed

+6691
-6488
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

584 files changed

+6691
-6488
lines changed

pr-preview/pr-1211/_sources/library/threadsafety.rst.txt

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,88 @@ For general guidance on writing thread-safe code in free-threaded Python, see
1313
:ref:`freethreading-python-howto`.
1414

1515

16+
.. _threadsafety-levels:
17+
18+
Thread safety levels
19+
====================
20+
21+
The C API documentation uses the following levels to describe the thread
22+
safety guarantees of each function. The levels are listed from least to
23+
most safe.
24+
25+
.. _threadsafety-level-incompatible:
26+
27+
Incompatible
28+
------------
29+
30+
A function or operation that cannot be made safe for concurrent use even
31+
with external synchronization. Incompatible code typically accesses
32+
global state in an unsynchronized way and must only be called from a single
33+
thread throughout the program's lifetime.
34+
35+
Example: a function that modifies process-wide state such as signal handlers
36+
or environment variables, where concurrent calls from any threads, even with
37+
external locking, can conflict with the runtime or other libraries.
38+
39+
.. _threadsafety-level-compatible:
40+
41+
Compatible
42+
----------
43+
44+
A function or operation that is safe to call from multiple threads
45+
*provided* the caller supplies appropriate external synchronization, for
46+
example by holding a :term:`lock` for the duration of each call. Without
47+
such synchronization, concurrent calls may produce :term:`race conditions
48+
<race condition>` or :term:`data races <data race>`.
49+
50+
Example: a function that reads from or writes to an object whose internal
51+
state is not protected by a lock. Callers must ensure that no two threads
52+
access the same object at the same time.
53+
54+
.. _threadsafety-level-distinct:
55+
56+
Safe on distinct objects
57+
------------------------
58+
59+
A function or operation that is safe to call from multiple threads without
60+
external synchronization, as long as each thread operates on a **different**
61+
object. Two threads may call the function at the same time, but they must
62+
not pass the same object (or objects that share underlying state) as
63+
arguments.
64+
65+
Example: a function that modifies fields of a struct using non-atomic
66+
writes. Two threads can each call the function on their own struct
67+
instance safely, but concurrent calls on the *same* instance require
68+
external synchronization.
69+
70+
.. _threadsafety-level-shared:
71+
72+
Safe on shared objects
73+
----------------------
74+
75+
A function or operation that is safe for concurrent use on the **same**
76+
object. The implementation uses internal synchronization (such as
77+
:term:`per-object locks <per-object lock>` or
78+
:ref:`critical sections <python-critical-section-api>`) to protect shared
79+
mutable state, so callers do not need to supply their own locking.
80+
81+
Example: :c:func:`PyList_GetItemRef` can be called from multiple threads on the
82+
same :c:type:`PyListObject` - it uses internal synchronization to serialize
83+
access.
84+
85+
.. _threadsafety-level-atomic:
86+
87+
Atomic
88+
------
89+
90+
A function or operation that appears :term:`atomic <atomic operation>` with
91+
respect to other threads - it executes instantaneously from the perspective
92+
of other threads. This is the strongest form of thread safety.
93+
94+
Example: :c:func:`PyMutex_IsLocked` performs an atomic read of the mutex
95+
state and can be called from any thread at any time.
96+
97+
1698
.. _thread-safety-list:
1799

18100
Thread safety for list objects

pr-preview/pr-1211/_static/pydoctheme.css

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,9 @@ div.footer a:hover {
448448
:root {
449449
--threadsafety-incompatible: var(--bad-color);
450450
--threadsafety-compatible: var(--middle-color);
451-
--threadsafety-safe: var(--good-color);
451+
--threadsafety-distinct: var(--middle-color);
452+
--threadsafety-shared: var(--good-color);
453+
--threadsafety-atomic: var(--good-color);
452454
}
453455

454456
.threadsafety.threadsafety-incompatible {
@@ -459,8 +461,16 @@ div.footer a:hover {
459461
color: var(--threadsafety-compatible);
460462
}
461463

462-
.threadsafety.threadsafety-safe {
463-
color: var(--threadsafety-safe);
464+
.threadsafety.threadsafety-distinct {
465+
color: var(--threadsafety-distinct);
466+
}
467+
468+
.threadsafety.threadsafety-shared {
469+
color: var(--threadsafety-shared);
470+
}
471+
472+
.threadsafety.threadsafety-atomic {
473+
color: var(--threadsafety-atomic);
464474
}
465475

466476

pr-preview/pr-1211/about.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<link rel="stylesheet" type="text/css" href="_static/pygments.css?v=b86133f3" />
1111
<link rel="stylesheet" type="text/css" href="_static/classic.css?v=234b1a7c" />
12-
<link rel="stylesheet" type="text/css" href="_static/pydoctheme.css?v=89a2f22a" />
12+
<link rel="stylesheet" type="text/css" href="_static/pydoctheme.css?v=82640b3f" />
1313
<link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="_static/pygments_dark.css?v=5349f25f" />
1414

1515
<script src="_static/documentation_options.js?v=e2c088d7"></script>
@@ -356,7 +356,7 @@ <h3>導航</h3>
356356
<a href="https://www.python.org/psf/donations/">敬請捐贈。</a>
357357
<br>
358358
<br>
359-
最後更新於 3月 12, 2026 (00:22 UTC)。
359+
最後更新於 3月 13, 2026 (00:24 UTC)。
360360

361361
<a href="/bugs.html">發現 bug</a>
362362

pr-preview/pr-1211/bugs.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<link rel="stylesheet" type="text/css" href="_static/pygments.css?v=b86133f3" />
1111
<link rel="stylesheet" type="text/css" href="_static/classic.css?v=234b1a7c" />
12-
<link rel="stylesheet" type="text/css" href="_static/pydoctheme.css?v=89a2f22a" />
12+
<link rel="stylesheet" type="text/css" href="_static/pydoctheme.css?v=82640b3f" />
1313
<link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="_static/pygments_dark.css?v=5349f25f" />
1414

1515
<script src="_static/documentation_options.js?v=e2c088d7"></script>
@@ -393,7 +393,7 @@ <h3>導航</h3>
393393
<a href="https://www.python.org/psf/donations/">敬請捐贈。</a>
394394
<br>
395395
<br>
396-
最後更新於 3月 12, 2026 (00:22 UTC)。
396+
最後更新於 3月 13, 2026 (00:24 UTC)。
397397

398398
<a href="/bugs.html">發現 bug</a>
399399

pr-preview/pr-1211/c-api/abstract.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />
1111
<link rel="stylesheet" type="text/css" href="../_static/classic.css?v=234b1a7c" />
12-
<link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=89a2f22a" />
12+
<link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=82640b3f" />
1313
<link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=5349f25f" />
1414

1515
<script src="../_static/documentation_options.js?v=e2c088d7"></script>
@@ -365,7 +365,7 @@ <h3>導航</h3>
365365
<a href="https://www.python.org/psf/donations/">敬請捐贈。</a>
366366
<br>
367367
<br>
368-
最後更新於 3月 12, 2026 (00:22 UTC)。
368+
最後更新於 3月 13, 2026 (00:24 UTC)。
369369

370370
<a href="/bugs.html">發現 bug</a>
371371

pr-preview/pr-1211/c-api/allocation.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />
1111
<link rel="stylesheet" type="text/css" href="../_static/classic.css?v=234b1a7c" />
12-
<link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=89a2f22a" />
12+
<link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=82640b3f" />
1313
<link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=5349f25f" />
1414

1515
<script src="../_static/documentation_options.js?v=e2c088d7"></script>
@@ -574,7 +574,7 @@ <h3>導航</h3>
574574
<a href="https://www.python.org/psf/donations/">敬請捐贈。</a>
575575
<br>
576576
<br>
577-
最後更新於 3月 12, 2026 (00:22 UTC)。
577+
最後更新於 3月 13, 2026 (00:24 UTC)。
578578

579579
<a href="/bugs.html">發現 bug</a>
580580

pr-preview/pr-1211/c-api/apiabiversion.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />
1111
<link rel="stylesheet" type="text/css" href="../_static/classic.css?v=234b1a7c" />
12-
<link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=89a2f22a" />
12+
<link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=82640b3f" />
1313
<link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=5349f25f" />
1414

1515
<script src="../_static/documentation_options.js?v=e2c088d7"></script>
@@ -514,7 +514,7 @@ <h3>導航</h3>
514514
<a href="https://www.python.org/psf/donations/">敬請捐贈。</a>
515515
<br>
516516
<br>
517-
最後更新於 3月 12, 2026 (00:22 UTC)。
517+
最後更新於 3月 13, 2026 (00:24 UTC)。
518518

519519
<a href="/bugs.html">發現 bug</a>
520520

pr-preview/pr-1211/c-api/arg.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />
1111
<link rel="stylesheet" type="text/css" href="../_static/classic.css?v=234b1a7c" />
12-
<link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=89a2f22a" />
12+
<link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=82640b3f" />
1313
<link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=5349f25f" />
1414

1515
<script src="../_static/documentation_options.js?v=e2c088d7"></script>
@@ -996,7 +996,7 @@ <h3>導航</h3>
996996
<a href="https://www.python.org/psf/donations/">敬請捐贈。</a>
997997
<br>
998998
<br>
999-
最後更新於 3月 12, 2026 (00:22 UTC)。
999+
最後更新於 3月 13, 2026 (00:24 UTC)。
10001000

10011001
<a href="/bugs.html">發現 bug</a>
10021002

pr-preview/pr-1211/c-api/bool.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />
1111
<link rel="stylesheet" type="text/css" href="../_static/classic.css?v=234b1a7c" />
12-
<link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=89a2f22a" />
12+
<link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=82640b3f" />
1313
<link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=5349f25f" />
1414

1515
<script src="../_static/documentation_options.js?v=e2c088d7"></script>
@@ -376,7 +376,7 @@ <h3>導航</h3>
376376
<a href="https://www.python.org/psf/donations/">敬請捐贈。</a>
377377
<br>
378378
<br>
379-
最後更新於 3月 12, 2026 (00:22 UTC)。
379+
最後更新於 3月 13, 2026 (00:24 UTC)。
380380

381381
<a href="/bugs.html">發現 bug</a>
382382

pr-preview/pr-1211/c-api/buffer.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />
1111
<link rel="stylesheet" type="text/css" href="../_static/classic.css?v=234b1a7c" />
12-
<link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=89a2f22a" />
12+
<link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=82640b3f" />
1313
<link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=5349f25f" />
1414

1515
<script src="../_static/documentation_options.js?v=e2c088d7"></script>
@@ -1064,7 +1064,7 @@ <h3>導航</h3>
10641064
<a href="https://www.python.org/psf/donations/">敬請捐贈。</a>
10651065
<br>
10661066
<br>
1067-
最後更新於 3月 12, 2026 (00:22 UTC)。
1067+
最後更新於 3月 13, 2026 (00:24 UTC)。
10681068

10691069
<a href="/bugs.html">發現 bug</a>
10701070

0 commit comments

Comments
 (0)