From 90cc8fe385255a5cdefa9035b7abd1e227128f03 Mon Sep 17 00:00:00 2001 From: rdb Date: Tue, 16 Oct 2018 22:15:36 +0200 Subject: [PATCH] Fix building with SIMPLE_THREADS=1 --- dtool/src/dtoolbase/dtoolbase.cxx | 8 ++++++++ dtool/src/dtoolbase/dtoolbase_cc.h | 9 +++++++++ dtool/src/interrogatedb/py_panda.cxx | 5 +++++ dtool/src/parser-inc/Python.h | 2 +- panda/src/pipeline/threadSimpleImpl.I | 8 -------- panda/src/pipeline/threadSimpleImpl.cxx | 14 +++++++++++--- panda/src/pipeline/threadSimpleImpl.h | 2 +- panda/src/pipeline/threadSimpleManager.cxx | 14 ++------------ 8 files changed, 37 insertions(+), 25 deletions(-) diff --git a/dtool/src/dtoolbase/dtoolbase.cxx b/dtool/src/dtoolbase/dtoolbase.cxx index a45a1de4e0..3bd34815c9 100644 --- a/dtool/src/dtoolbase/dtoolbase.cxx +++ b/dtool/src/dtoolbase/dtoolbase.cxx @@ -62,4 +62,12 @@ default_thread_consider_yield() { void (*global_thread_yield)() = default_thread_yield; void (*global_thread_consider_yield)() = default_thread_consider_yield; +#ifdef HAVE_PYTHON +static PyThreadState * +default_thread_state_swap(PyThreadState *state) { + return nullptr; +} +PyThreadState *(*global_thread_state_swap)(PyThreadState *tstate) = default_thread_state_swap; +#endif // HAVE_PYTHON + #endif // HAVE_THREADS && SIMPLE_THREADS diff --git a/dtool/src/dtoolbase/dtoolbase_cc.h b/dtool/src/dtoolbase/dtoolbase_cc.h index 36da6b2b0b..8894469422 100644 --- a/dtool/src/dtoolbase/dtoolbase_cc.h +++ b/dtool/src/dtoolbase/dtoolbase_cc.h @@ -233,6 +233,15 @@ INLINE void thread_consider_yield() { (*global_thread_consider_yield)(); } +#ifdef HAVE_PYTHON +typedef struct _ts PyThreadState; +extern EXPCL_DTOOL_DTOOLBASE PyThreadState *(*global_thread_state_swap)(PyThreadState *tstate); + +INLINE PyThreadState *thread_state_swap(PyThreadState *tstate) { + return (*global_thread_state_swap)(tstate); +} +#endif // HAVE_PYTHON + #else INLINE void thread_yield() { diff --git a/dtool/src/interrogatedb/py_panda.cxx b/dtool/src/interrogatedb/py_panda.cxx index 42bcdb9a35..dbb97ed0ff 100644 --- a/dtool/src/interrogatedb/py_panda.cxx +++ b/dtool/src/interrogatedb/py_panda.cxx @@ -752,6 +752,11 @@ PyObject *Dtool_PyModuleInitHelper(LibraryDef *defs[], const char *modulename) { ExecutionEnvironment::shadow_environment_variable("MAIN_DIR", main_dir.to_os_specific()); PyErr_Clear(); initialized_main_dir = true; + + // Also, while we are at it, initialize the thread swap hook. +#if defined(HAVE_THREADS) && defined(SIMPLE_THREADS) + global_thread_state_swap = PyThreadState_Swap; +#endif } PyModule_AddIntConstant(module, "Dtool_PyNativeInterface", 1); diff --git a/dtool/src/parser-inc/Python.h b/dtool/src/parser-inc/Python.h index ddd660072d..5f1a98a825 100644 --- a/dtool/src/parser-inc/Python.h +++ b/dtool/src/parser-inc/Python.h @@ -28,7 +28,7 @@ typedef _typeobject PyTypeObject; typedef struct {} PyStringObject; typedef struct {} PyUnicodeObject; -class PyThreadState; +typedef struct _ts PyThreadState; typedef int Py_ssize_t; typedef struct bufferinfo Py_buffer; diff --git a/panda/src/pipeline/threadSimpleImpl.I b/panda/src/pipeline/threadSimpleImpl.I index 001f3037d4..d712892883 100644 --- a/panda/src/pipeline/threadSimpleImpl.I +++ b/panda/src/pipeline/threadSimpleImpl.I @@ -51,14 +51,6 @@ is_threading_supported() { return true; } -/** - * - */ -INLINE bool ThreadSimpleImpl:: -is_true_threads() { - return (is_os_threads != 0); -} - /** * */ diff --git a/panda/src/pipeline/threadSimpleImpl.cxx b/panda/src/pipeline/threadSimpleImpl.cxx index a631421503..14d6f7561f 100644 --- a/panda/src/pipeline/threadSimpleImpl.cxx +++ b/panda/src/pipeline/threadSimpleImpl.cxx @@ -141,8 +141,8 @@ start(ThreadPriority priority, bool joinable) { #ifdef HAVE_PYTHON // Query the current Python thread state. - _python_state = PyThreadState_Swap(nullptr); - PyThreadState_Swap(_python_state); + _python_state = thread_state_swap(nullptr); + thread_state_swap(_python_state); #endif // HAVE_PYTHON init_thread_context(_context, _stack, _stack_size, st_begin_thread, this); @@ -201,6 +201,14 @@ prepare_for_exit() { manager->prepare_for_exit(); } +/** + * + */ +bool ThreadSimpleImpl:: +is_true_threads() { + return (is_os_threads != 0); +} + /** * */ @@ -238,7 +246,7 @@ st_begin_thread(void *data) { void ThreadSimpleImpl:: begin_thread() { #ifdef HAVE_PYTHON - PyThreadState_Swap(_python_state); + thread_state_swap(_python_state); #endif // HAVE_PYTHON #ifdef HAVE_POSIX_THREADS diff --git a/panda/src/pipeline/threadSimpleImpl.h b/panda/src/pipeline/threadSimpleImpl.h index c552dd1357..5bd6af3fdc 100644 --- a/panda/src/pipeline/threadSimpleImpl.h +++ b/panda/src/pipeline/threadSimpleImpl.h @@ -63,7 +63,7 @@ public: INLINE static void bind_thread(Thread *thread); INLINE static bool is_threading_supported(); - INLINE static bool is_true_threads(); + static bool is_true_threads(); INLINE static bool is_simple_threads(); INLINE static void sleep(double seconds); INLINE static void yield(); diff --git a/panda/src/pipeline/threadSimpleManager.cxx b/panda/src/pipeline/threadSimpleManager.cxx index 10b599116b..861e19cd77 100644 --- a/panda/src/pipeline/threadSimpleManager.cxx +++ b/panda/src/pipeline/threadSimpleManager.cxx @@ -237,7 +237,7 @@ next_context() { #ifdef HAVE_PYTHON // Save the current Python thread state. - _current_thread->_python_state = PyThreadState_Swap(nullptr); + _current_thread->_python_state = thread_state_swap(nullptr); #endif // HAVE_PYTHON #ifdef DO_PSTATS @@ -258,7 +258,7 @@ next_context() { #endif // DO_PSTATS #ifdef HAVE_PYTHON - PyThreadState_Swap(_current_thread->_python_state); + thread_state_swap(_current_thread->_python_state); #endif // HAVE_PYTHON } @@ -470,16 +470,6 @@ init_pointers() { _pointers_initialized = true; _global_ptr = new ThreadSimpleManager; Thread::get_main_thread(); - -#ifdef HAVE_PYTHON - // Ensure that the Python threading system is initialized and ready to go. - -#if PY_VERSION_HEX >= 0x03020000 - Py_Initialize(); -#endif - - PyEval_InitThreads(); -#endif } }