1
2 /* Thread and interpreter state structures and their interfaces */
3
4 #include "Python.h"
5 #include "internal/pystate.h"
6
7 #define GET_TSTATE() \
8 ((PyThreadState*)_Py_atomic_load_relaxed(&_PyThreadState_Current))
9 #define SET_TSTATE(value) \
10 _Py_atomic_store_relaxed(&_PyThreadState_Current, (uintptr_t)(value))
11 #define GET_INTERP_STATE() \
12 (GET_TSTATE()->interp)
13
14
15 /* --------------------------------------------------------------------------
16 CAUTION
17
18 Always use PyMem_RawMalloc() and PyMem_RawFree() directly in this file. A
19 number of these functions are advertised as safe to call when the GIL isn't
20 held, and in a debug build Python redirects (e.g.) PyMem_NEW (etc) to Python's
21 debugging obmalloc functions. Those aren't thread-safe (they rely on the GIL
22 to avoid the expense of doing their own locking).
23 -------------------------------------------------------------------------- */
24
25 #ifdef HAVE_DLOPEN
26 #ifdef HAVE_DLFCN_H
27 #include <dlfcn.h>
28 #endif
29 #if !HAVE_DECL_RTLD_LAZY
30 #define RTLD_LAZY 1
31 #endif
32 #endif
33
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37
38 static _PyInitError
_PyRuntimeState_Init_impl(_PyRuntimeState * runtime)39 _PyRuntimeState_Init_impl(_PyRuntimeState *runtime)
40 {
41 memset(runtime, 0, sizeof(*runtime));
42
43 _PyGC_Initialize(&runtime->gc);
44 _PyEval_Initialize(&runtime->ceval);
45
46 runtime->gilstate.check_enabled = 1;
47
48 /* A TSS key must be initialized with Py_tss_NEEDS_INIT
49 in accordance with the specification. */
50 Py_tss_t initial = Py_tss_NEEDS_INIT;
51 runtime->gilstate.autoTSSkey = initial;
52
53 runtime->interpreters.mutex = PyThread_allocate_lock();
54 if (runtime->interpreters.mutex == NULL) {
55 return _Py_INIT_ERR("Can't initialize threads for interpreter");
56 }
57 runtime->interpreters.next_id = -1;
58
59 return _Py_INIT_OK();
60 }
61
62 _PyInitError
_PyRuntimeState_Init(_PyRuntimeState * runtime)63 _PyRuntimeState_Init(_PyRuntimeState *runtime)
64 {
65 /* Force default allocator, since _PyRuntimeState_Fini() must
66 use the same allocator than this function. */
67 PyMemAllocatorEx old_alloc;
68 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
69
70 _PyInitError err = _PyRuntimeState_Init_impl(runtime);
71
72 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
73 return err;
74 }
75
76 void
_PyRuntimeState_Fini(_PyRuntimeState * runtime)77 _PyRuntimeState_Fini(_PyRuntimeState *runtime)
78 {
79 /* Force the allocator used by _PyRuntimeState_Init(). */
80 PyMemAllocatorEx old_alloc;
81 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
82
83 if (runtime->interpreters.mutex != NULL) {
84 PyThread_free_lock(runtime->interpreters.mutex);
85 runtime->interpreters.mutex = NULL;
86 }
87
88 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
89 }
90
91 #define HEAD_LOCK() PyThread_acquire_lock(_PyRuntime.interpreters.mutex, \
92 WAIT_LOCK)
93 #define HEAD_UNLOCK() PyThread_release_lock(_PyRuntime.interpreters.mutex)
94
95 static void _PyGILState_NoteThreadState(PyThreadState* tstate);
96
97 _PyInitError
_PyInterpreterState_Enable(_PyRuntimeState * runtime)98 _PyInterpreterState_Enable(_PyRuntimeState *runtime)
99 {
100 runtime->interpreters.next_id = 0;
101
102 /* Py_Finalize() calls _PyRuntimeState_Fini() which clears the mutex.
103 Create a new mutex if needed. */
104 if (runtime->interpreters.mutex == NULL) {
105 /* Force default allocator, since _PyRuntimeState_Fini() must
106 use the same allocator than this function. */
107 PyMemAllocatorEx old_alloc;
108 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
109
110 runtime->interpreters.mutex = PyThread_allocate_lock();
111
112 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
113
114 if (runtime->interpreters.mutex == NULL) {
115 return _Py_INIT_ERR("Can't initialize threads for interpreter");
116 }
117 }
118
119 return _Py_INIT_OK();
120 }
121
122 PyInterpreterState *
PyInterpreterState_New(void)123 PyInterpreterState_New(void)
124 {
125 PyInterpreterState *interp = (PyInterpreterState *)
126 PyMem_RawMalloc(sizeof(PyInterpreterState));
127
128 if (interp == NULL) {
129 return NULL;
130 }
131
132 interp->id_refcount = -1;
133 interp->id_mutex = NULL;
134 interp->modules = NULL;
135 interp->modules_by_index = NULL;
136 interp->sysdict = NULL;
137 interp->builtins = NULL;
138 interp->builtins_copy = NULL;
139 interp->tstate_head = NULL;
140 interp->check_interval = 100;
141 interp->num_threads = 0;
142 interp->pythread_stacksize = 0;
143 interp->codec_search_path = NULL;
144 interp->codec_search_cache = NULL;
145 interp->codec_error_registry = NULL;
146 interp->codecs_initialized = 0;
147 interp->fscodec_initialized = 0;
148 interp->core_config = _PyCoreConfig_INIT;
149 interp->config = _PyMainInterpreterConfig_INIT;
150 interp->importlib = NULL;
151 interp->import_func = NULL;
152 interp->eval_frame = _PyEval_EvalFrameDefault;
153 interp->co_extra_user_count = 0;
154 #ifdef HAVE_DLOPEN
155 #if HAVE_DECL_RTLD_NOW
156 interp->dlopenflags = RTLD_NOW;
157 #else
158 interp->dlopenflags = RTLD_LAZY;
159 #endif
160 #endif
161 #ifdef HAVE_FORK
162 interp->before_forkers = NULL;
163 interp->after_forkers_parent = NULL;
164 interp->after_forkers_child = NULL;
165 #endif
166 interp->pyexitfunc = NULL;
167 interp->pyexitmodule = NULL;
168
169 HEAD_LOCK();
170 interp->next = _PyRuntime.interpreters.head;
171 if (_PyRuntime.interpreters.main == NULL) {
172 _PyRuntime.interpreters.main = interp;
173 }
174 _PyRuntime.interpreters.head = interp;
175 if (_PyRuntime.interpreters.next_id < 0) {
176 /* overflow or Py_Initialize() not called! */
177 PyErr_SetString(PyExc_RuntimeError,
178 "failed to get an interpreter ID");
179 /* XXX deallocate! */
180 interp = NULL;
181 } else {
182 interp->id = _PyRuntime.interpreters.next_id;
183 _PyRuntime.interpreters.next_id += 1;
184 }
185 HEAD_UNLOCK();
186
187 interp->tstate_next_unique_id = 0;
188
189 return interp;
190 }
191
192
193 void
PyInterpreterState_Clear(PyInterpreterState * interp)194 PyInterpreterState_Clear(PyInterpreterState *interp)
195 {
196 PyThreadState *p;
197 HEAD_LOCK();
198 for (p = interp->tstate_head; p != NULL; p = p->next)
199 PyThreadState_Clear(p);
200 HEAD_UNLOCK();
201 _PyCoreConfig_Clear(&interp->core_config);
202 _PyMainInterpreterConfig_Clear(&interp->config);
203 Py_CLEAR(interp->codec_search_path);
204 Py_CLEAR(interp->codec_search_cache);
205 Py_CLEAR(interp->codec_error_registry);
206 Py_CLEAR(interp->modules);
207 Py_CLEAR(interp->modules_by_index);
208 Py_CLEAR(interp->sysdict);
209 Py_CLEAR(interp->builtins);
210 Py_CLEAR(interp->builtins_copy);
211 Py_CLEAR(interp->importlib);
212 Py_CLEAR(interp->import_func);
213 #ifdef HAVE_FORK
214 Py_CLEAR(interp->before_forkers);
215 Py_CLEAR(interp->after_forkers_parent);
216 Py_CLEAR(interp->after_forkers_child);
217 #endif
218 }
219
220
221 static void
zapthreads(PyInterpreterState * interp)222 zapthreads(PyInterpreterState *interp)
223 {
224 PyThreadState *p;
225 /* No need to lock the mutex here because this should only happen
226 when the threads are all really dead (XXX famous last words). */
227 while ((p = interp->tstate_head) != NULL) {
228 PyThreadState_Delete(p);
229 }
230 }
231
232
233 void
PyInterpreterState_Delete(PyInterpreterState * interp)234 PyInterpreterState_Delete(PyInterpreterState *interp)
235 {
236 PyInterpreterState **p;
237 zapthreads(interp);
238 HEAD_LOCK();
239 for (p = &_PyRuntime.interpreters.head; ; p = &(*p)->next) {
240 if (*p == NULL)
241 Py_FatalError(
242 "PyInterpreterState_Delete: invalid interp");
243 if (*p == interp)
244 break;
245 }
246 if (interp->tstate_head != NULL)
247 Py_FatalError("PyInterpreterState_Delete: remaining threads");
248 *p = interp->next;
249 if (_PyRuntime.interpreters.main == interp) {
250 _PyRuntime.interpreters.main = NULL;
251 if (_PyRuntime.interpreters.head != NULL)
252 Py_FatalError("PyInterpreterState_Delete: remaining subinterpreters");
253 }
254 HEAD_UNLOCK();
255 if (interp->id_mutex != NULL) {
256 PyThread_free_lock(interp->id_mutex);
257 }
258 PyMem_RawFree(interp);
259 }
260
261
262 int64_t
PyInterpreterState_GetID(PyInterpreterState * interp)263 PyInterpreterState_GetID(PyInterpreterState *interp)
264 {
265 if (interp == NULL) {
266 PyErr_SetString(PyExc_RuntimeError, "no interpreter provided");
267 return -1;
268 }
269 return interp->id;
270 }
271
272
273 PyInterpreterState *
_PyInterpreterState_LookUpID(PY_INT64_T requested_id)274 _PyInterpreterState_LookUpID(PY_INT64_T requested_id)
275 {
276 if (requested_id < 0)
277 goto error;
278
279 PyInterpreterState *interp = PyInterpreterState_Head();
280 while (interp != NULL) {
281 PY_INT64_T id = PyInterpreterState_GetID(interp);
282 if (id < 0)
283 return NULL;
284 if (requested_id == id)
285 return interp;
286 interp = PyInterpreterState_Next(interp);
287 }
288
289 error:
290 PyErr_Format(PyExc_RuntimeError,
291 "unrecognized interpreter ID %lld", requested_id);
292 return NULL;
293 }
294
295
296 int
_PyInterpreterState_IDInitref(PyInterpreterState * interp)297 _PyInterpreterState_IDInitref(PyInterpreterState *interp)
298 {
299 if (interp->id_mutex != NULL) {
300 return 0;
301 }
302 interp->id_mutex = PyThread_allocate_lock();
303 if (interp->id_mutex == NULL) {
304 PyErr_SetString(PyExc_RuntimeError,
305 "failed to create init interpreter ID mutex");
306 return -1;
307 }
308 interp->id_refcount = 0;
309 return 0;
310 }
311
312
313 void
_PyInterpreterState_IDIncref(PyInterpreterState * interp)314 _PyInterpreterState_IDIncref(PyInterpreterState *interp)
315 {
316 if (interp->id_mutex == NULL) {
317 return;
318 }
319 PyThread_acquire_lock(interp->id_mutex, WAIT_LOCK);
320 interp->id_refcount += 1;
321 PyThread_release_lock(interp->id_mutex);
322 }
323
324
325 void
_PyInterpreterState_IDDecref(PyInterpreterState * interp)326 _PyInterpreterState_IDDecref(PyInterpreterState *interp)
327 {
328 if (interp->id_mutex == NULL) {
329 return;
330 }
331 PyThread_acquire_lock(interp->id_mutex, WAIT_LOCK);
332 assert(interp->id_refcount != 0);
333 interp->id_refcount -= 1;
334 int64_t refcount = interp->id_refcount;
335 PyThread_release_lock(interp->id_mutex);
336
337 if (refcount == 0) {
338 // XXX Using the "head" thread isn't strictly correct.
339 PyThreadState *tstate = PyInterpreterState_ThreadHead(interp);
340 // XXX Possible GILState issues?
341 PyThreadState *save_tstate = PyThreadState_Swap(tstate);
342 Py_EndInterpreter(tstate);
343 PyThreadState_Swap(save_tstate);
344 }
345 }
346
347
348 /* Default implementation for _PyThreadState_GetFrame */
349 static struct _frame *
threadstate_getframe(PyThreadState * self)350 threadstate_getframe(PyThreadState *self)
351 {
352 return self->frame;
353 }
354
355 static PyThreadState *
new_threadstate(PyInterpreterState * interp,int init)356 new_threadstate(PyInterpreterState *interp, int init)
357 {
358 PyThreadState *tstate = (PyThreadState *)PyMem_RawMalloc(sizeof(PyThreadState));
359
360 if (_PyThreadState_GetFrame == NULL)
361 _PyThreadState_GetFrame = threadstate_getframe;
362
363 if (tstate != NULL) {
364 tstate->interp = interp;
365
366 tstate->frame = NULL;
367 tstate->recursion_depth = 0;
368 tstate->overflowed = 0;
369 tstate->recursion_critical = 0;
370 tstate->stackcheck_counter = 0;
371 tstate->tracing = 0;
372 tstate->use_tracing = 0;
373 tstate->gilstate_counter = 0;
374 tstate->async_exc = NULL;
375 tstate->thread_id = PyThread_get_thread_ident();
376
377 tstate->dict = NULL;
378
379 tstate->curexc_type = NULL;
380 tstate->curexc_value = NULL;
381 tstate->curexc_traceback = NULL;
382
383 tstate->exc_state.exc_type = NULL;
384 tstate->exc_state.exc_value = NULL;
385 tstate->exc_state.exc_traceback = NULL;
386 tstate->exc_state.previous_item = NULL;
387 tstate->exc_info = &tstate->exc_state;
388
389 tstate->c_profilefunc = NULL;
390 tstate->c_tracefunc = NULL;
391 tstate->c_profileobj = NULL;
392 tstate->c_traceobj = NULL;
393
394 tstate->trash_delete_nesting = 0;
395 tstate->trash_delete_later = NULL;
396 tstate->on_delete = NULL;
397 tstate->on_delete_data = NULL;
398
399 tstate->coroutine_origin_tracking_depth = 0;
400
401 tstate->coroutine_wrapper = NULL;
402 tstate->in_coroutine_wrapper = 0;
403
404 tstate->async_gen_firstiter = NULL;
405 tstate->async_gen_finalizer = NULL;
406
407 tstate->context = NULL;
408 tstate->context_ver = 1;
409
410 tstate->id = ++interp->tstate_next_unique_id;
411
412 if (init)
413 _PyThreadState_Init(tstate);
414
415 HEAD_LOCK();
416 tstate->prev = NULL;
417 tstate->next = interp->tstate_head;
418 if (tstate->next)
419 tstate->next->prev = tstate;
420 interp->tstate_head = tstate;
421 HEAD_UNLOCK();
422 }
423
424 return tstate;
425 }
426
427 PyThreadState *
PyThreadState_New(PyInterpreterState * interp)428 PyThreadState_New(PyInterpreterState *interp)
429 {
430 return new_threadstate(interp, 1);
431 }
432
433 PyThreadState *
_PyThreadState_Prealloc(PyInterpreterState * interp)434 _PyThreadState_Prealloc(PyInterpreterState *interp)
435 {
436 return new_threadstate(interp, 0);
437 }
438
439 void
_PyThreadState_Init(PyThreadState * tstate)440 _PyThreadState_Init(PyThreadState *tstate)
441 {
442 _PyGILState_NoteThreadState(tstate);
443 }
444
445 PyObject*
PyState_FindModule(struct PyModuleDef * module)446 PyState_FindModule(struct PyModuleDef* module)
447 {
448 Py_ssize_t index = module->m_base.m_index;
449 PyInterpreterState *state = GET_INTERP_STATE();
450 PyObject *res;
451 if (module->m_slots) {
452 return NULL;
453 }
454 if (index == 0)
455 return NULL;
456 if (state->modules_by_index == NULL)
457 return NULL;
458 if (index >= PyList_GET_SIZE(state->modules_by_index))
459 return NULL;
460 res = PyList_GET_ITEM(state->modules_by_index, index);
461 return res==Py_None ? NULL : res;
462 }
463
464 int
_PyState_AddModule(PyObject * module,struct PyModuleDef * def)465 _PyState_AddModule(PyObject* module, struct PyModuleDef* def)
466 {
467 PyInterpreterState *state;
468 if (!def) {
469 assert(PyErr_Occurred());
470 return -1;
471 }
472 if (def->m_slots) {
473 PyErr_SetString(PyExc_SystemError,
474 "PyState_AddModule called on module with slots");
475 return -1;
476 }
477 state = GET_INTERP_STATE();
478 if (!state->modules_by_index) {
479 state->modules_by_index = PyList_New(0);
480 if (!state->modules_by_index)
481 return -1;
482 }
483 while(PyList_GET_SIZE(state->modules_by_index) <= def->m_base.m_index)
484 if (PyList_Append(state->modules_by_index, Py_None) < 0)
485 return -1;
486 Py_INCREF(module);
487 return PyList_SetItem(state->modules_by_index,
488 def->m_base.m_index, module);
489 }
490
491 int
PyState_AddModule(PyObject * module,struct PyModuleDef * def)492 PyState_AddModule(PyObject* module, struct PyModuleDef* def)
493 {
494 Py_ssize_t index;
495 PyInterpreterState *state = GET_INTERP_STATE();
496 if (!def) {
497 Py_FatalError("PyState_AddModule: Module Definition is NULL");
498 return -1;
499 }
500 index = def->m_base.m_index;
501 if (state->modules_by_index) {
502 if(PyList_GET_SIZE(state->modules_by_index) >= index) {
503 if(module == PyList_GET_ITEM(state->modules_by_index, index)) {
504 Py_FatalError("PyState_AddModule: Module already added!");
505 return -1;
506 }
507 }
508 }
509 return _PyState_AddModule(module, def);
510 }
511
512 int
PyState_RemoveModule(struct PyModuleDef * def)513 PyState_RemoveModule(struct PyModuleDef* def)
514 {
515 PyInterpreterState *state;
516 Py_ssize_t index = def->m_base.m_index;
517 if (def->m_slots) {
518 PyErr_SetString(PyExc_SystemError,
519 "PyState_RemoveModule called on module with slots");
520 return -1;
521 }
522 state = GET_INTERP_STATE();
523 if (index == 0) {
524 Py_FatalError("PyState_RemoveModule: Module index invalid.");
525 return -1;
526 }
527 if (state->modules_by_index == NULL) {
528 Py_FatalError("PyState_RemoveModule: Interpreters module-list not acessible.");
529 return -1;
530 }
531 if (index > PyList_GET_SIZE(state->modules_by_index)) {
532 Py_FatalError("PyState_RemoveModule: Module index out of bounds.");
533 return -1;
534 }
535 Py_INCREF(Py_None);
536 return PyList_SetItem(state->modules_by_index, index, Py_None);
537 }
538
539 /* used by import.c:PyImport_Cleanup */
540 void
_PyState_ClearModules(void)541 _PyState_ClearModules(void)
542 {
543 PyInterpreterState *state = GET_INTERP_STATE();
544 if (state->modules_by_index) {
545 Py_ssize_t i;
546 for (i = 0; i < PyList_GET_SIZE(state->modules_by_index); i++) {
547 PyObject *m = PyList_GET_ITEM(state->modules_by_index, i);
548 if (PyModule_Check(m)) {
549 /* cleanup the saved copy of module dicts */
550 PyModuleDef *md = PyModule_GetDef(m);
551 if (md)
552 Py_CLEAR(md->m_base.m_copy);
553 }
554 }
555 /* Setting modules_by_index to NULL could be dangerous, so we
556 clear the list instead. */
557 if (PyList_SetSlice(state->modules_by_index,
558 0, PyList_GET_SIZE(state->modules_by_index),
559 NULL))
560 PyErr_WriteUnraisable(state->modules_by_index);
561 }
562 }
563
564 void
PyThreadState_Clear(PyThreadState * tstate)565 PyThreadState_Clear(PyThreadState *tstate)
566 {
567 if (Py_VerboseFlag && tstate->frame != NULL)
568 fprintf(stderr,
569 "PyThreadState_Clear: warning: thread still has a frame\n");
570
571 Py_CLEAR(tstate->frame);
572
573 Py_CLEAR(tstate->dict);
574 Py_CLEAR(tstate->async_exc);
575
576 Py_CLEAR(tstate->curexc_type);
577 Py_CLEAR(tstate->curexc_value);
578 Py_CLEAR(tstate->curexc_traceback);
579
580 Py_CLEAR(tstate->exc_state.exc_type);
581 Py_CLEAR(tstate->exc_state.exc_value);
582 Py_CLEAR(tstate->exc_state.exc_traceback);
583
584 /* The stack of exception states should contain just this thread. */
585 if (Py_VerboseFlag && tstate->exc_info != &tstate->exc_state) {
586 fprintf(stderr,
587 "PyThreadState_Clear: warning: thread still has a generator\n");
588 }
589
590 tstate->c_profilefunc = NULL;
591 tstate->c_tracefunc = NULL;
592 Py_CLEAR(tstate->c_profileobj);
593 Py_CLEAR(tstate->c_traceobj);
594
595 Py_CLEAR(tstate->coroutine_wrapper);
596 Py_CLEAR(tstate->async_gen_firstiter);
597 Py_CLEAR(tstate->async_gen_finalizer);
598
599 Py_CLEAR(tstate->context);
600 }
601
602
603 /* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
604 static void
tstate_delete_common(PyThreadState * tstate)605 tstate_delete_common(PyThreadState *tstate)
606 {
607 PyInterpreterState *interp;
608 if (tstate == NULL)
609 Py_FatalError("PyThreadState_Delete: NULL tstate");
610 interp = tstate->interp;
611 if (interp == NULL)
612 Py_FatalError("PyThreadState_Delete: NULL interp");
613 HEAD_LOCK();
614 if (tstate->prev)
615 tstate->prev->next = tstate->next;
616 else
617 interp->tstate_head = tstate->next;
618 if (tstate->next)
619 tstate->next->prev = tstate->prev;
620 HEAD_UNLOCK();
621 if (tstate->on_delete != NULL) {
622 tstate->on_delete(tstate->on_delete_data);
623 }
624 PyMem_RawFree(tstate);
625 }
626
627
628 void
PyThreadState_Delete(PyThreadState * tstate)629 PyThreadState_Delete(PyThreadState *tstate)
630 {
631 if (tstate == GET_TSTATE())
632 Py_FatalError("PyThreadState_Delete: tstate is still current");
633 if (_PyRuntime.gilstate.autoInterpreterState &&
634 PyThread_tss_get(&_PyRuntime.gilstate.autoTSSkey) == tstate)
635 {
636 PyThread_tss_set(&_PyRuntime.gilstate.autoTSSkey, NULL);
637 }
638 tstate_delete_common(tstate);
639 }
640
641
642 void
PyThreadState_DeleteCurrent()643 PyThreadState_DeleteCurrent()
644 {
645 PyThreadState *tstate = GET_TSTATE();
646 if (tstate == NULL)
647 Py_FatalError(
648 "PyThreadState_DeleteCurrent: no current tstate");
649 tstate_delete_common(tstate);
650 if (_PyRuntime.gilstate.autoInterpreterState &&
651 PyThread_tss_get(&_PyRuntime.gilstate.autoTSSkey) == tstate)
652 {
653 PyThread_tss_set(&_PyRuntime.gilstate.autoTSSkey, NULL);
654 }
655 SET_TSTATE(NULL);
656 PyEval_ReleaseLock();
657 }
658
659
660 /*
661 * Delete all thread states except the one passed as argument.
662 * Note that, if there is a current thread state, it *must* be the one
663 * passed as argument. Also, this won't touch any other interpreters
664 * than the current one, since we don't know which thread state should
665 * be kept in those other interpreteres.
666 */
667 void
_PyThreadState_DeleteExcept(PyThreadState * tstate)668 _PyThreadState_DeleteExcept(PyThreadState *tstate)
669 {
670 PyInterpreterState *interp = tstate->interp;
671 PyThreadState *p, *next, *garbage;
672 HEAD_LOCK();
673 /* Remove all thread states, except tstate, from the linked list of
674 thread states. This will allow calling PyThreadState_Clear()
675 without holding the lock. */
676 garbage = interp->tstate_head;
677 if (garbage == tstate)
678 garbage = tstate->next;
679 if (tstate->prev)
680 tstate->prev->next = tstate->next;
681 if (tstate->next)
682 tstate->next->prev = tstate->prev;
683 tstate->prev = tstate->next = NULL;
684 interp->tstate_head = tstate;
685 HEAD_UNLOCK();
686 /* Clear and deallocate all stale thread states. Even if this
687 executes Python code, we should be safe since it executes
688 in the current thread, not one of the stale threads. */
689 for (p = garbage; p; p = next) {
690 next = p->next;
691 PyThreadState_Clear(p);
692 PyMem_RawFree(p);
693 }
694 }
695
696
697 PyThreadState *
_PyThreadState_UncheckedGet(void)698 _PyThreadState_UncheckedGet(void)
699 {
700 return GET_TSTATE();
701 }
702
703
704 PyThreadState *
PyThreadState_Get(void)705 PyThreadState_Get(void)
706 {
707 PyThreadState *tstate = GET_TSTATE();
708 if (tstate == NULL)
709 Py_FatalError("PyThreadState_Get: no current thread");
710
711 return tstate;
712 }
713
714
715 PyThreadState *
PyThreadState_Swap(PyThreadState * newts)716 PyThreadState_Swap(PyThreadState *newts)
717 {
718 PyThreadState *oldts = GET_TSTATE();
719
720 SET_TSTATE(newts);
721 /* It should not be possible for more than one thread state
722 to be used for a thread. Check this the best we can in debug
723 builds.
724 */
725 #if defined(Py_DEBUG)
726 if (newts) {
727 /* This can be called from PyEval_RestoreThread(). Similar
728 to it, we need to ensure errno doesn't change.
729 */
730 int err = errno;
731 PyThreadState *check = PyGILState_GetThisThreadState();
732 if (check && check->interp == newts->interp && check != newts)
733 Py_FatalError("Invalid thread state for this thread");
734 errno = err;
735 }
736 #endif
737 return oldts;
738 }
739
740 /* An extension mechanism to store arbitrary additional per-thread state.
741 PyThreadState_GetDict() returns a dictionary that can be used to hold such
742 state; the caller should pick a unique key and store its state there. If
743 PyThreadState_GetDict() returns NULL, an exception has *not* been raised
744 and the caller should assume no per-thread state is available. */
745
746 PyObject *
PyThreadState_GetDict(void)747 PyThreadState_GetDict(void)
748 {
749 PyThreadState *tstate = GET_TSTATE();
750 if (tstate == NULL)
751 return NULL;
752
753 if (tstate->dict == NULL) {
754 PyObject *d;
755 tstate->dict = d = PyDict_New();
756 if (d == NULL)
757 PyErr_Clear();
758 }
759 return tstate->dict;
760 }
761
762
763 /* Asynchronously raise an exception in a thread.
764 Requested by Just van Rossum and Alex Martelli.
765 To prevent naive misuse, you must write your own extension
766 to call this, or use ctypes. Must be called with the GIL held.
767 Returns the number of tstates modified (normally 1, but 0 if `id` didn't
768 match any known thread id). Can be called with exc=NULL to clear an
769 existing async exception. This raises no exceptions. */
770
771 int
PyThreadState_SetAsyncExc(unsigned long id,PyObject * exc)772 PyThreadState_SetAsyncExc(unsigned long id, PyObject *exc)
773 {
774 PyInterpreterState *interp = GET_INTERP_STATE();
775 PyThreadState *p;
776
777 /* Although the GIL is held, a few C API functions can be called
778 * without the GIL held, and in particular some that create and
779 * destroy thread and interpreter states. Those can mutate the
780 * list of thread states we're traversing, so to prevent that we lock
781 * head_mutex for the duration.
782 */
783 HEAD_LOCK();
784 for (p = interp->tstate_head; p != NULL; p = p->next) {
785 if (p->thread_id == id) {
786 /* Tricky: we need to decref the current value
787 * (if any) in p->async_exc, but that can in turn
788 * allow arbitrary Python code to run, including
789 * perhaps calls to this function. To prevent
790 * deadlock, we need to release head_mutex before
791 * the decref.
792 */
793 PyObject *old_exc = p->async_exc;
794 Py_XINCREF(exc);
795 p->async_exc = exc;
796 HEAD_UNLOCK();
797 Py_XDECREF(old_exc);
798 _PyEval_SignalAsyncExc();
799 return 1;
800 }
801 }
802 HEAD_UNLOCK();
803 return 0;
804 }
805
806
807 /* Routines for advanced debuggers, requested by David Beazley.
808 Don't use unless you know what you are doing! */
809
810 PyInterpreterState *
PyInterpreterState_Head(void)811 PyInterpreterState_Head(void)
812 {
813 return _PyRuntime.interpreters.head;
814 }
815
816 PyInterpreterState *
PyInterpreterState_Main(void)817 PyInterpreterState_Main(void)
818 {
819 return _PyRuntime.interpreters.main;
820 }
821
822 PyInterpreterState *
PyInterpreterState_Next(PyInterpreterState * interp)823 PyInterpreterState_Next(PyInterpreterState *interp) {
824 return interp->next;
825 }
826
827 PyThreadState *
PyInterpreterState_ThreadHead(PyInterpreterState * interp)828 PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
829 return interp->tstate_head;
830 }
831
832 PyThreadState *
PyThreadState_Next(PyThreadState * tstate)833 PyThreadState_Next(PyThreadState *tstate) {
834 return tstate->next;
835 }
836
837 /* The implementation of sys._current_frames(). This is intended to be
838 called with the GIL held, as it will be when called via
839 sys._current_frames(). It's possible it would work fine even without
840 the GIL held, but haven't thought enough about that.
841 */
842 PyObject *
_PyThread_CurrentFrames(void)843 _PyThread_CurrentFrames(void)
844 {
845 PyObject *result;
846 PyInterpreterState *i;
847
848 result = PyDict_New();
849 if (result == NULL)
850 return NULL;
851
852 /* for i in all interpreters:
853 * for t in all of i's thread states:
854 * if t's frame isn't NULL, map t's id to its frame
855 * Because these lists can mutate even when the GIL is held, we
856 * need to grab head_mutex for the duration.
857 */
858 HEAD_LOCK();
859 for (i = _PyRuntime.interpreters.head; i != NULL; i = i->next) {
860 PyThreadState *t;
861 for (t = i->tstate_head; t != NULL; t = t->next) {
862 PyObject *id;
863 int stat;
864 struct _frame *frame = t->frame;
865 if (frame == NULL)
866 continue;
867 id = PyLong_FromUnsignedLong(t->thread_id);
868 if (id == NULL)
869 goto Fail;
870 stat = PyDict_SetItem(result, id, (PyObject *)frame);
871 Py_DECREF(id);
872 if (stat < 0)
873 goto Fail;
874 }
875 }
876 HEAD_UNLOCK();
877 return result;
878
879 Fail:
880 HEAD_UNLOCK();
881 Py_DECREF(result);
882 return NULL;
883 }
884
885 /* Python "auto thread state" API. */
886
887 /* Keep this as a static, as it is not reliable! It can only
888 ever be compared to the state for the *current* thread.
889 * If not equal, then it doesn't matter that the actual
890 value may change immediately after comparison, as it can't
891 possibly change to the current thread's state.
892 * If equal, then the current thread holds the lock, so the value can't
893 change until we yield the lock.
894 */
895 static int
PyThreadState_IsCurrent(PyThreadState * tstate)896 PyThreadState_IsCurrent(PyThreadState *tstate)
897 {
898 /* Must be the tstate for this thread */
899 assert(PyGILState_GetThisThreadState()==tstate);
900 return tstate == GET_TSTATE();
901 }
902
903 /* Internal initialization/finalization functions called by
904 Py_Initialize/Py_FinalizeEx
905 */
906 void
_PyGILState_Init(PyInterpreterState * i,PyThreadState * t)907 _PyGILState_Init(PyInterpreterState *i, PyThreadState *t)
908 {
909 assert(i && t); /* must init with valid states */
910 if (PyThread_tss_create(&_PyRuntime.gilstate.autoTSSkey) != 0) {
911 Py_FatalError("Could not allocate TSS entry");
912 }
913 _PyRuntime.gilstate.autoInterpreterState = i;
914 assert(PyThread_tss_get(&_PyRuntime.gilstate.autoTSSkey) == NULL);
915 assert(t->gilstate_counter == 0);
916
917 _PyGILState_NoteThreadState(t);
918 }
919
920 PyInterpreterState *
_PyGILState_GetInterpreterStateUnsafe(void)921 _PyGILState_GetInterpreterStateUnsafe(void)
922 {
923 return _PyRuntime.gilstate.autoInterpreterState;
924 }
925
926 void
_PyGILState_Fini(void)927 _PyGILState_Fini(void)
928 {
929 PyThread_tss_delete(&_PyRuntime.gilstate.autoTSSkey);
930 _PyRuntime.gilstate.autoInterpreterState = NULL;
931 }
932
933 /* Reset the TSS key - called by PyOS_AfterFork_Child().
934 * This should not be necessary, but some - buggy - pthread implementations
935 * don't reset TSS upon fork(), see issue #10517.
936 */
937 void
_PyGILState_Reinit(void)938 _PyGILState_Reinit(void)
939 {
940 /* Force default allocator, since _PyRuntimeState_Fini() must
941 use the same allocator than this function. */
942 PyMemAllocatorEx old_alloc;
943 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
944
945 _PyRuntime.interpreters.mutex = PyThread_allocate_lock();
946
947 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
948
949 if (_PyRuntime.interpreters.mutex == NULL) {
950 Py_FatalError("Can't initialize threads for interpreter");
951 }
952
953 PyThreadState *tstate = PyGILState_GetThisThreadState();
954 PyThread_tss_delete(&_PyRuntime.gilstate.autoTSSkey);
955 if (PyThread_tss_create(&_PyRuntime.gilstate.autoTSSkey) != 0) {
956 Py_FatalError("Could not allocate TSS entry");
957 }
958
959 /* If the thread had an associated auto thread state, reassociate it with
960 * the new key. */
961 if (tstate &&
962 PyThread_tss_set(&_PyRuntime.gilstate.autoTSSkey, (void *)tstate) != 0)
963 {
964 Py_FatalError("Couldn't create autoTSSkey mapping");
965 }
966 }
967
968 /* When a thread state is created for a thread by some mechanism other than
969 PyGILState_Ensure, it's important that the GILState machinery knows about
970 it so it doesn't try to create another thread state for the thread (this is
971 a better fix for SF bug #1010677 than the first one attempted).
972 */
973 static void
_PyGILState_NoteThreadState(PyThreadState * tstate)974 _PyGILState_NoteThreadState(PyThreadState* tstate)
975 {
976 /* If autoTSSkey isn't initialized, this must be the very first
977 threadstate created in Py_Initialize(). Don't do anything for now
978 (we'll be back here when _PyGILState_Init is called). */
979 if (!_PyRuntime.gilstate.autoInterpreterState)
980 return;
981
982 /* Stick the thread state for this thread in thread specific storage.
983
984 The only situation where you can legitimately have more than one
985 thread state for an OS level thread is when there are multiple
986 interpreters.
987
988 You shouldn't really be using the PyGILState_ APIs anyway (see issues
989 #10915 and #15751).
990
991 The first thread state created for that given OS level thread will
992 "win", which seems reasonable behaviour.
993 */
994 if (PyThread_tss_get(&_PyRuntime.gilstate.autoTSSkey) == NULL) {
995 if ((PyThread_tss_set(&_PyRuntime.gilstate.autoTSSkey, (void *)tstate)
996 ) != 0)
997 {
998 Py_FatalError("Couldn't create autoTSSkey mapping");
999 }
1000 }
1001
1002 /* PyGILState_Release must not try to delete this thread state. */
1003 tstate->gilstate_counter = 1;
1004 }
1005
1006 /* The public functions */
1007 PyThreadState *
PyGILState_GetThisThreadState(void)1008 PyGILState_GetThisThreadState(void)
1009 {
1010 if (_PyRuntime.gilstate.autoInterpreterState == NULL)
1011 return NULL;
1012 return (PyThreadState *)PyThread_tss_get(&_PyRuntime.gilstate.autoTSSkey);
1013 }
1014
1015 int
PyGILState_Check(void)1016 PyGILState_Check(void)
1017 {
1018 PyThreadState *tstate;
1019
1020 if (!_PyGILState_check_enabled)
1021 return 1;
1022
1023 if (!PyThread_tss_is_created(&_PyRuntime.gilstate.autoTSSkey)) {
1024 return 1;
1025 }
1026
1027 tstate = GET_TSTATE();
1028 if (tstate == NULL)
1029 return 0;
1030
1031 return (tstate == PyGILState_GetThisThreadState());
1032 }
1033
1034 PyGILState_STATE
PyGILState_Ensure(void)1035 PyGILState_Ensure(void)
1036 {
1037 int current;
1038 PyThreadState *tcur;
1039 int need_init_threads = 0;
1040
1041 /* Note that we do not auto-init Python here - apart from
1042 potential races with 2 threads auto-initializing, pep-311
1043 spells out other issues. Embedders are expected to have
1044 called Py_Initialize() and usually PyEval_InitThreads().
1045 */
1046 /* Py_Initialize() hasn't been called! */
1047 assert(_PyRuntime.gilstate.autoInterpreterState);
1048
1049 tcur = (PyThreadState *)PyThread_tss_get(&_PyRuntime.gilstate.autoTSSkey);
1050 if (tcur == NULL) {
1051 need_init_threads = 1;
1052
1053 /* Create a new thread state for this thread */
1054 tcur = PyThreadState_New(_PyRuntime.gilstate.autoInterpreterState);
1055 if (tcur == NULL)
1056 Py_FatalError("Couldn't create thread-state for new thread");
1057 /* This is our thread state! We'll need to delete it in the
1058 matching call to PyGILState_Release(). */
1059 tcur->gilstate_counter = 0;
1060 current = 0; /* new thread state is never current */
1061 }
1062 else {
1063 current = PyThreadState_IsCurrent(tcur);
1064 }
1065
1066 if (current == 0) {
1067 PyEval_RestoreThread(tcur);
1068 }
1069
1070 /* Update our counter in the thread-state - no need for locks:
1071 - tcur will remain valid as we hold the GIL.
1072 - the counter is safe as we are the only thread "allowed"
1073 to modify this value
1074 */
1075 ++tcur->gilstate_counter;
1076
1077 if (need_init_threads) {
1078 /* At startup, Python has no concrete GIL. If PyGILState_Ensure() is
1079 called from a new thread for the first time, we need the create the
1080 GIL. */
1081 PyEval_InitThreads();
1082 }
1083
1084 return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
1085 }
1086
1087 void
PyGILState_Release(PyGILState_STATE oldstate)1088 PyGILState_Release(PyGILState_STATE oldstate)
1089 {
1090 PyThreadState *tcur = (PyThreadState *)PyThread_tss_get(
1091 &_PyRuntime.gilstate.autoTSSkey);
1092 if (tcur == NULL)
1093 Py_FatalError("auto-releasing thread-state, "
1094 "but no thread-state for this thread");
1095 /* We must hold the GIL and have our thread state current */
1096 /* XXX - remove the check - the assert should be fine,
1097 but while this is very new (April 2003), the extra check
1098 by release-only users can't hurt.
1099 */
1100 if (! PyThreadState_IsCurrent(tcur))
1101 Py_FatalError("This thread state must be current when releasing");
1102 assert(PyThreadState_IsCurrent(tcur));
1103 --tcur->gilstate_counter;
1104 assert(tcur->gilstate_counter >= 0); /* illegal counter value */
1105
1106 /* If we're going to destroy this thread-state, we must
1107 * clear it while the GIL is held, as destructors may run.
1108 */
1109 if (tcur->gilstate_counter == 0) {
1110 /* can't have been locked when we created it */
1111 assert(oldstate == PyGILState_UNLOCKED);
1112 PyThreadState_Clear(tcur);
1113 /* Delete the thread-state. Note this releases the GIL too!
1114 * It's vital that the GIL be held here, to avoid shutdown
1115 * races; see bugs 225673 and 1061968 (that nasty bug has a
1116 * habit of coming back).
1117 */
1118 PyThreadState_DeleteCurrent();
1119 }
1120 /* Release the lock if necessary */
1121 else if (oldstate == PyGILState_UNLOCKED)
1122 PyEval_SaveThread();
1123 }
1124
1125
1126 #ifdef __cplusplus
1127 }
1128 #endif
1129