Skip to content

Commit cd70c96

Browse files
Move thread.c:initialized to _PyRuntimeState.
1 parent 530cc9d commit cd70c96

File tree

9 files changed

+51
-16
lines changed

9 files changed

+51
-16
lines changed

Include/internal/pycore_pythread.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef Py_INTERNAL_PYTHREAD_H
2+
#define Py_INTERNAL_PYTHREAD_H
3+
#ifdef __cplusplus
4+
extern "C" {
5+
#endif
6+
7+
#ifndef Py_BUILD_CORE
8+
# error "this header requires Py_BUILD_CORE define"
9+
#endif
10+
11+
12+
struct _pythread_runtime_state {
13+
int initialized;
14+
};
15+
16+
17+
#ifdef __cplusplus
18+
}
19+
#endif
20+
#endif /* !Py_INTERNAL_PYTHREAD_H */

Include/internal/pycore_runtime.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ extern "C" {
1919
#include "pycore_interp.h" // PyInterpreterState
2020
#include "pycore_pymem.h" // struct _pymem_allocators
2121
#include "pycore_pyhash.h" // struct pyhash_runtime_state
22+
#include "pycore_pythread.h" // struct _pythread_runtime_state
2223
#include "pycore_obmalloc.h" // struct obmalloc_state
2324
#include "pycore_unicodeobject.h" // struct _Py_unicode_runtime_ids
2425

@@ -102,6 +103,7 @@ typedef struct pyruntimestate {
102103
* KeyboardInterrupt exception, suggesting the user pressed ^C. */
103104
int unhandled_keyboard_interrupt;
104105
} signals;
106+
struct _pythread_runtime_state threads;
105107

106108
struct pyinterpreters {
107109
PyThread_type_lock mutex;

Makefile.pre.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1662,6 +1662,7 @@ PYTHON_HEADERS= \
16621662
$(srcdir)/Include/internal/pycore_pymem.h \
16631663
$(srcdir)/Include/internal/pycore_pymem_init.h \
16641664
$(srcdir)/Include/internal/pycore_pystate.h \
1665+
$(srcdir)/Include/internal/pycore_pythread.h \
16651666
$(srcdir)/Include/internal/pycore_range.h \
16661667
$(srcdir)/Include/internal/pycore_runtime.h \
16671668
$(srcdir)/Include/internal/pycore_runtime_init_generated.h \

PCbuild/pythoncore.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@
244244
<ClInclude Include="..\Include\internal\pycore_pymem.h" />
245245
<ClInclude Include="..\Include\internal\pycore_pymem_init.h" />
246246
<ClInclude Include="..\Include\internal\pycore_pystate.h" />
247+
<ClInclude Include="..\Include\internal\pycore_pythread.h" />
247248
<ClInclude Include="..\Include\internal\pycore_range.h" />
248249
<ClInclude Include="..\Include\internal\pycore_runtime.h" />
249250
<ClInclude Include="..\Include\internal\pycore_runtime_init.h" />

PCbuild/pythoncore.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,9 @@
636636
<ClInclude Include="..\Include\internal\pycore_pystate.h">
637637
<Filter>Include\internal</Filter>
638638
</ClInclude>
639+
<ClInclude Include="..\Include\internal\pycore_pythread.h">
640+
<Filter>Include\internal</Filter>
641+
</ClInclude>
639642
<ClInclude Include="..\Include\internal\pycore_range.h">
640643
<Filter>Include\internal</Filter>
641644
</ClInclude>

Python/thread.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,15 @@
4242

4343
#endif /* _POSIX_THREADS */
4444

45-
static int initialized;
46-
4745
static void PyThread__init_thread(void); /* Forward */
4846

4947
void
5048
PyThread_init_thread(void)
5149
{
52-
if (initialized)
50+
if (_PyRuntime.threads.initialized) {
5351
return;
54-
initialized = 1;
52+
}
53+
_PyRuntime.threads.initialized = 1;
5554
PyThread__init_thread();
5655
}
5756

Python/thread_nt.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,9 @@ PyThread_start_new_thread(void (*func)(void *), void *arg)
188188
unsigned threadID;
189189
callobj *obj;
190190

191-
if (!initialized)
191+
if (!_PyRuntime.threads.initialized) {
192192
PyThread_init_thread();
193+
}
193194

194195
obj = (callobj*)HeapAlloc(GetProcessHeap(), 0, sizeof(*obj));
195196
if (!obj)
@@ -223,8 +224,9 @@ PyThread_start_new_thread(void (*func)(void *), void *arg)
223224
unsigned long
224225
PyThread_get_thread_ident(void)
225226
{
226-
if (!initialized)
227+
if (!_PyRuntime.threads.initialized) {
227228
PyThread_init_thread();
229+
}
228230

229231
return GetCurrentThreadId();
230232
}
@@ -238,7 +240,7 @@ PyThread_get_thread_ident(void)
238240
unsigned long
239241
PyThread_get_thread_native_id(void)
240242
{
241-
if (!initialized) {
243+
if (!_PyRuntime.threads.initialized) {
242244
PyThread_init_thread();
243245
}
244246

@@ -251,8 +253,9 @@ PyThread_get_thread_native_id(void)
251253
void _Py_NO_RETURN
252254
PyThread_exit_thread(void)
253255
{
254-
if (!initialized)
256+
if (!_PyRuntime.threads.initialized) {
255257
exit(0);
258+
}
256259
_endthreadex(0);
257260
}
258261

@@ -266,8 +269,9 @@ PyThread_allocate_lock(void)
266269
{
267270
PNRMUTEX mutex;
268271

269-
if (!initialized)
272+
if (!_PyRuntime.threads.initialized) {
270273
PyThread_init_thread();
274+
}
271275

272276
mutex = AllocNonRecursiveMutex() ;
273277

Python/thread_pthread.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,9 @@ PyThread_start_new_thread(void (*func)(void *), void *arg)
243243
size_t tss;
244244
#endif
245245

246-
if (!initialized)
246+
if (!_PyRuntime.threads.initialized) {
247247
PyThread_init_thread();
248+
}
248249

249250
#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
250251
if (pthread_attr_init(&attrs) != 0)
@@ -310,8 +311,9 @@ unsigned long
310311
PyThread_get_thread_ident(void)
311312
{
312313
volatile pthread_t threadid;
313-
if (!initialized)
314+
if (!_PyRuntime.threads.initialized) {
314315
PyThread_init_thread();
316+
}
315317
threadid = pthread_self();
316318
return (unsigned long) threadid;
317319
}
@@ -320,8 +322,9 @@ PyThread_get_thread_ident(void)
320322
unsigned long
321323
PyThread_get_thread_native_id(void)
322324
{
323-
if (!initialized)
325+
if (!_PyRuntime.threads.initialized) {
324326
PyThread_init_thread();
327+
}
325328
#ifdef __APPLE__
326329
uint64_t native_id;
327330
(void) pthread_threadid_np(NULL, &native_id);
@@ -351,8 +354,9 @@ PyThread_get_thread_native_id(void)
351354
void _Py_NO_RETURN
352355
PyThread_exit_thread(void)
353356
{
354-
if (!initialized)
357+
if (!_PyRuntime.threads.initialized) {
355358
exit(0);
359+
}
356360
pthread_exit(0);
357361
}
358362

@@ -368,8 +372,9 @@ PyThread_allocate_lock(void)
368372
sem_t *lock;
369373
int status, error = 0;
370374

371-
if (!initialized)
375+
if (!_PyRuntime.threads.initialized) {
372376
PyThread_init_thread();
377+
}
373378

374379
lock = (sem_t *)PyMem_RawMalloc(sizeof(sem_t));
375380

@@ -550,8 +555,9 @@ PyThread_allocate_lock(void)
550555
pthread_lock *lock;
551556
int status, error = 0;
552557

553-
if (!initialized)
558+
if (!_PyRuntime.threads.initialized) {
554559
PyThread_init_thread();
560+
}
555561

556562
lock = (pthread_lock *) PyMem_RawCalloc(1, sizeof(pthread_lock));
557563
if (lock) {

Tools/c-analyzer/cpython/ignored.tsv

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ Python/getversion.c - version -
2222

2323
Python/bootstrap_hash.c - _Py_HashSecret_Initialized -
2424
Python/pyhash.c - _Py_HashSecret -
25-
Python/thread.c - initialized -
2625
Python/thread_pthread.h - condattr_monotonic -
2726

2827
# safe static buffer used during one-time initialization

0 commit comments

Comments
 (0)