#include "Python.h"
#include "pycore_interp.h"
#include "pycore_lock.h"
#include "pycore_moduleobject.h"
#include "pycore_modsupport.h"
#include "pycore_pylifecycle.h"
#include "pycore_pystate.h"
#include "pycore_sysmodule.h"
#include "pycore_time.h"
#include "pycore_weakref.h"
#include <stddef.h>
#ifdef HAVE_SIGNAL_H
# include <signal.h>
#endif
#define ThreadError …
static struct PyModuleDef thread_module;
thread_module_state;
static inline thread_module_state*
get_thread_state(PyObject *module)
{ … }
ThreadHandleState;
ThreadHandle;
static inline int
get_thread_handle_state(ThreadHandle *handle)
{ … }
static inline void
set_thread_handle_state(ThreadHandle *handle, ThreadHandleState state)
{ … }
static PyThread_ident_t
ThreadHandle_ident(ThreadHandle *handle)
{ … }
static int
ThreadHandle_get_os_handle(ThreadHandle *handle, PyThread_handle_t *os_handle)
{ … }
static void
add_to_shutdown_handles(thread_module_state *state, ThreadHandle *handle)
{ … }
static void
clear_shutdown_handles(thread_module_state *state)
{ … }
static void
remove_from_shutdown_handles(ThreadHandle *handle)
{ … }
static ThreadHandle *
ThreadHandle_new(void)
{ … }
static void
ThreadHandle_incref(ThreadHandle *self)
{ … }
static int
detach_thread(ThreadHandle *self)
{ … }
static void
ThreadHandle_decref(ThreadHandle *self)
{ … }
void
_PyThread_AfterFork(struct _pythread_runtime_state *state)
{ … }
struct bootstate { … };
static void
thread_bootstate_free(struct bootstate *boot, int decref)
{ … }
static void
thread_run(void *boot_raw)
{ … }
static int
force_done(ThreadHandle *handle)
{ … }
static int
ThreadHandle_start(ThreadHandle *self, PyObject *func, PyObject *args,
PyObject *kwargs)
{ … }
static int
join_thread(ThreadHandle *handle)
{ … }
static int
check_started(ThreadHandle *self)
{ … }
static int
ThreadHandle_join(ThreadHandle *self, PyTime_t timeout_ns)
{ … }
static int
set_done(ThreadHandle *handle)
{ … }
static int
ThreadHandle_set_done(ThreadHandle *self)
{ … }
PyThreadHandleObject;
static PyThreadHandleObject *
PyThreadHandleObject_new(PyTypeObject *type)
{ … }
static PyObject *
PyThreadHandleObject_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{ … }
static int
PyThreadHandleObject_traverse(PyThreadHandleObject *self, visitproc visit,
void *arg)
{ … }
static void
PyThreadHandleObject_dealloc(PyObject *op)
{ … }
static PyObject *
PyThreadHandleObject_repr(PyObject *op)
{ … }
static PyObject *
PyThreadHandleObject_get_ident(PyObject *op, void *Py_UNUSED(ignored))
{ … }
static PyObject *
PyThreadHandleObject_join(PyObject *op, PyObject *args)
{ … }
static PyObject *
PyThreadHandleObject_is_done(PyObject *op, PyObject *Py_UNUSED(ignored))
{ … }
static PyObject *
PyThreadHandleObject_set_done(PyObject *op, PyObject *Py_UNUSED(ignored))
{ … }
static PyGetSetDef ThreadHandle_getsetlist[] = …;
static PyMethodDef ThreadHandle_methods[] = …;
static PyType_Slot ThreadHandle_Type_slots[] = …;
static PyType_Spec ThreadHandle_Type_spec = …;
lockobject;
static int
lock_traverse(PyObject *self, visitproc visit, void *arg)
{ … }
static void
lock_dealloc(PyObject *op)
{ … }
static int
lock_acquire_parse_args(PyObject *args, PyObject *kwds,
PyTime_t *timeout)
{ … }
static PyObject *
lock_PyThread_acquire_lock(PyObject *op, PyObject *args, PyObject *kwds)
{ … }
PyDoc_STRVAR(acquire_doc,
"acquire($self, /, blocking=True, timeout=-1)\n\
--\n\
\n\
Lock the lock. Without argument, this blocks if the lock is already\n\
locked (even by the same thread), waiting for another thread to release\n\
the lock, and return True once the lock is acquired.\n\
With an argument, this will only block if the argument is true,\n\
and the return value reflects whether the lock is acquired.\n\
The blocking operation is interruptible.");
PyDoc_STRVAR(acquire_lock_doc,
"acquire_lock($self, /, blocking=True, timeout=-1)\n\
--\n\
\n\
An obsolete synonym of acquire().");
PyDoc_STRVAR(enter_doc,
"__enter__($self, /)\n\
--\n\
\n\
Lock the lock.");
static PyObject *
lock_PyThread_release_lock(PyObject *op, PyObject *Py_UNUSED(ignored))
{ … }
PyDoc_STRVAR(release_doc,
"release($self, /)\n\
--\n\
\n\
Release the lock, allowing another thread that is blocked waiting for\n\
the lock to acquire the lock. The lock must be in the locked state,\n\
but it needn't be locked by the same thread that unlocks it.");
PyDoc_STRVAR(release_lock_doc,
"release_lock($self, /)\n\
--\n\
\n\
An obsolete synonym of release().");
PyDoc_STRVAR(lock_exit_doc,
"__exit__($self, /, *exc_info)\n\
--\n\
\n\
Release the lock.");
static PyObject *
lock_locked_lock(PyObject *op, PyObject *Py_UNUSED(ignored))
{ … }
PyDoc_STRVAR(locked_doc,
"locked($self, /)\n\
--\n\
\n\
Return whether the lock is in the locked state.");
PyDoc_STRVAR(locked_lock_doc,
"locked_lock($self, /)\n\
--\n\
\n\
An obsolete synonym of locked().");
static PyObject *
lock_repr(PyObject *op)
{ … }
#ifdef HAVE_FORK
static PyObject *
lock__at_fork_reinit(PyObject *op, PyObject *Py_UNUSED(args))
{ … }
#endif
static lockobject *newlockobject(PyObject *module);
static PyObject *
lock_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{ … }
static PyMethodDef lock_methods[] = …;
PyDoc_STRVAR(lock_doc,
"lock()\n\
--\n\
\n\
A lock object is a synchronization primitive. To create a lock,\n\
call threading.Lock(). Methods are:\n\
\n\
acquire() -- lock the lock, possibly blocking until it can be obtained\n\
release() -- unlock of the lock\n\
locked() -- test whether the lock is currently locked\n\
\n\
A lock is not owned by the thread that locked it; another thread may\n\
unlock it. A thread attempting to lock a lock that it has already locked\n\
will block until another thread unlocks it. Deadlocks may ensue.");
static PyType_Slot lock_type_slots[] = …;
static PyType_Spec lock_type_spec = …;
rlockobject;
static int
rlock_traverse(rlockobject *self, visitproc visit, void *arg)
{ … }
static void
rlock_dealloc(PyObject *op)
{ … }
static PyObject *
rlock_acquire(PyObject *op, PyObject *args, PyObject *kwds)
{ … }
PyDoc_STRVAR(rlock_acquire_doc,
"acquire($self, /, blocking=True, timeout=-1)\n\
--\n\
\n\
Lock the lock. `blocking` indicates whether we should wait\n\
for the lock to be available or not. If `blocking` is False\n\
and another thread holds the lock, the method will return False\n\
immediately. If `blocking` is True and another thread holds\n\
the lock, the method will wait for the lock to be released,\n\
take it and then return True.\n\
(note: the blocking operation is interruptible.)\n\
\n\
In all other cases, the method will return True immediately.\n\
Precisely, if the current thread already holds the lock, its\n\
internal counter is simply incremented. If nobody holds the lock,\n\
the lock is taken and its internal counter initialized to 1.");
PyDoc_STRVAR(rlock_enter_doc,
"__enter__($self, /)\n\
--\n\
\n\
Lock the lock.");
static PyObject *
rlock_release(PyObject *op, PyObject *Py_UNUSED(ignored))
{ … }
PyDoc_STRVAR(rlock_release_doc,
"release($self, /)\n\
--\n\
\n\
Release the lock, allowing another thread that is blocked waiting for\n\
the lock to acquire the lock. The lock must be in the locked state,\n\
and must be locked by the same thread that unlocks it; otherwise a\n\
`RuntimeError` is raised.\n\
\n\
Do note that if the lock was acquire()d several times in a row by the\n\
current thread, release() needs to be called as many times for the lock\n\
to be available for other threads.");
PyDoc_STRVAR(rlock_exit_doc,
"__exit__($self, /, *exc_info)\n\
--\n\
\n\
Release the lock.");
static PyObject *
rlock_acquire_restore(PyObject *op, PyObject *args)
{ … }
PyDoc_STRVAR(rlock_acquire_restore_doc,
"_acquire_restore($self, state, /)\n\
--\n\
\n\
For internal use by `threading.Condition`.");
static PyObject *
rlock_release_save(PyObject *op, PyObject *Py_UNUSED(ignored))
{ … }
PyDoc_STRVAR(rlock_release_save_doc,
"_release_save($self, /)\n\
--\n\
\n\
For internal use by `threading.Condition`.");
static PyObject *
rlock_recursion_count(PyObject *op, PyObject *Py_UNUSED(ignored))
{ … }
PyDoc_STRVAR(rlock_recursion_count_doc,
"_recursion_count($self, /)\n\
--\n\
\n\
For internal use by reentrancy checks.");
static PyObject *
rlock_is_owned(PyObject *op, PyObject *Py_UNUSED(ignored))
{ … }
PyDoc_STRVAR(rlock_is_owned_doc,
"_is_owned($self, /)\n\
--\n\
\n\
For internal use by `threading.Condition`.");
static PyObject *
rlock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{ … }
static PyObject *
rlock_repr(PyObject *op)
{ … }
#ifdef HAVE_FORK
static PyObject *
rlock__at_fork_reinit(rlockobject *self, PyObject *Py_UNUSED(args))
{ … }
#endif
static PyMethodDef rlock_methods[] = …;
static PyType_Slot rlock_type_slots[] = …;
static PyType_Spec rlock_type_spec = …;
static lockobject *
newlockobject(PyObject *module)
{ … }
localdummyobject;
static void
localdummy_dealloc(PyObject *op)
{ … }
static PyMemberDef local_dummy_type_members[] = …;
static PyType_Slot local_dummy_type_slots[] = …;
static PyType_Spec local_dummy_type_spec = …;
localobject;
static int create_localsdict(localobject *self, thread_module_state *state,
PyObject **localsdict, PyObject **sentinel_wr);
static PyObject *clear_locals(PyObject *meth_self, PyObject *dummyweakref);
static PyObject *
create_sentinel_wr(localobject *self)
{ … }
static PyObject *
local_new(PyTypeObject *type, PyObject *args, PyObject *kw)
{ … }
static int
local_traverse(localobject *self, visitproc visit, void *arg)
{ … }
static int
local_clear(localobject *self)
{ … }
static void
local_dealloc(localobject *self)
{ … }
static int
create_localdummies(thread_module_state *state)
{ … }
static int
create_localsdict(localobject *self, thread_module_state *state,
PyObject **localsdict, PyObject **sentinel_wr)
{ … }
static PyObject *
_ldict(localobject *self, thread_module_state *state)
{ … }
static int
local_setattro(localobject *self, PyObject *name, PyObject *v)
{ … }
static PyObject *local_getattro(localobject *, PyObject *);
static PyMemberDef local_type_members[] = …;
static PyType_Slot local_type_slots[] = …;
static PyType_Spec local_type_spec = …;
static PyObject *
local_getattro(localobject *self, PyObject *name)
{ … }
static PyObject *
clear_locals(PyObject *locals_and_key, PyObject *dummyweakref)
{ … }
static PyObject *
thread_daemon_threads_allowed(PyObject *module, PyObject *Py_UNUSED(ignored))
{ … }
PyDoc_STRVAR(daemon_threads_allowed_doc,
"daemon_threads_allowed($module, /)\n\
--\n\
\n\
Return True if daemon threads are allowed in the current interpreter,\n\
and False otherwise.\n");
static int
do_start_new_thread(thread_module_state *state, PyObject *func, PyObject *args,
PyObject *kwargs, ThreadHandle *handle, int daemon)
{ … }
static PyObject *
thread_PyThread_start_new_thread(PyObject *module, PyObject *fargs)
{ … }
PyDoc_STRVAR(start_new_thread_doc,
"start_new_thread($module, function, args, kwargs={}, /)\n\
--\n\
\n\
Start a new thread and return its identifier.\n\
\n\
The thread will call the function with positional arguments from the\n\
tuple args and keyword arguments taken from the optional dictionary\n\
kwargs. The thread exits when the function returns; the return value\n\
is ignored. The thread will also exit when the function raises an\n\
unhandled exception; a stack trace will be printed unless the exception\n\
is SystemExit.");
PyDoc_STRVAR(start_new_doc,
"start_new($module, function, args, kwargs={}, /)\n\
--\n\
\n\
An obsolete synonym of start_new_thread().");
static PyObject *
thread_PyThread_start_joinable_thread(PyObject *module, PyObject *fargs,
PyObject *fkwargs)
{ … }
PyDoc_STRVAR(start_joinable_doc,
"start_joinable_thread($module, /, function, handle=None, daemon=True)\n\
--\n\
\n\
*For internal use only*: start a new thread.\n\
\n\
Like start_new_thread(), this starts a new thread calling the given function.\n\
Unlike start_new_thread(), this returns a handle object with methods to join\n\
or detach the given thread.\n\
This function is not for third-party code, please use the\n\
`threading` module instead. During finalization the runtime will not wait for\n\
the thread to exit if daemon is True. If handle is provided it must be a\n\
newly created thread._ThreadHandle instance.");
static PyObject *
thread_PyThread_exit_thread(PyObject *self, PyObject *Py_UNUSED(ignored))
{ … }
PyDoc_STRVAR(exit_doc,
"exit($module, /)\n\
--\n\
\n\
This is synonymous to ``raise SystemExit''. It will cause the current\n\
thread to exit silently unless the exception is caught.");
PyDoc_STRVAR(exit_thread_doc,
"exit_thread($module, /)\n\
--\n\
\n\
An obsolete synonym of exit().");
static PyObject *
thread_PyThread_interrupt_main(PyObject *self, PyObject *args)
{ … }
PyDoc_STRVAR(interrupt_doc,
"interrupt_main($module, signum=signal.SIGINT, /)\n\
--\n\
\n\
Simulate the arrival of the given signal in the main thread,\n\
where the corresponding signal handler will be executed.\n\
If *signum* is omitted, SIGINT is assumed.\n\
A subthread can use this function to interrupt the main thread.\n\
\n\
Note: the default signal handler for SIGINT raises ``KeyboardInterrupt``."
);
static PyObject *
thread_PyThread_allocate_lock(PyObject *module, PyObject *Py_UNUSED(ignored))
{ … }
PyDoc_STRVAR(allocate_lock_doc,
"allocate_lock($module, /)\n\
--\n\
\n\
Create a new lock object. See help(type(threading.Lock())) for\n\
information about locks.");
PyDoc_STRVAR(allocate_doc,
"allocate($module, /)\n\
--\n\
\n\
An obsolete synonym of allocate_lock().");
static PyObject *
thread_get_ident(PyObject *self, PyObject *Py_UNUSED(ignored))
{ … }
PyDoc_STRVAR(get_ident_doc,
"get_ident($module, /)\n\
--\n\
\n\
Return a non-zero integer that uniquely identifies the current thread\n\
amongst other threads that exist simultaneously.\n\
This may be used to identify per-thread resources.\n\
Even though on some platforms threads identities may appear to be\n\
allocated consecutive numbers starting at 1, this behavior should not\n\
be relied upon, and the number should be seen purely as a magic cookie.\n\
A thread's identity may be reused for another thread after it exits.");
#ifdef PY_HAVE_THREAD_NATIVE_ID
static PyObject *
thread_get_native_id(PyObject *self, PyObject *Py_UNUSED(ignored))
{ … }
PyDoc_STRVAR(get_native_id_doc,
"get_native_id($module, /)\n\
--\n\
\n\
Return a non-negative integer identifying the thread as reported\n\
by the OS (kernel). This may be used to uniquely identify a\n\
particular thread within a system.");
#endif
static PyObject *
thread__count(PyObject *self, PyObject *Py_UNUSED(ignored))
{ … }
PyDoc_STRVAR(_count_doc,
"_count($module, /)\n\
--\n\
\n\
Return the number of currently running Python threads, excluding\n\
the main thread. The returned number comprises all threads created\n\
through `start_new_thread()` as well as `threading.Thread`, and not\n\
yet finished.\n\
\n\
This function is meant for internal and specialized purposes only.\n\
In most applications `threading.enumerate()` should be used instead.");
static PyObject *
thread_stack_size(PyObject *self, PyObject *args)
{ … }
PyDoc_STRVAR(stack_size_doc,
"stack_size($module, size=0, /)\n\
--\n\
\n\
Return the thread stack size used when creating new threads. The\n\
optional size argument specifies the stack size (in bytes) to be used\n\
for subsequently created threads, and must be 0 (use platform or\n\
configured default) or a positive integer value of at least 32,768 (32k).\n\
If changing the thread stack size is unsupported, a ThreadError\n\
exception is raised. If the specified size is invalid, a ValueError\n\
exception is raised, and the stack size is unmodified. 32k bytes\n\
currently the minimum supported stack size value to guarantee\n\
sufficient stack space for the interpreter itself.\n\
\n\
Note that some platforms may have particular restrictions on values for\n\
the stack size, such as requiring a minimum stack size larger than 32 KiB or\n\
requiring allocation in multiples of the system memory page size\n\
- platform documentation should be referred to for more information\n\
(4 KiB pages are common; using multiples of 4096 for the stack size is\n\
the suggested approach in the absence of more specific information).");
static int
thread_excepthook_file(PyObject *file, PyObject *exc_type, PyObject *exc_value,
PyObject *exc_traceback, PyObject *thread)
{ … }
PyDoc_STRVAR(ExceptHookArgs__doc__,
"ExceptHookArgs\n\
\n\
Type used to pass arguments to threading.excepthook.");
static PyStructSequence_Field ExceptHookArgs_fields[] = …;
static PyStructSequence_Desc ExceptHookArgs_desc = …;
static PyObject *
thread_excepthook(PyObject *module, PyObject *args)
{ … }
PyDoc_STRVAR(excepthook_doc,
"_excepthook($module, (exc_type, exc_value, exc_traceback, thread), /)\n\
--\n\
\n\
Handle uncaught Thread.run() exception.");
static PyObject *
thread__is_main_interpreter(PyObject *module, PyObject *Py_UNUSED(ignored))
{ … }
PyDoc_STRVAR(thread__is_main_interpreter_doc,
"_is_main_interpreter($module, /)\n\
--\n\
\n\
Return True if the current interpreter is the main Python interpreter.");
static PyObject *
thread_shutdown(PyObject *self, PyObject *args)
{ … }
PyDoc_STRVAR(shutdown_doc,
"_shutdown($module, /)\n\
--\n\
\n\
Wait for all non-daemon threads (other than the calling thread) to stop.");
static PyObject *
thread__make_thread_handle(PyObject *module, PyObject *identobj)
{ … }
PyDoc_STRVAR(thread__make_thread_handle_doc,
"_make_thread_handle($module, ident, /)\n\
--\n\
\n\
Internal only. Make a thread handle for threads not spawned\n\
by the _thread or threading module.");
static PyObject *
thread__get_main_thread_ident(PyObject *module, PyObject *Py_UNUSED(ignored))
{ … }
PyDoc_STRVAR(thread__get_main_thread_ident_doc,
"_get_main_thread_ident($module, /)\n\
--\n\
\n\
Internal only. Return a non-zero integer that uniquely identifies the main thread\n\
of the main interpreter.");
static PyMethodDef thread_methods[] = …;
static int
thread_module_exec(PyObject *module)
{ … }
static int
thread_module_traverse(PyObject *module, visitproc visit, void *arg)
{ … }
static int
thread_module_clear(PyObject *module)
{ … }
static void
thread_module_free(void *module)
{ … }
PyDoc_STRVAR(thread_doc,
"This module provides primitive operations to write multi-threaded programs.\n\
The 'threading' module provides a more convenient interface.");
static PyModuleDef_Slot thread_module_slots[] = …;
static struct PyModuleDef thread_module = …;
PyMODINIT_FUNC
PyInit__thread(void)
{ … }