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( + const_cast(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()); +}