Skip to content

Commit

Permalink
[flang][runtime] Remove dependency on C++ <mutex> on Windows
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mmuetzel authored and rovka committed Jun 10, 2022
1 parent 4c38953 commit 99fe38a
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions flang/runtime/lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@

#if USE_PTHREADS
#include <pthread.h>
#elif defined(_WIN32)
// Do not define macros for "min" and "max"
#define NOMINMAX
#include <windows.h>
#else
#include <mutex>
#endif
Expand All @@ -40,6 +44,12 @@ class Lock {
}
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(); }
Expand All @@ -56,6 +66,8 @@ class Lock {
private:
#if USE_PTHREADS
pthread_mutex_t mutex_{};
#elif defined(_WIN32)
CRITICAL_SECTION cs_;
#else
std::mutex mutex_;
#endif
Expand Down

0 comments on commit 99fe38a

Please sign in to comment.