jpayne@69
|
1
|
jpayne@69
|
2 #ifndef Py_PYTHREAD_H
|
jpayne@69
|
3 #define Py_PYTHREAD_H
|
jpayne@69
|
4
|
jpayne@69
|
5 typedef void *PyThread_type_lock;
|
jpayne@69
|
6 typedef void *PyThread_type_sema;
|
jpayne@69
|
7
|
jpayne@69
|
8 #ifdef __cplusplus
|
jpayne@69
|
9 extern "C" {
|
jpayne@69
|
10 #endif
|
jpayne@69
|
11
|
jpayne@69
|
12 /* Return status codes for Python lock acquisition. Chosen for maximum
|
jpayne@69
|
13 * backwards compatibility, ie failure -> 0, success -> 1. */
|
jpayne@69
|
14 typedef enum PyLockStatus {
|
jpayne@69
|
15 PY_LOCK_FAILURE = 0,
|
jpayne@69
|
16 PY_LOCK_ACQUIRED = 1,
|
jpayne@69
|
17 PY_LOCK_INTR
|
jpayne@69
|
18 } PyLockStatus;
|
jpayne@69
|
19
|
jpayne@69
|
20 #ifndef Py_LIMITED_API
|
jpayne@69
|
21 #define PYTHREAD_INVALID_THREAD_ID ((unsigned long)-1)
|
jpayne@69
|
22 #endif
|
jpayne@69
|
23
|
jpayne@69
|
24 PyAPI_FUNC(void) PyThread_init_thread(void);
|
jpayne@69
|
25 PyAPI_FUNC(unsigned long) PyThread_start_new_thread(void (*)(void *), void *);
|
jpayne@69
|
26 PyAPI_FUNC(void) _Py_NO_RETURN PyThread_exit_thread(void);
|
jpayne@69
|
27 PyAPI_FUNC(unsigned long) PyThread_get_thread_ident(void);
|
jpayne@69
|
28
|
jpayne@69
|
29 #if defined(__APPLE__) || defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(_WIN32) || defined(_AIX)
|
jpayne@69
|
30 #define PY_HAVE_THREAD_NATIVE_ID
|
jpayne@69
|
31 PyAPI_FUNC(unsigned long) PyThread_get_thread_native_id(void);
|
jpayne@69
|
32 #endif
|
jpayne@69
|
33
|
jpayne@69
|
34 PyAPI_FUNC(PyThread_type_lock) PyThread_allocate_lock(void);
|
jpayne@69
|
35 PyAPI_FUNC(void) PyThread_free_lock(PyThread_type_lock);
|
jpayne@69
|
36 PyAPI_FUNC(int) PyThread_acquire_lock(PyThread_type_lock, int);
|
jpayne@69
|
37 #define WAIT_LOCK 1
|
jpayne@69
|
38 #define NOWAIT_LOCK 0
|
jpayne@69
|
39
|
jpayne@69
|
40 /* PY_TIMEOUT_T is the integral type used to specify timeouts when waiting
|
jpayne@69
|
41 on a lock (see PyThread_acquire_lock_timed() below).
|
jpayne@69
|
42 PY_TIMEOUT_MAX is the highest usable value (in microseconds) of that
|
jpayne@69
|
43 type, and depends on the system threading API.
|
jpayne@69
|
44
|
jpayne@69
|
45 NOTE: this isn't the same value as `_thread.TIMEOUT_MAX`. The _thread
|
jpayne@69
|
46 module exposes a higher-level API, with timeouts expressed in seconds
|
jpayne@69
|
47 and floating-point numbers allowed.
|
jpayne@69
|
48 */
|
jpayne@69
|
49 #define PY_TIMEOUT_T long long
|
jpayne@69
|
50
|
jpayne@69
|
51 #if defined(_POSIX_THREADS)
|
jpayne@69
|
52 /* PyThread_acquire_lock_timed() uses _PyTime_FromNanoseconds(us * 1000),
|
jpayne@69
|
53 convert microseconds to nanoseconds. */
|
jpayne@69
|
54 # define PY_TIMEOUT_MAX (PY_LLONG_MAX / 1000)
|
jpayne@69
|
55 #elif defined (NT_THREADS)
|
jpayne@69
|
56 /* In the NT API, the timeout is a DWORD and is expressed in milliseconds */
|
jpayne@69
|
57 # if 0xFFFFFFFFLL * 1000 < PY_LLONG_MAX
|
jpayne@69
|
58 # define PY_TIMEOUT_MAX (0xFFFFFFFFLL * 1000)
|
jpayne@69
|
59 # else
|
jpayne@69
|
60 # define PY_TIMEOUT_MAX PY_LLONG_MAX
|
jpayne@69
|
61 # endif
|
jpayne@69
|
62 #else
|
jpayne@69
|
63 # define PY_TIMEOUT_MAX PY_LLONG_MAX
|
jpayne@69
|
64 #endif
|
jpayne@69
|
65
|
jpayne@69
|
66
|
jpayne@69
|
67 /* If microseconds == 0, the call is non-blocking: it returns immediately
|
jpayne@69
|
68 even when the lock can't be acquired.
|
jpayne@69
|
69 If microseconds > 0, the call waits up to the specified duration.
|
jpayne@69
|
70 If microseconds < 0, the call waits until success (or abnormal failure)
|
jpayne@69
|
71
|
jpayne@69
|
72 microseconds must be less than PY_TIMEOUT_MAX. Behaviour otherwise is
|
jpayne@69
|
73 undefined.
|
jpayne@69
|
74
|
jpayne@69
|
75 If intr_flag is true and the acquire is interrupted by a signal, then the
|
jpayne@69
|
76 call will return PY_LOCK_INTR. The caller may reattempt to acquire the
|
jpayne@69
|
77 lock.
|
jpayne@69
|
78 */
|
jpayne@69
|
79 PyAPI_FUNC(PyLockStatus) PyThread_acquire_lock_timed(PyThread_type_lock,
|
jpayne@69
|
80 PY_TIMEOUT_T microseconds,
|
jpayne@69
|
81 int intr_flag);
|
jpayne@69
|
82
|
jpayne@69
|
83 PyAPI_FUNC(void) PyThread_release_lock(PyThread_type_lock);
|
jpayne@69
|
84
|
jpayne@69
|
85 PyAPI_FUNC(size_t) PyThread_get_stacksize(void);
|
jpayne@69
|
86 PyAPI_FUNC(int) PyThread_set_stacksize(size_t);
|
jpayne@69
|
87
|
jpayne@69
|
88 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
|
jpayne@69
|
89 PyAPI_FUNC(PyObject*) PyThread_GetInfo(void);
|
jpayne@69
|
90 #endif
|
jpayne@69
|
91
|
jpayne@69
|
92
|
jpayne@69
|
93 /* Thread Local Storage (TLS) API
|
jpayne@69
|
94 TLS API is DEPRECATED. Use Thread Specific Storage (TSS) API.
|
jpayne@69
|
95
|
jpayne@69
|
96 The existing TLS API has used int to represent TLS keys across all
|
jpayne@69
|
97 platforms, but it is not POSIX-compliant. Therefore, the new TSS API uses
|
jpayne@69
|
98 opaque data type to represent TSS keys to be compatible (see PEP 539).
|
jpayne@69
|
99 */
|
jpayne@69
|
100 Py_DEPRECATED(3.7) PyAPI_FUNC(int) PyThread_create_key(void);
|
jpayne@69
|
101 Py_DEPRECATED(3.7) PyAPI_FUNC(void) PyThread_delete_key(int key);
|
jpayne@69
|
102 Py_DEPRECATED(3.7) PyAPI_FUNC(int) PyThread_set_key_value(int key,
|
jpayne@69
|
103 void *value);
|
jpayne@69
|
104 Py_DEPRECATED(3.7) PyAPI_FUNC(void *) PyThread_get_key_value(int key);
|
jpayne@69
|
105 Py_DEPRECATED(3.7) PyAPI_FUNC(void) PyThread_delete_key_value(int key);
|
jpayne@69
|
106
|
jpayne@69
|
107 /* Cleanup after a fork */
|
jpayne@69
|
108 Py_DEPRECATED(3.7) PyAPI_FUNC(void) PyThread_ReInitTLS(void);
|
jpayne@69
|
109
|
jpayne@69
|
110
|
jpayne@69
|
111 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03070000
|
jpayne@69
|
112 /* New in 3.7 */
|
jpayne@69
|
113 /* Thread Specific Storage (TSS) API */
|
jpayne@69
|
114
|
jpayne@69
|
115 typedef struct _Py_tss_t Py_tss_t; /* opaque */
|
jpayne@69
|
116
|
jpayne@69
|
117 #ifndef Py_LIMITED_API
|
jpayne@69
|
118 #if defined(_POSIX_THREADS)
|
jpayne@69
|
119 /* Darwin needs pthread.h to know type name the pthread_key_t. */
|
jpayne@69
|
120 # include <pthread.h>
|
jpayne@69
|
121 # define NATIVE_TSS_KEY_T pthread_key_t
|
jpayne@69
|
122 #elif defined(NT_THREADS)
|
jpayne@69
|
123 /* In Windows, native TSS key type is DWORD,
|
jpayne@69
|
124 but hardcode the unsigned long to avoid errors for include directive.
|
jpayne@69
|
125 */
|
jpayne@69
|
126 # define NATIVE_TSS_KEY_T unsigned long
|
jpayne@69
|
127 #else
|
jpayne@69
|
128 # error "Require native threads. See https://bugs.python.org/issue31370"
|
jpayne@69
|
129 #endif
|
jpayne@69
|
130
|
jpayne@69
|
131 /* When Py_LIMITED_API is not defined, the type layout of Py_tss_t is
|
jpayne@69
|
132 exposed to allow static allocation in the API clients. Even in this case,
|
jpayne@69
|
133 you must handle TSS keys through API functions due to compatibility.
|
jpayne@69
|
134 */
|
jpayne@69
|
135 struct _Py_tss_t {
|
jpayne@69
|
136 int _is_initialized;
|
jpayne@69
|
137 NATIVE_TSS_KEY_T _key;
|
jpayne@69
|
138 };
|
jpayne@69
|
139
|
jpayne@69
|
140 #undef NATIVE_TSS_KEY_T
|
jpayne@69
|
141
|
jpayne@69
|
142 /* When static allocation, you must initialize with Py_tss_NEEDS_INIT. */
|
jpayne@69
|
143 #define Py_tss_NEEDS_INIT {0}
|
jpayne@69
|
144 #endif /* !Py_LIMITED_API */
|
jpayne@69
|
145
|
jpayne@69
|
146 PyAPI_FUNC(Py_tss_t *) PyThread_tss_alloc(void);
|
jpayne@69
|
147 PyAPI_FUNC(void) PyThread_tss_free(Py_tss_t *key);
|
jpayne@69
|
148
|
jpayne@69
|
149 /* The parameter key must not be NULL. */
|
jpayne@69
|
150 PyAPI_FUNC(int) PyThread_tss_is_created(Py_tss_t *key);
|
jpayne@69
|
151 PyAPI_FUNC(int) PyThread_tss_create(Py_tss_t *key);
|
jpayne@69
|
152 PyAPI_FUNC(void) PyThread_tss_delete(Py_tss_t *key);
|
jpayne@69
|
153 PyAPI_FUNC(int) PyThread_tss_set(Py_tss_t *key, void *value);
|
jpayne@69
|
154 PyAPI_FUNC(void *) PyThread_tss_get(Py_tss_t *key);
|
jpayne@69
|
155 #endif /* New in 3.7 */
|
jpayne@69
|
156
|
jpayne@69
|
157 #ifdef __cplusplus
|
jpayne@69
|
158 }
|
jpayne@69
|
159 #endif
|
jpayne@69
|
160
|
jpayne@69
|
161 #endif /* !Py_PYTHREAD_H */
|