File indexing completed on 2025-11-19 09:50:50
0001 #ifndef Py_INTERNAL_PYTHREAD_H
0002 #define Py_INTERNAL_PYTHREAD_H
0003 #ifdef __cplusplus
0004 extern "C" {
0005 #endif
0006
0007 #ifndef Py_BUILD_CORE
0008 # error "this header requires Py_BUILD_CORE define"
0009 #endif
0010
0011 #include "dynamic_annotations.h" // _Py_ANNOTATE_PURE_HAPPENS_BEFORE_MUTEX
0012 #include "pycore_llist.h" // struct llist_node
0013
0014
0015 #if (defined(HAVE_UNISTD_H) && !defined(_POSIX_THREADS) \
0016 && !defined(_POSIX_SEMAPHORES))
0017 # include <unistd.h> // _POSIX_THREADS, _POSIX_SEMAPHORES
0018 #endif
0019 #if (defined(HAVE_PTHREAD_H) && !defined(_POSIX_THREADS) \
0020 && !defined(_POSIX_SEMAPHORES))
0021
0022
0023
0024 # include <pthread.h> // _POSIX_THREADS, _POSIX_SEMAPHORES
0025 #endif
0026 #if !defined(_POSIX_THREADS) && defined(__hpux) && defined(_SC_THREADS)
0027
0028
0029
0030
0031
0032
0033
0034 # define _POSIX_THREADS
0035 #endif
0036
0037
0038 #if defined(_POSIX_THREADS) || defined(HAVE_PTHREAD_STUBS)
0039 # define _USE_PTHREADS
0040 #endif
0041
0042 #if defined(_USE_PTHREADS) && defined(HAVE_PTHREAD_CONDATTR_SETCLOCK) && defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
0043
0044 # define CONDATTR_MONOTONIC
0045 #endif
0046
0047
0048 #if defined(HAVE_PTHREAD_STUBS)
0049 #include "cpython/pthread_stubs.h" // PTHREAD_KEYS_MAX
0050 #include <stdbool.h> // bool
0051
0052
0053 struct py_stub_tls_entry {
0054 bool in_use;
0055 void *value;
0056 };
0057 #endif
0058
0059 struct _pythread_runtime_state {
0060 int initialized;
0061
0062 #ifdef _USE_PTHREADS
0063
0064 struct {
0065
0066 pthread_condattr_t *ptr;
0067 # ifdef CONDATTR_MONOTONIC
0068
0069 pthread_condattr_t val;
0070 # endif
0071 } _condattr_monotonic;
0072
0073 #endif
0074
0075 #if defined(HAVE_PTHREAD_STUBS)
0076 struct {
0077 struct py_stub_tls_entry tls_entries[PTHREAD_KEYS_MAX];
0078 } stubs;
0079 #endif
0080
0081
0082 struct llist_node handles;
0083 };
0084
0085 #define _pythread_RUNTIME_INIT(pythread) \
0086 { \
0087 .handles = LLIST_INIT(pythread.handles), \
0088 }
0089
0090 #ifdef HAVE_FORK
0091
0092
0093
0094 extern int _PyThread_at_fork_reinit(PyThread_type_lock *lock);
0095 extern void _PyThread_AfterFork(struct _pythread_runtime_state *state);
0096 #endif
0097
0098
0099
0100 #define PyThread_UNSET_TIMEOUT ((PyTime_t)(-1 * 1000 * 1000 * 1000))
0101
0102
0103 PyAPI_FUNC(int) PyThread_ParseTimeoutArg(
0104 PyObject *arg,
0105 int blocking,
0106 PY_TIMEOUT_T *timeout);
0107
0108
0109
0110
0111
0112
0113
0114
0115 PyAPI_FUNC(PyLockStatus) PyThread_acquire_lock_timed_with_retries(
0116 PyThread_type_lock,
0117 PY_TIMEOUT_T microseconds);
0118
0119 typedef unsigned long long PyThread_ident_t;
0120 typedef Py_uintptr_t PyThread_handle_t;
0121
0122 #define PY_FORMAT_THREAD_IDENT_T "llu"
0123 #define Py_PARSE_THREAD_IDENT_T "K"
0124
0125 PyAPI_FUNC(PyThread_ident_t) PyThread_get_thread_ident_ex(void);
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138 PyAPI_FUNC(int) PyThread_start_joinable_thread(void (*func)(void *),
0139 void *arg,
0140 PyThread_ident_t* ident,
0141 PyThread_handle_t* handle);
0142
0143
0144
0145
0146
0147 PyAPI_FUNC(int) PyThread_join_thread(PyThread_handle_t);
0148
0149
0150
0151
0152
0153
0154 PyAPI_FUNC(int) PyThread_detach_thread(PyThread_handle_t);
0155
0156 #ifdef __cplusplus
0157 }
0158 #endif
0159 #endif