Skip to content

Commit 6e443f4

Browse files
committed
Use a portable assertion for holding the GIL
1 parent 5c814c8 commit 6e443f4

File tree

11 files changed

+47
-33
lines changed

11 files changed

+47
-33
lines changed

Include/internal/pycore_pystate.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,18 @@ PyAPI_FUNC(const PyConfig*) _Py_GetConfig(void);
300300
// See also PyInterpreterState_Get() and _PyInterpreterState_GET().
301301
extern PyInterpreterState* _PyGILState_GetInterpreterStateUnsafe(void);
302302

303+
#ifndef NDEBUG
304+
/* Modern equivalent of assert(PyGILState_Check()) */
305+
static inline void
306+
_Py_AssertHoldsTstate(void)
307+
{
308+
PyThreadState *tstate = _PyThreadState_GET();
309+
_Py_EnsureTstateNotNULL(tstate);
310+
}
311+
#else
312+
#define _Py_AssertHoldsTstate()
313+
#endif
314+
303315
#ifdef __cplusplus
304316
}
305317
#endif

Modules/socketmodule.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -785,8 +785,8 @@ internal_select(PySocketSockObject *s, int writing, PyTime_t interval,
785785
struct timeval tv, *tvp;
786786
#endif
787787

788-
/* must be called with the GIL held */
789-
assert(PyGILState_Check());
788+
/* must be called with a thread state */
789+
_Py_AssertHoldsTstate();
790790

791791
/* Error condition is for output only */
792792
assert(!(connect && !writing));
@@ -899,8 +899,8 @@ sock_call_ex(PySocketSockObject *s,
899899
int deadline_initialized = 0;
900900
int res;
901901

902-
/* sock_call() must be called with the GIL held. */
903-
assert(PyGILState_Check());
902+
/* sock_call() must be called with a thread state. */
903+
_Py_AssertHoldsTstate();
904904

905905
/* outer loop to retry select() when select() is interrupted by a signal
906906
or to retry select()+sock_func() on false positive (see above) */

Objects/object.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3070,14 +3070,14 @@ _Py_SetRefcnt(PyObject *ob, Py_ssize_t refcnt)
30703070
}
30713071

30723072
int PyRefTracer_SetTracer(PyRefTracer tracer, void *data) {
3073-
assert(PyGILState_Check());
3073+
_Py_AssertHoldsTstate();
30743074
_PyRuntime.ref_tracer.tracer_func = tracer;
30753075
_PyRuntime.ref_tracer.tracer_data = data;
30763076
return 0;
30773077
}
30783078

30793079
PyRefTracer PyRefTracer_GetTracer(void** data) {
3080-
assert(PyGILState_Check());
3080+
_Py_AssertHoldsTstate();
30813081
if (data != NULL) {
30823082
*data = _PyRuntime.ref_tracer.tracer_data;
30833083
}

Objects/obmalloc.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2909,7 +2909,8 @@ _PyMem_DebugRawRealloc(void *ctx, void *p, size_t nbytes)
29092909
static inline void
29102910
_PyMem_DebugCheckGIL(const char *func)
29112911
{
2912-
if (!PyGILState_Check()) {
2912+
PyThreadState *tstate = _PyThreadState_GET();
2913+
if (tstate == NULL) {
29132914
#ifndef Py_GIL_DISABLED
29142915
_Py_FatalErrorFunc(func,
29152916
"Python memory allocator called "

Python/ceval_gil.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -995,7 +995,7 @@ _Py_unset_eval_breaker_bit_all(PyInterpreterState *interp, uintptr_t bit)
995995
void
996996
_Py_FinishPendingCalls(PyThreadState *tstate)
997997
{
998-
assert(PyGILState_Check());
998+
_Py_AssertHoldsTstate();
999999
assert(_PyThreadState_CheckConsistency(tstate));
10001000

10011001
struct _pending_calls *pending = &tstate->interp->ceval.pending;
@@ -1056,7 +1056,7 @@ _PyEval_MakePendingCalls(PyThreadState *tstate)
10561056
int
10571057
Py_MakePendingCalls(void)
10581058
{
1059-
assert(PyGILState_Check());
1059+
_Py_AssertHoldsTstate();
10601060

10611061
PyThreadState *tstate = _PyThreadState_GET();
10621062
assert(_PyThreadState_CheckConsistency(tstate));

Python/errors.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,8 @@ _PyErr_SetLocaleString(PyObject *exception, const char *string)
314314
PyObject* _Py_HOT_FUNCTION
315315
PyErr_Occurred(void)
316316
{
317-
/* The caller must hold the GIL. */
318-
assert(PyGILState_Check());
317+
/* The caller must hold a thread state. */
318+
_Py_AssertHoldsTstate();
319319

320320
PyThreadState *tstate = _PyThreadState_GET();
321321
return _PyErr_Occurred(tstate);

Python/fileutils.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "Python.h"
22
#include "pycore_fileutils.h" // fileutils definitions
33
#include "pycore_runtime.h" // _PyRuntime
4+
#include "pycore_pystate.h" // _Py_AssertHoldsTstate
45
#include "osdefs.h" // SEP
56

67
#include <stdlib.h> // mbstowcs()
@@ -1311,7 +1312,7 @@ _Py_fstat(int fd, struct _Py_stat_struct *status)
13111312
{
13121313
int res;
13131314

1314-
assert(PyGILState_Check());
1315+
_Py_AssertHoldsTstate();
13151316

13161317
Py_BEGIN_ALLOW_THREADS
13171318
res = _Py_fstat_noraise(fd, status);
@@ -1691,7 +1692,7 @@ int
16911692
_Py_open(const char *pathname, int flags)
16921693
{
16931694
/* _Py_open() must be called with the GIL held. */
1694-
assert(PyGILState_Check());
1695+
_Py_AssertHoldsTstate();
16951696
return _Py_open_impl(pathname, flags, 1);
16961697
}
16971698

@@ -1770,7 +1771,7 @@ _Py_fopen_obj(PyObject *path, const char *mode)
17701771
wchar_t wmode[10];
17711772
int usize;
17721773

1773-
assert(PyGILState_Check());
1774+
_Py_AssertHoldsTstate();
17741775

17751776
if (PySys_Audit("open", "Osi", path, mode, 0) < 0) {
17761777
return NULL;
@@ -1806,7 +1807,7 @@ _Py_fopen_obj(PyObject *path, const char *mode)
18061807
PyObject *bytes;
18071808
const char *path_bytes;
18081809

1809-
assert(PyGILState_Check());
1810+
_Py_AssertHoldsTstate();
18101811

18111812
if (!PyUnicode_FSConverter(path, &bytes))
18121813
return NULL;
@@ -1862,7 +1863,7 @@ _Py_read(int fd, void *buf, size_t count)
18621863
int err;
18631864
int async_err = 0;
18641865

1865-
assert(PyGILState_Check());
1866+
_Py_AssertHoldsTstate();
18661867

18671868
/* _Py_read() must not be called with an exception set, otherwise the
18681869
* caller may think that read() was interrupted by a signal and the signal
@@ -2028,7 +2029,7 @@ _Py_write_impl(int fd, const void *buf, size_t count, int gil_held)
20282029
Py_ssize_t
20292030
_Py_write(int fd, const void *buf, size_t count)
20302031
{
2031-
assert(PyGILState_Check());
2032+
_Py_AssertHoldsTstate();
20322033

20332034
/* _Py_write() must not be called with an exception set, otherwise the
20342035
* caller may think that write() was interrupted by a signal and the signal
@@ -2656,7 +2657,7 @@ _Py_dup(int fd)
26562657
HANDLE handle;
26572658
#endif
26582659

2659-
assert(PyGILState_Check());
2660+
_Py_AssertHoldsTstate();
26602661

26612662
#ifdef MS_WINDOWS
26622663
handle = _Py_get_osfhandle(fd);

Python/legacy_tracing.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -491,8 +491,8 @@ int
491491
_PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
492492
{
493493
assert(is_tstate_valid(tstate));
494-
/* The caller must hold the GIL */
495-
assert(PyGILState_Check());
494+
/* The caller must hold a thread state */
495+
_Py_AssertHoldsTstate();
496496

497497
/* Call _PySys_Audit() in the context of the current thread state,
498498
even if tstate is not the current thread state. */
@@ -586,8 +586,8 @@ int
586586
_PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
587587
{
588588
assert(is_tstate_valid(tstate));
589-
/* The caller must hold the GIL */
590-
assert(PyGILState_Check());
589+
/* The caller must hold a thread state */
590+
_Py_AssertHoldsTstate();
591591

592592
/* Call _PySys_Audit() in the context of the current thread state,
593593
even if tstate is not the current thread state. */

Python/pystate.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2897,7 +2897,6 @@ _PyInterpreterState_GetConfigCopy(PyConfig *config)
28972897
const PyConfig*
28982898
_Py_GetConfig(void)
28992899
{
2900-
assert(PyGILState_Check());
29012900
PyThreadState *tstate = current_fast_get();
29022901
_Py_EnsureTstateNotNULL(tstate);
29032902
return _PyInterpreterState_GetConfig(tstate->interp);

Python/pytime.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "Python.h"
22
#include "pycore_time.h" // PyTime_t
3+
#include "pycore_pystate.h" // _Py_AssertHoldsTstate
34

45
#include <time.h> // gmtime_r()
56
#ifdef HAVE_SYS_TIME_H
@@ -897,14 +898,14 @@ _PyTime_AsTimespec(PyTime_t t, struct timespec *ts)
897898
#endif
898899

899900

900-
// N.B. If raise_exc=0, this may be called without the GIL.
901+
// N.B. If raise_exc=0, this may be called without a thread state.
901902
static int
902903
py_get_system_clock(PyTime_t *tp, _Py_clock_info_t *info, int raise_exc)
903904
{
904905
assert(info == NULL || raise_exc);
905906
if (raise_exc) {
906-
// raise_exc requires to hold the GIL
907-
assert(PyGILState_Check());
907+
// raise_exc requires to hold a thread state
908+
_Py_AssertHoldsTstate();
908909
}
909910

910911
#ifdef MS_WINDOWS
@@ -1142,14 +1143,14 @@ py_mach_timebase_info(_PyTimeFraction *base, int raise_exc)
11421143
#endif
11431144

11441145

1145-
// N.B. If raise_exc=0, this may be called without the GIL.
1146+
// N.B. If raise_exc=0, this may be called without a thread state.
11461147
static int
11471148
py_get_monotonic_clock(PyTime_t *tp, _Py_clock_info_t *info, int raise_exc)
11481149
{
11491150
assert(info == NULL || raise_exc);
11501151
if (raise_exc) {
1151-
// raise_exc requires to hold the GIL
1152-
assert(PyGILState_Check());
1152+
// raise_exc requires to hold a thread state
1153+
_Py_AssertHoldsTstate();
11531154
}
11541155

11551156
#if defined(MS_WINDOWS)

0 commit comments

Comments
 (0)