1 |
|
---|
2 | /* Thread and interpreter state structures and their interfaces */
|
---|
3 |
|
---|
4 | #include "Python.h"
|
---|
5 |
|
---|
6 | /* --------------------------------------------------------------------------
|
---|
7 | CAUTION
|
---|
8 |
|
---|
9 | Always use malloc() and free() directly in this file. A number of these
|
---|
10 | functions are advertised as safe to call when the GIL isn't held, and in
|
---|
11 | a debug build Python redirects (e.g.) PyMem_NEW (etc) to Python's debugging
|
---|
12 | obmalloc functions. Those aren't thread-safe (they rely on the GIL to avoid
|
---|
13 | the expense of doing their own locking).
|
---|
14 | -------------------------------------------------------------------------- */
|
---|
15 |
|
---|
16 | #ifdef HAVE_DLOPEN
|
---|
17 | #ifdef HAVE_DLFCN_H
|
---|
18 | #include <dlfcn.h>
|
---|
19 | #endif
|
---|
20 | #ifndef RTLD_LAZY
|
---|
21 | #define RTLD_LAZY 1
|
---|
22 | #endif
|
---|
23 | #endif
|
---|
24 |
|
---|
25 |
|
---|
26 | #ifdef WITH_THREAD
|
---|
27 | #include "pythread.h"
|
---|
28 | static PyThread_type_lock head_mutex = NULL; /* Protects interp->tstate_head */
|
---|
29 | #define HEAD_INIT() (void)(head_mutex || (head_mutex = PyThread_allocate_lock()))
|
---|
30 | #define HEAD_LOCK() PyThread_acquire_lock(head_mutex, WAIT_LOCK)
|
---|
31 | #define HEAD_UNLOCK() PyThread_release_lock(head_mutex)
|
---|
32 |
|
---|
33 | #ifdef __cplusplus
|
---|
34 | extern "C" {
|
---|
35 | #endif
|
---|
36 |
|
---|
37 | /* The single PyInterpreterState used by this process'
|
---|
38 | GILState implementation
|
---|
39 | */
|
---|
40 | static PyInterpreterState *autoInterpreterState = NULL;
|
---|
41 | static int autoTLSkey = 0;
|
---|
42 | #else
|
---|
43 | #define HEAD_INIT() /* Nothing */
|
---|
44 | #define HEAD_LOCK() /* Nothing */
|
---|
45 | #define HEAD_UNLOCK() /* Nothing */
|
---|
46 | #endif
|
---|
47 |
|
---|
48 | static PyInterpreterState *interp_head = NULL;
|
---|
49 |
|
---|
50 | PyThreadState *_PyThreadState_Current = NULL;
|
---|
51 | PyThreadFrameGetter _PyThreadState_GetFrame = NULL;
|
---|
52 |
|
---|
53 | #ifdef WITH_THREAD
|
---|
54 | static void _PyGILState_NoteThreadState(PyThreadState* tstate);
|
---|
55 | #endif
|
---|
56 |
|
---|
57 |
|
---|
58 | PyInterpreterState *
|
---|
59 | PyInterpreterState_New(void)
|
---|
60 | {
|
---|
61 | PyInterpreterState *interp = (PyInterpreterState *)
|
---|
62 | malloc(sizeof(PyInterpreterState));
|
---|
63 |
|
---|
64 | if (interp != NULL) {
|
---|
65 | HEAD_INIT();
|
---|
66 | #ifdef WITH_THREAD
|
---|
67 | if (head_mutex == NULL)
|
---|
68 | Py_FatalError("Can't initialize threads for interpreter");
|
---|
69 | #endif
|
---|
70 | interp->modules = NULL;
|
---|
71 | interp->sysdict = NULL;
|
---|
72 | interp->builtins = NULL;
|
---|
73 | interp->tstate_head = NULL;
|
---|
74 | interp->codec_search_path = NULL;
|
---|
75 | interp->codec_search_cache = NULL;
|
---|
76 | interp->codec_error_registry = NULL;
|
---|
77 | #ifdef HAVE_DLOPEN
|
---|
78 | #ifdef RTLD_NOW
|
---|
79 | interp->dlopenflags = RTLD_NOW;
|
---|
80 | #else
|
---|
81 | interp->dlopenflags = RTLD_LAZY;
|
---|
82 | #endif
|
---|
83 | #endif
|
---|
84 | #ifdef WITH_TSC
|
---|
85 | interp->tscdump = 0;
|
---|
86 | #endif
|
---|
87 |
|
---|
88 | HEAD_LOCK();
|
---|
89 | interp->next = interp_head;
|
---|
90 | interp_head = interp;
|
---|
91 | HEAD_UNLOCK();
|
---|
92 | }
|
---|
93 |
|
---|
94 | return interp;
|
---|
95 | }
|
---|
96 |
|
---|
97 |
|
---|
98 | void
|
---|
99 | PyInterpreterState_Clear(PyInterpreterState *interp)
|
---|
100 | {
|
---|
101 | PyThreadState *p;
|
---|
102 | HEAD_LOCK();
|
---|
103 | for (p = interp->tstate_head; p != NULL; p = p->next)
|
---|
104 | PyThreadState_Clear(p);
|
---|
105 | HEAD_UNLOCK();
|
---|
106 | Py_CLEAR(interp->codec_search_path);
|
---|
107 | Py_CLEAR(interp->codec_search_cache);
|
---|
108 | Py_CLEAR(interp->codec_error_registry);
|
---|
109 | Py_CLEAR(interp->modules);
|
---|
110 | Py_CLEAR(interp->sysdict);
|
---|
111 | Py_CLEAR(interp->builtins);
|
---|
112 | }
|
---|
113 |
|
---|
114 |
|
---|
115 | static void
|
---|
116 | zapthreads(PyInterpreterState *interp)
|
---|
117 | {
|
---|
118 | PyThreadState *p;
|
---|
119 | /* No need to lock the mutex here because this should only happen
|
---|
120 | when the threads are all really dead (XXX famous last words). */
|
---|
121 | while ((p = interp->tstate_head) != NULL) {
|
---|
122 | PyThreadState_Delete(p);
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 |
|
---|
127 | void
|
---|
128 | PyInterpreterState_Delete(PyInterpreterState *interp)
|
---|
129 | {
|
---|
130 | PyInterpreterState **p;
|
---|
131 | zapthreads(interp);
|
---|
132 | HEAD_LOCK();
|
---|
133 | for (p = &interp_head; ; p = &(*p)->next) {
|
---|
134 | if (*p == NULL)
|
---|
135 | Py_FatalError(
|
---|
136 | "PyInterpreterState_Delete: invalid interp");
|
---|
137 | if (*p == interp)
|
---|
138 | break;
|
---|
139 | }
|
---|
140 | if (interp->tstate_head != NULL)
|
---|
141 | Py_FatalError("PyInterpreterState_Delete: remaining threads");
|
---|
142 | *p = interp->next;
|
---|
143 | HEAD_UNLOCK();
|
---|
144 | free(interp);
|
---|
145 | }
|
---|
146 |
|
---|
147 |
|
---|
148 | /* Default implementation for _PyThreadState_GetFrame */
|
---|
149 | static struct _frame *
|
---|
150 | threadstate_getframe(PyThreadState *self)
|
---|
151 | {
|
---|
152 | return self->frame;
|
---|
153 | }
|
---|
154 |
|
---|
155 | PyThreadState *
|
---|
156 | PyThreadState_New(PyInterpreterState *interp)
|
---|
157 | {
|
---|
158 | PyThreadState *tstate = (PyThreadState *)malloc(sizeof(PyThreadState));
|
---|
159 |
|
---|
160 | if (_PyThreadState_GetFrame == NULL)
|
---|
161 | _PyThreadState_GetFrame = threadstate_getframe;
|
---|
162 |
|
---|
163 | if (tstate != NULL) {
|
---|
164 | tstate->interp = interp;
|
---|
165 |
|
---|
166 | tstate->frame = NULL;
|
---|
167 | tstate->recursion_depth = 0;
|
---|
168 | tstate->tracing = 0;
|
---|
169 | tstate->use_tracing = 0;
|
---|
170 | tstate->tick_counter = 0;
|
---|
171 | tstate->gilstate_counter = 0;
|
---|
172 | tstate->async_exc = NULL;
|
---|
173 | #ifdef WITH_THREAD
|
---|
174 | tstate->thread_id = PyThread_get_thread_ident();
|
---|
175 | #else
|
---|
176 | tstate->thread_id = 0;
|
---|
177 | #endif
|
---|
178 |
|
---|
179 | tstate->dict = NULL;
|
---|
180 |
|
---|
181 | tstate->curexc_type = NULL;
|
---|
182 | tstate->curexc_value = NULL;
|
---|
183 | tstate->curexc_traceback = NULL;
|
---|
184 |
|
---|
185 | tstate->exc_type = NULL;
|
---|
186 | tstate->exc_value = NULL;
|
---|
187 | tstate->exc_traceback = NULL;
|
---|
188 |
|
---|
189 | tstate->c_profilefunc = NULL;
|
---|
190 | tstate->c_tracefunc = NULL;
|
---|
191 | tstate->c_profileobj = NULL;
|
---|
192 | tstate->c_traceobj = NULL;
|
---|
193 |
|
---|
194 | #ifdef WITH_THREAD
|
---|
195 | _PyGILState_NoteThreadState(tstate);
|
---|
196 | #endif
|
---|
197 |
|
---|
198 | HEAD_LOCK();
|
---|
199 | tstate->next = interp->tstate_head;
|
---|
200 | interp->tstate_head = tstate;
|
---|
201 | HEAD_UNLOCK();
|
---|
202 | }
|
---|
203 |
|
---|
204 | return tstate;
|
---|
205 | }
|
---|
206 |
|
---|
207 |
|
---|
208 | void
|
---|
209 | PyThreadState_Clear(PyThreadState *tstate)
|
---|
210 | {
|
---|
211 | if (Py_VerboseFlag && tstate->frame != NULL)
|
---|
212 | fprintf(stderr,
|
---|
213 | "PyThreadState_Clear: warning: thread still has a frame\n");
|
---|
214 |
|
---|
215 | Py_CLEAR(tstate->frame);
|
---|
216 |
|
---|
217 | Py_CLEAR(tstate->dict);
|
---|
218 | Py_CLEAR(tstate->async_exc);
|
---|
219 |
|
---|
220 | Py_CLEAR(tstate->curexc_type);
|
---|
221 | Py_CLEAR(tstate->curexc_value);
|
---|
222 | Py_CLEAR(tstate->curexc_traceback);
|
---|
223 |
|
---|
224 | Py_CLEAR(tstate->exc_type);
|
---|
225 | Py_CLEAR(tstate->exc_value);
|
---|
226 | Py_CLEAR(tstate->exc_traceback);
|
---|
227 |
|
---|
228 | tstate->c_profilefunc = NULL;
|
---|
229 | tstate->c_tracefunc = NULL;
|
---|
230 | Py_CLEAR(tstate->c_profileobj);
|
---|
231 | Py_CLEAR(tstate->c_traceobj);
|
---|
232 | }
|
---|
233 |
|
---|
234 |
|
---|
235 | /* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
|
---|
236 | static void
|
---|
237 | tstate_delete_common(PyThreadState *tstate)
|
---|
238 | {
|
---|
239 | PyInterpreterState *interp;
|
---|
240 | PyThreadState **p;
|
---|
241 | if (tstate == NULL)
|
---|
242 | Py_FatalError("PyThreadState_Delete: NULL tstate");
|
---|
243 | interp = tstate->interp;
|
---|
244 | if (interp == NULL)
|
---|
245 | Py_FatalError("PyThreadState_Delete: NULL interp");
|
---|
246 | HEAD_LOCK();
|
---|
247 | for (p = &interp->tstate_head; ; p = &(*p)->next) {
|
---|
248 | if (*p == NULL)
|
---|
249 | Py_FatalError(
|
---|
250 | "PyThreadState_Delete: invalid tstate");
|
---|
251 | if (*p == tstate)
|
---|
252 | break;
|
---|
253 | }
|
---|
254 | *p = tstate->next;
|
---|
255 | HEAD_UNLOCK();
|
---|
256 | free(tstate);
|
---|
257 | }
|
---|
258 |
|
---|
259 |
|
---|
260 | void
|
---|
261 | PyThreadState_Delete(PyThreadState *tstate)
|
---|
262 | {
|
---|
263 | if (tstate == _PyThreadState_Current)
|
---|
264 | Py_FatalError("PyThreadState_Delete: tstate is still current");
|
---|
265 | tstate_delete_common(tstate);
|
---|
266 | #ifdef WITH_THREAD
|
---|
267 | if (autoTLSkey && PyThread_get_key_value(autoTLSkey) == tstate)
|
---|
268 | PyThread_delete_key_value(autoTLSkey);
|
---|
269 | #endif /* WITH_THREAD */
|
---|
270 | }
|
---|
271 |
|
---|
272 |
|
---|
273 | #ifdef WITH_THREAD
|
---|
274 | void
|
---|
275 | PyThreadState_DeleteCurrent()
|
---|
276 | {
|
---|
277 | PyThreadState *tstate = _PyThreadState_Current;
|
---|
278 | if (tstate == NULL)
|
---|
279 | Py_FatalError(
|
---|
280 | "PyThreadState_DeleteCurrent: no current tstate");
|
---|
281 | _PyThreadState_Current = NULL;
|
---|
282 | tstate_delete_common(tstate);
|
---|
283 | if (autoTLSkey && PyThread_get_key_value(autoTLSkey) == tstate)
|
---|
284 | PyThread_delete_key_value(autoTLSkey);
|
---|
285 | PyEval_ReleaseLock();
|
---|
286 | }
|
---|
287 | #endif /* WITH_THREAD */
|
---|
288 |
|
---|
289 |
|
---|
290 | PyThreadState *
|
---|
291 | PyThreadState_Get(void)
|
---|
292 | {
|
---|
293 | if (_PyThreadState_Current == NULL)
|
---|
294 | Py_FatalError("PyThreadState_Get: no current thread");
|
---|
295 |
|
---|
296 | return _PyThreadState_Current;
|
---|
297 | }
|
---|
298 |
|
---|
299 |
|
---|
300 | PyThreadState *
|
---|
301 | PyThreadState_Swap(PyThreadState *newts)
|
---|
302 | {
|
---|
303 | PyThreadState *oldts = _PyThreadState_Current;
|
---|
304 |
|
---|
305 | _PyThreadState_Current = newts;
|
---|
306 | /* It should not be possible for more than one thread state
|
---|
307 | to be used for a thread. Check this the best we can in debug
|
---|
308 | builds.
|
---|
309 | */
|
---|
310 | #if defined(Py_DEBUG) && defined(WITH_THREAD)
|
---|
311 | if (newts) {
|
---|
312 | /* This can be called from PyEval_RestoreThread(). Similar
|
---|
313 | to it, we need to ensure errno doesn't change.
|
---|
314 | */
|
---|
315 | int err = errno;
|
---|
316 | PyThreadState *check = PyGILState_GetThisThreadState();
|
---|
317 | if (check && check->interp == newts->interp && check != newts)
|
---|
318 | Py_FatalError("Invalid thread state for this thread");
|
---|
319 | errno = err;
|
---|
320 | }
|
---|
321 | #endif
|
---|
322 | return oldts;
|
---|
323 | }
|
---|
324 |
|
---|
325 | /* An extension mechanism to store arbitrary additional per-thread state.
|
---|
326 | PyThreadState_GetDict() returns a dictionary that can be used to hold such
|
---|
327 | state; the caller should pick a unique key and store its state there. If
|
---|
328 | PyThreadState_GetDict() returns NULL, an exception has *not* been raised
|
---|
329 | and the caller should assume no per-thread state is available. */
|
---|
330 |
|
---|
331 | PyObject *
|
---|
332 | PyThreadState_GetDict(void)
|
---|
333 | {
|
---|
334 | if (_PyThreadState_Current == NULL)
|
---|
335 | return NULL;
|
---|
336 |
|
---|
337 | if (_PyThreadState_Current->dict == NULL) {
|
---|
338 | PyObject *d;
|
---|
339 | _PyThreadState_Current->dict = d = PyDict_New();
|
---|
340 | if (d == NULL)
|
---|
341 | PyErr_Clear();
|
---|
342 | }
|
---|
343 | return _PyThreadState_Current->dict;
|
---|
344 | }
|
---|
345 |
|
---|
346 |
|
---|
347 | /* Asynchronously raise an exception in a thread.
|
---|
348 | Requested by Just van Rossum and Alex Martelli.
|
---|
349 | To prevent naive misuse, you must write your own extension
|
---|
350 | to call this, or use ctypes. Must be called with the GIL held.
|
---|
351 | Returns the number of tstates modified (normally 1, but 0 if `id` didn't
|
---|
352 | match any known thread id). Can be called with exc=NULL to clear an
|
---|
353 | existing async exception. This raises no exceptions. */
|
---|
354 |
|
---|
355 | int
|
---|
356 | PyThreadState_SetAsyncExc(long id, PyObject *exc) {
|
---|
357 | PyThreadState *tstate = PyThreadState_GET();
|
---|
358 | PyInterpreterState *interp = tstate->interp;
|
---|
359 | PyThreadState *p;
|
---|
360 |
|
---|
361 | /* Although the GIL is held, a few C API functions can be called
|
---|
362 | * without the GIL held, and in particular some that create and
|
---|
363 | * destroy thread and interpreter states. Those can mutate the
|
---|
364 | * list of thread states we're traversing, so to prevent that we lock
|
---|
365 | * head_mutex for the duration.
|
---|
366 | */
|
---|
367 | HEAD_LOCK();
|
---|
368 | for (p = interp->tstate_head; p != NULL; p = p->next) {
|
---|
369 | if (p->thread_id == id) {
|
---|
370 | /* Tricky: we need to decref the current value
|
---|
371 | * (if any) in p->async_exc, but that can in turn
|
---|
372 | * allow arbitrary Python code to run, including
|
---|
373 | * perhaps calls to this function. To prevent
|
---|
374 | * deadlock, we need to release head_mutex before
|
---|
375 | * the decref.
|
---|
376 | */
|
---|
377 | PyObject *old_exc = p->async_exc;
|
---|
378 | Py_XINCREF(exc);
|
---|
379 | p->async_exc = exc;
|
---|
380 | HEAD_UNLOCK();
|
---|
381 | Py_XDECREF(old_exc);
|
---|
382 | return 1;
|
---|
383 | }
|
---|
384 | }
|
---|
385 | HEAD_UNLOCK();
|
---|
386 | return 0;
|
---|
387 | }
|
---|
388 |
|
---|
389 |
|
---|
390 | /* Routines for advanced debuggers, requested by David Beazley.
|
---|
391 | Don't use unless you know what you are doing! */
|
---|
392 |
|
---|
393 | PyInterpreterState *
|
---|
394 | PyInterpreterState_Head(void)
|
---|
395 | {
|
---|
396 | return interp_head;
|
---|
397 | }
|
---|
398 |
|
---|
399 | PyInterpreterState *
|
---|
400 | PyInterpreterState_Next(PyInterpreterState *interp) {
|
---|
401 | return interp->next;
|
---|
402 | }
|
---|
403 |
|
---|
404 | PyThreadState *
|
---|
405 | PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
|
---|
406 | return interp->tstate_head;
|
---|
407 | }
|
---|
408 |
|
---|
409 | PyThreadState *
|
---|
410 | PyThreadState_Next(PyThreadState *tstate) {
|
---|
411 | return tstate->next;
|
---|
412 | }
|
---|
413 |
|
---|
414 | /* The implementation of sys._current_frames(). This is intended to be
|
---|
415 | called with the GIL held, as it will be when called via
|
---|
416 | sys._current_frames(). It's possible it would work fine even without
|
---|
417 | the GIL held, but haven't thought enough about that.
|
---|
418 | */
|
---|
419 | PyObject *
|
---|
420 | _PyThread_CurrentFrames(void)
|
---|
421 | {
|
---|
422 | PyObject *result;
|
---|
423 | PyInterpreterState *i;
|
---|
424 |
|
---|
425 | result = PyDict_New();
|
---|
426 | if (result == NULL)
|
---|
427 | return NULL;
|
---|
428 |
|
---|
429 | /* for i in all interpreters:
|
---|
430 | * for t in all of i's thread states:
|
---|
431 | * if t's frame isn't NULL, map t's id to its frame
|
---|
432 | * Because these lists can mutute even when the GIL is held, we
|
---|
433 | * need to grab head_mutex for the duration.
|
---|
434 | */
|
---|
435 | HEAD_LOCK();
|
---|
436 | for (i = interp_head; i != NULL; i = i->next) {
|
---|
437 | PyThreadState *t;
|
---|
438 | for (t = i->tstate_head; t != NULL; t = t->next) {
|
---|
439 | PyObject *id;
|
---|
440 | int stat;
|
---|
441 | struct _frame *frame = t->frame;
|
---|
442 | if (frame == NULL)
|
---|
443 | continue;
|
---|
444 | id = PyInt_FromLong(t->thread_id);
|
---|
445 | if (id == NULL)
|
---|
446 | goto Fail;
|
---|
447 | stat = PyDict_SetItem(result, id, (PyObject *)frame);
|
---|
448 | Py_DECREF(id);
|
---|
449 | if (stat < 0)
|
---|
450 | goto Fail;
|
---|
451 | }
|
---|
452 | }
|
---|
453 | HEAD_UNLOCK();
|
---|
454 | return result;
|
---|
455 |
|
---|
456 | Fail:
|
---|
457 | HEAD_UNLOCK();
|
---|
458 | Py_DECREF(result);
|
---|
459 | return NULL;
|
---|
460 | }
|
---|
461 |
|
---|
462 | /* Python "auto thread state" API. */
|
---|
463 | #ifdef WITH_THREAD
|
---|
464 |
|
---|
465 | /* Keep this as a static, as it is not reliable! It can only
|
---|
466 | ever be compared to the state for the *current* thread.
|
---|
467 | * If not equal, then it doesn't matter that the actual
|
---|
468 | value may change immediately after comparison, as it can't
|
---|
469 | possibly change to the current thread's state.
|
---|
470 | * If equal, then the current thread holds the lock, so the value can't
|
---|
471 | change until we yield the lock.
|
---|
472 | */
|
---|
473 | static int
|
---|
474 | PyThreadState_IsCurrent(PyThreadState *tstate)
|
---|
475 | {
|
---|
476 | /* Must be the tstate for this thread */
|
---|
477 | assert(PyGILState_GetThisThreadState()==tstate);
|
---|
478 | /* On Windows at least, simple reads and writes to 32 bit values
|
---|
479 | are atomic.
|
---|
480 | */
|
---|
481 | return tstate == _PyThreadState_Current;
|
---|
482 | }
|
---|
483 |
|
---|
484 | /* Internal initialization/finalization functions called by
|
---|
485 | Py_Initialize/Py_Finalize
|
---|
486 | */
|
---|
487 | void
|
---|
488 | _PyGILState_Init(PyInterpreterState *i, PyThreadState *t)
|
---|
489 | {
|
---|
490 | assert(i && t); /* must init with valid states */
|
---|
491 | autoTLSkey = PyThread_create_key();
|
---|
492 | autoInterpreterState = i;
|
---|
493 | assert(PyThread_get_key_value(autoTLSkey) == NULL);
|
---|
494 | assert(t->gilstate_counter == 0);
|
---|
495 |
|
---|
496 | _PyGILState_NoteThreadState(t);
|
---|
497 | }
|
---|
498 |
|
---|
499 | void
|
---|
500 | _PyGILState_Fini(void)
|
---|
501 | {
|
---|
502 | PyThread_delete_key(autoTLSkey);
|
---|
503 | autoTLSkey = 0;
|
---|
504 | autoInterpreterState = NULL;;
|
---|
505 | }
|
---|
506 |
|
---|
507 | /* When a thread state is created for a thread by some mechanism other than
|
---|
508 | PyGILState_Ensure, it's important that the GILState machinery knows about
|
---|
509 | it so it doesn't try to create another thread state for the thread (this is
|
---|
510 | a better fix for SF bug #1010677 than the first one attempted).
|
---|
511 | */
|
---|
512 | static void
|
---|
513 | _PyGILState_NoteThreadState(PyThreadState* tstate)
|
---|
514 | {
|
---|
515 | /* If autoTLSkey is 0, this must be the very first threadstate created
|
---|
516 | in Py_Initialize(). Don't do anything for now (we'll be back here
|
---|
517 | when _PyGILState_Init is called). */
|
---|
518 | if (!autoTLSkey)
|
---|
519 | return;
|
---|
520 |
|
---|
521 | /* Stick the thread state for this thread in thread local storage.
|
---|
522 |
|
---|
523 | The only situation where you can legitimately have more than one
|
---|
524 | thread state for an OS level thread is when there are multiple
|
---|
525 | interpreters, when:
|
---|
526 |
|
---|
527 | a) You shouldn't really be using the PyGILState_ APIs anyway,
|
---|
528 | and:
|
---|
529 |
|
---|
530 | b) The slightly odd way PyThread_set_key_value works (see
|
---|
531 | comments by its implementation) means that the first thread
|
---|
532 | state created for that given OS level thread will "win",
|
---|
533 | which seems reasonable behaviour.
|
---|
534 | */
|
---|
535 | if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0)
|
---|
536 | Py_FatalError("Couldn't create autoTLSkey mapping");
|
---|
537 |
|
---|
538 | /* PyGILState_Release must not try to delete this thread state. */
|
---|
539 | tstate->gilstate_counter = 1;
|
---|
540 | }
|
---|
541 |
|
---|
542 | /* The public functions */
|
---|
543 | PyThreadState *
|
---|
544 | PyGILState_GetThisThreadState(void)
|
---|
545 | {
|
---|
546 | if (autoInterpreterState == NULL || autoTLSkey == 0)
|
---|
547 | return NULL;
|
---|
548 | return (PyThreadState *)PyThread_get_key_value(autoTLSkey);
|
---|
549 | }
|
---|
550 |
|
---|
551 | PyGILState_STATE
|
---|
552 | PyGILState_Ensure(void)
|
---|
553 | {
|
---|
554 | int current;
|
---|
555 | PyThreadState *tcur;
|
---|
556 | /* Note that we do not auto-init Python here - apart from
|
---|
557 | potential races with 2 threads auto-initializing, pep-311
|
---|
558 | spells out other issues. Embedders are expected to have
|
---|
559 | called Py_Initialize() and usually PyEval_InitThreads().
|
---|
560 | */
|
---|
561 | assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */
|
---|
562 | tcur = (PyThreadState *)PyThread_get_key_value(autoTLSkey);
|
---|
563 | if (tcur == NULL) {
|
---|
564 | /* Create a new thread state for this thread */
|
---|
565 | tcur = PyThreadState_New(autoInterpreterState);
|
---|
566 | if (tcur == NULL)
|
---|
567 | Py_FatalError("Couldn't create thread-state for new thread");
|
---|
568 | /* This is our thread state! We'll need to delete it in the
|
---|
569 | matching call to PyGILState_Release(). */
|
---|
570 | tcur->gilstate_counter = 0;
|
---|
571 | current = 0; /* new thread state is never current */
|
---|
572 | }
|
---|
573 | else
|
---|
574 | current = PyThreadState_IsCurrent(tcur);
|
---|
575 | if (current == 0)
|
---|
576 | PyEval_RestoreThread(tcur);
|
---|
577 | /* Update our counter in the thread-state - no need for locks:
|
---|
578 | - tcur will remain valid as we hold the GIL.
|
---|
579 | - the counter is safe as we are the only thread "allowed"
|
---|
580 | to modify this value
|
---|
581 | */
|
---|
582 | ++tcur->gilstate_counter;
|
---|
583 | return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
|
---|
584 | }
|
---|
585 |
|
---|
586 | void
|
---|
587 | PyGILState_Release(PyGILState_STATE oldstate)
|
---|
588 | {
|
---|
589 | PyThreadState *tcur = (PyThreadState *)PyThread_get_key_value(
|
---|
590 | autoTLSkey);
|
---|
591 | if (tcur == NULL)
|
---|
592 | Py_FatalError("auto-releasing thread-state, "
|
---|
593 | "but no thread-state for this thread");
|
---|
594 | /* We must hold the GIL and have our thread state current */
|
---|
595 | /* XXX - remove the check - the assert should be fine,
|
---|
596 | but while this is very new (April 2003), the extra check
|
---|
597 | by release-only users can't hurt.
|
---|
598 | */
|
---|
599 | if (! PyThreadState_IsCurrent(tcur))
|
---|
600 | Py_FatalError("This thread state must be current when releasing");
|
---|
601 | assert(PyThreadState_IsCurrent(tcur));
|
---|
602 | --tcur->gilstate_counter;
|
---|
603 | assert(tcur->gilstate_counter >= 0); /* illegal counter value */
|
---|
604 |
|
---|
605 | /* If we're going to destroy this thread-state, we must
|
---|
606 | * clear it while the GIL is held, as destructors may run.
|
---|
607 | */
|
---|
608 | if (tcur->gilstate_counter == 0) {
|
---|
609 | /* can't have been locked when we created it */
|
---|
610 | assert(oldstate == PyGILState_UNLOCKED);
|
---|
611 | PyThreadState_Clear(tcur);
|
---|
612 | /* Delete the thread-state. Note this releases the GIL too!
|
---|
613 | * It's vital that the GIL be held here, to avoid shutdown
|
---|
614 | * races; see bugs 225673 and 1061968 (that nasty bug has a
|
---|
615 | * habit of coming back).
|
---|
616 | */
|
---|
617 | PyThreadState_DeleteCurrent();
|
---|
618 | }
|
---|
619 | /* Release the lock if necessary */
|
---|
620 | else if (oldstate == PyGILState_UNLOCKED)
|
---|
621 | PyEval_SaveThread();
|
---|
622 | }
|
---|
623 |
|
---|
624 | #ifdef __cplusplus
|
---|
625 | }
|
---|
626 | #endif
|
---|
627 |
|
---|
628 | #endif /* WITH_THREAD */
|
---|
629 |
|
---|
630 |
|
---|