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

Schrodinger ZHU Yifan via libc-commits libc-commits at lists.llvm.org
Wed Jun 3 20:00:59 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);
----------------
SchrodingerZhu wrote:

This is not exactly precise AFAIK.

1. shared mmap futex call has higher overhead as it needs to translate the address to a global manner;

2. shared and private addressing mode in futex are separated; e.g. a private futex wake cannot wake up a shared waiter; vice versa.

The posix interface attribute should align with the Linux syscall flag in this case.

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


More information about the libc-commits mailing list