summaryrefslogtreecommitdiffstats
path: root/Python/thread_nt.h
diff options
context:
space:
mode:
authorAntoine Pitrou <antoine@python.org>2023-11-04 13:59:24 (GMT)
committerGitHub <noreply@github.com>2023-11-04 13:59:24 (GMT)
commit0e9c364f4ac18a2237bdbac702b96bcf8ef9cb09 (patch)
tree8febb8282c2c1ebd73a18205ec5b9229a99ac4fe /Python/thread_nt.h
parenta28a3967ab9a189122f895d51d2551f7b3a273b0 (diff)
downloadcpython-0e9c364f4ac18a2237bdbac702b96bcf8ef9cb09.zip
cpython-0e9c364f4ac18a2237bdbac702b96bcf8ef9cb09.tar.gz
cpython-0e9c364f4ac18a2237bdbac702b96bcf8ef9cb09.tar.bz2
GH-110829: Ensure Thread.join() joins the OS thread (#110848)
Joining a thread now ensures the underlying OS thread has exited. This is required for safer fork() in multi-threaded processes. --------- Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Diffstat (limited to 'Python/thread_nt.h')
-rw-r--r--Python/thread_nt.h58
1 files changed, 47 insertions, 11 deletions
diff --git a/Python/thread_nt.h b/Python/thread_nt.h
index 26f441b..14b9cdd 100644
--- a/Python/thread_nt.h
+++ b/Python/thread_nt.h
@@ -182,9 +182,9 @@ bootstrap(void *call)
return 0;
}
-unsigned long
-PyThread_start_new_thread(void (*func)(void *), void *arg)
-{
+int
+PyThread_start_joinable_thread(void (*func)(void *), void *arg,
+ PyThread_ident_t* ident, PyThread_handle_t* handle) {
HANDLE hThread;
unsigned threadID;
callobj *obj;
@@ -194,7 +194,7 @@ PyThread_start_new_thread(void (*func)(void *), void *arg)
obj = (callobj*)HeapAlloc(GetProcessHeap(), 0, sizeof(*obj));
if (!obj)
- return PYTHREAD_INVALID_THREAD_ID;
+ return -1;
obj->func = func;
obj->arg = arg;
PyThreadState *tstate = _PyThreadState_GET();
@@ -207,22 +207,51 @@ PyThread_start_new_thread(void (*func)(void *), void *arg)
/* I've seen errno == EAGAIN here, which means "there are
* too many threads".
*/
- int e = errno;
- threadID = (unsigned)-1;
HeapFree(GetProcessHeap(), 0, obj);
+ return -1;
}
- else {
- CloseHandle(hThread);
+ *ident = threadID;
+ // The cast is safe since HANDLE is pointer-sized
+ *handle = (PyThread_handle_t) hThread;
+ return 0;
+}
+
+unsigned long
+PyThread_start_new_thread(void (*func)(void *), void *arg) {
+ PyThread_handle_t handle;
+ PyThread_ident_t ident;
+ if (PyThread_start_joinable_thread(func, arg, &ident, &handle)) {
+ return PYTHREAD_INVALID_THREAD_ID;
}
- return threadID;
+ CloseHandle((HANDLE) handle);
+ // The cast is safe since the ident is really an unsigned int
+ return (unsigned long) ident;
+}
+
+int
+PyThread_join_thread(PyThread_handle_t handle) {
+ HANDLE hThread = (HANDLE) handle;
+ int errored = (WaitForSingleObject(hThread, INFINITE) != WAIT_OBJECT_0);
+ CloseHandle(hThread);
+ return errored;
+}
+
+int
+PyThread_detach_thread(PyThread_handle_t handle) {
+ HANDLE hThread = (HANDLE) handle;
+ return (CloseHandle(hThread) == 0);
+}
+
+void
+PyThread_update_thread_after_fork(PyThread_ident_t* ident, PyThread_handle_t* handle) {
}
/*
* Return the thread Id instead of a handle. The Id is said to uniquely identify the
* thread in the system
*/
-unsigned long
-PyThread_get_thread_ident(void)
+PyThread_ident_t
+PyThread_get_thread_ident_ex(void)
{
if (!initialized)
PyThread_init_thread();
@@ -230,6 +259,13 @@ PyThread_get_thread_ident(void)
return GetCurrentThreadId();
}
+unsigned long
+PyThread_get_thread_ident(void)
+{
+ return (unsigned long) PyThread_get_thread_ident_ex();
+}
+
+
#ifdef PY_HAVE_THREAD_NATIVE_ID
/*
* Return the native Thread ID (TID) of the calling thread.