mmuetzel added a comment. In D126291#3556725 <https://reviews.llvm.org/D126291#3556725>, @mstorsjo wrote:
> This looks mostly reasonable, but I'd recommend using a windows critical > section instead of a mutex - a critical section doesn't invoke the kernel > when there's no contention of the lock. For that, you'd use > `CRITICAL_SECTION`, `InitializeCriticalSection(&cs);`, > `EnterCriticalSection(&cs);`, `LeaveCriticalSection(&cs);`, > `DeleteCriticalSection(&cs);`. Thanks for the feedback. Updated patch: From 511c2f7695fe667486b5c07fb024f4badec6b23a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=BCtzel?= <markus.muet...@gmx.de> Date: Fri, 3 Jun 2022 21:59:02 +0200 Subject: [PATCH] lock without <mutex> on Windows --- flang/runtime/lock.h | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/flang/runtime/lock.h b/flang/runtime/lock.h index 0abc1158c2c1..c3572c72d17d 100644 --- a/flang/runtime/lock.h +++ b/flang/runtime/lock.h @@ -15,23 +15,17 @@ // Avoid <mutex> if possible to avoid introduction of C++ runtime // library dependence. -#ifndef _WIN32 -#define USE_PTHREADS 1 +#ifdef _WIN32 +#include <windows.h> #else -#undef USE_PTHREADS -#endif - -#if USE_PTHREADS #include <pthread.h> -#else -#include <mutex> #endif namespace Fortran::runtime { class Lock { public: -#if USE_PTHREADS +#ifndef _WIN32 Lock() { pthread_mutex_init(&mutex_, nullptr); } ~Lock() { pthread_mutex_destroy(&mutex_); } void Take() { @@ -41,9 +35,11 @@ public: bool Try() { return pthread_mutex_trylock(&mutex_) == 0; } void Drop() { pthread_mutex_unlock(&mutex_); } #else - void Take() { mutex_.lock(); } - bool Try() { return mutex_.try_lock(); } - void Drop() { mutex_.unlock(); } + Lock() { InitializeCriticalSection(&cs_); } + ~Lock() { DeleteCriticalSection(&cs_); } + void Take() { EnterCriticalSection(&cs_); } + bool Try() { return TryEnterCriticalSection(&cs_); } + void Drop() { LeaveCriticalSection(&cs_); } #endif void CheckLocked(const Terminator &terminator) { @@ -54,10 +50,10 @@ public: } private: -#if USE_PTHREADS +#ifndef _WIN32 pthread_mutex_t mutex_{}; #else - std::mutex mutex_; + CRITICAL_SECTION cs_; #endif }; -- 2.35.3.windows.1 Also works for me with the simple Hello World on MinGW (MSYS2 CLANG64). CHANGES SINCE LAST ACTION https://reviews.llvm.org/D126291/new/ https://reviews.llvm.org/D126291 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits