diff options
Diffstat (limited to 'Python/ceval.c')
-rw-r--r-- | Python/ceval.c | 81 |
1 files changed, 50 insertions, 31 deletions
diff --git a/Python/ceval.c b/Python/ceval.c index 7747b08..2e94f32 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -275,54 +275,52 @@ PyEval_ThreadsInitialized(void) } PyStatus -_PyEval_InitThreads(PyThreadState *tstate) +_PyEval_InitGIL(PyThreadState *tstate) { - assert(is_tstate_valid(tstate)); + if (!_Py_IsMainInterpreter(tstate)) { + /* Currently, the GIL is shared by all interpreters, + and only the main interpreter is responsible to create + and destroy it. */ + return _PyStatus_OK(); + } - if (_Py_IsMainInterpreter(tstate)) { - struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil; - if (gil_created(gil)) { - return _PyStatus_OK(); - } + struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil; + assert(!gil_created(gil)); - PyThread_init_thread(); - create_gil(gil); + PyThread_init_thread(); + create_gil(gil); - take_gil(tstate); - } - - struct _pending_calls *pending = &tstate->interp->ceval.pending; - assert(pending->lock == NULL); - pending->lock = PyThread_allocate_lock(); - if (pending->lock == NULL) { - return _PyStatus_NO_MEMORY(); - } + take_gil(tstate); + assert(gil_created(gil)); return _PyStatus_OK(); } void -PyEval_InitThreads(void) +_PyEval_FiniGIL(PyThreadState *tstate) { - /* Do nothing: kept for backward compatibility */ -} + if (!_Py_IsMainInterpreter(tstate)) { + /* Currently, the GIL is shared by all interpreters, + and only the main interpreter is responsible to create + and destroy it. */ + return; + } -void -_PyEval_FiniThreads(PyThreadState *tstate) -{ struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil; if (!gil_created(gil)) { + /* First Py_InitializeFromConfig() call: the GIL doesn't exist + yet: do nothing. */ return; } destroy_gil(gil); assert(!gil_created(gil)); +} - struct _pending_calls *pending = &tstate->interp->ceval.pending; - if (pending->lock != NULL) { - PyThread_free_lock(pending->lock); - pending->lock = NULL; - } +void +PyEval_InitThreads(void) +{ + /* Do nothing: kept for backward compatibility */ } void @@ -544,6 +542,10 @@ _PyEval_AddPendingCall(PyThreadState *tstate, { struct _pending_calls *pending = &tstate->interp->ceval.pending; + /* Ensure that _PyEval_InitPendingCalls() was called + and that _PyEval_FiniPendingCalls() is not called yet. */ + assert(pending->lock != NULL); + PyThread_acquire_lock(pending->lock, WAIT_LOCK); if (pending->finishing) { PyThread_release_lock(pending->lock); @@ -721,10 +723,27 @@ _PyEval_InitRuntimeState(struct _ceval_runtime_state *ceval) _gil_initialize(&ceval->gil); } -void +int _PyEval_InitState(struct _ceval_state *ceval) { - /* PyInterpreterState_New() initializes ceval to zero */ + struct _pending_calls *pending = &ceval->pending; + assert(pending->lock == NULL); + + pending->lock = PyThread_allocate_lock(); + if (pending->lock == NULL) { + return -1; + } + return 0; +} + +void +_PyEval_FiniState(struct _ceval_state *ceval) +{ + struct _pending_calls *pending = &ceval->pending; + if (pending->lock != NULL) { + PyThread_free_lock(pending->lock); + pending->lock = NULL; + } } int |