[libc-commits] [libc] [libc][semaphore] Add unnamed semaphore implementation (PR #190851)
Schrodinger ZHU Yifan via libc-commits
libc-commits at lists.llvm.org
Tue Apr 7 14:32:18 PDT 2026
================
@@ -0,0 +1,72 @@
+//===-- Shared helpers for POSIX semaphores -------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SEMAPHORE_POSIX_SEMAPHORE_H
+#define LLVM_LIBC_SRC_SEMAPHORE_POSIX_SEMAPHORE_H
+
+#include "hdr/types/sem_t.h"
+#include "src/__support/CPP/atomic.h"
+#include "src/__support/common.h"
+#include "src/__support/threads/linux/futex_word.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace sem_utils {
+
+// 0x53 = S, 0x45 = E, 0x4D = M, 0x31 = 1
+// canary value: SEM1 in hex
+LIBC_INLINE_VAR constexpr unsigned int SEM_CANARY = 0x53454D31U;
+
+static_assert(sizeof(__futex_word) == sizeof(FutexWordType));
+static_assert(alignof(__futex_word) >= alignof(FutexWordType));
----------------
SchrodingerZhu wrote:
I would prefer create a high-level structure
```
struct Semaphore {
Futex value;
unsigned int canary;
};
static_assert(alignof(Semaphore) == sizeof(sem_t));
static_assert(sizeof(Semaphore) <= alignof(sem_t));
```
https://github.com/llvm/llvm-project/pull/190851
More information about the libc-commits
mailing list