Skip to content

Commit 732802c

Browse files
authored
[mypyc] Document librt.threading (#21697)
1 parent 89deb7d commit 732802c

3 files changed

Lines changed: 57 additions & 0 deletions

File tree

mypyc/doc/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ generate fast code.
3535
librt_base64
3636
librt_random
3737
librt_strings
38+
librt_threading
3839
librt_time
3940
librt_vecs
4041

mypyc/doc/librt.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ Follow submodule links in the table to a detailed description of each submodule.
3030
- Pseudorandom number generation
3131
* - :doc:`librt.strings <librt_strings>`
3232
- String and bytes utilities
33+
* - :doc:`librt.threading <librt_threading>`
34+
- Threading primitives
3335
* - :doc:`librt.time <librt_time>`
3436
- Time utilities
3537
* - :doc:`librt.vecs <librt_vecs>`

mypyc/doc/librt_threading.rst

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
.. _librt-threading:
2+
3+
librt.threading
4+
===============
5+
6+
The ``librt.threading`` module is part of the ``librt`` package on PyPI, and it includes
7+
threading primitives.
8+
9+
Classes
10+
-------
11+
12+
Lock
13+
^^^^
14+
15+
.. class:: Lock
16+
17+
A fast mutual exclusion lock. This can be used as a faster replacement for
18+
:py:class:`threading.Lock` in compiled code.
19+
20+
Like :py:class:`threading.Lock`, a ``Lock`` is *unowned*: it may be released by a thread
21+
other than the one that acquired it, and it doesn't support reentrant (recursive) locking.
22+
A newly created lock is unlocked.
23+
24+
``Lock`` can be used as a context manager. The lock is acquired (blocking) on entry and
25+
released on exit, including when the body raises an exception::
26+
27+
def example(lock: Lock) -> None:
28+
with lock:
29+
... # Critical section; the lock is held here.
30+
31+
``Lock`` cannot be subclassed. ``Lock`` cannot be used with :py:class:`threading.Condition`.
32+
33+
.. method:: acquire(blocking: bool = True) -> bool
34+
35+
Acquire the lock.
36+
37+
When *blocking* is true (the default), block (if needed) until the lock is available,
38+
acquire it, and return ``True``. When *blocking* is false, acquire the lock only if it
39+
can be done without blocking: return ``True`` if the lock could be acquired, or
40+
``False`` otherwise (it was already locked by some thread).
41+
42+
Unlike :py:meth:`threading.Lock.acquire`, there is no *timeout* argument.
43+
44+
.. method:: release() -> None
45+
46+
Release the lock, allowing another thread (if any) that is blocked on :meth:`acquire`
47+
to proceed. Since the lock is unowned, it may be released from a thread other than the
48+
one that acquired it.
49+
50+
Raise :py:exc:`RuntimeError` if the lock is not currently held.
51+
52+
.. method:: locked() -> bool
53+
54+
Return ``True`` if the lock is currently held (by any thread).

0 commit comments

Comments
 (0)