From 35941efd3922baf8d12198e9fbc25e26288ef43c Mon Sep 17 00:00:00 2001 From: Joannah Nanjekye Date: Fri, 16 Aug 2019 19:55:18 +0000 Subject: [PATCH 1/9] Document PyThreadState_DeleteCurrent() --- Doc/c-api/init.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Doc/c-api/init.rst b/Doc/c-api/init.rst index 4985a1d867e2ad..9a38367a23b7fc 100644 --- a/Doc/c-api/init.rst +++ b/Doc/c-api/init.rst @@ -1023,6 +1023,12 @@ All of the following functions must be called after :c:func:`Py_Initialize`. The thread state must have been reset with a previous call to :c:func:`PyThreadState_Clear`. +.. c:function:: void PyThreadState_DeleteCurrent() + + Destroy the current thread state and releases the global interpreter. The global + interpreter lock need not be held. Like :c:func:`PyThreadState_Delete`, the global + interpreter lock need not be held. The thread state must have been reset with a previous + call to :c:func:`PyThreadState_Clear`. .. c:function:: PY_INT64_T PyInterpreterState_GetID(PyInterpreterState *interp) From d27549f5678c963fb90480609df8b283dd835bff Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Fri, 16 Aug 2019 20:01:15 +0000 Subject: [PATCH 2/9] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Documentation/2019-08-16-20-01-10.bpo-37878.MvA6rZ.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Documentation/2019-08-16-20-01-10.bpo-37878.MvA6rZ.rst diff --git a/Misc/NEWS.d/next/Documentation/2019-08-16-20-01-10.bpo-37878.MvA6rZ.rst b/Misc/NEWS.d/next/Documentation/2019-08-16-20-01-10.bpo-37878.MvA6rZ.rst new file mode 100644 index 00000000000000..1a03e3d7696068 --- /dev/null +++ b/Misc/NEWS.d/next/Documentation/2019-08-16-20-01-10.bpo-37878.MvA6rZ.rst @@ -0,0 +1 @@ +Added missing documentation for PyThreadState_DeleteCurrent(). \ No newline at end of file From 61ab0c37cf261ed7fc4f111f2615cbc4f896fe97 Mon Sep 17 00:00:00 2001 From: Joannah Nanjekye <33177550+nanjekyejoannah@users.noreply.github.com> Date: Sat, 17 Aug 2019 20:07:36 -0300 Subject: [PATCH 3/9] Update Doc/c-api/init.rst Co-Authored-By: Carol Willing --- Doc/c-api/init.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/c-api/init.rst b/Doc/c-api/init.rst index 9a38367a23b7fc..0c08e2b483dc52 100644 --- a/Doc/c-api/init.rst +++ b/Doc/c-api/init.rst @@ -1025,7 +1025,7 @@ All of the following functions must be called after :c:func:`Py_Initialize`. .. c:function:: void PyThreadState_DeleteCurrent() - Destroy the current thread state and releases the global interpreter. The global + Destroy the current thread state and release the global interpreter lock. interpreter lock need not be held. Like :c:func:`PyThreadState_Delete`, the global interpreter lock need not be held. The thread state must have been reset with a previous call to :c:func:`PyThreadState_Clear`. From 5034af81b30b0f2785a4e1a46a3f82a23ec6e7f7 Mon Sep 17 00:00:00 2001 From: Joannah Nanjekye <33177550+nanjekyejoannah@users.noreply.github.com> Date: Sat, 17 Aug 2019 20:07:48 -0300 Subject: [PATCH 4/9] Update Doc/c-api/init.rst Co-Authored-By: Carol Willing --- Doc/c-api/init.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/c-api/init.rst b/Doc/c-api/init.rst index 0c08e2b483dc52..c7eb795664fb9f 100644 --- a/Doc/c-api/init.rst +++ b/Doc/c-api/init.rst @@ -1027,7 +1027,7 @@ All of the following functions must be called after :c:func:`Py_Initialize`. Destroy the current thread state and release the global interpreter lock. interpreter lock need not be held. Like :c:func:`PyThreadState_Delete`, the global - interpreter lock need not be held. The thread state must have been reset with a previous + interpreter lock need not be held. When using :c:func:`PyThreadState_DeleteCurrent`, the thread state must have been reset with a previous call to :c:func:`PyThreadState_Clear`. .. c:function:: PY_INT64_T PyInterpreterState_GetID(PyInterpreterState *interp) From b4d0ccea683324022198f5342f4ac54b13391645 Mon Sep 17 00:00:00 2001 From: Joannah Nanjekye Date: Wed, 28 Aug 2019 16:22:33 +0000 Subject: [PATCH 5/9] make pyThreadState_DeleteCurrent internal --- Doc/c-api/init.rst | 6 ------ Include/internal/pycore_pylifecycle.h | 2 ++ Include/pystate.h | 1 - Modules/_threadmodule.c | 2 +- Modules/_tracemalloc.c | 4 ++-- Python/pystate.c | 12 +++--------- 6 files changed, 8 insertions(+), 19 deletions(-) diff --git a/Doc/c-api/init.rst b/Doc/c-api/init.rst index c7eb795664fb9f..4985a1d867e2ad 100644 --- a/Doc/c-api/init.rst +++ b/Doc/c-api/init.rst @@ -1023,12 +1023,6 @@ All of the following functions must be called after :c:func:`Py_Initialize`. The thread state must have been reset with a previous call to :c:func:`PyThreadState_Clear`. -.. c:function:: void PyThreadState_DeleteCurrent() - - Destroy the current thread state and release the global interpreter lock. - interpreter lock need not be held. Like :c:func:`PyThreadState_Delete`, the global - interpreter lock need not be held. When using :c:func:`PyThreadState_DeleteCurrent`, the thread state must have been reset with a previous - call to :c:func:`PyThreadState_Clear`. .. c:function:: PY_INT64_T PyInterpreterState_GetID(PyInterpreterState *interp) diff --git a/Include/internal/pycore_pylifecycle.h b/Include/internal/pycore_pylifecycle.h index da4af473296866..170d403419e08e 100644 --- a/Include/internal/pycore_pylifecycle.h +++ b/Include/internal/pycore_pylifecycle.h @@ -109,6 +109,8 @@ PyAPI_FUNC(void) _PyErr_Print(PyThreadState *tstate); PyAPI_FUNC(void) _PyErr_Display(PyObject *file, PyObject *exception, PyObject *value, PyObject *tb); +PyAPI_FUNC(void) _PyThreadState_DeleteCurrent(_PyRuntimeState *runtime); + #ifdef __cplusplus } #endif diff --git a/Include/pystate.h b/Include/pystate.h index 4c25e3f7ef84b5..1cb2305696f292 100644 --- a/Include/pystate.h +++ b/Include/pystate.h @@ -50,7 +50,6 @@ PyAPI_FUNC(PyObject*) PyState_FindModule(struct PyModuleDef*); PyAPI_FUNC(PyThreadState *) PyThreadState_New(PyInterpreterState *); PyAPI_FUNC(void) PyThreadState_Clear(PyThreadState *); PyAPI_FUNC(void) PyThreadState_Delete(PyThreadState *); -PyAPI_FUNC(void) PyThreadState_DeleteCurrent(void); /* Get the current thread state. diff --git a/Modules/_threadmodule.c b/Modules/_threadmodule.c index 6665827fe2babc..d7d272d5169939 100644 --- a/Modules/_threadmodule.c +++ b/Modules/_threadmodule.c @@ -1025,7 +1025,7 @@ t_bootstrap(void *boot_raw) PyMem_DEL(boot_raw); tstate->interp->num_threads--; PyThreadState_Clear(tstate); - PyThreadState_DeleteCurrent(); + _PyThreadState_DeleteCurrent(&_PyRuntime); PyThread_exit_thread(); } diff --git a/Modules/_tracemalloc.c b/Modules/_tracemalloc.c index ee32ac29b7eea2..26d76004aaa52c 100644 --- a/Modules/_tracemalloc.c +++ b/Modules/_tracemalloc.c @@ -34,7 +34,7 @@ static struct { #if defined(TRACE_RAW_MALLOC) /* This lock is needed because tracemalloc_free() is called without the GIL held from PyMem_RawFree(). It cannot acquire the lock because it - would introduce a deadlock in PyThreadState_DeleteCurrent(). */ + would introduce a deadlock in _PyThreadState_DeleteCurrent(). */ static PyThread_type_lock tables_lock; # define TABLES_LOCK() PyThread_acquire_lock(tables_lock, 1) # define TABLES_UNLOCK() PyThread_release_lock(tables_lock) @@ -728,7 +728,7 @@ tracemalloc_free(void *ctx, void *ptr) return; /* GIL cannot be locked in PyMem_RawFree() because it would introduce - a deadlock in PyThreadState_DeleteCurrent(). */ + a deadlock in _PyThreadState_DeleteCurrent(). */ alloc->free(alloc->ctx, ptr); diff --git a/Python/pystate.c b/Python/pystate.c index 3b2adf54be4598..1d16df0087532d 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -809,7 +809,7 @@ PyThreadState_Clear(PyThreadState *tstate) } -/* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */ +/* Common code for PyThreadState_Delete() and _PyThreadState_DeleteCurrent() */ static void tstate_delete_common(_PyRuntimeState *runtime, PyThreadState *tstate) { @@ -858,14 +858,14 @@ PyThreadState_Delete(PyThreadState *tstate) } -static void +void _PyThreadState_DeleteCurrent(_PyRuntimeState *runtime) { struct _gilstate_runtime_state *gilstate = &runtime->gilstate; PyThreadState *tstate = _PyRuntimeGILState_GetThreadState(gilstate); if (tstate == NULL) Py_FatalError( - "PyThreadState_DeleteCurrent: no current tstate"); + "_PyThreadState_DeleteCurrent: no current tstate"); tstate_delete_common(runtime, tstate); if (gilstate->autoInterpreterState && PyThread_tss_get(&gilstate->autoTSSkey) == tstate) @@ -876,12 +876,6 @@ _PyThreadState_DeleteCurrent(_PyRuntimeState *runtime) PyEval_ReleaseLock(); } -void -PyThreadState_DeleteCurrent() -{ - _PyThreadState_DeleteCurrent(&_PyRuntime); -} - /* * Delete all thread states except the one passed as argument. From 04677dd6ccaf1ffa744d0e061dab6c70b09a0690 Mon Sep 17 00:00:00 2001 From: Joannah Nanjekye <33177550+nanjekyejoannah@users.noreply.github.com> Date: Wed, 28 Aug 2019 13:30:34 -0300 Subject: [PATCH 6/9] Update 2019-08-16-20-01-10.bpo-37878.MvA6rZ.rst --- .../next/Documentation/2019-08-16-20-01-10.bpo-37878.MvA6rZ.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Documentation/2019-08-16-20-01-10.bpo-37878.MvA6rZ.rst b/Misc/NEWS.d/next/Documentation/2019-08-16-20-01-10.bpo-37878.MvA6rZ.rst index 1a03e3d7696068..8da4eb9717d89e 100644 --- a/Misc/NEWS.d/next/Documentation/2019-08-16-20-01-10.bpo-37878.MvA6rZ.rst +++ b/Misc/NEWS.d/next/Documentation/2019-08-16-20-01-10.bpo-37878.MvA6rZ.rst @@ -1 +1 @@ -Added missing documentation for PyThreadState_DeleteCurrent(). \ No newline at end of file +Make :c:func:`PyThreadState_DeleteCurrent` Internal. From a38b2b805e09cf4aac324c2de97d69c2d5fc42a5 Mon Sep 17 00:00:00 2001 From: Joannah Nanjekye Date: Thu, 5 Sep 2019 13:26:22 +0000 Subject: [PATCH 7/9] remove state from t_bootstrap() --- Doc/whatsnew/3.9.rst | 3 +++ Modules/_threadmodule.c | 9 +++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Doc/whatsnew/3.9.rst b/Doc/whatsnew/3.9.rst index f09e09c2b90560..3c888525ba6c79 100644 --- a/Doc/whatsnew/3.9.rst +++ b/Doc/whatsnew/3.9.rst @@ -191,6 +191,9 @@ Removed Use :meth:`~threading.Thread.is_alive()` instead. (Contributed by Dong-hee Na in :issue:`37804`.) +* ``PyThreadState_DeleteCurrent()`` has been removed. It was not documented. + (Contributed by Joannah Nanjekye in :issue:`37878`.) + Porting to Python 3.9 ===================== diff --git a/Modules/_threadmodule.c b/Modules/_threadmodule.c index d7d272d5169939..5ab51be961506b 100644 --- a/Modules/_threadmodule.c +++ b/Modules/_threadmodule.c @@ -993,6 +993,7 @@ struct bootstate { PyObject *args; PyObject *keyw; PyThreadState *tstate; + _PyRuntimeState *runtime; }; static void @@ -1000,11 +1001,13 @@ t_bootstrap(void *boot_raw) { struct bootstate *boot = (struct bootstate *) boot_raw; PyThreadState *tstate; + _PyRuntimeState *runtime; PyObject *res; + runtime = boot->runtime; tstate = boot->tstate; tstate->thread_id = PyThread_get_thread_ident(); - _PyThreadState_Init(&_PyRuntime, tstate); + _PyThreadState_Init(runtime, tstate); PyEval_AcquireThread(tstate); tstate->interp->num_threads++; res = PyObject_Call(boot->func, boot->args, boot->keyw); @@ -1025,13 +1028,14 @@ t_bootstrap(void *boot_raw) PyMem_DEL(boot_raw); tstate->interp->num_threads--; PyThreadState_Clear(tstate); - _PyThreadState_DeleteCurrent(&_PyRuntime); + _PyThreadState_DeleteCurrent(runtime); PyThread_exit_thread(); } static PyObject * thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs) { + _PyRuntimeState *runtime = &_PyRuntime; PyObject *func, *args, *keyw = NULL; struct bootstate *boot; unsigned long ident; @@ -1062,6 +1066,7 @@ thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs) boot->args = args; boot->keyw = keyw; boot->tstate = _PyThreadState_Prealloc(boot->interp); + boot->runtime = runtime; if (boot->tstate == NULL) { PyMem_DEL(boot); return PyErr_NoMemory(); From 19c7468dfe7525aa8d5d6ee26bd51862015c488c Mon Sep 17 00:00:00 2001 From: Joannah Nanjekye Date: Thu, 5 Sep 2019 14:36:43 +0000 Subject: [PATCH 8/9] Remove update --- Doc/whatsnew/3.9.rst | 3 --- 1 file changed, 3 deletions(-) diff --git a/Doc/whatsnew/3.9.rst b/Doc/whatsnew/3.9.rst index 3c888525ba6c79..f09e09c2b90560 100644 --- a/Doc/whatsnew/3.9.rst +++ b/Doc/whatsnew/3.9.rst @@ -191,9 +191,6 @@ Removed Use :meth:`~threading.Thread.is_alive()` instead. (Contributed by Dong-hee Na in :issue:`37804`.) -* ``PyThreadState_DeleteCurrent()`` has been removed. It was not documented. - (Contributed by Joannah Nanjekye in :issue:`37878`.) - Porting to Python 3.9 ===================== From 7acf1cb33304851734d7ec4079eb10677b2fb43b Mon Sep 17 00:00:00 2001 From: Joannah Nanjekye Date: Thu, 5 Sep 2019 14:41:21 +0000 Subject: [PATCH 9/9] resolve merge conflict --- Doc/whatsnew/3.9.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Doc/whatsnew/3.9.rst b/Doc/whatsnew/3.9.rst index 009a66adff7d58..756690d93793e6 100644 --- a/Doc/whatsnew/3.9.rst +++ b/Doc/whatsnew/3.9.rst @@ -219,6 +219,9 @@ Removed standard :class:`bytes` objects are always used. (Contributed by Jon Janzen in :issue:`36409`.) +* ``PyThreadState_DeleteCurrent()`` has been removed. It was not documented. + (Contributed by Joannah Nanjekye in :issue:`37878`.) + Porting to Python 3.9 =====================