blob: 2f80aa253b5aae754a16a475084c4fbdfaceba5f [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 Stinnercab5d072019-05-17 19:01:14 +020052 _PyPreConfig_Init(&runtime->preconfig);
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 Stinnerccb04422017-11-16 03:20:31 -0800109 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600110}
111
Eric Snow8479a342019-03-08 23:44:33 -0700112/* This function is called from PyOS_AfterFork_Child to ensure that
113 * newly created child processes do not share locks with the parent.
114 */
115
116void
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200117_PyRuntimeState_ReInitThreads(_PyRuntimeState *runtime)
Eric Snow8479a342019-03-08 23:44:33 -0700118{
119 // This was initially set in _PyRuntimeState_Init().
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200120 runtime->main_thread = PyThread_get_thread_ident();
Eric Snow8479a342019-03-08 23:44:33 -0700121
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200122 /* Force default allocator, since _PyRuntimeState_Fini() must
123 use the same allocator than this function. */
124 PyMemAllocatorEx old_alloc;
125 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
126
127 runtime->interpreters.mutex = PyThread_allocate_lock();
128 runtime->interpreters.main->id_mutex = PyThread_allocate_lock();
129 runtime->xidregistry.mutex = PyThread_allocate_lock();
130
131 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
132
133 if (runtime->interpreters.mutex == NULL) {
Eric Snow8479a342019-03-08 23:44:33 -0700134 Py_FatalError("Can't initialize lock for runtime interpreters");
135 }
136
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200137 if (runtime->interpreters.main->id_mutex == NULL) {
Eric Snow8479a342019-03-08 23:44:33 -0700138 Py_FatalError("Can't initialize ID lock for main interpreter");
139 }
140
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200141 if (runtime->xidregistry.mutex == NULL) {
Eric Snow8479a342019-03-08 23:44:33 -0700142 Py_FatalError("Can't initialize lock for cross-interpreter data registry");
143 }
144}
145
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200146#define HEAD_LOCK(runtime) \
147 PyThread_acquire_lock((runtime)->interpreters.mutex, WAIT_LOCK)
148#define HEAD_UNLOCK(runtime) \
149 PyThread_release_lock((runtime)->interpreters.mutex)
Eric Snow05351c12017-09-05 21:43:08 -0700150
Victor Stinner8bb32302019-04-24 16:47:40 +0200151/* Forward declaration */
152static void _PyGILState_NoteThreadState(
153 struct _gilstate_runtime_state *gilstate, PyThreadState* tstate);
Michael W. Hudson188d4362005-06-20 16:52:57 +0000154
Victor Stinnera7368ac2017-11-15 18:11:45 -0800155_PyInitError
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600156_PyInterpreterState_Enable(_PyRuntimeState *runtime)
Eric Snowe3774162017-05-22 19:46:40 -0700157{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200158 struct pyinterpreters *interpreters = &runtime->interpreters;
159 interpreters->next_id = 0;
Victor Stinner5d926472018-03-06 14:31:37 +0100160
161 /* Py_Finalize() calls _PyRuntimeState_Fini() which clears the mutex.
162 Create a new mutex if needed. */
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200163 if (interpreters->mutex == NULL) {
Victor Stinner5d926472018-03-06 14:31:37 +0100164 /* Force default allocator, since _PyRuntimeState_Fini() must
165 use the same allocator than this function. */
166 PyMemAllocatorEx old_alloc;
167 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
168
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200169 interpreters->mutex = PyThread_allocate_lock();
Victor Stinner5d926472018-03-06 14:31:37 +0100170
171 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
172
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200173 if (interpreters->mutex == NULL) {
Victor Stinnera7368ac2017-11-15 18:11:45 -0800174 return _Py_INIT_ERR("Can't initialize threads for interpreter");
175 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600176 }
Victor Stinner5d926472018-03-06 14:31:37 +0100177
Victor Stinnera7368ac2017-11-15 18:11:45 -0800178 return _Py_INIT_OK();
Eric Snowe3774162017-05-22 19:46:40 -0700179}
Guido van Rossuma027efa1997-05-05 20:56:21 +0000180
181PyInterpreterState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000182PyInterpreterState_New(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000183{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200184 PyInterpreterState *interp = PyMem_RawMalloc(sizeof(PyInterpreterState));
Victor Stinnerd4341102017-11-23 00:12:09 +0100185 if (interp == NULL) {
186 return NULL;
187 }
188
Eric Snow5be45a62019-03-08 22:47:07 -0700189 memset(interp, 0, sizeof(*interp));
Eric Snow4c6955e2018-02-16 18:53:40 -0700190 interp->id_refcount = -1;
Victor Stinnerd4341102017-11-23 00:12:09 +0100191 interp->check_interval = 100;
Victor Stinnercab5d072019-05-17 19:01:14 +0200192 _PyCoreConfig_Init(&interp->core_config);
Victor Stinnerd4341102017-11-23 00:12:09 +0100193 interp->eval_frame = _PyEval_EvalFrameDefault;
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000194#ifdef HAVE_DLOPEN
Serhiy Storchakac2f7d872016-05-04 09:44:44 +0300195#if HAVE_DECL_RTLD_NOW
Victor Stinnerd4341102017-11-23 00:12:09 +0100196 interp->dlopenflags = RTLD_NOW;
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000197#else
Victor Stinnerd4341102017-11-23 00:12:09 +0100198 interp->dlopenflags = RTLD_LAZY;
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000199#endif
200#endif
Guido van Rossum25ce5661997-08-02 03:10:38 +0000201
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200202 _PyRuntimeState *runtime = &_PyRuntime;
203 struct pyinterpreters *interpreters = &runtime->interpreters;
204
205 HEAD_LOCK(runtime);
206 if (interpreters->next_id < 0) {
Victor Stinnerd4341102017-11-23 00:12:09 +0100207 /* overflow or Py_Initialize() not called! */
208 PyErr_SetString(PyExc_RuntimeError,
209 "failed to get an interpreter ID");
Pablo Galindo95d630e2018-08-31 22:49:29 +0100210 PyMem_RawFree(interp);
Victor Stinnerd4341102017-11-23 00:12:09 +0100211 interp = NULL;
Victor Stinnerd4341102017-11-23 00:12:09 +0100212 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200213 else {
214 interp->id = interpreters->next_id;
215 interpreters->next_id += 1;
216 interp->next = interpreters->head;
217 if (interpreters->main == NULL) {
218 interpreters->main = interp;
219 }
220 interpreters->head = interp;
221 }
222 HEAD_UNLOCK(runtime);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000223
Pablo Galindo95d630e2018-08-31 22:49:29 +0100224 if (interp == NULL) {
225 return NULL;
226 }
227
Yury Selivanovf23746a2018-01-22 19:11:18 -0500228 interp->tstate_next_unique_id = 0;
229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 return interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000231}
232
233
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200234static void
235_PyInterpreterState_Clear(_PyRuntimeState *runtime, PyInterpreterState *interp)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000236{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200237 HEAD_LOCK(runtime);
238 for (PyThreadState *p = interp->tstate_head; p != NULL; p = p->next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 PyThreadState_Clear(p);
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200240 }
241 HEAD_UNLOCK(runtime);
Victor Stinnerda273412017-12-15 01:46:02 +0100242 _PyCoreConfig_Clear(&interp->core_config);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 Py_CLEAR(interp->codec_search_path);
244 Py_CLEAR(interp->codec_search_cache);
245 Py_CLEAR(interp->codec_error_registry);
Eric Snow93c92f72017-09-13 23:46:04 -0700246 Py_CLEAR(interp->modules);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 Py_CLEAR(interp->modules_by_index);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 Py_CLEAR(interp->sysdict);
249 Py_CLEAR(interp->builtins);
Serhiy Storchaka87a5c512014-02-10 18:21:34 +0200250 Py_CLEAR(interp->builtins_copy);
Brett Cannonfd074152012-04-14 14:10:13 -0400251 Py_CLEAR(interp->importlib);
Serhiy Storchaka133138a2016-08-02 22:51:21 +0300252 Py_CLEAR(interp->import_func);
Eric Snowd2fdd1f2019-03-15 17:47:43 -0600253 Py_CLEAR(interp->dict);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200254#ifdef HAVE_FORK
255 Py_CLEAR(interp->before_forkers);
256 Py_CLEAR(interp->after_forkers_parent);
257 Py_CLEAR(interp->after_forkers_child);
258#endif
Eric Snow86ea5812019-05-10 13:29:55 -0400259 if (runtime->finalizing == NULL) {
260 _PyWarnings_Fini(interp);
261 }
Eric Snow5be45a62019-03-08 22:47:07 -0700262 // XXX Once we have one allocator per interpreter (i.e.
263 // per-interpreter GC) we must ensure that all of the interpreter's
264 // objects have been cleaned up at the point.
Guido van Rossum25ce5661997-08-02 03:10:38 +0000265}
266
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200267void
268PyInterpreterState_Clear(PyInterpreterState *interp)
269{
270 _PyInterpreterState_Clear(&_PyRuntime, interp);
271}
272
Guido van Rossum25ce5661997-08-02 03:10:38 +0000273
274static void
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200275zapthreads(_PyRuntimeState *runtime, PyInterpreterState *interp)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000276{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 PyThreadState *p;
278 /* No need to lock the mutex here because this should only happen
279 when the threads are all really dead (XXX famous last words). */
280 while ((p = interp->tstate_head) != NULL) {
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200281 _PyThreadState_Delete(runtime, p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000283}
284
285
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200286static void
287_PyInterpreterState_Delete(_PyRuntimeState *runtime,
288 PyInterpreterState *interp)
289{
290 struct pyinterpreters *interpreters = &runtime->interpreters;
291 zapthreads(runtime, interp);
292 HEAD_LOCK(runtime);
293 PyInterpreterState **p;
294 for (p = &interpreters->head; ; p = &(*p)->next) {
295 if (*p == NULL) {
296 Py_FatalError("PyInterpreterState_Delete: invalid interp");
297 }
298 if (*p == interp) {
299 break;
300 }
301 }
302 if (interp->tstate_head != NULL) {
303 Py_FatalError("PyInterpreterState_Delete: remaining threads");
304 }
305 *p = interp->next;
306 if (interpreters->main == interp) {
307 interpreters->main = NULL;
308 if (interpreters->head != NULL) {
309 Py_FatalError("PyInterpreterState_Delete: remaining subinterpreters");
310 }
311 }
312 HEAD_UNLOCK(runtime);
313 if (interp->id_mutex != NULL) {
314 PyThread_free_lock(interp->id_mutex);
315 }
316 PyMem_RawFree(interp);
317}
318
319
Guido van Rossum25ce5661997-08-02 03:10:38 +0000320void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000321PyInterpreterState_Delete(PyInterpreterState *interp)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000322{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200323 _PyInterpreterState_Delete(&_PyRuntime, interp);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000324}
325
326
Eric Snow59032962018-09-14 14:17:20 -0700327/*
328 * Delete all interpreter states except the main interpreter. If there
329 * is a current interpreter state, it *must* be the main interpreter.
330 */
331void
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200332_PyInterpreterState_DeleteExceptMain(_PyRuntimeState *runtime)
Eric Snow59032962018-09-14 14:17:20 -0700333{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200334 struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200335 struct pyinterpreters *interpreters = &runtime->interpreters;
336
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200337 PyThreadState *tstate = _PyThreadState_Swap(gilstate, NULL);
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200338 if (tstate != NULL && tstate->interp != interpreters->main) {
Eric Snow59032962018-09-14 14:17:20 -0700339 Py_FatalError("PyInterpreterState_DeleteExceptMain: not main interpreter");
340 }
341
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200342 HEAD_LOCK(runtime);
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200343 PyInterpreterState *interp = interpreters->head;
344 interpreters->head = NULL;
Stéphane Wirtelb5409da2019-02-20 15:27:22 +0100345 while (interp != NULL) {
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200346 if (interp == interpreters->main) {
347 interpreters->main->next = NULL;
348 interpreters->head = interp;
Stéphane Wirtelb5409da2019-02-20 15:27:22 +0100349 interp = interp->next;
Eric Snow59032962018-09-14 14:17:20 -0700350 continue;
351 }
352
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200353 _PyInterpreterState_Clear(runtime, interp); // XXX must activate?
354 zapthreads(runtime, interp);
Eric Snow59032962018-09-14 14:17:20 -0700355 if (interp->id_mutex != NULL) {
356 PyThread_free_lock(interp->id_mutex);
357 }
Stéphane Wirtelb5409da2019-02-20 15:27:22 +0100358 PyInterpreterState *prev_interp = interp;
359 interp = interp->next;
360 PyMem_RawFree(prev_interp);
Eric Snow59032962018-09-14 14:17:20 -0700361 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200362 HEAD_UNLOCK(runtime);
Eric Snow59032962018-09-14 14:17:20 -0700363
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200364 if (interpreters->head == NULL) {
Eric Snow59032962018-09-14 14:17:20 -0700365 Py_FatalError("PyInterpreterState_DeleteExceptMain: missing main");
366 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200367 _PyThreadState_Swap(gilstate, tstate);
Eric Snow59032962018-09-14 14:17:20 -0700368}
369
370
Victor Stinnercaba55b2018-08-03 15:33:52 +0200371PyInterpreterState *
372_PyInterpreterState_Get(void)
373{
Victor Stinner50b48572018-11-01 01:51:40 +0100374 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnercaba55b2018-08-03 15:33:52 +0200375 if (tstate == NULL) {
376 Py_FatalError("_PyInterpreterState_Get(): no current thread state");
377 }
378 PyInterpreterState *interp = tstate->interp;
379 if (interp == NULL) {
380 Py_FatalError("_PyInterpreterState_Get(): no current interpreter");
381 }
382 return interp;
383}
384
385
Eric Snowe3774162017-05-22 19:46:40 -0700386int64_t
387PyInterpreterState_GetID(PyInterpreterState *interp)
388{
389 if (interp == NULL) {
390 PyErr_SetString(PyExc_RuntimeError, "no interpreter provided");
391 return -1;
392 }
393 return interp->id;
394}
395
396
Eric Snow5be45a62019-03-08 22:47:07 -0700397static PyInterpreterState *
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200398interp_look_up_id(_PyRuntimeState *runtime, PY_INT64_T requested_id)
Eric Snowb05b7112019-03-01 12:35:10 -0700399{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200400 PyInterpreterState *interp = runtime->interpreters.head;
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100401 while (interp != NULL) {
402 PY_INT64_T id = PyInterpreterState_GetID(interp);
Eric Snow5be45a62019-03-08 22:47:07 -0700403 if (id < 0) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100404 return NULL;
Eric Snow5be45a62019-03-08 22:47:07 -0700405 }
406 if (requested_id == id) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100407 return interp;
Eric Snow5be45a62019-03-08 22:47:07 -0700408 }
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100409 interp = PyInterpreterState_Next(interp);
Eric Snowb05b7112019-03-01 12:35:10 -0700410 }
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100411 return NULL;
Eric Snowb05b7112019-03-01 12:35:10 -0700412}
413
Eric Snow5be45a62019-03-08 22:47:07 -0700414PyInterpreterState *
415_PyInterpreterState_LookUpID(PY_INT64_T requested_id)
416{
417 PyInterpreterState *interp = NULL;
418 if (requested_id >= 0) {
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200419 _PyRuntimeState *runtime = &_PyRuntime;
420 HEAD_LOCK(runtime);
421 interp = interp_look_up_id(runtime, requested_id);
422 HEAD_UNLOCK(runtime);
Eric Snow5be45a62019-03-08 22:47:07 -0700423 }
424 if (interp == NULL && !PyErr_Occurred()) {
425 PyErr_Format(PyExc_RuntimeError,
426 "unrecognized interpreter ID %lld", requested_id);
427 }
428 return interp;
429}
430
Eric Snow4c6955e2018-02-16 18:53:40 -0700431
432int
433_PyInterpreterState_IDInitref(PyInterpreterState *interp)
434{
435 if (interp->id_mutex != NULL) {
436 return 0;
437 }
438 interp->id_mutex = PyThread_allocate_lock();
439 if (interp->id_mutex == NULL) {
440 PyErr_SetString(PyExc_RuntimeError,
441 "failed to create init interpreter ID mutex");
442 return -1;
443 }
444 interp->id_refcount = 0;
445 return 0;
446}
447
448
449void
450_PyInterpreterState_IDIncref(PyInterpreterState *interp)
451{
452 if (interp->id_mutex == NULL) {
453 return;
454 }
455 PyThread_acquire_lock(interp->id_mutex, WAIT_LOCK);
456 interp->id_refcount += 1;
457 PyThread_release_lock(interp->id_mutex);
458}
459
460
461void
462_PyInterpreterState_IDDecref(PyInterpreterState *interp)
463{
464 if (interp->id_mutex == NULL) {
465 return;
466 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200467 struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
Eric Snow4c6955e2018-02-16 18:53:40 -0700468 PyThread_acquire_lock(interp->id_mutex, WAIT_LOCK);
469 assert(interp->id_refcount != 0);
470 interp->id_refcount -= 1;
471 int64_t refcount = interp->id_refcount;
472 PyThread_release_lock(interp->id_mutex);
473
Eric Snowc11183c2019-03-15 16:35:46 -0600474 if (refcount == 0 && interp->requires_idref) {
Eric Snowf53d9f22018-02-20 16:30:17 -0700475 // XXX Using the "head" thread isn't strictly correct.
476 PyThreadState *tstate = PyInterpreterState_ThreadHead(interp);
477 // XXX Possible GILState issues?
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200478 PyThreadState *save_tstate = _PyThreadState_Swap(gilstate, tstate);
Eric Snow4c6955e2018-02-16 18:53:40 -0700479 Py_EndInterpreter(tstate);
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200480 _PyThreadState_Swap(gilstate, save_tstate);
Eric Snow4c6955e2018-02-16 18:53:40 -0700481 }
482}
483
Eric Snowc11183c2019-03-15 16:35:46 -0600484int
485_PyInterpreterState_RequiresIDRef(PyInterpreterState *interp)
486{
487 return interp->requires_idref;
488}
489
490void
491_PyInterpreterState_RequireIDRef(PyInterpreterState *interp, int required)
492{
493 interp->requires_idref = required ? 1 : 0;
494}
495
Eric Snowbe3b2952019-02-23 11:35:52 -0700496_PyCoreConfig *
497_PyInterpreterState_GetCoreConfig(PyInterpreterState *interp)
498{
499 return &interp->core_config;
500}
501
Eric Snowc11183c2019-03-15 16:35:46 -0600502PyObject *
503_PyInterpreterState_GetMainModule(PyInterpreterState *interp)
504{
505 if (interp->modules == NULL) {
506 PyErr_SetString(PyExc_RuntimeError, "interpreter not initialized");
507 return NULL;
508 }
509 return PyMapping_GetItemString(interp->modules, "__main__");
510}
511
Eric Snowd2fdd1f2019-03-15 17:47:43 -0600512PyObject *
513PyInterpreterState_GetDict(PyInterpreterState *interp)
514{
515 if (interp->dict == NULL) {
516 interp->dict = PyDict_New();
517 if (interp->dict == NULL) {
518 PyErr_Clear();
519 }
520 }
521 /* Returning NULL means no per-interpreter dict is available. */
522 return interp->dict;
523}
524
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000525/* Default implementation for _PyThreadState_GetFrame */
526static struct _frame *
527threadstate_getframe(PyThreadState *self)
528{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 return self->frame;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000530}
531
Victor Stinner45b9be52010-03-03 23:28:07 +0000532static PyThreadState *
533new_threadstate(PyInterpreterState *interp, int init)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000534{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200535 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner1a7425f2013-07-07 16:25:15 +0200536 PyThreadState *tstate = (PyThreadState *)PyMem_RawMalloc(sizeof(PyThreadState));
Victor Stinner8bb32302019-04-24 16:47:40 +0200537 if (tstate == NULL) {
538 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000540
Victor Stinner8bb32302019-04-24 16:47:40 +0200541 if (_PyThreadState_GetFrame == NULL) {
542 _PyThreadState_GetFrame = threadstate_getframe;
543 }
544
545 tstate->interp = interp;
546
547 tstate->frame = NULL;
548 tstate->recursion_depth = 0;
549 tstate->overflowed = 0;
550 tstate->recursion_critical = 0;
551 tstate->stackcheck_counter = 0;
552 tstate->tracing = 0;
553 tstate->use_tracing = 0;
554 tstate->gilstate_counter = 0;
555 tstate->async_exc = NULL;
556 tstate->thread_id = PyThread_get_thread_ident();
557
558 tstate->dict = NULL;
559
560 tstate->curexc_type = NULL;
561 tstate->curexc_value = NULL;
562 tstate->curexc_traceback = NULL;
563
564 tstate->exc_state.exc_type = NULL;
565 tstate->exc_state.exc_value = NULL;
566 tstate->exc_state.exc_traceback = NULL;
567 tstate->exc_state.previous_item = NULL;
568 tstate->exc_info = &tstate->exc_state;
569
570 tstate->c_profilefunc = NULL;
571 tstate->c_tracefunc = NULL;
572 tstate->c_profileobj = NULL;
573 tstate->c_traceobj = NULL;
574
575 tstate->trash_delete_nesting = 0;
576 tstate->trash_delete_later = NULL;
577 tstate->on_delete = NULL;
578 tstate->on_delete_data = NULL;
579
580 tstate->coroutine_origin_tracking_depth = 0;
581
582 tstate->coroutine_wrapper = NULL;
583 tstate->in_coroutine_wrapper = 0;
584
585 tstate->async_gen_firstiter = NULL;
586 tstate->async_gen_finalizer = NULL;
587
588 tstate->context = NULL;
589 tstate->context_ver = 1;
590
591 tstate->id = ++interp->tstate_next_unique_id;
592
593 if (init) {
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200594 _PyThreadState_Init(runtime, tstate);
Victor Stinner8bb32302019-04-24 16:47:40 +0200595 }
596
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200597 HEAD_LOCK(runtime);
Victor Stinner8bb32302019-04-24 16:47:40 +0200598 tstate->prev = NULL;
599 tstate->next = interp->tstate_head;
600 if (tstate->next)
601 tstate->next->prev = tstate;
602 interp->tstate_head = tstate;
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200603 HEAD_UNLOCK(runtime);
Victor Stinner8bb32302019-04-24 16:47:40 +0200604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 return tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000606}
607
Victor Stinner45b9be52010-03-03 23:28:07 +0000608PyThreadState *
609PyThreadState_New(PyInterpreterState *interp)
610{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611 return new_threadstate(interp, 1);
Victor Stinner45b9be52010-03-03 23:28:07 +0000612}
613
614PyThreadState *
615_PyThreadState_Prealloc(PyInterpreterState *interp)
616{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000617 return new_threadstate(interp, 0);
Victor Stinner45b9be52010-03-03 23:28:07 +0000618}
619
620void
Victor Stinner8bb32302019-04-24 16:47:40 +0200621_PyThreadState_Init(_PyRuntimeState *runtime, PyThreadState *tstate)
Victor Stinner45b9be52010-03-03 23:28:07 +0000622{
Victor Stinner8bb32302019-04-24 16:47:40 +0200623 _PyGILState_NoteThreadState(&runtime->gilstate, tstate);
Victor Stinner45b9be52010-03-03 23:28:07 +0000624}
625
Martin v. Löwis1a214512008-06-11 05:26:20 +0000626PyObject*
Martin v. Löwis7800f752012-06-22 12:20:55 +0200627PyState_FindModule(struct PyModuleDef* module)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000628{
Martin v. Löwis7800f752012-06-22 12:20:55 +0200629 Py_ssize_t index = module->m_base.m_index;
Victor Stinner9204fb82018-10-30 15:13:17 +0100630 PyInterpreterState *state = _PyInterpreterState_GET_UNSAFE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 PyObject *res;
Nick Coghland5cacbb2015-05-23 22:24:10 +1000632 if (module->m_slots) {
633 return NULL;
634 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 if (index == 0)
636 return NULL;
637 if (state->modules_by_index == NULL)
638 return NULL;
Antoine Pitrou75506e82012-08-20 19:30:46 +0200639 if (index >= PyList_GET_SIZE(state->modules_by_index))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 return NULL;
641 res = PyList_GET_ITEM(state->modules_by_index, index);
642 return res==Py_None ? NULL : res;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000643}
644
645int
646_PyState_AddModule(PyObject* module, struct PyModuleDef* def)
647{
Nick Coghland5cacbb2015-05-23 22:24:10 +1000648 PyInterpreterState *state;
Berker Peksag4b7b5652016-08-22 18:05:56 +0300649 if (!def) {
650 assert(PyErr_Occurred());
651 return -1;
652 }
Nick Coghland5cacbb2015-05-23 22:24:10 +1000653 if (def->m_slots) {
654 PyErr_SetString(PyExc_SystemError,
655 "PyState_AddModule called on module with slots");
656 return -1;
657 }
Victor Stinner9204fb82018-10-30 15:13:17 +0100658 state = _PyInterpreterState_GET_UNSAFE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 if (!state->modules_by_index) {
660 state->modules_by_index = PyList_New(0);
661 if (!state->modules_by_index)
662 return -1;
663 }
664 while(PyList_GET_SIZE(state->modules_by_index) <= def->m_base.m_index)
665 if (PyList_Append(state->modules_by_index, Py_None) < 0)
666 return -1;
667 Py_INCREF(module);
668 return PyList_SetItem(state->modules_by_index,
669 def->m_base.m_index, module);
Martin v. Löwis1a214512008-06-11 05:26:20 +0000670}
Guido van Rossuma027efa1997-05-05 20:56:21 +0000671
Martin v. Löwis7800f752012-06-22 12:20:55 +0200672int
673PyState_AddModule(PyObject* module, struct PyModuleDef* def)
674{
675 Py_ssize_t index;
Victor Stinner9204fb82018-10-30 15:13:17 +0100676 PyInterpreterState *state = _PyInterpreterState_GET_UNSAFE();
Martin v. Löwis7800f752012-06-22 12:20:55 +0200677 if (!def) {
678 Py_FatalError("PyState_AddModule: Module Definition is NULL");
679 return -1;
680 }
681 index = def->m_base.m_index;
682 if (state->modules_by_index) {
683 if(PyList_GET_SIZE(state->modules_by_index) >= index) {
684 if(module == PyList_GET_ITEM(state->modules_by_index, index)) {
685 Py_FatalError("PyState_AddModule: Module already added!");
686 return -1;
687 }
688 }
689 }
690 return _PyState_AddModule(module, def);
691}
692
693int
694PyState_RemoveModule(struct PyModuleDef* def)
695{
Nick Coghland5cacbb2015-05-23 22:24:10 +1000696 PyInterpreterState *state;
Martin v. Löwis7800f752012-06-22 12:20:55 +0200697 Py_ssize_t index = def->m_base.m_index;
Nick Coghland5cacbb2015-05-23 22:24:10 +1000698 if (def->m_slots) {
699 PyErr_SetString(PyExc_SystemError,
700 "PyState_RemoveModule called on module with slots");
701 return -1;
702 }
Victor Stinner9204fb82018-10-30 15:13:17 +0100703 state = _PyInterpreterState_GET_UNSAFE();
Martin v. Löwis7800f752012-06-22 12:20:55 +0200704 if (index == 0) {
705 Py_FatalError("PyState_RemoveModule: Module index invalid.");
706 return -1;
707 }
708 if (state->modules_by_index == NULL) {
709 Py_FatalError("PyState_RemoveModule: Interpreters module-list not acessible.");
710 return -1;
711 }
712 if (index > PyList_GET_SIZE(state->modules_by_index)) {
713 Py_FatalError("PyState_RemoveModule: Module index out of bounds.");
714 return -1;
715 }
Zackery Spytz2a893432018-12-05 00:14:00 -0700716 Py_INCREF(Py_None);
Martin v. Löwis7800f752012-06-22 12:20:55 +0200717 return PyList_SetItem(state->modules_by_index, index, Py_None);
718}
719
Antoine Pitrou40322e62013-08-11 00:30:09 +0200720/* used by import.c:PyImport_Cleanup */
721void
722_PyState_ClearModules(void)
723{
Victor Stinner9204fb82018-10-30 15:13:17 +0100724 PyInterpreterState *state = _PyInterpreterState_GET_UNSAFE();
Antoine Pitrou40322e62013-08-11 00:30:09 +0200725 if (state->modules_by_index) {
726 Py_ssize_t i;
727 for (i = 0; i < PyList_GET_SIZE(state->modules_by_index); i++) {
728 PyObject *m = PyList_GET_ITEM(state->modules_by_index, i);
729 if (PyModule_Check(m)) {
730 /* cleanup the saved copy of module dicts */
731 PyModuleDef *md = PyModule_GetDef(m);
732 if (md)
733 Py_CLEAR(md->m_base.m_copy);
734 }
735 }
736 /* Setting modules_by_index to NULL could be dangerous, so we
737 clear the list instead. */
738 if (PyList_SetSlice(state->modules_by_index,
739 0, PyList_GET_SIZE(state->modules_by_index),
740 NULL))
741 PyErr_WriteUnraisable(state->modules_by_index);
742 }
743}
744
Guido van Rossuma027efa1997-05-05 20:56:21 +0000745void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000746PyThreadState_Clear(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000747{
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200748 int verbose = tstate->interp->core_config.verbose;
749
750 if (verbose && tstate->frame != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000751 fprintf(stderr,
752 "PyThreadState_Clear: warning: thread still has a frame\n");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000753
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 Py_CLEAR(tstate->frame);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 Py_CLEAR(tstate->dict);
757 Py_CLEAR(tstate->async_exc);
Guido van Rossumede04391998-04-10 20:18:25 +0000758
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 Py_CLEAR(tstate->curexc_type);
760 Py_CLEAR(tstate->curexc_value);
761 Py_CLEAR(tstate->curexc_traceback);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000762
Mark Shannonae3087c2017-10-22 22:41:51 +0100763 Py_CLEAR(tstate->exc_state.exc_type);
764 Py_CLEAR(tstate->exc_state.exc_value);
765 Py_CLEAR(tstate->exc_state.exc_traceback);
Serhiy Storchakabdf42982017-10-26 16:59:40 +0300766
Mark Shannonae3087c2017-10-22 22:41:51 +0100767 /* The stack of exception states should contain just this thread. */
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200768 if (verbose && tstate->exc_info != &tstate->exc_state) {
Mark Shannonae3087c2017-10-22 22:41:51 +0100769 fprintf(stderr,
770 "PyThreadState_Clear: warning: thread still has a generator\n");
771 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000772
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 tstate->c_profilefunc = NULL;
774 tstate->c_tracefunc = NULL;
775 Py_CLEAR(tstate->c_profileobj);
776 Py_CLEAR(tstate->c_traceobj);
Yury Selivanov75445082015-05-11 22:57:16 -0400777
778 Py_CLEAR(tstate->coroutine_wrapper);
Yury Selivanoveb636452016-09-08 22:01:51 -0700779 Py_CLEAR(tstate->async_gen_firstiter);
780 Py_CLEAR(tstate->async_gen_finalizer);
Yury Selivanovf23746a2018-01-22 19:11:18 -0500781
782 Py_CLEAR(tstate->context);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000783}
784
785
Guido van Rossum29757862001-01-23 01:46:06 +0000786/* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
787static void
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200788tstate_delete_common(_PyRuntimeState *runtime, PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000789{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200790 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 Py_FatalError("PyThreadState_Delete: NULL tstate");
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200792 }
793 PyInterpreterState *interp = tstate->interp;
794 if (interp == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 Py_FatalError("PyThreadState_Delete: NULL interp");
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200796 }
797 HEAD_LOCK(runtime);
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200798 if (tstate->prev)
799 tstate->prev->next = tstate->next;
800 else
801 interp->tstate_head = tstate->next;
802 if (tstate->next)
803 tstate->next->prev = tstate->prev;
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200804 HEAD_UNLOCK(runtime);
Antoine Pitrou7b476992013-09-07 23:38:37 +0200805 if (tstate->on_delete != NULL) {
806 tstate->on_delete(tstate->on_delete_data);
807 }
Victor Stinner1a7425f2013-07-07 16:25:15 +0200808 PyMem_RawFree(tstate);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000809}
810
811
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200812static void
813_PyThreadState_Delete(_PyRuntimeState *runtime, PyThreadState *tstate)
Guido van Rossum29757862001-01-23 01:46:06 +0000814{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200815 struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
816 if (tstate == _PyRuntimeGILState_GetThreadState(gilstate)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 Py_FatalError("PyThreadState_Delete: tstate is still current");
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600818 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200819 if (gilstate->autoInterpreterState &&
820 PyThread_tss_get(&gilstate->autoTSSkey) == tstate)
821 {
822 PyThread_tss_set(&gilstate->autoTSSkey, NULL);
823 }
824 tstate_delete_common(runtime, tstate);
Guido van Rossum29757862001-01-23 01:46:06 +0000825}
826
827
Guido van Rossum29757862001-01-23 01:46:06 +0000828void
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200829PyThreadState_Delete(PyThreadState *tstate)
Guido van Rossum29757862001-01-23 01:46:06 +0000830{
Victor Stinner99e69d42019-04-26 05:48:51 +0200831 _PyThreadState_Delete(&_PyRuntime, tstate);
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200832}
833
834
835static void
836_PyThreadState_DeleteCurrent(_PyRuntimeState *runtime)
837{
838 struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
839 PyThreadState *tstate = _PyRuntimeGILState_GetThreadState(gilstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 if (tstate == NULL)
841 Py_FatalError(
842 "PyThreadState_DeleteCurrent: no current tstate");
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200843 tstate_delete_common(runtime, tstate);
844 if (gilstate->autoInterpreterState &&
845 PyThread_tss_get(&gilstate->autoTSSkey) == tstate)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600846 {
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200847 PyThread_tss_set(&gilstate->autoTSSkey, NULL);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600848 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200849 _PyRuntimeGILState_SetThreadState(gilstate, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 PyEval_ReleaseLock();
Guido van Rossum29757862001-01-23 01:46:06 +0000851}
Guido van Rossum29757862001-01-23 01:46:06 +0000852
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200853void
854PyThreadState_DeleteCurrent()
855{
856 _PyThreadState_DeleteCurrent(&_PyRuntime);
857}
858
Guido van Rossum29757862001-01-23 01:46:06 +0000859
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200860/*
861 * Delete all thread states except the one passed as argument.
862 * Note that, if there is a current thread state, it *must* be the one
863 * passed as argument. Also, this won't touch any other interpreters
864 * than the current one, since we don't know which thread state should
865 * be kept in those other interpreteres.
866 */
867void
Victor Stinner09532fe2019-05-10 23:39:09 +0200868_PyThreadState_DeleteExcept(_PyRuntimeState *runtime, PyThreadState *tstate)
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200869{
870 PyInterpreterState *interp = tstate->interp;
871 PyThreadState *p, *next, *garbage;
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200872 HEAD_LOCK(runtime);
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200873 /* Remove all thread states, except tstate, from the linked list of
874 thread states. This will allow calling PyThreadState_Clear()
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200875 without holding the lock. */
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200876 garbage = interp->tstate_head;
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200877 if (garbage == tstate)
878 garbage = tstate->next;
879 if (tstate->prev)
880 tstate->prev->next = tstate->next;
881 if (tstate->next)
882 tstate->next->prev = tstate->prev;
883 tstate->prev = tstate->next = NULL;
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200884 interp->tstate_head = tstate;
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200885 HEAD_UNLOCK(runtime);
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200886 /* Clear and deallocate all stale thread states. Even if this
887 executes Python code, we should be safe since it executes
888 in the current thread, not one of the stale threads. */
889 for (p = garbage; p; p = next) {
890 next = p->next;
891 PyThreadState_Clear(p);
Victor Stinner1a7425f2013-07-07 16:25:15 +0200892 PyMem_RawFree(p);
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200893 }
894}
895
896
Guido van Rossuma027efa1997-05-05 20:56:21 +0000897PyThreadState *
Victor Stinnerbfd316e2016-01-20 11:12:38 +0100898_PyThreadState_UncheckedGet(void)
899{
Victor Stinner50b48572018-11-01 01:51:40 +0100900 return _PyThreadState_GET();
Victor Stinnerbfd316e2016-01-20 11:12:38 +0100901}
902
903
904PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000905PyThreadState_Get(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000906{
Victor Stinner50b48572018-11-01 01:51:40 +0100907 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 if (tstate == NULL)
909 Py_FatalError("PyThreadState_Get: no current thread");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000910
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 return tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000912}
913
914
Victor Stinner09532fe2019-05-10 23:39:09 +0200915PyThreadState *
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200916_PyThreadState_Swap(struct _gilstate_runtime_state *gilstate, PyThreadState *newts)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000917{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200918 PyThreadState *oldts = _PyRuntimeGILState_GetThreadState(gilstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000919
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200920 _PyRuntimeGILState_SetThreadState(gilstate, newts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 /* It should not be possible for more than one thread state
922 to be used for a thread. Check this the best we can in debug
923 builds.
924 */
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200925#if defined(Py_DEBUG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 if (newts) {
927 /* This can be called from PyEval_RestoreThread(). Similar
928 to it, we need to ensure errno doesn't change.
929 */
930 int err = errno;
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200931 PyThreadState *check = _PyGILState_GetThisThreadState(gilstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 if (check && check->interp == newts->interp && check != newts)
933 Py_FatalError("Invalid thread state for this thread");
934 errno = err;
935 }
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000936#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 return oldts;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000938}
Guido van Rossumede04391998-04-10 20:18:25 +0000939
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200940PyThreadState *
941PyThreadState_Swap(PyThreadState *newts)
942{
943 return _PyThreadState_Swap(&_PyRuntime.gilstate, newts);
944}
945
Guido van Rossumede04391998-04-10 20:18:25 +0000946/* An extension mechanism to store arbitrary additional per-thread state.
947 PyThreadState_GetDict() returns a dictionary that can be used to hold such
948 state; the caller should pick a unique key and store its state there. If
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000949 PyThreadState_GetDict() returns NULL, an exception has *not* been raised
950 and the caller should assume no per-thread state is available. */
Guido van Rossumede04391998-04-10 20:18:25 +0000951
952PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000953PyThreadState_GetDict(void)
Guido van Rossumede04391998-04-10 20:18:25 +0000954{
Victor Stinner50b48572018-11-01 01:51:40 +0100955 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 if (tstate == NULL)
957 return NULL;
Guido van Rossumede04391998-04-10 20:18:25 +0000958
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 if (tstate->dict == NULL) {
960 PyObject *d;
961 tstate->dict = d = PyDict_New();
962 if (d == NULL)
963 PyErr_Clear();
964 }
965 return tstate->dict;
Guido van Rossumede04391998-04-10 20:18:25 +0000966}
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000967
968
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000969/* Asynchronously raise an exception in a thread.
970 Requested by Just van Rossum and Alex Martelli.
Guido van Rossum0f1f63c2005-02-08 02:07:57 +0000971 To prevent naive misuse, you must write your own extension
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000972 to call this, or use ctypes. Must be called with the GIL held.
973 Returns the number of tstates modified (normally 1, but 0 if `id` didn't
974 match any known thread id). Can be called with exc=NULL to clear an
975 existing async exception. This raises no exceptions. */
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000976
977int
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200978PyThreadState_SetAsyncExc(unsigned long id, PyObject *exc)
979{
Victor Stinner09532fe2019-05-10 23:39:09 +0200980 _PyRuntimeState *runtime = &_PyRuntime;
981 PyInterpreterState *interp = _PyRuntimeState_GetThreadState(runtime)->interp;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000982
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000983 /* Although the GIL is held, a few C API functions can be called
984 * without the GIL held, and in particular some that create and
985 * destroy thread and interpreter states. Those can mutate the
986 * list of thread states we're traversing, so to prevent that we lock
987 * head_mutex for the duration.
988 */
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200989 HEAD_LOCK(runtime);
Victor Stinner09532fe2019-05-10 23:39:09 +0200990 for (PyThreadState *p = interp->tstate_head; p != NULL; p = p->next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 if (p->thread_id == id) {
992 /* Tricky: we need to decref the current value
993 * (if any) in p->async_exc, but that can in turn
994 * allow arbitrary Python code to run, including
995 * perhaps calls to this function. To prevent
996 * deadlock, we need to release head_mutex before
997 * the decref.
998 */
999 PyObject *old_exc = p->async_exc;
1000 Py_XINCREF(exc);
1001 p->async_exc = exc;
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001002 HEAD_UNLOCK(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 Py_XDECREF(old_exc);
Victor Stinner09532fe2019-05-10 23:39:09 +02001004 _PyEval_SignalAsyncExc(&runtime->ceval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 return 1;
1006 }
1007 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001008 HEAD_UNLOCK(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 return 0;
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +00001010}
1011
1012
Guido van Rossumf5df46d2001-07-19 12:19:27 +00001013/* Routines for advanced debuggers, requested by David Beazley.
1014 Don't use unless you know what you are doing! */
1015
1016PyInterpreterState *
1017PyInterpreterState_Head(void)
1018{
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001019 return _PyRuntime.interpreters.head;
Guido van Rossumf5df46d2001-07-19 12:19:27 +00001020}
1021
1022PyInterpreterState *
Eric Snow6b4be192017-05-22 21:36:03 -07001023PyInterpreterState_Main(void)
1024{
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001025 return _PyRuntime.interpreters.main;
Eric Snow6b4be192017-05-22 21:36:03 -07001026}
1027
1028PyInterpreterState *
Guido van Rossumf5df46d2001-07-19 12:19:27 +00001029PyInterpreterState_Next(PyInterpreterState *interp) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 return interp->next;
Guido van Rossumf5df46d2001-07-19 12:19:27 +00001031}
1032
1033PyThreadState *
1034PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 return interp->tstate_head;
Guido van Rossumf5df46d2001-07-19 12:19:27 +00001036}
1037
1038PyThreadState *
1039PyThreadState_Next(PyThreadState *tstate) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 return tstate->next;
Guido van Rossumf5df46d2001-07-19 12:19:27 +00001041}
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001042
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001043/* The implementation of sys._current_frames(). This is intended to be
1044 called with the GIL held, as it will be when called via
1045 sys._current_frames(). It's possible it would work fine even without
1046 the GIL held, but haven't thought enough about that.
1047*/
1048PyObject *
1049_PyThread_CurrentFrames(void)
1050{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 PyObject *result;
1052 PyInterpreterState *i;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001053
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 result = PyDict_New();
1055 if (result == NULL)
1056 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 /* for i in all interpreters:
1059 * for t in all of i's thread states:
1060 * if t's frame isn't NULL, map t's id to its frame
Ezio Melotti13925002011-03-16 11:05:33 +02001061 * Because these lists can mutate even when the GIL is held, we
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 * need to grab head_mutex for the duration.
1063 */
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001064 _PyRuntimeState *runtime = &_PyRuntime;
1065 HEAD_LOCK(runtime);
1066 for (i = runtime->interpreters.head; i != NULL; i = i->next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 PyThreadState *t;
1068 for (t = i->tstate_head; t != NULL; t = t->next) {
1069 PyObject *id;
1070 int stat;
1071 struct _frame *frame = t->frame;
1072 if (frame == NULL)
1073 continue;
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001074 id = PyLong_FromUnsignedLong(t->thread_id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075 if (id == NULL)
1076 goto Fail;
1077 stat = PyDict_SetItem(result, id, (PyObject *)frame);
1078 Py_DECREF(id);
1079 if (stat < 0)
1080 goto Fail;
1081 }
1082 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001083 HEAD_UNLOCK(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001085
1086 Fail:
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001087 HEAD_UNLOCK(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 Py_DECREF(result);
1089 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001090}
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +00001091
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001092/* Python "auto thread state" API. */
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001093
1094/* Keep this as a static, as it is not reliable! It can only
1095 ever be compared to the state for the *current* thread.
1096 * If not equal, then it doesn't matter that the actual
1097 value may change immediately after comparison, as it can't
1098 possibly change to the current thread's state.
1099 * If equal, then the current thread holds the lock, so the value can't
1100 change until we yield the lock.
1101*/
1102static int
1103PyThreadState_IsCurrent(PyThreadState *tstate)
1104{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 /* Must be the tstate for this thread */
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001106 struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
1107 assert(_PyGILState_GetThisThreadState(gilstate) == tstate);
1108 return tstate == _PyRuntimeGILState_GetThreadState(gilstate);
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001109}
1110
Tim Peters4c1f5ec2004-10-09 17:25:05 +00001111/* Internal initialization/finalization functions called by
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001112 Py_Initialize/Py_FinalizeEx
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001113*/
Tim Peters19717fa2004-10-09 17:38:29 +00001114void
Victor Stinner43125222019-04-24 18:23:53 +02001115_PyGILState_Init(_PyRuntimeState *runtime,
1116 PyInterpreterState *interp, PyThreadState *tstate)
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001117{
Victor Stinner8bb32302019-04-24 16:47:40 +02001118 /* must init with valid states */
1119 assert(interp != NULL);
1120 assert(tstate != NULL);
1121
Victor Stinner8bb32302019-04-24 16:47:40 +02001122 struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
1123
1124 if (PyThread_tss_create(&gilstate->autoTSSkey) != 0) {
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001125 Py_FatalError("Could not allocate TSS entry");
1126 }
Victor Stinner8bb32302019-04-24 16:47:40 +02001127 gilstate->autoInterpreterState = interp;
1128 assert(PyThread_tss_get(&gilstate->autoTSSkey) == NULL);
1129 assert(tstate->gilstate_counter == 0);
Michael W. Hudson188d4362005-06-20 16:52:57 +00001130
Victor Stinner8bb32302019-04-24 16:47:40 +02001131 _PyGILState_NoteThreadState(gilstate, tstate);
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001132}
1133
Victor Stinner861d9ab2016-03-16 22:45:24 +01001134PyInterpreterState *
1135_PyGILState_GetInterpreterStateUnsafe(void)
1136{
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001137 return _PyRuntime.gilstate.autoInterpreterState;
Victor Stinner861d9ab2016-03-16 22:45:24 +01001138}
1139
Tim Peters19717fa2004-10-09 17:38:29 +00001140void
Victor Stinner8e91c242019-04-24 17:24:01 +02001141_PyGILState_Fini(_PyRuntimeState *runtime)
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001142{
Victor Stinner8e91c242019-04-24 17:24:01 +02001143 struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
1144 PyThread_tss_delete(&gilstate->autoTSSkey);
1145 gilstate->autoInterpreterState = NULL;
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001146}
1147
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001148/* Reset the TSS key - called by PyOS_AfterFork_Child().
Antoine Pitrou0c759fe2011-04-27 19:28:05 +02001149 * This should not be necessary, but some - buggy - pthread implementations
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001150 * don't reset TSS upon fork(), see issue #10517.
Antoine Pitrou0c759fe2011-04-27 19:28:05 +02001151 */
1152void
Victor Stinnerb930a2d2019-04-24 17:14:33 +02001153_PyGILState_Reinit(_PyRuntimeState *runtime)
Antoine Pitrou0c759fe2011-04-27 19:28:05 +02001154{
Victor Stinnerb930a2d2019-04-24 17:14:33 +02001155 struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001156 PyThreadState *tstate = _PyGILState_GetThisThreadState(gilstate);
Victor Stinnerb930a2d2019-04-24 17:14:33 +02001157
1158 PyThread_tss_delete(&gilstate->autoTSSkey);
1159 if (PyThread_tss_create(&gilstate->autoTSSkey) != 0) {
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001160 Py_FatalError("Could not allocate TSS entry");
1161 }
Antoine Pitrou0c759fe2011-04-27 19:28:05 +02001162
Charles-François Natalia233df82011-11-22 19:49:51 +01001163 /* If the thread had an associated auto thread state, reassociate it with
1164 * the new key. */
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001165 if (tstate &&
Victor Stinnerb930a2d2019-04-24 17:14:33 +02001166 PyThread_tss_set(&gilstate->autoTSSkey, (void *)tstate) != 0)
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001167 {
1168 Py_FatalError("Couldn't create autoTSSkey mapping");
1169 }
Antoine Pitrou0c759fe2011-04-27 19:28:05 +02001170}
1171
Michael W. Hudson188d4362005-06-20 16:52:57 +00001172/* When a thread state is created for a thread by some mechanism other than
1173 PyGILState_Ensure, it's important that the GILState machinery knows about
1174 it so it doesn't try to create another thread state for the thread (this is
1175 a better fix for SF bug #1010677 than the first one attempted).
1176*/
Thomas Wouters89f507f2006-12-13 04:49:30 +00001177static void
Victor Stinner8bb32302019-04-24 16:47:40 +02001178_PyGILState_NoteThreadState(struct _gilstate_runtime_state *gilstate, PyThreadState* tstate)
Michael W. Hudson188d4362005-06-20 16:52:57 +00001179{
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001180 /* If autoTSSkey isn't initialized, this must be the very first
Antoine Pitrou079ce542010-09-08 12:37:10 +00001181 threadstate created in Py_Initialize(). Don't do anything for now
1182 (we'll be back here when _PyGILState_Init is called). */
Victor Stinner8bb32302019-04-24 16:47:40 +02001183 if (!gilstate->autoInterpreterState) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 return;
Victor Stinner8bb32302019-04-24 16:47:40 +02001185 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001186
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001187 /* Stick the thread state for this thread in thread specific storage.
Michael W. Hudson188d4362005-06-20 16:52:57 +00001188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 The only situation where you can legitimately have more than one
1190 thread state for an OS level thread is when there are multiple
Victor Stinner590cebe2013-12-13 11:08:56 +01001191 interpreters.
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001192
Victor Stinner590cebe2013-12-13 11:08:56 +01001193 You shouldn't really be using the PyGILState_ APIs anyway (see issues
1194 #10915 and #15751).
Michael W. Hudson188d4362005-06-20 16:52:57 +00001195
Victor Stinner590cebe2013-12-13 11:08:56 +01001196 The first thread state created for that given OS level thread will
1197 "win", which seems reasonable behaviour.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 */
Victor Stinner8bb32302019-04-24 16:47:40 +02001199 if (PyThread_tss_get(&gilstate->autoTSSkey) == NULL) {
1200 if ((PyThread_tss_set(&gilstate->autoTSSkey, (void *)tstate)) != 0) {
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001201 Py_FatalError("Couldn't create autoTSSkey mapping");
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001202 }
Victor Stinner590cebe2013-12-13 11:08:56 +01001203 }
Michael W. Hudson188d4362005-06-20 16:52:57 +00001204
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 /* PyGILState_Release must not try to delete this thread state. */
1206 tstate->gilstate_counter = 1;
Michael W. Hudson188d4362005-06-20 16:52:57 +00001207}
1208
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001209/* The public functions */
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001210static PyThreadState *
1211_PyGILState_GetThisThreadState(struct _gilstate_runtime_state *gilstate)
1212{
1213 if (gilstate->autoInterpreterState == NULL)
1214 return NULL;
1215 return (PyThreadState *)PyThread_tss_get(&gilstate->autoTSSkey);
1216}
1217
Tim Peters19717fa2004-10-09 17:38:29 +00001218PyThreadState *
1219PyGILState_GetThisThreadState(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001220{
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001221 return _PyGILState_GetThisThreadState(&_PyRuntime.gilstate);
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001222}
1223
Kristján Valur Jónsson684cd0e2013-03-23 03:36:16 -07001224int
1225PyGILState_Check(void)
1226{
Victor Stinner8a1be612016-03-14 22:07:55 +01001227
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001228 if (!_PyGILState_check_enabled) {
Victor Stinner8a1be612016-03-14 22:07:55 +01001229 return 1;
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001230 }
Victor Stinner8a1be612016-03-14 22:07:55 +01001231
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001232 struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
1233 if (!PyThread_tss_is_created(&gilstate->autoTSSkey)) {
1234 return 1;
1235 }
Victor Stinner8a1be612016-03-14 22:07:55 +01001236
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001237 PyThreadState *tstate = _PyRuntimeGILState_GetThreadState(gilstate);
1238 if (tstate == NULL) {
1239 return 0;
1240 }
1241
1242 return (tstate == _PyGILState_GetThisThreadState(gilstate));
Kristján Valur Jónsson684cd0e2013-03-23 03:36:16 -07001243}
1244
Tim Peters19717fa2004-10-09 17:38:29 +00001245PyGILState_STATE
1246PyGILState_Ensure(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001247{
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001248 struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 int current;
1250 PyThreadState *tcur;
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +01001251 int need_init_threads = 0;
1252
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 /* Note that we do not auto-init Python here - apart from
1254 potential races with 2 threads auto-initializing, pep-311
1255 spells out other issues. Embedders are expected to have
1256 called Py_Initialize() and usually PyEval_InitThreads().
1257 */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001258 /* Py_Initialize() hasn't been called! */
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001259 assert(gilstate->autoInterpreterState);
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +01001260
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001261 tcur = (PyThreadState *)PyThread_tss_get(&gilstate->autoTSSkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001262 if (tcur == NULL) {
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +01001263 need_init_threads = 1;
Victor Stinner62ca1002013-12-13 01:46:43 +01001264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 /* Create a new thread state for this thread */
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001266 tcur = PyThreadState_New(gilstate->autoInterpreterState);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 if (tcur == NULL)
1268 Py_FatalError("Couldn't create thread-state for new thread");
1269 /* This is our thread state! We'll need to delete it in the
1270 matching call to PyGILState_Release(). */
1271 tcur->gilstate_counter = 0;
1272 current = 0; /* new thread state is never current */
1273 }
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +01001274 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 current = PyThreadState_IsCurrent(tcur);
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +01001276 }
1277
1278 if (current == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 PyEval_RestoreThread(tcur);
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +01001280 }
1281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 /* Update our counter in the thread-state - no need for locks:
1283 - tcur will remain valid as we hold the GIL.
1284 - the counter is safe as we are the only thread "allowed"
1285 to modify this value
1286 */
1287 ++tcur->gilstate_counter;
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +01001288
1289 if (need_init_threads) {
1290 /* At startup, Python has no concrete GIL. If PyGILState_Ensure() is
1291 called from a new thread for the first time, we need the create the
1292 GIL. */
1293 PyEval_InitThreads();
1294 }
1295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001297}
1298
Tim Peters19717fa2004-10-09 17:38:29 +00001299void
1300PyGILState_Release(PyGILState_STATE oldstate)
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001301{
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001302 _PyRuntimeState *runtime = &_PyRuntime;
1303 PyThreadState *tcur = PyThread_tss_get(&runtime->gilstate.autoTSSkey);
1304 if (tcur == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 Py_FatalError("auto-releasing thread-state, "
1306 "but no thread-state for this thread");
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001307 }
1308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 /* We must hold the GIL and have our thread state current */
1310 /* XXX - remove the check - the assert should be fine,
1311 but while this is very new (April 2003), the extra check
1312 by release-only users can't hurt.
1313 */
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001314 if (!PyThreadState_IsCurrent(tcur)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 Py_FatalError("This thread state must be current when releasing");
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001316 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 assert(PyThreadState_IsCurrent(tcur));
1318 --tcur->gilstate_counter;
1319 assert(tcur->gilstate_counter >= 0); /* illegal counter value */
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 /* If we're going to destroy this thread-state, we must
1322 * clear it while the GIL is held, as destructors may run.
1323 */
1324 if (tcur->gilstate_counter == 0) {
1325 /* can't have been locked when we created it */
1326 assert(oldstate == PyGILState_UNLOCKED);
1327 PyThreadState_Clear(tcur);
1328 /* Delete the thread-state. Note this releases the GIL too!
1329 * It's vital that the GIL be held here, to avoid shutdown
1330 * races; see bugs 225673 and 1061968 (that nasty bug has a
1331 * habit of coming back).
1332 */
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001333 _PyThreadState_DeleteCurrent(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 }
1335 /* Release the lock if necessary */
1336 else if (oldstate == PyGILState_UNLOCKED)
1337 PyEval_SaveThread();
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001338}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001339
Benjamin Peterson3bf01752012-04-13 18:06:36 -04001340
Eric Snow7f8bfc92018-01-29 18:23:44 -07001341/**************************/
1342/* cross-interpreter data */
1343/**************************/
1344
1345/* cross-interpreter data */
1346
1347crossinterpdatafunc _PyCrossInterpreterData_Lookup(PyObject *);
1348
1349/* This is a separate func from _PyCrossInterpreterData_Lookup in order
1350 to keep the registry code separate. */
1351static crossinterpdatafunc
1352_lookup_getdata(PyObject *obj)
1353{
1354 crossinterpdatafunc getdata = _PyCrossInterpreterData_Lookup(obj);
1355 if (getdata == NULL && PyErr_Occurred() == 0)
1356 PyErr_Format(PyExc_ValueError,
1357 "%S does not support cross-interpreter data", obj);
1358 return getdata;
1359}
1360
1361int
1362_PyObject_CheckCrossInterpreterData(PyObject *obj)
1363{
1364 crossinterpdatafunc getdata = _lookup_getdata(obj);
1365 if (getdata == NULL) {
1366 return -1;
1367 }
1368 return 0;
1369}
1370
1371static int
1372_check_xidata(_PyCrossInterpreterData *data)
1373{
1374 // data->data can be anything, including NULL, so we don't check it.
1375
1376 // data->obj may be NULL, so we don't check it.
1377
1378 if (data->interp < 0) {
1379 PyErr_SetString(PyExc_SystemError, "missing interp");
1380 return -1;
1381 }
1382
1383 if (data->new_object == NULL) {
1384 PyErr_SetString(PyExc_SystemError, "missing new_object func");
1385 return -1;
1386 }
1387
1388 // data->free may be NULL, so we don't check it.
1389
1390 return 0;
1391}
1392
1393int
1394_PyObject_GetCrossInterpreterData(PyObject *obj, _PyCrossInterpreterData *data)
1395{
Victor Stinnercaba55b2018-08-03 15:33:52 +02001396 // _PyInterpreterState_Get() aborts if lookup fails, so we don't need
Eric Snow7f8bfc92018-01-29 18:23:44 -07001397 // to check the result for NULL.
Victor Stinnercaba55b2018-08-03 15:33:52 +02001398 PyInterpreterState *interp = _PyInterpreterState_Get();
Eric Snow7f8bfc92018-01-29 18:23:44 -07001399
1400 // Reset data before re-populating.
1401 *data = (_PyCrossInterpreterData){0};
1402 data->free = PyMem_RawFree; // Set a default that may be overridden.
1403
1404 // Call the "getdata" func for the object.
1405 Py_INCREF(obj);
1406 crossinterpdatafunc getdata = _lookup_getdata(obj);
1407 if (getdata == NULL) {
1408 Py_DECREF(obj);
1409 return -1;
1410 }
1411 int res = getdata(obj, data);
1412 Py_DECREF(obj);
1413 if (res != 0) {
1414 return -1;
1415 }
1416
1417 // Fill in the blanks and validate the result.
Eric Snow7f8bfc92018-01-29 18:23:44 -07001418 data->interp = interp->id;
1419 if (_check_xidata(data) != 0) {
1420 _PyCrossInterpreterData_Release(data);
1421 return -1;
1422 }
1423
1424 return 0;
1425}
1426
Eric Snowb75b1a352019-04-12 10:20:10 -06001427static void
Eric Snow63799132018-06-01 18:45:20 -06001428_release_xidata(void *arg)
1429{
1430 _PyCrossInterpreterData *data = (_PyCrossInterpreterData *)arg;
1431 if (data->free != NULL) {
1432 data->free(data->data);
1433 }
1434 Py_XDECREF(data->obj);
Eric Snowb75b1a352019-04-12 10:20:10 -06001435}
1436
1437static void
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001438_call_in_interpreter(struct _gilstate_runtime_state *gilstate,
1439 PyInterpreterState *interp,
Eric Snowb75b1a352019-04-12 10:20:10 -06001440 void (*func)(void *), void *arg)
1441{
1442 /* We would use Py_AddPendingCall() if it weren't specific to the
1443 * main interpreter (see bpo-33608). In the meantime we take a
1444 * naive approach.
1445 */
1446 PyThreadState *save_tstate = NULL;
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001447 if (interp != _PyRuntimeGILState_GetThreadState(gilstate)->interp) {
Eric Snowb75b1a352019-04-12 10:20:10 -06001448 // XXX Using the "head" thread isn't strictly correct.
1449 PyThreadState *tstate = PyInterpreterState_ThreadHead(interp);
1450 // XXX Possible GILState issues?
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001451 save_tstate = _PyThreadState_Swap(gilstate, tstate);
Eric Snowb75b1a352019-04-12 10:20:10 -06001452 }
1453
1454 func(arg);
1455
1456 // Switch back.
1457 if (save_tstate != NULL) {
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001458 _PyThreadState_Swap(gilstate, save_tstate);
Eric Snowb75b1a352019-04-12 10:20:10 -06001459 }
Eric Snow63799132018-06-01 18:45:20 -06001460}
1461
Eric Snow7f8bfc92018-01-29 18:23:44 -07001462void
1463_PyCrossInterpreterData_Release(_PyCrossInterpreterData *data)
1464{
1465 if (data->data == NULL && data->obj == NULL) {
1466 // Nothing to release!
1467 return;
1468 }
1469
Eric Snowb75b1a352019-04-12 10:20:10 -06001470 // Switch to the original interpreter.
Eric Snow7f8bfc92018-01-29 18:23:44 -07001471 PyInterpreterState *interp = _PyInterpreterState_LookUpID(data->interp);
1472 if (interp == NULL) {
1473 // The intepreter was already destroyed.
1474 if (data->free != NULL) {
1475 // XXX Someone leaked some memory...
1476 }
1477 return;
1478 }
Eric Snowf53d9f22018-02-20 16:30:17 -07001479
Eric Snow7f8bfc92018-01-29 18:23:44 -07001480 // "Release" the data and/or the object.
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001481 struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
1482 _call_in_interpreter(gilstate, interp, _release_xidata, data);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001483}
1484
1485PyObject *
1486_PyCrossInterpreterData_NewObject(_PyCrossInterpreterData *data)
1487{
1488 return data->new_object(data);
1489}
1490
1491/* registry of {type -> crossinterpdatafunc} */
1492
1493/* For now we use a global registry of shareable classes. An
1494 alternative would be to add a tp_* slot for a class's
1495 crossinterpdatafunc. It would be simpler and more efficient. */
1496
1497static int
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001498_register_xidata(struct _xidregistry *xidregistry, PyTypeObject *cls,
1499 crossinterpdatafunc getdata)
Eric Snow7f8bfc92018-01-29 18:23:44 -07001500{
1501 // Note that we effectively replace already registered classes
1502 // rather than failing.
1503 struct _xidregitem *newhead = PyMem_RawMalloc(sizeof(struct _xidregitem));
1504 if (newhead == NULL)
1505 return -1;
1506 newhead->cls = cls;
1507 newhead->getdata = getdata;
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001508 newhead->next = xidregistry->head;
1509 xidregistry->head = newhead;
Eric Snow7f8bfc92018-01-29 18:23:44 -07001510 return 0;
1511}
1512
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001513static void _register_builtins_for_crossinterpreter_data(struct _xidregistry *xidregistry);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001514
1515int
Eric Snowc11183c2019-03-15 16:35:46 -06001516_PyCrossInterpreterData_RegisterClass(PyTypeObject *cls,
Eric Snow7f8bfc92018-01-29 18:23:44 -07001517 crossinterpdatafunc getdata)
1518{
1519 if (!PyType_Check(cls)) {
1520 PyErr_Format(PyExc_ValueError, "only classes may be registered");
1521 return -1;
1522 }
1523 if (getdata == NULL) {
1524 PyErr_Format(PyExc_ValueError, "missing 'getdata' func");
1525 return -1;
1526 }
1527
1528 // Make sure the class isn't ever deallocated.
1529 Py_INCREF((PyObject *)cls);
1530
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001531 struct _xidregistry *xidregistry = &_PyRuntime.xidregistry ;
1532 PyThread_acquire_lock(xidregistry->mutex, WAIT_LOCK);
1533 if (xidregistry->head == NULL) {
1534 _register_builtins_for_crossinterpreter_data(xidregistry);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001535 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001536 int res = _register_xidata(xidregistry, cls, getdata);
1537 PyThread_release_lock(xidregistry->mutex);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001538 return res;
1539}
1540
Eric Snow6d2cd902018-05-16 15:04:57 -04001541/* Cross-interpreter objects are looked up by exact match on the class.
1542 We can reassess this policy when we move from a global registry to a
1543 tp_* slot. */
1544
Eric Snow7f8bfc92018-01-29 18:23:44 -07001545crossinterpdatafunc
1546_PyCrossInterpreterData_Lookup(PyObject *obj)
1547{
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001548 struct _xidregistry *xidregistry = &_PyRuntime.xidregistry ;
Eric Snow7f8bfc92018-01-29 18:23:44 -07001549 PyObject *cls = PyObject_Type(obj);
1550 crossinterpdatafunc getdata = NULL;
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001551 PyThread_acquire_lock(xidregistry->mutex, WAIT_LOCK);
1552 struct _xidregitem *cur = xidregistry->head;
Eric Snow7f8bfc92018-01-29 18:23:44 -07001553 if (cur == NULL) {
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001554 _register_builtins_for_crossinterpreter_data(xidregistry);
1555 cur = xidregistry->head;
Eric Snow7f8bfc92018-01-29 18:23:44 -07001556 }
1557 for(; cur != NULL; cur = cur->next) {
1558 if (cur->cls == (PyTypeObject *)cls) {
1559 getdata = cur->getdata;
1560 break;
1561 }
1562 }
Eric Snow4e9da0d2018-02-02 21:49:49 -07001563 Py_DECREF(cls);
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001564 PyThread_release_lock(xidregistry->mutex);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001565 return getdata;
1566}
1567
1568/* cross-interpreter data for builtin types */
1569
Eric Snow6d2cd902018-05-16 15:04:57 -04001570struct _shared_bytes_data {
1571 char *bytes;
1572 Py_ssize_t len;
1573};
1574
Eric Snow7f8bfc92018-01-29 18:23:44 -07001575static PyObject *
1576_new_bytes_object(_PyCrossInterpreterData *data)
1577{
Eric Snow6d2cd902018-05-16 15:04:57 -04001578 struct _shared_bytes_data *shared = (struct _shared_bytes_data *)(data->data);
1579 return PyBytes_FromStringAndSize(shared->bytes, shared->len);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001580}
1581
1582static int
1583_bytes_shared(PyObject *obj, _PyCrossInterpreterData *data)
1584{
Eric Snow6d2cd902018-05-16 15:04:57 -04001585 struct _shared_bytes_data *shared = PyMem_NEW(struct _shared_bytes_data, 1);
1586 if (PyBytes_AsStringAndSize(obj, &shared->bytes, &shared->len) < 0) {
1587 return -1;
1588 }
1589 data->data = (void *)shared;
Eric Snow63799132018-06-01 18:45:20 -06001590 Py_INCREF(obj);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001591 data->obj = obj; // Will be "released" (decref'ed) when data released.
1592 data->new_object = _new_bytes_object;
Eric Snow6d2cd902018-05-16 15:04:57 -04001593 data->free = PyMem_Free;
1594 return 0;
1595}
1596
1597struct _shared_str_data {
1598 int kind;
1599 const void *buffer;
1600 Py_ssize_t len;
1601};
1602
1603static PyObject *
1604_new_str_object(_PyCrossInterpreterData *data)
1605{
1606 struct _shared_str_data *shared = (struct _shared_str_data *)(data->data);
1607 return PyUnicode_FromKindAndData(shared->kind, shared->buffer, shared->len);
1608}
1609
1610static int
1611_str_shared(PyObject *obj, _PyCrossInterpreterData *data)
1612{
1613 struct _shared_str_data *shared = PyMem_NEW(struct _shared_str_data, 1);
1614 shared->kind = PyUnicode_KIND(obj);
1615 shared->buffer = PyUnicode_DATA(obj);
1616 shared->len = PyUnicode_GET_LENGTH(obj) - 1;
1617 data->data = (void *)shared;
Eric Snow63799132018-06-01 18:45:20 -06001618 Py_INCREF(obj);
Eric Snow6d2cd902018-05-16 15:04:57 -04001619 data->obj = obj; // Will be "released" (decref'ed) when data released.
1620 data->new_object = _new_str_object;
1621 data->free = PyMem_Free;
1622 return 0;
1623}
1624
1625static PyObject *
1626_new_long_object(_PyCrossInterpreterData *data)
1627{
Alexey Izbyshev16f842d2019-02-12 19:06:43 +03001628 return PyLong_FromSsize_t((Py_ssize_t)(data->data));
Eric Snow6d2cd902018-05-16 15:04:57 -04001629}
1630
1631static int
1632_long_shared(PyObject *obj, _PyCrossInterpreterData *data)
1633{
Alexey Izbyshev16f842d2019-02-12 19:06:43 +03001634 /* Note that this means the size of shareable ints is bounded by
1635 * sys.maxsize. Hence on 32-bit architectures that is half the
1636 * size of maximum shareable ints on 64-bit.
1637 */
1638 Py_ssize_t value = PyLong_AsSsize_t(obj);
Eric Snow6d2cd902018-05-16 15:04:57 -04001639 if (value == -1 && PyErr_Occurred()) {
1640 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
1641 PyErr_SetString(PyExc_OverflowError, "try sending as bytes");
1642 }
1643 return -1;
1644 }
1645 data->data = (void *)value;
1646 data->obj = NULL;
1647 data->new_object = _new_long_object;
1648 data->free = NULL;
Eric Snow7f8bfc92018-01-29 18:23:44 -07001649 return 0;
1650}
1651
1652static PyObject *
1653_new_none_object(_PyCrossInterpreterData *data)
1654{
1655 // XXX Singleton refcounts are problematic across interpreters...
1656 Py_INCREF(Py_None);
1657 return Py_None;
1658}
1659
1660static int
1661_none_shared(PyObject *obj, _PyCrossInterpreterData *data)
1662{
1663 data->data = NULL;
1664 // data->obj remains NULL
1665 data->new_object = _new_none_object;
1666 data->free = NULL; // There is nothing to free.
1667 return 0;
1668}
1669
1670static void
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001671_register_builtins_for_crossinterpreter_data(struct _xidregistry *xidregistry)
Eric Snow7f8bfc92018-01-29 18:23:44 -07001672{
1673 // None
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001674 if (_register_xidata(xidregistry, (PyTypeObject *)PyObject_Type(Py_None), _none_shared) != 0) {
Eric Snow7f8bfc92018-01-29 18:23:44 -07001675 Py_FatalError("could not register None for cross-interpreter sharing");
1676 }
1677
Eric Snow6d2cd902018-05-16 15:04:57 -04001678 // int
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001679 if (_register_xidata(xidregistry, &PyLong_Type, _long_shared) != 0) {
Eric Snow6d2cd902018-05-16 15:04:57 -04001680 Py_FatalError("could not register int for cross-interpreter sharing");
1681 }
1682
Eric Snow7f8bfc92018-01-29 18:23:44 -07001683 // bytes
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001684 if (_register_xidata(xidregistry, &PyBytes_Type, _bytes_shared) != 0) {
Eric Snow7f8bfc92018-01-29 18:23:44 -07001685 Py_FatalError("could not register bytes for cross-interpreter sharing");
1686 }
Eric Snow6d2cd902018-05-16 15:04:57 -04001687
1688 // str
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001689 if (_register_xidata(xidregistry, &PyUnicode_Type, _str_shared) != 0) {
Eric Snow6d2cd902018-05-16 15:04:57 -04001690 Py_FatalError("could not register str for cross-interpreter sharing");
1691 }
Eric Snow7f8bfc92018-01-29 18:23:44 -07001692}
1693
1694
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001695#ifdef __cplusplus
1696}
1697#endif