[libc-commits] [libc] eb6075e - [libc][semaphore] Add internal unnamed semaphore implementation (#190851)

via libc-commits libc-commits at lists.llvm.org
Thu Apr 9 16:47:54 PDT 2026


Author: Pengxiang Huang
Date: 2026-04-09T16:47:49-07:00
New Revision: eb6075e3be0a40301f008b0b3640ed125b66929f

URL: https://github.com/llvm/llvm-project/commit/eb6075e3be0a40301f008b0b3640ed125b66929f
DIFF: https://github.com/llvm/llvm-project/commit/eb6075e3be0a40301f008b0b3640ed125b66929f.diff

LOG: [libc][semaphore] Add internal unnamed semaphore implementation (#190851)

Implements the first part for #190847

Add internal unnamed semaphore lifetime support, particularly:

`sem_init`:
https://pubs.opengroup.org/onlinepubs/9799919799/functions/sem_init.html#
`sem_destroy`:
https://pubs.opengroup.org/onlinepubs/9799919799/functions/sem_destroy.html#
`sem_getvalue`:
https://pubs.opengroup.org/onlinepubs/9799919799/functions/sem_getvalue.html#

Added: 
    libc/src/semaphore/CMakeLists.txt
    libc/src/semaphore/posix_semaphore.h
    libc/test/src/semaphore/CMakeLists.txt
    libc/test/src/semaphore/semaphore_test.cpp

Modified: 
    libc/src/CMakeLists.txt
    libc/test/src/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/libc/src/CMakeLists.txt b/libc/src/CMakeLists.txt
index 8a0acccaed708..e4b0de3d5c75d 100644
--- a/libc/src/CMakeLists.txt
+++ b/libc/src/CMakeLists.txt
@@ -25,6 +25,7 @@ if(${LIBC_TARGET_OS} STREQUAL "linux")
   add_subdirectory(poll)
   add_subdirectory(pthread)
   add_subdirectory(sched)
+  add_subdirectory(semaphore)
   add_subdirectory(sys)
   add_subdirectory(termios)
 endif()

diff  --git a/libc/src/semaphore/CMakeLists.txt b/libc/src/semaphore/CMakeLists.txt
new file mode 100644
index 0000000000000..ac73eb8e87b56
--- /dev/null
+++ b/libc/src/semaphore/CMakeLists.txt
@@ -0,0 +1,8 @@
+add_header_library(
+  posix_semaphore
+  HDRS
+    posix_semaphore.h
+  DEPENDS
+    libc.src.__support.CPP.atomic
+    libc.src.__support.threads.linux.futex_utils
+)

diff  --git a/libc/src/semaphore/posix_semaphore.h b/libc/src/semaphore/posix_semaphore.h
new file mode 100644
index 0000000000000..7b14d41165a96
--- /dev/null
+++ b/libc/src/semaphore/posix_semaphore.h
@@ -0,0 +1,59 @@
+//===-- Internal Semaphore implementation 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 "src/__support/CPP/atomic.h"
+#include "src/__support/common.h"
+#include "src/__support/threads/linux/futex_utils.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+class Semaphore {
+  Futex value;
+  unsigned int canary;
+
+  // 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:
+  // 1. Add named semaphore support: sem_open, sem_close, sem_unlink
+  // 2. Add the posting and waiting operations: sem_post, sem_wait,
+  //    sem_trywait, sem_timedwait, sem_clockwait.
+
+  LIBC_INLINE constexpr Semaphore(unsigned int value)
+      : value(value), canary(SEM_CANARY) {}
+
+  // Sanity check to detect use of uninitialized or destroyed semaphores.
+  LIBC_INLINE bool is_valid() const { return canary == SEM_CANARY; }
+
+  LIBC_INLINE void destroy() {
+    // Destroying a semaphore while threads are blocked on it is undefined
+    // behavior. Similarly, using a destroyed semaphore is undefined.
+    // Therefore no concurrency safe destruction is required here,
+    // RELAXED memory ordering is sufficient.
+    value.store(0, cpp::MemoryOrder::RELAXED);
+    canary = 0;
+  }
+
+  LIBC_INLINE int getvalue() const {
+    // get value is informational, not a synchronization op.
+    // RELAXED ordering is enough.
+    // TODO: handle the case where the semaphore is locked.
+    return static_cast<int>(
+        const_cast<Futex &>(value).load(cpp::MemoryOrder::RELAXED));
+  }
+};
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_SEMAPHORE_POSIX_SEMAPHORE_H

diff  --git a/libc/test/src/CMakeLists.txt b/libc/test/src/CMakeLists.txt
index 5e61c739c066a..236bee337d525 100644
--- a/libc/test/src/CMakeLists.txt
+++ b/libc/test/src/CMakeLists.txt
@@ -106,4 +106,5 @@ add_subdirectory(spawn)
 
 if(${LIBC_TARGET_OS} STREQUAL "linux")
   add_subdirectory(pthread)
+  add_subdirectory(semaphore)
 endif()

diff  --git a/libc/test/src/semaphore/CMakeLists.txt b/libc/test/src/semaphore/CMakeLists.txt
new file mode 100644
index 0000000000000..0c458db5d3669
--- /dev/null
+++ b/libc/test/src/semaphore/CMakeLists.txt
@@ -0,0 +1,11 @@
+add_custom_target(libc_semaphore_unittests)
+
+add_libc_unittest(
+  semaphore_test
+  SUITE
+    libc_semaphore_unittests
+  SRCS
+    semaphore_test.cpp
+  DEPENDS
+    libc.src.semaphore.posix_semaphore
+)

diff  --git a/libc/test/src/semaphore/semaphore_test.cpp b/libc/test/src/semaphore/semaphore_test.cpp
new file mode 100644
index 0000000000000..241078ef5a09c
--- /dev/null
+++ b/libc/test/src/semaphore/semaphore_test.cpp
@@ -0,0 +1,25 @@
+//===-- Unittests for the internal Semaphore class ------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/semaphore/posix_semaphore.h"
+#include "test/UnitTest/Test.h"
+
+using LIBC_NAMESPACE::Semaphore;
+
+TEST(LlvmLibcSemaphoreTest, InitAndGetValue) {
+  Semaphore sem(3);
+  ASSERT_TRUE(sem.is_valid());
+  ASSERT_EQ(sem.getvalue(), 3);
+}
+
+TEST(LlvmLibcSemaphoreTest, Destroy) {
+  Semaphore sem(5);
+  ASSERT_TRUE(sem.is_valid());
+  sem.destroy();
+  ASSERT_FALSE(sem.is_valid());
+}


        


More information about the libc-commits mailing list