summaryrefslogtreecommitdiffstats
path: root/Include/internal
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@redhat.com>2018-11-01 00:51:40 (GMT)
committerGitHub <noreply@github.com>2018-11-01 00:51:40 (GMT)
commit50b48572d9a90c5bb36e2bef6179548ea927a35a (patch)
tree3b77efe6c7182a3e0b98fcba9854198dbeca3a98 /Include/internal
parent27e2d1f21975dfb8c0ddcb192fa0f45a51b7977e (diff)
downloadcpython-50b48572d9a90c5bb36e2bef6179548ea927a35a.zip
cpython-50b48572d9a90c5bb36e2bef6179548ea927a35a.tar.gz
cpython-50b48572d9a90c5bb36e2bef6179548ea927a35a.tar.bz2
bpo-35081: Add _PyThreadState_GET() internal macro (GH-10266)
If Py_BUILD_CORE is defined, the PyThreadState_GET() macro access _PyRuntime which comes from the internal pycore_state.h header. Public headers must not require internal headers. Move PyThreadState_GET() and _PyInterpreterState_GET_UNSAFE() from Include/pystate.h to Include/internal/pycore_state.h, and rename PyThreadState_GET() to _PyThreadState_GET() there. The PyThreadState_GET() macro of pystate.h is now redefined when pycore_state.h is included, to use the fast _PyThreadState_GET(). Changes: * Add _PyThreadState_GET() macro * Replace "PyThreadState_GET()->interp" with _PyInterpreterState_GET_UNSAFE() * Replace PyThreadState_GET() with _PyThreadState_GET() in internal C files (compiled with Py_BUILD_CORE defined), but keep PyThreadState_GET() in the public header files. * _testcapimodule.c: replace PyThreadState_GET() with PyThreadState_Get(); the module is not compiled with Py_BUILD_CORE defined. * pycore_state.h now requires Py_BUILD_CORE to be defined.
Diffstat (limited to 'Include/internal')
-rw-r--r--Include/internal/pycore_state.h34
1 files changed, 34 insertions, 0 deletions
diff --git a/Include/internal/pycore_state.h b/Include/internal/pycore_state.h
index ff25d2e..9a084f7 100644
--- a/Include/internal/pycore_state.h
+++ b/Include/internal/pycore_state.h
@@ -4,6 +4,10 @@
extern "C" {
#endif
+#ifndef Py_BUILD_CORE
+# error "Py_BUILD_CORE must be defined to include this header"
+#endif
+
#include "pystate.h"
#include "pythread.h"
@@ -214,6 +218,36 @@ PyAPI_FUNC(_PyInitError) _PyRuntime_Initialize(void);
(_PyRuntime.finalizing == tstate)
+/* Variable and macro for in-line access to current thread
+ and interpreter state */
+
+/* Get the current Python thread state.
+
+ Efficient macro reading directly the 'gilstate.tstate_current' atomic
+ variable. The macro is unsafe: it does not check for error and it can
+ return NULL.
+
+ The caller must hold the GIL.
+
+ See also PyThreadState_Get() and PyThreadState_GET(). */
+#define _PyThreadState_GET() \
+ ((PyThreadState*)_Py_atomic_load_relaxed(&_PyRuntime.gilstate.tstate_current))
+
+/* Redefine PyThreadState_GET() as an alias to _PyThreadState_GET() */
+#undef PyThreadState_GET
+#define PyThreadState_GET() _PyThreadState_GET()
+
+/* Get the current interpreter state.
+
+ The macro is unsafe: it does not check for error and it can return NULL.
+
+ The caller must hold the GIL.
+
+ See also _PyInterpreterState_Get()
+ and _PyGILState_GetInterpreterStateUnsafe(). */
+#define _PyInterpreterState_GET_UNSAFE() (_PyThreadState_GET()->interp)
+
+
/* Other */
PyAPI_FUNC(_PyInitError) _PyInterpreterState_Enable(_PyRuntimeState *);