blob: e9c4c7d8376bf72aafa79e15e213de03c0775450 [file] [log] [blame]
Guido van Rossuma027efa1997-05-05 20:56:21 +00001
2/* Thread and interpreter state structures and their interfaces */
3
4#include "Python.h"
Victor Stinnerf684d832019-03-01 03:44:13 +01005#include "pycore_coreconfig.h"
Victor Stinner621cebe2018-11-12 16:53:38 +01006#include "pycore_pymem.h"
7#include "pycore_pystate.h"
Guido van Rossuma027efa1997-05-05 20:56:21 +00008
Tim Peters84705582004-10-10 02:47:33 +00009/* --------------------------------------------------------------------------
10CAUTION
11
Victor Stinner1a7425f2013-07-07 16:25:15 +020012Always use PyMem_RawMalloc() and PyMem_RawFree() directly in this file. A
13number of these functions are advertised as safe to call when the GIL isn't
14held, and in a debug build Python redirects (e.g.) PyMem_NEW (etc) to Python's
15debugging obmalloc functions. Those aren't thread-safe (they rely on the GIL
16to avoid the expense of doing their own locking).
Tim Peters84705582004-10-10 02:47:33 +000017-------------------------------------------------------------------------- */
18
Martin v. Löwisf0473d52001-07-18 16:17:16 +000019#ifdef HAVE_DLOPEN
20#ifdef HAVE_DLFCN_H
21#include <dlfcn.h>
22#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030023#if !HAVE_DECL_RTLD_LAZY
Martin v. Löwisf0473d52001-07-18 16:17:16 +000024#define RTLD_LAZY 1
25#endif
26#endif
27
Benjamin Peterson43162b82012-04-13 11:58:27 -040028#ifdef __cplusplus
29extern "C" {
30#endif
Martin v. Löwisf0473d52001-07-18 16:17:16 +000031
Victor Stinner10c8e6a2019-04-26 01:53:18 +020032#define _PyRuntimeGILState_GetThreadState(gilstate) \
33 ((PyThreadState*)_Py_atomic_load_relaxed(&(gilstate)->tstate_current))
34#define _PyRuntimeGILState_SetThreadState(gilstate, value) \
35 _Py_atomic_store_relaxed(&(gilstate)->tstate_current, \
36 (uintptr_t)(value))
37
38/* Forward declarations */
39static PyThreadState *_PyGILState_GetThisThreadState(struct _gilstate_runtime_state *gilstate);
40static void _PyThreadState_Delete(_PyRuntimeState *runtime, PyThreadState *tstate);
41static PyThreadState *_PyThreadState_Swap(struct _gilstate_runtime_state *gilstate, PyThreadState *newts);
42
43
Victor Stinner5d39e042017-11-29 17:20:38 +010044static _PyInitError
45_PyRuntimeState_Init_impl(_PyRuntimeState *runtime)
Eric Snow2ebc5ce2017-09-07 23:51:28 -060046{
47 memset(runtime, 0, sizeof(*runtime));
Victor Stinner8a1be612016-03-14 22:07:55 +010048
Eric Snow2ebc5ce2017-09-07 23:51:28 -060049 _PyGC_Initialize(&runtime->gc);
50 _PyEval_Initialize(&runtime->ceval);
Victor Stinner6d5ee972019-03-23 12:05:43 +010051 runtime->preconfig = _PyPreConfig_INIT;
Michael W. Hudson188d4362005-06-20 16:52:57 +000052
Eric Snow2ebc5ce2017-09-07 23:51:28 -060053 runtime->gilstate.check_enabled = 1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -080054
Masayuki Yamamoto731e1892017-10-06 19:41:34 +090055 /* A TSS key must be initialized with Py_tss_NEEDS_INIT
56 in accordance with the specification. */
Victor Stinnerf7e5b562017-11-15 15:48:08 -080057 Py_tss_t initial = Py_tss_NEEDS_INIT;
58 runtime->gilstate.autoTSSkey = initial;
Guido van Rossum1d5ad901999-06-18 14:22:24 +000059
Eric Snow2ebc5ce2017-09-07 23:51:28 -060060 runtime->interpreters.mutex = PyThread_allocate_lock();
Victor Stinnerf7e5b562017-11-15 15:48:08 -080061 if (runtime->interpreters.mutex == NULL) {
62 return _Py_INIT_ERR("Can't initialize threads for interpreter");
63 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -060064 runtime->interpreters.next_id = -1;
Eric Snow7f8bfc92018-01-29 18:23:44 -070065
66 runtime->xidregistry.mutex = PyThread_allocate_lock();
67 if (runtime->xidregistry.mutex == NULL) {
68 return _Py_INIT_ERR("Can't initialize threads for cross-interpreter data registry");
69 }
70
Eric Snow8479a342019-03-08 23:44:33 -070071 // Set it to the ID of the main thread of the main interpreter.
72 runtime->main_thread = PyThread_get_thread_ident();
Eric Snow5be45a62019-03-08 22:47:07 -070073
Victor Stinnerf7e5b562017-11-15 15:48:08 -080074 return _Py_INIT_OK();
Eric Snow2ebc5ce2017-09-07 23:51:28 -060075}
Eric Snow05351c12017-09-05 21:43:08 -070076
Victor Stinner5d39e042017-11-29 17:20:38 +010077_PyInitError
78_PyRuntimeState_Init(_PyRuntimeState *runtime)
79{
80 /* Force default allocator, since _PyRuntimeState_Fini() must
81 use the same allocator than this function. */
82 PyMemAllocatorEx old_alloc;
83 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
84
85 _PyInitError err = _PyRuntimeState_Init_impl(runtime);
86
87 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
88 return err;
89}
90
Eric Snow2ebc5ce2017-09-07 23:51:28 -060091void
92_PyRuntimeState_Fini(_PyRuntimeState *runtime)
93{
Victor Stinner5d39e042017-11-29 17:20:38 +010094 /* Force the allocator used by _PyRuntimeState_Init(). */
95 PyMemAllocatorEx old_alloc;
96 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinnerccb04422017-11-16 03:20:31 -080097
Eric Snow2ebc5ce2017-09-07 23:51:28 -060098 if (runtime->interpreters.mutex != NULL) {
99 PyThread_free_lock(runtime->interpreters.mutex);
100 runtime->interpreters.mutex = NULL;
101 }
Victor Stinnerccb04422017-11-16 03:20:31 -0800102
Stéphane Wirtel943395f2019-03-19 11:51:32 +0100103 if (runtime->xidregistry.mutex != NULL) {
104 PyThread_free_lock(runtime->xidregistry.mutex);
105 runtime->xidregistry.mutex = NULL;
106 }
107
Victor Stinner6d5ee972019-03-23 12:05:43 +0100108 _PyPreConfig_Clear(&runtime->preconfig);
109
Victor Stinnerccb04422017-11-16 03:20:31 -0800110 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600111}
112
Eric Snow8479a342019-03-08 23:44:33 -0700113/* This function is called from PyOS_AfterFork_Child to ensure that
114 * newly created child processes do not share locks with the parent.
115 */
116
117void
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200118_PyRuntimeState_ReInitThreads(_PyRuntimeState *runtime)
Eric Snow8479a342019-03-08 23:44:33 -0700119{
120 // This was initially set in _PyRuntimeState_Init().
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200121 runtime->main_thread = PyThread_get_thread_ident();
Eric Snow8479a342019-03-08 23:44:33 -0700122
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200123 /* Force default allocator, since _PyRuntimeState_Fini() must
124 use the same allocator than this function. */
125 PyMemAllocatorEx old_alloc;
126 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
127
128 runtime->interpreters.mutex = PyThread_allocate_lock();
129 runtime->interpreters.main->id_mutex = PyThread_allocate_lock();
130 runtime->xidregistry.mutex = PyThread_allocate_lock();
131
132 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
133
134 if (runtime->interpreters.mutex == NULL) {
Eric Snow8479a342019-03-08 23:44:33 -0700135 Py_FatalError("Can't initialize lock for runtime interpreters");
136 }
137
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200138 if (runtime->interpreters.main->id_mutex == NULL) {
Eric Snow8479a342019-03-08 23:44:33 -0700139 Py_FatalError("Can't initialize ID lock for main interpreter");
140 }
141
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200142 if (runtime->xidregistry.mutex == NULL) {
Eric Snow8479a342019-03-08 23:44:33 -0700143 Py_FatalError("Can't initialize lock for cross-interpreter data registry");
144 }
145}
146
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200147#define HEAD_LOCK(runtime) \
148 PyThread_acquire_lock((runtime)->interpreters.mutex, WAIT_LOCK)
149#define HEAD_UNLOCK(runtime) \
150 PyThread_release_lock((runtime)->interpreters.mutex)
Eric Snow05351c12017-09-05 21:43:08 -0700151
Victor Stinner8bb32302019-04-24 16:47:40 +0200152/* Forward declaration */
153static void _PyGILState_NoteThreadState(
154 struct _gilstate_runtime_state *gilstate, PyThreadState* tstate);
Michael W. Hudson188d4362005-06-20 16:52:57 +0000155
Victor Stinnera7368ac2017-11-15 18:11:45 -0800156_PyInitError
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600157_PyInterpreterState_Enable(_PyRuntimeState *runtime)
Eric Snowe3774162017-05-22 19:46:40 -0700158{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200159 struct pyinterpreters *interpreters = &runtime->interpreters;
160 interpreters->next_id = 0;
Victor Stinner5d926472018-03-06 14:31:37 +0100161
162 /* Py_Finalize() calls _PyRuntimeState_Fini() which clears the mutex.
163 Create a new mutex if needed. */
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200164 if (interpreters->mutex == NULL) {
Victor Stinner5d926472018-03-06 14:31:37 +0100165 /* Force default allocator, since _PyRuntimeState_Fini() must
166 use the same allocator than this function. */
167 PyMemAllocatorEx old_alloc;
168 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
169
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200170 interpreters->mutex = PyThread_allocate_lock();
Victor Stinner5d926472018-03-06 14:31:37 +0100171
172 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
173
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200174 if (interpreters->mutex == NULL) {
Victor Stinnera7368ac2017-11-15 18:11:45 -0800175 return _Py_INIT_ERR("Can't initialize threads for interpreter");
176 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600177 }
Victor Stinner5d926472018-03-06 14:31:37 +0100178
Victor Stinnera7368ac2017-11-15 18:11:45 -0800179 return _Py_INIT_OK();
Eric Snowe3774162017-05-22 19:46:40 -0700180}
Guido van Rossuma027efa1997-05-05 20:56:21 +0000181
182PyInterpreterState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000183PyInterpreterState_New(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000184{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200185 PyInterpreterState *interp = PyMem_RawMalloc(sizeof(PyInterpreterState));
Victor Stinnerd4341102017-11-23 00:12:09 +0100186 if (interp == NULL) {
187 return NULL;
188 }
189
Eric Snow5be45a62019-03-08 22:47:07 -0700190 memset(interp, 0, sizeof(*interp));
Eric Snow4c6955e2018-02-16 18:53:40 -0700191 interp->id_refcount = -1;
Victor Stinnerd4341102017-11-23 00:12:09 +0100192 interp->check_interval = 100;
Victor Stinnerd4341102017-11-23 00:12:09 +0100193 interp->core_config = _PyCoreConfig_INIT;
Victor Stinnerd4341102017-11-23 00:12:09 +0100194 interp->eval_frame = _PyEval_EvalFrameDefault;
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000195#ifdef HAVE_DLOPEN
Serhiy Storchakac2f7d872016-05-04 09:44:44 +0300196#if HAVE_DECL_RTLD_NOW
Victor Stinnerd4341102017-11-23 00:12:09 +0100197 interp->dlopenflags = RTLD_NOW;
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000198#else
Victor Stinnerd4341102017-11-23 00:12:09 +0100199 interp->dlopenflags = RTLD_LAZY;
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000200#endif
201#endif
Guido van Rossum25ce5661997-08-02 03:10:38 +0000202
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200203 _PyRuntimeState *runtime = &_PyRuntime;
204 struct pyinterpreters *interpreters = &runtime->interpreters;
205
206 HEAD_LOCK(runtime);
207 if (interpreters->next_id < 0) {
Victor Stinnerd4341102017-11-23 00:12:09 +0100208 /* overflow or Py_Initialize() not called! */
209 PyErr_SetString(PyExc_RuntimeError,
210 "failed to get an interpreter ID");
Pablo Galindo95d630e2018-08-31 22:49:29 +0100211 PyMem_RawFree(interp);
Victor Stinnerd4341102017-11-23 00:12:09 +0100212 interp = NULL;
Victor Stinnerd4341102017-11-23 00:12:09 +0100213 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200214 else {
215 interp->id = interpreters->next_id;
216 interpreters->next_id += 1;
217 interp->next = interpreters->head;
218 if (interpreters->main == NULL) {
219 interpreters->main = interp;
220 }
221 interpreters->head = interp;
222 }
223 HEAD_UNLOCK(runtime);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000224
Pablo Galindo95d630e2018-08-31 22:49:29 +0100225 if (interp == NULL) {
226 return NULL;
227 }
228
Yury Selivanovf23746a2018-01-22 19:11:18 -0500229 interp->tstate_next_unique_id = 0;
230
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 return interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000232}
233
234
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200235static void
236_PyInterpreterState_Clear(_PyRuntimeState *runtime, PyInterpreterState *interp)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000237{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200238 HEAD_LOCK(runtime);
239 for (PyThreadState *p = interp->tstate_head; p != NULL; p = p->next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 PyThreadState_Clear(p);
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200241 }
242 HEAD_UNLOCK(runtime);
Victor Stinnerda273412017-12-15 01:46:02 +0100243 _PyCoreConfig_Clear(&interp->core_config);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 Py_CLEAR(interp->codec_search_path);
245 Py_CLEAR(interp->codec_search_cache);
246 Py_CLEAR(interp->codec_error_registry);
Eric Snow93c92f72017-09-13 23:46:04 -0700247 Py_CLEAR(interp->modules);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 Py_CLEAR(interp->modules_by_index);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 Py_CLEAR(interp->sysdict);
250 Py_CLEAR(interp->builtins);
Serhiy Storchaka87a5c512014-02-10 18:21:34 +0200251 Py_CLEAR(interp->builtins_copy);
Brett Cannonfd074152012-04-14 14:10:13 -0400252 Py_CLEAR(interp->importlib);
Serhiy Storchaka133138a2016-08-02 22:51:21 +0300253 Py_CLEAR(interp->import_func);
Eric Snowd2fdd1f2019-03-15 17:47:43 -0600254 Py_CLEAR(interp->dict);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200255#ifdef HAVE_FORK
256 Py_CLEAR(interp->before_forkers);
257 Py_CLEAR(interp->after_forkers_parent);
258 Py_CLEAR(interp->after_forkers_child);
259#endif
Eric Snow5be45a62019-03-08 22:47:07 -0700260 // XXX Once we have one allocator per interpreter (i.e.
261 // per-interpreter GC) we must ensure that all of the interpreter's
262 // objects have been cleaned up at the point.
Guido van Rossum25ce5661997-08-02 03:10:38 +0000263}
264
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200265void
266PyInterpreterState_Clear(PyInterpreterState *interp)
267{
268 _PyInterpreterState_Clear(&_PyRuntime, interp);
269}
270
Guido van Rossum25ce5661997-08-02 03:10:38 +0000271
272static void
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200273zapthreads(_PyRuntimeState *runtime, PyInterpreterState *interp)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000274{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 PyThreadState *p;
276 /* No need to lock the mutex here because this should only happen
277 when the threads are all really dead (XXX famous last words). */
278 while ((p = interp->tstate_head) != NULL) {
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200279 _PyThreadState_Delete(runtime, p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000281}
282
283
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200284static void
285_PyInterpreterState_Delete(_PyRuntimeState *runtime,
286 PyInterpreterState *interp)
287{
288 struct pyinterpreters *interpreters = &runtime->interpreters;
289 zapthreads(runtime, interp);
290 HEAD_LOCK(runtime);
291 PyInterpreterState **p;
292 for (p = &interpreters->head; ; p = &(*p)->next) {
293 if (*p == NULL) {
294 Py_FatalError("PyInterpreterState_Delete: invalid interp");
295 }
296 if (*p == interp) {
297 break;
298 }
299 }
300 if (interp->tstate_head != NULL) {
301 Py_FatalError("PyInterpreterState_Delete: remaining threads");
302 }
303 *p = interp->next;
304 if (interpreters->main == interp) {
305 interpreters->main = NULL;
306 if (interpreters->head != NULL) {
307 Py_FatalError("PyInterpreterState_Delete: remaining subinterpreters");
308 }
309 }
310 HEAD_UNLOCK(runtime);
311 if (interp->id_mutex != NULL) {
312 PyThread_free_lock(interp->id_mutex);
313 }
314 PyMem_RawFree(interp);
315}
316
317
Guido van Rossum25ce5661997-08-02 03:10:38 +0000318void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000319PyInterpreterState_Delete(PyInterpreterState *interp)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000320{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200321 _PyInterpreterState_Delete(&_PyRuntime, interp);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000322}
323
324
Eric Snow59032962018-09-14 14:17:20 -0700325/*
326 * Delete all interpreter states except the main interpreter. If there
327 * is a current interpreter state, it *must* be the main interpreter.
328 */
329void
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200330_PyInterpreterState_DeleteExceptMain(_PyRuntimeState *runtime)
Eric Snow59032962018-09-14 14:17:20 -0700331{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200332 struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200333 struct pyinterpreters *interpreters = &runtime->interpreters;
334
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200335 PyThreadState *tstate = _PyThreadState_Swap(gilstate, NULL);
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200336 if (tstate != NULL && tstate->interp != interpreters->main) {
Eric Snow59032962018-09-14 14:17:20 -0700337 Py_FatalError("PyInterpreterState_DeleteExceptMain: not main interpreter");
338 }
339
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200340 HEAD_LOCK(runtime);
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200341 PyInterpreterState *interp = interpreters->head;
342 interpreters->head = NULL;
Stéphane Wirtelb5409da2019-02-20 15:27:22 +0100343 while (interp != NULL) {
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200344 if (interp == interpreters->main) {
345 interpreters->main->next = NULL;
346 interpreters->head = interp;
Stéphane Wirtelb5409da2019-02-20 15:27:22 +0100347 interp = interp->next;
Eric Snow59032962018-09-14 14:17:20 -0700348 continue;
349 }
350
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200351 _PyInterpreterState_Clear(runtime, interp); // XXX must activate?
352 zapthreads(runtime, interp);
Eric Snow59032962018-09-14 14:17:20 -0700353 if (interp->id_mutex != NULL) {
354 PyThread_free_lock(interp->id_mutex);
355 }
Stéphane Wirtelb5409da2019-02-20 15:27:22 +0100356 PyInterpreterState *prev_interp = interp;
357 interp = interp->next;
358 PyMem_RawFree(prev_interp);
Eric Snow59032962018-09-14 14:17:20 -0700359 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200360 HEAD_UNLOCK(runtime);
Eric Snow59032962018-09-14 14:17:20 -0700361
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200362 if (interpreters->head == NULL) {
Eric Snow59032962018-09-14 14:17:20 -0700363 Py_FatalError("PyInterpreterState_DeleteExceptMain: missing main");
364 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200365 _PyThreadState_Swap(gilstate, tstate);
Eric Snow59032962018-09-14 14:17:20 -0700366}
367
368
Victor Stinnercaba55b2018-08-03 15:33:52 +0200369PyInterpreterState *
370_PyInterpreterState_Get(void)
371{
Victor Stinner50b48572018-11-01 01:51:40 +0100372 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnercaba55b2018-08-03 15:33:52 +0200373 if (tstate == NULL) {
374 Py_FatalError("_PyInterpreterState_Get(): no current thread state");
375 }
376 PyInterpreterState *interp = tstate->interp;
377 if (interp == NULL) {
378 Py_FatalError("_PyInterpreterState_Get(): no current interpreter");
379 }
380 return interp;
381}
382
383
Eric Snowe3774162017-05-22 19:46:40 -0700384int64_t
385PyInterpreterState_GetID(PyInterpreterState *interp)
386{
387 if (interp == NULL) {
388 PyErr_SetString(PyExc_RuntimeError, "no interpreter provided");
389 return -1;
390 }
391 return interp->id;
392}
393
394
Eric Snow5be45a62019-03-08 22:47:07 -0700395static PyInterpreterState *
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200396interp_look_up_id(_PyRuntimeState *runtime, PY_INT64_T requested_id)
Eric Snowb05b7112019-03-01 12:35:10 -0700397{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200398 PyInterpreterState *interp = runtime->interpreters.head;
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100399 while (interp != NULL) {
400 PY_INT64_T id = PyInterpreterState_GetID(interp);
Eric Snow5be45a62019-03-08 22:47:07 -0700401 if (id < 0) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100402 return NULL;
Eric Snow5be45a62019-03-08 22:47:07 -0700403 }
404 if (requested_id == id) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100405 return interp;
Eric Snow5be45a62019-03-08 22:47:07 -0700406 }
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100407 interp = PyInterpreterState_Next(interp);
Eric Snowb05b7112019-03-01 12:35:10 -0700408 }
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100409 return NULL;
Eric Snowb05b7112019-03-01 12:35:10 -0700410}
411
Eric Snow5be45a62019-03-08 22:47:07 -0700412PyInterpreterState *
413_PyInterpreterState_LookUpID(PY_INT64_T requested_id)
414{
415 PyInterpreterState *interp = NULL;
416 if (requested_id >= 0) {
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200417 _PyRuntimeState *runtime = &_PyRuntime;
418 HEAD_LOCK(runtime);
419 interp = interp_look_up_id(runtime, requested_id);
420 HEAD_UNLOCK(runtime);
Eric Snow5be45a62019-03-08 22:47:07 -0700421 }
422 if (interp == NULL && !PyErr_Occurred()) {
423 PyErr_Format(PyExc_RuntimeError,
424 "unrecognized interpreter ID %lld", requested_id);
425 }
426 return interp;
427}
428
Eric Snow4c6955e2018-02-16 18:53:40 -0700429
430int
431_PyInterpreterState_IDInitref(PyInterpreterState *interp)
432{
433 if (interp->id_mutex != NULL) {
434 return 0;
435 }
436 interp->id_mutex = PyThread_allocate_lock();
437 if (interp->id_mutex == NULL) {
438 PyErr_SetString(PyExc_RuntimeError,
439 "failed to create init interpreter ID mutex");
440 return -1;
441 }
442 interp->id_refcount = 0;
443 return 0;
444}
445
446
447void
448_PyInterpreterState_IDIncref(PyInterpreterState *interp)
449{
450 if (interp->id_mutex == NULL) {
451 return;
452 }
453 PyThread_acquire_lock(interp->id_mutex, WAIT_LOCK);
454 interp->id_refcount += 1;
455 PyThread_release_lock(interp->id_mutex);
456}
457
458
459void
460_PyInterpreterState_IDDecref(PyInterpreterState *interp)
461{
462 if (interp->id_mutex == NULL) {
463 return;
464 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200465 struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
Eric Snow4c6955e2018-02-16 18:53:40 -0700466 PyThread_acquire_lock(interp->id_mutex, WAIT_LOCK);
467 assert(interp->id_refcount != 0);
468 interp->id_refcount -= 1;
469 int64_t refcount = interp->id_refcount;
470 PyThread_release_lock(interp->id_mutex);
471
Eric Snowc11183c2019-03-15 16:35:46 -0600472 if (refcount == 0 && interp->requires_idref) {
Eric Snowf53d9f22018-02-20 16:30:17 -0700473 // XXX Using the "head" thread isn't strictly correct.
474 PyThreadState *tstate = PyInterpreterState_ThreadHead(interp);
475 // XXX Possible GILState issues?
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200476 PyThreadState *save_tstate = _PyThreadState_Swap(gilstate, tstate);
Eric Snow4c6955e2018-02-16 18:53:40 -0700477 Py_EndInterpreter(tstate);
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200478 _PyThreadState_Swap(gilstate, save_tstate);
Eric Snow4c6955e2018-02-16 18:53:40 -0700479 }
480}
481
Eric Snowc11183c2019-03-15 16:35:46 -0600482int
483_PyInterpreterState_RequiresIDRef(PyInterpreterState *interp)
484{
485 return interp->requires_idref;
486}
487
488void
489_PyInterpreterState_RequireIDRef(PyInterpreterState *interp, int required)
490{
491 interp->requires_idref = required ? 1 : 0;
492}
493
Eric Snowbe3b2952019-02-23 11:35:52 -0700494_PyCoreConfig *
495_PyInterpreterState_GetCoreConfig(PyInterpreterState *interp)
496{
497 return &interp->core_config;
498}
499
Eric Snowc11183c2019-03-15 16:35:46 -0600500PyObject *
501_PyInterpreterState_GetMainModule(PyInterpreterState *interp)
502{
503 if (interp->modules == NULL) {
504 PyErr_SetString(PyExc_RuntimeError, "interpreter not initialized");
505 return NULL;
506 }
507 return PyMapping_GetItemString(interp->modules, "__main__");
508}
509
Eric Snowd2fdd1f2019-03-15 17:47:43 -0600510PyObject *
511PyInterpreterState_GetDict(PyInterpreterState *interp)
512{
513 if (interp->dict == NULL) {
514 interp->dict = PyDict_New();
515 if (interp->dict == NULL) {
516 PyErr_Clear();
517 }
518 }
519 /* Returning NULL means no per-interpreter dict is available. */
520 return interp->dict;
521}
522
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000523/* Default implementation for _PyThreadState_GetFrame */
524static struct _frame *
525threadstate_getframe(PyThreadState *self)
526{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 return self->frame;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000528}
529
Victor Stinner45b9be52010-03-03 23:28:07 +0000530static PyThreadState *
531new_threadstate(PyInterpreterState *interp, int init)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000532{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200533 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner1a7425f2013-07-07 16:25:15 +0200534 PyThreadState *tstate = (PyThreadState *)PyMem_RawMalloc(sizeof(PyThreadState));
Victor Stinner8bb32302019-04-24 16:47:40 +0200535 if (tstate == NULL) {
536 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000538
Victor Stinner8bb32302019-04-24 16:47:40 +0200539 if (_PyThreadState_GetFrame == NULL) {
540 _PyThreadState_GetFrame = threadstate_getframe;
541 }
542
543 tstate->interp = interp;
544
545 tstate->frame = NULL;
546 tstate->recursion_depth = 0;
547 tstate->overflowed = 0;
548 tstate->recursion_critical = 0;
549 tstate->stackcheck_counter = 0;
550 tstate->tracing = 0;
551 tstate->use_tracing = 0;
552 tstate->gilstate_counter = 0;
553 tstate->async_exc = NULL;
554 tstate->thread_id = PyThread_get_thread_ident();
555
556 tstate->dict = NULL;
557
558 tstate->curexc_type = NULL;
559 tstate->curexc_value = NULL;
560 tstate->curexc_traceback = NULL;
561
562 tstate->exc_state.exc_type = NULL;
563 tstate->exc_state.exc_value = NULL;
564 tstate->exc_state.exc_traceback = NULL;
565 tstate->exc_state.previous_item = NULL;
566 tstate->exc_info = &tstate->exc_state;
567
568 tstate->c_profilefunc = NULL;
569 tstate->c_tracefunc = NULL;
570 tstate->c_profileobj = NULL;
571 tstate->c_traceobj = NULL;
572
573 tstate->trash_delete_nesting = 0;
574 tstate->trash_delete_later = NULL;
575 tstate->on_delete = NULL;
576 tstate->on_delete_data = NULL;
577
578 tstate->coroutine_origin_tracking_depth = 0;
579
580 tstate->coroutine_wrapper = NULL;
581 tstate->in_coroutine_wrapper = 0;
582
583 tstate->async_gen_firstiter = NULL;
584 tstate->async_gen_finalizer = NULL;
585
586 tstate->context = NULL;
587 tstate->context_ver = 1;
588
589 tstate->id = ++interp->tstate_next_unique_id;
590
591 if (init) {
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200592 _PyThreadState_Init(runtime, tstate);
Victor Stinner8bb32302019-04-24 16:47:40 +0200593 }
594
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200595 HEAD_LOCK(runtime);
Victor Stinner8bb32302019-04-24 16:47:40 +0200596 tstate->prev = NULL;
597 tstate->next = interp->tstate_head;
598 if (tstate->next)
599 tstate->next->prev = tstate;
600 interp->tstate_head = tstate;
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200601 HEAD_UNLOCK(runtime);
Victor Stinner8bb32302019-04-24 16:47:40 +0200602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 return tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000604}
605
Victor Stinner45b9be52010-03-03 23:28:07 +0000606PyThreadState *
607PyThreadState_New(PyInterpreterState *interp)
608{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000609 return new_threadstate(interp, 1);
Victor Stinner45b9be52010-03-03 23:28:07 +0000610}
611
612PyThreadState *
613_PyThreadState_Prealloc(PyInterpreterState *interp)
614{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 return new_threadstate(interp, 0);
Victor Stinner45b9be52010-03-03 23:28:07 +0000616}
617
618void
Victor Stinner8bb32302019-04-24 16:47:40 +0200619_PyThreadState_Init(_PyRuntimeState *runtime, PyThreadState *tstate)
Victor Stinner45b9be52010-03-03 23:28:07 +0000620{
Victor Stinner8bb32302019-04-24 16:47:40 +0200621 _PyGILState_NoteThreadState(&runtime->gilstate, tstate);
Victor Stinner45b9be52010-03-03 23:28:07 +0000622}
623
Martin v. Löwis1a214512008-06-11 05:26:20 +0000624PyObject*
Martin v. Löwis7800f752012-06-22 12:20:55 +0200625PyState_FindModule(struct PyModuleDef* module)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000626{
Martin v. Löwis7800f752012-06-22 12:20:55 +0200627 Py_ssize_t index = module->m_base.m_index;
Victor Stinner9204fb82018-10-30 15:13:17 +0100628 PyInterpreterState *state = _PyInterpreterState_GET_UNSAFE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 PyObject *res;
Nick Coghland5cacbb2015-05-23 22:24:10 +1000630 if (module->m_slots) {
631 return NULL;
632 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 if (index == 0)
634 return NULL;
635 if (state->modules_by_index == NULL)
636 return NULL;
Antoine Pitrou75506e82012-08-20 19:30:46 +0200637 if (index >= PyList_GET_SIZE(state->modules_by_index))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 return NULL;
639 res = PyList_GET_ITEM(state->modules_by_index, index);
640 return res==Py_None ? NULL : res;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000641}
642
643int
644_PyState_AddModule(PyObject* module, struct PyModuleDef* def)
645{
Nick Coghland5cacbb2015-05-23 22:24:10 +1000646 PyInterpreterState *state;
Berker Peksag4b7b5652016-08-22 18:05:56 +0300647 if (!def) {
648 assert(PyErr_Occurred());
649 return -1;
650 }
Nick Coghland5cacbb2015-05-23 22:24:10 +1000651 if (def->m_slots) {
652 PyErr_SetString(PyExc_SystemError,
653 "PyState_AddModule called on module with slots");
654 return -1;
655 }
Victor Stinner9204fb82018-10-30 15:13:17 +0100656 state = _PyInterpreterState_GET_UNSAFE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 if (!state->modules_by_index) {
658 state->modules_by_index = PyList_New(0);
659 if (!state->modules_by_index)
660 return -1;
661 }
662 while(PyList_GET_SIZE(state->modules_by_index) <= def->m_base.m_index)
663 if (PyList_Append(state->modules_by_index, Py_None) < 0)
664 return -1;
665 Py_INCREF(module);
666 return PyList_SetItem(state->modules_by_index,
667 def->m_base.m_index, module);
Martin v. Löwis1a214512008-06-11 05:26:20 +0000668}
Guido van Rossuma027efa1997-05-05 20:56:21 +0000669
Martin v. Löwis7800f752012-06-22 12:20:55 +0200670int
671PyState_AddModule(PyObject* module, struct PyModuleDef* def)
672{
673 Py_ssize_t index;
Victor Stinner9204fb82018-10-30 15:13:17 +0100674 PyInterpreterState *state = _PyInterpreterState_GET_UNSAFE();
Martin v. Löwis7800f752012-06-22 12:20:55 +0200675 if (!def) {
676 Py_FatalError("PyState_AddModule: Module Definition is NULL");
677 return -1;
678 }
679 index = def->m_base.m_index;
680 if (state->modules_by_index) {
681 if(PyList_GET_SIZE(state->modules_by_index) >= index) {
682 if(module == PyList_GET_ITEM(state->modules_by_index, index)) {
683 Py_FatalError("PyState_AddModule: Module already added!");
684 return -1;
685 }
686 }
687 }
688 return _PyState_AddModule(module, def);
689}
690
691int
692PyState_RemoveModule(struct PyModuleDef* def)
693{
Nick Coghland5cacbb2015-05-23 22:24:10 +1000694 PyInterpreterState *state;
Martin v. Löwis7800f752012-06-22 12:20:55 +0200695 Py_ssize_t index = def->m_base.m_index;
Nick Coghland5cacbb2015-05-23 22:24:10 +1000696 if (def->m_slots) {
697 PyErr_SetString(PyExc_SystemError,
698 "PyState_RemoveModule called on module with slots");
699 return -1;
700 }
Victor Stinner9204fb82018-10-30 15:13:17 +0100701 state = _PyInterpreterState_GET_UNSAFE();
Martin v. Löwis7800f752012-06-22 12:20:55 +0200702 if (index == 0) {
703 Py_FatalError("PyState_RemoveModule: Module index invalid.");
704 return -1;
705 }
706 if (state->modules_by_index == NULL) {
707 Py_FatalError("PyState_RemoveModule: Interpreters module-list not acessible.");
708 return -1;
709 }
710 if (index > PyList_GET_SIZE(state->modules_by_index)) {
711 Py_FatalError("PyState_RemoveModule: Module index out of bounds.");
712 return -1;
713 }
Zackery Spytz2a893432018-12-05 00:14:00 -0700714 Py_INCREF(Py_None);
Martin v. Löwis7800f752012-06-22 12:20:55 +0200715 return PyList_SetItem(state->modules_by_index, index, Py_None);
716}
717
Antoine Pitrou40322e62013-08-11 00:30:09 +0200718/* used by import.c:PyImport_Cleanup */
719void
720_PyState_ClearModules(void)
721{
Victor Stinner9204fb82018-10-30 15:13:17 +0100722 PyInterpreterState *state = _PyInterpreterState_GET_UNSAFE();
Antoine Pitrou40322e62013-08-11 00:30:09 +0200723 if (state->modules_by_index) {
724 Py_ssize_t i;
725 for (i = 0; i < PyList_GET_SIZE(state->modules_by_index); i++) {
726 PyObject *m = PyList_GET_ITEM(state->modules_by_index, i);
727 if (PyModule_Check(m)) {
728 /* cleanup the saved copy of module dicts */
729 PyModuleDef *md = PyModule_GetDef(m);
730 if (md)
731 Py_CLEAR(md->m_base.m_copy);
732 }
733 }
734 /* Setting modules_by_index to NULL could be dangerous, so we
735 clear the list instead. */
736 if (PyList_SetSlice(state->modules_by_index,
737 0, PyList_GET_SIZE(state->modules_by_index),
738 NULL))
739 PyErr_WriteUnraisable(state->modules_by_index);
740 }
741}
742
Guido van Rossuma027efa1997-05-05 20:56:21 +0000743void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000744PyThreadState_Clear(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000745{
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200746 int verbose = tstate->interp->core_config.verbose;
747
748 if (verbose && tstate->frame != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 fprintf(stderr,
750 "PyThreadState_Clear: warning: thread still has a frame\n");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 Py_CLEAR(tstate->frame);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000753
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 Py_CLEAR(tstate->dict);
755 Py_CLEAR(tstate->async_exc);
Guido van Rossumede04391998-04-10 20:18:25 +0000756
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 Py_CLEAR(tstate->curexc_type);
758 Py_CLEAR(tstate->curexc_value);
759 Py_CLEAR(tstate->curexc_traceback);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000760
Mark Shannonae3087c2017-10-22 22:41:51 +0100761 Py_CLEAR(tstate->exc_state.exc_type);
762 Py_CLEAR(tstate->exc_state.exc_value);
763 Py_CLEAR(tstate->exc_state.exc_traceback);
Serhiy Storchakabdf42982017-10-26 16:59:40 +0300764
Mark Shannonae3087c2017-10-22 22:41:51 +0100765 /* The stack of exception states should contain just this thread. */
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200766 if (verbose && tstate->exc_info != &tstate->exc_state) {
Mark Shannonae3087c2017-10-22 22:41:51 +0100767 fprintf(stderr,
768 "PyThreadState_Clear: warning: thread still has a generator\n");
769 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 tstate->c_profilefunc = NULL;
772 tstate->c_tracefunc = NULL;
773 Py_CLEAR(tstate->c_profileobj);
774 Py_CLEAR(tstate->c_traceobj);
Yury Selivanov75445082015-05-11 22:57:16 -0400775
776 Py_CLEAR(tstate->coroutine_wrapper);
Yury Selivanoveb636452016-09-08 22:01:51 -0700777 Py_CLEAR(tstate->async_gen_firstiter);
778 Py_CLEAR(tstate->async_gen_finalizer);
Yury Selivanovf23746a2018-01-22 19:11:18 -0500779
780 Py_CLEAR(tstate->context);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000781}
782
783
Guido van Rossum29757862001-01-23 01:46:06 +0000784/* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
785static void
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200786tstate_delete_common(_PyRuntimeState *runtime, PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000787{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200788 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 Py_FatalError("PyThreadState_Delete: NULL tstate");
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200790 }
791 PyInterpreterState *interp = tstate->interp;
792 if (interp == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 Py_FatalError("PyThreadState_Delete: NULL interp");
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200794 }
795 HEAD_LOCK(runtime);
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200796 if (tstate->prev)
797 tstate->prev->next = tstate->next;
798 else
799 interp->tstate_head = tstate->next;
800 if (tstate->next)
801 tstate->next->prev = tstate->prev;
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200802 HEAD_UNLOCK(runtime);
Antoine Pitrou7b476992013-09-07 23:38:37 +0200803 if (tstate->on_delete != NULL) {
804 tstate->on_delete(tstate->on_delete_data);
805 }
Victor Stinner1a7425f2013-07-07 16:25:15 +0200806 PyMem_RawFree(tstate);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000807}
808
809
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200810static void
811_PyThreadState_Delete(_PyRuntimeState *runtime, PyThreadState *tstate)
Guido van Rossum29757862001-01-23 01:46:06 +0000812{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200813 struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
814 if (tstate == _PyRuntimeGILState_GetThreadState(gilstate)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 Py_FatalError("PyThreadState_Delete: tstate is still current");
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600816 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200817 if (gilstate->autoInterpreterState &&
818 PyThread_tss_get(&gilstate->autoTSSkey) == tstate)
819 {
820 PyThread_tss_set(&gilstate->autoTSSkey, NULL);
821 }
822 tstate_delete_common(runtime, tstate);
Guido van Rossum29757862001-01-23 01:46:06 +0000823}
824
825
Guido van Rossum29757862001-01-23 01:46:06 +0000826void
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200827PyThreadState_Delete(PyThreadState *tstate)
Guido van Rossum29757862001-01-23 01:46:06 +0000828{
Victor Stinner99e69d42019-04-26 05:48:51 +0200829 _PyThreadState_Delete(&_PyRuntime, tstate);
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200830}
831
832
833static void
834_PyThreadState_DeleteCurrent(_PyRuntimeState *runtime)
835{
836 struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
837 PyThreadState *tstate = _PyRuntimeGILState_GetThreadState(gilstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 if (tstate == NULL)
839 Py_FatalError(
840 "PyThreadState_DeleteCurrent: no current tstate");
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200841 tstate_delete_common(runtime, tstate);
842 if (gilstate->autoInterpreterState &&
843 PyThread_tss_get(&gilstate->autoTSSkey) == tstate)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600844 {
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200845 PyThread_tss_set(&gilstate->autoTSSkey, NULL);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600846 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200847 _PyRuntimeGILState_SetThreadState(gilstate, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 PyEval_ReleaseLock();
Guido van Rossum29757862001-01-23 01:46:06 +0000849}
Guido van Rossum29757862001-01-23 01:46:06 +0000850
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200851void
852PyThreadState_DeleteCurrent()
853{
854 _PyThreadState_DeleteCurrent(&_PyRuntime);
855}
856
Guido van Rossum29757862001-01-23 01:46:06 +0000857
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200858/*
859 * Delete all thread states except the one passed as argument.
860 * Note that, if there is a current thread state, it *must* be the one
861 * passed as argument. Also, this won't touch any other interpreters
862 * than the current one, since we don't know which thread state should
863 * be kept in those other interpreteres.
864 */
865void
866_PyThreadState_DeleteExcept(PyThreadState *tstate)
867{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200868 _PyRuntimeState *runtime = &_PyRuntime;
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200869 PyInterpreterState *interp = tstate->interp;
870 PyThreadState *p, *next, *garbage;
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200871 HEAD_LOCK(runtime);
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200872 /* Remove all thread states, except tstate, from the linked list of
873 thread states. This will allow calling PyThreadState_Clear()
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200874 without holding the lock. */
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200875 garbage = interp->tstate_head;
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200876 if (garbage == tstate)
877 garbage = tstate->next;
878 if (tstate->prev)
879 tstate->prev->next = tstate->next;
880 if (tstate->next)
881 tstate->next->prev = tstate->prev;
882 tstate->prev = tstate->next = NULL;
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200883 interp->tstate_head = tstate;
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200884 HEAD_UNLOCK(runtime);
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200885 /* Clear and deallocate all stale thread states. Even if this
886 executes Python code, we should be safe since it executes
887 in the current thread, not one of the stale threads. */
888 for (p = garbage; p; p = next) {
889 next = p->next;
890 PyThreadState_Clear(p);
Victor Stinner1a7425f2013-07-07 16:25:15 +0200891 PyMem_RawFree(p);
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200892 }
893}
894
895
Guido van Rossuma027efa1997-05-05 20:56:21 +0000896PyThreadState *
Victor Stinnerbfd316e2016-01-20 11:12:38 +0100897_PyThreadState_UncheckedGet(void)
898{
Victor Stinner50b48572018-11-01 01:51:40 +0100899 return _PyThreadState_GET();
Victor Stinnerbfd316e2016-01-20 11:12:38 +0100900}
901
902
903PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000904PyThreadState_Get(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000905{
Victor Stinner50b48572018-11-01 01:51:40 +0100906 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 if (tstate == NULL)
908 Py_FatalError("PyThreadState_Get: no current thread");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000909
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 return tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000911}
912
913
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200914static PyThreadState *
915_PyThreadState_Swap(struct _gilstate_runtime_state *gilstate, PyThreadState *newts)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000916{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200917 PyThreadState *oldts = _PyRuntimeGILState_GetThreadState(gilstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000918
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200919 _PyRuntimeGILState_SetThreadState(gilstate, newts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 /* It should not be possible for more than one thread state
921 to be used for a thread. Check this the best we can in debug
922 builds.
923 */
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200924#if defined(Py_DEBUG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 if (newts) {
926 /* This can be called from PyEval_RestoreThread(). Similar
927 to it, we need to ensure errno doesn't change.
928 */
929 int err = errno;
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200930 PyThreadState *check = _PyGILState_GetThisThreadState(gilstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 if (check && check->interp == newts->interp && check != newts)
932 Py_FatalError("Invalid thread state for this thread");
933 errno = err;
934 }
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000935#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 return oldts;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000937}
Guido van Rossumede04391998-04-10 20:18:25 +0000938
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200939PyThreadState *
940PyThreadState_Swap(PyThreadState *newts)
941{
942 return _PyThreadState_Swap(&_PyRuntime.gilstate, newts);
943}
944
Guido van Rossumede04391998-04-10 20:18:25 +0000945/* An extension mechanism to store arbitrary additional per-thread state.
946 PyThreadState_GetDict() returns a dictionary that can be used to hold such
947 state; the caller should pick a unique key and store its state there. If
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000948 PyThreadState_GetDict() returns NULL, an exception has *not* been raised
949 and the caller should assume no per-thread state is available. */
Guido van Rossumede04391998-04-10 20:18:25 +0000950
951PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000952PyThreadState_GetDict(void)
Guido van Rossumede04391998-04-10 20:18:25 +0000953{
Victor Stinner50b48572018-11-01 01:51:40 +0100954 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 if (tstate == NULL)
956 return NULL;
Guido van Rossumede04391998-04-10 20:18:25 +0000957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 if (tstate->dict == NULL) {
959 PyObject *d;
960 tstate->dict = d = PyDict_New();
961 if (d == NULL)
962 PyErr_Clear();
963 }
964 return tstate->dict;
Guido van Rossumede04391998-04-10 20:18:25 +0000965}
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000966
967
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000968/* Asynchronously raise an exception in a thread.
969 Requested by Just van Rossum and Alex Martelli.
Guido van Rossum0f1f63c2005-02-08 02:07:57 +0000970 To prevent naive misuse, you must write your own extension
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000971 to call this, or use ctypes. Must be called with the GIL held.
972 Returns the number of tstates modified (normally 1, but 0 if `id` didn't
973 match any known thread id). Can be called with exc=NULL to clear an
974 existing async exception. This raises no exceptions. */
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000975
976int
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200977PyThreadState_SetAsyncExc(unsigned long id, PyObject *exc)
978{
Victor Stinner9204fb82018-10-30 15:13:17 +0100979 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 PyThreadState *p;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 /* Although the GIL is held, a few C API functions can be called
983 * without the GIL held, and in particular some that create and
984 * destroy thread and interpreter states. Those can mutate the
985 * list of thread states we're traversing, so to prevent that we lock
986 * head_mutex for the duration.
987 */
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200988 _PyRuntimeState *runtime = &_PyRuntime;
989 HEAD_LOCK(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 for (p = interp->tstate_head; p != NULL; p = p->next) {
991 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);
Eric Snowb75b1a352019-04-12 10:20:10 -06001004 _PyEval_SignalAsyncExc();
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