summaryrefslogtreecommitdiffstats
path: root/Python/ceval_gil.h
Commit message (Collapse)AuthorAgeFilesLines
* gh-96387: take_gil() resets drop request before exit (GH-96869) (GH-96941)Miss Islington (bot)2022-09-201-0/+11
| | | | | | | | | | | | | | | | | | At Python exit, sometimes a thread holding the GIL can wait forever for a thread (usually a daemon thread) which requested to drop the GIL, whereas the thread already exited. To fix the race condition, the thread which requested the GIL drop now resets its request before exiting. take_gil() now calls RESET_GIL_DROP_REQUEST() before PyThread_exit_thread() if it called SET_GIL_DROP_REQUEST to fix a race condition with drop_gil(). Issue discovered and analyzed by Mingliang ZHAO. (cherry picked from commit 04f4977f508583954ad7b9cb09076ee1e57461f8) (cherry picked from commit 6ff54716f1073a4bcfed8a1ec0b518c489c1af0d) Co-authored-by: Victor Stinner <vstinner@python.org>
* bpo-43268: Pass interp rather than tstate to internal functions (GH-24580)Victor Stinner2021-02-191-1/+1
| | | | | | | | | | | | | | | Pass the current interpreter (interp) rather than the current Python thread state (tstate) to internal functions which only use the interpreter. Modified functions: * _PyXXX_Fini() and _PyXXX_ClearFreeList() functions * _PyEval_SignalAsyncExc(), make_pending_calls() * _PySys_GetObject(), sys_set_object(), sys_set_object_id(), sys_set_object_str() * should_audit(), set_flags_from_config(), make_flags() * _PyAtExit_Call() * init_stdio_encoding() * etc.
* bpo-40513: Per-interpreter GIL (GH-19943)Victor Stinner2020-05-051-2/+22
| | | | | | | | In the experimental isolated subinterpreters build mode, the GIL is now per-interpreter. Move gil from _PyRuntimeState.ceval to PyInterpreterState.ceval. new_interpreter() always get the config from the main interpreter.
* Revert "bpo-40513: Per-interpreter signals pending (GH-19924)" (GH-19932)Victor Stinner2020-05-051-1/+1
| | | This reverts commit 4e01946cafca0cf49f796c3118e0d65237bcad69.
* bpo-40513: Per-interpreter gil_drop_request (GH-19927)Victor Stinner2020-05-051-6/+7
| | | | Move gil_drop_request member from _PyRuntimeState.ceval to PyInterpreterState.ceval.
* bpo-40082: trip_signal() uses the main interpreter (GH-19441)Victor Stinner2020-04-081-6/+10
| | | | | | | | | | | | | | | | | | | | | | Fix the signal handler: it now always uses the main interpreter, rather than trying to get the current Python thread state. The following function now accepts an interpreter, instead of a Python thread state: * _PyEval_SignalReceived() * _Py_ThreadCanHandleSignals() * _PyEval_AddPendingCall() * COMPUTE_EVAL_BREAKER() * SET_GIL_DROP_REQUEST(), RESET_GIL_DROP_REQUEST() * SIGNAL_PENDING_CALLS(), UNSIGNAL_PENDING_CALLS() * SIGNAL_PENDING_SIGNALS(), UNSIGNAL_PENDING_SIGNALS() * SIGNAL_ASYNC_EXC(), UNSIGNAL_ASYNC_EXC() Py_AddPendingCall() now uses the main interpreter if it fails to the current Python thread state. Convert _PyThreadState_GET() and PyInterpreterState_GET_UNSAFE() macros to static inline functions.
* bpo-40010: Pass tstate to ceval GIL functions (GH-19077)Victor Stinner2020-03-201-14/+9
| | | | * Add eval_frame_handle_pending() helper function: cold code path. * Fix PyEval_ReleaseLock(): don't dereference tstate if it's NULL.
* bpo-39877: 4th take_gil() fix for daemon threads (GH-19080)Victor Stinner2020-03-191-22/+23
| | | | bpo-39877, bpo-40010: Add a third tstate_must_exit() check in take_gil() to prevent using tstate which has been freed.
* bpo-40010: Optimize signal handling in multithreaded applications (GH-19067)Victor Stinner2020-03-191-0/+9
| | | | | | | | | | | | | | | If a thread different than the main thread gets a signal, the bytecode evaluation loop is no longer interrupted at each bytecode instruction to check for pending signals which cannot be handled. Only the main thread of the main interpreter can handle signals. Previously, the bytecode evaluation loop was interrupted at each instruction until the main thread handles signals. Changes: * COMPUTE_EVAL_BREAKER() and SIGNAL_PENDING_SIGNALS() no longer set eval_breaker to 1 if the current thread cannot handle signals. * take_gil() now always recomputes eval_breaker.
* bpo-39984: Move pending calls to PyInterpreterState (GH-19066)Victor Stinner2020-03-191-4/+6
| | | | | | | | | | | | | | | | | If Py_AddPendingCall() is called in a subinterpreter, the function is now scheduled to be called from the subinterpreter, rather than being called from the main interpreter. Each subinterpreter now has its own list of scheduled calls. * Move pending and eval_breaker fields from _PyRuntimeState.ceval to PyInterpreterState.ceval. * new_interpreter() now calls _PyEval_InitThreads() to create pending calls lock. * Fix Py_AddPendingCall() for subinterpreters. It now calls _PyThreadState_GET() which works in a subinterpreter if the caller holds the GIL, and only falls back on PyGILState_GetThisThreadState() if _PyThreadState_GET() returns NULL.
* bpo-39984: Pass tstate to _PyEval_SignalAsyncExc() (GH-19049)Victor Stinner2020-03-181-1/+1
| | | | _PyEval_SignalAsyncExc() and _PyEval_FiniThreads() now expect tstate, instead of ceval.
* bpo-39877: Fix take_gil() for daemon threads (GH-19054)Victor Stinner2020-03-181-2/+6
| | | | bpo-39877, bpo-39984: If the thread must exit, don't access tstate to prevent a potential crash: tstate memory has been freed.
* bpo-39877: take_gil() checks tstate_must_exit() twice (GH-18890)Victor Stinner2020-03-091-1/+18
| | | | take_gil() now also checks tstate_must_exit() after acquiring the GIL: exit the thread if Py_Finalize() has been called.
* bpo-39877: Refactor take_gil() function (GH-18885)Victor Stinner2020-03-091-1/+40
| | | | | | | | | * Remove ceval parameter of take_gil(): get it from tstate. * Move exit_thread_if_finalizing() call inside take_gil(). Replace exit_thread_if_finalizing() with tstate_must_exit(): the caller is now responsible to call PyThread_exit_thread(). * Move is_tstate_valid() assertion inside take_gil(). Remove is_tstate_valid(): inline code into take_gil(). * Move gil_created() assertion inside take_gil().
* bpo-39877: Fix PyEval_RestoreThread() for daemon threads (GH-18811)Victor Stinner2020-03-081-4/+7
| | | | | | | | | | | | | | | | | | | | * exit_thread_if_finalizing() does now access directly _PyRuntime variable, rather than using tstate->interp->runtime since tstate can be a dangling pointer after Py_Finalize() has been called. * exit_thread_if_finalizing() is now called *before* calling take_gil(). _PyRuntime.finalizing is an atomic variable, we don't need to hold the GIL to access it. * Add ensure_tstate_not_null() function to check that tstate is not NULL at runtime. Check tstate earlier. take_gil() does not longer check if tstate is NULL. Cleanup: * PyEval_RestoreThread() no longer saves/restores errno: it's already done inside take_gil(). * PyEval_AcquireLock(), PyEval_AcquireThread(), PyEval_RestoreThread() and _PyEval_EvalFrameDefault() now check if tstate is valid with the new is_tstate_valid() function which uses _PyMem_IsPtrFreed().
* Revert "bpo-33608: Factor out a private, per-interpreter ↵Victor Stinner2019-06-031-18/+10
| | | | | _Py_AddPendingCall(). (gh-13714)" (GH-13780) This reverts commit 6a150bcaeb190d1731b38ab9c7a5d1a352847ddc.
* bpo-33608: Factor out a private, per-interpreter _Py_AddPendingCall(). ↵Eric Snow2019-06-011-10/+18
| | | | (gh-13714)
* bpo-36710: Add 'ceval' local variable to ceval.c (GH-12934)Victor Stinner2019-05-101-82/+76
| | | | | | | | | | | | | | | | | | | | | | | | | Add "struct _ceval_runtime_state *ceval = &_PyRuntime.ceval;" local variables to function to better highlight the dependency on the global variable _PyRuntime and to point directly to _PyRuntime.ceval field rather than on the larger _PyRuntime. Changes: * Add _PyRuntimeState_GetThreadState(runtime) macro. * Add _PyEval_AddPendingCall(ceval, ...) and _PyThreadState_Swap(gilstate, ...) functions. * _PyThreadState_GET() macro now calls _PyRuntimeState_GetThreadState() using &_PyRuntime. * Add 'ceval' parameter to COMPUTE_EVAL_BREAKER(), SIGNAL_PENDING_SIGNALS(), _PyEval_SignalAsyncExc(), _PyEval_SignalReceived() and _PyEval_FiniThreads() macros and functions. * Add 'tstate' parameter to call_function(), do_call_core() and do_raise(). * Add 'runtime' parameter to _Py_CURRENTLY_FINALIZING(), _Py_FinishPendingCalls() and _PyThreadState_DeleteExcept() macros and functions. * Declare 'runtime', 'tstate', 'ceval' and 'eval_breaker' variables as constant.
* bpo-33608: Revert "Factor out a private, per-interpreter ↵Eric Snow2019-04-121-4/+4
| | | | | _Py_AddPendingCall()." (gh-12806) This reverts commit f13c5c8b9401a9dc19e95d8b420ee100ac022208 (gh-12360).
* bpo-33608: Factor out a private, per-interpreter _Py_AddPendingCall(). ↵Eric Snow2019-04-121-4/+4
| | | | | (gh-12360) This is effectively an un-revert of #11617 and #12024 (reverted in #12159). Portions of those were merged in other PRs (with lower risk) and this represents the remainder. Note that I found 3 different bugs in the original PRs and have fixed them here.
* Revert: bpo-33608: Factor out a private, per-interpreter ↵Victor Stinner2019-03-041-4/+4
| | | | | | | | | | | | | | | | | | | _Py_AddPendingCall(). (GH-11617) (GH-12159) * Revert "bpo-36097: Use only public C-API in the_xxsubinterpreters module (adding as necessary). (#12003)" This reverts commit bcfa450f210074e16feb761ae5b3e966a2532fcf. * Revert "bpo-33608: Simplify ceval's DISPATCH by hoisting eval_breaker ahead of time. (gh-12062)" This reverts commit bda918bf65a88560ec453aaba0758a9c0d49b449. * Revert "bpo-33608: Use _Py_AddPendingCall() in _PyCrossInterpreterData_Release(). (gh-12024)" This reverts commit b05b711a2cef6c6c381e01069dedac372e0b9fb2. * Revert "bpo-33608: Factor out a private, per-interpreter _Py_AddPendingCall(). (GH-11617)" This reverts commit ef4ac967e2f3a9a18330cc6abe14adb4bc3d0465.
* bpo-33608: Factor out a private, per-interpreter _Py_AddPendingCall(). ↵Eric Snow2019-02-241-4/+4
| | | | | | | (GH-11617) This involves moving the global "pending calls" state to PyInterpreterState. https://bugs.python.org/issue33608
* bpo-35081: Add pycore_ prefix to internal header files (GH-10263)Victor Stinner2018-10-311-1/+1
| | | | | | | | | | | | | | | | | | | | * Rename Include/internal/ header files: * pyatomic.h -> pycore_atomic.h * ceval.h -> pycore_ceval.h * condvar.h -> pycore_condvar.h * context.h -> pycore_context.h * pygetopt.h -> pycore_getopt.h * gil.h -> pycore_gil.h * hamt.h -> pycore_hamt.h * hash.h -> pycore_hash.h * mem.h -> pycore_mem.h * pystate.h -> pycore_state.h * warnings.h -> pycore_warnings.h * PCbuild project, Makefile.pre.in, Modules/Setup: add the Include/internal/ directory to the search paths of header files. * Update includes. For example, replace #include "internal/mem.h" with #include "pycore_mem.h".
* bpo-35081: Move Include/pyatomic.c to Include/internal/ (GH-10239)Victor Stinner2018-10-301-0/+1
| | | Add pyatomic.h to the VS project (it wasn't referenced).
* bpo-30860: Consolidate stateful runtime globals. (#3397)Eric Snow2017-09-081-85/+74
| | | | | | | * group the (stateful) runtime globals into various topical structs * consolidate the topical structs under a single top-level _PyRuntimeState struct * add a check-c-globals.py script that helps identify runtime globals Other globals are excluded (see globals.txt and check-c-globals.py).
* Revert "bpo-30860: Consolidate stateful runtime globals." (#3379)Eric Snow2017-09-061-74/+85
| | | Windows buildbots started failing due to include-related errors.
* bpo-30860: Consolidate stateful runtime globals. (#2594)Eric Snow2017-09-061-85/+74
| | | | | | | | | * group the (stateful) runtime globals into various topical structs * consolidate the topical structs under a single top-level _PyRuntimeState struct * add a check-c-globals.py script that helps identify runtime globals Other globals are excluded (see globals.txt and check-c-globals.py).
* replace Py_(u)intptr_t with the c99 standard typesBenjamin Peterson2016-09-061-2/+2
|
* Use Py_uintptr_t for atomic pointersVictor Stinner2016-01-221-4/+4
| | | | | | | | Issue #26161: Use Py_uintptr_t instead of void* for atomic pointers in pyatomic.h. Use atomic_uintptr_t when <stdatomic.h> is used. Using void* causes compilation warnings depending on which implementation of atomic types is used.
* Removed unintentional trailing spaces in non-external and non-generated C files.Serhiy Storchaka2015-03-181-6/+6
|
* Fixed few compiler warnings.Serhiy Storchaka2015-02-161-2/+2
|
* Issue #15038: Optimize python Locks on WindowsKristján Valur Jónsson2012-06-181-191/+27
| | | | | Extract cross-platform condition variable support into a separate file and provide user-mode non-recursive locks for Windows.
* Signal condition variables with the mutex held. Destroy condition variablesKristján Valur Jónsson2012-06-051-4/+5
| | | | before their mutexes.
* Fix the GIL with subinterpreters. Hopefully this will allow mod_wsgi to work ↵Antoine Pitrou2011-01-151-4/+7
| | | | | | with 3.2. (we need some tests for this)
* Issue #9828: Destroy the GIL in Py_Finalize(), so that it gets properlyAntoine Pitrou2010-09-131-0/+21
| | | | | re-created on a subsequent call to Py_Initialize(). The problem (a crash) wouldn't appear in 3.1 or 2.7 where the GIL's structure is more trivial.
* Issue #8411: new condition variable emulation under Windows for the new GIL,Antoine Pitrou2010-08-101-52/+105
| | | | | by Kristján. Unfortunately the 3.x Windows buildbots are in a wreck, so we'll have to watch them when they become fit again.
* Make (most of) Python's tests pass under Thread Sanitizer.Jeffrey Yasskin2010-05-031-20/+28
| | | | | | | | | | | | | | | | | | http://code.google.com/p/data-race-test/wiki/ThreadSanitizer is a dynamic data race detector that runs on top of valgrind. With this patch, the binaries at http://code.google.com/p/data-race-test/wiki/ThreadSanitizer#Binaries pass many but not all of the Python tests. All of regrtest still passes outside of tsan. I've implemented part of the C1x atomic types so that we can explicitly mark variables that are used across threads, and get defined behavior as compilers advance. I've added tsan's client header and implementation to the codebase in dynamic_annotations.{h,c} (docs at http://code.google.com/p/data-race-test/wiki/DynamicAnnotations). Unfortunately, I haven't been able to get helgrind and drd to give sensible error messages, even when I use their client annotations, so I'm not supporting them.
* Try to strengthen condition-waiting under Windows.Antoine Pitrou2009-11-121-11/+9
| | | | | If it doesn't work (doesn't solve erratic freezes) we'll have to resort to tougher (Windows-only) measures.
* Our condition variable emulation under Windows is imperfect, whichAntoine Pitrou2009-11-111-9/+24
| | | | | seems to be the cause of the buildbot hangs. Try to fix it, and add some comments.
* Merge in the new GIL.Antoine Pitrou2009-11-101-0/+335