Skip to content

Commit 064bc50

Browse files
committed
gh-142518: Define lock-free and per-object lock
- Add definitions of lock-free and per-object lock to the glossary - Cross-reference these from list thread safety notes - Change admonition to rubric
1 parent 25e99b3 commit 064bc50

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

Doc/glossary.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,16 @@ Glossary
951951
to locks exist such as queues, producer/consumer patterns, and
952952
thread-local state. See also :term:`deadlock`, and :term:`reentrant`.
953953

954+
lock-free
955+
An operation that does not acquire any :term:`lock` and uses atomic CPU
956+
instructions to ensure correctness. Lock-free operations can execute
957+
concurrently without blocking each other and cannot be blocked by
958+
operations that hold locks. In :term:`free-threaded <free threading>`
959+
Python, built-in types like :class:`dict` and :class:`list` provide
960+
lock-free read operations, which means other threads may observe
961+
intermediate states during multi-step modifications even when those
962+
modifications hold the :term:`per-object lock`.
963+
954964
loader
955965
An object that loads a module.
956966
It must define the :meth:`!exec_module` and :meth:`!create_module` methods
@@ -1217,6 +1227,16 @@ Glossary
12171227
<faq-argument-vs-parameter>`, the :class:`inspect.Parameter` class, the
12181228
:ref:`function` section, and :pep:`362`.
12191229

1230+
per-object lock
1231+
A :term:`lock` associated with an individual object instance rather than
1232+
a global lock shared across all objects. In :term:`free-threaded
1233+
<free threading>` Python, built-in types like :class:`dict` and
1234+
:class:`list` use per-object locks to allow concurrent operations on
1235+
different objects while serializing operations on the same object.
1236+
Operations that hold the per-object lock prevent other locking operations
1237+
on the same object from proceeding, but do not block :term:`lock-free`
1238+
operations.
1239+
12201240
path entry
12211241
A single location on the :term:`import path` which the :term:`path
12221242
based finder` consults to find modules for importing.

Doc/library/stdtypes.rst

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,7 +1441,9 @@ application).
14411441
list appear empty for the duration, and raises :exc:`ValueError` if it can
14421442
detect that the list has been mutated during a sort.
14431443

1444-
.. admonition:: Thread safety
1444+
.. _thread-safety-list:
1445+
1446+
.. rubric:: Thread safety for list objects
14451447

14461448
Reading a single element from a :class:`list` is
14471449
:term:`atomic <atomic operation>`:
@@ -1462,11 +1464,11 @@ application).
14621464
lst.index(item)
14631465
lst.count(item)
14641466
1465-
All of the above methods/operations are also lock-free. They do not block
1466-
concurrent modifications. Other operations that hold a lock will not block
1467-
these from observing intermediate states.
1467+
All of the above methods/operations are also :term:`lock-free`. They do not
1468+
block concurrent modifications. Other operations that hold a lock will not
1469+
block these from observing intermediate states.
14681470

1469-
All other operations from here on block using the per-object lock.
1471+
All other operations from here on block using the :term:`per-object lock`.
14701472

14711473
Writing a single item via ``lst[i] = x`` is safe to call from multiple
14721474
threads and will not corrupt the list.
@@ -1497,7 +1499,7 @@ application).
14971499
Other threads cannot observe intermediate states during sorting, but the
14981500
list appears empty for the duration of the sort.
14991501

1500-
The following operations may allow lock-free operations to observe
1502+
The following operations may allow :term:`lock-free` operations to observe
15011503
intermediate states since they modify multiple elements in place:
15021504

15031505
.. code-block::

0 commit comments

Comments
 (0)