[libcxx-commits] [libcxx] 1773eec - [libcxx] Implement semaphores for windows

Martin Storsjö via libcxx-commits libcxx-commits at lists.llvm.org
Fri Mar 5 00:49:17 PST 2021


Author: Martin Storsjö
Date: 2021-03-05T10:48:59+02:00
New Revision: 1773eec6928f4e37b377e23b84d7a2a07d0d1d0d

URL: https://github.com/llvm/llvm-project/commit/1773eec6928f4e37b377e23b84d7a2a07d0d1d0d
DIFF: https://github.com/llvm/llvm-project/commit/1773eec6928f4e37b377e23b84d7a2a07d0d1d0d.diff

LOG: [libcxx] Implement semaphores for windows

Also add WIN32_LEAN_AND_MEAN before including windows.h, for consistency
with other sources.

Differential Revision: https://reviews.llvm.org/D97539

Added: 
    

Modified: 
    libcxx/include/__threading_support
    libcxx/src/support/win32/thread_win32.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/include/__threading_support b/libcxx/include/__threading_support
index 46438b72f9fc..430ec5419118 100644
--- a/libcxx/include/__threading_support
+++ b/libcxx/include/__threading_support
@@ -15,6 +15,7 @@
 #include <chrono>
 #include <iosfwd>
 #include <errno.h>
+#include <limits>
 
 #ifdef __MVS__
 # include <__support/ibm/nanosleep.h>
@@ -149,6 +150,9 @@ typedef void* __libcpp_condvar_t;
 
 // Semaphore
 typedef void* __libcpp_semaphore_t;
+#if defined(_LIBCPP_HAS_THREAD_API_WIN32)
+# define _LIBCPP_SEMAPHORE_MAX (::std::numeric_limits<long>::max())
+#endif
 
 // Execute Once
 typedef void* __libcpp_exec_once_flag;

diff  --git a/libcxx/src/support/win32/thread_win32.cpp b/libcxx/src/support/win32/thread_win32.cpp
index 35c4c871457d..2b1aa5635ac7 100644
--- a/libcxx/src/support/win32/thread_win32.cpp
+++ b/libcxx/src/support/win32/thread_win32.cpp
@@ -8,6 +8,8 @@
 //===----------------------------------------------------------------------===//
 
 #include <__threading_support>
+#define NOMINMAX
+#define WIN32_LEAN_AND_MEAN
 #include <windows.h>
 #include <process.h>
 #include <fibersapi.h>
@@ -37,6 +39,9 @@ static_assert(alignof(__libcpp_thread_t) == alignof(HANDLE), "");
 static_assert(sizeof(__libcpp_tls_key) == sizeof(DWORD), "");
 static_assert(alignof(__libcpp_tls_key) == alignof(DWORD), "");
 
+static_assert(sizeof(__libcpp_semaphore_t) == sizeof(HANDLE), "");
+static_assert(alignof(__libcpp_semaphore_t) == alignof(HANDLE), "");
+
 // Mutex
 int __libcpp_recursive_mutex_init(__libcpp_recursive_mutex_t *__m)
 {
@@ -272,4 +277,37 @@ int __libcpp_tls_set(__libcpp_tls_key __key, void *__p)
   return 0;
 }
 
+// Semaphores
+bool __libcpp_semaphore_init(__libcpp_semaphore_t* __sem, int __init)
+{
+  *(PHANDLE)__sem = CreateSemaphoreEx(nullptr, __init, _LIBCPP_SEMAPHORE_MAX,
+                                      nullptr, 0, SEMAPHORE_ALL_ACCESS);
+  return *__sem != nullptr;
+}
+
+bool __libcpp_semaphore_destroy(__libcpp_semaphore_t* __sem)
+{
+  CloseHandle(*(PHANDLE)__sem);
+  return true;
+}
+
+bool __libcpp_semaphore_post(__libcpp_semaphore_t* __sem)
+{
+  return ReleaseSemaphore(*(PHANDLE)__sem, 1, nullptr);
+}
+
+bool __libcpp_semaphore_wait(__libcpp_semaphore_t* __sem)
+{
+  return WaitForSingleObjectEx(*(PHANDLE)__sem, INFINITE, false) ==
+         WAIT_OBJECT_0;
+}
+
+bool __libcpp_semaphore_wait_timed(__libcpp_semaphore_t* __sem,
+                                   chrono::nanoseconds const& __ns)
+{
+  chrono::milliseconds __ms = std::chrono::ceil<chrono::milliseconds>(__ns);
+  return WaitForSingleObjectEx(*(PHANDLE)__sem, __ms.count(), false) ==
+         WAIT_OBJECT_0;
+}
+
 _LIBCPP_END_NAMESPACE_STD


        


More information about the libcxx-commits mailing list