This is something that I have been working on for a while. Currently pthread_once creates a mutex on-the-fly to serialize concurrent threads accessing a pthread_once_t. This is not great for performance. For one, waiting threads are not released at the same time but in series. The number of necessary system calls is also increased.
Another shortcoming of the current code is that it doesn't use load-acquire and write-release, which makes the code incorrect on weakly-ordered architectures like ARM, AArch64. Technically we need compiler barriers also on x86, x64, but that's unlikely to be a problem in practical terms. The new implementation is based on WaitOnAddress / WakeByAddressAll, i.e. the futex implementation on Windows. These functions are available starting with Windows 8 -- for downlevel support we emulate WaitOnAddress / WakeByAddressAll using a table which contains manual-reset event objects. The performance improvements are quite worthwhile; here are a few numbers: | Implementation | Timings (lower is better) | | ---------------------- | ------------------------- | | Current implementation | 10 s | | WaitOnAddress | 30ms | | WaitOnAddress-emulated | 70ms | Sample located at https://gitlab.com/-/snippets/6062104 Best Regards, Luca Luca Bacci (6): winpthreads: Initialize TLS slot at startup winpthreads: Remove pthread_once_raw winpthreads: Add unordered table implementation winpthreads: Retrieve WaitOnAddress (and related functions) via GetProcAddress winpthreads: Rework pthread_once implementation winpthreads: Also test downlevel implementation of pthread_once mingw-w64-libraries/winpthreads/Makefile.am | 1 + mingw-w64-libraries/winpthreads/src/misc.c | 241 +++++++++- mingw-w64-libraries/winpthreads/src/misc.h | 40 ++ mingw-w64-libraries/winpthreads/src/once.c | 415 ++++++++++++++++++ mingw-w64-libraries/winpthreads/src/once.h | 31 ++ mingw-w64-libraries/winpthreads/src/thread.c | 178 ++------ .../winpthreads/tests/Makefile.am | 73 +-- .../winpthreads/tests/pthread_once/once1.c | 8 +- .../winpthreads/tests/pthread_once/once2.c | 8 +- .../winpthreads/tests/pthread_once/once3.c | 6 + .../winpthreads/tests/pthread_once/once4.c | 8 +- 11 files changed, 805 insertions(+), 204 deletions(-) create mode 100644 mingw-w64-libraries/winpthreads/src/once.c create mode 100644 mingw-w64-libraries/winpthreads/src/once.h -- 2.55.0.windows.5 _______________________________________________ Mingw-w64-public mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/mingw-w64-public
