[flang-commits] [PATCH] D127316: [flang][runtime] Remove dependency on C++ <mutex> on Windows

Markus Mützel via Phabricator via flang-commits flang-commits at lists.llvm.org
Wed Jun 8 14:35:59 PDT 2022


mmuetzel updated this revision to Diff 435340.
mmuetzel added a comment.

Leave in (unreachable) code that uses `std::mutex`.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D127316/new/

https://reviews.llvm.org/D127316

Files:
  flang/runtime/lock.h


Index: flang/runtime/lock.h
===================================================================
--- flang/runtime/lock.h
+++ flang/runtime/lock.h
@@ -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
@@ -40,6 +44,12 @@
   }
   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 @@
 private:
 #if USE_PTHREADS
   pthread_mutex_t mutex_{};
+#elif defined(_WIN32)
+  CRITICAL_SECTION cs_;
 #else
   std::mutex mutex_;
 #endif


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D127316.435340.patch
Type: text/x-patch
Size: 993 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20220608/9972c792/attachment-0001.bin>


More information about the flang-commits mailing list