blob: 879a5a91f8a3b09d9fb0a34b18aad9d6f6f43944 [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 Stinner022be022019-05-22 23:58:50 +020052 _PyPreConfig_InitPythonConfig(&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 Stinner022be022019-05-22 23:58:50 +0200192
193 _PyInitError err = _PyCoreConfig_InitPythonConfig(&interp->core_config);
194 if (_Py_INIT_FAILED(err)) {
195 PyMem_RawFree(interp);
196 return NULL;
197 }
198
Victor Stinnerd4341102017-11-23 00:12:09 +0100199 interp->eval_frame = _PyEval_EvalFrameDefault;
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000200#ifdef HAVE_DLOPEN
Serhiy Storchakac2f7d872016-05-04 09:44:44 +0300201#if HAVE_DECL_RTLD_NOW
Victor Stinnerd4341102017-11-23 00:12:09 +0100202 interp->dlopenflags = RTLD_NOW;
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000203#else
Victor Stinnerd4341102017-11-23 00:12:09 +0100204 interp->dlopenflags = RTLD_LAZY;
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000205#endif
206#endif
Guido van Rossum25ce5661997-08-02 03:10:38 +0000207
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200208 _PyRuntimeState *runtime = &_PyRuntime;
209 struct pyinterpreters *interpreters = &runtime->interpreters;
210
211 HEAD_LOCK(runtime);
212 if (interpreters->next_id < 0) {
Victor Stinnerd4341102017-11-23 00:12:09 +0100213 /* overflow or Py_Initialize() not called! */
214 PyErr_SetString(PyExc_RuntimeError,
215 "failed to get an interpreter ID");
Pablo Galindo95d630e2018-08-31 22:49:29 +0100216 PyMem_RawFree(interp);
Victor Stinnerd4341102017-11-23 00:12:09 +0100217 interp = NULL;
Victor Stinnerd4341102017-11-23 00:12:09 +0100218 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200219 else {
220 interp->id = interpreters->next_id;
221 interpreters->next_id += 1;
222 interp->next = interpreters->head;
223 if (interpreters->main == NULL) {
224 interpreters->main = interp;
225 }
226 interpreters->head = interp;
227 }
228 HEAD_UNLOCK(runtime);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000229
Pablo Galindo95d630e2018-08-31 22:49:29 +0100230 if (interp == NULL) {
231 return NULL;
232 }
233
Yury Selivanovf23746a2018-01-22 19:11:18 -0500234 interp->tstate_next_unique_id = 0;
235
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 return interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000237}
238
239
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200240static void
241_PyInterpreterState_Clear(_PyRuntimeState *runtime, PyInterpreterState *interp)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000242{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200243 HEAD_LOCK(runtime);
244 for (PyThreadState *p = interp->tstate_head; p != NULL; p = p->next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 PyThreadState_Clear(p);
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200246 }
247 HEAD_UNLOCK(runtime);
Victor Stinnerda273412017-12-15 01:46:02 +0100248 _PyCoreConfig_Clear(&interp->core_config);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 Py_CLEAR(interp->codec_search_path);
250 Py_CLEAR(interp->codec_search_cache);
251 Py_CLEAR(interp->codec_error_registry);
Eric Snow93c92f72017-09-13 23:46:04 -0700252 Py_CLEAR(interp->modules);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 Py_CLEAR(interp->modules_by_index);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 Py_CLEAR(interp->sysdict);
255 Py_CLEAR(interp->builtins);
Serhiy Storchaka87a5c512014-02-10 18:21:34 +0200256 Py_CLEAR(interp->builtins_copy);
Brett Cannonfd074152012-04-14 14:10:13 -0400257 Py_CLEAR(interp->importlib);
Serhiy Storchaka133138a2016-08-02 22:51:21 +0300258 Py_CLEAR(interp->import_func);
Eric Snowd2fdd1f2019-03-15 17:47:43 -0600259 Py_CLEAR(interp->dict);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200260#ifdef HAVE_FORK
261 Py_CLEAR(interp->before_forkers);
262 Py_CLEAR(interp->after_forkers_parent);
263 Py_CLEAR(interp->after_forkers_child);
264#endif
Eric Snow86ea5812019-05-10 13:29:55 -0400265 if (runtime->finalizing == NULL) {
266 _PyWarnings_Fini(interp);
267 }
Eric Snow5be45a62019-03-08 22:47:07 -0700268 // XXX Once we have one allocator per interpreter (i.e.
269 // per-interpreter GC) we must ensure that all of the interpreter's
270 // objects have been cleaned up at the point.
Guido van Rossum25ce5661997-08-02 03:10:38 +0000271}
272
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200273void
274PyInterpreterState_Clear(PyInterpreterState *interp)
275{
276 _PyInterpreterState_Clear(&_PyRuntime, interp);
277}
278
Guido van Rossum25ce5661997-08-02 03:10:38 +0000279
280static void
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200281zapthreads(_PyRuntimeState *runtime, PyInterpreterState *interp)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000282{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 PyThreadState *p;
284 /* No need to lock the mutex here because this should only happen
285 when the threads are all really dead (XXX famous last words). */
286 while ((p = interp->tstate_head) != NULL) {
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200287 _PyThreadState_Delete(runtime, p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000289}
290
291
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200292static void
293_PyInterpreterState_Delete(_PyRuntimeState *runtime,
294 PyInterpreterState *interp)
295{
296 struct pyinterpreters *interpreters = &runtime->interpreters;
297 zapthreads(runtime, interp);
298 HEAD_LOCK(runtime);
299 PyInterpreterState **p;
300 for (p = &interpreters->head; ; p = &(*p)->next) {
301 if (*p == NULL) {
302 Py_FatalError("PyInterpreterState_Delete: invalid interp");
303 }
304 if (*p == interp) {
305 break;
306 }
307 }
308 if (interp->tstate_head != NULL) {
309 Py_FatalError("PyInterpreterState_Delete: remaining threads");
310 }
311 *p = interp->next;
312 if (interpreters->main == interp) {
313 interpreters->main = NULL;
314 if (interpreters->head != NULL) {
315 Py_FatalError("PyInterpreterState_Delete: remaining subinterpreters");
316 }
317 }
318 HEAD_UNLOCK(runtime);
319 if (interp->id_mutex != NULL) {
320 PyThread_free_lock(interp->id_mutex);
321 }
322 PyMem_RawFree(interp);
323}
324
325
Guido van Rossum25ce5661997-08-02 03:10:38 +0000326void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000327PyInterpreterState_Delete(PyInterpreterState *interp)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000328{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200329 _PyInterpreterState_Delete(&_PyRuntime, interp);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000330}
331
332
Eric Snow59032962018-09-14 14:17:20 -0700333/*
334 * Delete all interpreter states except the main interpreter. If there
335 * is a current interpreter state, it *must* be the main interpreter.
336 */
337void
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200338_PyInterpreterState_DeleteExceptMain(_PyRuntimeState *runtime)
Eric Snow59032962018-09-14 14:17:20 -0700339{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200340 struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200341 struct pyinterpreters *interpreters = &runtime->interpreters;
342
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200343 PyThreadState *tstate = _PyThreadState_Swap(gilstate, NULL);
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200344 if (tstate != NULL && tstate->interp != interpreters->main) {
Eric Snow59032962018-09-14 14:17:20 -0700345 Py_FatalError("PyInterpreterState_DeleteExceptMain: not main interpreter");
346 }
347
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200348 HEAD_LOCK(runtime);
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200349 PyInterpreterState *interp = interpreters->head;
350 interpreters->head = NULL;
Stéphane Wirtelb5409da2019-02-20 15:27:22 +0100351 while (interp != NULL) {
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200352 if (interp == interpreters->main) {
353 interpreters->main->next = NULL;
354 interpreters->head = interp;
Stéphane Wirtelb5409da2019-02-20 15:27:22 +0100355 interp = interp->next;
Eric Snow59032962018-09-14 14:17:20 -0700356 continue;
357 }
358
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200359 _PyInterpreterState_Clear(runtime, interp); // XXX must activate?
360 zapthreads(runtime, interp);
Eric Snow59032962018-09-14 14:17:20 -0700361 if (interp->id_mutex != NULL) {
362 PyThread_free_lock(interp->id_mutex);
363 }
Stéphane Wirtelb5409da2019-02-20 15:27:22 +0100364 PyInterpreterState *prev_interp = interp;
365 interp = interp->next;
366 PyMem_RawFree(prev_interp);
Eric Snow59032962018-09-14 14:17:20 -0700367 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200368 HEAD_UNLOCK(runtime);
Eric Snow59032962018-09-14 14:17:20 -0700369
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200370 if (interpreters->head == NULL) {
Eric Snow59032962018-09-14 14:17:20 -0700371 Py_FatalError("PyInterpreterState_DeleteExceptMain: missing main");
372 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200373 _PyThreadState_Swap(gilstate, tstate);
Eric Snow59032962018-09-14 14:17:20 -0700374}
375
376
Victor Stinnercaba55b2018-08-03 15:33:52 +0200377PyInterpreterState *
378_PyInterpreterState_Get(void)
379{
Victor Stinner50b48572018-11-01 01:51:40 +0100380 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnercaba55b2018-08-03 15:33:52 +0200381 if (tstate == NULL) {
382 Py_FatalError("_PyInterpreterState_Get(): no current thread state");
383 }
384 PyInterpreterState *interp = tstate->interp;
385 if (interp == NULL) {
386 Py_FatalError("_PyInterpreterState_Get(): no current interpreter");
387 }
388 return interp;
389}
390
391
Eric Snowe3774162017-05-22 19:46:40 -0700392int64_t
393PyInterpreterState_GetID(PyInterpreterState *interp)
394{
395 if (interp == NULL) {
396 PyErr_SetString(PyExc_RuntimeError, "no interpreter provided");
397 return -1;
398 }
399 return interp->id;
400}
401
402
Eric Snow5be45a62019-03-08 22:47:07 -0700403static PyInterpreterState *
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200404interp_look_up_id(_PyRuntimeState *runtime, PY_INT64_T requested_id)
Eric Snowb05b7112019-03-01 12:35:10 -0700405{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200406 PyInterpreterState *interp = runtime->interpreters.head;
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100407 while (interp != NULL) {
408 PY_INT64_T id = PyInterpreterState_GetID(interp);
Eric Snow5be45a62019-03-08 22:47:07 -0700409 if (id < 0) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100410 return NULL;
Eric Snow5be45a62019-03-08 22:47:07 -0700411 }
412 if (requested_id == id) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100413 return interp;
Eric Snow5be45a62019-03-08 22:47:07 -0700414 }
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100415 interp = PyInterpreterState_Next(interp);
Eric Snowb05b7112019-03-01 12:35:10 -0700416 }
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100417 return NULL;
Eric Snowb05b7112019-03-01 12:35:10 -0700418}
419
Eric Snow5be45a62019-03-08 22:47:07 -0700420PyInterpreterState *
421_PyInterpreterState_LookUpID(PY_INT64_T requested_id)
422{
423 PyInterpreterState *interp = NULL;
424 if (requested_id >= 0) {
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200425 _PyRuntimeState *runtime = &_PyRuntime;
426 HEAD_LOCK(runtime);
427 interp = interp_look_up_id(runtime, requested_id);
428 HEAD_UNLOCK(runtime);
Eric Snow5be45a62019-03-08 22:47:07 -0700429 }
430 if (interp == NULL && !PyErr_Occurred()) {
431 PyErr_Format(PyExc_RuntimeError,
432 "unrecognized interpreter ID %lld", requested_id);
433 }
434 return interp;
435}
436
Eric Snow4c6955e2018-02-16 18:53:40 -0700437
438int
439_PyInterpreterState_IDInitref(PyInterpreterState *interp)
440{
441 if (interp->id_mutex != NULL) {
442 return 0;
443 }
444 interp->id_mutex = PyThread_allocate_lock();
445 if (interp->id_mutex == NULL) {
446 PyErr_SetString(PyExc_RuntimeError,
447 "failed to create init interpreter ID mutex");
448 return -1;
449 }
450 interp->id_refcount = 0;
451 return 0;
452}
453
454
455void
456_PyInterpreterState_IDIncref(PyInterpreterState *interp)
457{
458 if (interp->id_mutex == NULL) {
459 return;
460 }
461 PyThread_acquire_lock(interp->id_mutex, WAIT_LOCK);
462 interp->id_refcount += 1;
463 PyThread_release_lock(interp->id_mutex);
464}
465
466
467void
468_PyInterpreterState_IDDecref(PyInterpreterState *interp)
469{
470 if (interp->id_mutex == NULL) {
471 return;
472 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200473 struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
Eric Snow4c6955e2018-02-16 18:53:40 -0700474 PyThread_acquire_lock(interp->id_mutex, WAIT_LOCK);
475 assert(interp->id_refcount != 0);
476 interp->id_refcount -= 1;
477 int64_t refcount = interp->id_refcount;
478 PyThread_release_lock(interp->id_mutex);
479
Eric Snowc11183c2019-03-15 16:35:46 -0600480 if (refcount == 0 && interp->requires_idref) {
Eric Snowf53d9f22018-02-20 16:30:17 -0700481 // XXX Using the "head" thread isn't strictly correct.
482 PyThreadState *tstate = PyInterpreterState_ThreadHead(interp);
483 // XXX Possible GILState issues?
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200484 PyThreadState *save_tstate = _PyThreadState_Swap(gilstate, tstate);
Eric Snow4c6955e2018-02-16 18:53:40 -0700485 Py_EndInterpreter(tstate);
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200486 _PyThreadState_Swap(gilstate, save_tstate);
Eric Snow4c6955e2018-02-16 18:53:40 -0700487 }
488}
489
Eric Snowc11183c2019-03-15 16:35:46 -0600490int
491_PyInterpreterState_RequiresIDRef(PyInterpreterState *interp)
492{
493 return interp->requires_idref;
494}
495
496void
497_PyInterpreterState_RequireIDRef(PyInterpreterState *interp, int required)
498{
499 interp->requires_idref = required ? 1 : 0;
500}
501
Eric Snowbe3b2952019-02-23 11:35:52 -0700502_PyCoreConfig *
503_PyInterpreterState_GetCoreConfig(PyInterpreterState *interp)
504{
505 return &interp->core_config;
506}
507
Eric Snowc11183c2019-03-15 16:35:46 -0600508PyObject *
509_PyInterpreterState_GetMainModule(PyInterpreterState *interp)
510{
511 if (interp->modules == NULL) {
512 PyErr_SetString(PyExc_RuntimeError, "interpreter not initialized");
513 return NULL;
514 }
515 return PyMapping_GetItemString(interp->modules, "__main__");
516}
517
Eric Snowd2fdd1f2019-03-15 17:47:43 -0600518PyObject *
519PyInterpreterState_GetDict(PyInterpreterState *interp)
520{
521 if (interp->dict == NULL) {
522 interp->dict = PyDict_New();
523 if (interp->dict == NULL) {
524 PyErr_Clear();
525 }
526 }
527 /* Returning NULL means no per-interpreter dict is available. */
528 return interp->dict;
529}
530
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000531/* Default implementation for _PyThreadState_GetFrame */
532static struct _frame *
533threadstate_getframe(PyThreadState *self)
534{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 return self->frame;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000536}
537
Victor Stinner45b9be52010-03-03 23:28:07 +0000538static PyThreadState *
539new_threadstate(PyInterpreterState *interp, int init)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000540{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200541 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner1a7425f2013-07-07 16:25:15 +0200542 PyThreadState *tstate = (PyThreadState *)PyMem_RawMalloc(sizeof(PyThreadState));
Victor Stinner8bb32302019-04-24 16:47:40 +0200543 if (tstate == NULL) {
544 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000546
Victor Stinner8bb32302019-04-24 16:47:40 +0200547 if (_PyThreadState_GetFrame == NULL) {
548 _PyThreadState_GetFrame = threadstate_getframe;
549 }
550
551 tstate->interp = interp;
552
553 tstate->frame = NULL;
554 tstate->recursion_depth = 0;
555 tstate->overflowed = 0;
556 tstate->recursion_critical = 0;
557 tstate->stackcheck_counter = 0;
558 tstate->tracing = 0;
559 tstate->use_tracing = 0;
560 tstate->gilstate_counter = 0;
561 tstate->async_exc = NULL;
562 tstate->thread_id = PyThread_get_thread_ident();
563
564 tstate->dict = NULL;
565
566 tstate->curexc_type = NULL;
567 tstate->curexc_value = NULL;
568 tstate->curexc_traceback = NULL;
569
570 tstate->exc_state.exc_type = NULL;
571 tstate->exc_state.exc_value = NULL;
572 tstate->exc_state.exc_traceback = NULL;
573 tstate->exc_state.previous_item = NULL;
574 tstate->exc_info = &tstate->exc_state;
575
576 tstate->c_profilefunc = NULL;
577 tstate->c_tracefunc = NULL;
578 tstate->c_profileobj = NULL;
579 tstate->c_traceobj = NULL;
580
581 tstate->trash_delete_nesting = 0;
582 tstate->trash_delete_later = NULL;
583 tstate->on_delete = NULL;
584 tstate->on_delete_data = NULL;
585
586 tstate->coroutine_origin_tracking_depth = 0;
587
588 tstate->coroutine_wrapper = NULL;
589 tstate->in_coroutine_wrapper = 0;
590
591 tstate->async_gen_firstiter = NULL;
592 tstate->async_gen_finalizer = NULL;
593
594 tstate->context = NULL;
595 tstate->context_ver = 1;
596
597 tstate->id = ++interp->tstate_next_unique_id;
598
599 if (init) {
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200600 _PyThreadState_Init(runtime, tstate);
Victor Stinner8bb32302019-04-24 16:47:40 +0200601 }
602
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200603 HEAD_LOCK(runtime);
Victor Stinner8bb32302019-04-24 16:47:40 +0200604 tstate->prev = NULL;
605 tstate->next = interp->tstate_head;
606 if (tstate->next)
607 tstate->next->prev = tstate;
608 interp->tstate_head = tstate;
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200609 HEAD_UNLOCK(runtime);
Victor Stinner8bb32302019-04-24 16:47:40 +0200610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611 return tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000612}
613
Victor Stinner45b9be52010-03-03 23:28:07 +0000614PyThreadState *
615PyThreadState_New(PyInterpreterState *interp)
616{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000617 return new_threadstate(interp, 1);
Victor Stinner45b9be52010-03-03 23:28:07 +0000618}
619
620PyThreadState *
621_PyThreadState_Prealloc(PyInterpreterState *interp)
622{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 return new_threadstate(interp, 0);
Victor Stinner45b9be52010-03-03 23:28:07 +0000624}
625
626void
Victor Stinner8bb32302019-04-24 16:47:40 +0200627_PyThreadState_Init(_PyRuntimeState *runtime, PyThreadState *tstate)
Victor Stinner45b9be52010-03-03 23:28:07 +0000628{
Victor Stinner8bb32302019-04-24 16:47:40 +0200629 _PyGILState_NoteThreadState(&runtime->gilstate, tstate);
Victor Stinner45b9be52010-03-03 23:28:07 +0000630}
631
Martin v. Löwis1a214512008-06-11 05:26:20 +0000632PyObject*
Martin v. Löwis7800f752012-06-22 12:20:55 +0200633PyState_FindModule(struct PyModuleDef* module)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000634{
Martin v. Löwis7800f752012-06-22 12:20:55 +0200635 Py_ssize_t index = module->m_base.m_index;
Victor Stinner9204fb82018-10-30 15:13:17 +0100636 PyInterpreterState *state = _PyInterpreterState_GET_UNSAFE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 PyObject *res;
Nick Coghland5cacbb2015-05-23 22:24:10 +1000638 if (module->m_slots) {
639 return NULL;
640 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 if (index == 0)
642 return NULL;
643 if (state->modules_by_index == NULL)
644 return NULL;
Antoine Pitrou75506e82012-08-20 19:30:46 +0200645 if (index >= PyList_GET_SIZE(state->modules_by_index))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 return NULL;
647 res = PyList_GET_ITEM(state->modules_by_index, index);
648 return res==Py_None ? NULL : res;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000649}
650
651int
652_PyState_AddModule(PyObject* module, struct PyModuleDef* def)
653{
Nick Coghland5cacbb2015-05-23 22:24:10 +1000654 PyInterpreterState *state;
Berker Peksag4b7b5652016-08-22 18:05:56 +0300655 if (!def) {
656 assert(PyErr_Occurred());
657 return -1;
658 }
Nick Coghland5cacbb2015-05-23 22:24:10 +1000659 if (def->m_slots) {
660 PyErr_SetString(PyExc_SystemError,
661 "PyState_AddModule called on module with slots");
662 return -1;
663 }
Victor Stinner9204fb82018-10-30 15:13:17 +0100664 state = _PyInterpreterState_GET_UNSAFE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 if (!state->modules_by_index) {
666 state->modules_by_index = PyList_New(0);
667 if (!state->modules_by_index)
668 return -1;
669 }
670 while(PyList_GET_SIZE(state->modules_by_index) <= def->m_base.m_index)
671 if (PyList_Append(state->modules_by_index, Py_None) < 0)
672 return -1;
673 Py_INCREF(module);
674 return PyList_SetItem(state->modules_by_index,
675 def->m_base.m_index, module);
Martin v. Löwis1a214512008-06-11 05:26:20 +0000676}
Guido van Rossuma027efa1997-05-05 20:56:21 +0000677
Martin v. Löwis7800f752012-06-22 12:20:55 +0200678int
679PyState_AddModule(PyObject* module, struct PyModuleDef* def)
680{
681 Py_ssize_t index;
Victor Stinner9204fb82018-10-30 15:13:17 +0100682 PyInterpreterState *state = _PyInterpreterState_GET_UNSAFE();
Martin v. Löwis7800f752012-06-22 12:20:55 +0200683 if (!def) {
684 Py_FatalError("PyState_AddModule: Module Definition is NULL");
685 return -1;
686 }
687 index = def->m_base.m_index;
688 if (state->modules_by_index) {
689 if(PyList_GET_SIZE(state->modules_by_index) >= index) {
690 if(module == PyList_GET_ITEM(state->modules_by_index, index)) {
691 Py_FatalError("PyState_AddModule: Module already added!");
692 return -1;
693 }
694 }
695 }
696 return _PyState_AddModule(module, def);
697}
698
699int
700PyState_RemoveModule(struct PyModuleDef* def)
701{
Nick Coghland5cacbb2015-05-23 22:24:10 +1000702 PyInterpreterState *state;
Martin v. Löwis7800f752012-06-22 12:20:55 +0200703 Py_ssize_t index = def->m_base.m_index;
Nick Coghland5cacbb2015-05-23 22:24:10 +1000704 if (def->m_slots) {
705 PyErr_SetString(PyExc_SystemError,
706 "PyState_RemoveModule called on module with slots");
707 return -1;
708 }
Victor Stinner9204fb82018-10-30 15:13:17 +0100709 state = _PyInterpreterState_GET_UNSAFE();
Martin v. Löwis7800f752012-06-22 12:20:55 +0200710 if (index == 0) {
711 Py_FatalError("PyState_RemoveModule: Module index invalid.");
712 return -1;
713 }
714 if (state->modules_by_index == NULL) {
715 Py_FatalError("PyState_RemoveModule: Interpreters module-list not acessible.");
716 return -1;
717 }
718 if (index > PyList_GET_SIZE(state->modules_by_index)) {
719 Py_FatalError("PyState_RemoveModule: Module index out of bounds.");
720 return -1;
721 }
Zackery Spytz2a893432018-12-05 00:14:00 -0700722 Py_INCREF(Py_None);
Martin v. Löwis7800f752012-06-22 12:20:55 +0200723 return PyList_SetItem(state->modules_by_index, index, Py_None);
724}
725
Antoine Pitrou40322e62013-08-11 00:30:09 +0200726/* used by import.c:PyImport_Cleanup */
727void
728_PyState_ClearModules(void)
729{
Victor Stinner9204fb82018-10-30 15:13:17 +0100730 PyInterpreterState *state = _PyInterpreterState_GET_UNSAFE();
Antoine Pitrou40322e62013-08-11 00:30:09 +0200731 if (state->modules_by_index) {
732 Py_ssize_t i;
733 for (i = 0; i < PyList_GET_SIZE(state->modules_by_index); i++) {
734 PyObject *m = PyList_GET_ITEM(state->modules_by_index, i);
735 if (PyModule_Check(m)) {
736 /* cleanup the saved copy of module dicts */
737 PyModuleDef *md = PyModule_GetDef(m);
738 if (md)
739 Py_CLEAR(md->m_base.m_copy);
740 }
741 }
742 /* Setting modules_by_index to NULL could be dangerous, so we
743 clear the list instead. */
744 if (PyList_SetSlice(state->modules_by_index,
745 0, PyList_GET_SIZE(state->modules_by_index),
746 NULL))
747 PyErr_WriteUnraisable(state->modules_by_index);
748 }
749}
750
Guido van Rossuma027efa1997-05-05 20:56:21 +0000751void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000752PyThreadState_Clear(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000753{
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200754 int verbose = tstate->interp->core_config.verbose;
755
756 if (verbose && tstate->frame != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 fprintf(stderr,
758 "PyThreadState_Clear: warning: thread still has a frame\n");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000759
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 Py_CLEAR(tstate->frame);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 Py_CLEAR(tstate->dict);
763 Py_CLEAR(tstate->async_exc);
Guido van Rossumede04391998-04-10 20:18:25 +0000764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 Py_CLEAR(tstate->curexc_type);
766 Py_CLEAR(tstate->curexc_value);
767 Py_CLEAR(tstate->curexc_traceback);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000768
Mark Shannonae3087c2017-10-22 22:41:51 +0100769 Py_CLEAR(tstate->exc_state.exc_type);
770 Py_CLEAR(tstate->exc_state.exc_value);
771 Py_CLEAR(tstate->exc_state.exc_traceback);
Serhiy Storchakabdf42982017-10-26 16:59:40 +0300772
Mark Shannonae3087c2017-10-22 22:41:51 +0100773 /* The stack of exception states should contain just this thread. */
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200774 if (verbose && tstate->exc_info != &tstate->exc_state) {
Mark Shannonae3087c2017-10-22 22:41:51 +0100775 fprintf(stderr,
776 "PyThreadState_Clear: warning: thread still has a generator\n");
777 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000778
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 tstate->c_profilefunc = NULL;
780 tstate->c_tracefunc = NULL;
781 Py_CLEAR(tstate->c_profileobj);
782 Py_CLEAR(tstate->c_traceobj);
Yury Selivanov75445082015-05-11 22:57:16 -0400783
784 Py_CLEAR(tstate->coroutine_wrapper);
Yury Selivanoveb636452016-09-08 22:01:51 -0700785 Py_CLEAR(tstate->async_gen_firstiter);
786 Py_CLEAR(tstate->async_gen_finalizer);
Yury Selivanovf23746a2018-01-22 19:11:18 -0500787
788 Py_CLEAR(tstate->context);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000789}
790
791
Guido van Rossum29757862001-01-23 01:46:06 +0000792/* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
793static void
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200794tstate_delete_common(_PyRuntimeState *runtime, PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000795{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200796 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 Py_FatalError("PyThreadState_Delete: NULL tstate");
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200798 }
799 PyInterpreterState *interp = tstate->interp;
800 if (interp == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 Py_FatalError("PyThreadState_Delete: NULL interp");
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200802 }
803 HEAD_LOCK(runtime);
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200804 if (tstate->prev)
805 tstate->prev->next = tstate->next;
806 else
807 interp->tstate_head = tstate->next;
808 if (tstate->next)
809 tstate->next->prev = tstate->prev;
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200810 HEAD_UNLOCK(runtime);
Antoine Pitrou7b476992013-09-07 23:38:37 +0200811 if (tstate->on_delete != NULL) {
812 tstate->on_delete(tstate->on_delete_data);
813 }
Victor Stinner1a7425f2013-07-07 16:25:15 +0200814 PyMem_RawFree(tstate);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000815}
816
817
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200818static void
819_PyThreadState_Delete(_PyRuntimeState *runtime, PyThreadState *tstate)
Guido van Rossum29757862001-01-23 01:46:06 +0000820{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200821 struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
822 if (tstate == _PyRuntimeGILState_GetThreadState(gilstate)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 Py_FatalError("PyThreadState_Delete: tstate is still current");
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600824 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200825 if (gilstate->autoInterpreterState &&
826 PyThread_tss_get(&gilstate->autoTSSkey) == tstate)
827 {
828 PyThread_tss_set(&gilstate->autoTSSkey, NULL);
829 }
830 tstate_delete_common(runtime, tstate);
Guido van Rossum29757862001-01-23 01:46:06 +0000831}
832
833
Guido van Rossum29757862001-01-23 01:46:06 +0000834void
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200835PyThreadState_Delete(PyThreadState *tstate)
Guido van Rossum29757862001-01-23 01:46:06 +0000836{
Victor Stinner99e69d42019-04-26 05:48:51 +0200837 _PyThreadState_Delete(&_PyRuntime, tstate);
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200838}
839
840
841static void
842_PyThreadState_DeleteCurrent(_PyRuntimeState *runtime)
843{
844 struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
845 PyThreadState *tstate = _PyRuntimeGILState_GetThreadState(gilstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 if (tstate == NULL)
847 Py_FatalError(
848 "PyThreadState_DeleteCurrent: no current tstate");
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200849 tstate_delete_common(runtime, tstate);
850 if (gilstate->autoInterpreterState &&
851 PyThread_tss_get(&gilstate->autoTSSkey) == tstate)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600852 {
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200853 PyThread_tss_set(&gilstate->autoTSSkey, NULL);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600854 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200855 _PyRuntimeGILState_SetThreadState(gilstate, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 PyEval_ReleaseLock();
Guido van Rossum29757862001-01-23 01:46:06 +0000857}
Guido van Rossum29757862001-01-23 01:46:06 +0000858
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200859void
860PyThreadState_DeleteCurrent()
861{
862 _PyThreadState_DeleteCurrent(&_PyRuntime);
863}
864
Guido van Rossum29757862001-01-23 01:46:06 +0000865
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200866/*
867 * Delete all thread states except the one passed as argument.
868 * Note that, if there is a current thread state, it *must* be the one
869 * passed as argument. Also, this won't touch any other interpreters
870 * than the current one, since we don't know which thread state should
871 * be kept in those other interpreteres.
872 */
873void
Victor Stinner09532fe2019-05-10 23:39:09 +0200874_PyThreadState_DeleteExcept(_PyRuntimeState *runtime, PyThreadState *tstate)
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200875{
876 PyInterpreterState *interp = tstate->interp;
877 PyThreadState *p, *next, *garbage;
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200878 HEAD_LOCK(runtime);
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200879 /* Remove all thread states, except tstate, from the linked list of
880 thread states. This will allow calling PyThreadState_Clear()
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200881 without holding the lock. */
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200882 garbage = interp->tstate_head;
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200883 if (garbage == tstate)
884 garbage = tstate->next;
885 if (tstate->prev)
886 tstate->prev->next = tstate->next;
887 if (tstate->next)
888 tstate->next->prev = tstate->prev;
889 tstate->prev = tstate->next = NULL;
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200890 interp->tstate_head = tstate;
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200891 HEAD_UNLOCK(runtime);
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200892 /* Clear and deallocate all stale thread states. Even if this
893 executes Python code, we should be safe since it executes
894 in the current thread, not one of the stale threads. */
895 for (p = garbage; p; p = next) {
896 next = p->next;
897 PyThreadState_Clear(p);
Victor Stinner1a7425f2013-07-07 16:25:15 +0200898 PyMem_RawFree(p);
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200899 }
900}
901
902
Guido van Rossuma027efa1997-05-05 20:56:21 +0000903PyThreadState *
Victor Stinnerbfd316e2016-01-20 11:12:38 +0100904_PyThreadState_UncheckedGet(void)
905{
Victor Stinner50b48572018-11-01 01:51:40 +0100906 return _PyThreadState_GET();
Victor Stinnerbfd316e2016-01-20 11:12:38 +0100907}
908
909
910PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000911PyThreadState_Get(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000912{
Victor Stinner50b48572018-11-01 01:51:40 +0100913 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 if (tstate == NULL)
915 Py_FatalError("PyThreadState_Get: no current thread");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000916
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 return tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000918}
919
920
Victor Stinner09532fe2019-05-10 23:39:09 +0200921PyThreadState *
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200922_PyThreadState_Swap(struct _gilstate_runtime_state *gilstate, PyThreadState *newts)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000923{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200924 PyThreadState *oldts = _PyRuntimeGILState_GetThreadState(gilstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000925
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200926 _PyRuntimeGILState_SetThreadState(gilstate, newts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 /* It should not be possible for more than one thread state
928 to be used for a thread. Check this the best we can in debug
929 builds.
930 */
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200931#if defined(Py_DEBUG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 if (newts) {
933 /* This can be called from PyEval_RestoreThread(). Similar
934 to it, we need to ensure errno doesn't change.
935 */
936 int err = errno;
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200937 PyThreadState *check = _PyGILState_GetThisThreadState(gilstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 if (check && check->interp == newts->interp && check != newts)
939 Py_FatalError("Invalid thread state for this thread");
940 errno = err;
941 }
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000942#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 return oldts;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000944}
Guido van Rossumede04391998-04-10 20:18:25 +0000945
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200946PyThreadState *
947PyThreadState_Swap(PyThreadState *newts)
948{
949 return _PyThreadState_Swap(&_PyRuntime.gilstate, newts);
950}
951
Guido van Rossumede04391998-04-10 20:18:25 +0000952/* An extension mechanism to store arbitrary additional per-thread state.
953 PyThreadState_GetDict() returns a dictionary that can be used to hold such
954 state; the caller should pick a unique key and store its state there. If
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000955 PyThreadState_GetDict() returns NULL, an exception has *not* been raised
956 and the caller should assume no per-thread state is available. */
Guido van Rossumede04391998-04-10 20:18:25 +0000957
958PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000959PyThreadState_GetDict(void)
Guido van Rossumede04391998-04-10 20:18:25 +0000960{
Victor Stinner50b48572018-11-01 01:51:40 +0100961 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 if (tstate == NULL)
963 return NULL;
Guido van Rossumede04391998-04-10 20:18:25 +0000964
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 if (tstate->dict == NULL) {
966 PyObject *d;
967 tstate->dict = d = PyDict_New();
968 if (d == NULL)
969 PyErr_Clear();
970 }
971 return tstate->dict;
Guido van Rossumede04391998-04-10 20:18:25 +0000972}
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000973
974
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000975/* Asynchronously raise an exception in a thread.
976 Requested by Just van Rossum and Alex Martelli.
Guido van Rossum0f1f63c2005-02-08 02:07:57 +0000977 To prevent naive misuse, you must write your own extension
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000978 to call this, or use ctypes. Must be called with the GIL held.
979 Returns the number of tstates modified (normally 1, but 0 if `id` didn't
980 match any known thread id). Can be called with exc=NULL to clear an
981 existing async exception. This raises no exceptions. */
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000982
983int
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200984PyThreadState_SetAsyncExc(unsigned long id, PyObject *exc)
985{
Victor Stinner09532fe2019-05-10 23:39:09 +0200986 _PyRuntimeState *runtime = &_PyRuntime;
987 PyInterpreterState *interp = _PyRuntimeState_GetThreadState(runtime)->interp;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000988
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 /* Although the GIL is held, a few C API functions can be called
990 * without the GIL held, and in particular some that create and
991 * destroy thread and interpreter states. Those can mutate the
992 * list of thread states we're traversing, so to prevent that we lock
993 * head_mutex for the duration.
994 */
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200995 HEAD_LOCK(runtime);
Victor Stinner09532fe2019-05-10 23:39:09 +0200996 for (PyThreadState *p = interp->tstate_head; p != NULL; p = p->next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 if (p->thread_id == id) {
998 /* Tricky: we need to decref the current value
999 * (if any) in p->async_exc, but that can in turn
1000 * allow arbitrary Python code to run, including
1001 * perhaps calls to this function. To prevent
1002 * deadlock, we need to release head_mutex before
1003 * the decref.
1004 */
1005 PyObject *old_exc = p->async_exc;
1006 Py_XINCREF(exc);
1007 p->async_exc = exc;
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001008 HEAD_UNLOCK(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 Py_XDECREF(old_exc);
Victor Stinner09532fe2019-05-10 23:39:09 +02001010 _PyEval_SignalAsyncExc(&runtime->ceval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 return 1;
1012 }
1013 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001014 HEAD_UNLOCK(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 return 0;
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +00001016}
1017
1018
Guido van Rossumf5df46d2001-07-19 12:19:27 +00001019/* Routines for advanced debuggers, requested by David Beazley.
1020 Don't use unless you know what you are doing! */
1021
1022PyInterpreterState *
1023PyInterpreterState_Head(void)
1024{
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001025 return _PyRuntime.interpreters.head;
Guido van Rossumf5df46d2001-07-19 12:19:27 +00001026}
1027
1028PyInterpreterState *
Eric Snow6b4be192017-05-22 21:36:03 -07001029PyInterpreterState_Main(void)
1030{
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001031 return _PyRuntime.interpreters.main;
Eric Snow6b4be192017-05-22 21:36:03 -07001032}
1033
1034PyInterpreterState *
Guido van Rossumf5df46d2001-07-19 12:19:27 +00001035PyInterpreterState_Next(PyInterpreterState *interp) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 return interp->next;
Guido van Rossumf5df46d2001-07-19 12:19:27 +00001037}
1038
1039PyThreadState *
1040PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 return interp->tstate_head;
Guido van Rossumf5df46d2001-07-19 12:19:27 +00001042}
1043
1044PyThreadState *
1045PyThreadState_Next(PyThreadState *tstate) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001046 return tstate->next;
Guido van Rossumf5df46d2001-07-19 12:19:27 +00001047}
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001048
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001049/* The implementation of sys._current_frames(). This is intended to be
1050 called with the GIL held, as it will be when called via
1051 sys._current_frames(). It's possible it would work fine even without
1052 the GIL held, but haven't thought enough about that.
1053*/
1054PyObject *
1055_PyThread_CurrentFrames(void)
1056{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 PyObject *result;
1058 PyInterpreterState *i;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 result = PyDict_New();
1061 if (result == NULL)
1062 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001063
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 /* for i in all interpreters:
1065 * for t in all of i's thread states:
1066 * if t's frame isn't NULL, map t's id to its frame
Ezio Melotti13925002011-03-16 11:05:33 +02001067 * Because these lists can mutate even when the GIL is held, we
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 * need to grab head_mutex for the duration.
1069 */
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001070 _PyRuntimeState *runtime = &_PyRuntime;
1071 HEAD_LOCK(runtime);
1072 for (i = runtime->interpreters.head; i != NULL; i = i->next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 PyThreadState *t;
1074 for (t = i->tstate_head; t != NULL; t = t->next) {
1075 PyObject *id;
1076 int stat;
1077 struct _frame *frame = t->frame;
1078 if (frame == NULL)
1079 continue;
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001080 id = PyLong_FromUnsignedLong(t->thread_id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001081 if (id == NULL)
1082 goto Fail;
1083 stat = PyDict_SetItem(result, id, (PyObject *)frame);
1084 Py_DECREF(id);
1085 if (stat < 0)
1086 goto Fail;
1087 }
1088 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001089 HEAD_UNLOCK(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001091
1092 Fail:
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001093 HEAD_UNLOCK(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 Py_DECREF(result);
1095 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001096}
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +00001097
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001098/* Python "auto thread state" API. */
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001099
1100/* Keep this as a static, as it is not reliable! It can only
1101 ever be compared to the state for the *current* thread.
1102 * If not equal, then it doesn't matter that the actual
1103 value may change immediately after comparison, as it can't
1104 possibly change to the current thread's state.
1105 * If equal, then the current thread holds the lock, so the value can't
1106 change until we yield the lock.
1107*/
1108static int
1109PyThreadState_IsCurrent(PyThreadState *tstate)
1110{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 /* Must be the tstate for this thread */
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001112 struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
1113 assert(_PyGILState_GetThisThreadState(gilstate) == tstate);
1114 return tstate == _PyRuntimeGILState_GetThreadState(gilstate);
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001115}
1116
Tim Peters4c1f5ec2004-10-09 17:25:05 +00001117/* Internal initialization/finalization functions called by
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001118 Py_Initialize/Py_FinalizeEx
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001119*/
Tim Peters19717fa2004-10-09 17:38:29 +00001120void
Victor Stinner43125222019-04-24 18:23:53 +02001121_PyGILState_Init(_PyRuntimeState *runtime,
1122 PyInterpreterState *interp, PyThreadState *tstate)
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001123{
Victor Stinner8bb32302019-04-24 16:47:40 +02001124 /* must init with valid states */
1125 assert(interp != NULL);
1126 assert(tstate != NULL);
1127
Victor Stinner8bb32302019-04-24 16:47:40 +02001128 struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
1129
1130 if (PyThread_tss_create(&gilstate->autoTSSkey) != 0) {
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001131 Py_FatalError("Could not allocate TSS entry");
1132 }
Victor Stinner8bb32302019-04-24 16:47:40 +02001133 gilstate->autoInterpreterState = interp;
1134 assert(PyThread_tss_get(&gilstate->autoTSSkey) == NULL);
1135 assert(tstate->gilstate_counter == 0);
Michael W. Hudson188d4362005-06-20 16:52:57 +00001136
Victor Stinner8bb32302019-04-24 16:47:40 +02001137 _PyGILState_NoteThreadState(gilstate, tstate);
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001138}
1139
Victor Stinner861d9ab2016-03-16 22:45:24 +01001140PyInterpreterState *
1141_PyGILState_GetInterpreterStateUnsafe(void)
1142{
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001143 return _PyRuntime.gilstate.autoInterpreterState;
Victor Stinner861d9ab2016-03-16 22:45:24 +01001144}
1145
Tim Peters19717fa2004-10-09 17:38:29 +00001146void
Victor Stinner8e91c242019-04-24 17:24:01 +02001147_PyGILState_Fini(_PyRuntimeState *runtime)
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001148{
Victor Stinner8e91c242019-04-24 17:24:01 +02001149 struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
1150 PyThread_tss_delete(&gilstate->autoTSSkey);
1151 gilstate->autoInterpreterState = NULL;
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001152}
1153
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001154/* Reset the TSS key - called by PyOS_AfterFork_Child().
Antoine Pitrou0c759fe2011-04-27 19:28:05 +02001155 * This should not be necessary, but some - buggy - pthread implementations
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001156 * don't reset TSS upon fork(), see issue #10517.
Antoine Pitrou0c759fe2011-04-27 19:28:05 +02001157 */
1158void
Victor Stinnerb930a2d2019-04-24 17:14:33 +02001159_PyGILState_Reinit(_PyRuntimeState *runtime)
Antoine Pitrou0c759fe2011-04-27 19:28:05 +02001160{
Victor Stinnerb930a2d2019-04-24 17:14:33 +02001161 struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001162 PyThreadState *tstate = _PyGILState_GetThisThreadState(gilstate);
Victor Stinnerb930a2d2019-04-24 17:14:33 +02001163
1164 PyThread_tss_delete(&gilstate->autoTSSkey);
1165 if (PyThread_tss_create(&gilstate->autoTSSkey) != 0) {
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001166 Py_FatalError("Could not allocate TSS entry");
1167 }
Antoine Pitrou0c759fe2011-04-27 19:28:05 +02001168
Charles-François Natalia233df82011-11-22 19:49:51 +01001169 /* If the thread had an associated auto thread state, reassociate it with
1170 * the new key. */
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001171 if (tstate &&
Victor Stinnerb930a2d2019-04-24 17:14:33 +02001172 PyThread_tss_set(&gilstate->autoTSSkey, (void *)tstate) != 0)
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001173 {
1174 Py_FatalError("Couldn't create autoTSSkey mapping");
1175 }
Antoine Pitrou0c759fe2011-04-27 19:28:05 +02001176}
1177
Michael W. Hudson188d4362005-06-20 16:52:57 +00001178/* When a thread state is created for a thread by some mechanism other than
1179 PyGILState_Ensure, it's important that the GILState machinery knows about
1180 it so it doesn't try to create another thread state for the thread (this is
1181 a better fix for SF bug #1010677 than the first one attempted).
1182*/
Thomas Wouters89f507f2006-12-13 04:49:30 +00001183static void
Victor Stinner8bb32302019-04-24 16:47:40 +02001184_PyGILState_NoteThreadState(struct _gilstate_runtime_state *gilstate, PyThreadState* tstate)
Michael W. Hudson188d4362005-06-20 16:52:57 +00001185{
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001186 /* If autoTSSkey isn't initialized, this must be the very first
Antoine Pitrou079ce542010-09-08 12:37:10 +00001187 threadstate created in Py_Initialize(). Don't do anything for now
1188 (we'll be back here when _PyGILState_Init is called). */
Victor Stinner8bb32302019-04-24 16:47:40 +02001189 if (!gilstate->autoInterpreterState) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 return;
Victor Stinner8bb32302019-04-24 16:47:40 +02001191 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001192
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001193 /* Stick the thread state for this thread in thread specific storage.
Michael W. Hudson188d4362005-06-20 16:52:57 +00001194
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 The only situation where you can legitimately have more than one
1196 thread state for an OS level thread is when there are multiple
Victor Stinner590cebe2013-12-13 11:08:56 +01001197 interpreters.
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001198
Victor Stinner590cebe2013-12-13 11:08:56 +01001199 You shouldn't really be using the PyGILState_ APIs anyway (see issues
1200 #10915 and #15751).
Michael W. Hudson188d4362005-06-20 16:52:57 +00001201
Victor Stinner590cebe2013-12-13 11:08:56 +01001202 The first thread state created for that given OS level thread will
1203 "win", which seems reasonable behaviour.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001204 */
Victor Stinner8bb32302019-04-24 16:47:40 +02001205 if (PyThread_tss_get(&gilstate->autoTSSkey) == NULL) {
1206 if ((PyThread_tss_set(&gilstate->autoTSSkey, (void *)tstate)) != 0) {
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001207 Py_FatalError("Couldn't create autoTSSkey mapping");
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001208 }
Victor Stinner590cebe2013-12-13 11:08:56 +01001209 }
Michael W. Hudson188d4362005-06-20 16:52:57 +00001210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 /* PyGILState_Release must not try to delete this thread state. */
1212 tstate->gilstate_counter = 1;
Michael W. Hudson188d4362005-06-20 16:52:57 +00001213}
1214
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001215/* The public functions */
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001216static PyThreadState *
1217_PyGILState_GetThisThreadState(struct _gilstate_runtime_state *gilstate)
1218{
1219 if (gilstate->autoInterpreterState == NULL)
1220 return NULL;
1221 return (PyThreadState *)PyThread_tss_get(&gilstate->autoTSSkey);
1222}
1223
Tim Peters19717fa2004-10-09 17:38:29 +00001224PyThreadState *
1225PyGILState_GetThisThreadState(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001226{
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001227 return _PyGILState_GetThisThreadState(&_PyRuntime.gilstate);
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001228}
1229
Kristján Valur Jónsson684cd0e2013-03-23 03:36:16 -07001230int
1231PyGILState_Check(void)
1232{
Victor Stinner8a1be612016-03-14 22:07:55 +01001233
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001234 if (!_PyGILState_check_enabled) {
Victor Stinner8a1be612016-03-14 22:07:55 +01001235 return 1;
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001236 }
Victor Stinner8a1be612016-03-14 22:07:55 +01001237
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001238 struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
1239 if (!PyThread_tss_is_created(&gilstate->autoTSSkey)) {
1240 return 1;
1241 }
Victor Stinner8a1be612016-03-14 22:07:55 +01001242
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001243 PyThreadState *tstate = _PyRuntimeGILState_GetThreadState(gilstate);
1244 if (tstate == NULL) {
1245 return 0;
1246 }
1247
1248 return (tstate == _PyGILState_GetThisThreadState(gilstate));
Kristján Valur Jónsson684cd0e2013-03-23 03:36:16 -07001249}
1250
Tim Peters19717fa2004-10-09 17:38:29 +00001251PyGILState_STATE
1252PyGILState_Ensure(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001253{
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001254 struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 int current;
1256 PyThreadState *tcur;
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +01001257 int need_init_threads = 0;
1258
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 /* Note that we do not auto-init Python here - apart from
1260 potential races with 2 threads auto-initializing, pep-311
1261 spells out other issues. Embedders are expected to have
1262 called Py_Initialize() and usually PyEval_InitThreads().
1263 */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001264 /* Py_Initialize() hasn't been called! */
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001265 assert(gilstate->autoInterpreterState);
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +01001266
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001267 tcur = (PyThreadState *)PyThread_tss_get(&gilstate->autoTSSkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 if (tcur == NULL) {
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +01001269 need_init_threads = 1;
Victor Stinner62ca1002013-12-13 01:46:43 +01001270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 /* Create a new thread state for this thread */
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001272 tcur = PyThreadState_New(gilstate->autoInterpreterState);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273 if (tcur == NULL)
1274 Py_FatalError("Couldn't create thread-state for new thread");
1275 /* This is our thread state! We'll need to delete it in the
1276 matching call to PyGILState_Release(). */
1277 tcur->gilstate_counter = 0;
1278 current = 0; /* new thread state is never current */
1279 }
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +01001280 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 current = PyThreadState_IsCurrent(tcur);
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +01001282 }
1283
1284 if (current == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 PyEval_RestoreThread(tcur);
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +01001286 }
1287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 /* Update our counter in the thread-state - no need for locks:
1289 - tcur will remain valid as we hold the GIL.
1290 - the counter is safe as we are the only thread "allowed"
1291 to modify this value
1292 */
1293 ++tcur->gilstate_counter;
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +01001294
1295 if (need_init_threads) {
1296 /* At startup, Python has no concrete GIL. If PyGILState_Ensure() is
1297 called from a new thread for the first time, we need the create the
1298 GIL. */
1299 PyEval_InitThreads();
1300 }
1301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001302 return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001303}
1304
Tim Peters19717fa2004-10-09 17:38:29 +00001305void
1306PyGILState_Release(PyGILState_STATE oldstate)
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001307{
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001308 _PyRuntimeState *runtime = &_PyRuntime;
1309 PyThreadState *tcur = PyThread_tss_get(&runtime->gilstate.autoTSSkey);
1310 if (tcur == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 Py_FatalError("auto-releasing thread-state, "
1312 "but no thread-state for this thread");
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001313 }
1314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 /* We must hold the GIL and have our thread state current */
1316 /* XXX - remove the check - the assert should be fine,
1317 but while this is very new (April 2003), the extra check
1318 by release-only users can't hurt.
1319 */
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001320 if (!PyThreadState_IsCurrent(tcur)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 Py_FatalError("This thread state must be current when releasing");
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001322 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323 assert(PyThreadState_IsCurrent(tcur));
1324 --tcur->gilstate_counter;
1325 assert(tcur->gilstate_counter >= 0); /* illegal counter value */
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 /* If we're going to destroy this thread-state, we must
1328 * clear it while the GIL is held, as destructors may run.
1329 */
1330 if (tcur->gilstate_counter == 0) {
1331 /* can't have been locked when we created it */
1332 assert(oldstate == PyGILState_UNLOCKED);
1333 PyThreadState_Clear(tcur);
1334 /* Delete the thread-state. Note this releases the GIL too!
1335 * It's vital that the GIL be held here, to avoid shutdown
1336 * races; see bugs 225673 and 1061968 (that nasty bug has a
1337 * habit of coming back).
1338 */
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001339 _PyThreadState_DeleteCurrent(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 }
1341 /* Release the lock if necessary */
1342 else if (oldstate == PyGILState_UNLOCKED)
1343 PyEval_SaveThread();
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001344}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001345
Benjamin Peterson3bf01752012-04-13 18:06:36 -04001346
Eric Snow7f8bfc92018-01-29 18:23:44 -07001347/**************************/
1348/* cross-interpreter data */
1349/**************************/
1350
1351/* cross-interpreter data */
1352
1353crossinterpdatafunc _PyCrossInterpreterData_Lookup(PyObject *);
1354
1355/* This is a separate func from _PyCrossInterpreterData_Lookup in order
1356 to keep the registry code separate. */
1357static crossinterpdatafunc
1358_lookup_getdata(PyObject *obj)
1359{
1360 crossinterpdatafunc getdata = _PyCrossInterpreterData_Lookup(obj);
1361 if (getdata == NULL && PyErr_Occurred() == 0)
1362 PyErr_Format(PyExc_ValueError,
1363 "%S does not support cross-interpreter data", obj);
1364 return getdata;
1365}
1366
1367int
1368_PyObject_CheckCrossInterpreterData(PyObject *obj)
1369{
1370 crossinterpdatafunc getdata = _lookup_getdata(obj);
1371 if (getdata == NULL) {
1372 return -1;
1373 }
1374 return 0;
1375}
1376
1377static int
1378_check_xidata(_PyCrossInterpreterData *data)
1379{
1380 // data->data can be anything, including NULL, so we don't check it.
1381
1382 // data->obj may be NULL, so we don't check it.
1383
1384 if (data->interp < 0) {
1385 PyErr_SetString(PyExc_SystemError, "missing interp");
1386 return -1;
1387 }
1388
1389 if (data->new_object == NULL) {
1390 PyErr_SetString(PyExc_SystemError, "missing new_object func");
1391 return -1;
1392 }
1393
1394 // data->free may be NULL, so we don't check it.
1395
1396 return 0;
1397}
1398
1399int
1400_PyObject_GetCrossInterpreterData(PyObject *obj, _PyCrossInterpreterData *data)
1401{
Victor Stinnercaba55b2018-08-03 15:33:52 +02001402 // _PyInterpreterState_Get() aborts if lookup fails, so we don't need
Eric Snow7f8bfc92018-01-29 18:23:44 -07001403 // to check the result for NULL.
Victor Stinnercaba55b2018-08-03 15:33:52 +02001404 PyInterpreterState *interp = _PyInterpreterState_Get();
Eric Snow7f8bfc92018-01-29 18:23:44 -07001405
1406 // Reset data before re-populating.
1407 *data = (_PyCrossInterpreterData){0};
1408 data->free = PyMem_RawFree; // Set a default that may be overridden.
1409
1410 // Call the "getdata" func for the object.
1411 Py_INCREF(obj);
1412 crossinterpdatafunc getdata = _lookup_getdata(obj);
1413 if (getdata == NULL) {
1414 Py_DECREF(obj);
1415 return -1;
1416 }
1417 int res = getdata(obj, data);
1418 Py_DECREF(obj);
1419 if (res != 0) {
1420 return -1;
1421 }
1422
1423 // Fill in the blanks and validate the result.
Eric Snow7f8bfc92018-01-29 18:23:44 -07001424 data->interp = interp->id;
1425 if (_check_xidata(data) != 0) {
1426 _PyCrossInterpreterData_Release(data);
1427 return -1;
1428 }
1429
1430 return 0;
1431}
1432
Eric Snowb75b1a352019-04-12 10:20:10 -06001433static void
Eric Snow63799132018-06-01 18:45:20 -06001434_release_xidata(void *arg)
1435{
1436 _PyCrossInterpreterData *data = (_PyCrossInterpreterData *)arg;
1437 if (data->free != NULL) {
1438 data->free(data->data);
1439 }
1440 Py_XDECREF(data->obj);
Eric Snowb75b1a352019-04-12 10:20:10 -06001441}
1442
1443static void
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001444_call_in_interpreter(struct _gilstate_runtime_state *gilstate,
1445 PyInterpreterState *interp,
Eric Snowb75b1a352019-04-12 10:20:10 -06001446 void (*func)(void *), void *arg)
1447{
1448 /* We would use Py_AddPendingCall() if it weren't specific to the
1449 * main interpreter (see bpo-33608). In the meantime we take a
1450 * naive approach.
1451 */
1452 PyThreadState *save_tstate = NULL;
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001453 if (interp != _PyRuntimeGILState_GetThreadState(gilstate)->interp) {
Eric Snowb75b1a352019-04-12 10:20:10 -06001454 // XXX Using the "head" thread isn't strictly correct.
1455 PyThreadState *tstate = PyInterpreterState_ThreadHead(interp);
1456 // XXX Possible GILState issues?
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001457 save_tstate = _PyThreadState_Swap(gilstate, tstate);
Eric Snowb75b1a352019-04-12 10:20:10 -06001458 }
1459
1460 func(arg);
1461
1462 // Switch back.
1463 if (save_tstate != NULL) {
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001464 _PyThreadState_Swap(gilstate, save_tstate);
Eric Snowb75b1a352019-04-12 10:20:10 -06001465 }
Eric Snow63799132018-06-01 18:45:20 -06001466}
1467
Eric Snow7f8bfc92018-01-29 18:23:44 -07001468void
1469_PyCrossInterpreterData_Release(_PyCrossInterpreterData *data)
1470{
1471 if (data->data == NULL && data->obj == NULL) {
1472 // Nothing to release!
1473 return;
1474 }
1475
Eric Snowb75b1a352019-04-12 10:20:10 -06001476 // Switch to the original interpreter.
Eric Snow7f8bfc92018-01-29 18:23:44 -07001477 PyInterpreterState *interp = _PyInterpreterState_LookUpID(data->interp);
1478 if (interp == NULL) {
1479 // The intepreter was already destroyed.
1480 if (data->free != NULL) {
1481 // XXX Someone leaked some memory...
1482 }
1483 return;
1484 }
Eric Snowf53d9f22018-02-20 16:30:17 -07001485
Eric Snow7f8bfc92018-01-29 18:23:44 -07001486 // "Release" the data and/or the object.
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001487 struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
1488 _call_in_interpreter(gilstate, interp, _release_xidata, data);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001489}
1490
1491PyObject *
1492_PyCrossInterpreterData_NewObject(_PyCrossInterpreterData *data)
1493{
1494 return data->new_object(data);
1495}
1496
1497/* registry of {type -> crossinterpdatafunc} */
1498
1499/* For now we use a global registry of shareable classes. An
1500 alternative would be to add a tp_* slot for a class's
1501 crossinterpdatafunc. It would be simpler and more efficient. */
1502
1503static int
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001504_register_xidata(struct _xidregistry *xidregistry, PyTypeObject *cls,
1505 crossinterpdatafunc getdata)
Eric Snow7f8bfc92018-01-29 18:23:44 -07001506{
1507 // Note that we effectively replace already registered classes
1508 // rather than failing.
1509 struct _xidregitem *newhead = PyMem_RawMalloc(sizeof(struct _xidregitem));
1510 if (newhead == NULL)
1511 return -1;
1512 newhead->cls = cls;
1513 newhead->getdata = getdata;
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001514 newhead->next = xidregistry->head;
1515 xidregistry->head = newhead;
Eric Snow7f8bfc92018-01-29 18:23:44 -07001516 return 0;
1517}
1518
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001519static void _register_builtins_for_crossinterpreter_data(struct _xidregistry *xidregistry);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001520
1521int
Eric Snowc11183c2019-03-15 16:35:46 -06001522_PyCrossInterpreterData_RegisterClass(PyTypeObject *cls,
Eric Snow7f8bfc92018-01-29 18:23:44 -07001523 crossinterpdatafunc getdata)
1524{
1525 if (!PyType_Check(cls)) {
1526 PyErr_Format(PyExc_ValueError, "only classes may be registered");
1527 return -1;
1528 }
1529 if (getdata == NULL) {
1530 PyErr_Format(PyExc_ValueError, "missing 'getdata' func");
1531 return -1;
1532 }
1533
1534 // Make sure the class isn't ever deallocated.
1535 Py_INCREF((PyObject *)cls);
1536
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001537 struct _xidregistry *xidregistry = &_PyRuntime.xidregistry ;
1538 PyThread_acquire_lock(xidregistry->mutex, WAIT_LOCK);
1539 if (xidregistry->head == NULL) {
1540 _register_builtins_for_crossinterpreter_data(xidregistry);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001541 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001542 int res = _register_xidata(xidregistry, cls, getdata);
1543 PyThread_release_lock(xidregistry->mutex);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001544 return res;
1545}
1546
Eric Snow6d2cd902018-05-16 15:04:57 -04001547/* Cross-interpreter objects are looked up by exact match on the class.
1548 We can reassess this policy when we move from a global registry to a
1549 tp_* slot. */
1550
Eric Snow7f8bfc92018-01-29 18:23:44 -07001551crossinterpdatafunc
1552_PyCrossInterpreterData_Lookup(PyObject *obj)
1553{
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001554 struct _xidregistry *xidregistry = &_PyRuntime.xidregistry ;
Eric Snow7f8bfc92018-01-29 18:23:44 -07001555 PyObject *cls = PyObject_Type(obj);
1556 crossinterpdatafunc getdata = NULL;
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001557 PyThread_acquire_lock(xidregistry->mutex, WAIT_LOCK);
1558 struct _xidregitem *cur = xidregistry->head;
Eric Snow7f8bfc92018-01-29 18:23:44 -07001559 if (cur == NULL) {
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001560 _register_builtins_for_crossinterpreter_data(xidregistry);
1561 cur = xidregistry->head;
Eric Snow7f8bfc92018-01-29 18:23:44 -07001562 }
1563 for(; cur != NULL; cur = cur->next) {
1564 if (cur->cls == (PyTypeObject *)cls) {
1565 getdata = cur->getdata;
1566 break;
1567 }
1568 }
Eric Snow4e9da0d2018-02-02 21:49:49 -07001569 Py_DECREF(cls);
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001570 PyThread_release_lock(xidregistry->mutex);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001571 return getdata;
1572}
1573
1574/* cross-interpreter data for builtin types */
1575
Eric Snow6d2cd902018-05-16 15:04:57 -04001576struct _shared_bytes_data {
1577 char *bytes;
1578 Py_ssize_t len;
1579};
1580
Eric Snow7f8bfc92018-01-29 18:23:44 -07001581static PyObject *
1582_new_bytes_object(_PyCrossInterpreterData *data)
1583{
Eric Snow6d2cd902018-05-16 15:04:57 -04001584 struct _shared_bytes_data *shared = (struct _shared_bytes_data *)(data->data);
1585 return PyBytes_FromStringAndSize(shared->bytes, shared->len);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001586}
1587
1588static int
1589_bytes_shared(PyObject *obj, _PyCrossInterpreterData *data)
1590{
Eric Snow6d2cd902018-05-16 15:04:57 -04001591 struct _shared_bytes_data *shared = PyMem_NEW(struct _shared_bytes_data, 1);
1592 if (PyBytes_AsStringAndSize(obj, &shared->bytes, &shared->len) < 0) {
1593 return -1;
1594 }
1595 data->data = (void *)shared;
Eric Snow63799132018-06-01 18:45:20 -06001596 Py_INCREF(obj);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001597 data->obj = obj; // Will be "released" (decref'ed) when data released.
1598 data->new_object = _new_bytes_object;
Eric Snow6d2cd902018-05-16 15:04:57 -04001599 data->free = PyMem_Free;
1600 return 0;
1601}
1602
1603struct _shared_str_data {
1604 int kind;
1605 const void *buffer;
1606 Py_ssize_t len;
1607};
1608
1609static PyObject *
1610_new_str_object(_PyCrossInterpreterData *data)
1611{
1612 struct _shared_str_data *shared = (struct _shared_str_data *)(data->data);
1613 return PyUnicode_FromKindAndData(shared->kind, shared->buffer, shared->len);
1614}
1615
1616static int
1617_str_shared(PyObject *obj, _PyCrossInterpreterData *data)
1618{
1619 struct _shared_str_data *shared = PyMem_NEW(struct _shared_str_data, 1);
1620 shared->kind = PyUnicode_KIND(obj);
1621 shared->buffer = PyUnicode_DATA(obj);
1622 shared->len = PyUnicode_GET_LENGTH(obj) - 1;
1623 data->data = (void *)shared;
Eric Snow63799132018-06-01 18:45:20 -06001624 Py_INCREF(obj);
Eric Snow6d2cd902018-05-16 15:04:57 -04001625 data->obj = obj; // Will be "released" (decref'ed) when data released.
1626 data->new_object = _new_str_object;
1627 data->free = PyMem_Free;
1628 return 0;
1629}
1630
1631static PyObject *
1632_new_long_object(_PyCrossInterpreterData *data)
1633{
Alexey Izbyshev16f842d2019-02-12 19:06:43 +03001634 return PyLong_FromSsize_t((Py_ssize_t)(data->data));
Eric Snow6d2cd902018-05-16 15:04:57 -04001635}
1636
1637static int
1638_long_shared(PyObject *obj, _PyCrossInterpreterData *data)
1639{
Alexey Izbyshev16f842d2019-02-12 19:06:43 +03001640 /* Note that this means the size of shareable ints is bounded by
1641 * sys.maxsize. Hence on 32-bit architectures that is half the
1642 * size of maximum shareable ints on 64-bit.
1643 */
1644 Py_ssize_t value = PyLong_AsSsize_t(obj);
Eric Snow6d2cd902018-05-16 15:04:57 -04001645 if (value == -1 && PyErr_Occurred()) {
1646 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
1647 PyErr_SetString(PyExc_OverflowError, "try sending as bytes");
1648 }
1649 return -1;
1650 }
1651 data->data = (void *)value;
1652 data->obj = NULL;
1653 data->new_object = _new_long_object;
1654 data->free = NULL;
Eric Snow7f8bfc92018-01-29 18:23:44 -07001655 return 0;
1656}
1657
1658static PyObject *
1659_new_none_object(_PyCrossInterpreterData *data)
1660{
1661 // XXX Singleton refcounts are problematic across interpreters...
1662 Py_INCREF(Py_None);
1663 return Py_None;
1664}
1665
1666static int
1667_none_shared(PyObject *obj, _PyCrossInterpreterData *data)
1668{
1669 data->data = NULL;
1670 // data->obj remains NULL
1671 data->new_object = _new_none_object;
1672 data->free = NULL; // There is nothing to free.
1673 return 0;
1674}
1675
1676static void
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001677_register_builtins_for_crossinterpreter_data(struct _xidregistry *xidregistry)
Eric Snow7f8bfc92018-01-29 18:23:44 -07001678{
1679 // None
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001680 if (_register_xidata(xidregistry, (PyTypeObject *)PyObject_Type(Py_None), _none_shared) != 0) {
Eric Snow7f8bfc92018-01-29 18:23:44 -07001681 Py_FatalError("could not register None for cross-interpreter sharing");
1682 }
1683
Eric Snow6d2cd902018-05-16 15:04:57 -04001684 // int
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001685 if (_register_xidata(xidregistry, &PyLong_Type, _long_shared) != 0) {
Eric Snow6d2cd902018-05-16 15:04:57 -04001686 Py_FatalError("could not register int for cross-interpreter sharing");
1687 }
1688
Eric Snow7f8bfc92018-01-29 18:23:44 -07001689 // bytes
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001690 if (_register_xidata(xidregistry, &PyBytes_Type, _bytes_shared) != 0) {
Eric Snow7f8bfc92018-01-29 18:23:44 -07001691 Py_FatalError("could not register bytes for cross-interpreter sharing");
1692 }
Eric Snow6d2cd902018-05-16 15:04:57 -04001693
1694 // str
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001695 if (_register_xidata(xidregistry, &PyUnicode_Type, _str_shared) != 0) {
Eric Snow6d2cd902018-05-16 15:04:57 -04001696 Py_FatalError("could not register str for cross-interpreter sharing");
1697 }
Eric Snow7f8bfc92018-01-29 18:23:44 -07001698}
1699
1700
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001701#ifdef __cplusplus
1702}
1703#endif