From 99fe38a13a2d111fbb0b7848415d4ba3cab11437 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Markus=20M=C3=BCtzel?= Date: Fri, 10 Jun 2022 08:31:42 +0000 Subject: [PATCH] [flang][runtime] Remove dependency on C++ on Windows The implementation of the Lock class on Windows currently uses C++ mutexes. That introduces a dependency on the C++ runtime on that platform. Use a Windows CriticalSection instead of a std::mutex to avoid that dependency. This works for me with MinGW (tested in a CLANG64 environment of MSYS2). See also D126291. Differential Revision: https://reviews.llvm.org/D127316 --- flang/runtime/lock.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/flang/runtime/lock.h b/flang/runtime/lock.h index 0abc115..7af1ab8 100644 --- a/flang/runtime/lock.h +++ b/flang/runtime/lock.h @@ -23,6 +23,10 @@ #if USE_PTHREADS #include +#elif defined(_WIN32) +// Do not define macros for "min" and "max" +#define NOMINMAX +#include #else #include #endif @@ -40,6 +44,12 @@ public: } bool Try() { return pthread_mutex_trylock(&mutex_) == 0; } void Drop() { pthread_mutex_unlock(&mutex_); } +#elif defined(_WIN32) + Lock() { InitializeCriticalSection(&cs_); } + ~Lock() { DeleteCriticalSection(&cs_); } + void Take() { EnterCriticalSection(&cs_); } + bool Try() { return TryEnterCriticalSection(&cs_); } + void Drop() { LeaveCriticalSection(&cs_); } #else void Take() { mutex_.lock(); } bool Try() { return mutex_.try_lock(); } @@ -56,6 +66,8 @@ public: private: #if USE_PTHREADS pthread_mutex_t mutex_{}; +#elif defined(_WIN32) + CRITICAL_SECTION cs_; #else std::mutex mutex_; #endif -- 2.7.4