GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — cpython/Python/pystate.c at main · saurabh3737/cpython Expand file tree/ Copy pathpystate.cMore file actions 2088 lines (1774 loc) · 59.6 KB / Copy pathpystate.cFile metadata and controls2088 lines (1774 loc) · 59.6 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 /* Thread and interpreter state structures and their interfaces */ #include "Python.h" #include "pycore_ceval.h" #include "pycore_frame.h" #include "pycore_initconfig.h" #include "pycore_object.h" // _PyType_InitCache() #include "pycore_pyerrors.h" #include "pycore_pylifecycle.h" #include "pycore_pymem.h" // _PyMem_SetDefaultAllocator() #include "pycore_pystate.h" // _PyThreadState_GET() #include "pycore_sysmodule.h" /* -------------------------------------------------------------------------- CAUTION Always use PyMem_RawMalloc() and PyMem_RawFree() directly in this file. A number of these functions are advertised as safe to call when the GIL isn't held, and in a debug build Python redirects (e.g.) PyMem_NEW (etc) to Python's debugging obmalloc functions. Those aren't thread-safe (they rely on the GIL to avoid the expense of doing their own locking). -------------------------------------------------------------------------- */ #ifdef HAVE_DLOPEN #ifdef HAVE_DLFCN_H #include <dlfcn.h> #endif #if !HAVE_DECL_RTLD_LAZY #define RTLD_LAZY 1 #endif #endif #ifdef __cplusplus extern "C" { #endif #define _PyRuntimeGILState_GetThreadState(gilstate) \ ((PyThreadState*)_Py_atomic_load_relaxed(&(gilstate)->tstate_current)) #define _PyRuntimeGILState_SetThreadState(gilstate, value) \ _Py_atomic_store_relaxed(&(gilstate)->tstate_current, \ (uintptr_t)(value)) /* Forward declarations */ static PyThreadState *_PyGILState_GetThisThreadState(struct _gilstate_runtime_state *gilstate); static void _PyThreadState_Delete(PyThreadState *tstate, int check_current); static PyStatus _PyRuntimeState_Init_impl(_PyRuntimeState *runtime) { /* We preserve the hook across init, because there is currently no public API to set it between runtime initialization and interpreter initialization. */ void *open_code_hook = runtime->open_code_hook; void *open_code_userdata = runtime->open_code_userdata; _Py_AuditHookEntry *audit_hook_head = runtime->audit_hook_head; // bpo-42882: Preserve next_index value if Py_Initialize()/Py_Finalize() // is called multiple times. Py_ssize_t unicode_next_index = runtime->unicode_ids.next_index; memset(runtime, 0, sizeof(*runtime)); runtime->open_code_hook = open_code_hook; runtime->open_code_userdata = open_code_userdata; runtime->audit_hook_head = audit_hook_head; _PyEval_InitRuntimeState(&runtime->ceval); PyPreConfig_InitPythonConfig(&runtime->preconfig); runtime->gilstate.check_enabled = 1; /* A TSS key must be initialized with Py_tss_NEEDS_INIT in accordance with the specification. */ Py_tss_t initial = Py_tss_NEEDS_INIT; runtime->gilstate.autoTSSkey = initial; runtime->interpreters.mutex = PyThread_allocate_lock(); if (runtime->interpreters.mutex == NULL) { return _PyStatus_NO_MEMORY(); } runtime->interpreters.next_id = -1; runtime->xidregistry.mutex = PyThread_allocate_lock(); if (runtime->xidregistry.mutex == NULL) { return _PyStatus_NO_MEMORY(); } // Set it to the ID of the main thread of the main interpreter. runtime->main_thread = PyThread_get_thread_ident(); runtime->unicode_ids.lock = PyThread_allocate_lock(); if (runtime->unicode_ids.lock == NULL) { return _PyStatus_NO_MEMORY(); } runtime->unicode_ids.next_index = unicode_next_index; return _PyStatus_OK(); } PyStatus _PyRuntimeState_Init(_PyRuntimeState *runtime) { /* Force default allocator, since _PyRuntimeState_Fini() must use the same allocator than this function. */ PyMemAllocatorEx old_alloc; _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); PyStatus status = _PyRuntimeState_Init_impl(runtime); PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); return status; } void _PyRuntimeState_Fini(_PyRuntimeState *runtime) { /* Force the allocator used by _PyRuntimeState_Init(). */ PyMemAllocatorEx old_alloc; _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); #define FREE_LOCK(LOCK) \ if (LOCK != NULL) { \ PyThread_free_lock(LOCK); \ LOCK = NULL; \ } FREE_LOCK(runtime->interpreters.mutex); FREE_LOCK(runtime->xidregistry.mutex); FREE_LOCK(runtime->unicode_ids.lock); #undef FREE_LOCK PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); } #ifdef HAVE_FORK /* This function is called from PyOS_AfterFork_Child to ensure that newly created child processes do not share locks with the parent. */ PyStatus _PyRuntimeState_ReInitThreads(_PyRuntimeState *runtime) { // This was initially set in _PyRuntimeState_Init(). runtime->main_thread = PyThread_get_thread_ident(); /* Force default allocator, since _PyRuntimeState_Fini() must use the same allocator than this function. */ PyMemAllocatorEx old_alloc; _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); int reinit_interp = _PyThread_at_fork_reinit(&runtime->interpreters.mutex); int reinit_main_id = _PyThread_at_fork_reinit(&runtime->interpreters.main->id_mutex); int reinit_xidregistry = _PyThread_at_fork_reinit(&runtime->xidregistry.mutex); int reinit_unicode_ids = _PyThread_at_fork_reinit(&runtime->unicode_ids.lock); PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); if (reinit_interp < 0 || reinit_main_id < 0 || reinit_xidregistry < 0 || reinit_unicode_ids < 0) { return _PyStatus_ERR("Failed to reinitialize runtime locks"); } return _PyStatus_OK(); } #endif #define HEAD_LOCK(runtime) \ PyThread_acquire_lock((runtime)->interpreters.mutex, WAIT_LOCK) #define HEAD_UNLOCK(runtime) \ PyThread_release_lock((runtime)->interpreters.mutex) /* Forward declaration */ static void _PyGILState_NoteThreadState( struct _gilstate_runtime_state *gilstate, PyThreadState* tstate); PyStatus _PyInterpreterState_Enable(_PyRuntimeState *runtime) { struct pyinterpreters *interpreters = &runtime->interpreters; interpreters->next_id = 0; /* Py_Finalize() calls _PyRuntimeState_Fini() which clears the mutex. Create a new mutex if needed. */ if (interpreters->mutex == NULL) { /* Force default allocator, since _PyRuntimeState_Fini() must use the same allocator than this function. */ PyMemAllocatorEx old_alloc; _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); interpreters->mutex = PyThread_allocate_lock(); PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); if (interpreters->mutex == NULL) { return _PyStatus_ERR("Can't initialize threads for interpreter"); } } return _PyStatus_OK(); } PyInterpreterState * PyInterpreterState_New(void) { PyThreadState *tstate = _PyThreadState_GET(); /* tstate is NULL when Py_InitializeFromConfig() calls PyInterpreterState_New() to create the main interpreter. */ if (_PySys_Audit(tstate, "cpython.PyInterpreterState_New", NULL) < 0) { return NULL; } PyInterpreterState *interp = PyMem_RawCalloc(1, sizeof(PyInterpreterState)); if (interp == NULL) { return NULL; } interp->id_refcount = -1; /* Don't get runtime from tstate since tstate can be NULL */ _PyRuntimeState *runtime = &_PyRuntime; interp->runtime = runtime; if (_PyEval_InitState(&interp->ceval) < 0) { goto out_of_memory; } _PyGC_InitState(&interp->gc); PyConfig_InitPythonConfig(&interp->config); _PyType_InitCache(interp); interp->eval_frame = _PyEval_EvalFrameDefault; #ifdef HAVE_DLOPEN #if HAVE_DECL_RTLD_NOW interp->dlopenflags = RTLD_NOW; #else interp->dlopenflags = RTLD_LAZY; #endif #endif struct pyinterpreters *interpreters = &runtime->interpreters; HEAD_LOCK(runtime); if (interpreters->next_id < 0) { /* overflow or Py_Initialize() not called! */ if (tstate != NULL) { _PyErr_SetString(tstate, PyExc_RuntimeError, "failed to get an interpreter ID"); } PyMem_RawFree(interp); interp = NULL; } else { interp->id = interpreters->next_id; interpreters->next_id += 1; interp->next = interpreters->head; if (interpreters->main == NULL) { interpreters->main = interp; } interpreters->head = interp; } HEAD_UNLOCK(runtime); if (interp == NULL) { return NULL; } interp->tstate_next_unique_id = 0; interp->audit_hooks = NULL; return interp; out_of_memory: if (tstate != NULL) { _PyErr_NoMemory(tstate); } PyMem_RawFree(interp); return NULL; } static void interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate) { _PyRuntimeState *runtime = interp->runtime; if (_PySys_Audit(tstate, "cpython.PyInterpreterState_Clear", NULL) < 0) { _PyErr_Clear(tstate); } HEAD_LOCK(runtime); for (PyThreadState *p = interp->tstate_head; p != NULL; p = p->next) { PyThreadState_Clear(p); } HEAD_UNLOCK(runtime); Py_CLEAR(interp->audit_hooks); PyConfig_Clear(&interp->config); Py_CLEAR(interp->codec_search_path); Py_CLEAR(interp->codec_search_cache); Py_CLEAR(interp->codec_error_registry); Py_CLEAR(interp->modules); Py_CLEAR(interp->modules_by_index); Py_CLEAR(interp->builtins_copy); Py_CLEAR(interp->importlib); Py_CLEAR(interp->import_func); Py_CLEAR(interp->dict); #ifdef HAVE_FORK Py_CLEAR(interp->before_forkers); Py_CLEAR(interp->after_forkers_parent); Py_CLEAR(interp->after_forkers_child); #endif _PyAST_Fini(interp); _PyWarnings_Fini(interp); _PyAtExit_Fini(interp); // All Python types must be destroyed before the last GC collection. Python // types create a reference cycle to themselves in their in their // PyTypeObject.tp_mro member (the tuple contains the type). /* Last garbage collection on this interpreter */ _PyGC_CollectNoFail(tstate); _PyGC_Fini(interp); /* We don't clear sysdict and builtins until the end of this function. Because clearing other attributes can execute arbitrary Python code which requires sysdict and builtins. */ PyDict_Clear(interp->sysdict); PyDict_Clear(interp->builtins); Py_CLEAR(interp->sysdict); Py_CLEAR(interp->builtins); // XXX Once we have one allocator per interpreter (i.e. // per-interpreter GC) we must ensure that all of the interpreter's // objects have been cleaned up at the point. } void PyInterpreterState_Clear(PyInterpreterState *interp) { // Use the current Python thread state to call audit hooks and to collect // garbage. It can be different than the current Python thread state // of 'interp'. PyThreadState *current_tstate = _PyThreadState_GET(); interpreter_clear(interp, current_tstate); } void _PyInterpreterState_Clear(PyThreadState *tstate) { interpreter_clear(tstate->interp, tstate); } static void zapthreads(PyInterpreterState *interp, int check_current) { PyThreadState *tstate; /* No need to lock the mutex here because this should only happen when the threads are all really dead (XXX famous last words). */ while ((tstate = interp->tstate_head) != NULL) { _PyThreadState_Delete(tstate, check_current); } } void PyInterpreterState_Delete(PyInterpreterState *interp) { _PyRuntimeState *runtime = interp->runtime; struct pyinterpreters *interpreters = &runtime->interpreters; zapthreads(interp, 0); _PyEval_FiniState(&interp->ceval); /* Delete current thread. After this, many C API calls become crashy. */ _PyThreadState_Swap(&runtime->gilstate, NULL); HEAD_LOCK(runtime); PyInterpreterState **p; for (p = &interpreters->head; ; p = &(*p)->next) { if (*p == NULL) { Py_FatalError("NULL interpreter"); } if (*p == interp) { break; } } if (interp->tstate_head != NULL) { Py_FatalError("remaining threads"); } *p = interp->next; if (interpreters->main == interp) { interpreters->main = NULL; if (interpreters->head != NULL) { Py_FatalError("remaining subinterpreters"); } } HEAD_UNLOCK(runtime); if (interp->id_mutex != NULL) { PyThread_free_lock(interp->id_mutex); } PyMem_RawFree(interp); } #ifdef HAVE_FORK /* * Delete all interpreter states except the main interpreter. If there * is a current interpreter state, it *must* be the main interpreter. */ PyStatus _PyInterpreterState_DeleteExceptMain(_PyRuntimeState *runtime) { struct _gilstate_runtime_state *gilstate = &runtime->gilstate; struct pyinterpreters *interpreters = &runtime->interpreters; PyThreadState *tstate = _PyThreadState_Swap(gilstate, NULL); if (tstate != NULL && tstate->interp != interpreters->main) { return _PyStatus_ERR("not main interpreter"); } HEAD_LOCK(runtime); PyInterpreterState *interp = interpreters->head; interpreters->head = NULL; while (interp != NULL) { if (interp == interpreters->main) { interpreters->main->next = NULL; interpreters->head = interp; interp = interp->next; continue; } PyInterpreterState_Clear(interp); // XXX must activate? zapthreads(interp, 1); if (interp->id_mutex != NULL) { PyThread_free_lock(interp->id_mutex); } PyInterpreterState *prev_interp = interp; interp = interp->next; PyMem_RawFree(prev_interp); } HEAD_UNLOCK(runtime); if (interpreters->head == NULL) { return _PyStatus_ERR("missing main interpreter"); } _PyThreadState_Swap(gilstate, tstate); return _PyStatus_OK(); } #endif PyInterpreterState * PyInterpreterState_Get(void) { PyThreadState *tstate = _PyThreadState_GET(); _Py_EnsureTstateNotNULL(tstate); PyInterpreterState *interp = tstate->interp; if (interp == NULL) { Py_FatalError("no current interpreter"); } return interp; } int64_t PyInterpreterState_GetID(PyInterpreterState *interp) { if (interp == NULL) { PyErr_SetString(PyExc_RuntimeError, "no interpreter provided"); return -1; } return interp->id; } static PyInterpreterState * interp_look_up_id(_PyRuntimeState *runtime, int64_t requested_id) { PyInterpreterState *interp = runtime->interpreters.head; while (interp != NULL) { int64_t id = PyInterpreterState_GetID(interp); if (id < 0) { return NULL; } if (requested_id == id) { return interp; } interp = PyInterpreterState_Next(interp); } return NULL; } PyInterpreterState * _PyInterpreterState_LookUpID(int64_t requested_id) { PyInterpreterState *interp = NULL; if (requested_id >= 0) { _PyRuntimeState *runtime = &_PyRuntime; HEAD_LOCK(runtime); interp = interp_look_up_id(runtime, requested_id); HEAD_UNLOCK(runtime); } if (interp == NULL && !PyErr_Occurred()) { PyErr_Format(PyExc_RuntimeError, "unrecognized interpreter ID %lld", requested_id); } return interp; } int _PyInterpreterState_IDInitref(PyInterpreterState *interp) { if (interp->id_mutex != NULL) { return 0; } interp->id_mutex = PyThread_allocate_lock(); if (interp->id_mutex == NULL) { PyErr_SetString(PyExc_RuntimeError, "failed to create init interpreter ID mutex"); return -1; } interp->id_refcount = 0; return 0; } int _PyInterpreterState_IDIncref(PyInterpreterState *interp) { if (_PyInterpreterState_IDInitref(interp) < 0) { return -1; } PyThread_acquire_lock(interp->id_mutex, WAIT_LOCK); interp->id_refcount += 1; PyThread_release_lock(interp->id_mutex); return 0; } void _PyInterpreterState_IDDecref(PyInterpreterState *interp) { assert(interp->id_mutex != NULL); struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate; PyThread_acquire_lock(interp->id_mutex, WAIT_LOCK); assert(interp->id_refcount != 0); interp->id_refcount -= 1; int64_t refcount = interp->id_refcount; PyThread_release_lock(interp->id_mutex); if (refcount == 0 && interp->requires_idref) { // XXX Using the "head" thread isn't strictly correct. PyThreadState *tstate = PyInterpreterState_ThreadHead(interp); // XXX Possible GILState issues? PyThreadState *save_tstate = _PyThreadState_Swap(gilstate, tstate); Py_EndInterpreter(tstate); _PyThreadState_Swap(gilstate, save_tstate); } } int _PyInterpreterState_RequiresIDRef(PyInterpreterState *interp) { return interp->requires_idref; } void _PyInterpreterState_RequireIDRef(PyInterpreterState *interp, int required) { interp->requires_idref = required ? 1 : 0; } PyObject * _PyInterpreterState_GetMainModule(PyInterpreterState *interp) { if (interp->modules == NULL) { PyErr_SetString(PyExc_RuntimeError, "interpreter not initialized"); return NULL; } return PyMapping_GetItemString(interp->modules, "__main__"); } PyObject * PyInterpreterState_GetDict(PyInterpreterState *interp) { if (interp->dict == NULL) { interp->dict = PyDict_New(); if (interp->dict == NULL) { PyErr_Clear(); } } /* Returning NULL means no per-interpreter dict is available. */ return interp->dict; } /* Minimum size of data stack chunk */ #define DATA_STACK_CHUNK_SIZE (16*1024) static _PyStackChunk* allocate_chunk(int size_in_bytes, _PyStackChunk* previous) { assert(size_in_bytes % sizeof(PyObject **) == 0); _PyStackChunk *res = _PyObject_VirtualAlloc(size_in_bytes); if (res == NULL) { return NULL; } res->previous = previous; res->size = size_in_bytes; res->top = 0; return res; } static PyThreadState * new_threadstate(PyInterpreterState *interp, int init) { _PyRuntimeState *runtime = interp->runtime; PyThreadState *tstate = (PyThreadState *)PyMem_RawMalloc(sizeof(PyThreadState)); if (tstate == NULL) { return NULL; } tstate->interp = interp; tstate->frame = NULL; tstate->recursion_depth = 0; tstate->recursion_headroom = 0; tstate->stackcheck_counter = 0; tstate->tracing = 0; tstate->root_cframe.use_tracing = 0; tstate->cframe = &tstate->root_cframe; tstate->gilstate_counter = 0; tstate->async_exc = NULL; tstate->thread_id = PyThread_get_thread_ident(); #ifdef PY_HAVE_THREAD_NATIVE_ID tstate->native_thread_id = PyThread_get_thread_native_id(); #else tstate->native_thread_id = 0; #endif tstate->dict = NULL; tstate->curexc_type = NULL; tstate->curexc_value = NULL; tstate->curexc_traceback = NULL; tstate->exc_state.exc_type = NULL; tstate->exc_state.exc_value = NULL; tstate->exc_state.exc_traceback = NULL; tstate->exc_state.previous_item = NULL; tstate->exc_info = &tstate->exc_state; tstate->c_profilefunc = NULL; tstate->c_tracefunc = NULL; tstate->c_profileobj = NULL; tstate->c_traceobj = NULL; tstate->trash_delete_nesting = 0; tstate->trash_delete_later = NULL; tstate->on_delete = NULL; tstate->on_delete_data = NULL; tstate->coroutine_origin_tracking_depth = 0; tstate->async_gen_firstiter = NULL; tstate->async_gen_finalizer = NULL; tstate->context = NULL; tstate->context_ver = 1; tstate->datastack_chunk = allocate_chunk(DATA_STACK_CHUNK_SIZE, NULL); if (tstate->datastack_chunk == NULL) { PyMem_RawFree(tstate); return NULL; } /* If top points to entry 0, then _PyThreadState_PopFrame will try to pop this chunk */ tstate->datastack_top = &tstate->datastack_chunk->data[1]; tstate->datastack_limit = (PyObject **)(((char *)tstate->datastack_chunk) + DATA_STACK_CHUNK_SIZE); /* Mark trace_info as uninitialized */ tstate->trace_info.code = NULL; if (init) { _PyThreadState_Init(tstate); } HEAD_LOCK(runtime); tstate->id = ++interp->tstate_next_unique_id; tstate->prev = NULL; tstate->next = interp->tstate_head; if (tstate->next) tstate->next->prev = tstate; interp->tstate_head = tstate; HEAD_UNLOCK(runtime); return tstate; } PyThreadState * PyThreadState_New(PyInterpreterState *interp) { return new_threadstate(interp, 1); } PyThreadState * _PyThreadState_Prealloc(PyInterpreterState *interp) { return new_threadstate(interp, 0); } void _PyThreadState_Init(PyThreadState *tstate) { _PyGILState_NoteThreadState(&tstate->interp->runtime->gilstate, tstate); } PyObject* PyState_FindModule(struct PyModuleDef* module) { Py_ssize_t index = module->m_base.m_index; PyInterpreterState *state = _PyInterpreterState_GET(); PyObject *res; if (module->m_slots) { return NULL; } if (index == 0) return NULL; if (state->modules_by_index == NULL) return NULL; if (index >= PyList_GET_SIZE(state->modules_by_index)) return NULL; res = PyList_GET_ITEM(state->modules_by_index, index); return res==Py_None ? NULL : res; } int _PyState_AddModule(PyThreadState *tstate, PyObject* module, struct PyModuleDef* def) { if (!def) { assert(_PyErr_Occurred(tstate)); return -1; } if (def->m_slots) { _PyErr_SetString(tstate, PyExc_SystemError, "PyState_AddModule called on module with slots"); return -1; } PyInterpreterState *interp = tstate->interp; if (!interp->modules_by_index) { interp->modules_by_index = PyList_New(0); if (!interp->modules_by_index) { return -1; } } while (PyList_GET_SIZE(interp->modules_by_index) <= def->m_base.m_index) { if (PyList_Append(interp->modules_by_index, Py_None) < 0) { return -1; } } Py_INCREF(module); return PyList_SetItem(interp->modules_by_index, def->m_base.m_index, module); } int PyState_AddModule(PyObject* module, struct PyModuleDef* def) { if (!def) { Py_FatalError("module definition is NULL"); return -1; } PyThreadState *tstate = _PyThreadState_GET(); PyInterpreterState *interp = tstate->interp; Py_ssize_t index = def->m_base.m_index; if (interp->modules_by_index && index < PyList_GET_SIZE(interp->modules_by_index) && module == PyList_GET_ITEM(interp->modules_by_index, index)) { _Py_FatalErrorFormat(__func__, "module %p already added", module); return -1; } return _PyState_AddModule(tstate, module, def); } int PyState_RemoveModule(struct PyModuleDef* def) { PyThreadState *tstate = _PyThreadState_GET(); PyInterpreterState *interp = tstate->interp; if (def->m_slots) { _PyErr_SetString(tstate, PyExc_SystemError, "PyState_RemoveModule called on module with slots"); return -1; } Py_ssize_t index = def->m_base.m_index; if (index == 0) { Py_FatalError("invalid module index"); } if (interp->modules_by_index == NULL) { Py_FatalError("Interpreters module-list not accessible."); } if (index > PyList_GET_SIZE(interp->modules_by_index)) { Py_FatalError("Module index out of bounds."); } Py_INCREF(Py_None); return PyList_SetItem(interp->modules_by_index, index, Py_None); } // Used by finalize_modules() void _PyInterpreterState_ClearModules(PyInterpreterState *interp) { if (!interp->modules_by_index) { return; } Py_ssize_t i; for (i = 0; i < PyList_GET_SIZE(interp->modules_by_index); i++) { PyObject *m = PyList_GET_ITEM(interp->modules_by_index, i); if (PyModule_Check(m)) { /* cleanup the saved copy of module dicts */ PyModuleDef *md = PyModule_GetDef(m); if (md) { Py_CLEAR(md->m_base.m_copy); } } } /* Setting modules_by_index to NULL could be dangerous, so we clear the list instead. */ if (PyList_SetSlice(interp->modules_by_index, 0, PyList_GET_SIZE(interp->modules_by_index), NULL)) { PyErr_WriteUnraisable(interp->modules_by_index); } } void PyThreadState_Clear(PyThreadState *tstate) { int verbose = _PyInterpreterState_GetConfig(tstate->interp)->verbose; if (verbose && tstate->frame != NULL) { /* bpo-20526: After the main thread calls _PyRuntimeState_SetFinalizing() in Py_FinalizeEx(), threads must exit when trying to take the GIL. If a thread exit in the middle of _PyEval_EvalFrameDefault(), tstate->frame is not reset to its previous value. It is more likely with daemon threads, but it can happen with regular threads if threading._shutdown() fails (ex: interrupted by CTRL+C). */ fprintf(stderr, "PyThreadState_Clear: warning: thread still has a frame\n"); } /* Don't clear tstate->pyframe: it is a borrowed reference */ Py_CLEAR(tstate->dict); Py_CLEAR(tstate->async_exc); Py_CLEAR(tstate->curexc_type); Py_CLEAR(tstate->curexc_value); Py_CLEAR(tstate->curexc_traceback); Py_CLEAR(tstate->exc_state.exc_type); Py_CLEAR(tstate->exc_state.exc_value); Py_CLEAR(tstate->exc_state.exc_traceback); /* The stack of exception states should contain just this thread. */ if (verbose && tstate->exc_info != &tstate->exc_state) { fprintf(stderr, "PyThreadState_Clear: warning: thread still has a generator\n"); } tstate->c_profilefunc = NULL; tstate->c_tracefunc = NULL; Py_CLEAR(tstate->c_profileobj); Py_CLEAR(tstate->c_traceobj); Py_CLEAR(tstate->async_gen_firstiter); Py_CLEAR(tstate->async_gen_finalizer); Py_CLEAR(tstate->context); if (tstate->on_delete != NULL) { tstate->on_delete(tstate->on_delete_data); } } /* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */ static void tstate_delete_common(PyThreadState *tstate, struct _gilstate_runtime_state *gilstate) { _Py_EnsureTstateNotNULL(tstate); PyInterpreterState *interp = tstate->interp; if (interp == NULL) { Py_FatalError("NULL interpreter"); } _PyRuntimeState *runtime = interp->runtime; HEAD_LOCK(runtime); if (tstate->prev) { tstate->prev->next = tstate->next; } else { interp->tstate_head = tstate->next; } if (tstate->next) { tstate->next->prev = tstate->prev; } HEAD_UNLOCK(runtime); if (gilstate->autoInterpreterState && PyThread_tss_get(&gilstate->autoTSSkey) == tstate) { PyThread_tss_set(&gilstate->autoTSSkey, NULL); } _PyStackChunk *chunk = tstate->datastack_chunk; tstate->datastack_chunk = NULL; while (chunk != NULL) { _PyStackChunk *prev = chunk->previous; _PyObject_VirtualFree(chunk, chunk->size); chunk = prev; } } static void _PyThreadState_Delete(PyThreadState *tstate, int check_current) { struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate; if (check_current) { if (tstate == _PyRuntimeGILState_GetThreadState(gilstate)) { _Py_FatalErrorFormat(__func__, "tstate %p is still current", tstate); } } tstate_delete_common(tstate, gilstate); PyMem_RawFree(tstate); } void PyThreadState_Delete(PyThreadState *tstate) { _PyThreadState_Delete(tstate, 1); } void _PyThreadState_DeleteCurrent(PyThreadState *tstate) { _Py_EnsureTstateNotNULL(tstate); struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate; tstate_delete_common(tstate, gilstate); _PyRuntimeGILState_SetThreadState(gilstate, NULL); _PyEval_ReleaseLock(tstate); PyMem_RawFree(tstate); } void PyThreadState_DeleteCurrent(void) { struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate; PyThreadState *tstate = _PyRuntimeGILState_GetThreadState(gilstate); _PyThreadState_DeleteCurrent(tstate); } /* * Delete all thread states except the one passed as argument. * Note that, if there is a current thread state, it *must* be the one * passed as argument. Also, this won't touch any other interpreters * than the current one, since we don't know which thread state should * be kept in those other interpreters. */ void _PyThreadState_DeleteExcept(_PyRuntimeState *runtime, PyThreadState *tstate) { PyInterpreterState *interp = tstate->interp; |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |