blob: 44acfed6b983551ce5a121e362527e3eea17a266 [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 Stinnerf684d832019-03-01 03:44:13 +01005#include "pycore_coreconfig.h"
Victor Stinner621cebe2018-11-12 16:53:38 +01006#include "pycore_pymem.h"
7#include "pycore_pystate.h"
Eric Snow86ea5812019-05-10 13:29:55 -04008#include "pycore_pylifecycle.h"
Guido van Rossuma027efa1997-05-05 20:56:21 +00009
Tim Peters84705582004-10-10 02:47:33 +000010/* --------------------------------------------------------------------------
11CAUTION
12
Victor Stinner1a7425f2013-07-07 16:25:15 +020013Always use PyMem_RawMalloc() and PyMem_RawFree() directly in this file. A
14number of these functions are advertised as safe to call when the GIL isn't
15held, and in a debug build Python redirects (e.g.) PyMem_NEW (etc) to Python's
16debugging obmalloc functions. Those aren't thread-safe (they rely on the GIL
17to avoid the expense of doing their own locking).
Tim Peters84705582004-10-10 02:47:33 +000018-------------------------------------------------------------------------- */
19
Martin v. Löwisf0473d52001-07-18 16:17:16 +000020#ifdef HAVE_DLOPEN
21#ifdef HAVE_DLFCN_H
22#include <dlfcn.h>
23#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030024#if !HAVE_DECL_RTLD_LAZY
Martin v. Löwisf0473d52001-07-18 16:17:16 +000025#define RTLD_LAZY 1
26#endif
27#endif
28
Benjamin Peterson43162b82012-04-13 11:58:27 -040029#ifdef __cplusplus
30extern "C" {
31#endif
Martin v. Löwisf0473d52001-07-18 16:17:16 +000032
Victor Stinner10c8e6a2019-04-26 01:53:18 +020033#define _PyRuntimeGILState_GetThreadState(gilstate) \
34 ((PyThreadState*)_Py_atomic_load_relaxed(&(gilstate)->tstate_current))
35#define _PyRuntimeGILState_SetThreadState(gilstate, value) \
36 _Py_atomic_store_relaxed(&(gilstate)->tstate_current, \
37 (uintptr_t)(value))
38
39/* Forward declarations */
40static PyThreadState *_PyGILState_GetThisThreadState(struct _gilstate_runtime_state *gilstate);
41static void _PyThreadState_Delete(_PyRuntimeState *runtime, PyThreadState *tstate);
42static PyThreadState *_PyThreadState_Swap(struct _gilstate_runtime_state *gilstate, PyThreadState *newts);
43
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
870_PyThreadState_DeleteExcept(PyThreadState *tstate)
871{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200872 _PyRuntimeState *runtime = &_PyRuntime;
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200873 PyInterpreterState *interp = tstate->interp;
874 PyThreadState *p, *next, *garbage;
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200875 HEAD_LOCK(runtime);
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200876 /* Remove all thread states, except tstate, from the linked list of
877 thread states. This will allow calling PyThreadState_Clear()
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200878 without holding the lock. */
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200879 garbage = interp->tstate_head;
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200880 if (garbage == tstate)
881 garbage = tstate->next;
882 if (tstate->prev)
883 tstate->prev->next = tstate->next;
884 if (tstate->next)
885 tstate->next->prev = tstate->prev;
886 tstate->prev = tstate->next = NULL;
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200887 interp->tstate_head = tstate;
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200888 HEAD_UNLOCK(runtime);
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200889 /* Clear and deallocate all stale thread states. Even if this
890 executes Python code, we should be safe since it executes
891 in the current thread, not one of the stale threads. */
892 for (p = garbage; p; p = next) {
893 next = p->next;
894 PyThreadState_Clear(p);
Victor Stinner1a7425f2013-07-07 16:25:15 +0200895 PyMem_RawFree(p);
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200896 }
897}
898
899
Guido van Rossuma027efa1997-05-05 20:56:21 +0000900PyThreadState *
Victor Stinnerbfd316e2016-01-20 11:12:38 +0100901_PyThreadState_UncheckedGet(void)
902{
Victor Stinner50b48572018-11-01 01:51:40 +0100903 return _PyThreadState_GET();
Victor Stinnerbfd316e2016-01-20 11:12:38 +0100904}
905
906
907PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000908PyThreadState_Get(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000909{
Victor Stinner50b48572018-11-01 01:51:40 +0100910 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 if (tstate == NULL)
912 Py_FatalError("PyThreadState_Get: no current thread");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000913
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 return tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000915}
916
917
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200918static PyThreadState *
919_PyThreadState_Swap(struct _gilstate_runtime_state *gilstate, PyThreadState *newts)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000920{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200921 PyThreadState *oldts = _PyRuntimeGILState_GetThreadState(gilstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000922
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200923 _PyRuntimeGILState_SetThreadState(gilstate, newts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 /* It should not be possible for more than one thread state
925 to be used for a thread. Check this the best we can in debug
926 builds.
927 */
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200928#if defined(Py_DEBUG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 if (newts) {
930 /* This can be called from PyEval_RestoreThread(). Similar
931 to it, we need to ensure errno doesn't change.
932 */
933 int err = errno;
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200934 PyThreadState *check = _PyGILState_GetThisThreadState(gilstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 if (check && check->interp == newts->interp && check != newts)
936 Py_FatalError("Invalid thread state for this thread");
937 errno = err;
938 }
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000939#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 return oldts;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000941}
Guido van Rossumede04391998-04-10 20:18:25 +0000942
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200943PyThreadState *
944PyThreadState_Swap(PyThreadState *newts)
945{
946 return _PyThreadState_Swap(&_PyRuntime.gilstate, newts);
947}
948
Guido van Rossumede04391998-04-10 20:18:25 +0000949/* An extension mechanism to store arbitrary additional per-thread state.
950 PyThreadState_GetDict() returns a dictionary that can be used to hold such
951 state; the caller should pick a unique key and store its state there. If
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000952 PyThreadState_GetDict() returns NULL, an exception has *not* been raised
953 and the caller should assume no per-thread state is available. */
Guido van Rossumede04391998-04-10 20:18:25 +0000954
955PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000956PyThreadState_GetDict(void)
Guido van Rossumede04391998-04-10 20:18:25 +0000957{
Victor Stinner50b48572018-11-01 01:51:40 +0100958 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 if (tstate == NULL)
960 return NULL;
Guido van Rossumede04391998-04-10 20:18:25 +0000961
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 if (tstate->dict == NULL) {
963 PyObject *d;
964 tstate->dict = d = PyDict_New();
965 if (d == NULL)
966 PyErr_Clear();
967 }
968 return tstate->dict;
Guido van Rossumede04391998-04-10 20:18:25 +0000969}
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000970
971
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000972/* Asynchronously raise an exception in a thread.
973 Requested by Just van Rossum and Alex Martelli.
Guido van Rossum0f1f63c2005-02-08 02:07:57 +0000974 To prevent naive misuse, you must write your own extension
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000975 to call this, or use ctypes. Must be called with the GIL held.
976 Returns the number of tstates modified (normally 1, but 0 if `id` didn't
977 match any known thread id). Can be called with exc=NULL to clear an
978 existing async exception. This raises no exceptions. */
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000979
980int
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200981PyThreadState_SetAsyncExc(unsigned long id, PyObject *exc)
982{
Victor Stinner9204fb82018-10-30 15:13:17 +0100983 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 PyThreadState *p;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000985
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 /* Although the GIL is held, a few C API functions can be called
987 * without the GIL held, and in particular some that create and
988 * destroy thread and interpreter states. Those can mutate the
989 * list of thread states we're traversing, so to prevent that we lock
990 * head_mutex for the duration.
991 */
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200992 _PyRuntimeState *runtime = &_PyRuntime;
993 HEAD_LOCK(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 for (p = interp->tstate_head; p != NULL; p = p->next) {
995 if (p->thread_id == id) {
996 /* Tricky: we need to decref the current value
997 * (if any) in p->async_exc, but that can in turn
998 * allow arbitrary Python code to run, including
999 * perhaps calls to this function. To prevent
1000 * deadlock, we need to release head_mutex before
1001 * the decref.
1002 */
1003 PyObject *old_exc = p->async_exc;
1004 Py_XINCREF(exc);
1005 p->async_exc = exc;
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001006 HEAD_UNLOCK(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 Py_XDECREF(old_exc);
Eric Snowb75b1a352019-04-12 10:20:10 -06001008 _PyEval_SignalAsyncExc();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 return 1;
1010 }
1011 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001012 HEAD_UNLOCK(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 return 0;
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +00001014}
1015
1016
Guido van Rossumf5df46d2001-07-19 12:19:27 +00001017/* Routines for advanced debuggers, requested by David Beazley.
1018 Don't use unless you know what you are doing! */
1019
1020PyInterpreterState *
1021PyInterpreterState_Head(void)
1022{
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001023 return _PyRuntime.interpreters.head;
Guido van Rossumf5df46d2001-07-19 12:19:27 +00001024}
1025
1026PyInterpreterState *
Eric Snow6b4be192017-05-22 21:36:03 -07001027PyInterpreterState_Main(void)
1028{
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001029 return _PyRuntime.interpreters.main;
Eric Snow6b4be192017-05-22 21:36:03 -07001030}
1031
1032PyInterpreterState *
Guido van Rossumf5df46d2001-07-19 12:19:27 +00001033PyInterpreterState_Next(PyInterpreterState *interp) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 return interp->next;
Guido van Rossumf5df46d2001-07-19 12:19:27 +00001035}
1036
1037PyThreadState *
1038PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 return interp->tstate_head;
Guido van Rossumf5df46d2001-07-19 12:19:27 +00001040}
1041
1042PyThreadState *
1043PyThreadState_Next(PyThreadState *tstate) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 return tstate->next;
Guido van Rossumf5df46d2001-07-19 12:19:27 +00001045}
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001046
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001047/* The implementation of sys._current_frames(). This is intended to be
1048 called with the GIL held, as it will be when called via
1049 sys._current_frames(). It's possible it would work fine even without
1050 the GIL held, but haven't thought enough about that.
1051*/
1052PyObject *
1053_PyThread_CurrentFrames(void)
1054{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 PyObject *result;
1056 PyInterpreterState *i;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 result = PyDict_New();
1059 if (result == NULL)
1060 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001061
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 /* for i in all interpreters:
1063 * for t in all of i's thread states:
1064 * if t's frame isn't NULL, map t's id to its frame
Ezio Melotti13925002011-03-16 11:05:33 +02001065 * Because these lists can mutate even when the GIL is held, we
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 * need to grab head_mutex for the duration.
1067 */
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001068 _PyRuntimeState *runtime = &_PyRuntime;
1069 HEAD_LOCK(runtime);
1070 for (i = runtime->interpreters.head; i != NULL; i = i->next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 PyThreadState *t;
1072 for (t = i->tstate_head; t != NULL; t = t->next) {
1073 PyObject *id;
1074 int stat;
1075 struct _frame *frame = t->frame;
1076 if (frame == NULL)
1077 continue;
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001078 id = PyLong_FromUnsignedLong(t->thread_id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 if (id == NULL)
1080 goto Fail;
1081 stat = PyDict_SetItem(result, id, (PyObject *)frame);
1082 Py_DECREF(id);
1083 if (stat < 0)
1084 goto Fail;
1085 }
1086 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001087 HEAD_UNLOCK(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001089
1090 Fail:
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001091 HEAD_UNLOCK(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001092 Py_DECREF(result);
1093 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001094}
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +00001095
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001096/* Python "auto thread state" API. */
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001097
1098/* Keep this as a static, as it is not reliable! It can only
1099 ever be compared to the state for the *current* thread.
1100 * If not equal, then it doesn't matter that the actual
1101 value may change immediately after comparison, as it can't
1102 possibly change to the current thread's state.
1103 * If equal, then the current thread holds the lock, so the value can't
1104 change until we yield the lock.
1105*/
1106static int
1107PyThreadState_IsCurrent(PyThreadState *tstate)
1108{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 /* Must be the tstate for this thread */
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001110 struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
1111 assert(_PyGILState_GetThisThreadState(gilstate) == tstate);
1112 return tstate == _PyRuntimeGILState_GetThreadState(gilstate);
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001113}
1114
Tim Peters4c1f5ec2004-10-09 17:25:05 +00001115/* Internal initialization/finalization functions called by
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001116 Py_Initialize/Py_FinalizeEx
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001117*/
Tim Peters19717fa2004-10-09 17:38:29 +00001118void
Victor Stinner43125222019-04-24 18:23:53 +02001119_PyGILState_Init(_PyRuntimeState *runtime,
1120 PyInterpreterState *interp, PyThreadState *tstate)
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001121{
Victor Stinner8bb32302019-04-24 16:47:40 +02001122 /* must init with valid states */
1123 assert(interp != NULL);
1124 assert(tstate != NULL);
1125
Victor Stinner8bb32302019-04-24 16:47:40 +02001126 struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
1127
1128 if (PyThread_tss_create(&gilstate->autoTSSkey) != 0) {
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001129 Py_FatalError("Could not allocate TSS entry");
1130 }
Victor Stinner8bb32302019-04-24 16:47:40 +02001131 gilstate->autoInterpreterState = interp;
1132 assert(PyThread_tss_get(&gilstate->autoTSSkey) == NULL);
1133 assert(tstate->gilstate_counter == 0);
Michael W. Hudson188d4362005-06-20 16:52:57 +00001134
Victor Stinner8bb32302019-04-24 16:47:40 +02001135 _PyGILState_NoteThreadState(gilstate, tstate);
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001136}
1137
Victor Stinner861d9ab2016-03-16 22:45:24 +01001138PyInterpreterState *
1139_PyGILState_GetInterpreterStateUnsafe(void)
1140{
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001141 return _PyRuntime.gilstate.autoInterpreterState;
Victor Stinner861d9ab2016-03-16 22:45:24 +01001142}
1143
Tim Peters19717fa2004-10-09 17:38:29 +00001144void
Victor Stinner8e91c242019-04-24 17:24:01 +02001145_PyGILState_Fini(_PyRuntimeState *runtime)
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001146{
Victor Stinner8e91c242019-04-24 17:24:01 +02001147 struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
1148 PyThread_tss_delete(&gilstate->autoTSSkey);
1149 gilstate->autoInterpreterState = NULL;
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001150}
1151
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001152/* Reset the TSS key - called by PyOS_AfterFork_Child().
Antoine Pitrou0c759fe2011-04-27 19:28:05 +02001153 * This should not be necessary, but some - buggy - pthread implementations
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001154 * don't reset TSS upon fork(), see issue #10517.
Antoine Pitrou0c759fe2011-04-27 19:28:05 +02001155 */
1156void
Victor Stinnerb930a2d2019-04-24 17:14:33 +02001157_PyGILState_Reinit(_PyRuntimeState *runtime)
Antoine Pitrou0c759fe2011-04-27 19:28:05 +02001158{
Victor Stinnerb930a2d2019-04-24 17:14:33 +02001159 struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001160 PyThreadState *tstate = _PyGILState_GetThisThreadState(gilstate);
Victor Stinnerb930a2d2019-04-24 17:14:33 +02001161
1162 PyThread_tss_delete(&gilstate->autoTSSkey);
1163 if (PyThread_tss_create(&gilstate->autoTSSkey) != 0) {
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001164 Py_FatalError("Could not allocate TSS entry");
1165 }
Antoine Pitrou0c759fe2011-04-27 19:28:05 +02001166
Charles-François Natalia233df82011-11-22 19:49:51 +01001167 /* If the thread had an associated auto thread state, reassociate it with
1168 * the new key. */
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001169 if (tstate &&
Victor Stinnerb930a2d2019-04-24 17:14:33 +02001170 PyThread_tss_set(&gilstate->autoTSSkey, (void *)tstate) != 0)
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001171 {
1172 Py_FatalError("Couldn't create autoTSSkey mapping");
1173 }
Antoine Pitrou0c759fe2011-04-27 19:28:05 +02001174}
1175
Michael W. Hudson188d4362005-06-20 16:52:57 +00001176/* When a thread state is created for a thread by some mechanism other than
1177 PyGILState_Ensure, it's important that the GILState machinery knows about
1178 it so it doesn't try to create another thread state for the thread (this is
1179 a better fix for SF bug #1010677 than the first one attempted).
1180*/
Thomas Wouters89f507f2006-12-13 04:49:30 +00001181static void
Victor Stinner8bb32302019-04-24 16:47:40 +02001182_PyGILState_NoteThreadState(struct _gilstate_runtime_state *gilstate, PyThreadState* tstate)
Michael W. Hudson188d4362005-06-20 16:52:57 +00001183{
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001184 /* If autoTSSkey isn't initialized, this must be the very first
Antoine Pitrou079ce542010-09-08 12:37:10 +00001185 threadstate created in Py_Initialize(). Don't do anything for now
1186 (we'll be back here when _PyGILState_Init is called). */
Victor Stinner8bb32302019-04-24 16:47:40 +02001187 if (!gilstate->autoInterpreterState) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 return;
Victor Stinner8bb32302019-04-24 16:47:40 +02001189 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001190
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001191 /* Stick the thread state for this thread in thread specific storage.
Michael W. Hudson188d4362005-06-20 16:52:57 +00001192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 The only situation where you can legitimately have more than one
1194 thread state for an OS level thread is when there are multiple
Victor Stinner590cebe2013-12-13 11:08:56 +01001195 interpreters.
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001196
Victor Stinner590cebe2013-12-13 11:08:56 +01001197 You shouldn't really be using the PyGILState_ APIs anyway (see issues
1198 #10915 and #15751).
Michael W. Hudson188d4362005-06-20 16:52:57 +00001199
Victor Stinner590cebe2013-12-13 11:08:56 +01001200 The first thread state created for that given OS level thread will
1201 "win", which seems reasonable behaviour.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 */
Victor Stinner8bb32302019-04-24 16:47:40 +02001203 if (PyThread_tss_get(&gilstate->autoTSSkey) == NULL) {
1204 if ((PyThread_tss_set(&gilstate->autoTSSkey, (void *)tstate)) != 0) {
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001205 Py_FatalError("Couldn't create autoTSSkey mapping");
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001206 }
Victor Stinner590cebe2013-12-13 11:08:56 +01001207 }
Michael W. Hudson188d4362005-06-20 16:52:57 +00001208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 /* PyGILState_Release must not try to delete this thread state. */
1210 tstate->gilstate_counter = 1;
Michael W. Hudson188d4362005-06-20 16:52:57 +00001211}
1212
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001213/* The public functions */
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001214static PyThreadState *
1215_PyGILState_GetThisThreadState(struct _gilstate_runtime_state *gilstate)
1216{
1217 if (gilstate->autoInterpreterState == NULL)
1218 return NULL;
1219 return (PyThreadState *)PyThread_tss_get(&gilstate->autoTSSkey);
1220}
1221
Tim Peters19717fa2004-10-09 17:38:29 +00001222PyThreadState *
1223PyGILState_GetThisThreadState(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001224{
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001225 return _PyGILState_GetThisThreadState(&_PyRuntime.gilstate);
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001226}
1227
Kristján Valur Jónsson684cd0e2013-03-23 03:36:16 -07001228int
1229PyGILState_Check(void)
1230{
Victor Stinner8a1be612016-03-14 22:07:55 +01001231
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001232 if (!_PyGILState_check_enabled) {
Victor Stinner8a1be612016-03-14 22:07:55 +01001233 return 1;
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001234 }
Victor Stinner8a1be612016-03-14 22:07:55 +01001235
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001236 struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
1237 if (!PyThread_tss_is_created(&gilstate->autoTSSkey)) {
1238 return 1;
1239 }
Victor Stinner8a1be612016-03-14 22:07:55 +01001240
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001241 PyThreadState *tstate = _PyRuntimeGILState_GetThreadState(gilstate);
1242 if (tstate == NULL) {
1243 return 0;
1244 }
1245
1246 return (tstate == _PyGILState_GetThisThreadState(gilstate));
Kristján Valur Jónsson684cd0e2013-03-23 03:36:16 -07001247}
1248
Tim Peters19717fa2004-10-09 17:38:29 +00001249PyGILState_STATE
1250PyGILState_Ensure(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001251{
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001252 struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 int current;
1254 PyThreadState *tcur;
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +01001255 int need_init_threads = 0;
1256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 /* Note that we do not auto-init Python here - apart from
1258 potential races with 2 threads auto-initializing, pep-311
1259 spells out other issues. Embedders are expected to have
1260 called Py_Initialize() and usually PyEval_InitThreads().
1261 */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001262 /* Py_Initialize() hasn't been called! */
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001263 assert(gilstate->autoInterpreterState);
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +01001264
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001265 tcur = (PyThreadState *)PyThread_tss_get(&gilstate->autoTSSkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 if (tcur == NULL) {
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +01001267 need_init_threads = 1;
Victor Stinner62ca1002013-12-13 01:46:43 +01001268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 /* Create a new thread state for this thread */
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001270 tcur = PyThreadState_New(gilstate->autoInterpreterState);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 if (tcur == NULL)
1272 Py_FatalError("Couldn't create thread-state for new thread");
1273 /* This is our thread state! We'll need to delete it in the
1274 matching call to PyGILState_Release(). */
1275 tcur->gilstate_counter = 0;
1276 current = 0; /* new thread state is never current */
1277 }
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +01001278 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 current = PyThreadState_IsCurrent(tcur);
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +01001280 }
1281
1282 if (current == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 PyEval_RestoreThread(tcur);
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +01001284 }
1285
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 /* Update our counter in the thread-state - no need for locks:
1287 - tcur will remain valid as we hold the GIL.
1288 - the counter is safe as we are the only thread "allowed"
1289 to modify this value
1290 */
1291 ++tcur->gilstate_counter;
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +01001292
1293 if (need_init_threads) {
1294 /* At startup, Python has no concrete GIL. If PyGILState_Ensure() is
1295 called from a new thread for the first time, we need the create the
1296 GIL. */
1297 PyEval_InitThreads();
1298 }
1299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001300 return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001301}
1302
Tim Peters19717fa2004-10-09 17:38:29 +00001303void
1304PyGILState_Release(PyGILState_STATE oldstate)
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001305{
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001306 _PyRuntimeState *runtime = &_PyRuntime;
1307 PyThreadState *tcur = PyThread_tss_get(&runtime->gilstate.autoTSSkey);
1308 if (tcur == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 Py_FatalError("auto-releasing thread-state, "
1310 "but no thread-state for this thread");
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001311 }
1312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 /* We must hold the GIL and have our thread state current */
1314 /* XXX - remove the check - the assert should be fine,
1315 but while this is very new (April 2003), the extra check
1316 by release-only users can't hurt.
1317 */
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001318 if (!PyThreadState_IsCurrent(tcur)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 Py_FatalError("This thread state must be current when releasing");
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001320 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 assert(PyThreadState_IsCurrent(tcur));
1322 --tcur->gilstate_counter;
1323 assert(tcur->gilstate_counter >= 0); /* illegal counter value */
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 /* If we're going to destroy this thread-state, we must
1326 * clear it while the GIL is held, as destructors may run.
1327 */
1328 if (tcur->gilstate_counter == 0) {
1329 /* can't have been locked when we created it */
1330 assert(oldstate == PyGILState_UNLOCKED);
1331 PyThreadState_Clear(tcur);
1332 /* Delete the thread-state. Note this releases the GIL too!
1333 * It's vital that the GIL be held here, to avoid shutdown
1334 * races; see bugs 225673 and 1061968 (that nasty bug has a
1335 * habit of coming back).
1336 */
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001337 _PyThreadState_DeleteCurrent(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 }
1339 /* Release the lock if necessary */
1340 else if (oldstate == PyGILState_UNLOCKED)
1341 PyEval_SaveThread();
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001342}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001343
Benjamin Peterson3bf01752012-04-13 18:06:36 -04001344
Eric Snow7f8bfc92018-01-29 18:23:44 -07001345/**************************/
1346/* cross-interpreter data */
1347/**************************/
1348
1349/* cross-interpreter data */
1350
1351crossinterpdatafunc _PyCrossInterpreterData_Lookup(PyObject *);
1352
1353/* This is a separate func from _PyCrossInterpreterData_Lookup in order
1354 to keep the registry code separate. */
1355static crossinterpdatafunc
1356_lookup_getdata(PyObject *obj)
1357{
1358 crossinterpdatafunc getdata = _PyCrossInterpreterData_Lookup(obj);
1359 if (getdata == NULL && PyErr_Occurred() == 0)
1360 PyErr_Format(PyExc_ValueError,
1361 "%S does not support cross-interpreter data", obj);
1362 return getdata;
1363}
1364
1365int
1366_PyObject_CheckCrossInterpreterData(PyObject *obj)
1367{
1368 crossinterpdatafunc getdata = _lookup_getdata(obj);
1369 if (getdata == NULL) {
1370 return -1;
1371 }
1372 return 0;
1373}
1374
1375static int
1376_check_xidata(_PyCrossInterpreterData *data)
1377{
1378 // data->data can be anything, including NULL, so we don't check it.
1379
1380 // data->obj may be NULL, so we don't check it.
1381
1382 if (data->interp < 0) {
1383 PyErr_SetString(PyExc_SystemError, "missing interp");
1384 return -1;
1385 }
1386
1387 if (data->new_object == NULL) {
1388 PyErr_SetString(PyExc_SystemError, "missing new_object func");
1389 return -1;
1390 }
1391
1392 // data->free may be NULL, so we don't check it.
1393
1394 return 0;
1395}
1396
1397int
1398_PyObject_GetCrossInterpreterData(PyObject *obj, _PyCrossInterpreterData *data)
1399{
Victor Stinnercaba55b2018-08-03 15:33:52 +02001400 // _PyInterpreterState_Get() aborts if lookup fails, so we don't need
Eric Snow7f8bfc92018-01-29 18:23:44 -07001401 // to check the result for NULL.
Victor Stinnercaba55b2018-08-03 15:33:52 +02001402 PyInterpreterState *interp = _PyInterpreterState_Get();
Eric Snow7f8bfc92018-01-29 18:23:44 -07001403
1404 // Reset data before re-populating.
1405 *data = (_PyCrossInterpreterData){0};
1406 data->free = PyMem_RawFree; // Set a default that may be overridden.
1407
1408 // Call the "getdata" func for the object.
1409 Py_INCREF(obj);
1410 crossinterpdatafunc getdata = _lookup_getdata(obj);
1411 if (getdata == NULL) {
1412 Py_DECREF(obj);
1413 return -1;
1414 }
1415 int res = getdata(obj, data);
1416 Py_DECREF(obj);
1417 if (res != 0) {
1418 return -1;
1419 }
1420
1421 // Fill in the blanks and validate the result.
Eric Snow7f8bfc92018-01-29 18:23:44 -07001422 data->interp = interp->id;
1423 if (_check_xidata(data) != 0) {
1424 _PyCrossInterpreterData_Release(data);
1425 return -1;
1426 }
1427
1428 return 0;
1429}
1430
Eric Snowb75b1a352019-04-12 10:20:10 -06001431static void
Eric Snow63799132018-06-01 18:45:20 -06001432_release_xidata(void *arg)
1433{
1434 _PyCrossInterpreterData *data = (_PyCrossInterpreterData *)arg;
1435 if (data->free != NULL) {
1436 data->free(data->data);
1437 }
1438 Py_XDECREF(data->obj);
Eric Snowb75b1a352019-04-12 10:20:10 -06001439}
1440
1441static void
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001442_call_in_interpreter(struct _gilstate_runtime_state *gilstate,
1443 PyInterpreterState *interp,
Eric Snowb75b1a352019-04-12 10:20:10 -06001444 void (*func)(void *), void *arg)
1445{
1446 /* We would use Py_AddPendingCall() if it weren't specific to the
1447 * main interpreter (see bpo-33608). In the meantime we take a
1448 * naive approach.
1449 */
1450 PyThreadState *save_tstate = NULL;
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001451 if (interp != _PyRuntimeGILState_GetThreadState(gilstate)->interp) {
Eric Snowb75b1a352019-04-12 10:20:10 -06001452 // XXX Using the "head" thread isn't strictly correct.
1453 PyThreadState *tstate = PyInterpreterState_ThreadHead(interp);
1454 // XXX Possible GILState issues?
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001455 save_tstate = _PyThreadState_Swap(gilstate, tstate);
Eric Snowb75b1a352019-04-12 10:20:10 -06001456 }
1457
1458 func(arg);
1459
1460 // Switch back.
1461 if (save_tstate != NULL) {
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001462 _PyThreadState_Swap(gilstate, save_tstate);
Eric Snowb75b1a352019-04-12 10:20:10 -06001463 }
Eric Snow63799132018-06-01 18:45:20 -06001464}
1465
Eric Snow7f8bfc92018-01-29 18:23:44 -07001466void
1467_PyCrossInterpreterData_Release(_PyCrossInterpreterData *data)
1468{
1469 if (data->data == NULL && data->obj == NULL) {
1470 // Nothing to release!
1471 return;
1472 }
1473
Eric Snowb75b1a352019-04-12 10:20:10 -06001474 // Switch to the original interpreter.
Eric Snow7f8bfc92018-01-29 18:23:44 -07001475 PyInterpreterState *interp = _PyInterpreterState_LookUpID(data->interp);
1476 if (interp == NULL) {
1477 // The intepreter was already destroyed.
1478 if (data->free != NULL) {
1479 // XXX Someone leaked some memory...
1480 }
1481 return;
1482 }
Eric Snowf53d9f22018-02-20 16:30:17 -07001483
Eric Snow7f8bfc92018-01-29 18:23:44 -07001484 // "Release" the data and/or the object.
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001485 struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
1486 _call_in_interpreter(gilstate, interp, _release_xidata, data);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001487}
1488
1489PyObject *
1490_PyCrossInterpreterData_NewObject(_PyCrossInterpreterData *data)
1491{
1492 return data->new_object(data);
1493}
1494
1495/* registry of {type -> crossinterpdatafunc} */
1496
1497/* For now we use a global registry of shareable classes. An
1498 alternative would be to add a tp_* slot for a class's
1499 crossinterpdatafunc. It would be simpler and more efficient. */
1500
1501static int
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001502_register_xidata(struct _xidregistry *xidregistry, PyTypeObject *cls,
1503 crossinterpdatafunc getdata)
Eric Snow7f8bfc92018-01-29 18:23:44 -07001504{
1505 // Note that we effectively replace already registered classes
1506 // rather than failing.
1507 struct _xidregitem *newhead = PyMem_RawMalloc(sizeof(struct _xidregitem));
1508 if (newhead == NULL)
1509 return -1;
1510 newhead->cls = cls;
1511 newhead->getdata = getdata;
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001512 newhead->next = xidregistry->head;
1513 xidregistry->head = newhead;
Eric Snow7f8bfc92018-01-29 18:23:44 -07001514 return 0;
1515}
1516
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001517static void _register_builtins_for_crossinterpreter_data(struct _xidregistry *xidregistry);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001518
1519int
Eric Snowc11183c2019-03-15 16:35:46 -06001520_PyCrossInterpreterData_RegisterClass(PyTypeObject *cls,
Eric Snow7f8bfc92018-01-29 18:23:44 -07001521 crossinterpdatafunc getdata)
1522{
1523 if (!PyType_Check(cls)) {
1524 PyErr_Format(PyExc_ValueError, "only classes may be registered");
1525 return -1;
1526 }
1527 if (getdata == NULL) {
1528 PyErr_Format(PyExc_ValueError, "missing 'getdata' func");
1529 return -1;
1530 }
1531
1532 // Make sure the class isn't ever deallocated.
1533 Py_INCREF((PyObject *)cls);
1534
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001535 struct _xidregistry *xidregistry = &_PyRuntime.xidregistry ;
1536 PyThread_acquire_lock(xidregistry->mutex, WAIT_LOCK);
1537 if (xidregistry->head == NULL) {
1538 _register_builtins_for_crossinterpreter_data(xidregistry);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001539 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001540 int res = _register_xidata(xidregistry, cls, getdata);
1541 PyThread_release_lock(xidregistry->mutex);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001542 return res;
1543}
1544
Eric Snow6d2cd902018-05-16 15:04:57 -04001545/* Cross-interpreter objects are looked up by exact match on the class.
1546 We can reassess this policy when we move from a global registry to a
1547 tp_* slot. */
1548
Eric Snow7f8bfc92018-01-29 18:23:44 -07001549crossinterpdatafunc
1550_PyCrossInterpreterData_Lookup(PyObject *obj)
1551{
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001552 struct _xidregistry *xidregistry = &_PyRuntime.xidregistry ;
Eric Snow7f8bfc92018-01-29 18:23:44 -07001553 PyObject *cls = PyObject_Type(obj);
1554 crossinterpdatafunc getdata = NULL;
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001555 PyThread_acquire_lock(xidregistry->mutex, WAIT_LOCK);
1556 struct _xidregitem *cur = xidregistry->head;
Eric Snow7f8bfc92018-01-29 18:23:44 -07001557 if (cur == NULL) {
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001558 _register_builtins_for_crossinterpreter_data(xidregistry);
1559 cur = xidregistry->head;
Eric Snow7f8bfc92018-01-29 18:23:44 -07001560 }
1561 for(; cur != NULL; cur = cur->next) {
1562 if (cur->cls == (PyTypeObject *)cls) {
1563 getdata = cur->getdata;
1564 break;
1565 }
1566 }
Eric Snow4e9da0d2018-02-02 21:49:49 -07001567 Py_DECREF(cls);
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001568 PyThread_release_lock(xidregistry->mutex);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001569 return getdata;
1570}
1571
1572/* cross-interpreter data for builtin types */
1573
Eric Snow6d2cd902018-05-16 15:04:57 -04001574struct _shared_bytes_data {
1575 char *bytes;
1576 Py_ssize_t len;
1577};
1578
Eric Snow7f8bfc92018-01-29 18:23:44 -07001579static PyObject *
1580_new_bytes_object(_PyCrossInterpreterData *data)
1581{
Eric Snow6d2cd902018-05-16 15:04:57 -04001582 struct _shared_bytes_data *shared = (struct _shared_bytes_data *)(data->data);
1583 return PyBytes_FromStringAndSize(shared->bytes, shared->len);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001584}
1585
1586static int
1587_bytes_shared(PyObject *obj, _PyCrossInterpreterData *data)
1588{
Eric Snow6d2cd902018-05-16 15:04:57 -04001589 struct _shared_bytes_data *shared = PyMem_NEW(struct _shared_bytes_data, 1);
1590 if (PyBytes_AsStringAndSize(obj, &shared->bytes, &shared->len) < 0) {
1591 return -1;
1592 }
1593 data->data = (void *)shared;
Eric Snow63799132018-06-01 18:45:20 -06001594 Py_INCREF(obj);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001595 data->obj = obj; // Will be "released" (decref'ed) when data released.
1596 data->new_object = _new_bytes_object;
Eric Snow6d2cd902018-05-16 15:04:57 -04001597 data->free = PyMem_Free;
1598 return 0;
1599}
1600
1601struct _shared_str_data {
1602 int kind;
1603 const void *buffer;
1604 Py_ssize_t len;
1605};
1606
1607static PyObject *
1608_new_str_object(_PyCrossInterpreterData *data)
1609{
1610 struct _shared_str_data *shared = (struct _shared_str_data *)(data->data);
1611 return PyUnicode_FromKindAndData(shared->kind, shared->buffer, shared->len);
1612}
1613
1614static int
1615_str_shared(PyObject *obj, _PyCrossInterpreterData *data)
1616{
1617 struct _shared_str_data *shared = PyMem_NEW(struct _shared_str_data, 1);
1618 shared->kind = PyUnicode_KIND(obj);
1619 shared->buffer = PyUnicode_DATA(obj);
1620 shared->len = PyUnicode_GET_LENGTH(obj) - 1;
1621 data->data = (void *)shared;
Eric Snow63799132018-06-01 18:45:20 -06001622 Py_INCREF(obj);
Eric Snow6d2cd902018-05-16 15:04:57 -04001623 data->obj = obj; // Will be "released" (decref'ed) when data released.
1624 data->new_object = _new_str_object;
1625 data->free = PyMem_Free;
1626 return 0;
1627}
1628
1629static PyObject *
1630_new_long_object(_PyCrossInterpreterData *data)
1631{
Alexey Izbyshev16f842d2019-02-12 19:06:43 +03001632 return PyLong_FromSsize_t((Py_ssize_t)(data->data));
Eric Snow6d2cd902018-05-16 15:04:57 -04001633}
1634
1635static int
1636_long_shared(PyObject *obj, _PyCrossInterpreterData *data)
1637{
Alexey Izbyshev16f842d2019-02-12 19:06:43 +03001638 /* Note that this means the size of shareable ints is bounded by
1639 * sys.maxsize. Hence on 32-bit architectures that is half the
1640 * size of maximum shareable ints on 64-bit.
1641 */
1642 Py_ssize_t value = PyLong_AsSsize_t(obj);
Eric Snow6d2cd902018-05-16 15:04:57 -04001643 if (value == -1 && PyErr_Occurred()) {
1644 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
1645 PyErr_SetString(PyExc_OverflowError, "try sending as bytes");
1646 }
1647 return -1;
1648 }
1649 data->data = (void *)value;
1650 data->obj = NULL;
1651 data->new_object = _new_long_object;
1652 data->free = NULL;
Eric Snow7f8bfc92018-01-29 18:23:44 -07001653 return 0;
1654}
1655
1656static PyObject *
1657_new_none_object(_PyCrossInterpreterData *data)
1658{
1659 // XXX Singleton refcounts are problematic across interpreters...
1660 Py_INCREF(Py_None);
1661 return Py_None;
1662}
1663
1664static int
1665_none_shared(PyObject *obj, _PyCrossInterpreterData *data)
1666{
1667 data->data = NULL;
1668 // data->obj remains NULL
1669 data->new_object = _new_none_object;
1670 data->free = NULL; // There is nothing to free.
1671 return 0;
1672}
1673
1674static void
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001675_register_builtins_for_crossinterpreter_data(struct _xidregistry *xidregistry)
Eric Snow7f8bfc92018-01-29 18:23:44 -07001676{
1677 // None
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001678 if (_register_xidata(xidregistry, (PyTypeObject *)PyObject_Type(Py_None), _none_shared) != 0) {
Eric Snow7f8bfc92018-01-29 18:23:44 -07001679 Py_FatalError("could not register None for cross-interpreter sharing");
1680 }
1681
Eric Snow6d2cd902018-05-16 15:04:57 -04001682 // int
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001683 if (_register_xidata(xidregistry, &PyLong_Type, _long_shared) != 0) {
Eric Snow6d2cd902018-05-16 15:04:57 -04001684 Py_FatalError("could not register int for cross-interpreter sharing");
1685 }
1686
Eric Snow7f8bfc92018-01-29 18:23:44 -07001687 // bytes
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001688 if (_register_xidata(xidregistry, &PyBytes_Type, _bytes_shared) != 0) {
Eric Snow7f8bfc92018-01-29 18:23:44 -07001689 Py_FatalError("could not register bytes for cross-interpreter sharing");
1690 }
Eric Snow6d2cd902018-05-16 15:04:57 -04001691
1692 // str
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001693 if (_register_xidata(xidregistry, &PyUnicode_Type, _str_shared) != 0) {
Eric Snow6d2cd902018-05-16 15:04:57 -04001694 Py_FatalError("could not register str for cross-interpreter sharing");
1695 }
Eric Snow7f8bfc92018-01-29 18:23:44 -07001696}
1697
1698
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001699#ifdef __cplusplus
1700}
1701#endif