summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--c/misc_thread_common.h46
1 files changed, 24 insertions, 22 deletions
diff --git a/c/misc_thread_common.h b/c/misc_thread_common.h
index b93bdfe..007c2ee 100644
--- a/c/misc_thread_common.h
+++ b/c/misc_thread_common.h
@@ -62,33 +62,35 @@ static void restore_errno_only(void)
#endif
-/* Seems that CPython 3.5.1 made our job harder. Did not find out how
- to do that without these hacks. We can't use PyThreadState_GET(),
- because that calls PyThreadState_Get() which fails an assert if the
- result is NULL. We can use _PyThreadState_UncheckedGet() from 3.6,
- though. It was added in 3.5.2 but should never be used in 3.5.x
- because it is not available in 3.5.0 or 3.5.1. */
-#if PY_VERSION_HEX >= 0x03060000
+/* MESS. We can't use PyThreadState_GET(), because that calls
+ PyThreadState_Get() which fails an assert if the result is NULL.
+
+ * in Python 2.7 and <= 3.4, the variable _PyThreadState_Current
+ is directly available, so use that.
+
+ * in Python 3.5, the variable is available too, but it might be
+ the case that the headers don't define it (this changed in 3.5.1).
+ In case we're compiling with 3.5.x with x >= 1, we need to
+ manually define this variable.
+
+ * in Python >= 3.6 there is _PyThreadState_UncheckedGet().
+ It was added in 3.5.2 but should never be used in 3.5.x
+ because it is not available in 3.5.0 or 3.5.1.
+*/
+#if PY_VERSION_HEX >= 0x03050100 && PY_VERSION_HEX < 0x03060000
+PyAPI_DATA(void *volatile) _PyThreadState_Current;
+#endif
+
static PyThreadState *get_current_ts(void)
{
+#if PY_VERSION_HEX >= 0x03060000
return _PyThreadState_UncheckedGet();
-}
-#else
-# if PY_MAJOR_VERSION >= 3 && !defined(_Py_atomic_load_relaxed)
- /* this was abruptly un-defined in 3.5.1 */
-extern void *volatile _PyThreadState_Current;
- /* XXX simple volatile access is assumed atomic */
-# define _Py_atomic_load_relaxed(pp) (*(pp))
-# endif
-static PyThreadState *get_current_ts(void)
-{
-# if defined(_Py_atomic_load_relaxed)
+#elif defined(_Py_atomic_load_relaxed)
return (PyThreadState*)_Py_atomic_load_relaxed(&_PyThreadState_Current);
-# else
- return _PyThreadState_Current;
-# endif
-}
+#else
+ return (PyThreadState*)_PyThreadState_Current; /* assume atomic read */
#endif
+}
static PyGILState_STATE gil_ensure(void)
{