On Wednesday 24 September 2025 21:24:41 LIU Hao wrote:
> 在 2025-9-24 03:53, Pali Rohár 写道:
> > 32-bit x86 kernel32.dll provides 6 Interlocked functions:
> > InterlockedDecrement@4
> > InterlockedExchange@8
> > InterlockedIncrement@4
> > InterlockedCompareExchange@12
> > InterlockedExchangeAdd@8
> > InterlockedCompareExchange64@20
> > 
> > WinSDK provides functions for these symbol in kernel32.lib import library
> > as direct reference of import symbols.
> > 
> > These 6 stdcall functions are currently missing in the 32-bit x86 mingw-w64
> > libkernel32.a library. mingw-w64 libkernel32.a library provides functions
> > with same functionality but prefixed by underscore and with cdec calling
> > convention. They are implemened as wrappers around gcc __sync_ builtin
> > functions.
> > 
> > So provide missing stdcall functions in 32-bit x86 libkernel32.a library
> > via intrin.h and update corresponding comments in lib32/kernel32.def.
> 
> Using MSVC to compile for x86-32, `InterlockedExchange` is defined as a
> macro to `_InterlockedExchange`; the former is not declared, only the latter
> is, but with `__cdecl` calling convention (because it's an intrinsic?).
> Hence, not only is it not callable in a normal setup, it also causes an
> error if someone declares `LONG __stdcall InterlockedExchange(volatile
> LONG*, LONG);` themself, due to inconsistency about the calling convention.

Without including windows.h it is possible to do declare
InterlockedExchange and call it. See: https://godbolt.org/z/78qKcMKox

long __stdcall InterlockedExchange(volatile long*, long);
long test(volatile long* arg1, long arg2) { return InterlockedExchange(arg1, 
arg2); }


And it is possible also with windows.h by undefining macro
InterlockedExchange. See: https://godbolt.org/z/bKboTEoYT

#include <windows.h>
#undef InterlockedExchange
LONG WINAPI InterlockedExchange(volatile LONG*, LONG);
LONG test(volatile LONG* arg1, LONG arg2) { return InterlockedExchange(arg1, 
arg2); }

> In addition, is there a reason to re-implement them, instead of removing 
> `DATA` from the DEF?

Well, I do not know. I saw that all intrinsics are defined in source
code instead of redirection to kernel32.dll implementation. So choose
same strategy, define in in the mingw-w64 source code.

I can imagine that LTO compilations (now or in future) could be able to
inline this call, but that is just a speculation.

Maybe one thing, which could be interested:
API of InterlockedIncrement and InterlockedDecrement in kernel32.dll was
changed in past, previously in past it returned only sign of the
original value, now it returns the full original value.


_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to