Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7f468f2

Browse files
committed
SF patch 1044089: New C API function PyEval_ThreadsInitialized(), by Nick
Coghlan, for determining whether PyEval_InitThreads() has been called. Also purged the undocumented+unused _PyThread_Started int.
1 parent 89c0ec9 commit 7f468f2

7 files changed

Lines changed: 24 additions & 10 deletions

File tree

Doc/api/init.tex

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ \chapter{Initialization, Finalization, and Threads
2424
\end{cfuncdesc}
2525

2626
\begin{cfuncdesc}{void}{Py_InitializeEx}{int initsigs}
27-
This function works like \cfunction{Py_Initialize} if
27+
This function works like \cfunction{Py_Initialize()} if
2828
\var{initsigs} is 1. If \var{initsigs} is 0, it skips
2929
initialization registration of signal handlers, which
3030
might be useful when Python is embedded. \versionadded{2.4}
@@ -517,14 +517,14 @@ \section{Thread State and the Global Interpreter Lock
517517
This is a common situation (most Python programs do not use
518518
threads), and the lock operations slow the interpreter down a bit.
519519
Therefore, the lock is not created initially. This situation is
520-
equivalent to having acquired the lock: when there is only a single
520+
equivalent to having acquired the lock: when there is only a single
521521
thread, all object accesses are safe. Therefore, when this function
522522
initializes the lock, it also acquires it. Before the Python
523523
\module{thread}\refbimodindex{thread} module creates a new thread,
524524
knowing that either it has the lock or the lock hasn't been created
525525
yet, it calls \cfunction{PyEval_InitThreads()}. When this call
526-
returns, it is guaranteed that the lock has been created and that it
527-
has acquired it.
526+
returns, it is guaranteed that the lock has been created and that the
527+
calling thread has acquired it.
528528

529529
It is \strong{not} safe to call this function when it is unknown
530530
which thread (if any) currently has the global interpreter lock.
@@ -533,6 +533,14 @@ \section{Thread State and the Global Interpreter Lock
533533
compile time.
534534
\end{cfuncdesc}
535535

536+
\begin{cfuncdesc}{int}{PyEval_ThreadsInitialized}{}
537+
Returns a non-zero value if \cfunction{PyEval_InitThreads()} has been
538+
called. This function can be called without holding the lock, and
539+
therefore can be used to avoid calls to the locking API when running
540+
single-threaded. This function is not available when thread support
541+
is disabled at compile time. \versionadded{2.4}
542+
\end{cfuncdesc}
543+
536544
\begin{cfuncdesc}{void}{PyEval_AcquireLock}{}
537545
Acquire the global interpreter lock. The lock must have been
538546
created earlier. If this thread already has the lock, a deadlock

Include/ceval.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ PyAPI_FUNC(void) PyEval_RestoreThread(PyThreadState *);
120120

121121
#ifdef WITH_THREAD
122122

123+
PyAPI_FUNC(int) PyEval_ThreadsInitialized(void);
123124
PyAPI_FUNC(void) PyEval_InitThreads(void);
124125
PyAPI_FUNC(void) PyEval_AcquireLock(void);
125126
PyAPI_FUNC(void) PyEval_ReleaseLock(void);

Misc/NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ Build
136136
C API
137137
-----
138138

139+
- SF patch 1044089: New function ``PyEval_ThreadsInitialized()`` returns
140+
non-zero if PyEval_InitThreads() has been called.
141+
142+
- The undocumented and unused extern int ``_PyThread_Started`` was removed.
143+
139144
- The C API calls ``PyInterpreterState_New()`` and ``PyThreadState_New()``
140145
are two of the very few advertised as being safe to call without holding
141146
the GIL. However, this wasn't true in a debug build, as bug 1041645

PC/os2emx/python24.def

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -980,7 +980,6 @@ EXPORTS
980980
"Py_UseClassExceptionsFlag"
981981
"Py_UnicodeFlag"
982982
"_Py_QnewFlag"
983-
"_PyThread_Started"
984983

985984
; From python24_s.lib(structmember)
986985
"PyMember_Get"

PC/os2vacpp/python.def

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ EXPORTS
6969
_PyImport_Inittab
7070
_PyParser_Grammar
7171
_PyParser_TokenNames
72-
_PyThread_Started
7372
_Py_EllipsisObject
7473
_Py_NoneStruct
7574
_Py_PackageContext

Python/ceval.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,17 +203,20 @@ PyEval_GetCallStats(PyObject *self)
203203
#endif
204204
#include "pythread.h"
205205

206-
extern int _PyThread_Started; /* Flag for Py_Exit */
207-
208206
static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
209207
static long main_thread = 0;
210208

209+
int
210+
PyEval_ThreadsInitialized(void)
211+
{
212+
return interpreter_lock != 0;
213+
}
214+
211215
void
212216
PyEval_InitThreads(void)
213217
{
214218
if (interpreter_lock)
215219
return;
216-
_PyThread_Started = 1;
217220
interpreter_lock = PyThread_allocate_lock();
218221
PyThread_acquire_lock(interpreter_lock, 1);
219222
main_thread = PyThread_get_thread_ident();

Python/pythonrun.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1517,7 +1517,6 @@ Py_FatalError(const char *msg)
15171517

15181518
#ifdef WITH_THREAD
15191519
#include "pythread.h"
1520-
int _PyThread_Started = 0; /* Set by threadmodule.c and maybe others */
15211520
#endif
15221521

15231522
#define NEXITFUNCS 32

0 commit comments

Comments
 (0)