blob: 67315756ab70a71a59e4ca55a6264c8fd6e3d35a [file] [log] [blame]
Guido van Rossuma027efa1997-05-05 20:56:21 +00001
2/* Thread and interpreter state structures and their interfaces */
3
4#include "Python.h"
Victor Stinner09532fe2019-05-10 23:39:09 +02005#include "pycore_ceval.h"
Victor Stinnerf684d832019-03-01 03:44:13 +01006#include "pycore_coreconfig.h"
Victor Stinner621cebe2018-11-12 16:53:38 +01007#include "pycore_pymem.h"
8#include "pycore_pystate.h"
Eric Snow86ea5812019-05-10 13:29:55 -04009#include "pycore_pylifecycle.h"
Guido van Rossuma027efa1997-05-05 20:56:21 +000010
Tim Peters84705582004-10-10 02:47:33 +000011/* --------------------------------------------------------------------------
12CAUTION
13
Victor Stinner1a7425f2013-07-07 16:25:15 +020014Always use PyMem_RawMalloc() and PyMem_RawFree() directly in this file. A
15number of these functions are advertised as safe to call when the GIL isn't
16held, and in a debug build Python redirects (e.g.) PyMem_NEW (etc) to Python's
17debugging obmalloc functions. Those aren't thread-safe (they rely on the GIL
18to avoid the expense of doing their own locking).
Tim Peters84705582004-10-10 02:47:33 +000019-------------------------------------------------------------------------- */
20
Martin v. Löwisf0473d52001-07-18 16:17:16 +000021#ifdef HAVE_DLOPEN
22#ifdef HAVE_DLFCN_H
23#include <dlfcn.h>
24#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030025#if !HAVE_DECL_RTLD_LAZY
Martin v. Löwisf0473d52001-07-18 16:17:16 +000026#define RTLD_LAZY 1
27#endif
28#endif
29
Benjamin Peterson43162b82012-04-13 11:58:27 -040030#ifdef __cplusplus
31extern "C" {
32#endif
Martin v. Löwisf0473d52001-07-18 16:17:16 +000033
Victor Stinner10c8e6a2019-04-26 01:53:18 +020034#define _PyRuntimeGILState_GetThreadState(gilstate) \
35 ((PyThreadState*)_Py_atomic_load_relaxed(&(gilstate)->tstate_current))
36#define _PyRuntimeGILState_SetThreadState(gilstate, value) \
37 _Py_atomic_store_relaxed(&(gilstate)->tstate_current, \
38 (uintptr_t)(value))
39
40/* Forward declarations */
41static PyThreadState *_PyGILState_GetThisThreadState(struct _gilstate_runtime_state *gilstate);
42static void _PyThreadState_Delete(_PyRuntimeState *runtime, PyThreadState *tstate);
Victor Stinner10c8e6a2019-04-26 01:53:18 +020043
44
Victor Stinner5d39e042017-11-29 17:20:38 +010045static _PyInitError
46_PyRuntimeState_Init_impl(_PyRuntimeState *runtime)
Eric Snow2ebc5ce2017-09-07 23:51:28 -060047{
48 memset(runtime, 0, sizeof(*runtime));
Victor Stinner8a1be612016-03-14 22:07:55 +010049
Eric Snow2ebc5ce2017-09-07 23:51:28 -060050 _PyGC_Initialize(&runtime->gc);
51 _PyEval_Initialize(&runtime->ceval);
Victor Stinner6d5ee972019-03-23 12:05:43 +010052 runtime->preconfig = _PyPreConfig_INIT;
Michael W. Hudson188d4362005-06-20 16:52:57 +000053
Eric Snow2ebc5ce2017-09-07 23:51:28 -060054 runtime->gilstate.check_enabled = 1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -080055
Masayuki Yamamoto731e1892017-10-06 19:41:34 +090056 /* A TSS key must be initialized with Py_tss_NEEDS_INIT
57 in accordance with the specification. */
Victor Stinnerf7e5b562017-11-15 15:48:08 -080058 Py_tss_t initial = Py_tss_NEEDS_INIT;
59 runtime->gilstate.autoTSSkey = initial;
Guido van Rossum1d5ad901999-06-18 14:22:24 +000060
Eric Snow2ebc5ce2017-09-07 23:51:28 -060061 runtime->interpreters.mutex = PyThread_allocate_lock();
Victor Stinnerf7e5b562017-11-15 15:48:08 -080062 if (runtime->interpreters.mutex == NULL) {
63 return _Py_INIT_ERR("Can't initialize threads for interpreter");
64 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -060065 runtime->interpreters.next_id = -1;
Eric Snow7f8bfc92018-01-29 18:23:44 -070066
67 runtime->xidregistry.mutex = PyThread_allocate_lock();
68 if (runtime->xidregistry.mutex == NULL) {
69 return _Py_INIT_ERR("Can't initialize threads for cross-interpreter data registry");
70 }
71
Eric Snow8479a342019-03-08 23:44:33 -070072 // Set it to the ID of the main thread of the main interpreter.
73 runtime->main_thread = PyThread_get_thread_ident();
Eric Snow5be45a62019-03-08 22:47:07 -070074
Victor Stinnerf7e5b562017-11-15 15:48:08 -080075 return _Py_INIT_OK();
Eric Snow2ebc5ce2017-09-07 23:51:28 -060076}
Eric Snow05351c12017-09-05 21:43:08 -070077
Victor Stinner5d39e042017-11-29 17:20:38 +010078_PyInitError
79_PyRuntimeState_Init(_PyRuntimeState *runtime)
80{
81 /* Force default allocator, since _PyRuntimeState_Fini() must
82 use the same allocator than this function. */
83 PyMemAllocatorEx old_alloc;
84 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
85
86 _PyInitError err = _PyRuntimeState_Init_impl(runtime);
87
88 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
89 return err;
90}
91
Eric Snow2ebc5ce2017-09-07 23:51:28 -060092void
93_PyRuntimeState_Fini(_PyRuntimeState *runtime)
94{
Victor Stinner5d39e042017-11-29 17:20:38 +010095 /* Force the allocator used by _PyRuntimeState_Init(). */
96 PyMemAllocatorEx old_alloc;
97 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinnerccb04422017-11-16 03:20:31 -080098
Eric Snow2ebc5ce2017-09-07 23:51:28 -060099 if (runtime->interpreters.mutex != NULL) {
100 PyThread_free_lock(runtime->interpreters.mutex);
101 runtime->interpreters.mutex = NULL;
102 }
Victor Stinnerccb04422017-11-16 03:20:31 -0800103
Stéphane Wirtel943395f2019-03-19 11:51:32 +0100104 if (runtime->xidregistry.mutex != NULL) {
105 PyThread_free_lock(runtime->xidregistry.mutex);
106 runtime->xidregistry.mutex = NULL;
107 }
108
Victor Stinner6d5ee972019-03-23 12:05:43 +0100109 _PyPreConfig_Clear(&runtime->preconfig);
110
Victor Stinnerccb04422017-11-16 03:20:31 -0800111 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600112}
113
Eric Snow8479a342019-03-08 23:44:33 -0700114/* This function is called from PyOS_AfterFork_Child to ensure that
115 * newly created child processes do not share locks with the parent.
116 */
117
118void
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200119_PyRuntimeState_ReInitThreads(_PyRuntimeState *runtime)
Eric Snow8479a342019-03-08 23:44:33 -0700120{
121 // This was initially set in _PyRuntimeState_Init().
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200122 runtime->main_thread = PyThread_get_thread_ident();
Eric Snow8479a342019-03-08 23:44:33 -0700123
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200124 /* Force default allocator, since _PyRuntimeState_Fini() must
125 use the same allocator than this function. */
126 PyMemAllocatorEx old_alloc;
127 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
128
129 runtime->interpreters.mutex = PyThread_allocate_lock();
130 runtime->interpreters.main->id_mutex = PyThread_allocate_lock();
131 runtime->xidregistry.mutex = PyThread_allocate_lock();
132
133 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
134
135 if (runtime->interpreters.mutex == NULL) {
Eric Snow8479a342019-03-08 23:44:33 -0700136 Py_FatalError("Can't initialize lock for runtime interpreters");
137 }
138
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200139 if (runtime->interpreters.main->id_mutex == NULL) {
Eric Snow8479a342019-03-08 23:44:33 -0700140 Py_FatalError("Can't initialize ID lock for main interpreter");
141 }
142
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200143 if (runtime->xidregistry.mutex == NULL) {
Eric Snow8479a342019-03-08 23:44:33 -0700144 Py_FatalError("Can't initialize lock for cross-interpreter data registry");
145 }
146}
147
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200148#define HEAD_LOCK(runtime) \
149 PyThread_acquire_lock((runtime)->interpreters.mutex, WAIT_LOCK)
150#define HEAD_UNLOCK(runtime) \
151 PyThread_release_lock((runtime)->interpreters.mutex)
Eric Snow05351c12017-09-05 21:43:08 -0700152
Victor Stinner8bb32302019-04-24 16:47:40 +0200153/* Forward declaration */
154static void _PyGILState_NoteThreadState(
155 struct _gilstate_runtime_state *gilstate, PyThreadState* tstate);
Michael W. Hudson188d4362005-06-20 16:52:57 +0000156
Victor Stinnera7368ac2017-11-15 18:11:45 -0800157_PyInitError
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600158_PyInterpreterState_Enable(_PyRuntimeState *runtime)
Eric Snowe3774162017-05-22 19:46:40 -0700159{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200160 struct pyinterpreters *interpreters = &runtime->interpreters;
161 interpreters->next_id = 0;
Victor Stinner5d926472018-03-06 14:31:37 +0100162
163 /* Py_Finalize() calls _PyRuntimeState_Fini() which clears the mutex.
164 Create a new mutex if needed. */
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200165 if (interpreters->mutex == NULL) {
Victor Stinner5d926472018-03-06 14:31:37 +0100166 /* Force default allocator, since _PyRuntimeState_Fini() must
167 use the same allocator than this function. */
168 PyMemAllocatorEx old_alloc;
169 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
170
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200171 interpreters->mutex = PyThread_allocate_lock();
Victor Stinner5d926472018-03-06 14:31:37 +0100172
173 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
174
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200175 if (interpreters->mutex == NULL) {
Victor Stinnera7368ac2017-11-15 18:11:45 -0800176 return _Py_INIT_ERR("Can't initialize threads for interpreter");
177 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600178 }
Victor Stinner5d926472018-03-06 14:31:37 +0100179
Victor Stinnera7368ac2017-11-15 18:11:45 -0800180 return _Py_INIT_OK();
Eric Snowe3774162017-05-22 19:46:40 -0700181}
Guido van Rossuma027efa1997-05-05 20:56:21 +0000182
183PyInterpreterState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000184PyInterpreterState_New(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000185{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200186 PyInterpreterState *interp = PyMem_RawMalloc(sizeof(PyInterpreterState));
Victor Stinnerd4341102017-11-23 00:12:09 +0100187 if (interp == NULL) {
188 return NULL;
189 }
190
Eric Snow5be45a62019-03-08 22:47:07 -0700191 memset(interp, 0, sizeof(*interp));
Eric Snow4c6955e2018-02-16 18:53:40 -0700192 interp->id_refcount = -1;
Victor Stinnerd4341102017-11-23 00:12:09 +0100193 interp->check_interval = 100;
Victor Stinnerd4341102017-11-23 00:12:09 +0100194 interp->core_config = _PyCoreConfig_INIT;
Victor Stinnerd4341102017-11-23 00:12:09 +0100195 interp->eval_frame = _PyEval_EvalFrameDefault;
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000196#ifdef HAVE_DLOPEN
Serhiy Storchakac2f7d872016-05-04 09:44:44 +0300197#if HAVE_DECL_RTLD_NOW
Victor Stinnerd4341102017-11-23 00:12:09 +0100198 interp->dlopenflags = RTLD_NOW;
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000199#else
Victor Stinnerd4341102017-11-23 00:12:09 +0100200 interp->dlopenflags = RTLD_LAZY;
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000201#endif
202#endif
Guido van Rossum25ce5661997-08-02 03:10:38 +0000203
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200204 _PyRuntimeState *runtime = &_PyRuntime;
205 struct pyinterpreters *interpreters = &runtime->interpreters;
206
207 HEAD_LOCK(runtime);
208 if (interpreters->next_id < 0) {
Victor Stinnerd4341102017-11-23 00:12:09 +0100209 /* overflow or Py_Initialize() not called! */
210 PyErr_SetString(PyExc_RuntimeError,
211 "failed to get an interpreter ID");
Pablo Galindo95d630e2018-08-31 22:49:29 +0100212 PyMem_RawFree(interp);
Victor Stinnerd4341102017-11-23 00:12:09 +0100213 interp = NULL;
Victor Stinnerd4341102017-11-23 00:12:09 +0100214 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200215 else {
216 interp->id = interpreters->next_id;
217 interpreters->next_id += 1;
218 interp->next = interpreters->head;
219 if (interpreters->main == NULL) {
220 interpreters->main = interp;
221 }
222 interpreters->head = interp;
223 }
224 HEAD_UNLOCK(runtime);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000225
Pablo Galindo95d630e2018-08-31 22:49:29 +0100226 if (interp == NULL) {
227 return NULL;
228 }
229
Yury Selivanovf23746a2018-01-22 19:11:18 -0500230 interp->tstate_next_unique_id = 0;
231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 return interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000233}
234
235
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200236static void
237_PyInterpreterState_Clear(_PyRuntimeState *runtime, PyInterpreterState *interp)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000238{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200239 HEAD_LOCK(runtime);
240 for (PyThreadState *p = interp->tstate_head; p != NULL; p = p->next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 PyThreadState_Clear(p);
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200242 }
243 HEAD_UNLOCK(runtime);
Victor Stinnerda273412017-12-15 01:46:02 +0100244 _PyCoreConfig_Clear(&interp->core_config);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 Py_CLEAR(interp->codec_search_path);
246 Py_CLEAR(interp->codec_search_cache);
247 Py_CLEAR(interp->codec_error_registry);
Eric Snow93c92f72017-09-13 23:46:04 -0700248 Py_CLEAR(interp->modules);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 Py_CLEAR(interp->modules_by_index);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 Py_CLEAR(interp->sysdict);
251 Py_CLEAR(interp->builtins);
Serhiy Storchaka87a5c512014-02-10 18:21:34 +0200252 Py_CLEAR(interp->builtins_copy);
Brett Cannonfd074152012-04-14 14:10:13 -0400253 Py_CLEAR(interp->importlib);
Serhiy Storchaka133138a2016-08-02 22:51:21 +0300254 Py_CLEAR(interp->import_func);
Eric Snowd2fdd1f2019-03-15 17:47:43 -0600255 Py_CLEAR(interp->dict);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200256#ifdef HAVE_FORK
257 Py_CLEAR(interp->before_forkers);
258 Py_CLEAR(interp->after_forkers_parent);
259 Py_CLEAR(interp->after_forkers_child);
260#endif
Eric Snow86ea5812019-05-10 13:29:55 -0400261 if (runtime->finalizing == NULL) {
262 _PyWarnings_Fini(interp);
263 }
Eric Snow5be45a62019-03-08 22:47:07 -0700264 // XXX Once we have one allocator per interpreter (i.e.
265 // per-interpreter GC) we must ensure that all of the interpreter's
266 // objects have been cleaned up at the point.
Guido van Rossum25ce5661997-08-02 03:10:38 +0000267}
268
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200269void
270PyInterpreterState_Clear(PyInterpreterState *interp)
271{
272 _PyInterpreterState_Clear(&_PyRuntime, interp);
273}
274
Guido van Rossum25ce5661997-08-02 03:10:38 +0000275
276static void
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200277zapthreads(_PyRuntimeState *runtime, PyInterpreterState *interp)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000278{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 PyThreadState *p;
280 /* No need to lock the mutex here because this should only happen
281 when the threads are all really dead (XXX famous last words). */
282 while ((p = interp->tstate_head) != NULL) {
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200283 _PyThreadState_Delete(runtime, p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000285}
286
287
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200288static void
289_PyInterpreterState_Delete(_PyRuntimeState *runtime,
290 PyInterpreterState *interp)
291{
292 struct pyinterpreters *interpreters = &runtime->interpreters;
293 zapthreads(runtime, interp);
294 HEAD_LOCK(runtime);
295 PyInterpreterState **p;
296 for (p = &interpreters->head; ; p = &(*p)->next) {
297 if (*p == NULL) {
298 Py_FatalError("PyInterpreterState_Delete: invalid interp");
299 }
300 if (*p == interp) {
301 break;
302 }
303 }
304 if (interp->tstate_head != NULL) {
305 Py_FatalError("PyInterpreterState_Delete: remaining threads");
306 }
307 *p = interp->next;
308 if (interpreters->main == interp) {
309 interpreters->main = NULL;
310 if (interpreters->head != NULL) {
311 Py_FatalError("PyInterpreterState_Delete: remaining subinterpreters");
312 }
313 }
314 HEAD_UNLOCK(runtime);
315 if (interp->id_mutex != NULL) {
316 PyThread_free_lock(interp->id_mutex);
317 }
318 PyMem_RawFree(interp);
319}
320
321
Guido van Rossum25ce5661997-08-02 03:10:38 +0000322void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000323PyInterpreterState_Delete(PyInterpreterState *interp)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000324{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200325 _PyInterpreterState_Delete(&_PyRuntime, interp);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000326}
327
328
Eric Snow59032962018-09-14 14:17:20 -0700329/*
330 * Delete all interpreter states except the main interpreter. If there
331 * is a current interpreter state, it *must* be the main interpreter.
332 */
333void
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200334_PyInterpreterState_DeleteExceptMain(_PyRuntimeState *runtime)
Eric Snow59032962018-09-14 14:17:20 -0700335{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200336 struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200337 struct pyinterpreters *interpreters = &runtime->interpreters;
338
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200339 PyThreadState *tstate = _PyThreadState_Swap(gilstate, NULL);
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200340 if (tstate != NULL && tstate->interp != interpreters->main) {
Eric Snow59032962018-09-14 14:17:20 -0700341 Py_FatalError("PyInterpreterState_DeleteExceptMain: not main interpreter");
342 }
343
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200344 HEAD_LOCK(runtime);
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200345 PyInterpreterState *interp = interpreters->head;
346 interpreters->head = NULL;
Stéphane Wirtelb5409da2019-02-20 15:27:22 +0100347 while (interp != NULL) {
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200348 if (interp == interpreters->main) {
349 interpreters->main->next = NULL;
350 interpreters->head = interp;
Stéphane Wirtelb5409da2019-02-20 15:27:22 +0100351 interp = interp->next;
Eric Snow59032962018-09-14 14:17:20 -0700352 continue;
353 }
354
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200355 _PyInterpreterState_Clear(runtime, interp); // XXX must activate?
356 zapthreads(runtime, interp);
Eric Snow59032962018-09-14 14:17:20 -0700357 if (interp->id_mutex != NULL) {
358 PyThread_free_lock(interp->id_mutex);
359 }
Stéphane Wirtelb5409da2019-02-20 15:27:22 +0100360 PyInterpreterState *prev_interp = interp;
361 interp = interp->next;
362 PyMem_RawFree(prev_interp);
Eric Snow59032962018-09-14 14:17:20 -0700363 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200364 HEAD_UNLOCK(runtime);
Eric Snow59032962018-09-14 14:17:20 -0700365
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200366 if (interpreters->head == NULL) {
Eric Snow59032962018-09-14 14:17:20 -0700367 Py_FatalError("PyInterpreterState_DeleteExceptMain: missing main");
368 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200369 _PyThreadState_Swap(gilstate, tstate);
Eric Snow59032962018-09-14 14:17:20 -0700370}
371
372
Victor Stinnercaba55b2018-08-03 15:33:52 +0200373PyInterpreterState *
374_PyInterpreterState_Get(void)
375{
Victor Stinner50b48572018-11-01 01:51:40 +0100376 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnercaba55b2018-08-03 15:33:52 +0200377 if (tstate == NULL) {
378 Py_FatalError("_PyInterpreterState_Get(): no current thread state");
379 }
380 PyInterpreterState *interp = tstate->interp;
381 if (interp == NULL) {
382 Py_FatalError("_PyInterpreterState_Get(): no current interpreter");
383 }
384 return interp;
385}
386
387
Eric Snowe3774162017-05-22 19:46:40 -0700388int64_t
389PyInterpreterState_GetID(PyInterpreterState *interp)
390{
391 if (interp == NULL) {
392 PyErr_SetString(PyExc_RuntimeError, "no interpreter provided");
393 return -1;
394 }
395 return interp->id;
396}
397
398
Eric Snow5be45a62019-03-08 22:47:07 -0700399static PyInterpreterState *
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200400interp_look_up_id(_PyRuntimeState *runtime, PY_INT64_T requested_id)
Eric Snowb05b7112019-03-01 12:35:10 -0700401{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200402 PyInterpreterState *interp = runtime->interpreters.head;
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100403 while (interp != NULL) {
404 PY_INT64_T id = PyInterpreterState_GetID(interp);
Eric Snow5be45a62019-03-08 22:47:07 -0700405 if (id < 0) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100406 return NULL;
Eric Snow5be45a62019-03-08 22:47:07 -0700407 }
408 if (requested_id == id) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100409 return interp;
Eric Snow5be45a62019-03-08 22:47:07 -0700410 }
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100411 interp = PyInterpreterState_Next(interp);
Eric Snowb05b7112019-03-01 12:35:10 -0700412 }
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100413 return NULL;
Eric Snowb05b7112019-03-01 12:35:10 -0700414}
415
Eric Snow5be45a62019-03-08 22:47:07 -0700416PyInterpreterState *
417_PyInterpreterState_LookUpID(PY_INT64_T requested_id)
418{
419 PyInterpreterState *interp = NULL;
420 if (requested_id >= 0) {
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200421 _PyRuntimeState *runtime = &_PyRuntime;
422 HEAD_LOCK(runtime);
423 interp = interp_look_up_id(runtime, requested_id);
424 HEAD_UNLOCK(runtime);
Eric Snow5be45a62019-03-08 22:47:07 -0700425 }
426 if (interp == NULL && !PyErr_Occurred()) {
427 PyErr_Format(PyExc_RuntimeError,
428 "unrecognized interpreter ID %lld", requested_id);
429 }
430 return interp;
431}
432
Eric Snow4c6955e2018-02-16 18:53:40 -0700433
434int
435_PyInterpreterState_IDInitref(PyInterpreterState *interp)
436{
437 if (interp->id_mutex != NULL) {
438 return 0;
439 }
440 interp->id_mutex = PyThread_allocate_lock();
441 if (interp->id_mutex == NULL) {
442 PyErr_SetString(PyExc_RuntimeError,
443 "failed to create init interpreter ID mutex");
444 return -1;
445 }
446 interp->id_refcount = 0;
447 return 0;
448}
449
450
451void
452_PyInterpreterState_IDIncref(PyInterpreterState *interp)
453{
454 if (interp->id_mutex == NULL) {
455 return;
456 }
457 PyThread_acquire_lock(interp->id_mutex, WAIT_LOCK);
458 interp->id_refcount += 1;
459 PyThread_release_lock(interp->id_mutex);
460}
461
462
463void
464_PyInterpreterState_IDDecref(PyInterpreterState *interp)
465{
466 if (interp->id_mutex == NULL) {
467 return;
468 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200469 struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
Eric Snow4c6955e2018-02-16 18:53:40 -0700470 PyThread_acquire_lock(interp->id_mutex, WAIT_LOCK);
471 assert(interp->id_refcount != 0);
472 interp->id_refcount -= 1;
473 int64_t refcount = interp->id_refcount;
474 PyThread_release_lock(interp->id_mutex);
475
Eric Snowc11183c2019-03-15 16:35:46 -0600476 if (refcount == 0 && interp->requires_idref) {
Eric Snowf53d9f22018-02-20 16:30:17 -0700477 // XXX Using the "head" thread isn't strictly correct.
478 PyThreadState *tstate = PyInterpreterState_ThreadHead(interp);
479 // XXX Possible GILState issues?
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200480 PyThreadState *save_tstate = _PyThreadState_Swap(gilstate, tstate);
Eric Snow4c6955e2018-02-16 18:53:40 -0700481 Py_EndInterpreter(tstate);
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200482 _PyThreadState_Swap(gilstate, save_tstate);
Eric Snow4c6955e2018-02-16 18:53:40 -0700483 }
484}
485
Eric Snowc11183c2019-03-15 16:35:46 -0600486int
487_PyInterpreterState_RequiresIDRef(PyInterpreterState *interp)
488{
489 return interp->requires_idref;
490}
491
492void
493_PyInterpreterState_RequireIDRef(PyInterpreterState *interp, int required)
494{
495 interp->requires_idref = required ? 1 : 0;
496}
497
Eric Snowbe3b2952019-02-23 11:35:52 -0700498_PyCoreConfig *
499_PyInterpreterState_GetCoreConfig(PyInterpreterState *interp)
500{
501 return &interp->core_config;
502}
503
Eric Snowc11183c2019-03-15 16:35:46 -0600504PyObject *
505_PyInterpreterState_GetMainModule(PyInterpreterState *interp)
506{
507 if (interp->modules == NULL) {
508 PyErr_SetString(PyExc_RuntimeError, "interpreter not initialized");
509 return NULL;
510 }
511 return PyMapping_GetItemString(interp->modules, "__main__");
512}
513
Eric Snowd2fdd1f2019-03-15 17:47:43 -0600514PyObject *
515PyInterpreterState_GetDict(PyInterpreterState *interp)
516{
517 if (interp->dict == NULL) {
518 interp->dict = PyDict_New();
519 if (interp->dict == NULL) {
520 PyErr_Clear();
521 }
522 }
523 /* Returning NULL means no per-interpreter dict is available. */
524 return interp->dict;
525}
526
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000527/* Default implementation for _PyThreadState_GetFrame */
528static struct _frame *
529threadstate_getframe(PyThreadState *self)
530{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 return self->frame;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000532}
533
Victor Stinner45b9be52010-03-03 23:28:07 +0000534static PyThreadState *
535new_threadstate(PyInterpreterState *interp, int init)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000536{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200537 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner1a7425f2013-07-07 16:25:15 +0200538 PyThreadState *tstate = (PyThreadState *)PyMem_RawMalloc(sizeof(PyThreadState));
Victor Stinner8bb32302019-04-24 16:47:40 +0200539 if (tstate == NULL) {
540 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000542
Victor Stinner8bb32302019-04-24 16:47:40 +0200543 if (_PyThreadState_GetFrame == NULL) {
544 _PyThreadState_GetFrame = threadstate_getframe;
545 }
546
547 tstate->interp = interp;
548
549 tstate->frame = NULL;
550 tstate->recursion_depth = 0;
551 tstate->overflowed = 0;
552 tstate->recursion_critical = 0;
553 tstate->stackcheck_counter = 0;
554 tstate->tracing = 0;
555 tstate->use_tracing = 0;
556 tstate->gilstate_counter = 0;
557 tstate->async_exc = NULL;
558 tstate->thread_id = PyThread_get_thread_ident();
559
560 tstate->dict = NULL;
561
562 tstate->curexc_type = NULL;
563 tstate->curexc_value = NULL;
564 tstate->curexc_traceback = NULL;
565
566 tstate->exc_state.exc_type = NULL;
567 tstate->exc_state.exc_value = NULL;
568 tstate->exc_state.exc_traceback = NULL;
569 tstate->exc_state.previous_item = NULL;
570 tstate->exc_info = &tstate->exc_state;
571
572 tstate->c_profilefunc = NULL;
573 tstate->c_tracefunc = NULL;
574 tstate->c_profileobj = NULL;
575 tstate->c_traceobj = NULL;
576
577 tstate->trash_delete_nesting = 0;
578 tstate->trash_delete_later = NULL;
579 tstate->on_delete = NULL;
580 tstate->on_delete_data = NULL;
581
582 tstate->coroutine_origin_tracking_depth = 0;
583
584 tstate->coroutine_wrapper = NULL;
585 tstate->in_coroutine_wrapper = 0;
586
587 tstate->async_gen_firstiter = NULL;
588 tstate->async_gen_finalizer = NULL;
589
590 tstate->context = NULL;
591 tstate->context_ver = 1;
592
593 tstate->id = ++interp->tstate_next_unique_id;
594
595 if (init) {
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200596 _PyThreadState_Init(runtime, tstate);
Victor Stinner8bb32302019-04-24 16:47:40 +0200597 }
598
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200599 HEAD_LOCK(runtime);
Victor Stinner8bb32302019-04-24 16:47:40 +0200600 tstate->prev = NULL;
601 tstate->next = interp->tstate_head;
602 if (tstate->next)
603 tstate->next->prev = tstate;
604 interp->tstate_head = tstate;
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200605 HEAD_UNLOCK(runtime);
Victor Stinner8bb32302019-04-24 16:47:40 +0200606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 return tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000608}
609
Victor Stinner45b9be52010-03-03 23:28:07 +0000610PyThreadState *
611PyThreadState_New(PyInterpreterState *interp)
612{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000613 return new_threadstate(interp, 1);
Victor Stinner45b9be52010-03-03 23:28:07 +0000614}
615
616PyThreadState *
617_PyThreadState_Prealloc(PyInterpreterState *interp)
618{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 return new_threadstate(interp, 0);
Victor Stinner45b9be52010-03-03 23:28:07 +0000620}
621
622void
Victor Stinner8bb32302019-04-24 16:47:40 +0200623_PyThreadState_Init(_PyRuntimeState *runtime, PyThreadState *tstate)
Victor Stinner45b9be52010-03-03 23:28:07 +0000624{
Victor Stinner8bb32302019-04-24 16:47:40 +0200625 _PyGILState_NoteThreadState(&runtime->gilstate, tstate);
Victor Stinner45b9be52010-03-03 23:28:07 +0000626}
627
Martin v. Löwis1a214512008-06-11 05:26:20 +0000628PyObject*
Martin v. Löwis7800f752012-06-22 12:20:55 +0200629PyState_FindModule(struct PyModuleDef* module)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000630{
Martin v. Löwis7800f752012-06-22 12:20:55 +0200631 Py_ssize_t index = module->m_base.m_index;
Victor Stinner9204fb82018-10-30 15:13:17 +0100632 PyInterpreterState *state = _PyInterpreterState_GET_UNSAFE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 PyObject *res;
Nick Coghland5cacbb2015-05-23 22:24:10 +1000634 if (module->m_slots) {
635 return NULL;
636 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 if (index == 0)
638 return NULL;
639 if (state->modules_by_index == NULL)
640 return NULL;
Antoine Pitrou75506e82012-08-20 19:30:46 +0200641 if (index >= PyList_GET_SIZE(state->modules_by_index))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 return NULL;
643 res = PyList_GET_ITEM(state->modules_by_index, index);
644 return res==Py_None ? NULL : res;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000645}
646
647int
648_PyState_AddModule(PyObject* module, struct PyModuleDef* def)
649{
Nick Coghland5cacbb2015-05-23 22:24:10 +1000650 PyInterpreterState *state;
Berker Peksag4b7b5652016-08-22 18:05:56 +0300651 if (!def) {
652 assert(PyErr_Occurred());
653 return -1;
654 }
Nick Coghland5cacbb2015-05-23 22:24:10 +1000655 if (def->m_slots) {
656 PyErr_SetString(PyExc_SystemError,
657 "PyState_AddModule called on module with slots");
658 return -1;
659 }
Victor Stinner9204fb82018-10-30 15:13:17 +0100660 state = _PyInterpreterState_GET_UNSAFE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 if (!state->modules_by_index) {
662 state->modules_by_index = PyList_New(0);
663 if (!state->modules_by_index)
664 return -1;
665 }
666 while(PyList_GET_SIZE(state->modules_by_index) <= def->m_base.m_index)
667 if (PyList_Append(state->modules_by_index, Py_None) < 0)
668 return -1;
669 Py_INCREF(module);
670 return PyList_SetItem(state->modules_by_index,
671 def->m_base.m_index, module);
Martin v. Löwis1a214512008-06-11 05:26:20 +0000672}
Guido van Rossuma027efa1997-05-05 20:56:21 +0000673
Martin v. Löwis7800f752012-06-22 12:20:55 +0200674int
675PyState_AddModule(PyObject* module, struct PyModuleDef* def)
676{
677 Py_ssize_t index;
Victor Stinner9204fb82018-10-30 15:13:17 +0100678 PyInterpreterState *state = _PyInterpreterState_GET_UNSAFE();
Martin v. Löwis7800f752012-06-22 12:20:55 +0200679 if (!def) {
680 Py_FatalError("PyState_AddModule: Module Definition is NULL");
681 return -1;
682 }
683 index = def->m_base.m_index;
684 if (state->modules_by_index) {
685 if(PyList_GET_SIZE(state->modules_by_index) >= index) {
686 if(module == PyList_GET_ITEM(state->modules_by_index, index)) {
687 Py_FatalError("PyState_AddModule: Module already added!");
688 return -1;
689 }
690 }
691 }
692 return _PyState_AddModule(module, def);
693}
694
695int
696PyState_RemoveModule(struct PyModuleDef* def)
697{
Nick Coghland5cacbb2015-05-23 22:24:10 +1000698 PyInterpreterState *state;
Martin v. Löwis7800f752012-06-22 12:20:55 +0200699 Py_ssize_t index = def->m_base.m_index;
Nick Coghland5cacbb2015-05-23 22:24:10 +1000700 if (def->m_slots) {
701 PyErr_SetString(PyExc_SystemError,
702 "PyState_RemoveModule called on module with slots");
703 return -1;
704 }
Victor Stinner9204fb82018-10-30 15:13:17 +0100705 state = _PyInterpreterState_GET_UNSAFE();
Martin v. Löwis7800f752012-06-22 12:20:55 +0200706 if (index == 0) {
707 Py_FatalError("PyState_RemoveModule: Module index invalid.");
708 return -1;
709 }
710 if (state->modules_by_index == NULL) {
711 Py_FatalError("PyState_RemoveModule: Interpreters module-list not acessible.");
712 return -1;
713 }
714 if (index > PyList_GET_SIZE(state->modules_by_index)) {
715 Py_FatalError("PyState_RemoveModule: Module index out of bounds.");
716 return -1;
717 }
Zackery Spytz2a893432018-12-05 00:14:00 -0700718 Py_INCREF(Py_None);
Martin v. Löwis7800f752012-06-22 12:20:55 +0200719 return PyList_SetItem(state->modules_by_index, index, Py_None);
720}
721
Antoine Pitrou40322e62013-08-11 00:30:09 +0200722/* used by import.c:PyImport_Cleanup */
723void
724_PyState_ClearModules(void)
725{
Victor Stinner9204fb82018-10-30 15:13:17 +0100726 PyInterpreterState *state = _PyInterpreterState_GET_UNSAFE();
Antoine Pitrou40322e62013-08-11 00:30:09 +0200727 if (state->modules_by_index) {
728 Py_ssize_t i;
729 for (i = 0; i < PyList_GET_SIZE(state->modules_by_index); i++) {
730 PyObject *m = PyList_GET_ITEM(state->modules_by_index, i);
731 if (PyModule_Check(m)) {
732 /* cleanup the saved copy of module dicts */
733 PyModuleDef *md = PyModule_GetDef(m);
734 if (md)
735 Py_CLEAR(md->m_base.m_copy);
736 }
737 }
738 /* Setting modules_by_index to NULL could be dangerous, so we
739 clear the list instead. */
740 if (PyList_SetSlice(state->modules_by_index,
741 0, PyList_GET_SIZE(state->modules_by_index),
742 NULL))
743 PyErr_WriteUnraisable(state->modules_by_index);
744 }
745}
746
Guido van Rossuma027efa1997-05-05 20:56:21 +0000747void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000748PyThreadState_Clear(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000749{
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200750 int verbose = tstate->interp->core_config.verbose;
751
752 if (verbose && tstate->frame != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 fprintf(stderr,
754 "PyThreadState_Clear: warning: thread still has a frame\n");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 Py_CLEAR(tstate->frame);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000757
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 Py_CLEAR(tstate->dict);
759 Py_CLEAR(tstate->async_exc);
Guido van Rossumede04391998-04-10 20:18:25 +0000760
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 Py_CLEAR(tstate->curexc_type);
762 Py_CLEAR(tstate->curexc_value);
763 Py_CLEAR(tstate->curexc_traceback);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000764
Mark Shannonae3087c2017-10-22 22:41:51 +0100765 Py_CLEAR(tstate->exc_state.exc_type);
766 Py_CLEAR(tstate->exc_state.exc_value);
767 Py_CLEAR(tstate->exc_state.exc_traceback);
Serhiy Storchakabdf42982017-10-26 16:59:40 +0300768
Mark Shannonae3087c2017-10-22 22:41:51 +0100769 /* The stack of exception states should contain just this thread. */
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200770 if (verbose && tstate->exc_info != &tstate->exc_state) {
Mark Shannonae3087c2017-10-22 22:41:51 +0100771 fprintf(stderr,
772 "PyThreadState_Clear: warning: thread still has a generator\n");
773 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000774
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 tstate->c_profilefunc = NULL;
776 tstate->c_tracefunc = NULL;
777 Py_CLEAR(tstate->c_profileobj);
778 Py_CLEAR(tstate->c_traceobj);
Yury Selivanov75445082015-05-11 22:57:16 -0400779
780 Py_CLEAR(tstate->coroutine_wrapper);
Yury Selivanoveb636452016-09-08 22:01:51 -0700781 Py_CLEAR(tstate->async_gen_firstiter);
782 Py_CLEAR(tstate->async_gen_finalizer);
Yury Selivanovf23746a2018-01-22 19:11:18 -0500783
784 Py_CLEAR(tstate->context);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000785}
786
787
Guido van Rossum29757862001-01-23 01:46:06 +0000788/* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
789static void
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200790tstate_delete_common(_PyRuntimeState *runtime, PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000791{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200792 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 Py_FatalError("PyThreadState_Delete: NULL tstate");
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200794 }
795 PyInterpreterState *interp = tstate->interp;
796 if (interp == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 Py_FatalError("PyThreadState_Delete: NULL interp");
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200798 }
799 HEAD_LOCK(runtime);
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200800 if (tstate->prev)
801 tstate->prev->next = tstate->next;
802 else
803 interp->tstate_head = tstate->next;
804 if (tstate->next)
805 tstate->next->prev = tstate->prev;
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200806 HEAD_UNLOCK(runtime);
Antoine Pitrou7b476992013-09-07 23:38:37 +0200807 if (tstate->on_delete != NULL) {
808 tstate->on_delete(tstate->on_delete_data);
809 }
Victor Stinner1a7425f2013-07-07 16:25:15 +0200810 PyMem_RawFree(tstate);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000811}
812
813
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200814static void
815_PyThreadState_Delete(_PyRuntimeState *runtime, PyThreadState *tstate)
Guido van Rossum29757862001-01-23 01:46:06 +0000816{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200817 struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
818 if (tstate == _PyRuntimeGILState_GetThreadState(gilstate)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 Py_FatalError("PyThreadState_Delete: tstate is still current");
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600820 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200821 if (gilstate->autoInterpreterState &&
822 PyThread_tss_get(&gilstate->autoTSSkey) == tstate)
823 {
824 PyThread_tss_set(&gilstate->autoTSSkey, NULL);
825 }
826 tstate_delete_common(runtime, tstate);
Guido van Rossum29757862001-01-23 01:46:06 +0000827}
828
829
Guido van Rossum29757862001-01-23 01:46:06 +0000830void
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200831PyThreadState_Delete(PyThreadState *tstate)
Guido van Rossum29757862001-01-23 01:46:06 +0000832{
Victor Stinner99e69d42019-04-26 05:48:51 +0200833 _PyThreadState_Delete(&_PyRuntime, tstate);
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200834}
835
836
837static void
838_PyThreadState_DeleteCurrent(_PyRuntimeState *runtime)
839{
840 struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
841 PyThreadState *tstate = _PyRuntimeGILState_GetThreadState(gilstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 if (tstate == NULL)
843 Py_FatalError(
844 "PyThreadState_DeleteCurrent: no current tstate");
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200845 tstate_delete_common(runtime, tstate);
846 if (gilstate->autoInterpreterState &&
847 PyThread_tss_get(&gilstate->autoTSSkey) == tstate)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600848 {
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200849 PyThread_tss_set(&gilstate->autoTSSkey, NULL);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600850 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200851 _PyRuntimeGILState_SetThreadState(gilstate, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 PyEval_ReleaseLock();
Guido van Rossum29757862001-01-23 01:46:06 +0000853}
Guido van Rossum29757862001-01-23 01:46:06 +0000854
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200855void
856PyThreadState_DeleteCurrent()
857{
858 _PyThreadState_DeleteCurrent(&_PyRuntime);
859}
860
Guido van Rossum29757862001-01-23 01:46:06 +0000861
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200862/*
863 * Delete all thread states except the one passed as argument.
864 * Note that, if there is a current thread state, it *must* be the one
865 * passed as argument. Also, this won't touch any other interpreters
866 * than the current one, since we don't know which thread state should
867 * be kept in those other interpreteres.
868 */
869void
Victor Stinner09532fe2019-05-10 23:39:09 +0200870_PyThreadState_DeleteExcept(_PyRuntimeState *runtime, PyThreadState *tstate)
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200871{
872 PyInterpreterState *interp = tstate->interp;
873 PyThreadState *p, *next, *garbage;
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200874 HEAD_LOCK(runtime);
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200875 /* Remove all thread states, except tstate, from the linked list of
876 thread states. This will allow calling PyThreadState_Clear()
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200877 without holding the lock. */
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200878 garbage = interp->tstate_head;
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200879 if (garbage == tstate)
880 garbage = tstate->next;
881 if (tstate->prev)
882 tstate->prev->next = tstate->next;
883 if (tstate->next)
884 tstate->next->prev = tstate->prev;
885 tstate->prev = tstate->next = NULL;
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200886 interp->tstate_head = tstate;
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200887 HEAD_UNLOCK(runtime);
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200888 /* Clear and deallocate all stale thread states. Even if this
889 executes Python code, we should be safe since it executes
890 in the current thread, not one of the stale threads. */
891 for (p = garbage; p; p = next) {
892 next = p->next;
893 PyThreadState_Clear(p);
Victor Stinner1a7425f2013-07-07 16:25:15 +0200894 PyMem_RawFree(p);
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200895 }
896}
897
898
Guido van Rossuma027efa1997-05-05 20:56:21 +0000899PyThreadState *
Victor Stinnerbfd316e2016-01-20 11:12:38 +0100900_PyThreadState_UncheckedGet(void)
901{
Victor Stinner50b48572018-11-01 01:51:40 +0100902 return _PyThreadState_GET();
Victor Stinnerbfd316e2016-01-20 11:12:38 +0100903}
904
905
906PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000907PyThreadState_Get(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000908{
Victor Stinner50b48572018-11-01 01:51:40 +0100909 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 if (tstate == NULL)
911 Py_FatalError("PyThreadState_Get: no current thread");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000912
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 return tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000914}
915
916
Victor Stinner09532fe2019-05-10 23:39:09 +0200917PyThreadState *
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200918_PyThreadState_Swap(struct _gilstate_runtime_state *gilstate, PyThreadState *newts)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000919{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200920 PyThreadState *oldts = _PyRuntimeGILState_GetThreadState(gilstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000921
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200922 _PyRuntimeGILState_SetThreadState(gilstate, newts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 /* It should not be possible for more than one thread state
924 to be used for a thread. Check this the best we can in debug
925 builds.
926 */
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200927#if defined(Py_DEBUG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 if (newts) {
929 /* This can be called from PyEval_RestoreThread(). Similar
930 to it, we need to ensure errno doesn't change.
931 */
932 int err = errno;
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200933 PyThreadState *check = _PyGILState_GetThisThreadState(gilstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 if (check && check->interp == newts->interp && check != newts)
935 Py_FatalError("Invalid thread state for this thread");
936 errno = err;
937 }
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000938#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 return oldts;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000940}
Guido van Rossumede04391998-04-10 20:18:25 +0000941
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200942PyThreadState *
943PyThreadState_Swap(PyThreadState *newts)
944{
945 return _PyThreadState_Swap(&_PyRuntime.gilstate, newts);
946}
947
Guido van Rossumede04391998-04-10 20:18:25 +0000948/* An extension mechanism to store arbitrary additional per-thread state.
949 PyThreadState_GetDict() returns a dictionary that can be used to hold such
950 state; the caller should pick a unique key and store its state there. If
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000951 PyThreadState_GetDict() returns NULL, an exception has *not* been raised
952 and the caller should assume no per-thread state is available. */
Guido van Rossumede04391998-04-10 20:18:25 +0000953
954PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000955PyThreadState_GetDict(void)
Guido van Rossumede04391998-04-10 20:18:25 +0000956{
Victor Stinner50b48572018-11-01 01:51:40 +0100957 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 if (tstate == NULL)
959 return NULL;
Guido van Rossumede04391998-04-10 20:18:25 +0000960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 if (tstate->dict == NULL) {
962 PyObject *d;
963 tstate->dict = d = PyDict_New();
964 if (d == NULL)
965 PyErr_Clear();
966 }
967 return tstate->dict;
Guido van Rossumede04391998-04-10 20:18:25 +0000968}
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000969
970
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000971/* Asynchronously raise an exception in a thread.
972 Requested by Just van Rossum and Alex Martelli.
Guido van Rossum0f1f63c2005-02-08 02:07:57 +0000973 To prevent naive misuse, you must write your own extension
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000974 to call this, or use ctypes. Must be called with the GIL held.
975 Returns the number of tstates modified (normally 1, but 0 if `id` didn't
976 match any known thread id). Can be called with exc=NULL to clear an
977 existing async exception. This raises no exceptions. */
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000978
979int
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200980PyThreadState_SetAsyncExc(unsigned long id, PyObject *exc)
981{
Victor Stinner09532fe2019-05-10 23:39:09 +0200982 _PyRuntimeState *runtime = &_PyRuntime;
983 PyInterpreterState *interp = _PyRuntimeState_GetThreadState(runtime)->interp;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 /* Although the GIL is held, a few C API functions can be called
986 * without the GIL held, and in particular some that create and
987 * destroy thread and interpreter states. Those can mutate the
988 * list of thread states we're traversing, so to prevent that we lock
989 * head_mutex for the duration.
990 */
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200991 HEAD_LOCK(runtime);
Victor Stinner09532fe2019-05-10 23:39:09 +0200992 for (PyThreadState *p = interp->tstate_head; p != NULL; p = p->next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 if (p->thread_id == id) {
994 /* Tricky: we need to decref the current value
995 * (if any) in p->async_exc, but that can in turn
996 * allow arbitrary Python code to run, including
997 * perhaps calls to this function. To prevent
998 * deadlock, we need to release head_mutex before
999 * the decref.
1000 */
1001 PyObject *old_exc = p->async_exc;
1002 Py_XINCREF(exc);
1003 p->async_exc = exc;
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001004 HEAD_UNLOCK(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 Py_XDECREF(old_exc);
Victor Stinner09532fe2019-05-10 23:39:09 +02001006 _PyEval_SignalAsyncExc(&runtime->ceval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 return 1;
1008 }
1009 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001010 HEAD_UNLOCK(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 return 0;
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +00001012}
1013
1014
Guido van Rossumf5df46d2001-07-19 12:19:27 +00001015/* Routines for advanced debuggers, requested by David Beazley.
1016 Don't use unless you know what you are doing! */
1017
1018PyInterpreterState *
1019PyInterpreterState_Head(void)
1020{
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001021 return _PyRuntime.interpreters.head;
Guido van Rossumf5df46d2001-07-19 12:19:27 +00001022}
1023
1024PyInterpreterState *
Eric Snow6b4be192017-05-22 21:36:03 -07001025PyInterpreterState_Main(void)
1026{
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001027 return _PyRuntime.interpreters.main;
Eric Snow6b4be192017-05-22 21:36:03 -07001028}
1029
1030PyInterpreterState *
Guido van Rossumf5df46d2001-07-19 12:19:27 +00001031PyInterpreterState_Next(PyInterpreterState *interp) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 return interp->next;
Guido van Rossumf5df46d2001-07-19 12:19:27 +00001033}
1034
1035PyThreadState *
1036PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 return interp->tstate_head;
Guido van Rossumf5df46d2001-07-19 12:19:27 +00001038}
1039
1040PyThreadState *
1041PyThreadState_Next(PyThreadState *tstate) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 return tstate->next;
Guido van Rossumf5df46d2001-07-19 12:19:27 +00001043}
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001044
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001045/* The implementation of sys._current_frames(). This is intended to be
1046 called with the GIL held, as it will be when called via
1047 sys._current_frames(). It's possible it would work fine even without
1048 the GIL held, but haven't thought enough about that.
1049*/
1050PyObject *
1051_PyThread_CurrentFrames(void)
1052{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 PyObject *result;
1054 PyInterpreterState *i;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 result = PyDict_New();
1057 if (result == NULL)
1058 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 /* for i in all interpreters:
1061 * for t in all of i's thread states:
1062 * if t's frame isn't NULL, map t's id to its frame
Ezio Melotti13925002011-03-16 11:05:33 +02001063 * Because these lists can mutate even when the GIL is held, we
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 * need to grab head_mutex for the duration.
1065 */
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001066 _PyRuntimeState *runtime = &_PyRuntime;
1067 HEAD_LOCK(runtime);
1068 for (i = runtime->interpreters.head; i != NULL; i = i->next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 PyThreadState *t;
1070 for (t = i->tstate_head; t != NULL; t = t->next) {
1071 PyObject *id;
1072 int stat;
1073 struct _frame *frame = t->frame;
1074 if (frame == NULL)
1075 continue;
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001076 id = PyLong_FromUnsignedLong(t->thread_id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 if (id == NULL)
1078 goto Fail;
1079 stat = PyDict_SetItem(result, id, (PyObject *)frame);
1080 Py_DECREF(id);
1081 if (stat < 0)
1082 goto Fail;
1083 }
1084 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001085 HEAD_UNLOCK(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001086 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001087
1088 Fail:
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001089 HEAD_UNLOCK(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 Py_DECREF(result);
1091 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001092}
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +00001093
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001094/* Python "auto thread state" API. */
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001095
1096/* Keep this as a static, as it is not reliable! It can only
1097 ever be compared to the state for the *current* thread.
1098 * If not equal, then it doesn't matter that the actual
1099 value may change immediately after comparison, as it can't
1100 possibly change to the current thread's state.
1101 * If equal, then the current thread holds the lock, so the value can't
1102 change until we yield the lock.
1103*/
1104static int
1105PyThreadState_IsCurrent(PyThreadState *tstate)
1106{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 /* Must be the tstate for this thread */
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001108 struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
1109 assert(_PyGILState_GetThisThreadState(gilstate) == tstate);
1110 return tstate == _PyRuntimeGILState_GetThreadState(gilstate);
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001111}
1112
Tim Peters4c1f5ec2004-10-09 17:25:05 +00001113/* Internal initialization/finalization functions called by
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001114 Py_Initialize/Py_FinalizeEx
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001115*/
Tim Peters19717fa2004-10-09 17:38:29 +00001116void
Victor Stinner43125222019-04-24 18:23:53 +02001117_PyGILState_Init(_PyRuntimeState *runtime,
1118 PyInterpreterState *interp, PyThreadState *tstate)
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001119{
Victor Stinner8bb32302019-04-24 16:47:40 +02001120 /* must init with valid states */
1121 assert(interp != NULL);
1122 assert(tstate != NULL);
1123
Victor Stinner8bb32302019-04-24 16:47:40 +02001124 struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
1125
1126 if (PyThread_tss_create(&gilstate->autoTSSkey) != 0) {
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001127 Py_FatalError("Could not allocate TSS entry");
1128 }
Victor Stinner8bb32302019-04-24 16:47:40 +02001129 gilstate->autoInterpreterState = interp;
1130 assert(PyThread_tss_get(&gilstate->autoTSSkey) == NULL);
1131 assert(tstate->gilstate_counter == 0);
Michael W. Hudson188d4362005-06-20 16:52:57 +00001132
Victor Stinner8bb32302019-04-24 16:47:40 +02001133 _PyGILState_NoteThreadState(gilstate, tstate);
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001134}
1135
Victor Stinner861d9ab2016-03-16 22:45:24 +01001136PyInterpreterState *
1137_PyGILState_GetInterpreterStateUnsafe(void)
1138{
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001139 return _PyRuntime.gilstate.autoInterpreterState;
Victor Stinner861d9ab2016-03-16 22:45:24 +01001140}
1141
Tim Peters19717fa2004-10-09 17:38:29 +00001142void
Victor Stinner8e91c242019-04-24 17:24:01 +02001143_PyGILState_Fini(_PyRuntimeState *runtime)
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001144{
Victor Stinner8e91c242019-04-24 17:24:01 +02001145 struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
1146 PyThread_tss_delete(&gilstate->autoTSSkey);
1147 gilstate->autoInterpreterState = NULL;
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001148}
1149
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001150/* Reset the TSS key - called by PyOS_AfterFork_Child().
Antoine Pitrou0c759fe2011-04-27 19:28:05 +02001151 * This should not be necessary, but some - buggy - pthread implementations
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001152 * don't reset TSS upon fork(), see issue #10517.
Antoine Pitrou0c759fe2011-04-27 19:28:05 +02001153 */
1154void
Victor Stinnerb930a2d2019-04-24 17:14:33 +02001155_PyGILState_Reinit(_PyRuntimeState *runtime)
Antoine Pitrou0c759fe2011-04-27 19:28:05 +02001156{
Victor Stinnerb930a2d2019-04-24 17:14:33 +02001157 struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001158 PyThreadState *tstate = _PyGILState_GetThisThreadState(gilstate);
Victor Stinnerb930a2d2019-04-24 17:14:33 +02001159
1160 PyThread_tss_delete(&gilstate->autoTSSkey);
1161 if (PyThread_tss_create(&gilstate->autoTSSkey) != 0) {
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001162 Py_FatalError("Could not allocate TSS entry");
1163 }
Antoine Pitrou0c759fe2011-04-27 19:28:05 +02001164
Charles-François Natalia233df82011-11-22 19:49:51 +01001165 /* If the thread had an associated auto thread state, reassociate it with
1166 * the new key. */
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001167 if (tstate &&
Victor Stinnerb930a2d2019-04-24 17:14:33 +02001168 PyThread_tss_set(&gilstate->autoTSSkey, (void *)tstate) != 0)
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001169 {
1170 Py_FatalError("Couldn't create autoTSSkey mapping");
1171 }
Antoine Pitrou0c759fe2011-04-27 19:28:05 +02001172}
1173
Michael W. Hudson188d4362005-06-20 16:52:57 +00001174/* When a thread state is created for a thread by some mechanism other than
1175 PyGILState_Ensure, it's important that the GILState machinery knows about
1176 it so it doesn't try to create another thread state for the thread (this is
1177 a better fix for SF bug #1010677 than the first one attempted).
1178*/
Thomas Wouters89f507f2006-12-13 04:49:30 +00001179static void
Victor Stinner8bb32302019-04-24 16:47:40 +02001180_PyGILState_NoteThreadState(struct _gilstate_runtime_state *gilstate, PyThreadState* tstate)
Michael W. Hudson188d4362005-06-20 16:52:57 +00001181{
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001182 /* If autoTSSkey isn't initialized, this must be the very first
Antoine Pitrou079ce542010-09-08 12:37:10 +00001183 threadstate created in Py_Initialize(). Don't do anything for now
1184 (we'll be back here when _PyGILState_Init is called). */
Victor Stinner8bb32302019-04-24 16:47:40 +02001185 if (!gilstate->autoInterpreterState) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 return;
Victor Stinner8bb32302019-04-24 16:47:40 +02001187 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001188
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001189 /* Stick the thread state for this thread in thread specific storage.
Michael W. Hudson188d4362005-06-20 16:52:57 +00001190
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001191 The only situation where you can legitimately have more than one
1192 thread state for an OS level thread is when there are multiple
Victor Stinner590cebe2013-12-13 11:08:56 +01001193 interpreters.
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001194
Victor Stinner590cebe2013-12-13 11:08:56 +01001195 You shouldn't really be using the PyGILState_ APIs anyway (see issues
1196 #10915 and #15751).
Michael W. Hudson188d4362005-06-20 16:52:57 +00001197
Victor Stinner590cebe2013-12-13 11:08:56 +01001198 The first thread state created for that given OS level thread will
1199 "win", which seems reasonable behaviour.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001200 */
Victor Stinner8bb32302019-04-24 16:47:40 +02001201 if (PyThread_tss_get(&gilstate->autoTSSkey) == NULL) {
1202 if ((PyThread_tss_set(&gilstate->autoTSSkey, (void *)tstate)) != 0) {
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001203 Py_FatalError("Couldn't create autoTSSkey mapping");
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001204 }
Victor Stinner590cebe2013-12-13 11:08:56 +01001205 }
Michael W. Hudson188d4362005-06-20 16:52:57 +00001206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207 /* PyGILState_Release must not try to delete this thread state. */
1208 tstate->gilstate_counter = 1;
Michael W. Hudson188d4362005-06-20 16:52:57 +00001209}
1210
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001211/* The public functions */
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001212static PyThreadState *
1213_PyGILState_GetThisThreadState(struct _gilstate_runtime_state *gilstate)
1214{
1215 if (gilstate->autoInterpreterState == NULL)
1216 return NULL;
1217 return (PyThreadState *)PyThread_tss_get(&gilstate->autoTSSkey);
1218}
1219
Tim Peters19717fa2004-10-09 17:38:29 +00001220PyThreadState *
1221PyGILState_GetThisThreadState(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001222{
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001223 return _PyGILState_GetThisThreadState(&_PyRuntime.gilstate);
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001224}
1225
Kristján Valur Jónsson684cd0e2013-03-23 03:36:16 -07001226int
1227PyGILState_Check(void)
1228{
Victor Stinner8a1be612016-03-14 22:07:55 +01001229
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001230 if (!_PyGILState_check_enabled) {
Victor Stinner8a1be612016-03-14 22:07:55 +01001231 return 1;
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001232 }
Victor Stinner8a1be612016-03-14 22:07:55 +01001233
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001234 struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
1235 if (!PyThread_tss_is_created(&gilstate->autoTSSkey)) {
1236 return 1;
1237 }
Victor Stinner8a1be612016-03-14 22:07:55 +01001238
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001239 PyThreadState *tstate = _PyRuntimeGILState_GetThreadState(gilstate);
1240 if (tstate == NULL) {
1241 return 0;
1242 }
1243
1244 return (tstate == _PyGILState_GetThisThreadState(gilstate));
Kristján Valur Jónsson684cd0e2013-03-23 03:36:16 -07001245}
1246
Tim Peters19717fa2004-10-09 17:38:29 +00001247PyGILState_STATE
1248PyGILState_Ensure(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001249{
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001250 struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 int current;
1252 PyThreadState *tcur;
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +01001253 int need_init_threads = 0;
1254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 /* Note that we do not auto-init Python here - apart from
1256 potential races with 2 threads auto-initializing, pep-311
1257 spells out other issues. Embedders are expected to have
1258 called Py_Initialize() and usually PyEval_InitThreads().
1259 */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001260 /* Py_Initialize() hasn't been called! */
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001261 assert(gilstate->autoInterpreterState);
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +01001262
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001263 tcur = (PyThreadState *)PyThread_tss_get(&gilstate->autoTSSkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 if (tcur == NULL) {
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +01001265 need_init_threads = 1;
Victor Stinner62ca1002013-12-13 01:46:43 +01001266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 /* Create a new thread state for this thread */
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001268 tcur = PyThreadState_New(gilstate->autoInterpreterState);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 if (tcur == NULL)
1270 Py_FatalError("Couldn't create thread-state for new thread");
1271 /* This is our thread state! We'll need to delete it in the
1272 matching call to PyGILState_Release(). */
1273 tcur->gilstate_counter = 0;
1274 current = 0; /* new thread state is never current */
1275 }
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +01001276 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 current = PyThreadState_IsCurrent(tcur);
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +01001278 }
1279
1280 if (current == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 PyEval_RestoreThread(tcur);
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +01001282 }
1283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 /* Update our counter in the thread-state - no need for locks:
1285 - tcur will remain valid as we hold the GIL.
1286 - the counter is safe as we are the only thread "allowed"
1287 to modify this value
1288 */
1289 ++tcur->gilstate_counter;
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +01001290
1291 if (need_init_threads) {
1292 /* At startup, Python has no concrete GIL. If PyGILState_Ensure() is
1293 called from a new thread for the first time, we need the create the
1294 GIL. */
1295 PyEval_InitThreads();
1296 }
1297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001298 return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001299}
1300
Tim Peters19717fa2004-10-09 17:38:29 +00001301void
1302PyGILState_Release(PyGILState_STATE oldstate)
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001303{
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001304 _PyRuntimeState *runtime = &_PyRuntime;
1305 PyThreadState *tcur = PyThread_tss_get(&runtime->gilstate.autoTSSkey);
1306 if (tcur == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 Py_FatalError("auto-releasing thread-state, "
1308 "but no thread-state for this thread");
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001309 }
1310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 /* We must hold the GIL and have our thread state current */
1312 /* XXX - remove the check - the assert should be fine,
1313 but while this is very new (April 2003), the extra check
1314 by release-only users can't hurt.
1315 */
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001316 if (!PyThreadState_IsCurrent(tcur)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 Py_FatalError("This thread state must be current when releasing");
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001318 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 assert(PyThreadState_IsCurrent(tcur));
1320 --tcur->gilstate_counter;
1321 assert(tcur->gilstate_counter >= 0); /* illegal counter value */
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323 /* If we're going to destroy this thread-state, we must
1324 * clear it while the GIL is held, as destructors may run.
1325 */
1326 if (tcur->gilstate_counter == 0) {
1327 /* can't have been locked when we created it */
1328 assert(oldstate == PyGILState_UNLOCKED);
1329 PyThreadState_Clear(tcur);
1330 /* Delete the thread-state. Note this releases the GIL too!
1331 * It's vital that the GIL be held here, to avoid shutdown
1332 * races; see bugs 225673 and 1061968 (that nasty bug has a
1333 * habit of coming back).
1334 */
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001335 _PyThreadState_DeleteCurrent(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001336 }
1337 /* Release the lock if necessary */
1338 else if (oldstate == PyGILState_UNLOCKED)
1339 PyEval_SaveThread();
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001340}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001341
Benjamin Peterson3bf01752012-04-13 18:06:36 -04001342
Eric Snow7f8bfc92018-01-29 18:23:44 -07001343/**************************/
1344/* cross-interpreter data */
1345/**************************/
1346
1347/* cross-interpreter data */
1348
1349crossinterpdatafunc _PyCrossInterpreterData_Lookup(PyObject *);
1350
1351/* This is a separate func from _PyCrossInterpreterData_Lookup in order
1352 to keep the registry code separate. */
1353static crossinterpdatafunc
1354_lookup_getdata(PyObject *obj)
1355{
1356 crossinterpdatafunc getdata = _PyCrossInterpreterData_Lookup(obj);
1357 if (getdata == NULL && PyErr_Occurred() == 0)
1358 PyErr_Format(PyExc_ValueError,
1359 "%S does not support cross-interpreter data", obj);
1360 return getdata;
1361}
1362
1363int
1364_PyObject_CheckCrossInterpreterData(PyObject *obj)
1365{
1366 crossinterpdatafunc getdata = _lookup_getdata(obj);
1367 if (getdata == NULL) {
1368 return -1;
1369 }
1370 return 0;
1371}
1372
1373static int
1374_check_xidata(_PyCrossInterpreterData *data)
1375{
1376 // data->data can be anything, including NULL, so we don't check it.
1377
1378 // data->obj may be NULL, so we don't check it.
1379
1380 if (data->interp < 0) {
1381 PyErr_SetString(PyExc_SystemError, "missing interp");
1382 return -1;
1383 }
1384
1385 if (data->new_object == NULL) {
1386 PyErr_SetString(PyExc_SystemError, "missing new_object func");
1387 return -1;
1388 }
1389
1390 // data->free may be NULL, so we don't check it.
1391
1392 return 0;
1393}
1394
1395int
1396_PyObject_GetCrossInterpreterData(PyObject *obj, _PyCrossInterpreterData *data)
1397{
Victor Stinnercaba55b2018-08-03 15:33:52 +02001398 // _PyInterpreterState_Get() aborts if lookup fails, so we don't need
Eric Snow7f8bfc92018-01-29 18:23:44 -07001399 // to check the result for NULL.
Victor Stinnercaba55b2018-08-03 15:33:52 +02001400 PyInterpreterState *interp = _PyInterpreterState_Get();
Eric Snow7f8bfc92018-01-29 18:23:44 -07001401
1402 // Reset data before re-populating.
1403 *data = (_PyCrossInterpreterData){0};
1404 data->free = PyMem_RawFree; // Set a default that may be overridden.
1405
1406 // Call the "getdata" func for the object.
1407 Py_INCREF(obj);
1408 crossinterpdatafunc getdata = _lookup_getdata(obj);
1409 if (getdata == NULL) {
1410 Py_DECREF(obj);
1411 return -1;
1412 }
1413 int res = getdata(obj, data);
1414 Py_DECREF(obj);
1415 if (res != 0) {
1416 return -1;
1417 }
1418
1419 // Fill in the blanks and validate the result.
Eric Snow7f8bfc92018-01-29 18:23:44 -07001420 data->interp = interp->id;
1421 if (_check_xidata(data) != 0) {
1422 _PyCrossInterpreterData_Release(data);
1423 return -1;
1424 }
1425
1426 return 0;
1427}
1428
Eric Snowb75b1a352019-04-12 10:20:10 -06001429static void
Eric Snow63799132018-06-01 18:45:20 -06001430_release_xidata(void *arg)
1431{
1432 _PyCrossInterpreterData *data = (_PyCrossInterpreterData *)arg;
1433 if (data->free != NULL) {
1434 data->free(data->data);
1435 }
1436 Py_XDECREF(data->obj);
Eric Snowb75b1a352019-04-12 10:20:10 -06001437}
1438
1439static void
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001440_call_in_interpreter(struct _gilstate_runtime_state *gilstate,
1441 PyInterpreterState *interp,
Eric Snowb75b1a352019-04-12 10:20:10 -06001442 void (*func)(void *), void *arg)
1443{
1444 /* We would use Py_AddPendingCall() if it weren't specific to the
1445 * main interpreter (see bpo-33608). In the meantime we take a
1446 * naive approach.
1447 */
1448 PyThreadState *save_tstate = NULL;
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001449 if (interp != _PyRuntimeGILState_GetThreadState(gilstate)->interp) {
Eric Snowb75b1a352019-04-12 10:20:10 -06001450 // XXX Using the "head" thread isn't strictly correct.
1451 PyThreadState *tstate = PyInterpreterState_ThreadHead(interp);
1452 // XXX Possible GILState issues?
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001453 save_tstate = _PyThreadState_Swap(gilstate, tstate);
Eric Snowb75b1a352019-04-12 10:20:10 -06001454 }
1455
1456 func(arg);
1457
1458 // Switch back.
1459 if (save_tstate != NULL) {
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001460 _PyThreadState_Swap(gilstate, save_tstate);
Eric Snowb75b1a352019-04-12 10:20:10 -06001461 }
Eric Snow63799132018-06-01 18:45:20 -06001462}
1463
Eric Snow7f8bfc92018-01-29 18:23:44 -07001464void
1465_PyCrossInterpreterData_Release(_PyCrossInterpreterData *data)
1466{
1467 if (data->data == NULL && data->obj == NULL) {
1468 // Nothing to release!
1469 return;
1470 }
1471
Eric Snowb75b1a352019-04-12 10:20:10 -06001472 // Switch to the original interpreter.
Eric Snow7f8bfc92018-01-29 18:23:44 -07001473 PyInterpreterState *interp = _PyInterpreterState_LookUpID(data->interp);
1474 if (interp == NULL) {
1475 // The intepreter was already destroyed.
1476 if (data->free != NULL) {
1477 // XXX Someone leaked some memory...
1478 }
1479 return;
1480 }
Eric Snowf53d9f22018-02-20 16:30:17 -07001481
Eric Snow7f8bfc92018-01-29 18:23:44 -07001482 // "Release" the data and/or the object.
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001483 struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
1484 _call_in_interpreter(gilstate, interp, _release_xidata, data);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001485}
1486
1487PyObject *
1488_PyCrossInterpreterData_NewObject(_PyCrossInterpreterData *data)
1489{
1490 return data->new_object(data);
1491}
1492
1493/* registry of {type -> crossinterpdatafunc} */
1494
1495/* For now we use a global registry of shareable classes. An
1496 alternative would be to add a tp_* slot for a class's
1497 crossinterpdatafunc. It would be simpler and more efficient. */
1498
1499static int
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001500_register_xidata(struct _xidregistry *xidregistry, PyTypeObject *cls,
1501 crossinterpdatafunc getdata)
Eric Snow7f8bfc92018-01-29 18:23:44 -07001502{
1503 // Note that we effectively replace already registered classes
1504 // rather than failing.
1505 struct _xidregitem *newhead = PyMem_RawMalloc(sizeof(struct _xidregitem));
1506 if (newhead == NULL)
1507 return -1;
1508 newhead->cls = cls;
1509 newhead->getdata = getdata;
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001510 newhead->next = xidregistry->head;
1511 xidregistry->head = newhead;
Eric Snow7f8bfc92018-01-29 18:23:44 -07001512 return 0;
1513}
1514
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001515static void _register_builtins_for_crossinterpreter_data(struct _xidregistry *xidregistry);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001516
1517int
Eric Snowc11183c2019-03-15 16:35:46 -06001518_PyCrossInterpreterData_RegisterClass(PyTypeObject *cls,
Eric Snow7f8bfc92018-01-29 18:23:44 -07001519 crossinterpdatafunc getdata)
1520{
1521 if (!PyType_Check(cls)) {
1522 PyErr_Format(PyExc_ValueError, "only classes may be registered");
1523 return -1;
1524 }
1525 if (getdata == NULL) {
1526 PyErr_Format(PyExc_ValueError, "missing 'getdata' func");
1527 return -1;
1528 }
1529
1530 // Make sure the class isn't ever deallocated.
1531 Py_INCREF((PyObject *)cls);
1532
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001533 struct _xidregistry *xidregistry = &_PyRuntime.xidregistry ;
1534 PyThread_acquire_lock(xidregistry->mutex, WAIT_LOCK);
1535 if (xidregistry->head == NULL) {
1536 _register_builtins_for_crossinterpreter_data(xidregistry);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001537 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001538 int res = _register_xidata(xidregistry, cls, getdata);
1539 PyThread_release_lock(xidregistry->mutex);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001540 return res;
1541}
1542
Eric Snow6d2cd902018-05-16 15:04:57 -04001543/* Cross-interpreter objects are looked up by exact match on the class.
1544 We can reassess this policy when we move from a global registry to a
1545 tp_* slot. */
1546
Eric Snow7f8bfc92018-01-29 18:23:44 -07001547crossinterpdatafunc
1548_PyCrossInterpreterData_Lookup(PyObject *obj)
1549{
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001550 struct _xidregistry *xidregistry = &_PyRuntime.xidregistry ;
Eric Snow7f8bfc92018-01-29 18:23:44 -07001551 PyObject *cls = PyObject_Type(obj);
1552 crossinterpdatafunc getdata = NULL;
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001553 PyThread_acquire_lock(xidregistry->mutex, WAIT_LOCK);
1554 struct _xidregitem *cur = xidregistry->head;
Eric Snow7f8bfc92018-01-29 18:23:44 -07001555 if (cur == NULL) {
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001556 _register_builtins_for_crossinterpreter_data(xidregistry);
1557 cur = xidregistry->head;
Eric Snow7f8bfc92018-01-29 18:23:44 -07001558 }
1559 for(; cur != NULL; cur = cur->next) {
1560 if (cur->cls == (PyTypeObject *)cls) {
1561 getdata = cur->getdata;
1562 break;
1563 }
1564 }
Eric Snow4e9da0d2018-02-02 21:49:49 -07001565 Py_DECREF(cls);
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001566 PyThread_release_lock(xidregistry->mutex);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001567 return getdata;
1568}
1569
1570/* cross-interpreter data for builtin types */
1571
Eric Snow6d2cd902018-05-16 15:04:57 -04001572struct _shared_bytes_data {
1573 char *bytes;
1574 Py_ssize_t len;
1575};
1576
Eric Snow7f8bfc92018-01-29 18:23:44 -07001577static PyObject *
1578_new_bytes_object(_PyCrossInterpreterData *data)
1579{
Eric Snow6d2cd902018-05-16 15:04:57 -04001580 struct _shared_bytes_data *shared = (struct _shared_bytes_data *)(data->data);
1581 return PyBytes_FromStringAndSize(shared->bytes, shared->len);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001582}
1583
1584static int
1585_bytes_shared(PyObject *obj, _PyCrossInterpreterData *data)
1586{
Eric Snow6d2cd902018-05-16 15:04:57 -04001587 struct _shared_bytes_data *shared = PyMem_NEW(struct _shared_bytes_data, 1);
1588 if (PyBytes_AsStringAndSize(obj, &shared->bytes, &shared->len) < 0) {
1589 return -1;
1590 }
1591 data->data = (void *)shared;
Eric Snow63799132018-06-01 18:45:20 -06001592 Py_INCREF(obj);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001593 data->obj = obj; // Will be "released" (decref'ed) when data released.
1594 data->new_object = _new_bytes_object;
Eric Snow6d2cd902018-05-16 15:04:57 -04001595 data->free = PyMem_Free;
1596 return 0;
1597}
1598
1599struct _shared_str_data {
1600 int kind;
1601 const void *buffer;
1602 Py_ssize_t len;
1603};
1604
1605static PyObject *
1606_new_str_object(_PyCrossInterpreterData *data)
1607{
1608 struct _shared_str_data *shared = (struct _shared_str_data *)(data->data);
1609 return PyUnicode_FromKindAndData(shared->kind, shared->buffer, shared->len);
1610}
1611
1612static int
1613_str_shared(PyObject *obj, _PyCrossInterpreterData *data)
1614{
1615 struct _shared_str_data *shared = PyMem_NEW(struct _shared_str_data, 1);
1616 shared->kind = PyUnicode_KIND(obj);
1617 shared->buffer = PyUnicode_DATA(obj);
1618 shared->len = PyUnicode_GET_LENGTH(obj) - 1;
1619 data->data = (void *)shared;
Eric Snow63799132018-06-01 18:45:20 -06001620 Py_INCREF(obj);
Eric Snow6d2cd902018-05-16 15:04:57 -04001621 data->obj = obj; // Will be "released" (decref'ed) when data released.
1622 data->new_object = _new_str_object;
1623 data->free = PyMem_Free;
1624 return 0;
1625}
1626
1627static PyObject *
1628_new_long_object(_PyCrossInterpreterData *data)
1629{
Alexey Izbyshev16f842d2019-02-12 19:06:43 +03001630 return PyLong_FromSsize_t((Py_ssize_t)(data->data));
Eric Snow6d2cd902018-05-16 15:04:57 -04001631}
1632
1633static int
1634_long_shared(PyObject *obj, _PyCrossInterpreterData *data)
1635{
Alexey Izbyshev16f842d2019-02-12 19:06:43 +03001636 /* Note that this means the size of shareable ints is bounded by
1637 * sys.maxsize. Hence on 32-bit architectures that is half the
1638 * size of maximum shareable ints on 64-bit.
1639 */
1640 Py_ssize_t value = PyLong_AsSsize_t(obj);
Eric Snow6d2cd902018-05-16 15:04:57 -04001641 if (value == -1 && PyErr_Occurred()) {
1642 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
1643 PyErr_SetString(PyExc_OverflowError, "try sending as bytes");
1644 }
1645 return -1;
1646 }
1647 data->data = (void *)value;
1648 data->obj = NULL;
1649 data->new_object = _new_long_object;
1650 data->free = NULL;
Eric Snow7f8bfc92018-01-29 18:23:44 -07001651 return 0;
1652}
1653
1654static PyObject *
1655_new_none_object(_PyCrossInterpreterData *data)
1656{
1657 // XXX Singleton refcounts are problematic across interpreters...
1658 Py_INCREF(Py_None);
1659 return Py_None;
1660}
1661
1662static int
1663_none_shared(PyObject *obj, _PyCrossInterpreterData *data)
1664{
1665 data->data = NULL;
1666 // data->obj remains NULL
1667 data->new_object = _new_none_object;
1668 data->free = NULL; // There is nothing to free.
1669 return 0;
1670}
1671
1672static void
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001673_register_builtins_for_crossinterpreter_data(struct _xidregistry *xidregistry)
Eric Snow7f8bfc92018-01-29 18:23:44 -07001674{
1675 // None
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001676 if (_register_xidata(xidregistry, (PyTypeObject *)PyObject_Type(Py_None), _none_shared) != 0) {
Eric Snow7f8bfc92018-01-29 18:23:44 -07001677 Py_FatalError("could not register None for cross-interpreter sharing");
1678 }
1679
Eric Snow6d2cd902018-05-16 15:04:57 -04001680 // int
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001681 if (_register_xidata(xidregistry, &PyLong_Type, _long_shared) != 0) {
Eric Snow6d2cd902018-05-16 15:04:57 -04001682 Py_FatalError("could not register int for cross-interpreter sharing");
1683 }
1684
Eric Snow7f8bfc92018-01-29 18:23:44 -07001685 // bytes
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001686 if (_register_xidata(xidregistry, &PyBytes_Type, _bytes_shared) != 0) {
Eric Snow7f8bfc92018-01-29 18:23:44 -07001687 Py_FatalError("could not register bytes for cross-interpreter sharing");
1688 }
Eric Snow6d2cd902018-05-16 15:04:57 -04001689
1690 // str
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001691 if (_register_xidata(xidregistry, &PyUnicode_Type, _str_shared) != 0) {
Eric Snow6d2cd902018-05-16 15:04:57 -04001692 Py_FatalError("could not register str for cross-interpreter sharing");
1693 }
Eric Snow7f8bfc92018-01-29 18:23:44 -07001694}
1695
1696
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001697#ifdef __cplusplus
1698}
1699#endif