[libc-commits] [libc] [libc][semaphore] Add post and wait operations for internal semaphore (PR #198959)
Michael Jones via libc-commits
libc-commits at lists.llvm.org
Mon Jun 22 15:49:46 PDT 2026
================
@@ -9,30 +9,64 @@
#ifndef LLVM_LIBC_SRC_SEMAPHORE_LINUX_SEMAPHORE_H
#define LLVM_LIBC_SRC_SEMAPHORE_LINUX_SEMAPHORE_H
+#include "hdr/errno_macros.h"
+#include "hdr/time_macros.h"
+#include "hdr/types/clockid_t.h"
#include "hdr/types/mode_t.h"
+#include "hdr/types/struct_timespec.h"
#include "src/__support/CPP/atomic.h"
+#include "src/__support/CPP/limits.h"
#include "src/__support/common.h"
#include "src/__support/error_or.h"
+#include "src/__support/libc_assert.h"
#include "src/__support/threads/futex_utils.h"
+#include "src/__support/time/abs_timeout.h"
namespace LIBC_NAMESPACE_DECL {
+// Define SEM_VALUE_MAX as INT_MAX
+constexpr unsigned int SEM_VALUE_MAX =
+ static_cast<unsigned int>(cpp::numeric_limits<int>::max());
+
class Semaphore {
Futex value;
unsigned int canary;
+ // Whether the semaphore is shared between processes.
+ bool is_shared;
+
// A private constant canary used to detect use of uninitialized or
// destroyed semaphores. Chose "SEM1" in ASCII (0x53='S', 0x45='E',
// 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) {
+ while (trywait() != 0) {
+ auto wait_or = value.wait(/*expected=*/0, timeout, is_shared);
+ if (wait_or.has_value())
+ continue;
+
+ if (wait_or.error() == EAGAIN)
+ continue;
----------------
michaelrj-google wrote:
Combine
```suggestion
if (wait_or.has_value() || (wait_or.error() == EAGAIN))
continue;
```
https://github.com/llvm/llvm-project/pull/198959
More information about the libc-commits
mailing list