Commit Graph

300 Commits

Author SHA1 Message Date
Antonin Décimo
04ec40f5bc winpthreads: also use SetThreadDescription to set thread name
Using SetThreadDescription has the advantage that on recent Visual
Studio / Windows, the thread name is always available regardless of
whether a debugger was attached, is also available in crash dumps, and
to other tools.

Quoting MSDN at Set a thread name in C/C++:
https://learn.microsoft.com/en-us/visualstudio/debugger/tips-for-debugging-threads?view=vs-2022&tabs=csharp#set-a-thread-name-in-cc

> There are two ways to set a thread name. The first is via the
> SetThreadDescription function. The second is by throwing a
> particular exception while the Visual Studio debugger is attached to
> the process. Each approach has benefits and caveats. The use of
> SetThreadDescription is supported starting in Windows 10 version
> 1607 or Windows Server 2016.
>
> It's worth noting that both approaches can be used together, if
> desired, since the mechanisms by which they work are independent of
> each other.

Signed-off-by: Antonin Décimo <antonin@tarides.com>
Signed-off-by: LIU Hao <lh_mouse@126.com>
2024-10-29 22:39:01 +08:00
LIU Hao
93f3505a75 winpthreads: Add missing __stdcall on lazy-binding function pointers
Signed-off-by: LIU Hao <lh_mouse@126.com>
2024-09-24 10:41:30 +08:00
Le Philousophe
251fc7e1c3 winpthreads: Fix constructor priority to make it run first
Here is a patch which fixes bugs #988 and #992 following the commit
7b3379.

This commit introduced a new function winpthreads_init in charge of
setting up the _pthread_get_system_time_best_as_file_time and _pthread_get_tick_count_64
pointers using a runtime detection.

This function is defined with the constructor attribute. The problem is
that every static object making use of a constructor is also tagged with
this constructor attribute and the call ordering is unspecified.

For example, when linking code making use of C++11 chrono objects the
libstdc++ code is called before winpthreads_init but makes of the
clock_gettime function which causes a crash.

A minimal reproduction code can be found here in bug #992.

This bug can be fixed by setting a priority of 0 to the
winpthreads_constructor. It is then called before the static
constructors.

Signed-off-by: LIU Hao <lh_mouse@126.com>
2024-09-10 10:02:04 +08:00
Isuru Fernando
c6bf4bdf65 winpthreads: Include pthread_compat.h in sched.h
Signed-off-by: LIU Hao <lh_mouse@126.com>
2024-08-27 09:53:18 +08:00
Antonin Décimo
53c4889a9c winpthreads: add a .gitignore
This helps when vendoring winpthreads as a subtree.

Signed-off-by: LIU Hao <lh_mouse@126.com>
2024-04-25 22:14:16 +08:00
Martin Storsjö
c7d21e9aa8 winpthreads: Avoid using MemoryBarrier() in public headers with GCC/Clang
Commit f281f4969d changed
pthread_cleanup_push() to use MemoryBarrier() instead of
__sync_synchronize(), for various reasons - one of them being
able to build winpthreads with MSVC.

Unfortunately, this change had the effect of breaking user code,
including winpthreads headers and using pthread_cleanup_push()
without including Windows headers first, as the MemoryBarrier()
intrinsic requires including headers to get it properly declared
(both with GCC, Clang and MSVC).

To avoid this issue, revert to __sync_synchronize() in the
installed header, as long as it is included with a GCC compatible
compiler. For MSVC, keep using MemoryBarrier().

Signed-off-by: Martin Storsjö <martin@martin.st>
2024-04-25 13:48:20 +03:00
Antonin Décimo
57f796c80b winpthreads: Move __declspec specifiers before types
Microsoft documentation states that MSVC doesn't support __declspec
after the pointer star for a return type. Move all the specifiers
before return types for consistency.

    // Recommended, selectany & int both part of decl-specifier
    __declspec(selectany) int * pi1 = 0;
    // OK, selectany & int both part of decl-specifier
    int __declspec(selectany) * pi2 = 0;
    // ERROR, selectany is not part of a declarator
    int * __declspec(selectany) pi3 = 0;

https://learn.microsoft.com/en-us/cpp/cpp/declspec?view=msvc-170

Signed-off-by: Antonin Décimo <antonin@tarides.com>
Signed-off-by: Martin Storsjö <martin@martin.st>
2024-02-10 00:11:35 +02:00
Antonin Décimo
15dba71e64 winpthreads: Use GetModuleHandleA to prevent Unicode errors
The non-suffixed macro GetModuleHandle depends on whether the file is
being compiled in Unicode mode or not. Prefer using the char string
literal here, for compatibility with old Windows not supporting
Unicode.

Signed-off-by: Antonin Décimo <antonin@tarides.com>
Signed-off-by: Martin Storsjö <martin@martin.st>
2024-02-10 00:11:35 +02:00
Antonin Décimo
e0908b58f1 winpthreads: Simplify placing a symbol in a section
It is sufficient to specify the section on the symbol declaration.

Signed-off-by: Antonin Décimo <antonin@tarides.com>
Signed-off-by: Martin Storsjö <martin@martin.st>
2024-02-10 00:11:35 +02:00
Antonin Décimo
7b33798917 winpthreads: Unify discovery of recent WinAPI functions
Some WinAPI functions were introduced in recent versions of
Windows. We could detect whether to use the newer functions based on
the _WIN32_WINNT define. However, the issue is that _WIN32_WINNT
defaults to Win10 these days, so a default build of a toolchain won't
work for older targets, which is why it was agreed to do the change to
try to load this function at runtime.

https://sourceforge.net/p/mingw-w64/mailman/message/47371359/,
https://sourceforge.net/p/mingw-w64/mailman/message/49958237/ and
https://sourceforge.net/p/mingw-w64/mailman/message/48131379/.

Multiple workarounds are needed for MSVC to make this detection
portable.

1. Missing constructor attribute in MSVC

> The `constructor` attribute causes the function to be called before
> entering `main()`.

https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-constructor-function-attribute
https://clang.llvm.org/docs/AttributeReference.html#constructor

This feature is not supported by the MSVC compiler, and can be worked
around by putting a function pointer to the constructor in the
.CRT$XC? section of the executable.

https://learn.microsoft.com/en-us/cpp/c-runtime-library/crt-initialization?view=msvc-170#linker-features-for-initialization

We chose the .CRT$XCT section, however the documentation warns:

> The names .CRT$XCT and .CRT$XCV aren't used by either the compiler
> or the CRT library right now, but there's no guarantee that they'll
> remain unused in the future. And, your variables could still be
> optimized away by the compiler. Consider the potential engineering,
> maintenance, and portability issues before adopting this technique.

2. Missing typeof extension in MSVC

As MSVC doesn't support the GCC __typeof__ extension, we explicit the
type of the function pointer.

Casting the result of GetProcAddress (a function pointer) to
a (void *) (a data pointer) is allowed as an extension, mimics the
POSIX dlsym function, and prevents compiler warnings.

3. Missing __atomic functions in MSVC

MSVC doesn't support GCC __atomic functions. Finding whether
GetSystemTimePreciseAsFileTime is present on the system can be done
using a function tagged with the constructor attribute, avoiding the
need of atomics to prevent races.

4. Gather the initialization bits into a single function

Considering the amount of boilerplate code needed to instruct the
linker on MSVC, gather the discovery of the WinAPI functions in a
single winpthreads_init function. The WinAPI functions can be accessed
through internal global function pointers.

Signed-off-by: Antonin Décimo <antonin@tarides.com>
Signed-off-by: Martin Storsjö <martin@martin.st>
2024-02-10 00:11:35 +02:00
Antonin Décimo
f281f4969d winpthreads: Don't use GCC __sync_synchronize
The __sync_synchronize built-in function [1] is deprecated, and not
defined with MSVC. Prefer MemoryBarrier [2] and _ReadWriteBarrier [3].

[1]: https://gcc.gnu.org/onlinedocs/gcc/_005f_005fsync-Builtins.html#index-_005f_005fsync_005fsynchronize
[2]: https://learn.microsoft.com/en-us/windows/win32/api/winnt/nf-winnt-memorybarrier
[3]: https://learn.microsoft.com/en-us/cpp/intrinsics/readwritebarrier\?view\=msvc-170

Signed-off-by: Antonin Décimo <antonin@tarides.com>
Signed-off-by: Martin Storsjö <martin@martin.st>
2024-02-08 23:13:14 +02:00
Antonin Décimo
29def8832e winpthreads: Replace GCC's __sync comparisons with WinAPI functions
Legacy `__sync_*` GCC built-in functions for memory access [1] are
deprecated and MSVC doesn't provide them.
Consequently, use Windows' Interlocked Variable Access [2] functions
instead.

[1]: https://gcc.gnu.org/onlinedocs/gcc/_005f_005fsync-Builtins.html
[2]: https://learn.microsoft.com/en-us/windows/win32/sync/interlocked-variable-access

Signed-off-by: Antonin Décimo <antonin@tarides.com>
Signed-off-by: Martin Storsjö <martin@martin.st>
2024-02-08 23:13:14 +02:00
Antonin Décimo
9a80e57890 winpthreads: Use YieldProcessor in pthread_spin_lock
Use YieldProcessor which already has the best definition for all MSVC
and MinGW supported architectures of the memory pause / yield
instruction.

Signed-off-by: Antonin Décimo <antonin@tarides.com>
Signed-off-by: Martin Storsjö <martin@martin.st>
2024-02-08 23:13:14 +02:00
Antonin Décimo
ad2b46ca1e winpthreads: Fix pthread_create_wrapper type
- Make pthread_create_wrapper return an unsigned;

  _beginthreadindex takes a function returning an unsigned. The
  variable holding the return value of pthread_create_wrapper is
  already an unsigned.

- Make pthread_create_wrapper __stdcall.

Co-authored-by: Samuel Hym <samuel.hym@rustyne.lautre.net>
Signed-off-by: Antonin Décimo <antonin@tarides.com>
Signed-off-by: Martin Storsjö <martin@martin.st>
2024-02-08 23:13:14 +02:00
Antonin Décimo
fa31804358 winpthreads: Check if requested stack size fits in an unsigned int
The requested stack size is a `size_t`, whereas _beginthreadex takes
an `unsigned int`. This provides a bit of input sanitation, and
prevents a compiler warning.

Co-authored-by: Samuel Hym <samuel.hym@rustyne.lautre.net>
Signed-off-by: Antonin Décimo <antonin@tarides.com>
Signed-off-by: Martin Storsjö <martin@martin.st>
2024-02-08 23:13:14 +02:00
LIU Hao
b4515a6935 Regenerate configure with autoconf 2.72
Reference: https://sourceforge.net/p/mingw-w64/mailman/message/58721403/
Signed-off-by: LIU Hao <lh_mouse@126.com>
2024-01-09 22:26:23 +08:00
Antonin Décimo
883cc25d70 winpthreads: Fix strict prototypes warnings
Signed-off-by: Antonin Décimo <antonin@tarides.com>
Signed-off-by: Martin Storsjö <martin@martin.st>
2023-12-20 15:37:44 +02:00
Antonin Décimo
b0655be771 winpthreads: Test explicitly the result of an assign in a condition
This particular snippet raises an annoying MSVC warning, C4706. We
apply the suggested workaround.

> If you intend to make your test value the result of an assignment,
> test to ensure that the assignment is non-zero or not null.

https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-4-c4706?view=msvc-170

Signed-off-by: Antonin Décimo <antonin@tarides.com>
Signed-off-by: Martin Storsjö <martin@martin.st>
2023-12-20 15:37:44 +02:00
Antonin Décimo
0d147fde50 winpthreads: Fix __WINPTHREAD_ENABLE_WRAP_API typo
Retain the old name for backwards compatibility.

Signed-off-by: Antonin Décimo <antonin@tarides.com>
Signed-off-by: Martin Storsjö <martin@martin.st>
2023-12-20 15:37:44 +02:00
Antonin Décimo
1cfe1a81c1 winpthreads: Update version.rc and support RC
Binutils' windres uses the C preprocessor and gets MinGW pre-defined
macros, whereas the Microsoft tool RC [1] implements its own C
preprocessor and doesn't call MSVC, so it defines a subset of the
macros.

Also update the copyright year, license field, and url of the project.

License field is updated to "MIT AND BSD-3-Clause" using SPDX [2]
identifiers and expressions, removing ZPL, considering the COPYING
file in winpthreads.

[1]: https://learn.microsoft.com/en-us/windows/win32/menurc/resource-compiler
[2]: https://spdx.dev/use/specifications/

Signed-off-by: Antonin Décimo <antonin@tarides.com>
Signed-off-by: Martin Storsjö <martin@martin.st>
2023-12-20 15:37:44 +02:00
Antonin Décimo
56102cef5d winpthreads: Regenerate autotools scripts with automake 1.16.5
This is done by running `WANT_AUTOMAKE=1.16 autoreconf -ifv` in the
winpthreads directory.

Signed-off-by: Antonin Décimo <antonin@tarides.com>
Signed-off-by: Martin Storsjö <martin@martin.st>
2023-12-20 15:37:44 +02:00
Antonin Décimo
2d919e7071 winpthreads: Disable warnings for POSIX function names
Microsoft considers these POSIX function names to be deprecated, and
provides alternative names starting with an underscore. A deprecation
warning is raised if the old names are used, which can be disabled
with the _CRT_NONSTDC_NO_WARNINGS macro.

https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-3-c4996?view=msvc-170#posix-function-names

Signed-off-by: Antonin Décimo <antonin@tarides.com>
Signed-off-by: Martin Storsjö <martin@martin.st>
2023-12-20 15:37:44 +02:00
Antonin Décimo
12c236ee8b winpthreads: Separate MSVC and MinGW specific flags and targets
- don't pass -D__USE_MINGW_ANSI_STDIO=0 to MSVC;
- don't build libgcc/fakelib with MSVC.

Signed-off-by: Antonin Décimo <antonin@tarides.com>
Signed-off-by: Martin Storsjö <martin@martin.st>
2023-12-20 15:37:44 +02:00
Antonin Décimo
c7ae35680f winpthreads: Silence MSVC copyright message
Signed-off-by: Antonin Décimo <antonin@tarides.com>
Signed-off-by: Martin Storsjö <martin@martin.st>
2023-12-20 15:37:44 +02:00
Antonin Décimo
0f9248bc73 winpthreads: Add windres-rc, a wrapper for windres to rc
Signed-off-by: Antonin Décimo <antonin@tarides.com>
Signed-off-by: Martin Storsjö <martin@martin.st>
2023-12-20 15:37:44 +02:00
Antonin Décimo
2689852da4 winpthreads: AC_PROG_RANLIB is obsoleted by LT_INIT
Fixes a warning from libtoolize: 'AC_PROG_RANLIB' is rendered obsolete
by 'LT_INIT'.

Signed-off-by: Antonin Décimo <antonin@tarides.com>
Signed-off-by: Martin Storsjö <martin@martin.st>
2023-12-20 15:37:44 +02:00
Antonin Décimo
f4d7cd9c4c winpthreads: Small cleanups for clang
- remove superflous semicolons;
- avoid dead code warnings;
- avoid function redeclaration with an added 'dllexport' attribute.

Signed-off-by: Antonin Décimo <antonin@tarides.com>
Signed-off-by: Martin Storsjö <martin@martin.st>
2023-12-20 15:37:44 +02:00
Antonin Décimo
ab0572b5b7 winpthreads: Fix include style
MSVC warns [1] against relative path in include directives, and
semaphore.h is already in the include path.

    warning C4464: relative include path contains '..'

[1]: https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-4-c4464?view=msvc-170

windows.h is a system header and better placed at first, enclosed in
angle brackets.

Signed-off-by: Antonin Décimo <antonin@tarides.com>
Signed-off-by: Martin Storsjö <martin@martin.st>
2023-12-20 15:37:43 +02:00
Antonin Décimo
a70784c9b3 winpthreads: Prevent scoping issues and warnings in cleanup_push/pop
Interestingly, pthread_cleanup_push/pop are allowed by POSIX to be
implemented as macros opening and closing an lexical scope. By using
the well-known trick of do { ... } while (0) [1], we prevent potential
scoping issues in surrounding code when the macro is expanded.

Removing the comma operator in pthread_cleanup_pop prevents compiler
warnings against not using the return value of the expression, and a
against an empty statement.

[1]: https://gcc.gnu.org/onlinedocs/cpp/Swallowing-the-Semicolon.html

Signed-off-by: Antonin Décimo <antonin@tarides.com>
Signed-off-by: Martin Storsjö <martin@martin.st>
2023-12-20 15:37:43 +02:00
Antonin Décimo
f4eb6e17e1 winpthreads: Protect macros with do { ... } while (0) idiom
A classic idiom preventing all kinds of bad interactions with
surrounding code when the macro is expanded. Newer compilers may also
warn against empty statements of the form

    { expressions... };

where the ending ; is actually an empty statement.

https://gcc.gnu.org/onlinedocs/cpp/Swallowing-the-Semicolon.html

Signed-off-by: Antonin Décimo <antonin@tarides.com>
Signed-off-by: Martin Storsjö <martin@martin.st>
2023-12-20 15:37:43 +02:00
Antonin Décimo
929aa05205 winpthreads: Fix printf format specifiers
As DWORD is a typedef for unsigned long, we can use the %lu format
specifier. The %p format specifier requires (void *) pointers.

Signed-off-by: Antonin Décimo <antonin@tarides.com>
Signed-off-by: Martin Storsjö <martin@martin.st>
2023-12-20 15:37:43 +02:00
Antonin Décimo
b1a9061ff4 winpthreads: Fix thread-local storage callbacks on MSVC
- Force a reference to _tls_used to make the linker create the TLS
  directory if it's not already there.  (e.g. if __declspec(thread) is
  not used).
- Force a reference to __xl_f to prevent whole program optimization
  from discarding the variable.
- On x86, symbols are prefixed with an underscore.
- Mark the TLS callback as const.
- Prefer using #pragma section (over const_seg or data_seg) as it
  makes the code shorter and allows sharing code with GCC, and is
  portable to all architectures.
- Declare the variable as extern const to silence clang-cl warnings.

[1]: https://learn.microsoft.com/en-us/cpp/preprocessor/pragma-seg?view=msvc-170

Signed-off-by: Antonin Décimo <antonin@tarides.com>
Signed-off-by: Martin Storsjö <martin@martin.st>
2023-12-20 15:37:43 +02:00
Antonin Décimo
af65c1f7d3 winpthreads: PIMAGE_TLS_CALLBACK returns void, not BOOL
https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#tls-callback-functions

Signed-off-by: Antonin Décimo <antonin@tarides.com>
Signed-off-by: Martin Storsjö <martin@martin.st>
2023-12-20 15:37:43 +02:00
Antonin Décimo
b92de67cf1 winpthreads: Format error string with snprintf
Previous code was too complex and hard to understand, and snprintf
fits the job nicely.

Signed-off-by: Antonin Décimo <antonin@tarides.com>
Signed-off-by: Martin Storsjö <martin@martin.st>
2023-12-20 15:37:43 +02:00
Antonin Décimo
91d37a4ab7 winpthreads: Inline INIT_RWLOCK into its only callsite
Prevents a warning of `r` being shadowed, and makes the code clearer.

Signed-off-by: Antonin Décimo <antonin@tarides.com>
Signed-off-by: Martin Storsjö <martin@martin.st>
2023-12-20 15:37:43 +02:00
Antonin Décimo
d64ea219aa winpthreads: Use __assume(0) MSVC builtin for unreachable code
https://learn.microsoft.com/en-us/cpp/intrinsics/assume?view=msvc-170

Signed-off-by: Antonin Décimo <antonin@tarides.com>
Signed-off-by: Martin Storsjö <martin@martin.st>
2023-12-20 15:37:43 +02:00
Antonin Décimo
bff371691f winpthreads: Move likely/unlikely to misc.h, noop under MSVC
Signed-off-by: Antonin Décimo <antonin@tarides.com>
Signed-off-by: Martin Storsjö <martin@martin.st>
2023-12-20 15:37:43 +02:00
LIU Hao
3ebb92804e Regenerate autotools scripts with automake 1.16.5
This is done by running `WANT_AUTOMAKE=1.16 autoreconf -ifv` in the top
directory.

Signed-off-by: LIU Hao <lh_mouse@126.com>
2023-11-17 13:14:02 +08:00
LIU Hao
b6de4efae6 Run autoreconf -ifv in top directory
Reference: https://sourceforge.net/p/mingw-w64/mailman/message/51784428/
Signed-off-by: LIU Hao <lh_mouse@126.com>
2023-11-16 22:44:40 +08:00
Martin Storsjö
fa94a17c9c winpthreads: Check GetModuleHandle return value before calling GetProcAddress
In Windows Store builds with the winstorecompat library,
GetModuleHandle returns null, and GetProcAddress is not documented
to allow passing null.

Signed-off-by: Martin Storsjö <martin@martin.st>
2023-11-10 10:46:38 +02:00
Martin Storsjö
d6c07d9561 Revert commit f86c3e7bbd
This was pushed accidentally as part of an earlier branch, updating
files with a slightly different version of tools than what has been
used before.
2023-10-27 21:02:54 +03:00
Martin Storsjö
f86c3e7bbd autoreconf 2023-10-26 12:02:51 +03:00
Ozkan Sezer
b57612d46f winpthreads: change LoadLibrary calls to GetModuleHandle after cb7f42e. 2023-10-24 17:11:04 +03:00
Jonathan Schleifer
cb7f42e05b winpthreads: Make winpthreads work on Win98
Signed-off-by: LIU Hao <lh_mouse@126.com>
2023-10-23 22:35:47 +08:00
LIU Hao
d7877cbd2e winstorecompat: Regenerate Makefile.in
Signed-off-by: LIU Hao <lh_mouse@126.com>
2023-06-24 18:24:23 +08:00
Steve Lhomme
7aa6f9504b crt: use wincrypt API from windowsapp in Windows 10
The hidden API are found in windowsapp since the RS4/19H1 SDK. They are
also allowed by the WACK in api-ms-win-security-cryptoapi-l1-1-0.
That DLL has been on all Windows 10 versions [1].

It's better to use the real API than using CCryptography winrt API just for
these calls.

Crypto.c is kept in the old winstorecompat when targetting Windows 8.

Apps targetting UWP before 19H1 and using CryptGenRandom may not work
if api-ms-win-security-cryptoapi-l1-1-0.dll on older Windows doesn't
contain the entry.

[1] https://learn.microsoft.com/en-us/uwp/win32-and-com/win32-apis#apis-from-api-ms-win-security-cryptoapi-l1-1-0dll

Signed-off-by: LIU Hao <lh_mouse@126.com>
2023-06-24 18:20:47 +08:00
LIU Hao
d62236ff33 winstorecompat: Regenerate Makefile.in
Signed-off-by: LIU Hao <lh_mouse@126.com>
2023-06-05 23:27:33 +08:00
Steve Lhomme
b62f0faa91 winstorecompat: remove GetModuleHandle() from windowsappcompat
It's allowed now in Windows 10. Keep it forbidden in
winstorecompat (Win8).

Signed-off-by: LIU Hao <lh_mouse@126.com>
2023-06-05 22:31:18 +08:00
Steve Lhomme
b220602e1b winstorecompat: remove _beginthread() from windowsappcompat
It's allowed now in Windows 10. Keep it forbidden in
winstorecompat (Win8) as it's
forbidden there [1].

Win10 UWP builds will use the version in api-ms-win-crt-runtime-l1-1-0.dll
via windowsapp.

[1] https://learn.microsoft.com/en-us/cpp/cppcx/crt-functions-not-supported-in-universal-windows-platform-apps?view=msvc-160#windows-8x-store-apps-and-windows-phone-8x-apps

Signed-off-by: LIU Hao <lh_mouse@126.com>
2023-06-05 22:21:11 +08:00
LIU Hao
36d8079c8e winstorecompat: Regenerate configure and Makefile.in
Signed-off-by: LIU Hao <lh_mouse@126.com>
2023-05-28 16:42:18 +08:00