\end{cfuncdesc}
\begin{cfuncdesc}{void}{Py_InitializeEx}{int initsigs}
- This function works like \cfunction{Py_Initialize} if
+ This function works like \cfunction{Py_Initialize()} if
\var{initsigs} is 1. If \var{initsigs} is 0, it skips
initialization registration of signal handlers, which
might be useful when Python is embedded. \versionadded{2.4}
This is a common situation (most Python programs do not use
threads), and the lock operations slow the interpreter down a bit.
Therefore, the lock is not created initially. This situation is
- equivalent to having acquired the lock: when there is only a single
+ equivalent to having acquired the lock: when there is only a single
thread, all object accesses are safe. Therefore, when this function
initializes the lock, it also acquires it. Before the Python
\module{thread}\refbimodindex{thread} module creates a new thread,
knowing that either it has the lock or the lock hasn't been created
yet, it calls \cfunction{PyEval_InitThreads()}. When this call
- returns, it is guaranteed that the lock has been created and that it
- has acquired it.
+ returns, it is guaranteed that the lock has been created and that the
+ calling thread has acquired it.
It is \strong{not} safe to call this function when it is unknown
which thread (if any) currently has the global interpreter lock.
compile time.
\end{cfuncdesc}
+\begin{cfuncdesc}{int}{PyEval_ThreadsInitialized}{}
+ Returns a non-zero value if \cfunction{PyEval_InitThreads()} has been
+ called. This function can be called without holding the lock, and
+ therefore can be used to avoid calls to the locking API when running
+ single-threaded. This function is not available when thread support
+ is disabled at compile time. \versionadded{2.4}
+\end{cfuncdesc}
+
\begin{cfuncdesc}{void}{PyEval_AcquireLock}{}
Acquire the global interpreter lock. The lock must have been
created earlier. If this thread already has the lock, a deadlock
C API
-----
+- SF patch 1044089: New function ``PyEval_ThreadsInitialized()`` returns
+ non-zero if PyEval_InitThreads() has been called.
+
+- The undocumented and unused extern int ``_PyThread_Started`` was removed.
+
- The C API calls ``PyInterpreterState_New()`` and ``PyThreadState_New()``
are two of the very few advertised as being safe to call without holding
the GIL. However, this wasn't true in a debug build, as bug 1041645
#endif
#include "pythread.h"
-extern int _PyThread_Started; /* Flag for Py_Exit */
-
static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
static long main_thread = 0;
+int
+PyEval_ThreadsInitialized(void)
+{
+ return interpreter_lock != 0;
+}
+
void
PyEval_InitThreads(void)
{
if (interpreter_lock)
return;
- _PyThread_Started = 1;
interpreter_lock = PyThread_allocate_lock();
PyThread_acquire_lock(interpreter_lock, 1);
main_thread = PyThread_get_thread_ident();