[flang-commits] [flang] [flang] Remove C++ runtime dependency from Sleep extension (PR #84911)

David Truby via flang-commits flang-commits at lists.llvm.org
Tue Apr 30 08:46:59 PDT 2024


https://github.com/DavidTruby updated https://github.com/llvm/llvm-project/pull/84911

>From 4e988fa105539a419af469741cd86b57e8ef59ec Mon Sep 17 00:00:00 2001
From: David Truby <david.truby at arm.com>
Date: Tue, 12 Mar 2024 13:34:58 +0000
Subject: [PATCH 1/4] [flang] Remove C++ runtime dependency from Sleep
 extension

The Sleep extension currently has a potential dependency on the C++ runtime. I
run into this dependency using libc++ on Linux. This patch uses the POSIX
`sleep` function or the Windows `Sleep` function instead to avoid this
dependency.
---
 flang/runtime/extensions.cpp | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/flang/runtime/extensions.cpp b/flang/runtime/extensions.cpp
index 3ac98000335d7d..fb11e792f613f5 100644
--- a/flang/runtime/extensions.cpp
+++ b/flang/runtime/extensions.cpp
@@ -135,7 +135,11 @@ void RTNAME(Sleep)(std::int64_t seconds) {
   if (seconds < 1) {
     return;
   }
-  std::this_thread::sleep_for(std::chrono::seconds(seconds));
+#if _WIN32
+  Sleep(seconds * 1000);
+#else
+  sleep(seconds);
+#endif
 }
 
 } // namespace Fortran::runtime

>From 2856f66c09d8a5464faa8e84191eb255774bbf61 Mon Sep 17 00:00:00 2001
From: David Truby <david.truby at arm.com>
Date: Tue, 12 Mar 2024 14:47:16 +0000
Subject: [PATCH 2/4] Add missing include for Win32

---
 flang/runtime/extensions.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/flang/runtime/extensions.cpp b/flang/runtime/extensions.cpp
index fb11e792f613f5..2b4f9b30ebb170 100644
--- a/flang/runtime/extensions.cpp
+++ b/flang/runtime/extensions.cpp
@@ -22,6 +22,8 @@
 #include <thread>
 
 #ifdef _WIN32
+#include <synchapi.h>
+
 inline void CtimeBuffer(char *buffer, size_t bufsize, const time_t cur_time,
     Fortran::runtime::Terminator terminator) {
   int error{ctime_s(buffer, bufsize, &cur_time)};

>From 3cc20519472e8daeaa808a27c550d12e5891c7d1 Mon Sep 17 00:00:00 2001
From: David Truby <david.truby at arm.com>
Date: Wed, 13 Mar 2024 16:10:57 +0000
Subject: [PATCH 3/4] Include windows.h

---
 flang/runtime/extensions.cpp | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/flang/runtime/extensions.cpp b/flang/runtime/extensions.cpp
index 2b4f9b30ebb170..72591b0459e924 100644
--- a/flang/runtime/extensions.cpp
+++ b/flang/runtime/extensions.cpp
@@ -22,6 +22,9 @@
 #include <thread>
 
 #ifdef _WIN32
+#define WIN32_LEAN_AND_MEAN
+#define NOMINMAX
+#include <windows.h>
 #include <synchapi.h>
 
 inline void CtimeBuffer(char *buffer, size_t bufsize, const time_t cur_time,

>From be8a11b054b2e2e8454ce36cf78f9bb06583c788 Mon Sep 17 00:00:00 2001
From: David Truby <david.truby at arm.com>
Date: Tue, 30 Apr 2024 15:46:19 +0000
Subject: [PATCH 4/4] clang-format

---
 flang/runtime/extensions.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/flang/runtime/extensions.cpp b/flang/runtime/extensions.cpp
index 72591b0459e924..ca12dc7551136c 100644
--- a/flang/runtime/extensions.cpp
+++ b/flang/runtime/extensions.cpp
@@ -24,8 +24,8 @@
 #ifdef _WIN32
 #define WIN32_LEAN_AND_MEAN
 #define NOMINMAX
-#include <windows.h>
 #include <synchapi.h>
+#include <windows.h>
 
 inline void CtimeBuffer(char *buffer, size_t bufsize, const time_t cur_time,
     Fortran::runtime::Terminator terminator) {



More information about the flang-commits mailing list