Skip to content

Commit 8d98d2c

Browse files
committed
New PyGILState_ API - implements pep 311, from patch 684256.
1 parent e36b690 commit 8d98d2c

File tree

10 files changed

+395
-131
lines changed

10 files changed

+395
-131
lines changed

Include/pystate.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ typedef struct _ts {
7272
PyObject *dict;
7373

7474
int tick_counter;
75+
int gilstate_counter;
7576

7677
/* XXX signal handlers should also be here */
7778

@@ -104,6 +105,51 @@ PyAPI_DATA(PyThreadState *) _PyThreadState_Current;
104105
#define PyThreadState_GET() (_PyThreadState_Current)
105106
#endif
106107

108+
typedef
109+
enum {PyGILState_LOCKED, PyGILState_UNLOCKED}
110+
PyGILState_STATE;
111+
112+
/* Ensure that the current thread is ready to call the Python
113+
C API, regardless of the current state of Python, or of its
114+
thread lock. This may be called as many times as desired
115+
by a thread so long as each call is matched with a call to
116+
PyGILState_Release(). In general, other thread-state APIs may
117+
be used between _Ensure() and _Release() calls, so long as the
118+
thread-state is restored to its previous state before the Release().
119+
For example, normal use of the Py_BEGIN_ALLOW_THREADS/
120+
Py_END_ALLOW_THREADS macros are acceptable.
121+
122+
The return value is an opaque "handle" to the thread state when
123+
PyGILState_Acquire() was called, and must be passed to
124+
PyGILState_Release() to ensure Python is left in the same state. Even
125+
though recursive calls are allowed, these handles can *not* be shared -
126+
each unique call to PyGILState_Ensure must save the handle for its
127+
call to PyGILState_Release.
128+
129+
When the function returns, the current thread will hold the GIL.
130+
131+
Failure is a fatal error.
132+
*/
133+
PyAPI_FUNC(PyGILState_STATE) PyGILState_Ensure(void);
134+
135+
/* Release any resources previously acquired. After this call, Python's
136+
state will be the same as it was prior to the corresponding
137+
PyGILState_Acquire call (but generally this state will be unknown to
138+
the caller, hence the use of the GILState API.)
139+
140+
Every call to PyGILState_Ensure must be matched by a call to
141+
PyGILState_Release on the same thread.
142+
*/
143+
PyAPI_FUNC(void) PyGILState_Release(PyGILState_STATE);
144+
145+
/* Helper/diagnostic function - get the current thread state for
146+
this thread. May return NULL if no GILState API has been used
147+
on the current thread. Note the main thread always has such a
148+
thread-state, even if no auto-thread-state call has been made
149+
on the main thread.
150+
*/
151+
PyAPI_FUNC(PyThreadState *) PyGILState_GetThisThreadState(void);
152+
107153
/* Routines for advanced debuggers, requested by David Beazley.
108154
Don't use unless you know what you are doing! */
109155
PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Head(void);

Include/pythread.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ PyAPI_FUNC(void) PyThread_exit_prog(int);
3030
PyAPI_FUNC(void) PyThread__PyThread_exit_prog(int);
3131
#endif
3232

33+
/* Thread Local Storage (TLS) API */
3334
PyAPI_FUNC(int) PyThread_create_key(void);
3435
PyAPI_FUNC(void) PyThread_delete_key(int);
3536
PyAPI_FUNC(int) PyThread_set_key_value(int, void *);
3637
PyAPI_FUNC(void *) PyThread_get_key_value(int);
38+
PyAPI_FUNC(void) PyThread_delete_key_value(int key);
3739

3840
#ifdef __cplusplus
3941
}

Lib/test/test_capi.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,32 @@
1414
test()
1515
except _testcapi.error:
1616
raise test_support.TestFailed, sys.exc_info()[1]
17+
18+
# some extra thread-state tests driven via _testcapi
19+
def TestThreadState():
20+
import thread
21+
import time
22+
23+
if test_support.verbose:
24+
print "auto-thread-state"
25+
26+
idents = []
27+
28+
def callback():
29+
idents.append(thread.get_ident())
30+
31+
_testcapi._test_thread_state(callback)
32+
time.sleep(1)
33+
# Check our main thread is in the list exactly 3 times.
34+
if idents.count(thread.get_ident()) != 3:
35+
raise test_support.TestFailed, \
36+
"Couldn't find main thread correctly in the list"
37+
38+
try:
39+
_testcapi._test_thread_state
40+
have_thread_state = True
41+
except AttributeError:
42+
have_thread_state = False
43+
44+
if have_thread_state:
45+
TestThreadState()

Modules/_testcapimodule.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88
#include "Python.h"
99

10+
#ifdef WITH_THREAD
11+
#include "pythread.h"
12+
#endif /* WITH_THREAD */
13+
1014
static PyObject *TestError; /* set to exception object in init */
1115

1216
/* Raise TestError with test_name + ": " + msg, and return NULL. */
@@ -535,6 +539,46 @@ raise_exception(PyObject *self, PyObject *args)
535539
return NULL;
536540
}
537541

542+
#ifdef WITH_THREAD
543+
544+
void _make_call(void *callable)
545+
{
546+
PyObject *rc;
547+
PyGILState_STATE s = PyGILState_Ensure();
548+
rc = PyObject_CallFunction(callable, "");
549+
Py_XDECREF(rc);
550+
PyGILState_Release(s);
551+
}
552+
553+
static PyObject *
554+
test_thread_state(PyObject *self, PyObject *args)
555+
{
556+
PyObject *fn;
557+
if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn))
558+
return NULL;
559+
/* Ensure Python is setup for threading */
560+
PyEval_InitThreads();
561+
/* Start a new thread for our callback. */
562+
PyThread_start_new_thread( _make_call, fn);
563+
/* Make the callback with the thread lock held by this thread */
564+
_make_call(fn);
565+
/* Do it all again, but this time with the thread-lock released */
566+
Py_BEGIN_ALLOW_THREADS
567+
_make_call(fn);
568+
Py_END_ALLOW_THREADS
569+
/* And once more with and without a thread
570+
XXX - should use a lock and work out exactly what we are trying
571+
to test <wink>
572+
*/
573+
Py_BEGIN_ALLOW_THREADS
574+
PyThread_start_new_thread( _make_call, fn);
575+
_make_call(fn);
576+
Py_END_ALLOW_THREADS
577+
Py_INCREF(Py_None);
578+
return Py_None;
579+
}
580+
#endif
581+
538582
static PyMethodDef TestMethods[] = {
539583
{"raise_exception", raise_exception, METH_VARARGS},
540584
{"test_config", (PyCFunction)test_config, METH_NOARGS},
@@ -553,6 +597,9 @@ static PyMethodDef TestMethods[] = {
553597
#endif
554598
#ifdef Py_USING_UNICODE
555599
{"test_u_code", (PyCFunction)test_u_code, METH_NOARGS},
600+
#endif
601+
#ifdef WITH_THREAD
602+
{"_test_thread_state", (PyCFunction)test_thread_state, METH_VARARGS},
556603
#endif
557604
{NULL, NULL} /* sentinel */
558605
};

Modules/posixmodule.c

Lines changed: 6 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4354,22 +4354,11 @@ _PyPopen(char *cmdstring, int mode, int n)
43544354
* exit code as the result of the close() operation. This permits the
43554355
* files to be closed in any order - it is always the close() of the
43564356
* final handle that will return the exit code.
4357+
*
4358+
* NOTE: This function is currently called with the GIL released.
4359+
* hence we use the GILState API to manage our state.
43574360
*/
43584361

4359-
/* RED_FLAG 31-Aug-2000 Tim
4360-
* This is always called (today!) between a pair of
4361-
* Py_BEGIN_ALLOW_THREADS/ Py_END_ALLOW_THREADS
4362-
* macros. So the thread running this has no valid thread state, as
4363-
* far as Python is concerned. However, this calls some Python API
4364-
* functions that cannot be called safely without a valid thread
4365-
* state, in particular PyDict_GetItem.
4366-
* As a temporary hack (although it may last for years ...), we
4367-
* *rely* on not having a valid thread state in this function, in
4368-
* order to create our own "from scratch".
4369-
* This will deadlock if _PyPclose is ever called by a thread
4370-
* holding the global lock.
4371-
*/
4372-
43734362
static int _PyPclose(FILE *file)
43744363
{
43754364
int result;
@@ -4378,40 +4367,16 @@ static int _PyPclose(FILE *file)
43784367
PyObject *procObj, *hProcessObj, *intObj, *fileObj;
43794368
long file_count;
43804369
#ifdef WITH_THREAD
4381-
PyInterpreterState* pInterpreterState;
4382-
PyThreadState* pThreadState;
4370+
PyGILState_STATE state;
43834371
#endif
43844372

43854373
/* Close the file handle first, to ensure it can't block the
43864374
* child from exiting if it's the last handle.
43874375
*/
43884376
result = fclose(file);
4389-
43904377
#ifdef WITH_THREAD
4391-
/* Bootstrap a valid thread state into existence. */
4392-
pInterpreterState = PyInterpreterState_New();
4393-
if (!pInterpreterState) {
4394-
/* Well, we're hosed now! We don't have a thread
4395-
* state, so can't call a nice error routine, or raise
4396-
* an exception. Just die.
4397-
*/
4398-
Py_FatalError("unable to allocate interpreter state "
4399-
"when closing popen object");
4400-
return -1; /* unreachable */
4401-
}
4402-
pThreadState = PyThreadState_New(pInterpreterState);
4403-
if (!pThreadState) {
4404-
Py_FatalError("unable to allocate thread state "
4405-
"when closing popen object");
4406-
return -1; /* unreachable */
4407-
}
4408-
/* Grab the global lock. Note that this will deadlock if the
4409-
* current thread already has the lock! (see RED_FLAG comments
4410-
* before this function)
4411-
*/
4412-
PyEval_RestoreThread(pThreadState);
4378+
state = PyGILState_Ensure();
44134379
#endif
4414-
44154380
if (_PyPopenProcs) {
44164381
if ((fileObj = PyLong_FromVoidPtr(file)) != NULL &&
44174382
(procObj = PyDict_GetItem(_PyPopenProcs,
@@ -4470,17 +4435,8 @@ static int _PyPclose(FILE *file)
44704435
} /* if _PyPopenProcs */
44714436

44724437
#ifdef WITH_THREAD
4473-
/* Tear down the thread & interpreter states.
4474-
* Note that interpreter state clear & delete functions automatically
4475-
* call the thread clear & delete functions, and indeed insist on
4476-
* doing that themselves. The lock must be held during the clear, but
4477-
* need not be held during the delete.
4478-
*/
4479-
PyInterpreterState_Clear(pInterpreterState);
4480-
PyEval_ReleaseThread(pThreadState);
4481-
PyInterpreterState_Delete(pInterpreterState);
4438+
PyGILState_Release(state);
44824439
#endif
4483-
44844440
return result;
44854441
}
44864442

Python/ceval.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,8 @@ PyEval_AcquireThread(PyThreadState *tstate)
321321
{
322322
if (tstate == NULL)
323323
Py_FatalError("PyEval_AcquireThread: NULL new thread state");
324+
/* Check someone has called PyEval_InitThreads() to create the lock */
325+
assert(interpreter_lock);
324326
PyThread_acquire_lock(interpreter_lock, 1);
325327
if (PyThreadState_Swap(tstate) != NULL)
326328
Py_FatalError(

0 commit comments

Comments
 (0)