[PATCH] D126291: [flang][Driver] Update link job on windows

Markus Mützel via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jun 3 10:24:58 PDT 2022


mmuetzel added a comment.

With this additional change, I no longer need the `-lc++` flag:

  From 62920169c43de27569ecae9ba0ec0fe4ec01633a Mon Sep 17 00:00:00 2001
  From: =?UTF-8?q?Markus=20M=C3=BCtzel?= <markus.muetzel at gmx.de>
  Date: Fri, 3 Jun 2022 19:17:34 +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..bd8c1ca0e747 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() { mutex_=CreateMutex(nullptr, FALSE, nullptr); }
  +  ~Lock() { CloseHandle(mutex_); }
  +  void Take() { WaitForSingleObject(mutex_, INFINITE); }
  +  bool Try() { return WaitForSingleObject(mutex_, 0); }
  +  void Drop() { ReleaseMutex(mutex_); }
   #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_;
  +  HANDLE mutex_;
   #endif
   };
   
  -- 
  2.35.3.windows.1

The previous logic was: Use <mutex> on Windows and pthread everywhere else. The new logic is: Use the WinAPI (CreateMutex and Co) on Windows and pthread everywhere else.
Someone should check if that change is sane. But it seems to work for me (with the "Hello World" example).


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

https://reviews.llvm.org/D126291



More information about the cfe-commits mailing list