]> Cypherpunks repositories - gostls13.git/commitdiff
runtime/cgo: use CreateThread instead of _beginthread
authorqmuntal <quimmuntal@gmail.com>
Mon, 24 Feb 2025 14:37:41 +0000 (15:37 +0100)
committerQuim Muntal <quimmuntal@gmail.com>
Tue, 25 Feb 2025 20:01:21 +0000 (12:01 -0800)
_beginthread is intended to be used together with the C runtime.
The cgo runtime doesn't use it, so better use CreateThread directly,
which is the Windows API for creating threads.

Cq-Include-Trybots: luci.golang.try:gotip-windows-amd64-longtest,gotip-windows-arm64
Change-Id: Ic6cf75f69f62a3babf5e74155da1aac70961886c
Reviewed-on: https://go-review.googlesource.com/c/go/+/651995
Reviewed-by: Michael Pratt <mpratt@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>

src/runtime/cgo/gcc_libinit_windows.c
src/runtime/cgo/gcc_windows_386.c
src/runtime/cgo/gcc_windows_amd64.c
src/runtime/cgo/gcc_windows_arm64.c
src/runtime/cgo/libcgo_windows.h

index ddc0ad7010e5080fa31d012b775e3a3be03c6ae1..5bd64acf23656b54c1bcb89419ca3ef095d2f721 100644 (file)
@@ -2,19 +2,15 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-# ifdef __CYGWIN__
+#ifdef __CYGWIN__
 #error "don't use the cygwin compiler to build native Windows programs; use MinGW instead"
-#else
-// Exclude the following code from Cygwin builds.
-// Cygwin doesn't implement process.h nor does it support _beginthread.
+#endif
 
 #define WIN32_LEAN_AND_MEAN
 #include <windows.h>
-#include <process.h>
 
 #include <stdio.h>
 #include <stdlib.h>
-#include <errno.h>
 
 #include "libcgo.h"
 #include "libcgo_windows.h"
@@ -69,7 +65,7 @@ _cgo_maybe_run_preinit() {
 }
 
 void
-x_cgo_sys_thread_create(void (*func)(void*), void* arg) {
+x_cgo_sys_thread_create(unsigned long (__stdcall *func)(void*), void* arg) {
        _cgo_beginthread(func, arg);
 }
 
@@ -141,26 +137,25 @@ void (*(_cgo_get_context_function(void)))(struct context_arg*) {
        return ret;
 }
 
-void _cgo_beginthread(void (*func)(void*), void* arg) {
+void _cgo_beginthread(unsigned long (__stdcall *func)(void*), void* arg) {
        int tries;
-       uintptr_t thandle;
+       HANDLE thandle;
 
        for (tries = 0; tries < 20; tries++) {
-               thandle = _beginthread(func, 0, arg);
-               if (thandle == -1 && errno == EACCES) {
+               thandle = CreateThread(NULL, 0, func, arg, 0, NULL);
+               if (thandle == 0 && GetLastError() == ERROR_NOT_ENOUGH_MEMORY) {
                        // "Insufficient resources", try again in a bit.
                        //
                        // Note that the first Sleep(0) is a yield.
                        Sleep(tries); // milliseconds
                        continue;
-               } else if (thandle == -1) {
+               } else if (thandle == 0) {
                        break;
                }
+               CloseHandle(thandle);
                return; // Success!
        }
 
-       fprintf(stderr, "runtime: failed to create new OS thread (%d)\n", errno);
+       fprintf(stderr, "runtime: failed to create new OS thread (%lu)\n", GetLastError());
        abort();
 }
-
-#endif // __CYGWIN__
\ No newline at end of file
index 983e14b7c8ff3b782e1943d58d3ee221945df211..d394dc6acb9318a04ffed9ce509b720b0f1504c0 100644 (file)
@@ -11,7 +11,7 @@
 #include "libcgo.h"
 #include "libcgo_windows.h"
 
-static void threadentry(void*);
+static unsigned long __stdcall threadentry(void*);
 static void (*setg_gcc)(void*);
 static DWORD *tls_g;
 
@@ -29,7 +29,8 @@ _cgo_sys_thread_start(ThreadStart *ts)
 }
 
 extern void crosscall1(void (*fn)(void), void (*setg_gcc)(void*), void *g);
-static void
+static unsigned long
+__stdcall
 threadentry(void *v)
 {
        ThreadStart ts;
@@ -50,4 +51,5 @@ threadentry(void *v)
        );
 
        crosscall1(ts.fn, setg_gcc, ts.g);
+       return 0;
 }
index e26887a1727caed6c72467a3c7e24c03105d2775..455b6b7cb29f2825ef17002e5e7127a5905c8f59 100644 (file)
@@ -11,7 +11,7 @@
 #include "libcgo.h"
 #include "libcgo_windows.h"
 
-static void threadentry(void*);
+static unsigned long __stdcall threadentry(void*);
 static void (*setg_gcc)(void*);
 static DWORD *tls_g;
 
@@ -30,7 +30,8 @@ _cgo_sys_thread_start(ThreadStart *ts)
 }
 
 extern void crosscall1(void (*fn)(void), void (*setg_gcc)(void*), void *g);
-static void
+static unsigned long
+__stdcall
 threadentry(void *v)
 {
        ThreadStart ts;
@@ -49,4 +50,5 @@ threadentry(void *v)
        );
 
        crosscall1(ts.fn, setg_gcc, (void*)ts.g);
+       return 0;
 }
index 8f113cc3b16727290d12f86eda522860bb7fd547..7f02d4bd5c048b105895e7ba7a9aff036083d7e9 100644 (file)
@@ -11,7 +11,7 @@
 #include "libcgo.h"
 #include "libcgo_windows.h"
 
-static void threadentry(void*);
+static unsigned long __stdcall threadentry(void*);
 static void (*setg_gcc)(void*);
 
 void
@@ -28,7 +28,8 @@ _cgo_sys_thread_start(ThreadStart *ts)
 
 extern void crosscall1(void (*fn)(void), void (*setg_gcc)(void*), void *g);
 
-static void
+static unsigned long
+__stdcall
 threadentry(void *v)
 {
        ThreadStart ts;
@@ -37,4 +38,5 @@ threadentry(void *v)
        free(v);
 
        crosscall1(ts.fn, setg_gcc, (void *)ts.g);
+       return 0;
 }
index 33d7637fece0ecb5e0c3667c560a7f7850d96a29..682b7bdbbf2c1c38c3c1ff699ce57f857789ad26 100644 (file)
@@ -3,4 +3,4 @@
 // license that can be found in the LICENSE file.
 
 // Call _beginthread, aborting on failure.
-void _cgo_beginthread(void (*func)(void*), void* arg);
+void _cgo_beginthread(unsigned long (__stdcall *func)(void*), void* arg);