Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b02ef71

Browse files
committed
Use Py_uintptr_t for atomic pointers
Issue #26161: Use Py_uintptr_t instead of void* for atomic pointers in pyatomic.h. Use atomic_uintptr_t when <stdatomic.h> is used. Using void* causes compilation warnings depending on which implementation of atomic types is used.
1 parent efb2413 commit b02ef71

3 files changed

Lines changed: 31 additions & 30 deletions

File tree

Include/pyatomic.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ typedef enum _Py_memory_order {
3030
} _Py_memory_order;
3131

3232
typedef struct _Py_atomic_address {
33-
_Atomic void *_value;
33+
atomic_uintptr_t _value;
3434
} _Py_atomic_address;
3535

3636
typedef struct _Py_atomic_int {
@@ -61,7 +61,7 @@ typedef enum _Py_memory_order {
6161
} _Py_memory_order;
6262

6363
typedef struct _Py_atomic_address {
64-
void *_value;
64+
Py_uintptr_t _value;
6565
} _Py_atomic_address;
6666

6767
typedef struct _Py_atomic_int {
@@ -98,7 +98,7 @@ typedef enum _Py_memory_order {
9898
} _Py_memory_order;
9999

100100
typedef struct _Py_atomic_address {
101-
void *_value;
101+
Py_uintptr_t _value;
102102
} _Py_atomic_address;
103103

104104
typedef struct _Py_atomic_int {

Python/ceval_gil.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ static _Py_atomic_int gil_locked = {-1};
111111
static unsigned long gil_switch_number = 0;
112112
/* Last PyThreadState holding / having held the GIL. This helps us know
113113
whether anyone else was scheduled after we dropped the GIL. */
114-
static _Py_atomic_address gil_last_holder = {NULL};
114+
static _Py_atomic_address gil_last_holder = {0};
115115

116116
/* This condition variable allows one or several threads to wait until
117117
the GIL is released. In addition, the mutex also protects the above
@@ -142,7 +142,7 @@ static void create_gil(void)
142142
#ifdef FORCE_SWITCHING
143143
COND_INIT(switch_cond);
144144
#endif
145-
_Py_atomic_store_relaxed(&gil_last_holder, NULL);
145+
_Py_atomic_store_relaxed(&gil_last_holder, 0);
146146
_Py_ANNOTATE_RWLOCK_CREATE(&gil_locked);
147147
_Py_atomic_store_explicit(&gil_locked, 0, _Py_memory_order_release);
148148
}
@@ -178,7 +178,7 @@ static void drop_gil(PyThreadState *tstate)
178178
/* Sub-interpreter support: threads might have been switched
179179
under our feet using PyThreadState_Swap(). Fix the GIL last
180180
holder variable so that our heuristics work. */
181-
_Py_atomic_store_relaxed(&gil_last_holder, tstate);
181+
_Py_atomic_store_relaxed(&gil_last_holder, (Py_uintptr_t)tstate);
182182
}
183183

184184
MUTEX_LOCK(gil_mutex);
@@ -240,7 +240,7 @@ static void take_gil(PyThreadState *tstate)
240240
_Py_ANNOTATE_RWLOCK_ACQUIRED(&gil_locked, /*is_write=*/1);
241241

242242
if (tstate != (PyThreadState*)_Py_atomic_load_relaxed(&gil_last_holder)) {
243-
_Py_atomic_store_relaxed(&gil_last_holder, tstate);
243+
_Py_atomic_store_relaxed(&gil_last_holder, (Py_uintptr_t)tstate);
244244
++gil_switch_number;
245245
}
246246

Python/pystate.c

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33

44
#include "Python.h"
55

6-
#ifndef Py_BUILD_CORE
7-
/* ensure that PyThreadState_GET() is a macro, not an alias to
8-
* PyThreadState_Get() */
9-
# error "pystate.c must be compiled with Py_BUILD_CORE defined"
10-
#endif
6+
#define GET_TSTATE() \
7+
((PyThreadState*)_Py_atomic_load_relaxed(&_PyThreadState_Current))
8+
#define SET_TSTATE(value) \
9+
_Py_atomic_store_relaxed(&_PyThreadState_Current, (Py_uintptr_t)(value))
10+
#define GET_INTERP_STATE() \
11+
(GET_TSTATE()->interp)
12+
1113

1214
/* --------------------------------------------------------------------------
1315
CAUTION
@@ -54,7 +56,7 @@ static PyInterpreterState *interp_head = NULL;
5456

5557
/* Assuming the current thread holds the GIL, this is the
5658
PyThreadState for the current thread. */
57-
_Py_atomic_address _PyThreadState_Current = {NULL};
59+
_Py_atomic_address _PyThreadState_Current = {0};
5860
PyThreadFrameGetter _PyThreadState_GetFrame = NULL;
5961

6062
#ifdef WITH_THREAD
@@ -260,7 +262,7 @@ PyObject*
260262
PyState_FindModule(struct PyModuleDef* module)
261263
{
262264
Py_ssize_t index = module->m_base.m_index;
263-
PyInterpreterState *state = PyThreadState_GET()->interp;
265+
PyInterpreterState *state = GET_INTERP_STATE();
264266
PyObject *res;
265267
if (module->m_slots) {
266268
return NULL;
@@ -284,7 +286,7 @@ _PyState_AddModule(PyObject* module, struct PyModuleDef* def)
284286
"PyState_AddModule called on module with slots");
285287
return -1;
286288
}
287-
state = PyThreadState_GET()->interp;
289+
state = GET_INTERP_STATE();
288290
if (!def)
289291
return -1;
290292
if (!state->modules_by_index) {
@@ -304,7 +306,7 @@ int
304306
PyState_AddModule(PyObject* module, struct PyModuleDef* def)
305307
{
306308
Py_ssize_t index;
307-
PyInterpreterState *state = PyThreadState_GET()->interp;
309+
PyInterpreterState *state = GET_INTERP_STATE();
308310
if (!def) {
309311
Py_FatalError("PyState_AddModule: Module Definition is NULL");
310312
return -1;
@@ -331,7 +333,7 @@ PyState_RemoveModule(struct PyModuleDef* def)
331333
"PyState_RemoveModule called on module with slots");
332334
return -1;
333335
}
334-
state = PyThreadState_GET()->interp;
336+
state = GET_INTERP_STATE();
335337
if (index == 0) {
336338
Py_FatalError("PyState_RemoveModule: Module index invalid.");
337339
return -1;
@@ -351,7 +353,7 @@ PyState_RemoveModule(struct PyModuleDef* def)
351353
void
352354
_PyState_ClearModules(void)
353355
{
354-
PyInterpreterState *state = PyThreadState_GET()->interp;
356+
PyInterpreterState *state = GET_INTERP_STATE();
355357
if (state->modules_by_index) {
356358
Py_ssize_t i;
357359
for (i = 0; i < PyList_GET_SIZE(state->modules_by_index); i++) {
@@ -429,7 +431,7 @@ tstate_delete_common(PyThreadState *tstate)
429431
void
430432
PyThreadState_Delete(PyThreadState *tstate)
431433
{
432-
if (tstate == PyThreadState_GET())
434+
if (tstate == GET_TSTATE())
433435
Py_FatalError("PyThreadState_Delete: tstate is still current");
434436
#ifdef WITH_THREAD
435437
if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate)
@@ -443,11 +445,11 @@ PyThreadState_Delete(PyThreadState *tstate)
443445
void
444446
PyThreadState_DeleteCurrent()
445447
{
446-
PyThreadState *tstate = PyThreadState_GET();
448+
PyThreadState *tstate = GET_TSTATE();
447449
if (tstate == NULL)
448450
Py_FatalError(
449451
"PyThreadState_DeleteCurrent: no current tstate");
450-
_Py_atomic_store_relaxed(&_PyThreadState_Current, NULL);
452+
SET_TSTATE(NULL);
451453
if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate)
452454
PyThread_delete_key_value(autoTLSkey);
453455
tstate_delete_common(tstate);
@@ -496,14 +498,14 @@ _PyThreadState_DeleteExcept(PyThreadState *tstate)
496498
PyThreadState *
497499
_PyThreadState_UncheckedGet(void)
498500
{
499-
return PyThreadState_GET();
501+
return GET_TSTATE();
500502
}
501503

502504

503505
PyThreadState *
504506
PyThreadState_Get(void)
505507
{
506-
PyThreadState *tstate = PyThreadState_GET();
508+
PyThreadState *tstate = GET_TSTATE();
507509
if (tstate == NULL)
508510
Py_FatalError("PyThreadState_Get: no current thread");
509511

@@ -514,9 +516,9 @@ PyThreadState_Get(void)
514516
PyThreadState *
515517
PyThreadState_Swap(PyThreadState *newts)
516518
{
517-
PyThreadState *oldts = PyThreadState_GET();
519+
PyThreadState *oldts = GET_TSTATE();
518520

519-
_Py_atomic_store_relaxed(&_PyThreadState_Current, newts);
521+
SET_TSTATE(newts);
520522
/* It should not be possible for more than one thread state
521523
to be used for a thread. Check this the best we can in debug
522524
builds.
@@ -545,7 +547,7 @@ PyThreadState_Swap(PyThreadState *newts)
545547
PyObject *
546548
PyThreadState_GetDict(void)
547549
{
548-
PyThreadState *tstate = PyThreadState_GET();
550+
PyThreadState *tstate = GET_TSTATE();
549551
if (tstate == NULL)
550552
return NULL;
551553

@@ -569,8 +571,7 @@ PyThreadState_GetDict(void)
569571

570572
int
571573
PyThreadState_SetAsyncExc(long id, PyObject *exc) {
572-
PyThreadState *tstate = PyThreadState_GET();
573-
PyInterpreterState *interp = tstate->interp;
574+
PyInterpreterState *interp = GET_INTERP_STATE();
574575
PyThreadState *p;
575576

576577
/* Although the GIL is held, a few C API functions can be called
@@ -691,7 +692,7 @@ PyThreadState_IsCurrent(PyThreadState *tstate)
691692
{
692693
/* Must be the tstate for this thread */
693694
assert(PyGILState_GetThisThreadState()==tstate);
694-
return tstate == PyThreadState_GET();
695+
return tstate == GET_TSTATE();
695696
}
696697

697698
/* Internal initialization/finalization functions called by
@@ -783,7 +784,7 @@ PyGILState_GetThisThreadState(void)
783784
int
784785
PyGILState_Check(void)
785786
{
786-
PyThreadState *tstate = PyThreadState_GET();
787+
PyThreadState *tstate = GET_TSTATE();
787788
return tstate && (tstate == PyGILState_GetThisThreadState());
788789
}
789790

0 commit comments

Comments
 (0)