Skip to content

Commit 8a4e519

Browse files
authored
gh-79315: Add Include/cpython/pythread.h header (#91798)
1 parent 6f9addb commit 8a4e519

File tree

5 files changed

+50
-44
lines changed

5 files changed

+50
-44
lines changed

Include/cpython/pythread.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#ifndef Py_CPYTHON_PYTHREAD_H
2+
# error "this header file must not be included directly"
3+
#endif
4+
5+
#define PYTHREAD_INVALID_THREAD_ID ((unsigned long)-1)
6+
7+
#ifdef HAVE_FORK
8+
/* Private function to reinitialize a lock at fork in the child process.
9+
Reset the lock to the unlocked state.
10+
Return 0 on success, return -1 on error. */
11+
PyAPI_FUNC(int) _PyThread_at_fork_reinit(PyThread_type_lock *lock);
12+
#endif /* HAVE_FORK */
13+
14+
#ifdef HAVE_PTHREAD_H
15+
/* Darwin needs pthread.h to know type name the pthread_key_t. */
16+
# include <pthread.h>
17+
# define NATIVE_TSS_KEY_T pthread_key_t
18+
#elif defined(NT_THREADS)
19+
/* In Windows, native TSS key type is DWORD,
20+
but hardcode the unsigned long to avoid errors for include directive.
21+
*/
22+
# define NATIVE_TSS_KEY_T unsigned long
23+
#else
24+
# error "Require native threads. See https://bugs.python.org/issue31370"
25+
#endif
26+
27+
/* When Py_LIMITED_API is not defined, the type layout of Py_tss_t is
28+
exposed to allow static allocation in the API clients. Even in this case,
29+
you must handle TSS keys through API functions due to compatibility.
30+
*/
31+
struct _Py_tss_t {
32+
int _is_initialized;
33+
NATIVE_TSS_KEY_T _key;
34+
};
35+
36+
#undef NATIVE_TSS_KEY_T
37+
38+
/* When static allocation, you must initialize with Py_tss_NEEDS_INIT. */
39+
#define Py_tss_NEEDS_INIT {0}

Include/pythread.h

Lines changed: 6 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
#ifndef Py_PYTHREAD_H
32
#define Py_PYTHREAD_H
43

@@ -16,10 +15,6 @@ typedef enum PyLockStatus {
1615
PY_LOCK_INTR
1716
} PyLockStatus;
1817

19-
#ifndef Py_LIMITED_API
20-
#define PYTHREAD_INVALID_THREAD_ID ((unsigned long)-1)
21-
#endif
22-
2318
PyAPI_FUNC(void) PyThread_init_thread(void);
2419
PyAPI_FUNC(unsigned long) PyThread_start_new_thread(void (*)(void *), void *);
2520
PyAPI_FUNC(void) _Py_NO_RETURN PyThread_exit_thread(void);
@@ -36,15 +31,6 @@ PyAPI_FUNC(int) PyThread_acquire_lock(PyThread_type_lock, int);
3631
#define WAIT_LOCK 1
3732
#define NOWAIT_LOCK 0
3833

39-
#ifndef Py_LIMITED_API
40-
#ifdef HAVE_FORK
41-
/* Private function to reinitialize a lock at fork in the child process.
42-
Reset the lock to the unlocked state.
43-
Return 0 on success, return -1 on error. */
44-
PyAPI_FUNC(int) _PyThread_at_fork_reinit(PyThread_type_lock *lock);
45-
#endif /* HAVE_FORK */
46-
#endif /* !Py_LIMITED_API */
47-
4834
/* PY_TIMEOUT_T is the integral type used to specify timeouts when waiting
4935
on a lock (see PyThread_acquire_lock_timed() below).
5036
PY_TIMEOUT_MAX is the highest usable value (in microseconds) of that
@@ -124,35 +110,6 @@ Py_DEPRECATED(3.7) PyAPI_FUNC(void) PyThread_ReInitTLS(void);
124110

125111
typedef struct _Py_tss_t Py_tss_t; /* opaque */
126112

127-
#ifndef Py_LIMITED_API
128-
#ifdef HAVE_PTHREAD_H
129-
/* Darwin needs pthread.h to know type name the pthread_key_t. */
130-
# include <pthread.h>
131-
# define NATIVE_TSS_KEY_T pthread_key_t
132-
#elif defined(NT_THREADS)
133-
/* In Windows, native TSS key type is DWORD,
134-
but hardcode the unsigned long to avoid errors for include directive.
135-
*/
136-
# define NATIVE_TSS_KEY_T unsigned long
137-
#else
138-
# error "Require native threads. See https://bugs.python.org/issue31370"
139-
#endif
140-
141-
/* When Py_LIMITED_API is not defined, the type layout of Py_tss_t is
142-
exposed to allow static allocation in the API clients. Even in this case,
143-
you must handle TSS keys through API functions due to compatibility.
144-
*/
145-
struct _Py_tss_t {
146-
int _is_initialized;
147-
NATIVE_TSS_KEY_T _key;
148-
};
149-
150-
#undef NATIVE_TSS_KEY_T
151-
152-
/* When static allocation, you must initialize with Py_tss_NEEDS_INIT. */
153-
#define Py_tss_NEEDS_INIT {0}
154-
#endif /* !Py_LIMITED_API */
155-
156113
PyAPI_FUNC(Py_tss_t *) PyThread_tss_alloc(void);
157114
PyAPI_FUNC(void) PyThread_tss_free(Py_tss_t *key);
158115

@@ -164,8 +121,13 @@ PyAPI_FUNC(int) PyThread_tss_set(Py_tss_t *key, void *value);
164121
PyAPI_FUNC(void *) PyThread_tss_get(Py_tss_t *key);
165122
#endif /* New in 3.7 */
166123

124+
#ifndef Py_LIMITED_API
125+
# define Py_CPYTHON_PYTHREAD_H
126+
# include "cpython/pythread.h"
127+
# undef Py_CPYTHON_PYTHREAD_H
128+
#endif
129+
167130
#ifdef __cplusplus
168131
}
169132
#endif
170-
171133
#endif /* !Py_PYTHREAD_H */

Makefile.pre.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,6 +1559,7 @@ PYTHON_HEADERS= \
15591559
$(srcdir)/Include/cpython/pymem.h \
15601560
$(srcdir)/Include/cpython/pystate.h \
15611561
$(srcdir)/Include/cpython/pythonrun.h \
1562+
$(srcdir)/Include/cpython/pythread.h \
15621563
$(srcdir)/Include/cpython/pytime.h \
15631564
$(srcdir)/Include/cpython/setobject.h \
15641565
$(srcdir)/Include/cpython/sysmodule.h \

PCbuild/pythoncore.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@
171171
<ClInclude Include="..\Include\cpython\pymem.h" />
172172
<ClInclude Include="..\Include\cpython\pystate.h" />
173173
<ClInclude Include="..\Include\cpython\pythonrun.h" />
174+
<ClInclude Include="..\Include\cpython\pythread.h" />
174175
<ClInclude Include="..\Include\cpython\pytime.h" />
175176
<ClInclude Include="..\Include\cpython\setobject.h" />
176177
<ClInclude Include="..\Include\cpython\sysmodule.h" />

PCbuild/pythoncore.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,9 @@
459459
<ClInclude Include="..\Include\cpython\pythonrun.h">
460460
<Filter>Include\cpython</Filter>
461461
</ClInclude>
462+
<ClInclude Include="..\Include\cpython\pythread.h">
463+
<Filter>Include\cpython</Filter>
464+
</ClInclude>
462465
<ClInclude Include="..\Include\cpython\setobject.h">
463466
<Filter>Include\cpython</Filter>
464467
</ClInclude>

0 commit comments

Comments
 (0)