Coverage Report

Created: 2022-07-08 09:39

/home/mdboom/Work/builds/cpython/Python/pystate.c
Line
Count
Source (jump to first uncovered line)
1
2
/* Thread and interpreter state structures and their interfaces */
3
4
#include "Python.h"
5
#include "pycore_ceval.h"
6
#include "pycore_code.h"           // stats
7
#include "pycore_frame.h"
8
#include "pycore_initconfig.h"
9
#include "pycore_object.h"        // _PyType_InitCache()
10
#include "pycore_pyerrors.h"
11
#include "pycore_pylifecycle.h"
12
#include "pycore_pymem.h"         // _PyMem_SetDefaultAllocator()
13
#include "pycore_pystate.h"       // _PyThreadState_GET()
14
#include "pycore_runtime_init.h"  // _PyRuntimeState_INIT
15
#include "pycore_sysmodule.h"
16
17
/* --------------------------------------------------------------------------
18
CAUTION
19
20
Always use PyMem_RawMalloc() and PyMem_RawFree() directly in this file.  A
21
number of these functions are advertised as safe to call when the GIL isn't
22
held, and in a debug build Python redirects (e.g.) PyMem_NEW (etc) to Python's
23
debugging obmalloc functions.  Those aren't thread-safe (they rely on the GIL
24
to avoid the expense of doing their own locking).
25
-------------------------------------------------------------------------- */
26
27
#ifdef HAVE_DLOPEN
28
#ifdef HAVE_DLFCN_H
29
#include <dlfcn.h>
30
#endif
31
#if !HAVE_DECL_RTLD_LAZY
32
#define RTLD_LAZY 1
33
#endif
34
#endif
35
36
#ifdef __cplusplus
37
extern "C" {
38
#endif
39
40
#define _PyRuntimeGILState_GetThreadState(gilstate) \
41
    ((PyThreadState*)_Py_atomic_load_relaxed(&(gilstate)->tstate_current))
42
#define _PyRuntimeGILState_SetThreadState(gilstate, value) \
43
    _Py_atomic_store_relaxed(&(gilstate)->tstate_current, \
44
                             (uintptr_t)(value))
45
46
/* Forward declarations */
47
static PyThreadState *_PyGILState_GetThisThreadState(struct _gilstate_runtime_state *gilstate);
48
static void _PyThreadState_Delete(PyThreadState *tstate, int check_current);
49
50
/* Suppress deprecation warning for PyBytesObject.ob_shash */
51
_Py_COMP_DIAG_PUSH
52
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
53
/* We use "initial" if the runtime gets re-used
54
   (e.g. Py_Finalize() followed by Py_Initialize(). */
55
static const _PyRuntimeState initial = _PyRuntimeState_INIT;
56
_Py_COMP_DIAG_POP
57
58
static int
59
alloc_for_runtime(PyThread_type_lock *plock1, PyThread_type_lock *plock2,
60
                  PyThread_type_lock *plock3)
61
{
62
    /* Force default allocator, since _PyRuntimeState_Fini() must
63
       use the same allocator than this function. */
64
    PyMemAllocatorEx old_alloc;
65
    _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
66
67
    PyThread_type_lock lock1 = PyThread_allocate_lock();
68
    if (lock1 == NULL) {
  Branch (68:9): [True: 0, False: 107]
69
        return -1;
70
    }
71
72
    PyThread_type_lock lock2 = PyThread_allocate_lock();
73
    if (lock2 == NULL) {
  Branch (73:9): [True: 0, False: 107]
74
        PyThread_free_lock(lock1);
75
        return -1;
76
    }
77
78
    PyThread_type_lock lock3 = PyThread_allocate_lock();
79
    if (lock3 == NULL) {
  Branch (79:9): [True: 0, False: 107]
80
        PyThread_free_lock(lock1);
81
        PyThread_free_lock(lock2);
82
        return -1;
83
    }
84
85
    PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
86
87
    *plock1 = lock1;
88
    *plock2 = lock2;
89
    *plock3 = lock3;
90
    return 0;
91
}
92
93
static void
94
init_runtime(_PyRuntimeState *runtime,
95
             void *open_code_hook, void *open_code_userdata,
96
             _Py_AuditHookEntry *audit_hook_head,
97
             Py_ssize_t unicode_next_index,
98
             PyThread_type_lock unicode_ids_mutex,
99
             PyThread_type_lock interpreters_mutex,
100
             PyThread_type_lock xidregistry_mutex)
101
{
102
    if (runtime->_initialized) {
  Branch (102:9): [True: 0, False: 107]
103
        Py_FatalError("runtime already initialized");
104
    }
105
    assert(!runtime->preinitializing &&
106
           !runtime->preinitialized &&
107
           !runtime->core_initialized &&
108
           !runtime->initialized);
109
110
    runtime->open_code_hook = open_code_hook;
111
    runtime->open_code_userdata = open_code_userdata;
112
    runtime->audit_hook_head = audit_hook_head;
113
114
    _PyEval_InitRuntimeState(&runtime->ceval);
115
116
    PyPreConfig_InitPythonConfig(&runtime->preconfig);
117
118
    runtime->interpreters.mutex = interpreters_mutex;
119
120
    runtime->xidregistry.mutex = xidregistry_mutex;
121
122
    // Set it to the ID of the main thread of the main interpreter.
123
    runtime->main_thread = PyThread_get_thread_ident();
124
125
    runtime->unicode_ids.next_index = unicode_next_index;
126
    runtime->unicode_ids.lock = unicode_ids_mutex;
127
128
    runtime->_initialized = 1;
129
}
130
131
PyStatus
132
_PyRuntimeState_Init(_PyRuntimeState *runtime)
133
{
134
    /* We preserve the hook across init, because there is
135
       currently no public API to set it between runtime
136
       initialization and interpreter initialization. */
137
    void *open_code_hook = runtime->open_code_hook;
138
    void *open_code_userdata = runtime->open_code_userdata;
139
    _Py_AuditHookEntry *audit_hook_head = runtime->audit_hook_head;
140
    // bpo-42882: Preserve next_index value if Py_Initialize()/Py_Finalize()
141
    // is called multiple times.
142
    Py_ssize_t unicode_next_index = runtime->unicode_ids.next_index;
143
144
    PyThread_type_lock lock1, lock2, lock3;
145
    if (alloc_for_runtime(&lock1, &lock2, &lock3) != 0) {
  Branch (145:9): [True: 0, False: 107]
146
        return _PyStatus_NO_MEMORY();
147
    }
148
149
    if (runtime->_initialized) {
  Branch (149:9): [True: 34, False: 73]
150
        // Py_Initialize() must be running again.
151
        // Reset to _PyRuntimeState_INIT.
152
        memcpy(runtime, &initial, sizeof(*runtime));
153
    }
154
    init_runtime(runtime, open_code_hook, open_code_userdata, audit_hook_head,
155
                 unicode_next_index, lock1, lock2, lock3);
156
157
    return _PyStatus_OK();
158
}
159
160
void
161
_PyRuntimeState_Fini(_PyRuntimeState *runtime)
162
{
163
    /* Force the allocator used by _PyRuntimeState_Init(). */
164
    PyMemAllocatorEx old_alloc;
165
    _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
166
#define FREE_LOCK(LOCK) \
167
    if (LOCK != NULL) { \
168
        PyThread_free_lock(LOCK); \
169
        LOCK = NULL; \
170
    }
171
172
    FREE_LOCK(runtime->interpreters.mutex);
173
    FREE_LOCK(runtime->xidregistry.mutex);
174
    FREE_LOCK(runtime->unicode_ids.lock);
175
176
#undef FREE_LOCK
177
    PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
178
}
179
180
#ifdef HAVE_FORK
181
/* This function is called from PyOS_AfterFork_Child to ensure that
182
   newly created child processes do not share locks with the parent. */
183
PyStatus
184
_PyRuntimeState_ReInitThreads(_PyRuntimeState *runtime)
185
{
186
    // This was initially set in _PyRuntimeState_Init().
187
    runtime->main_thread = PyThread_get_thread_ident();
188
189
    /* Force default allocator, since _PyRuntimeState_Fini() must
190
       use the same allocator than this function. */
191
    PyMemAllocatorEx old_alloc;
192
    _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
193
194
    int reinit_interp = _PyThread_at_fork_reinit(&runtime->interpreters.mutex);
195
    int reinit_xidregistry = _PyThread_at_fork_reinit(&runtime->xidregistry.mutex);
196
    int reinit_unicode_ids = _PyThread_at_fork_reinit(&runtime->unicode_ids.lock);
197
198
    PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
199
200
    /* bpo-42540: id_mutex is freed by _PyInterpreterState_Delete, which does
201
     * not force the default allocator. */
202
    int reinit_main_id = _PyThread_at_fork_reinit(&runtime->interpreters.main->id_mutex);
203
204
    if (reinit_interp < 0
  Branch (204:9): [True: 0, False: 0]
205
        || reinit_main_id < 0
  Branch (205:12): [True: 0, False: 0]
206
        || reinit_xidregistry < 0
  Branch (206:12): [True: 0, False: 0]
207
        || reinit_unicode_ids < 0)
  Branch (207:12): [True: 0, False: 0]
208
    {
209
        return _PyStatus_ERR("Failed to reinitialize runtime locks");
210
211
    }
212
    return _PyStatus_OK();
213
}
214
#endif
215
216
#define HEAD_LOCK(runtime) \
217
    PyThread_acquire_lock((runtime)->interpreters.mutex, WAIT_LOCK)
218
#define HEAD_UNLOCK(runtime) \
219
    PyThread_release_lock((runtime)->interpreters.mutex)
220
221
/* Forward declaration */
222
static void _PyGILState_NoteThreadState(
223
    struct _gilstate_runtime_state *gilstate, PyThreadState* tstate);
224
225
PyStatus
226
_PyInterpreterState_Enable(_PyRuntimeState *runtime)
227
{
228
    struct pyinterpreters *interpreters = &runtime->interpreters;
229
    interpreters->next_id = 0;
230
231
    /* Py_Finalize() calls _PyRuntimeState_Fini() which clears the mutex.
232
       Create a new mutex if needed. */
233
    if (interpreters->mutex == NULL) {
  Branch (233:9): [True: 0, False: 107]
234
        /* Force default allocator, since _PyRuntimeState_Fini() must
235
           use the same allocator than this function. */
236
        PyMemAllocatorEx old_alloc;
237
        _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
238
239
        interpreters->mutex = PyThread_allocate_lock();
240
241
        PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
242
243
        if (interpreters->mutex == NULL) {
  Branch (243:13): [True: 0, False: 0]
244
            return _PyStatus_ERR("Can't initialize threads for interpreter");
245
        }
246
    }
247
248
    return _PyStatus_OK();
249
}
250
251
static PyInterpreterState *
252
alloc_interpreter(void)
253
{
254
    return PyMem_RawCalloc(1, sizeof(PyInterpreterState));
255
}
256
257
static void
258
free_interpreter(PyInterpreterState *interp)
259
{
260
    if (!interp->_static) {
  Branch (260:9): [True: 0, False: 272]
261
        PyMem_RawFree(interp);
262
    }
263
}
264
265
/* Get the interpreter state to a minimal consistent state.
266
   Further init happens in pylifecycle.c before it can be used.
267
   All fields not initialized here are expected to be zeroed out,
268
   e.g. by PyMem_RawCalloc() or memset(), or otherwise pre-initialized.
269
   The runtime state is not manipulated.  Instead it is assumed that
270
   the interpreter is getting added to the runtime.
271
  */
272
273
static void
274
init_interpreter(PyInterpreterState *interp,
275
                 _PyRuntimeState *runtime, int64_t id,
276
                 PyInterpreterState *next,
277
                 PyThread_type_lock pending_lock)
278
{
279
    if (interp->_initialized) {
  Branch (279:9): [True: 0, False: 278]
280
        Py_FatalError("interpreter already initialized");
281
    }
282
283
    assert(runtime != NULL);
284
    interp->runtime = runtime;
285
286
    assert(id > 0 || (id == 0 && interp == runtime->interpreters.main));
287
    interp->id = id;
288
289
    assert(runtime->interpreters.head == interp);
290
    assert(next != NULL || (interp == runtime->interpreters.main));
291
    interp->next = next;
292
293
    _PyEval_InitState(&interp->ceval, pending_lock);
294
    _PyGC_InitState(&interp->gc);
295
    PyConfig_InitPythonConfig(&interp->config);
296
    _PyType_InitCache(interp);
297
298
    interp->_initialized = 1;
299
}
300
301
PyInterpreterState *
302
PyInterpreterState_New(void)
303
{
304
    PyInterpreterState *interp;
305
    PyThreadState *tstate = _PyThreadState_GET();
306
307
    /* tstate is NULL when Py_InitializeFromConfig() calls
308
       PyInterpreterState_New() to create the main interpreter. */
309
    if (_PySys_Audit(tstate, "cpython.PyInterpreterState_New", NULL) < 0) {
  Branch (309:9): [True: 0, False: 278]
310
        return NULL;
311
    }
312
313
    PyThread_type_lock pending_lock = PyThread_allocate_lock();
314
    if (pending_lock == NULL) {
  Branch (314:9): [True: 0, False: 278]
315
        if (tstate != NULL) {
  Branch (315:13): [True: 0, False: 0]
316
            _PyErr_NoMemory(tstate);
317
        }
318
        return NULL;
319
    }
320
321
    /* Don't get runtime from tstate since tstate can be NULL. */
322
    _PyRuntimeState *runtime = &_PyRuntime;
323
    struct pyinterpreters *interpreters = &runtime->interpreters;
324
325
    /* We completely serialize creation of multiple interpreters, since
326
       it simplifies things here and blocking concurrent calls isn't a problem.
327
       Regardless, we must fully block subinterpreter creation until
328
       after the main interpreter is created. */
329
    HEAD_LOCK(runtime);
330
331
    int64_t id = interpreters->next_id;
332
    interpreters->next_id += 1;
333
334
    // Allocate the interpreter and add it to the runtime state.
335
    PyInterpreterState *old_head = interpreters->head;
336
    if (old_head == NULL) {
  Branch (336:9): [True: 107, False: 171]
337
        // We are creating the main interpreter.
338
        assert(interpreters->main == NULL);
339
        assert(id == 0);
340
341
        interp = &runtime->_main_interpreter;
342
        assert(interp->id == 0);
343
        assert(interp->next == NULL);
344
345
        interpreters->main = interp;
346
    }
347
    else {
348
        assert(interpreters->main != NULL);
349
        assert(id != 0);
350
351
        interp = alloc_interpreter();
352
        if (interp == NULL) {
  Branch (352:13): [True: 0, False: 171]
353
            goto error;
354
        }
355
        // Set to _PyInterpreterState_INIT.
356
        memcpy(interp, &initial._main_interpreter,
357
               sizeof(*interp));
358
359
        if (id < 0) {
  Branch (359:13): [True: 0, False: 171]
360
            /* overflow or Py_Initialize() not called yet! */
361
            if (tstate != NULL) {
  Branch (361:17): [True: 0, False: 0]
362
                _PyErr_SetString(tstate, PyExc_RuntimeError,
363
                                 "failed to get an interpreter ID");
364
            }
365
            goto error;
366
        }
367
    }
368
    interpreters->head = interp;
369
370
    init_interpreter(interp, runtime, id, old_head, pending_lock);
371
372
    HEAD_UNLOCK(runtime);
373
    return interp;
374
375
error:
376
    HEAD_UNLOCK(runtime);
377
378
    PyThread_free_lock(pending_lock);
379
    if (interp != NULL) {
  Branch (379:9): [True: 0, False: 0]
380
        free_interpreter(interp);
381
    }
382
    return NULL;
383
}
384
385
386
static void
387
interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate)
388
{
389
    _PyRuntimeState *runtime = interp->runtime;
390
391
    if (_PySys_Audit(tstate, "cpython.PyInterpreterState_Clear", NULL) < 0) {
  Branch (391:9): [True: 0, False: 272]
392
        _PyErr_Clear(tstate);
393
    }
394
395
    HEAD_LOCK(runtime);
396
    for (PyThreadState *p = interp->threads.head; p != NULL; 
p = p->next272
) {
  Branch (396:51): [True: 272, False: 272]
397
        PyThreadState_Clear(p);
398
    }
399
    HEAD_UNLOCK(runtime);
400
401
    Py_CLEAR(interp->audit_hooks);
402
403
    PyConfig_Clear(&interp->config);
404
    Py_CLEAR(interp->codec_search_path);
405
    Py_CLEAR(interp->codec_search_cache);
406
    Py_CLEAR(interp->codec_error_registry);
407
    Py_CLEAR(interp->modules);
408
    Py_CLEAR(interp->modules_by_index);
409
    Py_CLEAR(interp->builtins_copy);
410
    Py_CLEAR(interp->importlib);
411
    Py_CLEAR(interp->import_func);
412
    Py_CLEAR(interp->dict);
413
#ifdef HAVE_FORK
414
    Py_CLEAR(interp->before_forkers);
415
    Py_CLEAR(interp->after_forkers_parent);
416
    Py_CLEAR(interp->after_forkers_child);
417
#endif
418
419
    _PyAST_Fini(interp);
420
    _PyWarnings_Fini(interp);
421
    _PyAtExit_Fini(interp);
422
423
    // All Python types must be destroyed before the last GC collection. Python
424
    // types create a reference cycle to themselves in their in their
425
    // PyTypeObject.tp_mro member (the tuple contains the type).
426
427
    /* Last garbage collection on this interpreter */
428
    _PyGC_CollectNoFail(tstate);
429
    _PyGC_Fini(interp);
430
431
    /* We don't clear sysdict and builtins until the end of this function.
432
       Because clearing other attributes can execute arbitrary Python code
433
       which requires sysdict and builtins. */
434
    PyDict_Clear(interp->sysdict);
435
    PyDict_Clear(interp->builtins);
436
    Py_CLEAR(interp->sysdict);
437
    Py_CLEAR(interp->builtins);
438
439
    // XXX Once we have one allocator per interpreter (i.e.
440
    // per-interpreter GC) we must ensure that all of the interpreter's
441
    // objects have been cleaned up at the point.
442
}
443
444
445
void
446
PyInterpreterState_Clear(PyInterpreterState *interp)
447
{
448
    // Use the current Python thread state to call audit hooks and to collect
449
    // garbage. It can be different than the current Python thread state
450
    // of 'interp'.
451
    PyThreadState *current_tstate = _PyThreadState_GET();
452
453
    interpreter_clear(interp, current_tstate);
454
}
455
456
457
void
458
_PyInterpreterState_Clear(PyThreadState *tstate)
459
{
460
    interpreter_clear(tstate->interp, tstate);
461
}
462
463
464
static void
465
zapthreads(PyInterpreterState *interp, int check_current)
466
{
467
    PyThreadState *tstate;
468
    /* No need to lock the mutex here because this should only happen
469
       when the threads are all really dead (XXX famous last words). */
470
    while ((tstate = interp->threads.head) != NULL) {
  Branch (470:12): [True: 272, False: 272]
471
        _PyThreadState_Delete(tstate, check_current);
472
    }
473
}
474
475
476
void
477
PyInterpreterState_Delete(PyInterpreterState *interp)
478
{
479
    _PyRuntimeState *runtime = interp->runtime;
480
    struct pyinterpreters *interpreters = &runtime->interpreters;
481
    zapthreads(interp, 0);
482
483
    _PyEval_FiniState(&interp->ceval);
484
485
    /* Delete current thread. After this, many C API calls become crashy. */
486
    _PyThreadState_Swap(&runtime->gilstate, NULL);
487
488
    HEAD_LOCK(runtime);
489
    PyInterpreterState **p;
490
    for (p = &interpreters->head; ; 
p = &(*p)->next34
) {
491
        if (*p == NULL) {
  Branch (491:13): [True: 0, False: 306]
492
            Py_FatalError("NULL interpreter");
493
        }
494
        if (*p == interp) {
  Branch (494:13): [True: 272, False: 34]
495
            break;
496
        }
497
    }
498
    if (interp->threads.head != NULL) {
  Branch (498:9): [True: 0, False: 272]
499
        Py_FatalError("remaining threads");
500
    }
501
    *p = interp->next;
502
503
    if (interpreters->main == interp) {
  Branch (503:9): [True: 103, False: 169]
504
        interpreters->main = NULL;
505
        if (interpreters->head != NULL) {
  Branch (505:13): [True: 0, False: 103]
506
            Py_FatalError("remaining subinterpreters");
507
        }
508
    }
509
    HEAD_UNLOCK(runtime);
510
511
    if (interp->id_mutex != NULL) {
  Branch (511:9): [True: 122, False: 150]
512
        PyThread_free_lock(interp->id_mutex);
513
    }
514
    free_interpreter(interp);
515
}
516
517
518
#ifdef HAVE_FORK
519
/*
520
 * Delete all interpreter states except the main interpreter.  If there
521
 * is a current interpreter state, it *must* be the main interpreter.
522
 */
523
PyStatus
524
_PyInterpreterState_DeleteExceptMain(_PyRuntimeState *runtime)
525
{
526
    struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
527
    struct pyinterpreters *interpreters = &runtime->interpreters;
528
529
    PyThreadState *tstate = _PyThreadState_Swap(gilstate, NULL);
530
    if (tstate != NULL && tstate->interp != interpreters->main) {
  Branch (530:9): [True: 0, False: 0]
  Branch (530:27): [True: 0, False: 0]
531
        return _PyStatus_ERR("not main interpreter");
532
    }
533
534
    HEAD_LOCK(runtime);
535
    PyInterpreterState *interp = interpreters->head;
536
    interpreters->head = NULL;
537
    while (interp != NULL) {
  Branch (537:12): [True: 0, False: 0]
538
        if (interp == interpreters->main) {
  Branch (538:13): [True: 0, False: 0]
539
            interpreters->main->next = NULL;
540
            interpreters->head = interp;
541
            interp = interp->next;
542
            continue;
543
        }
544
545
        PyInterpreterState_Clear(interp);  // XXX must activate?
546
        zapthreads(interp, 1);
547
        if (interp->id_mutex != NULL) {
  Branch (547:13): [True: 0, False: 0]
548
            PyThread_free_lock(interp->id_mutex);
549
        }
550
        PyInterpreterState *prev_interp = interp;
551
        interp = interp->next;
552
        free_interpreter(prev_interp);
553
    }
554
    HEAD_UNLOCK(runtime);
555
556
    if (interpreters->head == NULL) {
  Branch (556:9): [True: 0, False: 0]
557
        return _PyStatus_ERR("missing main interpreter");
558
    }
559
    _PyThreadState_Swap(gilstate, tstate);
560
    return _PyStatus_OK();
561
}
562
#endif
563
564
565
PyInterpreterState *
566
PyInterpreterState_Get(void)
567
{
568
    PyThreadState *tstate = _PyThreadState_GET();
569
    _Py_EnsureTstateNotNULL(tstate);
570
    PyInterpreterState *interp = tstate->interp;
571
    if (interp == NULL) {
  Branch (571:9): [True: 0, False: 6.47k]
572
        Py_FatalError("no current interpreter");
573
    }
574
    return interp;
575
}
576
577
578
int64_t
579
PyInterpreterState_GetID(PyInterpreterState *interp)
580
{
581
    if (interp == NULL) {
  Branch (581:9): [True: 0, False: 3.75k]
582
        PyErr_SetString(PyExc_RuntimeError, "no interpreter provided");
583
        return -1;
584
    }
585
    return interp->id;
586
}
587
588
589
static PyInterpreterState *
590
interp_look_up_id(_PyRuntimeState *runtime, int64_t requested_id)
591
{
592
    PyInterpreterState *interp = runtime->interpreters.head;
593
    while (interp != NULL) {
  Branch (593:12): [True: 1.99k, False: 90]
594
        int64_t id = PyInterpreterState_GetID(interp);
595
        if (id < 0) {
  Branch (595:13): [True: 0, False: 1.99k]
596
            return NULL;
597
        }
598
        if (requested_id == id) {
  Branch (598:13): [True: 1.58k, False: 414]
599
            return interp;
600
        }
601
        interp = PyInterpreterState_Next(interp);
602
    }
603
    return NULL;
604
}
605
606
PyInterpreterState *
607
_PyInterpreterState_LookUpID(int64_t requested_id)
608
{
609
    PyInterpreterState *interp = NULL;
610
    if (requested_id >= 0) {
  Branch (610:9): [True: 1.67k, False: 0]
611
        _PyRuntimeState *runtime = &_PyRuntime;
612
        HEAD_LOCK(runtime);
613
        interp = interp_look_up_id(runtime, requested_id);
614
        HEAD_UNLOCK(runtime);
615
    }
616
    if (interp == NULL && 
!PyErr_Occurred()90
) {
  Branch (616:9): [True: 90, False: 1.58k]
  Branch (616:27): [True: 90, False: 0]
617
        PyErr_Format(PyExc_RuntimeError,
618
                     "unrecognized interpreter ID %lld", requested_id);
619
    }
620
    return interp;
621
}
622
623
624
int
625
_PyInterpreterState_IDInitref(PyInterpreterState *interp)
626
{
627
    if (interp->id_mutex != NULL) {
  Branch (627:9): [True: 785, False: 122]
628
        return 0;
629
    }
630
    interp->id_mutex = PyThread_allocate_lock();
631
    if (interp->id_mutex == NULL) {
  Branch (631:9): [True: 0, False: 122]
632
        PyErr_SetString(PyExc_RuntimeError,
633
                        "failed to create init interpreter ID mutex");
634
        return -1;
635
    }
636
    interp->id_refcount = 0;
637
    return 0;
638
}
639
640
641
int
642
_PyInterpreterState_IDIncref(PyInterpreterState *interp)
643
{
644
    if (_PyInterpreterState_IDInitref(interp) < 0) {
  Branch (644:9): [True: 0, False: 464]
645
        return -1;
646
    }
647
648
    PyThread_acquire_lock(interp->id_mutex, WAIT_LOCK);
649
    interp->id_refcount += 1;
650
    PyThread_release_lock(interp->id_mutex);
651
    return 0;
652
}
653
654
655
void
656
_PyInterpreterState_IDDecref(PyInterpreterState *interp)
657
{
658
    assert(interp->id_mutex != NULL);
659
660
    struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
661
    PyThread_acquire_lock(interp->id_mutex, WAIT_LOCK);
662
    assert(interp->id_refcount != 0);
663
    interp->id_refcount -= 1;
664
    int64_t refcount = interp->id_refcount;
665
    PyThread_release_lock(interp->id_mutex);
666
667
    if (refcount == 0 && 
interp->requires_idref291
) {
  Branch (667:9): [True: 291, False: 102]
  Branch (667:26): [True: 70, False: 221]
668
        // XXX Using the "head" thread isn't strictly correct.
669
        PyThreadState *tstate = PyInterpreterState_ThreadHead(interp);
670
        // XXX Possible GILState issues?
671
        PyThreadState *save_tstate = _PyThreadState_Swap(gilstate, tstate);
672
        Py_EndInterpreter(tstate);
673
        _PyThreadState_Swap(gilstate, save_tstate);
674
    }
675
}
676
677
int
678
_PyInterpreterState_RequiresIDRef(PyInterpreterState *interp)
679
{
680
    return interp->requires_idref;
681
}
682
683
void
684
_PyInterpreterState_RequireIDRef(PyInterpreterState *interp, int required)
685
{
686
    interp->requires_idref = required ? 1 : 
00
;
  Branch (686:30): [True: 121, False: 0]
687
}
688
689
PyObject *
690
_PyInterpreterState_GetMainModule(PyInterpreterState *interp)
691
{
692
    if (interp->modules == NULL) {
  Branch (692:9): [True: 0, False: 61]
693
        PyErr_SetString(PyExc_RuntimeError, "interpreter not initialized");
694
        return NULL;
695
    }
696
    return PyMapping_GetItemString(interp->modules, "__main__");
697
}
698
699
PyObject *
700
PyInterpreterState_GetDict(PyInterpreterState *interp)
701
{
702
    if (interp->dict == NULL) {
  Branch (702:9): [True: 0, False: 0]
703
        interp->dict = PyDict_New();
704
        if (interp->dict == NULL) {
  Branch (704:13): [True: 0, False: 0]
705
            PyErr_Clear();
706
        }
707
    }
708
    /* Returning NULL means no per-interpreter dict is available. */
709
    return interp->dict;
710
}
711
712
/* Minimum size of data stack chunk */
713
#define DATA_STACK_CHUNK_SIZE (16*1024)
714
715
static _PyStackChunk*
716
allocate_chunk(int size_in_bytes, _PyStackChunk* previous)
717
{
718
    assert(size_in_bytes % sizeof(PyObject **) == 0);
719
    _PyStackChunk *res = _PyObject_VirtualAlloc(size_in_bytes);
720
    if (res == NULL) {
  Branch (720:9): [True: 0, False: 13.1k]
721
        return NULL;
722
    }
723
    res->previous = previous;
724
    res->size = size_in_bytes;
725
    res->top = 0;
726
    return res;
727
}
728
729
static PyThreadState *
730
alloc_threadstate(void)
731
{
732
    return PyMem_RawCalloc(1, sizeof(PyThreadState));
733
}
734
735
static void
736
free_threadstate(PyThreadState *tstate)
737
{
738
    if (!tstate->_static) {
  Branch (738:9): [True: 0, False: 5.97k]
739
        PyMem_RawFree(tstate);
740
    }
741
}
742
743
/* Get the thread state to a minimal consistent state.
744
   Further init happens in pylifecycle.c before it can be used.
745
   All fields not initialized here are expected to be zeroed out,
746
   e.g. by PyMem_RawCalloc() or memset(), or otherwise pre-initialized.
747
   The interpreter state is not manipulated.  Instead it is assumed that
748
   the thread is getting added to the interpreter.
749
  */
750
751
static void
752
init_threadstate(PyThreadState *tstate,
753
                 PyInterpreterState *interp, uint64_t id,
754
                 PyThreadState *next)
755
{
756
    if (tstate->_initialized) {
  Branch (756:9): [True: 0, False: 5.98k]
757
        Py_FatalError("thread state already initialized");
758
    }
759
760
    assert(interp != NULL);
761
    tstate->interp = interp;
762
763
    assert(id > 0);
764
    tstate->id = id;
765
766
    assert(interp->threads.head == tstate);
767
    assert((next != NULL && id != 1) || (next == NULL && id == 1));
768
    if (next != NULL) {
  Branch (768:9): [True: 5.70k, False: 278]
769
        assert(next->prev == NULL || next->prev == tstate);
770
        next->prev = tstate;
771
    }
772
    tstate->next = next;
773
    assert(tstate->prev == NULL);
774
775
    tstate->thread_id = PyThread_get_thread_ident();
776
#ifdef PY_HAVE_THREAD_NATIVE_ID
777
    tstate->native_thread_id = PyThread_get_thread_native_id();
778
#endif
779
780
    tstate->recursion_limit = interp->ceval.recursion_limit,
781
    tstate->recursion_remaining = interp->ceval.recursion_limit,
782
783
    tstate->exc_info = &tstate->exc_state;
784
785
    tstate->cframe = &tstate->root_cframe;
786
    tstate->datastack_chunk = NULL;
787
    tstate->datastack_top = NULL;
788
    tstate->datastack_limit = NULL;
789
790
    tstate->_initialized = 1;
791
}
792
793
static PyThreadState *
794
new_threadstate(PyInterpreterState *interp)
795
{
796
    PyThreadState *tstate;
797
    _PyRuntimeState *runtime = interp->runtime;
798
799
    /* We serialize concurrent creation to protect global state. */
800
    HEAD_LOCK(runtime);
801
802
    interp->threads.next_unique_id += 1;
803
    uint64_t id = interp->threads.next_unique_id;
804
805
    // Allocate the thread state and add it to the interpreter.
806
    PyThreadState *old_head = interp->threads.head;
807
    if (old_head == NULL) {
  Branch (807:9): [True: 278, False: 5.70k]
808
        // It's the interpreter's initial thread state.
809
        assert(id == 1);
810
811
        tstate = &interp->_initial_thread;
812
    }
813
    else {
814
        // Every valid interpreter must have at least one thread.
815
        assert(id > 1);
816
        assert(old_head->prev == NULL);
817
818
        tstate = alloc_threadstate();
819
        if (tstate == NULL) {
  Branch (819:13): [True: 0, False: 5.70k]
820
            goto error;
821
        }
822
        // Set to _PyThreadState_INIT.
823
        memcpy(tstate,
824
               &initial._main_interpreter._initial_thread,
825
               sizeof(*tstate));
826
    }
827
    interp->threads.head = tstate;
828
829
    init_threadstate(tstate, interp, id, old_head);
830
831
    HEAD_UNLOCK(runtime);
832
    return tstate;
833
834
error:
835
    HEAD_UNLOCK(runtime);
836
    return NULL;
837
}
838
839
PyThreadState *
840
PyThreadState_New(PyInterpreterState *interp)
841
{
842
    PyThreadState *tstate = new_threadstate(interp);
843
    _PyThreadState_SetCurrent(tstate);
844
    return tstate;
845
}
846
847
PyThreadState *
848
_PyThreadState_Prealloc(PyInterpreterState *interp)
849
{
850
    return new_threadstate(interp);
851
}
852
853
// We keep this around for (accidental) stable ABI compatibility.
854
// Realisically, no extensions are using it.
855
void
856
_PyThreadState_Init(PyThreadState *tstate)
857
{
858
    Py_FatalError("_PyThreadState_Init() is for internal use only");
859
}
860
861
void
862
_PyThreadState_SetCurrent(PyThreadState *tstate)
863
{
864
    _PyGILState_NoteThreadState(&tstate->interp->runtime->gilstate, tstate);
865
}
866
867
PyObject*
868
PyState_FindModule(PyModuleDef* module)
869
{
870
    Py_ssize_t index = module->m_base.m_index;
871
    PyInterpreterState *state = _PyInterpreterState_GET();
872
    PyObject *res;
873
    if (module->m_slots) {
  Branch (873:9): [True: 2, False: 459k]
874
        return NULL;
875
    }
876
    if (index == 0)
  Branch (876:9): [True: 2, False: 459k]
877
        return NULL;
878
    if (state->modules_by_index == NULL)
  Branch (878:9): [True: 0, False: 459k]
879
        return NULL;
880
    if (index >= PyList_GET_SIZE(state->modules_by_index))
  Branch (880:9): [True: 7, False: 459k]
881
        return NULL;
882
    res = PyList_GET_ITEM(state->modules_by_index, index);
883
    return res==Py_None ? NULL : res;
  Branch (883:12): [True: 0, False: 459k]
884
}
885
886
int
887
_PyState_AddModule(PyThreadState *tstate, PyObject* module, PyModuleDef* def)
888
{
889
    if (!def) {
  Branch (889:9): [True: 0, False: 960]
890
        assert(_PyErr_Occurred(tstate));
891
        return -1;
892
    }
893
    if (def->m_slots) {
  Branch (893:9): [True: 2, False: 958]
894
        _PyErr_SetString(tstate,
895
                         PyExc_SystemError,
896
                         "PyState_AddModule called on module with slots");
897
        return -1;
898
    }
899
900
    PyInterpreterState *interp = tstate->interp;
901
    if (!interp->modules_by_index) {
  Branch (901:9): [True: 278, False: 680]
902
        interp->modules_by_index = PyList_New(0);
903
        if (!interp->modules_by_index) {
  Branch (903:13): [True: 0, False: 278]
904
            return -1;
905
        }
906
    }
907
908
    
while (958
PyList_GET_SIZE(interp->modules_by_index) <= def->m_base.m_index) {
  Branch (908:12): [True: 5.32k, False: 958]
909
        if (PyList_Append(interp->modules_by_index, Py_None) < 0) {
  Branch (909:13): [True: 0, False: 5.32k]
910
            return -1;
911
        }
912
    }
913
914
    Py_INCREF(module);
915
    return PyList_SetItem(interp->modules_by_index,
916
                          def->m_base.m_index, module);
917
}
918
919
int
920
PyState_AddModule(PyObject* module, PyModuleDef* def)
921
{
922
    if (!def) {
  Branch (922:9): [True: 0, False: 3]
923
        Py_FatalError("module definition is NULL");
924
        return -1;
925
    }
926
927
    PyThreadState *tstate = _PyThreadState_GET();
928
    PyInterpreterState *interp = tstate->interp;
929
    Py_ssize_t index = def->m_base.m_index;
930
    if (interp->modules_by_index &&
  Branch (930:9): [True: 3, False: 0]
931
        index < PyList_GET_SIZE(interp->modules_by_index) &&
  Branch (931:9): [True: 2, False: 1]
932
        
module == 2
PyList_GET_ITEM2
(interp->modules_by_index, index))
  Branch (932:9): [True: 0, False: 2]
933
    {
934
        _Py_FatalErrorFormat(__func__, "module %p already added", module);
935
        return -1;
936
    }
937
    return _PyState_AddModule(tstate, module, def);
938
}
939
940
int
941
PyState_RemoveModule(PyModuleDef* def)
942
{
943
    PyThreadState *tstate = _PyThreadState_GET();
944
    PyInterpreterState *interp = tstate->interp;
945
946
    if (def->m_slots) {
  Branch (946:9): [True: 2, False: 0]
947
        _PyErr_SetString(tstate,
948
                         PyExc_SystemError,
949
                         "PyState_RemoveModule called on module with slots");
950
        return -1;
951
    }
952
953
    Py_ssize_t index = def->m_base.m_index;
954
    if (index == 0) {
  Branch (954:9): [True: 0, False: 0]
955
        Py_FatalError("invalid module index");
956
    }
957
    if (interp->modules_by_index == NULL) {
  Branch (957:9): [True: 0, False: 0]
958
        Py_FatalError("Interpreters module-list not accessible.");
959
    }
960
    if (index > PyList_GET_SIZE(interp->modules_by_index)) {
  Branch (960:9): [True: 0, False: 0]
961
        Py_FatalError("Module index out of bounds.");
962
    }
963
964
    Py_INCREF(Py_None);
965
    return PyList_SetItem(interp->modules_by_index, index, Py_None);
966
}
967
968
// Used by finalize_modules()
969
void
970
_PyInterpreterState_ClearModules(PyInterpreterState *interp)
971
{
972
    if (!interp->modules_by_index) {
  Branch (972:9): [True: 0, False: 272]
973
        return;
974
    }
975
976
    Py_ssize_t i;
977
    for (i = 0; i < PyList_GET_SIZE(interp->modules_by_index); 
i++5.26k
) {
  Branch (977:17): [True: 5.26k, False: 272]
978
        PyObject *m = PyList_GET_ITEM(interp->modules_by_index, i);
979
        if (PyModule_Check(m)) {
980
            /* cleanup the saved copy of module dicts */
981
            PyModuleDef *md = PyModule_GetDef(m);
982
            if (md) {
  Branch (982:17): [True: 879, False: 45]
983
                Py_CLEAR(md->m_base.m_copy);
984
            }
985
        }
986
    }
987
988
    /* Setting modules_by_index to NULL could be dangerous, so we
989
       clear the list instead. */
990
    if (PyList_SetSlice(interp->modules_by_index,
  Branch (990:9): [True: 0, False: 272]
991
                        0, PyList_GET_SIZE(interp->modules_by_index),
992
                        NULL)) {
993
        PyErr_WriteUnraisable(interp->modules_by_index);
994
    }
995
}
996
997
void
998
PyThreadState_Clear(PyThreadState *tstate)
999
{
1000
    int verbose = _PyInterpreterState_GetConfig(tstate->interp)->verbose;
1001
1002
    if (verbose && 
tstate->cframe->current_frame != NULL4
) {
  Branch (1002:9): [True: 4, False: 5.97k]
  Branch (1002:20): [True: 0, False: 4]
1003
        /* bpo-20526: After the main thread calls
1004
           _PyRuntimeState_SetFinalizing() in Py_FinalizeEx(), threads must
1005
           exit when trying to take the GIL. If a thread exit in the middle of
1006
           _PyEval_EvalFrameDefault(), tstate->frame is not reset to its
1007
           previous value. It is more likely with daemon threads, but it can
1008
           happen with regular threads if threading._shutdown() fails
1009
           (ex: interrupted by CTRL+C). */
1010
        fprintf(stderr,
1011
          "PyThreadState_Clear: warning: thread still has a frame\n");
1012
    }
1013
1014
    /* Don't clear tstate->pyframe: it is a borrowed reference */
1015
1016
    Py_CLEAR(tstate->dict);
1017
    Py_CLEAR(tstate->async_exc);
1018
1019
    Py_CLEAR(tstate->curexc_type);
1020
    Py_CLEAR(tstate->curexc_value);
1021
    Py_CLEAR(tstate->curexc_traceback);
1022
1023
    Py_CLEAR(tstate->exc_state.exc_value);
1024
1025
    /* The stack of exception states should contain just this thread. */
1026
    if (verbose && 
tstate->exc_info != &tstate->exc_state4
) {
  Branch (1026:9): [True: 4, False: 5.97k]
  Branch (1026:20): [True: 0, False: 4]
1027
        fprintf(stderr,
1028
          "PyThreadState_Clear: warning: thread still has a generator\n");
1029
    }
1030
1031
    tstate->c_profilefunc = NULL;
1032
    tstate->c_tracefunc = NULL;
1033
    Py_CLEAR(tstate->c_profileobj);
1034
    Py_CLEAR(tstate->c_traceobj);
1035
1036
    Py_CLEAR(tstate->async_gen_firstiter);
1037
    Py_CLEAR(tstate->async_gen_finalizer);
1038
1039
    Py_CLEAR(tstate->context);
1040
1041
    if (tstate->on_delete != NULL) {
  Branch (1041:9): [True: 4.78k, False: 1.19k]
1042
        tstate->on_delete(tstate->on_delete_data);
1043
    }
1044
}
1045
1046
1047
/* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
1048
static void
1049
tstate_delete_common(PyThreadState *tstate,
1050
                     struct _gilstate_runtime_state *gilstate)
1051
{
1052
    _Py_EnsureTstateNotNULL(tstate);
1053
    PyInterpreterState *interp = tstate->interp;
1054
    if (interp == NULL) {
  Branch (1054:9): [True: 0, False: 5.97k]
1055
        Py_FatalError("NULL interpreter");
1056
    }
1057
    _PyRuntimeState *runtime = interp->runtime;
1058
1059
    HEAD_LOCK(runtime);
1060
    if (tstate->prev) {
  Branch (1060:9): [True: 1.63k, False: 4.34k]
1061
        tstate->prev->next = tstate->next;
1062
    }
1063
    else {
1064
        interp->threads.head = tstate->next;
1065
    }
1066
    if (tstate->next) {
  Branch (1066:9): [True: 5.70k, False: 272]
1067
        tstate->next->prev = tstate->prev;
1068
    }
1069
    HEAD_UNLOCK(runtime);
1070
1071
    if (gilstate->autoInterpreterState &&
  Branch (1071:9): [True: 5.87k, False: 103]
1072
        
PyThread_tss_get(&gilstate->autoTSSkey) == tstate5.87k
)
  Branch (1072:9): [True: 5.70k, False: 169]
1073
    {
1074
        PyThread_tss_set(&gilstate->autoTSSkey, NULL);
1075
    }
1076
    _PyStackChunk *chunk = tstate->datastack_chunk;
1077
    tstate->datastack_chunk = NULL;
1078
    while (chunk != NULL) {
  Branch (1078:12): [True: 5.97k, False: 5.97k]
1079
        _PyStackChunk *prev = chunk->previous;
1080
        _PyObject_VirtualFree(chunk, chunk->size);
1081
        chunk = prev;
1082
    }
1083
}
1084
1085
static void
1086
_PyThreadState_Delete(PyThreadState *tstate, int check_current)
1087
{
1088
    struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
1089
    if (check_current) {
  Branch (1089:9): [True: 0, False: 272]
1090
        if (tstate == _PyRuntimeGILState_GetThreadState(gilstate)) {
  Branch (1090:13): [True: 0, False: 0]
1091
            _Py_FatalErrorFormat(__func__, "tstate %p is still current", tstate);
1092
        }
1093
    }
1094
    tstate_delete_common(tstate, gilstate);
1095
    free_threadstate(tstate);
1096
}
1097
1098
1099
void
1100
PyThreadState_Delete(PyThreadState *tstate)
1101
{
1102
    _PyThreadState_Delete(tstate, 1);
1103
}
1104
1105
1106
void
1107
_PyThreadState_DeleteCurrent(PyThreadState *tstate)
1108
{
1109
    _Py_EnsureTstateNotNULL(tstate);
1110
    struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
1111
    tstate_delete_common(tstate, gilstate);
1112
    _PyRuntimeGILState_SetThreadState(gilstate, NULL);
1113
    _PyEval_ReleaseLock(tstate);
1114
    free_threadstate(tstate);
1115
}
1116
1117
void
1118
PyThreadState_DeleteCurrent(void)
1119
{
1120
    struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
1121
    PyThreadState *tstate = _PyRuntimeGILState_GetThreadState(gilstate);
1122
    _PyThreadState_DeleteCurrent(tstate);
1123
}
1124
1125
1126
/*
1127
 * Delete all thread states except the one passed as argument.
1128
 * Note that, if there is a current thread state, it *must* be the one
1129
 * passed as argument.  Also, this won't touch any other interpreters
1130
 * than the current one, since we don't know which thread state should
1131
 * be kept in those other interpreters.
1132
 */
1133
void
1134
_PyThreadState_DeleteExcept(_PyRuntimeState *runtime, PyThreadState *tstate)
1135
{
1136
    PyInterpreterState *interp = tstate->interp;
1137
1138
    HEAD_LOCK(runtime);
1139
    /* Remove all thread states, except tstate, from the linked list of
1140
       thread states.  This will allow calling PyThreadState_Clear()
1141
       without holding the lock. */
1142
    PyThreadState *list = interp->threads.head;
1143
    if (list == tstate) {
  Branch (1143:9): [True: 104, False: 0]
1144
        list = tstate->next;
1145
    }
1146
    if (tstate->prev) {
  Branch (1146:9): [True: 0, False: 104]
1147
        tstate->prev->next = tstate->next;
1148
    }
1149
    if (tstate->next) {
  Branch (1149:9): [True: 0, False: 104]
1150
        tstate->next->prev = tstate->prev;
1151
    }
1152
    tstate->prev = tstate->next = NULL;
1153
    interp->threads.head = tstate;
1154
    HEAD_UNLOCK(runtime);
1155
1156
    /* Clear and deallocate all stale thread states.  Even if this
1157
       executes Python code, we should be safe since it executes
1158
       in the current thread, not one of the stale threads. */
1159
    PyThreadState *p, *next;
1160
    for (p = list; p; 
p = next0
) {
  Branch (1160:20): [True: 0, False: 104]
1161
        next = p->next;
1162
        PyThreadState_Clear(p);
1163
        free_threadstate(p);
1164
    }
1165
}
1166
1167
1168
PyThreadState *
1169
_PyThreadState_UncheckedGet(void)
1170
{
1171
    return _PyThreadState_GET();
1172
}
1173
1174
1175
PyThreadState *
1176
PyThreadState_Get(void)
1177
{
1178
    PyThreadState *tstate = _PyThreadState_GET();
1179
    _Py_EnsureTstateNotNULL(tstate);
1180
    return tstate;
1181
}
1182
1183
1184
PyThreadState *
1185
_PyThreadState_Swap(struct _gilstate_runtime_state *gilstate, PyThreadState *newts)
1186
{
1187
    PyThreadState *oldts = _PyRuntimeGILState_GetThreadState(gilstate);
1188
1189
    _PyRuntimeGILState_SetThreadState(gilstate, newts);
1190
    /* It should not be possible for more than one thread state
1191
       to be used for a thread.  Check this the best we can in debug
1192
       builds.
1193
    */
1194
#if defined(Py_DEBUG)
1195
    if (newts) {
1196
        /* This can be called from PyEval_RestoreThread(). Similar
1197
           to it, we need to ensure errno doesn't change.
1198
        */
1199
        int err = errno;
1200
        PyThreadState *check = _PyGILState_GetThisThreadState(gilstate);
1201
        if (check && check->interp == newts->interp && check != newts)
1202
            Py_FatalError("Invalid thread state for this thread");
1203
        errno = err;
1204
    }
1205
#endif
1206
    return oldts;
1207
}
1208
1209
PyThreadState *
1210
PyThreadState_Swap(PyThreadState *newts)
1211
{
1212
    return _PyThreadState_Swap(&_PyRuntime.gilstate, newts);
1213
}
1214
1215
/* An extension mechanism to store arbitrary additional per-thread state.
1216
   PyThreadState_GetDict() returns a dictionary that can be used to hold such
1217
   state; the caller should pick a unique key and store its state there.  If
1218
   PyThreadState_GetDict() returns NULL, an exception has *not* been raised
1219
   and the caller should assume no per-thread state is available. */
1220
1221
PyObject *
1222
_PyThreadState_GetDict(PyThreadState *tstate)
1223
{
1224
    assert(tstate != NULL);
1225
    if (tstate->dict == NULL) {
  Branch (1225:9): [True: 322, False: 148k]
1226
        tstate->dict = PyDict_New();
1227
        if (tstate->dict == NULL) {
  Branch (1227:13): [True: 0, False: 322]
1228
            _PyErr_Clear(tstate);
1229
        }
1230
    }
1231
    return tstate->dict;
1232
}
1233
1234
1235
PyObject *
1236
PyThreadState_GetDict(void)
1237
{
1238
    PyThreadState *tstate = _PyThreadState_GET();
1239
    if (tstate == NULL) {
  Branch (1239:9): [True: 0, False: 138k]
1240
        return NULL;
1241
    }
1242
    return _PyThreadState_GetDict(tstate);
1243
}
1244
1245
1246
PyInterpreterState *
1247
PyThreadState_GetInterpreter(PyThreadState *tstate)
1248
{
1249
    assert(tstate != NULL);
1250
    return tstate->interp;
1251
}
1252
1253
1254
PyFrameObject*
1255
PyThreadState_GetFrame(PyThreadState *tstate)
1256
{
1257
    assert(tstate != NULL);
1258
    if (tstate->cframe->current_frame == NULL) {
  Branch (1258:9): [True: 0, False: 2.91k]
1259
        return NULL;
1260
    }
1261
    PyFrameObject *frame = _PyFrame_GetFrameObject(tstate->cframe->current_frame);
1262
    if (frame == NULL) {
  Branch (1262:9): [True: 0, False: 2.91k]
1263
        PyErr_Clear();
1264
    }
1265
    Py_XINCREF(frame);
1266
    return frame;
1267
}
1268
1269
1270
uint64_t
1271
PyThreadState_GetID(PyThreadState *tstate)
1272
{
1273
    assert(tstate != NULL);
1274
    return tstate->id;
1275
}
1276
1277
1278
/* Asynchronously raise an exception in a thread.
1279
   Requested by Just van Rossum and Alex Martelli.
1280
   To prevent naive misuse, you must write your own extension
1281
   to call this, or use ctypes.  Must be called with the GIL held.
1282
   Returns the number of tstates modified (normally 1, but 0 if `id` didn't
1283
   match any known thread id).  Can be called with exc=NULL to clear an
1284
   existing async exception.  This raises no exceptions. */
1285
1286
int
1287
PyThreadState_SetAsyncExc(unsigned long id, PyObject *exc)
1288
{
1289
    _PyRuntimeState *runtime = &_PyRuntime;
1290
    PyInterpreterState *interp = _PyRuntimeState_GetThreadState(runtime)->interp;
1291
1292
    /* Although the GIL is held, a few C API functions can be called
1293
     * without the GIL held, and in particular some that create and
1294
     * destroy thread and interpreter states.  Those can mutate the
1295
     * list of thread states we're traversing, so to prevent that we lock
1296
     * head_mutex for the duration.
1297
     */
1298
    HEAD_LOCK(runtime);
1299
    for (PyThreadState *tstate = interp->threads.head; tstate != NULL; 
tstate = tstate->next2
) {
  Branch (1299:56): [True: 4, False: 1]
1300
        if (tstate->thread_id != id) {
  Branch (1300:13): [True: 2, False: 2]
1301
            continue;
1302
        }
1303
1304
        /* Tricky:  we need to decref the current value
1305
         * (if any) in tstate->async_exc, but that can in turn
1306
         * allow arbitrary Python code to run, including
1307
         * perhaps calls to this function.  To prevent
1308
         * deadlock, we need to release head_mutex before
1309
         * the decref.
1310
         */
1311
        PyObject *old_exc = tstate->async_exc;
1312
        Py_XINCREF(exc);
1313
        tstate->async_exc = exc;
1314
        HEAD_UNLOCK(runtime);
1315
1316
        Py_XDECREF(old_exc);
1317
        _PyEval_SignalAsyncExc(tstate->interp);
1318
        return 1;
1319
    }
1320
    HEAD_UNLOCK(runtime);
1321
    return 0;
1322
}
1323
1324
/* Routines for advanced debuggers, requested by David Beazley.
1325
   Don't use unless you know what you are doing! */
1326
1327
PyInterpreterState *
1328
PyInterpreterState_Head(void)
1329
{
1330
    return _PyRuntime.interpreters.head;
1331
}
1332
1333
PyInterpreterState *
1334
PyInterpreterState_Main(void)
1335
{
1336
    return _PyInterpreterState_Main();
1337
}
1338
1339
PyInterpreterState *
1340
PyInterpreterState_Next(PyInterpreterState *interp) {
1341
    return interp->next;
1342
}
1343
1344
PyThreadState *
1345
PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
1346
    return interp->threads.head;
1347
}
1348
1349
PyThreadState *
1350
PyThreadState_Next(PyThreadState *tstate) {
1351
    return tstate->next;
1352
}
1353
1354
/* The implementation of sys._current_frames().  This is intended to be
1355
   called with the GIL held, as it will be when called via
1356
   sys._current_frames().  It's possible it would work fine even without
1357
   the GIL held, but haven't thought enough about that.
1358
*/
1359
PyObject *
1360
_PyThread_CurrentFrames(void)
1361
{
1362
    PyThreadState *tstate = _PyThreadState_GET();
1363
    if (_PySys_Audit(tstate, "sys._current_frames", NULL) < 0) {
  Branch (1363:9): [True: 0, False: 1]
1364
        return NULL;
1365
    }
1366
1367
    PyObject *result = PyDict_New();
1368
    if (result == NULL) {
  Branch (1368:9): [True: 0, False: 1]
1369
        return NULL;
1370
    }
1371
1372
    /* for i in all interpreters:
1373
     *     for t in all of i's thread states:
1374
     *          if t's frame isn't NULL, map t's id to its frame
1375
     * Because these lists can mutate even when the GIL is held, we
1376
     * need to grab head_mutex for the duration.
1377
     */
1378
    _PyRuntimeState *runtime = tstate->interp->runtime;
1379
    HEAD_LOCK(runtime);
1380
    PyInterpreterState *i;
1381
    for (i = runtime->interpreters.head; i != NULL; 
i = i->next1
) {
  Branch (1381:42): [True: 1, False: 1]
1382
        PyThreadState *t;
1383
        for (t = i->threads.head; t != NULL; 
t = t->next2
) {
  Branch (1383:35): [True: 2, False: 1]
1384
            _PyInterpreterFrame *frame = t->cframe->current_frame;
1385
            if (frame == NULL) {
  Branch (1385:17): [True: 0, False: 2]
1386
                continue;
1387
            }
1388
            PyObject *id = PyLong_FromUnsignedLong(t->thread_id);
1389
            if (id == NULL) {
  Branch (1389:17): [True: 0, False: 2]
1390
                goto fail;
1391
            }
1392
            int stat = PyDict_SetItem(result, id, (PyObject *)_PyFrame_GetFrameObject(frame));
1393
            Py_DECREF(id);
1394
            if (stat < 0) {
  Branch (1394:17): [True: 0, False: 2]
1395
                goto fail;
1396
            }
1397
        }
1398
    }
1399
    goto done;
1400
1401
fail:
1402
    Py_CLEAR(result);
1403
1404
done:
1405
    HEAD_UNLOCK(runtime);
1406
    return result;
1407
}
1408
1409
PyObject *
1410
_PyThread_CurrentExceptions(void)
1411
{
1412
    PyThreadState *tstate = _PyThreadState_GET();
1413
1414
    _Py_EnsureTstateNotNULL(tstate);
1415
1416
    if (_PySys_Audit(tstate, "sys._current_exceptions", NULL) < 0) {
  Branch (1416:9): [True: 0, False: 1]
1417
        return NULL;
1418
    }
1419
1420
    PyObject *result = PyDict_New();
1421
    if (result == NULL) {
  Branch (1421:9): [True: 0, False: 1]
1422
        return NULL;
1423
    }
1424
1425
    /* for i in all interpreters:
1426
     *     for t in all of i's thread states:
1427
     *          if t's frame isn't NULL, map t's id to its frame
1428
     * Because these lists can mutate even when the GIL is held, we
1429
     * need to grab head_mutex for the duration.
1430
     */
1431
    _PyRuntimeState *runtime = tstate->interp->runtime;
1432
    HEAD_LOCK(runtime);
1433
    PyInterpreterState *i;
1434
    for (i = runtime->interpreters.head; i != NULL; 
i = i->next1
) {
  Branch (1434:42): [True: 1, False: 1]
1435
        PyThreadState *t;
1436
        for (t = i->threads.head; t != NULL; 
t = t->next2
) {
  Branch (1436:35): [True: 2, False: 1]
1437
            _PyErr_StackItem *err_info = _PyErr_GetTopmostException(t);
1438
            if (err_info == NULL) {
  Branch (1438:17): [True: 0, False: 2]
1439
                continue;
1440
            }
1441
            PyObject *id = PyLong_FromUnsignedLong(t->thread_id);
1442
            if (id == NULL) {
  Branch (1442:17): [True: 0, False: 2]
1443
                goto fail;
1444
            }
1445
            PyObject *exc_info = _PyErr_StackItemToExcInfoTuple(err_info);
1446
            if (exc_info == NULL) {
  Branch (1446:17): [True: 0, False: 2]
1447
                Py_DECREF(id);
1448
                goto fail;
1449
            }
1450
            int stat = PyDict_SetItem(result, id, exc_info);
1451
            Py_DECREF(id);
1452
            Py_DECREF(exc_info);
1453
            if (stat < 0) {
  Branch (1453:17): [True: 0, False: 2]
1454
                goto fail;
1455
            }
1456
        }
1457
    }
1458
    goto done;
1459
1460
fail:
1461
    Py_CLEAR(result);
1462
1463
done:
1464
    HEAD_UNLOCK(runtime);
1465
    return result;
1466
}
1467
1468
/* Python "auto thread state" API. */
1469
1470
/* Keep this as a static, as it is not reliable!  It can only
1471
   ever be compared to the state for the *current* thread.
1472
   * If not equal, then it doesn't matter that the actual
1473
     value may change immediately after comparison, as it can't
1474
     possibly change to the current thread's state.
1475
   * If equal, then the current thread holds the lock, so the value can't
1476
     change until we yield the lock.
1477
*/
1478
static int
1479
PyThreadState_IsCurrent(PyThreadState *tstate)
1480
{
1481
    /* Must be the tstate for this thread */
1482
    struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
1483
    assert(_PyGILState_GetThisThreadState(gilstate) == tstate);
1484
    return tstate == _PyRuntimeGILState_GetThreadState(gilstate);
1485
}
1486
1487
/* Internal initialization/finalization functions called by
1488
   Py_Initialize/Py_FinalizeEx
1489
*/
1490
PyStatus
1491
_PyGILState_Init(_PyRuntimeState *runtime)
1492
{
1493
    struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
1494
    if (PyThread_tss_create(&gilstate->autoTSSkey) != 0) {
  Branch (1494:9): [True: 0, False: 107]
1495
        return _PyStatus_NO_MEMORY();
1496
    }
1497
    // PyThreadState_New() calls _PyGILState_NoteThreadState() which does
1498
    // nothing before autoInterpreterState is set.
1499
    assert(gilstate->autoInterpreterState == NULL);
1500
    return _PyStatus_OK();
1501
}
1502
1503
1504
PyStatus
1505
_PyGILState_SetTstate(PyThreadState *tstate)
1506
{
1507
    if (!_Py_IsMainInterpreter(tstate->interp)) {
  Branch (1507:9): [True: 171, False: 107]
1508
        /* Currently, PyGILState is shared by all interpreters. The main
1509
         * interpreter is responsible to initialize it. */
1510
        return _PyStatus_OK();
1511
    }
1512
1513
    /* must init with valid states */
1514
    assert(tstate != NULL);
1515
    assert(tstate->interp != NULL);
1516
1517
    struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
1518
1519
    gilstate->autoInterpreterState = tstate->interp;
1520
    assert(PyThread_tss_get(&gilstate->autoTSSkey) == NULL);
1521
    assert(tstate->gilstate_counter == 0);
1522
1523
    _PyGILState_NoteThreadState(gilstate, tstate);
1524
    return _PyStatus_OK();
1525
}
1526
1527
PyInterpreterState *
1528
_PyGILState_GetInterpreterStateUnsafe(void)
1529
{
1530
    return _PyRuntime.gilstate.autoInterpreterState;
1531
}
1532
1533
void
1534
_PyGILState_Fini(PyInterpreterState *interp)
1535
{
1536
    struct _gilstate_runtime_state *gilstate = &interp->runtime->gilstate;
1537
    PyThread_tss_delete(&gilstate->autoTSSkey);
1538
    gilstate->autoInterpreterState = NULL;
1539
}
1540
1541
#ifdef HAVE_FORK
1542
/* Reset the TSS key - called by PyOS_AfterFork_Child().
1543
 * This should not be necessary, but some - buggy - pthread implementations
1544
 * don't reset TSS upon fork(), see issue #10517.
1545
 */
1546
PyStatus
1547
_PyGILState_Reinit(_PyRuntimeState *runtime)
1548
{
1549
    struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
1550
    PyThreadState *tstate = _PyGILState_GetThisThreadState(gilstate);
1551
1552
    PyThread_tss_delete(&gilstate->autoTSSkey);
1553
    if (PyThread_tss_create(&gilstate->autoTSSkey) != 0) {
  Branch (1553:9): [True: 0, False: 0]
1554
        return _PyStatus_NO_MEMORY();
1555
    }
1556
1557
    /* If the thread had an associated auto thread state, reassociate it with
1558
     * the new key. */
1559
    if (tstate &&
  Branch (1559:9): [True: 0, False: 0]
1560
        PyThread_tss_set(&gilstate->autoTSSkey, (void *)tstate) != 0)
  Branch (1560:9): [True: 0, False: 0]
1561
    {
1562
        return _PyStatus_ERR("failed to set autoTSSkey");
1563
    }
1564
    return _PyStatus_OK();
1565
}
1566
#endif
1567
1568
/* When a thread state is created for a thread by some mechanism other than
1569
   PyGILState_Ensure, it's important that the GILState machinery knows about
1570
   it so it doesn't try to create another thread state for the thread (this is
1571
   a better fix for SF bug #1010677 than the first one attempted).
1572
*/
1573
static void
1574
_PyGILState_NoteThreadState(struct _gilstate_runtime_state *gilstate, PyThreadState* tstate)
1575
{
1576
    /* If autoTSSkey isn't initialized, this must be the very first
1577
       threadstate created in Py_Initialize().  Don't do anything for now
1578
       (we'll be back here when _PyGILState_Init is called). */
1579
    if (!gilstate->autoInterpreterState) {
  Branch (1579:9): [True: 107, False: 5.98k]
1580
        return;
1581
    }
1582
1583
    /* Stick the thread state for this thread in thread specific storage.
1584
1585
       The only situation where you can legitimately have more than one
1586
       thread state for an OS level thread is when there are multiple
1587
       interpreters.
1588
1589
       You shouldn't really be using the PyGILState_ APIs anyway (see issues
1590
       #10915 and #15751).
1591
1592
       The first thread state created for that given OS level thread will
1593
       "win", which seems reasonable behaviour.
1594
    */
1595
    if (PyThread_tss_get(&gilstate->autoTSSkey) == NULL) {
  Branch (1595:9): [True: 5.81k, False: 170]
1596
        if ((PyThread_tss_set(&gilstate->autoTSSkey, (void *)tstate)) != 0) {
  Branch (1596:13): [True: 0, False: 5.81k]
1597
            Py_FatalError("Couldn't create autoTSSkey mapping");
1598
        }
1599
    }
1600
1601
    /* PyGILState_Release must not try to delete this thread state. */
1602
    tstate->gilstate_counter = 1;
1603
}
1604
1605
/* The public functions */
1606
static PyThreadState *
1607
_PyGILState_GetThisThreadState(struct _gilstate_runtime_state *gilstate)
1608
{
1609
    if (gilstate->autoInterpreterState == NULL)
  Branch (1609:9): [True: 0, False: 1.87M]
1610
        return NULL;
1611
    return (PyThreadState *)PyThread_tss_get(&gilstate->autoTSSkey);
1612
}
1613
1614
PyThreadState *
1615
PyGILState_GetThisThreadState(void)
1616
{
1617
    return _PyGILState_GetThisThreadState(&_PyRuntime.gilstate);
1618
}
1619
1620
int
1621
PyGILState_Check(void)
1622
{
1623
    struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
1624
    if (!gilstate->check_enabled) {
  Branch (1624:9): [True: 0, False: 320k]
1625
        return 1;
1626
    }
1627
1628
    if (!PyThread_tss_is_created(&gilstate->autoTSSkey)) {
  Branch (1628:9): [True: 0, False: 320k]
1629
        return 1;
1630
    }
1631
1632
    PyThreadState *tstate = _PyRuntimeGILState_GetThreadState(gilstate);
1633
    if (tstate == NULL) {
  Branch (1633:9): [True: 0, False: 320k]
1634
        return 0;
1635
    }
1636
1637
    return (tstate == _PyGILState_GetThisThreadState(gilstate));
1638
}
1639
1640
PyGILState_STATE
1641
PyGILState_Ensure(void)
1642
{
1643
    _PyRuntimeState *runtime = &_PyRuntime;
1644
    struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
1645
1646
    /* Note that we do not auto-init Python here - apart from
1647
       potential races with 2 threads auto-initializing, pep-311
1648
       spells out other issues.  Embedders are expected to have
1649
       called Py_Initialize(). */
1650
1651
    /* Ensure that _PyEval_InitThreads() and _PyGILState_Init() have been
1652
       called by Py_Initialize() */
1653
    assert(_PyEval_ThreadsInitialized(runtime));
1654
    assert(gilstate->autoInterpreterState);
1655
1656
    PyThreadState *tcur = (PyThreadState *)PyThread_tss_get(&gilstate->autoTSSkey);
1657
    int current;
1658
    if (tcur == NULL) {
  Branch (1658:9): [True: 6, False: 2.33k]
1659
        /* Create a new Python thread state for this thread */
1660
        tcur = PyThreadState_New(gilstate->autoInterpreterState);
1661
        if (tcur == NULL) {
  Branch (1661:13): [True: 0, False: 6]
1662
            Py_FatalError("Couldn't create thread-state for new thread");
1663
        }
1664
1665
        /* This is our thread state!  We'll need to delete it in the
1666
           matching call to PyGILState_Release(). */
1667
        tcur->gilstate_counter = 0;
1668
        current = 0; /* new thread state is never current */
1669
    }
1670
    else {
1671
        current = PyThreadState_IsCurrent(tcur);
1672
    }
1673
1674
    if (current == 0) {
  Branch (1674:9): [True: 1.79k, False: 537]
1675
        PyEval_RestoreThread(tcur);
1676
    }
1677
1678
    /* Update our counter in the thread-state - no need for locks:
1679
       - tcur will remain valid as we hold the GIL.
1680
       - the counter is safe as we are the only thread "allowed"
1681
         to modify this value
1682
    */
1683
    ++tcur->gilstate_counter;
1684
1685
    return current ? 
PyGILState_LOCKED537
:
PyGILState_UNLOCKED1.79k
;
  Branch (1685:12): [True: 537, False: 1.79k]
1686
}
1687
1688
void
1689
PyGILState_Release(PyGILState_STATE oldstate)
1690
{
1691
    _PyRuntimeState *runtime = &_PyRuntime;
1692
    PyThreadState *tstate = PyThread_tss_get(&runtime->gilstate.autoTSSkey);
1693
    if (tstate == NULL) {
  Branch (1693:9): [True: 0, False: 2.33k]
1694
        Py_FatalError("auto-releasing thread-state, "
1695
                      "but no thread-state for this thread");
1696
    }
1697
1698
    /* We must hold the GIL and have our thread state current */
1699
    /* XXX - remove the check - the assert should be fine,
1700
       but while this is very new (April 2003), the extra check
1701
       by release-only users can't hurt.
1702
    */
1703
    if (!PyThreadState_IsCurrent(tstate)) {
  Branch (1703:9): [True: 0, False: 2.33k]
1704
        _Py_FatalErrorFormat(__func__,
1705
                             "thread state %p must be current when releasing",
1706
                             tstate);
1707
    }
1708
    assert(PyThreadState_IsCurrent(tstate));
1709
    --tstate->gilstate_counter;
1710
    assert(tstate->gilstate_counter >= 0); /* illegal counter value */
1711
1712
    /* If we're going to destroy this thread-state, we must
1713
     * clear it while the GIL is held, as destructors may run.
1714
     */
1715
    if (tstate->gilstate_counter == 0) {
  Branch (1715:9): [True: 6, False: 2.33k]
1716
        /* can't have been locked when we created it */
1717
        assert(oldstate == PyGILState_UNLOCKED);
1718
        PyThreadState_Clear(tstate);
1719
        /* Delete the thread-state.  Note this releases the GIL too!
1720
         * It's vital that the GIL be held here, to avoid shutdown
1721
         * races; see bugs 225673 and 1061968 (that nasty bug has a
1722
         * habit of coming back).
1723
         */
1724
        assert(_PyRuntimeGILState_GetThreadState(&runtime->gilstate) == tstate);
1725
        _PyThreadState_DeleteCurrent(tstate);
1726
    }
1727
    /* Release the lock if necessary */
1728
    else if (oldstate == PyGILState_UNLOCKED)
  Branch (1728:14): [True: 1.79k, False: 537]
1729
        PyEval_SaveThread();
1730
}
1731
1732
1733
/**************************/
1734
/* cross-interpreter data */
1735
/**************************/
1736
1737
/* cross-interpreter data */
1738
1739
crossinterpdatafunc _PyCrossInterpreterData_Lookup(PyObject *);
1740
1741
/* This is a separate func from _PyCrossInterpreterData_Lookup in order
1742
   to keep the registry code separate. */
1743
static crossinterpdatafunc
1744
_lookup_getdata(PyObject *obj)
1745
{
1746
    crossinterpdatafunc getdata = _PyCrossInterpreterData_Lookup(obj);
1747
    if (getdata == NULL && 
PyErr_Occurred() == 024
)
  Branch (1747:9): [True: 24, False: 617]
  Branch (1747:28): [True: 24, False: 0]
1748
        PyErr_Format(PyExc_ValueError,
1749
                     "%S does not support cross-interpreter data", obj);
1750
    return getdata;
1751
}
1752
1753
int
1754
_PyObject_CheckCrossInterpreterData(PyObject *obj)
1755
{
1756
    crossinterpdatafunc getdata = _lookup_getdata(obj);
1757
    if (getdata == NULL) {
  Branch (1757:9): [True: 24, False: 10]
1758
        return -1;
1759
    }
1760
    return 0;
1761
}
1762
1763
static int
1764
_check_xidata(PyThreadState *tstate, _PyCrossInterpreterData *data)
1765
{
1766
    // data->data can be anything, including NULL, so we don't check it.
1767
1768
    // data->obj may be NULL, so we don't check it.
1769
1770
    if (data->interp < 0) {
  Branch (1770:9): [True: 0, False: 604]
1771
        _PyErr_SetString(tstate, PyExc_SystemError, "missing interp");
1772
        return -1;
1773
    }
1774
1775
    if (data->new_object == NULL) {
  Branch (1775:9): [True: 0, False: 604]
1776
        _PyErr_SetString(tstate, PyExc_SystemError, "missing new_object func");
1777
        return -1;
1778
    }
1779
1780
    // data->free may be NULL, so we don't check it.
1781
1782
    return 0;
1783
}
1784
1785
int
1786
_PyObject_GetCrossInterpreterData(PyObject *obj, _PyCrossInterpreterData *data)
1787
{
1788
    PyThreadState *tstate = _PyThreadState_GET();
1789
#ifdef Py_DEBUG
1790
    // The caller must hold the GIL
1791
    _Py_EnsureTstateNotNULL(tstate);
1792
#endif
1793
    PyInterpreterState *interp = tstate->interp;
1794
1795
    // Reset data before re-populating.
1796
    *data = (_PyCrossInterpreterData){0};
1797
    data->free = PyMem_RawFree;  // Set a default that may be overridden.
1798
1799
    // Call the "getdata" func for the object.
1800
    Py_INCREF(obj);
1801
    crossinterpdatafunc getdata = _lookup_getdata(obj);
1802
    if (getdata == NULL) {
  Branch (1802:9): [True: 0, False: 607]
1803
        Py_DECREF(obj);
1804
        return -1;
1805
    }
1806
    int res = getdata(obj, data);
1807
    Py_DECREF(obj);
1808
    if (res != 0) {
  Branch (1808:9): [True: 3, False: 604]
1809
        return -1;
1810
    }
1811
1812
    // Fill in the blanks and validate the result.
1813
    data->interp = interp->id;
1814
    if (_check_xidata(tstate, data) != 0) {
  Branch (1814:9): [True: 0, False: 604]
1815
        _PyCrossInterpreterData_Release(data);
1816
        return -1;
1817
    }
1818
1819
    return 0;
1820
}
1821
1822
static void
1823
_release_xidata(void *arg)
1824
{
1825
    _PyCrossInterpreterData *data = (_PyCrossInterpreterData *)arg;
1826
    if (data->free != NULL) {
  Branch (1826:9): [True: 335, False: 263]
1827
        data->free(data->data);
1828
    }
1829
    Py_XDECREF(data->obj);
1830
}
1831
1832
static void
1833
_call_in_interpreter(struct _gilstate_runtime_state *gilstate,
1834
                     PyInterpreterState *interp,
1835
                     void (*func)(void *), void *arg)
1836
{
1837
    /* We would use Py_AddPendingCall() if it weren't specific to the
1838
     * main interpreter (see bpo-33608).  In the meantime we take a
1839
     * naive approach.
1840
     */
1841
    PyThreadState *save_tstate = NULL;
1842
    if (interp != _PyRuntimeGILState_GetThreadState(gilstate)->interp) {
  Branch (1842:9): [True: 15, False: 583]
1843
        // XXX Using the "head" thread isn't strictly correct.
1844
        PyThreadState *tstate = PyInterpreterState_ThreadHead(interp);
1845
        // XXX Possible GILState issues?
1846
        save_tstate = _PyThreadState_Swap(gilstate, tstate);
1847
    }
1848
1849
    func(arg);
1850
1851
    // Switch back.
1852
    if (save_tstate != NULL) {
  Branch (1852:9): [True: 15, False: 583]
1853
        _PyThreadState_Swap(gilstate, save_tstate);
1854
    }
1855
}
1856
1857
void
1858
_PyCrossInterpreterData_Release(_PyCrossInterpreterData *data)
1859
{
1860
    if (data->data == NULL && 
data->obj == NULL6
) {
  Branch (1860:9): [True: 6, False: 598]
  Branch (1860:31): [True: 6, False: 0]
1861
        // Nothing to release!
1862
        return;
1863
    }
1864
1865
    // Switch to the original interpreter.
1866
    PyInterpreterState *interp = _PyInterpreterState_LookUpID(data->interp);
1867
    if (interp == NULL) {
  Branch (1867:9): [True: 0, False: 598]
1868
        // The interpreter was already destroyed.
1869
        if (data->free != NULL) {
  Branch (1869:13): [True: 0, False: 0]
1870
            // XXX Someone leaked some memory...
1871
        }
1872
        return;
1873
    }
1874
1875
    // "Release" the data and/or the object.
1876
    struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
1877
    _call_in_interpreter(gilstate, interp, _release_xidata, data);
1878
}
1879
1880
PyObject *
1881
_PyCrossInterpreterData_NewObject(_PyCrossInterpreterData *data)
1882
{
1883
    return data->new_object(data);
1884
}
1885
1886
/* registry of {type -> crossinterpdatafunc} */
1887
1888
/* For now we use a global registry of shareable classes.  An
1889
   alternative would be to add a tp_* slot for a class's
1890
   crossinterpdatafunc. It would be simpler and more efficient. */
1891
1892
static int
1893
_register_xidata(struct _xidregistry *xidregistry, PyTypeObject *cls,
1894
                 crossinterpdatafunc getdata)
1895
{
1896
    // Note that we effectively replace already registered classes
1897
    // rather than failing.
1898
    struct _xidregitem *newhead = PyMem_RawMalloc(sizeof(struct _xidregitem));
1899
    if (newhead == NULL)
  Branch (1899:9): [True: 0, False: 5]
1900
        return -1;
1901
    newhead->cls = cls;
1902
    newhead->getdata = getdata;
1903
    newhead->next = xidregistry->head;
1904
    xidregistry->head = newhead;
1905
    return 0;
1906
}
1907
1908
static void _register_builtins_for_crossinterpreter_data(struct _xidregistry *xidregistry);
1909
1910
int
1911
_PyCrossInterpreterData_RegisterClass(PyTypeObject *cls,
1912
                                       crossinterpdatafunc getdata)
1913
{
1914
    if (!PyType_Check(cls)) {
  Branch (1914:9): [True: 0, False: 1]
1915
        PyErr_Format(PyExc_ValueError, "only classes may be registered");
1916
        return -1;
1917
    }
1918
    if (getdata == NULL) {
  Branch (1918:9): [True: 0, False: 1]
1919
        PyErr_Format(PyExc_ValueError, "missing 'getdata' func");
1920
        return -1;
1921
    }
1922
1923
    // Make sure the class isn't ever deallocated.
1924
    Py_INCREF((PyObject *)cls);
1925
1926
    struct _xidregistry *xidregistry = &_PyRuntime.xidregistry ;
1927
    PyThread_acquire_lock(xidregistry->mutex, WAIT_LOCK);
1928
    if (xidregistry->head == NULL) {
  Branch (1928:9): [True: 1, False: 0]
1929
        _register_builtins_for_crossinterpreter_data(xidregistry);
1930
    }
1931
    int res = _register_xidata(xidregistry, cls, getdata);
1932
    PyThread_release_lock(xidregistry->mutex);
1933
    return res;
1934
}
1935
1936
/* Cross-interpreter objects are looked up by exact match on the class.
1937
   We can reassess this policy when we move from a global registry to a
1938
   tp_* slot. */
1939
1940
crossinterpdatafunc
1941
_PyCrossInterpreterData_Lookup(PyObject *obj)
1942
{
1943
    struct _xidregistry *xidregistry = &_PyRuntime.xidregistry ;
1944
    PyObject *cls = PyObject_Type(obj);
1945
    crossinterpdatafunc getdata = NULL;
1946
    PyThread_acquire_lock(xidregistry->mutex, WAIT_LOCK);
1947
    struct _xidregitem *cur = xidregistry->head;
1948
    if (cur == NULL) {
  Branch (1948:9): [True: 0, False: 641]
1949
        _register_builtins_for_crossinterpreter_data(xidregistry);
1950
        cur = xidregistry->head;
1951
    }
1952
    for(; cur != NULL; 
cur = cur->next1.62k
) {
  Branch (1952:11): [True: 2.23k, False: 24]
1953
        if (cur->cls == (PyTypeObject *)cls) {
  Branch (1953:13): [True: 617, False: 1.62k]
1954
            getdata = cur->getdata;
1955
            break;
1956
        }
1957
    }
1958
    Py_DECREF(cls);
1959
    PyThread_release_lock(xidregistry->mutex);
1960
    return getdata;
1961
}
1962
1963
/* cross-interpreter data for builtin types */
1964
1965
struct _shared_bytes_data {
1966
    char *bytes;
1967
    Py_ssize_t len;
1968
};
1969
1970
static PyObject *
1971
_new_bytes_object(_PyCrossInterpreterData *data)
1972
{
1973
    struct _shared_bytes_data *shared = (struct _shared_bytes_data *)(data->data);
1974
    return PyBytes_FromStringAndSize(shared->bytes, shared->len);
1975
}
1976
1977
static int
1978
_bytes_shared(PyObject *obj, _PyCrossInterpreterData *data)
1979
{
1980
    struct _shared_bytes_data *shared = PyMem_NEW(struct _shared_bytes_data, 1);
1981
    if (PyBytes_AsStringAndSize(obj, &shared->bytes, &shared->len) < 0) {
  Branch (1981:9): [True: 0, False: 322]
1982
        return -1;
1983
    }
1984
    data->data = (void *)shared;
1985
    Py_INCREF(obj);
1986
    data->obj = obj;  // Will be "released" (decref'ed) when data released.
1987
    data->new_object = _new_bytes_object;
1988
    data->free = PyMem_Free;
1989
    return 0;
1990
}
1991
1992
struct _shared_str_data {
1993
    int kind;
1994
    const void *buffer;
1995
    Py_ssize_t len;
1996
};
1997
1998
static PyObject *
1999
_new_str_object(_PyCrossInterpreterData *data)
2000
{
2001
    struct _shared_str_data *shared = (struct _shared_str_data *)(data->data);
2002
    return PyUnicode_FromKindAndData(shared->kind, shared->buffer, shared->len);
2003
}
2004
2005
static int
2006
_str_shared(PyObject *obj, _PyCrossInterpreterData *data)
2007
{
2008
    struct _shared_str_data *shared = PyMem_NEW(struct _shared_str_data, 1);
2009
    shared->kind = PyUnicode_KIND(obj);
2010
    shared->buffer = PyUnicode_DATA(obj);
2011
    shared->len = PyUnicode_GET_LENGTH(obj);
2012
    data->data = (void *)shared;
2013
    Py_INCREF(obj);
2014
    data->obj = obj;  // Will be "released" (decref'ed) when data released.
2015
    data->new_object = _new_str_object;
2016
    data->free = PyMem_Free;
2017
    return 0;
2018
}
2019
2020
static PyObject *
2021
_new_long_object(_PyCrossInterpreterData *data)
2022
{
2023
    return PyLong_FromSsize_t((Py_ssize_t)(data->data));
2024
}
2025
2026
static int
2027
_long_shared(PyObject *obj, _PyCrossInterpreterData *data)
2028
{
2029
    /* Note that this means the size of shareable ints is bounded by
2030
     * sys.maxsize.  Hence on 32-bit architectures that is half the
2031
     * size of maximum shareable ints on 64-bit.
2032
     */
2033
    Py_ssize_t value = PyLong_AsSsize_t(obj);
2034
    if (value == -1 && 
PyErr_Occurred()4
) {
  Branch (2034:9): [True: 4, False: 263]
  Branch (2034:24): [True: 3, False: 1]
2035
        if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
  Branch (2035:13): [True: 3, False: 0]
2036
            PyErr_SetString(PyExc_OverflowError, "try sending as bytes");
2037
        }
2038
        return -1;
2039
    }
2040
    data->data = (void *)value;
2041
    data->obj = NULL;
2042
    data->new_object = _new_long_object;
2043
    data->free = NULL;
2044
    return 0;
2045
}
2046
2047
static PyObject *
2048
_new_none_object(_PyCrossInterpreterData *data)
2049
{
2050
    // XXX Singleton refcounts are problematic across interpreters...
2051
    Py_INCREF(Py_None);
2052
    return Py_None;
2053
}
2054
2055
static int
2056
_none_shared(PyObject *obj, _PyCrossInterpreterData *data)
2057
{
2058
    data->data = NULL;
2059
    // data->obj remains NULL
2060
    data->new_object = _new_none_object;
2061
    data->free = NULL;  // There is nothing to free.
2062
    return 0;
2063
}
2064
2065
static void
2066
_register_builtins_for_crossinterpreter_data(struct _xidregistry *xidregistry)
2067
{
2068
    // None
2069
    if (_register_xidata(xidregistry, (PyTypeObject *)PyObject_Type(Py_None), _none_shared) != 0) {
  Branch (2069:9): [True: 0, False: 1]
2070
        Py_FatalError("could not register None for cross-interpreter sharing");
2071
    }
2072
2073
    // int
2074
    if (_register_xidata(xidregistry, &PyLong_Type, _long_shared) != 0) {
  Branch (2074:9): [True: 0, False: 1]
2075
        Py_FatalError("could not register int for cross-interpreter sharing");
2076
    }
2077
2078
    // bytes
2079
    if (_register_xidata(xidregistry, &PyBytes_Type, _bytes_shared) != 0) {
  Branch (2079:9): [True: 0, False: 1]
2080
        Py_FatalError("could not register bytes for cross-interpreter sharing");
2081
    }
2082
2083
    // str
2084
    if (_register_xidata(xidregistry, &PyUnicode_Type, _str_shared) != 0) {
  Branch (2084:9): [True: 0, False: 1]
2085
        Py_FatalError("could not register str for cross-interpreter sharing");
2086
    }
2087
}
2088
2089
2090
_PyFrameEvalFunction
2091
_PyInterpreterState_GetEvalFrameFunc(PyInterpreterState *interp)
2092
{
2093
    if (interp->eval_frame == NULL) {
  Branch (2093:9): [True: 0, False: 0]
2094
        return _PyEval_EvalFrameDefault;
2095
    }
2096
    return interp->eval_frame;
2097
}
2098
2099
2100
void
2101
_PyInterpreterState_SetEvalFrameFunc(PyInterpreterState *interp,
2102
                                     _PyFrameEvalFunction eval_frame)
2103
{
2104
    if (eval_frame == _PyEval_EvalFrameDefault) {
  Branch (2104:9): [True: 2, False: 2]
2105
        interp->eval_frame = NULL;
2106
    }
2107
    else {
2108
        interp->eval_frame = eval_frame;
2109
    }
2110
}
2111
2112
2113
const PyConfig*
2114
_PyInterpreterState_GetConfig(PyInterpreterState *interp)
2115
{
2116
    return &interp->config;
2117
}
2118
2119
2120
int
2121
_PyInterpreterState_GetConfigCopy(PyConfig *config)
2122
{
2123
    PyInterpreterState *interp = PyInterpreterState_Get();
2124
2125
    PyStatus status = _PyConfig_Copy(config, &interp->config);
2126
    if (PyStatus_Exception(status)) {
  Branch (2126:9): [True: 0, False: 53]
2127
        _PyErr_SetFromPyStatus(status);
2128
        return -1;
2129
    }
2130
    return 0;
2131
}
2132
2133
2134
const PyConfig*
2135
_Py_GetConfig(void)
2136
{
2137
    assert(PyGILState_Check());
2138
    PyThreadState *tstate = _PyThreadState_GET();
2139
    _Py_EnsureTstateNotNULL(tstate);
2140
    return _PyInterpreterState_GetConfig(tstate->interp);
2141
}
2142
2143
#define MINIMUM_OVERHEAD 1000
2144
2145
static PyObject **
2146
push_chunk(PyThreadState *tstate, int size)
2147
{
2148
    int allocate_size = DATA_STACK_CHUNK_SIZE;
2149
    while (allocate_size < (int)sizeof(PyObject*)*(size + MINIMUM_OVERHEAD)) {
  Branch (2149:12): [True: 2, False: 13.1k]
2150
        allocate_size *= 2;
2151
    }
2152
    _PyStackChunk *new = allocate_chunk(allocate_size, tstate->datastack_chunk);
2153
    if (new == NULL) {
  Branch (2153:9): [True: 0, False: 13.1k]
2154
        return NULL;
2155
    }
2156
    if (tstate->datastack_chunk) {
  Branch (2156:9): [True: 7.16k, False: 5.98k]
2157
        tstate->datastack_chunk->top = tstate->datastack_top -
2158
                                       &tstate->datastack_chunk->data[0];
2159
    }
2160
    tstate->datastack_chunk = new;
2161
    tstate->datastack_limit = (PyObject **)(((char *)new) + allocate_size);
2162
    // When new is the "root" chunk (i.e. new->previous == NULL), we can keep
2163
    // _PyThreadState_PopFrame from freeing it later by "skipping" over the
2164
    // first element:
2165
    PyObject **res = &new->data[new->previous == NULL];
2166
    tstate->datastack_top = res + size;
2167
    return res;
2168
}
2169
2170
_PyInterpreterFrame *
2171
_PyThreadState_PushFrame(PyThreadState *tstate, size_t size)
2172
{
2173
    assert(size < INT_MAX/sizeof(PyObject *));
2174
    PyObject **base = tstate->datastack_top;
2175
    PyObject **top = base + size;
2176
    if (top >= tstate->datastack_limit) {
  Branch (2176:9): [True: 13.1k, False: 81.2M]
2177
        base = push_chunk(tstate, (int)size);
2178
    }
2179
    else {
2180
        tstate->datastack_top = top;
2181
    }
2182
    return (_PyInterpreterFrame *)base;
2183
}
2184
2185
void
2186
_PyThreadState_PopFrame(PyThreadState *tstate, _PyInterpreterFrame * frame)
2187
{
2188
    assert(tstate->datastack_chunk);
2189
    PyObject **base = (PyObject **)frame;
2190
    if (base == &tstate->datastack_chunk->data[0]) {
  Branch (2190:9): [True: 7.16k, False: 178M]
2191
        _PyStackChunk *chunk = tstate->datastack_chunk;
2192
        _PyStackChunk *previous = chunk->previous;
2193
        // push_chunk ensures that the root chunk is never popped:
2194
        assert(previous);
2195
        tstate->datastack_top = &previous->data[previous->top];
2196
        tstate->datastack_chunk = previous;
2197
        _PyObject_VirtualFree(chunk, chunk->size);
2198
        tstate->datastack_limit = (PyObject **)(((char *)previous) + previous->size);
2199
    }
2200
    else {
2201
        assert(tstate->datastack_top);
2202
        assert(tstate->datastack_top >= base);
2203
        tstate->datastack_top = base;
2204
    }
2205
}
2206
2207
2208
#ifdef __cplusplus
2209
}
2210
#endif