diff options
author | Jonas Devlieghere <jonas@devlieghere.com> | 2025-08-04 09:08:20 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-08-04 09:08:20 -0700 |
commit | 0ee1811624383b4601c969d72e851377f868120f (patch) | |
tree | 8f4b1867b42bd0a1d300fde46dd595b680d28318 /lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp | |
parent | 8d9afb8d955553bcc6aecba930fe94264b3ceebc (diff) | |
download | llvm-0ee1811624383b4601c969d72e851377f868120f.zip llvm-0ee1811624383b4601c969d72e851377f868120f.tar.gz llvm-0ee1811624383b4601c969d72e851377f868120f.tar.bz2 |
[lldb] Eliminate InitializePythonRAII::InitializeThreadsPrivate (NFC) (#151780)
The behavior of thread initialization changed in Python 3.7. The minimum
supported Python version is now 3.8. That means that
`PyEval_ThreadsInitialized` always returns true and `PyEval_InitThreads`
is never called.
The helper function existed to coordinate initializing the threads and
acquiring the GIL, which is no longer necessary. With that, there's no
point in having the helper at all. This PR eliminates the function and
inlines to GIL acquisition into the caller.
Diffstat (limited to 'lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp')
-rw-r--r-- | lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp | 51 |
1 files changed, 10 insertions, 41 deletions
diff --git a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp index 300518f2..fc4df81 100644 --- a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp +++ b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp @@ -137,7 +137,16 @@ public: config.install_signal_handlers = 0; Py_InitializeFromConfig(&config); PyConfig_Clear(&config); - InitializeThreadsPrivate(); + + // The only case we should go further and acquire the GIL: it is unlocked. + if (PyGILState_Check()) + return; + + m_was_already_initialized = true; + m_gil_state = PyGILState_Ensure(); + LLDB_LOGV(GetLog(LLDBLog::Script), + "Ensured PyGILState. Previous state = {0}locked\n", + m_gil_state == PyGILState_UNLOCKED ? "un" : ""); } ~InitializePythonRAII() { @@ -153,46 +162,6 @@ public: } private: - void InitializeThreadsPrivate() { - // Since Python 3.7 `Py_Initialize` calls `PyEval_InitThreads` inside - // itself, so there is no way to determine whether the embedded interpreter - // was already initialized by some external code. - // `PyEval_ThreadsInitialized` would always return `true` and - // `PyGILState_Ensure/Release` flow would be executed instead of unlocking - // GIL with `PyEval_SaveThread`. When an another thread calls - // `PyGILState_Ensure` it would get stuck in deadlock. - - // The only case we should go further and acquire the GIL: it is unlocked. - if (PyGILState_Check()) - return; - -// `PyEval_ThreadsInitialized` was deprecated in Python 3.9 and removed in -// Python 3.13. It has been returning `true` always since Python 3.7. -#if PY_VERSION_HEX < 0x03090000 - if (PyEval_ThreadsInitialized()) { -#else - if (true) { -#endif - Log *log = GetLog(LLDBLog::Script); - - m_was_already_initialized = true; - m_gil_state = PyGILState_Ensure(); - LLDB_LOGV(log, "Ensured PyGILState. Previous state = {0}locked\n", - m_gil_state == PyGILState_UNLOCKED ? "un" : ""); - -// `PyEval_InitThreads` was deprecated in Python 3.9 and removed in -// Python 3.13. -#if PY_VERSION_HEX < 0x03090000 - return; - } - - // InitThreads acquires the GIL if it hasn't been called before. - PyEval_InitThreads(); -#else - } -#endif - } - PyGILState_STATE m_gil_state = PyGILState_UNLOCKED; bool m_was_already_initialized = false; }; |