Hello, I read existing mentioned C11 thrd implementations on github, used them as inspiration and I tried to prepare thrd_* functions for mingw-w64 to be compatible with MSVC ABI. So not the whole C11 threads.h, just only thrd_* functions.
In the attachment mingw_c11_threads_2.patches are changes which implements them and one of the sigfpe test which verifies that basic functionality with C signal handlers is working. Changes are applied on top of the changes from previous email (mingw_c11_threads.patches) where is the threads.h and vcruntime140_threads implementation. What do you think about it? Kirill run CI tests (there are test-ffmpeg jobs failures but seems not related): https://github.com/maiddaisuki/mingw-w64/actions/runs/37606114334
>From db8aa5dcc547e22fafc8705a1e267b9884379171 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]> Date: Sun, 4 Oct 2026 21:42:20 +0200 Subject: [PATCH 1/5] fixup headers: Add C11 threads.h header file --- mingw-w64-headers/crt/threads.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mingw-w64-headers/crt/threads.h b/mingw-w64-headers/crt/threads.h index cf3e8db21a67..c483ee1b8b6e 100644 --- a/mingw-w64-headers/crt/threads.h +++ b/mingw-w64-headers/crt/threads.h @@ -53,7 +53,7 @@ int __cdecl cnd_init(cnd_t *_Cond); int __cdecl cnd_signal(cnd_t *_Cond); int __cdecl _cnd_timedwait32(cnd_t *_Cond, mtx_t *_Mtx, const struct _timespec32 *_Ts); int __cdecl _cnd_timedwait64(cnd_t *_Cond, mtx_t *_Mtx, const struct _timespec64 *_Ts); -int cnd_wait(cnd_t *_Cond, mtx_t *_Mtx); +int __cdecl cnd_wait(cnd_t *_Cond, mtx_t *_Mtx); /* thrd */ @@ -99,7 +99,7 @@ int __cdecl tss_set(tss_t _Key, void *_Val); /* once */ -#define ONCE_FLAG_INIT { 0 } +#define ONCE_FLAG_INIT { NULL } typedef struct { void *_Opaque; -- 2.20.1 >From 83475a686d312bc03fb299db3eafdf35751e311f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]> Date: Sun, 4 Oct 2026 23:10:39 +0200 Subject: [PATCH 2/5] crt: Deduplicate Makefile.am rules for libucrtbase.a/libucrtbased.a via generic pattern Use same generic pattern which is already used for the libucrt.a library. Because mingw-w64 contains files libucrtbase.def/libucrtbased.def in its source three, the "lib64/lib%.a: lib-common/%.def" rule can take precedence for the generic pattern libucrtbase.a/libucrtbased.a rule. So use the GNU make syntax "target : pattern : dependency" to explicitly define targets. --- mingw-w64-crt/Makefile.am | 50 +++++++++++---------------------------- 1 file changed, 14 insertions(+), 36 deletions(-) diff --git a/mingw-w64-crt/Makefile.am b/mingw-w64-crt/Makefile.am index 1d0cbc77c2f1..c9aedafa9b2f 100644 --- a/mingw-w64-crt/Makefile.am +++ b/mingw-w64-crt/Makefile.am @@ -1765,15 +1765,7 @@ lib32_DATA += lib32/libmsvcr120_app.a lib32/libmsvcr120_app.a: lib-common/msvcr120_app.mri lib32/libmsvcr120_app_def.a lib32/libmsvcrt_common.a lib32/libmsvcr120_extra.a lib32/libmsvcr120_app_extra.a $(AM_V_GEN)cd $(dir $@) && $(AR) -M < $(abspath $<) -lib32_DATA += lib32/libucrtbase.a -lib32/libucrtbase.a: lib-common/ucrtbase.mri lib32/libucrtbase_def.a lib32/libucrt_extra.a - $(AM_V_GEN)cd $(dir $@) && $(AR) -M < $(abspath $<) - -lib32_DATA += lib32/libucrtbased.a -lib32/libucrtbased.a: lib-common/ucrtbased.mri lib32/libucrtbased_def.a lib32/libucrt_extra.a - $(AM_V_GEN)cd $(dir $@) && $(AR) -M < $(abspath $<) - -lib32_DATA += lib32/libucrt.a lib32/libucrtapp.a lib32/libucrt_vcruntime140.a +lib32_DATA += lib32/libucrt.a lib32/libucrtbase.a lib32/libucrtbased.a lib32/libucrtapp.a lib32/libucrt_vcruntime140.a noinst_LIBRARIES += lib32/libcrtdll_extra.a lib32/libmsvcrt10_extra.a lib32/libmsvcrt20_extra.a lib32/libmsvcrt40_extra.a lib32/libmsvcrt_common.a lib32/libmsvcrt_extra.a lib32/libmsvcrtd_extra.a lib32/libmsvcr70_extra.a lib32/libmsvcr71_extra.a lib32/libmsvcr80_extra.a lib32/libmsvcr90_extra.a lib32/libmsvcr100_extra.a lib32/libmsvcr110_extra.a lib32/libmsvcr120_extra.a lib32/libmsvcr120_app_extra.a lib32/libucrt_extra.a lib32/libucrtapp_extra.a lib32_libcrtdll_extra_a_SOURCES = $(src_crtdll) lib32_libcrtdll_extra_a_CPPFLAGS = $(CPPFLAGS32) -D__LIBMSVCRT__ $(extra_include) $(sysincludes) @@ -2148,15 +2140,7 @@ lib64_DATA += lib64/libmsvcr120_app.a lib64/libmsvcr120_app.a: lib-common/msvcr120_app.mri lib64/libmsvcr120_app_def.a lib64/libmsvcrt_common.a lib64/libmsvcr120_extra.a lib64/libmsvcr120_app_extra.a $(AM_V_GEN)cd $(dir $@) && $(AR) -M < $(abspath $<) -lib64_DATA += lib64/libucrtbase.a -lib64/libucrtbase.a: lib-common/ucrtbase.mri lib64/libucrtbase_def.a lib64/libucrt_extra.a - $(AM_V_GEN)cd $(dir $@) && $(AR) -M < $(abspath $<) - -lib64_DATA += lib64/libucrtbased.a -lib64/libucrtbased.a: lib-common/ucrtbased.mri lib64/libucrtbased_def.a lib64/libucrt_extra.a - $(AM_V_GEN)cd $(dir $@) && $(AR) -M < $(abspath $<) - -lib64_DATA += lib64/libucrt.a lib64/libucrtapp.a lib64/libucrt_vcruntime140.a +lib64_DATA += lib64/libucrt.a lib64/libucrtbase.a lib64/libucrtbased.a lib64/libucrtapp.a lib64/libucrt_vcruntime140.a noinst_LIBRARIES += lib64/libmsvcrt_common.a lib64/libmsvcrt_extra.a lib64/libmsvcr80_extra.a lib64/libmsvcr90_extra.a lib64/libmsvcr100_extra.a lib64/libmsvcr110_extra.a lib64/libmsvcr120_extra.a lib64/libmsvcr120_app_extra.a lib64/libucrt_extra.a lib64/libucrtapp_extra.a lib64_libmsvcrt_common_a_SOURCES = $(src_msvcrt_common64) lib64_libmsvcrt_common_a_CPPFLAGS=$(CPPFLAGS64) -D__LIBMSVCRT__ $(extra_include) $(sysincludes) @@ -2487,15 +2471,7 @@ libarm32_DATA += libarm32/libmsvcr120_app.a libarm32/libmsvcr120_app.a: lib-common/msvcr120_app.mri libarm32/libmsvcr120_app_def.a libarm32/libmsvcrt_common.a libarm32/libmsvcr120_app_extra.a $(AM_V_GEN)cd $(dir $@) && $(AR) -M < $(abspath $<) -libarm32_DATA += libarm32/libucrtbase.a -libarm32/libucrtbase.a: lib-common/ucrtbase.mri libarm32/libucrtbase_def.a libarm32/libucrt_extra.a - $(AM_V_GEN)cd $(dir $@) && $(AR) -M < $(abspath $<) - -libarm32_DATA += libarm32/libucrtbased.a -libarm32/libucrtbased.a: lib-common/ucrtbased.mri libarm32/libucrtbased_def.a libarm32/libucrt_extra.a - $(AM_V_GEN)cd $(dir $@) && $(AR) -M < $(abspath $<) - -libarm32_DATA += libarm32/libucrt.a libarm32/libucrtapp.a libarm32/libucrt_vcruntime140.a +libarm32_DATA += libarm32/libucrt.a libarm32/libucrtbase.a libarm32/libucrtbased.a libarm32/libucrtapp.a libarm32/libucrt_vcruntime140.a noinst_LIBRARIES += libarm32/libmsvcrt_common.a libarm32/libmsvcrt_extra.a libarm32/libmsvcr110_extra.a libarm32/libmsvcr120_extra.a libarm32/libmsvcr120_app_extra.a libarm32/libucrt_extra.a libarm32/libucrtapp_extra.a libarm32_libmsvcrt_common_a_SOURCES = $(src_msvcrt_commonarm32) libarm32_libmsvcrt_common_a_CPPFLAGS=$(CPPFLAGSARM32) -D__LIBMSVCRT__ $(extra_include) $(sysincludes) @@ -2820,15 +2796,7 @@ endif include libarm64/Makefile.am if !W32API -libarm64_DATA += libarm64/libucrtbase.a -libarm64/libucrtbase.a: lib-common/ucrtbase.mri libarm64/libucrtbase_def.a libarm64/libucrt_extra.a - $(AM_V_GEN)cd $(dir $@) && $(AR) -M < $(abspath $<) - -libarm64_DATA += libarm64/libucrtbased.a -libarm64/libucrtbased.a: lib-common/ucrtbased.mri libarm64/libucrtbased_def.a libarm64/libucrt_extra.a - $(AM_V_GEN)cd $(dir $@) && $(AR) -M < $(abspath $<) - -libarm64_DATA += libarm64/libucrt.a libarm64/libucrtapp.a libarm64/libucrt_vcruntime140.a +libarm64_DATA += libarm64/libucrt.a libarm64/libucrtbase.a libarm64/libucrtbased.a libarm64/libucrtapp.a libarm64/libucrt_vcruntime140.a noinst_LIBRARIES += libarm64/libmsvcrt_common.a libarm64/libmsvcrt_extra.a libarm64/libucrt_extra.a libarm64/libucrtapp_extra.a libarm64_libmsvcrt_common_a_SOURCES = $(src_msvcrt_commonarm64) libarm64_libmsvcrt_common_a_CPPFLAGS=$(CPPFLAGSARM64) -D__LIBMSVCRT__ $(extra_include) $(sysincludes) @@ -4501,6 +4469,16 @@ endif $(AM_V_GEN)cd $(dir $@) && $(AR) -M < $(abspath $<) $(AM_V_at)$(RANLIB) $@ +lib32/libucrtbase.a lib64/libucrtbase.a libarm32/libucrtbase.a libarm64/libucrtbase.a: %/libucrtbase.a: lib-common/ucrtbase.mri %/libucrtbase_def.a %/libucrt_extra.a + $(AM_V_at)rm -f $@ + $(AM_V_GEN)cd $(dir $@) && $(AR) -M < $(abspath $<) + $(AM_V_at)$(RANLIB) $@ + +lib32/libucrtbased.a lib64/libucrtbased.a libarm32/libucrtbased.a libarm64/libucrtbased.a: %/libucrtbased.a: lib-common/ucrtbased.mri %/libucrtbased_def.a %/libucrt_extra.a + $(AM_V_at)rm -f $@ + $(AM_V_GEN)cd $(dir $@) && $(AR) -M < $(abspath $<) + $(AM_V_at)$(RANLIB) $@ + %/libucrt_common.a: lib-common/ucrt_common.mri %/libucrt_extra.a \ %/libapi-ms-win-crt-conio-l1-1-0.a %/libapi-ms-win-crt-convert-l1-1-0.a %/libapi-ms-win-crt-environment-l1-1-0.a \ %/libapi-ms-win-crt-filesystem-l1-1-0.a %/libapi-ms-win-crt-heap-l1-1-0.a %/libapi-ms-win-crt-locale-l1-1-0.a \ -- 2.20.1 >From eea56f44c1c0e85c101bba58b914b227a1f381c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]> Date: Sun, 4 Oct 2026 22:11:19 +0200 Subject: [PATCH 3/5] crt: Implement C11 threads.h thrd_* functions Include them into all CRT import libraries which do not provide them nativelly (all except ucrt_vcruntime140 on non-arm32). --- mingw-w64-crt/Makefile.am | 34 +++++++++++++--- mingw-w64-crt/lib-common/ucrt.mri | 1 + mingw-w64-crt/lib-common/ucrtapp.mri | 1 + mingw-w64-crt/lib-common/ucrtbase.mri | 1 + mingw-w64-crt/lib-common/ucrtbased.mri | 1 + mingw-w64-crt/libarm32/ucrt_vcruntime140.mri | 1 + mingw-w64-crt/threads/_thrd_sleep32.c | 13 ++++++ mingw-w64-crt/threads/_thrd_sleep64.c | 24 +++++++++++ mingw-w64-crt/threads/thrd_create.c | 43 ++++++++++++++++++++ mingw-w64-crt/threads/thrd_current.c | 13 ++++++ mingw-w64-crt/threads/thrd_detach.c | 13 ++++++ mingw-w64-crt/threads/thrd_equal.c | 12 ++++++ mingw-w64-crt/threads/thrd_exit.c | 13 ++++++ mingw-w64-crt/threads/thrd_join.c | 20 +++++++++ mingw-w64-crt/threads/thrd_yield.c | 40 ++++++++++++++++++ 15 files changed, 225 insertions(+), 5 deletions(-) create mode 100644 mingw-w64-crt/threads/_thrd_sleep32.c create mode 100644 mingw-w64-crt/threads/_thrd_sleep64.c create mode 100644 mingw-w64-crt/threads/thrd_create.c create mode 100644 mingw-w64-crt/threads/thrd_current.c create mode 100644 mingw-w64-crt/threads/thrd_detach.c create mode 100644 mingw-w64-crt/threads/thrd_equal.c create mode 100644 mingw-w64-crt/threads/thrd_exit.c create mode 100644 mingw-w64-crt/threads/thrd_join.c create mode 100644 mingw-w64-crt/threads/thrd_yield.c diff --git a/mingw-w64-crt/Makefile.am b/mingw-w64-crt/Makefile.am index c9aedafa9b2f..93b420e6301b 100644 --- a/mingw-w64-crt/Makefile.am +++ b/mingw-w64-crt/Makefile.am @@ -174,8 +174,20 @@ src_libws2_32=libsrc/ws2_32.c \ libsrc/wspiapi/WspiapiLoad.c libsrc/wspiapi/WspiapiGetAddrInfo.c libsrc/wspiapi/WspiapiGetNameInfo.c \ libsrc/wspiapi/WspiapiFreeAddrInfo.c +src_c11_threads=\ + threads/_thrd_sleep32.c \ + threads/_thrd_sleep64.c \ + threads/thrd_create.c \ + threads/thrd_current.c \ + threads/thrd_detach.c \ + threads/thrd_equal.c \ + threads/thrd_exit.c \ + threads/thrd_join.c \ + threads/thrd_yield.c + # Files included in all libmsvcr*.a src_msvcrt_common=\ + $(src_c11_threads) \ include/msvcrt.h \ include/msvcrt_or_emu_glue.h \ include/mingw-wchar.h \ @@ -1681,6 +1693,9 @@ include lib32/Makefile.am if !W32API lib32_DATA += lib32/libglut.a lib32/libmsvcp60.a lib32/libmsvcp120_app.a +noinst_LIBRARIES += lib32/libc11_threads.a +lib32_libc11_threads_a_SOURCES = $(src_c11_threads) + lib32_DATA += lib32/libcrtdll.a lib32/libcrtdll.a: lib32/crtdll.mri lib32/libcrtdll_def.a lib32/libmsvcrt_common.a lib32/libcrtdll_extra.a $(AM_V_GEN)cd $(dir $@) && $(AR) -M < $(abspath $<) @@ -2096,6 +2111,9 @@ include lib64/Makefile.am if !W32API lib64_DATA += lib64/libmsvcp60.a lib64/libmsvcp120_app.a +noinst_LIBRARIES += lib64/libc11_threads.a +lib64_libc11_threads_a_SOURCES = $(src_c11_threads) + lib64_DATA += lib64/libmsvcr80.a lib64/libmsvcr80.a: lib-common/msvcr80.mri lib64/libmsvcr80_def.a lib64/libmsvcrt_common.a lib64/libmsvcr80_extra.a $(AM_V_GEN)cd $(dir $@) && $(AR) -M < $(abspath $<) @@ -2451,6 +2469,9 @@ include libarm32/Makefile.am if !W32API libarm32_DATA += libarm32/libmsvcp60.a +noinst_LIBRARIES += libarm32/libc11_threads.a +libarm32_libc11_threads_a_SOURCES = $(src_c11_threads) + libarm32_DATA += libarm32/libmsvcr110.a libarm32/libmsvcr110.a: lib-common/msvcr110.mri libarm32/libmsvcr110_def.a libarm32/libmsvcrt_common.a libarm32/libmsvcr110_extra.a $(AM_V_GEN)cd $(dir $@) && $(AR) -M < $(abspath $<) @@ -2796,6 +2817,9 @@ endif include libarm64/Makefile.am if !W32API +noinst_LIBRARIES += libarm64/libc11_threads.a +libarm64_libc11_threads_a_SOURCES = $(src_c11_threads) + libarm64_DATA += libarm64/libucrt.a libarm64/libucrtbase.a libarm64/libucrtbased.a libarm64/libucrtapp.a libarm64/libucrt_vcruntime140.a noinst_LIBRARIES += libarm64/libmsvcrt_common.a libarm64/libmsvcrt_extra.a libarm64/libucrt_extra.a libarm64/libucrtapp_extra.a libarm64_libmsvcrt_common_a_SOURCES = $(src_msvcrt_commonarm64) @@ -4469,12 +4493,12 @@ endif $(AM_V_GEN)cd $(dir $@) && $(AR) -M < $(abspath $<) $(AM_V_at)$(RANLIB) $@ -lib32/libucrtbase.a lib64/libucrtbase.a libarm32/libucrtbase.a libarm64/libucrtbase.a: %/libucrtbase.a: lib-common/ucrtbase.mri %/libucrtbase_def.a %/libucrt_extra.a +lib32/libucrtbase.a lib64/libucrtbase.a libarm32/libucrtbase.a libarm64/libucrtbase.a: %/libucrtbase.a: lib-common/ucrtbase.mri %/libucrtbase_def.a %/libucrt_extra.a %/libc11_threads.a $(AM_V_at)rm -f $@ $(AM_V_GEN)cd $(dir $@) && $(AR) -M < $(abspath $<) $(AM_V_at)$(RANLIB) $@ -lib32/libucrtbased.a lib64/libucrtbased.a libarm32/libucrtbased.a libarm64/libucrtbased.a: %/libucrtbased.a: lib-common/ucrtbased.mri %/libucrtbased_def.a %/libucrt_extra.a +lib32/libucrtbased.a lib64/libucrtbased.a libarm32/libucrtbased.a libarm64/libucrtbased.a: %/libucrtbased.a: lib-common/ucrtbased.mri %/libucrtbased_def.a %/libucrt_extra.a %/libc11_threads.a $(AM_V_at)rm -f $@ $(AM_V_GEN)cd $(dir $@) && $(AR) -M < $(abspath $<) $(AM_V_at)$(RANLIB) $@ @@ -4489,20 +4513,20 @@ lib32/libucrtbased.a lib64/libucrtbased.a libarm32/libucrtbased.a libarm64/libuc $(AM_V_GEN)cd $(dir $@) && $(AR) -M < $(abspath $<) $(AM_V_at)$(RANLIB) $@ -%/libucrt.a: lib-common/ucrt.mri %/libucrt_common.a %/libapi-ms-win-crt-private-l1-1-0.a +%/libucrt.a: lib-common/ucrt.mri %/libucrt_common.a %/libapi-ms-win-crt-private-l1-1-0.a %/libc11_threads.a $(AM_V_at)rm -f $@ $(AM_V_GEN)cd $(dir $@) && $(AR) -M < $(abspath $<) $(AM_V_at)$(RANLIB) $@ # The same as libucrt.a above, but without the -private lib and with some more # statically linked functions. -%/libucrtapp.a: lib-common/ucrtapp.mri %/libucrt_common.a %/libucrtapp_extra.a +%/libucrtapp.a: lib-common/ucrtapp.mri %/libucrt_common.a %/libucrtapp_extra.a %/libc11_threads.a $(AM_V_at)rm -f $@ $(AM_V_GEN)cd $(dir $@) && $(AR) -M < $(abspath $<) $(AM_V_at)$(RANLIB) $@ # The same as libucrt.a above, but without the -private lib and with vcruntime140 + vcruntime140_threads lib (for non-arm32) -libarm32/libucrt_vcruntime140.a: %/libucrt_vcruntime140.a: libarm32/ucrt_vcruntime140.mri %/libucrt_common.a %/libvcruntime140.a +libarm32/libucrt_vcruntime140.a: %/libucrt_vcruntime140.a: libarm32/ucrt_vcruntime140.mri %/libucrt_common.a %/libvcruntime140.a %/libc11_threads.a lib32/libucrt_vcruntime140.a lib64/libucrt_vcruntime140.a libarm64/libucrt_vcruntime140.a: %/libucrt_vcruntime140.a: lib-common/ucrt_vcruntime140.mri %/libucrt_common.a %/libvcruntime140.a %/libvcruntime140_threads.a %/libucrt_vcruntime140.a: $(AM_V_at)rm -f $@ diff --git a/mingw-w64-crt/lib-common/ucrt.mri b/mingw-w64-crt/lib-common/ucrt.mri index c7ec391278ce..d81a0610e154 100644 --- a/mingw-w64-crt/lib-common/ucrt.mri +++ b/mingw-w64-crt/lib-common/ucrt.mri @@ -1,5 +1,6 @@ CREATE libucrt.a ADDLIB libucrt_common.a ADDLIB libapi-ms-win-crt-private-l1-1-0.a +ADDLIB libc11_threads.a SAVE END diff --git a/mingw-w64-crt/lib-common/ucrtapp.mri b/mingw-w64-crt/lib-common/ucrtapp.mri index 8919e4bdc6ba..bd9391dc9757 100644 --- a/mingw-w64-crt/lib-common/ucrtapp.mri +++ b/mingw-w64-crt/lib-common/ucrtapp.mri @@ -1,5 +1,6 @@ CREATE libucrtapp.a ADDLIB libucrt_common.a ADDLIB libucrtapp_extra.a +ADDLIB libc11_threads.a SAVE END diff --git a/mingw-w64-crt/lib-common/ucrtbase.mri b/mingw-w64-crt/lib-common/ucrtbase.mri index c9e3d792a76c..6bd497f614fb 100644 --- a/mingw-w64-crt/lib-common/ucrtbase.mri +++ b/mingw-w64-crt/lib-common/ucrtbase.mri @@ -1,5 +1,6 @@ CREATE libucrtbase.a ADDLIB libucrtbase_def.a ADDLIB libucrt_extra.a +ADDLIB libc11_threads.a SAVE END diff --git a/mingw-w64-crt/lib-common/ucrtbased.mri b/mingw-w64-crt/lib-common/ucrtbased.mri index fdf8f1b19886..e0e0bd5ce575 100644 --- a/mingw-w64-crt/lib-common/ucrtbased.mri +++ b/mingw-w64-crt/lib-common/ucrtbased.mri @@ -1,5 +1,6 @@ CREATE libucrtbased.a ADDLIB libucrtbased_def.a ADDLIB libucrt_extra.a +ADDLIB libc11_threads.a SAVE END diff --git a/mingw-w64-crt/libarm32/ucrt_vcruntime140.mri b/mingw-w64-crt/libarm32/ucrt_vcruntime140.mri index c5a4161c45f2..b88b71b3f20a 100644 --- a/mingw-w64-crt/libarm32/ucrt_vcruntime140.mri +++ b/mingw-w64-crt/libarm32/ucrt_vcruntime140.mri @@ -1,5 +1,6 @@ CREATE libucrt_vcruntime140.a ADDLIB libucrt_common.a ADDLIB libvcruntime140.a +ADDLIB libc11_threads.a SAVE END diff --git a/mingw-w64-crt/threads/_thrd_sleep32.c b/mingw-w64-crt/threads/_thrd_sleep32.c new file mode 100644 index 000000000000..0a75620e2c8c --- /dev/null +++ b/mingw-w64-crt/threads/_thrd_sleep32.c @@ -0,0 +1,13 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the mingw-w64 runtime package. + * No warranty is given; refer to the file DISCLAIMER.PD within this package. + */ + +#include <threads.h> + +int __cdecl _thrd_sleep32(const struct _timespec32 *duration, struct _timespec32 *__UNUSED_PARAM(remaining)) +{ + return _thrd_sleep64(&(struct _timespec64){.tv_sec = duration->tv_sec, .tv_nsec = duration->tv_nsec}, NULL); + /* vcruntime140_threads.dll _thrd_sleep32 does not touch remaining */ +} diff --git a/mingw-w64-crt/threads/_thrd_sleep64.c b/mingw-w64-crt/threads/_thrd_sleep64.c new file mode 100644 index 000000000000..49b80cd03a87 --- /dev/null +++ b/mingw-w64-crt/threads/_thrd_sleep64.c @@ -0,0 +1,24 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the mingw-w64 runtime package. + * No warranty is given; refer to the file DISCLAIMER.PD within this package. + */ + +#include <threads.h> +#include <windows.h> /* for Sleep() */ + +int __cdecl _thrd_sleep64(const struct _timespec64 *duration, struct _timespec64 *__UNUSED_PARAM(remaining)) +{ + long long ms; + if (__builtin_mul_overflow(duration->tv_sec, 1000, &ms)) + return -2; + if (__builtin_add_overflow(ms, (duration->tv_nsec + 999999) / 1000000, &ms)) + return -2; + if (ms > 0xffffffff) + return -2; + if (ms < 0) + ms = 0; + Sleep(ms); + /* vcruntime140_threads.dll _thrd_sleep64 does not touch remaining */ + return 0; +} diff --git a/mingw-w64-crt/threads/thrd_create.c b/mingw-w64-crt/threads/thrd_create.c new file mode 100644 index 000000000000..31bf8aea7adb --- /dev/null +++ b/mingw-w64-crt/threads/thrd_create.c @@ -0,0 +1,43 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the mingw-w64 runtime package. + * No warranty is given; refer to the file DISCLAIMER.PD within this package. + */ + +#include <threads.h> +#include <stdlib.h> /* for malloc() and free() */ +#include <process.h> /* for _beginthreadex() */ +#include <errno.h> /* for errno and ENOMEM */ + +#if defined(__i386__) +/* We need to make sure that we align the stack to 16 bytes for the sake of SSE */ +__attribute__((force_align_arg_pointer)) +#endif +static unsigned __stdcall thread_func(void *data) +{ + void **thread_args = data; + thrd_start_t func = thread_args[0]; + void *arg = thread_args[1]; + free(thread_args); + return func(arg); +} + +int __cdecl thrd_create(thrd_t *thr, thrd_start_t func, void *arg) +{ + void *thread_handle; + unsigned thread_id; + void **thread_args = malloc(2 * sizeof(void *)); + if (!thread_args) + return thrd_nomem; + thread_args[0] = (void *)func; + thread_args[1] = arg; + /* Use _beginthreadex() instead of CreateThread() to ensure that C signal dispatcher is properly registered */ + thread_handle = (void *)_beginthreadex(NULL, 0, thread_func, thread_args, 0, &thread_id); + if (!thread_handle) { + free(thread_args); + return errno == ENOMEM ? thrd_nomem : thrd_error; + } + thr->_Handle = thread_handle; + thr->_Tid = thread_id; + return thrd_success; +} diff --git a/mingw-w64-crt/threads/thrd_current.c b/mingw-w64-crt/threads/thrd_current.c new file mode 100644 index 000000000000..0fd1650ed655 --- /dev/null +++ b/mingw-w64-crt/threads/thrd_current.c @@ -0,0 +1,13 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the mingw-w64 runtime package. + * No warranty is given; refer to the file DISCLAIMER.PD within this package. + */ + +#include <threads.h> +#include <windows.h> /* for GetCurrentThreadId() */ + +thrd_t __cdecl thrd_current(void) +{ + return (thrd_t){NULL, GetCurrentThreadId()}; +} diff --git a/mingw-w64-crt/threads/thrd_detach.c b/mingw-w64-crt/threads/thrd_detach.c new file mode 100644 index 000000000000..3d174f408759 --- /dev/null +++ b/mingw-w64-crt/threads/thrd_detach.c @@ -0,0 +1,13 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the mingw-w64 runtime package. + * No warranty is given; refer to the file DISCLAIMER.PD within this package. + */ + +#include <threads.h> +#include <windows.h> /* for CloseHandle() */ + +int __cdecl thrd_detach(thrd_t thr) +{ + return CloseHandle(thr._Handle) ? thrd_success : thrd_error; +} diff --git a/mingw-w64-crt/threads/thrd_equal.c b/mingw-w64-crt/threads/thrd_equal.c new file mode 100644 index 000000000000..a8dcf61097f4 --- /dev/null +++ b/mingw-w64-crt/threads/thrd_equal.c @@ -0,0 +1,12 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the mingw-w64 runtime package. + * No warranty is given; refer to the file DISCLAIMER.PD within this package. + */ + +#include <threads.h> + +int __cdecl thrd_equal(thrd_t thr0, thrd_t thr1) +{ + return thr0._Tid == thr1._Tid; +} diff --git a/mingw-w64-crt/threads/thrd_exit.c b/mingw-w64-crt/threads/thrd_exit.c new file mode 100644 index 000000000000..7625a1d2b37c --- /dev/null +++ b/mingw-w64-crt/threads/thrd_exit.c @@ -0,0 +1,13 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the mingw-w64 runtime package. + * No warranty is given; refer to the file DISCLAIMER.PD within this package. + */ + +#include <threads.h> +#include <windows.h> /* for ExitThread() */ + +void __cdecl __MINGW_ATTRIB_NORETURN thrd_exit(int res) +{ + ExitThread(res); +} diff --git a/mingw-w64-crt/threads/thrd_join.c b/mingw-w64-crt/threads/thrd_join.c new file mode 100644 index 000000000000..836ce2d03dce --- /dev/null +++ b/mingw-w64-crt/threads/thrd_join.c @@ -0,0 +1,20 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the mingw-w64 runtime package. + * No warranty is given; refer to the file DISCLAIMER.PD within this package. + */ + +#include <threads.h> +#include <windows.h> /* for WaitForSingleObject(), GetExitCodeThread() and CloseHandle() */ + +int __cdecl thrd_join(thrd_t thr, int *res) +{ + DWORD exit_code; + if (WaitForSingleObject(thr._Handle, INFINITE)) + return thrd_error; + if (!GetExitCodeThread(thr._Handle, &exit_code)) + return thrd_error; + if (res) + *res = exit_code; + return CloseHandle(thr._Handle) ? thrd_success : thrd_error; +} diff --git a/mingw-w64-crt/threads/thrd_yield.c b/mingw-w64-crt/threads/thrd_yield.c new file mode 100644 index 000000000000..3e25b0698072 --- /dev/null +++ b/mingw-w64-crt/threads/thrd_yield.c @@ -0,0 +1,40 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the mingw-w64 runtime package. + * No warranty is given; refer to the file DISCLAIMER.PD within this package. + */ + +#include <threads.h> +#include <windows.h> /* for SwitchToThread() */ + +#if defined(__i386__) + +static WINBOOL WINAPI SwitchToThreadEmul(void) +{ + Sleep(0); + return TRUE; +} +_Static_assert(__builtin_types_compatible_p(typeof(SwitchToThreadEmul), typeof(SwitchToThread)), "SwitchToThreadEmul() and SwitchToThread() are not compatible"); + +static WINBOOL WINAPI SwitchToThreadInit(void); +static typeof(SwitchToThread) *SwitchToThreadPtr = SwitchToThreadInit; + +static WINBOOL WINAPI SwitchToThreadInit(void) +{ + /* SwitchToThread() is not available in older WinNT versions */ + HMODULE kernel32 = GetModuleHandleA("kernel32.dll"); + FARPROC proc = kernel32 ? GetProcAddress(kernel32, "SwitchToThread") : NULL; + if (!proc) proc = SwitchToThreadEmul; + (void)InterlockedExchangePointer((PVOID *)&SwitchToThreadPtr, proc); + return SwitchToThreadPtr(); +} +_Static_assert(__builtin_types_compatible_p(typeof(SwitchToThreadInit), typeof(SwitchToThread)), "SwitchToThreadInit() and SwitchToThread() are not compatible"); + +#define SwitchToThread SwitchToThreadPtr + +#endif + +void __cdecl thrd_yield(void) +{ + SwitchToThread(); +} -- 2.20.1 >From 6dba889da04326bf36269388983414a2df44172d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]> Date: Sun, 4 Oct 2026 22:11:36 +0200 Subject: [PATCH 4/5] crt: Implement C11 threads.h once_* functions --- mingw-w64-crt/Makefile.am | 1 + mingw-w64-crt/threads/call_once.c | 70 +++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 mingw-w64-crt/threads/call_once.c diff --git a/mingw-w64-crt/Makefile.am b/mingw-w64-crt/Makefile.am index 93b420e6301b..1cef1f6d2aa3 100644 --- a/mingw-w64-crt/Makefile.am +++ b/mingw-w64-crt/Makefile.am @@ -177,6 +177,7 @@ src_libws2_32=libsrc/ws2_32.c \ src_c11_threads=\ threads/_thrd_sleep32.c \ threads/_thrd_sleep64.c \ + threads/call_once.c \ threads/thrd_create.c \ threads/thrd_current.c \ threads/thrd_detach.c \ diff --git a/mingw-w64-crt/threads/call_once.c b/mingw-w64-crt/threads/call_once.c new file mode 100644 index 000000000000..df91f4ded8d5 --- /dev/null +++ b/mingw-w64-crt/threads/call_once.c @@ -0,0 +1,70 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the mingw-w64 runtime package. + * No warranty is given; refer to the file DISCLAIMER.PD within this package. + */ + +#include <threads.h> +#include <windows.h> /* for InitOnceExecuteOnce */ + +#if defined(__i386__) || defined(__x86_64__) + +static WINBOOL WINAPI InitOnceExecuteOnceEmul(INIT_ONCE *init_once, PINIT_ONCE_FN init_fn, PVOID parameter, LPVOID *context) +{ + /* For pre-Vista systems use ABI compatible InitOnceExecuteOnce() logic: + * state 0 - func did not finished successfully yet + * state 1 - func is being executing + * state 2 - func already finished successfully + */ + while (init_once->Ptr != (void *)2) { + if (InterlockedCompareExchangePointer(&init_once->Ptr, (void *)1, 0) == (void *)0) { + if (!init_fn(init_once, parameter, context)) { + (void)InterlockedExchangePointer(&init_once->Ptr, (void *)0); + return FALSE; + } else { + (void)InterlockedExchangePointer(&init_once->Ptr, (void *)2); + return TRUE; + } + } else { + while (init_once->Ptr == (void *)1) + Sleep(1); + } + } + return TRUE; +} +_Static_assert(__builtin_types_compatible_p(typeof(InitOnceExecuteOnceEmul), typeof(InitOnceExecuteOnce)), "InitOnceExecuteOnceEmul() and InitOnceExecuteOnce() are not compatible"); + +static WINBOOL WINAPI InitOnceExecuteOnceInit(INIT_ONCE *init_once, PINIT_ONCE_FN init_fn, PVOID parameter, LPVOID *context); +static typeof(InitOnceExecuteOnce) *InitOnceExecuteOncePtr = InitOnceExecuteOnceInit; + +static WINBOOL WINAPI InitOnceExecuteOnceInit(INIT_ONCE *init_once, PINIT_ONCE_FN init_fn, PVOID parameter, LPVOID *context) +{ + /* InitOnceExecuteOnce() is available since Windows Vista */ + HMODULE kernel32 = GetModuleHandleA("kernel32.dll"); + FARPROC proc = kernel32 ? GetProcAddress(kernel32, "InitOnceExecuteOnce") : NULL; + if (!proc) proc = (FARPROC)(void(*)(void))InitOnceExecuteOnceEmul; + (void)InterlockedExchangePointer((PVOID *)&InitOnceExecuteOncePtr, proc); + return InitOnceExecuteOncePtr(init_once, init_fn, parameter, context); +} +_Static_assert(__builtin_types_compatible_p(typeof(InitOnceExecuteOnceInit), typeof(InitOnceExecuteOnce)), "InitOnceExecuteOnceInit() and InitOnceExecuteOnce() are not compatible"); + +#define InitOnceExecuteOnce InitOnceExecuteOncePtr + +#endif + +#if defined(__i386__) +/* We need to make sure that we align the stack to 16 bytes for the sake of SSE */ +__attribute__((force_align_arg_pointer)) +#endif +static WINBOOL WINAPI call_once_func(INIT_ONCE *__UNUSED_PARAM(init_once), PVOID parameter, PVOID *__UNUSED_PARAM(context)) +{ + void(__cdecl*func)(void) = (void(__cdecl*)(void))parameter; + func(); + return TRUE; +} + +void __cdecl call_once(once_flag *flag, void(__cdecl*func)(void)) +{ + _Static_assert(sizeof(once_flag) == sizeof(INIT_ONCE), "once_flag and INIT_ONCE are not size-compatible"); + InitOnceExecuteOnce((INIT_ONCE *)flag, call_once_func, (void *)func, NULL); +} -- 2.20.1 >From 0c17086acf315da340310f1e42338240d1289d42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pali=20Roh=C3=A1r?= <[email protected]> Date: Sun, 4 Oct 2026 23:24:40 +0200 Subject: [PATCH 5/5] crt: test: Extend t_sigfpe.c for C11 threads.h --- mingw-w64-crt/testcases/t_sigfpe.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/mingw-w64-crt/testcases/t_sigfpe.c b/mingw-w64-crt/testcases/t_sigfpe.c index 6cb7263330b0..5cdc9f415422 100644 --- a/mingw-w64-crt/testcases/t_sigfpe.c +++ b/mingw-w64-crt/testcases/t_sigfpe.c @@ -9,6 +9,7 @@ #include <stdint.h> #include <stdio.h> #include <stdlib.h> +#include <threads.h> #ifdef _WIN32 #include <process.h> @@ -1191,6 +1192,12 @@ static void __cdecl test_from_beginthread(void *arg) } #endif +static int __cdecl test_from_thrd_create(void *arg) +{ + (void)arg; + return test(); +} + #ifdef _WIN32 #if defined(__i386__) /* We need to make sure that we align the stack to 16 bytes for the sake of SSE */ @@ -1207,6 +1214,7 @@ static LONG WINAPI catch_unhandled_seh_exception(EXCEPTION_POINTERS *exception_d int main(void) { int ret = 0; + thrd_t cthread; pthread_t pthread; #ifdef _WIN32 HANDLE wthread; @@ -1289,6 +1297,22 @@ int main(void) } #endif + puts(""); + + puts("==== fourth thread via thrd_create ===="); + if (thrd_create(&cthread, test_from_thrd_create, NULL) != thrd_success) + { + puts("\nfailed to spawn fourth thread via thrd_create()"); + ret |= 1; + } + else + { + int tret; + if (thrd_join(cthread, &tret) != thrd_success) + tret = 1; + ret |= tret; + } + if (ret) puts("\nsome subtests failed"); else -- 2.20.1
_______________________________________________ Mingw-w64-public mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/mingw-w64-public
