[libc-commits] [libc] [libc][semaphore] Add post and wait operations for internal semaphore (PR #198959)

Pengxiang Huang via libc-commits libc-commits at lists.llvm.org
Wed Jun 3 19:50:30 PDT 2026


================
@@ -26,11 +36,24 @@ class Semaphore {
   // 0x4D='M', 0x31='1').
   static constexpr unsigned int SEM_CANARY = 0x53454D31U;
 
-public:
-  // TODO:
-  // Add the posting and waiting operations: sem_post, sem_wait,
-  //    sem_trywait, sem_timedwait, sem_clockwait.
+  // Helper function for timed blocking wait, wait for a required
+  // absolute timeout.
+  LIBC_INLINE int wait_until(internal::AbsTimeout timeout) {
+    for (;;) {
+      if (trywait() == 0)
+        return 0;
+
+      auto wait_or = value.wait(/*expected=*/0, timeout, /*is_shared=*/true);
----------------
Pengxiang-Huang wrote:

`is_shared` is used for the kernel to compute the futex key for identifying the sleepers/waker. The kernel can still compute the right key in the private mode, but with the unnecessary page table walking overhead. It's easier to avoid writing additional control flow handling, but I also agree it can be confusing to understand as you pointed out in the posix description. 

https://github.com/llvm/llvm-project/pull/198959


More information about the libc-commits mailing list