[libcxx-commits] [PATCH] D97539: [libcxx] Implement semaphores for windows

Martin Storsjö via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Fri Feb 26 06:13:20 PST 2021


mstorsjo updated this revision to Diff 326675.
mstorsjo edited the summary of this revision.
mstorsjo added a comment.

Added `WIN32_LEAN_AND_MEAN`, using `chrono::ceil`.


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

https://reviews.llvm.org/D97539

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


Index: libcxx/src/support/win32/thread_win32.cpp
===================================================================
--- libcxx/src/support/win32/thread_win32.cpp
+++ 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(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 @@
   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
Index: libcxx/include/__threading_support
===================================================================
--- libcxx/include/__threading_support
+++ libcxx/include/__threading_support
@@ -149,6 +149,9 @@
 
 // Semaphore
 typedef void* __libcpp_semaphore_t;
+#if defined(_LIBCPP_HAS_THREAD_API_WIN32)
+# define _LIBCPP_SEMAPHORE_MAX (numeric_limits<long>::max())
+#endif
 
 // Execute Once
 typedef void* __libcpp_exec_once_flag;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D97539.326675.patch
Type: text/x-patch
Size: 2322 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20210226/a5ae411e/attachment.bin>


More information about the libcxx-commits mailing list