Skip to content

Commit 0e3d1b8

Browse files
miss-islingtonZeroIntensityvstinner
authored
[3.13] gh-141004: Document missing PyThread* APIs (GH-141810) (GH-141955)
* gh-141004: Document missing `PyThread*` APIs (GH-141810) (cherry picked from commit a89ee4b) Co-authored-by: Peter Bierma <zintensitydev@gmail.com> Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent fffa6c9 commit 0e3d1b8

File tree

1 file changed

+215
-0
lines changed

1 file changed

+215
-0
lines changed

Doc/c-api/init.rst

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2544,3 +2544,218 @@ code triggered by the finalizer blocks and calls :c:func:`PyEval_SaveThread`.
25442544
In the default build, this macro expands to ``}``.
25452545
25462546
.. versionadded:: 3.13
2547+
2548+
2549+
Legacy Locking APIs
2550+
-------------------
2551+
2552+
These APIs are obsolete since Python 3.13 with the introduction of
2553+
:c:type:`PyMutex`.
2554+
2555+
.. versionchanged:: 3.15
2556+
These APIs are now a simple wrapper around ``PyMutex``.
2557+
2558+
2559+
.. c:type:: PyThread_type_lock
2560+
2561+
A pointer to a mutual exclusion lock.
2562+
2563+
2564+
.. c:type:: PyLockStatus
2565+
2566+
The result of acquiring a lock with a timeout.
2567+
2568+
.. c:namespace:: NULL
2569+
2570+
.. c:enumerator:: PY_LOCK_FAILURE
2571+
2572+
Failed to acquire the lock.
2573+
2574+
.. c:enumerator:: PY_LOCK_ACQUIRED
2575+
2576+
The lock was successfully acquired.
2577+
2578+
.. c:enumerator:: PY_LOCK_INTR
2579+
2580+
The lock was interrupted by a signal.
2581+
2582+
2583+
.. c:function:: PyThread_type_lock PyThread_allocate_lock(void)
2584+
2585+
Allocate a new lock.
2586+
2587+
On success, this function returns a lock; on failure, this
2588+
function returns ``0`` without an exception set.
2589+
2590+
The caller does not need to hold the :term:`GIL`.
2591+
2592+
.. versionchanged:: 3.15
2593+
This function now always uses :c:type:`PyMutex`. In prior versions, this
2594+
would use a lock provided by the operating system.
2595+
2596+
2597+
.. c:function:: void PyThread_free_lock(PyThread_type_lock lock)
2598+
2599+
Destroy *lock*. The lock should not be held by any thread when calling
2600+
this.
2601+
2602+
The caller does not need to hold the :term:`GIL`.
2603+
2604+
2605+
.. c:function:: PyLockStatus PyThread_acquire_lock_timed(PyThread_type_lock lock, long long microseconds, int intr_flag)
2606+
2607+
Acquire *lock* with a timeout.
2608+
2609+
This will wait for *microseconds* microseconds to acquire the lock. If the
2610+
timeout expires, this function returns :c:enumerator:`PY_LOCK_FAILURE`.
2611+
If *microseconds* is ``-1``, this will wait indefinitely until the lock has
2612+
been released.
2613+
2614+
If *intr_flag* is ``1``, acquiring the lock may be interrupted by a signal,
2615+
in which case this function returns :c:enumerator:`PY_LOCK_INTR`. Upon
2616+
interruption, it's generally expected that the caller makes a call to
2617+
:c:func:`Py_MakePendingCalls` to propagate an exception to Python code.
2618+
2619+
If the lock is successfully acquired, this function returns
2620+
:c:enumerator:`PY_LOCK_ACQUIRED`.
2621+
2622+
The caller does not need to hold the :term:`GIL`.
2623+
2624+
2625+
.. c:function:: int PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
2626+
2627+
Acquire *lock*.
2628+
2629+
If *waitflag* is ``1`` and another thread currently holds the lock, this
2630+
function will wait until the lock can be acquired and will always return
2631+
``1``.
2632+
2633+
If *waitflag* is ``0`` and another thread holds the lock, this function will
2634+
not wait and instead return ``0``. If the lock is not held by any other
2635+
thread, then this function will acquire it and return ``1``.
2636+
2637+
Unlike :c:func:`PyThread_acquire_lock_timed`, acquiring the lock cannot be
2638+
interrupted by a signal.
2639+
2640+
The caller does not need to hold the :term:`GIL`.
2641+
2642+
2643+
.. c:function:: int PyThread_release_lock(PyThread_type_lock lock)
2644+
2645+
Release *lock*. If *lock* is not held, then this function issues a
2646+
fatal error.
2647+
2648+
The caller does not need to hold the :term:`GIL`.
2649+
2650+
2651+
Operating System Thread APIs
2652+
============================
2653+
2654+
.. c:macro:: PYTHREAD_INVALID_THREAD_ID
2655+
2656+
Sentinel value for an invalid thread ID.
2657+
2658+
This is currently equivalent to ``(unsigned long)-1``.
2659+
2660+
2661+
.. c:function:: unsigned long PyThread_start_new_thread(void (*func)(void *), void *arg)
2662+
2663+
Start function *func* in a new thread with argument *arg*.
2664+
The resulting thread is not intended to be joined.
2665+
2666+
*func* must not be ``NULL``, but *arg* may be ``NULL``.
2667+
2668+
On success, this function returns the identifier of the new thread; on failure,
2669+
this returns :c:macro:`PYTHREAD_INVALID_THREAD_ID`.
2670+
2671+
The caller does not need to hold the :term:`GIL`.
2672+
2673+
2674+
.. c:function:: unsigned long PyThread_get_thread_ident(void)
2675+
2676+
Return the identifier of the current thread, which will never be zero.
2677+
2678+
This function cannot fail, and the caller does not need to hold the
2679+
:term:`GIL`.
2680+
2681+
.. seealso::
2682+
:py:func:`threading.get_ident`
2683+
2684+
2685+
.. c:function:: PyObject *PyThread_GetInfo(void)
2686+
2687+
Get general information about the current thread in the form of a
2688+
:ref:`struct sequence <struct-sequence-objects>` object. This information is
2689+
accessible as :py:attr:`sys.thread_info` in Python.
2690+
2691+
On success, this returns a new :term:`strong reference` to the thread
2692+
information; on failure, this returns ``NULL`` with an exception set.
2693+
2694+
The caller must hold the :term:`GIL`.
2695+
2696+
2697+
.. c:macro:: PY_HAVE_THREAD_NATIVE_ID
2698+
2699+
This macro is defined when the system supports native thread IDs.
2700+
2701+
2702+
.. c:function:: unsigned long PyThread_get_thread_native_id(void)
2703+
2704+
Get the native identifier of the current thread as it was assigned by the operating
2705+
system's kernel, which will never be less than zero.
2706+
2707+
This function is only available when :c:macro:`PY_HAVE_THREAD_NATIVE_ID` is
2708+
defined.
2709+
2710+
This function cannot fail, and the caller does not need to hold the
2711+
:term:`GIL`.
2712+
2713+
.. seealso::
2714+
:py:func:`threading.get_native_id`
2715+
2716+
2717+
.. c:function:: void PyThread_exit_thread(void)
2718+
2719+
Terminate the current thread. This function is generally considered unsafe
2720+
and should be avoided. It is kept solely for backwards compatibility.
2721+
2722+
This function is only safe to call if all functions in the full call
2723+
stack are written to safely allow it.
2724+
2725+
.. warning::
2726+
2727+
If the current system uses POSIX threads (also known as "pthreads"),
2728+
this calls :manpage:`pthread_exit(3)`, which attempts to unwind the stack
2729+
and call C++ destructors on some libc implementations. However, if a
2730+
``noexcept`` function is reached, it may terminate the process.
2731+
Other systems, such as macOS, do unwinding.
2732+
2733+
On Windows, this function calls ``_endthreadex()``, which kills the thread
2734+
without calling C++ destructors.
2735+
2736+
In any case, there is a risk of corruption on the thread's stack.
2737+
2738+
2739+
.. c:function:: void PyThread_init_thread(void)
2740+
2741+
Initialize ``PyThread*`` APIs. Python executes this function automatically,
2742+
so there's little need to call it from an extension module.
2743+
2744+
2745+
.. c:function:: int PyThread_set_stacksize(size_t size)
2746+
2747+
Set the stack size of the current thread to *size* bytes.
2748+
2749+
This function returns ``0`` on success, ``-1`` if *size* is invalid, or
2750+
``-2`` if the system does not support changing the stack size. This function
2751+
does not set exceptions.
2752+
2753+
The caller does not need to hold the :term:`GIL`.
2754+
2755+
2756+
.. c:function:: size_t PyThread_get_stacksize(void)
2757+
2758+
Return the stack size of the current thread in bytes, or ``0`` if the system's
2759+
default stack size is in use.
2760+
2761+
The caller does not need to hold the :term:`GIL`.

0 commit comments

Comments
 (0)