[libc-commits] [libc] [libc][Semaphore] Add public sem_t type and entry (PR #213489)

via libc-commits libc-commits at lists.llvm.org
Sat Aug 1 13:30:30 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Pengxiang Huang (Pengxiang-Huang)

<details>
<summary>Changes</summary>

Implements #<!-- -->190847 

Add the public `sem_t` class that operates on the internal `Semaphore` class. Assisted-by: Claude Code Fable 5. 

---

Patch is 54.38 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/213489.diff


44 Files Affected:

- (modified) libc/config/linux/x86_64/entrypoints.txt (+13) 
- (modified) libc/config/linux/x86_64/headers.txt (+1) 
- (modified) libc/hdr/CMakeLists.txt (+9) 
- (added) libc/hdr/semaphore_macros.h (+22) 
- (modified) libc/hdr/types/CMakeLists.txt (+8) 
- (added) libc/hdr/types/sem_t.h (+22) 
- (modified) libc/include/CMakeLists.txt (+13) 
- (modified) libc/include/limits.yaml (+2) 
- (modified) libc/include/llvm-libc-macros/CMakeLists.txt (+6) 
- (modified) libc/include/llvm-libc-macros/limits-macros.h (+5) 
- (added) libc/include/llvm-libc-macros/semaphore-macros.h (+15) 
- (modified) libc/include/llvm-libc-types/CMakeLists.txt (+1) 
- (added) libc/include/llvm-libc-types/sem_t.h (+24) 
- (added) libc/include/semaphore.yaml (+86) 
- (modified) libc/src/semaphore/CMakeLists.txt (+77) 
- (modified) libc/src/semaphore/linux/CMakeLists.txt (+153-1) 
- (added) libc/src/semaphore/linux/sem_clockwait.cpp (+38) 
- (added) libc/src/semaphore/linux/sem_close.cpp (+34) 
- (added) libc/src/semaphore/linux/sem_destroy.cpp (+31) 
- (added) libc/src/semaphore/linux/sem_getvalue.cpp (+32) 
- (added) libc/src/semaphore/linux/sem_init.cpp (+33) 
- (added) libc/src/semaphore/linux/sem_open.cpp (+45) 
- (added) libc/src/semaphore/linux/sem_post.cpp (+34) 
- (added) libc/src/semaphore/linux/sem_timedwait.cpp (+37) 
- (added) libc/src/semaphore/linux/sem_trywait.cpp (+35) 
- (added) libc/src/semaphore/linux/sem_unlink.cpp (+26) 
- (added) libc/src/semaphore/linux/sem_wait.cpp (+34) 
- (modified) libc/src/semaphore/linux/semaphore.h (+10-5) 
- (added) libc/src/semaphore/sem_clockwait.h (+24) 
- (added) libc/src/semaphore/sem_close.h (+21) 
- (added) libc/src/semaphore/sem_destroy.h (+21) 
- (added) libc/src/semaphore/sem_getvalue.h (+21) 
- (added) libc/src/semaphore/sem_init.h (+21) 
- (added) libc/src/semaphore/sem_open.h (+21) 
- (added) libc/src/semaphore/sem_post.h (+21) 
- (added) libc/src/semaphore/sem_timedwait.h (+23) 
- (added) libc/src/semaphore/sem_trywait.h (+21) 
- (added) libc/src/semaphore/sem_unlink.h (+20) 
- (added) libc/src/semaphore/sem_wait.h (+21) 
- (modified) libc/test/src/semaphore/CMakeLists.txt (+50) 
- (modified) libc/test/src/semaphore/linux/CMakeLists.txt (+1-2) 
- (modified) libc/test/src/semaphore/linux/semaphore_test.cpp (+3-2) 
- (added) libc/test/src/semaphore/sem_open_test.cpp (+102) 
- (added) libc/test/src/semaphore/sem_test.cpp (+195) 


``````````diff
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index bf809e268f585..b39cc972200bf 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1352,6 +1352,19 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.sched.__sched_setcpuzero
     libc.src.sched.__sched_xorcpuset
 
+    # semaphore.h entrypoints
+    libc.src.semaphore.sem_clockwait
+    libc.src.semaphore.sem_close
+    libc.src.semaphore.sem_destroy
+    libc.src.semaphore.sem_getvalue
+    libc.src.semaphore.sem_init
+    libc.src.semaphore.sem_open
+    libc.src.semaphore.sem_post
+    libc.src.semaphore.sem_timedwait
+    libc.src.semaphore.sem_trywait
+    libc.src.semaphore.sem_unlink
+    libc.src.semaphore.sem_wait
+
     # setjmp.h entrypoints
     libc.src.setjmp.longjmp
     libc.src.setjmp.setjmp
diff --git a/libc/config/linux/x86_64/headers.txt b/libc/config/linux/x86_64/headers.txt
index b8751f7810b30..3aea6394c98cb 100644
--- a/libc/config/linux/x86_64/headers.txt
+++ b/libc/config/linux/x86_64/headers.txt
@@ -35,6 +35,7 @@ set(TARGET_PUBLIC_HEADERS
     libc.include.pwd
     libc.include.sched
     libc.include.search
+    libc.include.semaphore
     libc.include.setjmp
     libc.include.signal
     libc.include.spawn
diff --git a/libc/hdr/CMakeLists.txt b/libc/hdr/CMakeLists.txt
index a3eb7769e3c91..2eb684a7ee181 100644
--- a/libc/hdr/CMakeLists.txt
+++ b/libc/hdr/CMakeLists.txt
@@ -112,6 +112,15 @@ add_proxy_header_library(
     libc.include.llvm-libc-macros.sched_macros
 )
 
+add_proxy_header_library(
+  semaphore_macros
+  HDRS
+    semaphore_macros.h
+  FULL_BUILD_DEPENDS
+    libc.include.llvm-libc-macros.semaphore_macros
+    libc.include.semaphore
+)
+
 add_proxy_header_library(
   signal_macros
   HDRS
diff --git a/libc/hdr/semaphore_macros.h b/libc/hdr/semaphore_macros.h
new file mode 100644
index 0000000000000..88647dd971f11
--- /dev/null
+++ b/libc/hdr/semaphore_macros.h
@@ -0,0 +1,22 @@
+//===-- Definition of macros from semaphore.h -----------------------------===//
+//
+// 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_HDR_SEMAPHORE_MACROS_H
+#define LLVM_LIBC_HDR_SEMAPHORE_MACROS_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-macros/semaphore-macros.h"
+
+#else // Overlay mode
+
+#include <semaphore.h>
+
+#endif // LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_SEMAPHORE_MACROS_H
diff --git a/libc/hdr/types/CMakeLists.txt b/libc/hdr/types/CMakeLists.txt
index 8e4b2175cb7cd..e7f2aa21de4ed 100644
--- a/libc/hdr/types/CMakeLists.txt
+++ b/libc/hdr/types/CMakeLists.txt
@@ -341,6 +341,14 @@ add_proxy_header_library(
     libc.include.llvm-libc-types.pthread_barrier_t
 )
 
+add_proxy_header_library(
+  sem_t
+  HDRS
+    sem_t.h
+  FULL_BUILD_DEPENDS
+    libc.include.llvm-libc-types.sem_t
+)
+
 add_proxy_header_library(
   pthread_barrierattr_t
   HDRS
diff --git a/libc/hdr/types/sem_t.h b/libc/hdr/types/sem_t.h
new file mode 100644
index 0000000000000..f7071a0836d32
--- /dev/null
+++ b/libc/hdr/types/sem_t.h
@@ -0,0 +1,22 @@
+//===-- Proxy for sem_t ---------------------------------------------------===//
+//
+// 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_HDR_TYPES_SEM_T_H
+#define LLVM_LIBC_HDR_TYPES_SEM_T_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-types/sem_t.h"
+
+#else // Overlay mode
+
+#error "Cannot overlay sem_t"
+
+#endif // LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_TYPES_SEM_T_H
diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index 196490dcfdcc1..1a25df0990750 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -579,6 +579,19 @@ add_header_macro(
     .llvm-libc-types.struct_timespec
 )
 
+add_header_macro(
+  semaphore
+  ../libc/include/semaphore.yaml
+  semaphore.h
+  DEPENDS
+    .llvm_libc_common_h
+    .llvm-libc-macros.semaphore_macros
+    .llvm-libc-types.clockid_t
+    .llvm-libc-types.mode_t
+    .llvm-libc-types.sem_t
+    .llvm-libc-types.struct_timespec
+)
+
 add_header_macro(
   spawn
   ../libc/include/spawn.yaml
diff --git a/libc/include/limits.yaml b/libc/include/limits.yaml
index 1c951318fb14c..f14aa9abed9be 100644
--- a/libc/include/limits.yaml
+++ b/libc/include/limits.yaml
@@ -86,6 +86,8 @@ macros:
     macro_header: limits-macros.h
   - macro_name: PTHREAD_DESTRUCTOR_ITERATIONS
     macro_header: limits-macros.h
+  - macro_name: SEM_VALUE_MAX
+    macro_header: limits-macros.h
   - macro_name: SSIZE_MAX
     macro_header: limits-macros.h
 types: []
diff --git a/libc/include/llvm-libc-macros/CMakeLists.txt b/libc/include/llvm-libc-macros/CMakeLists.txt
index 45a1dbc851d2c..59272a603671f 100644
--- a/libc/include/llvm-libc-macros/CMakeLists.txt
+++ b/libc/include/llvm-libc-macros/CMakeLists.txt
@@ -201,6 +201,12 @@ add_macro_header(
     sched-macros.h
 )
 
+add_macro_header(
+  semaphore_macros
+  HDR
+    semaphore-macros.h
+)
+
 add_macro_header(
   signal_macros
   HDR
diff --git a/libc/include/llvm-libc-macros/limits-macros.h b/libc/include/llvm-libc-macros/limits-macros.h
index 024591eb5b40d..7a6b58e2d598c 100644
--- a/libc/include/llvm-libc-macros/limits-macros.h
+++ b/libc/include/llvm-libc-macros/limits-macros.h
@@ -256,6 +256,11 @@
 #define _POSIX_HOST_NAME_MAX 255
 #endif
 
+#ifndef SEM_VALUE_MAX
+/// The maximum value a semaphore may hold.
+#define SEM_VALUE_MAX INT_MAX
+#endif
+
 #ifdef __linux__
 
 #ifndef PATH_MAX
diff --git a/libc/include/llvm-libc-macros/semaphore-macros.h b/libc/include/llvm-libc-macros/semaphore-macros.h
new file mode 100644
index 0000000000000..d3a88209f8cde
--- /dev/null
+++ b/libc/include/llvm-libc-macros/semaphore-macros.h
@@ -0,0 +1,15 @@
+//===-- Definition of macros from semaphore.h -----------------------------===//
+//
+// 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_MACROS_SEMAPHORE_MACROS_H
+#define LLVM_LIBC_MACROS_SEMAPHORE_MACROS_H
+
+// The value returned by sem_open on failure.
+#define SEM_FAILED ((sem_t *)0)
+
+#endif // LLVM_LIBC_MACROS_SEMAPHORE_MACROS_H
diff --git a/libc/include/llvm-libc-types/CMakeLists.txt b/libc/include/llvm-libc-types/CMakeLists.txt
index 3286ebe54d9a2..7111a7b5ed18c 100644
--- a/libc/include/llvm-libc-types/CMakeLists.txt
+++ b/libc/include/llvm-libc-types/CMakeLists.txt
@@ -119,6 +119,7 @@ add_header(pthread_spinlock_t HDR pthread_spinlock_t.h DEPENDS .pid_t)
 add_header(pthread_t HDR pthread_t.h DEPENDS .__thread_type)
 add_header(pthread_id_np_t HDR pthread_id_np_t.h DEPENDS libc.include.llvm-libc-macros.stdint_macros)
 add_header(rlim_t HDR rlim_t.h)
+add_header(sem_t HDR sem_t.h DEPENDS .__futex_word)
 add_header(time_t HDR time_t_64.h DEST_HDR time_t.h)
 add_header(sighandler_t HDR sighandler_t.h)
 add_header(stack_t HDR stack_t.h DEPENDS .size_t)
diff --git a/libc/include/llvm-libc-types/sem_t.h b/libc/include/llvm-libc-types/sem_t.h
new file mode 100644
index 0000000000000..a32a5bf6fc289
--- /dev/null
+++ b/libc/include/llvm-libc-types/sem_t.h
@@ -0,0 +1,24 @@
+//===-- Definition of sem_t type ------------------------------------------===//
+//
+// 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_TYPES_SEM_T_H
+#define LLVM_LIBC_TYPES_SEM_T_H
+
+#include "__futex_word.h"
+
+typedef struct {
+  // The semaphore count. Waiters block on this word.
+  __futex_word __value;
+  // Set to a fixed value by sem_init/sem_open and cleared by sem_destroy, so
+  // that operations on an uninitialized or destroyed semaphore can be detected.
+  unsigned int __canary;
+  // Whether the semaphore is shared between processes.
+  unsigned int __is_shared : 1;
+} sem_t;
+
+#endif // LLVM_LIBC_TYPES_SEM_T_H
diff --git a/libc/include/semaphore.yaml b/libc/include/semaphore.yaml
new file mode 100644
index 0000000000000..4c51174359f1e
--- /dev/null
+++ b/libc/include/semaphore.yaml
@@ -0,0 +1,86 @@
+header: semaphore.h
+standards:
+  - posix
+macros:
+  - macro_name: SEM_FAILED
+    macro_header: semaphore-macros.h
+types:
+  - type_name: clockid_t
+  - type_name: mode_t
+  - type_name: sem_t
+  - type_name: struct_timespec
+functions:
+  - name: sem_clockwait
+    standards:
+      - posix
+    return_type: int
+    arguments:
+      - type: sem_t *__restrict
+      - type: clockid_t
+      - type: const struct timespec *__restrict
+  - name: sem_close
+    standards:
+      - posix
+    return_type: int
+    arguments:
+      - type: sem_t *
+  - name: sem_destroy
+    standards:
+      - posix
+    return_type: int
+    arguments:
+      - type: sem_t *
+  - name: sem_getvalue
+    standards:
+      - posix
+    return_type: int
+    arguments:
+      - type: sem_t *__restrict
+      - type: int *__restrict
+  - name: sem_init
+    standards:
+      - posix
+    return_type: int
+    arguments:
+      - type: sem_t *
+      - type: int
+      - type: unsigned
+  - name: sem_open
+    standards:
+      - posix
+    return_type: sem_t *
+    arguments:
+      - type: const char *
+      - type: int
+      - type: '...'
+  - name: sem_post
+    standards:
+      - posix
+    return_type: int
+    arguments:
+      - type: sem_t *
+  - name: sem_timedwait
+    standards:
+      - posix
+    return_type: int
+    arguments:
+      - type: sem_t *__restrict
+      - type: const struct timespec *__restrict
+  - name: sem_trywait
+    standards:
+      - posix
+    return_type: int
+    arguments:
+      - type: sem_t *
+  - name: sem_unlink
+    standards:
+      - posix
+    return_type: int
+    arguments:
+      - type: const char *
+  - name: sem_wait
+    standards:
+      - posix
+    return_type: int
+    arguments:
+      - type: sem_t *
diff --git a/libc/src/semaphore/CMakeLists.txt b/libc/src/semaphore/CMakeLists.txt
index cbe2c334fb571..9d3f6e4e96513 100644
--- a/libc/src/semaphore/CMakeLists.txt
+++ b/libc/src/semaphore/CMakeLists.txt
@@ -5,3 +5,80 @@ endif()
 if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
   add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
 endif()
+
+add_entrypoint_object(
+  sem_init
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.sem_init
+)
+
+add_entrypoint_object(
+  sem_destroy
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.sem_destroy
+)
+
+add_entrypoint_object(
+  sem_getvalue
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.sem_getvalue
+)
+
+add_entrypoint_object(
+  sem_open
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.sem_open
+)
+
+add_entrypoint_object(
+  sem_close
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.sem_close
+)
+
+add_entrypoint_object(
+  sem_unlink
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.sem_unlink
+)
+
+add_entrypoint_object(
+  sem_post
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.sem_post
+)
+
+add_entrypoint_object(
+  sem_wait
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.sem_wait
+)
+
+add_entrypoint_object(
+  sem_trywait
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.sem_trywait
+)
+
+add_entrypoint_object(
+  sem_timedwait
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.sem_timedwait
+)
+
+add_entrypoint_object(
+  sem_clockwait
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.sem_clockwait
+)
diff --git a/libc/src/semaphore/linux/CMakeLists.txt b/libc/src/semaphore/linux/CMakeLists.txt
index bd57b9ee5fc5d..cbc49d07fa0e9 100644
--- a/libc/src/semaphore/linux/CMakeLists.txt
+++ b/libc/src/semaphore/linux/CMakeLists.txt
@@ -4,12 +4,13 @@ add_header_library(
     semaphore.h
   DEPENDS
     libc.hdr.errno_macros
+    libc.hdr.limits_macros
     libc.hdr.time_macros
     libc.hdr.types.clockid_t
     libc.hdr.types.mode_t
     libc.hdr.types.struct_timespec
+    libc.include.llvm-libc-types.sem_t
     libc.src.__support.CPP.atomic
-    libc.src.__support.CPP.limits
     libc.src.__support.error_or
     libc.src.__support.libc_assert
     libc.src.__support.threads.futex_utils
@@ -42,3 +43,154 @@ add_object_library(
     libc.src.string.memory_utils.inline_memcpy
     libc.src.sys.mman.linux.shm_common
 )
+
+add_entrypoint_object(
+  sem_init
+  SRCS
+    sem_init.cpp
+  HDRS
+    ../sem_init.h
+  DEPENDS
+    .semaphore
+    libc.hdr.errno_macros
+    libc.hdr.limits_macros
+    libc.hdr.types.sem_t
+    libc.src.__support.CPP.new
+    libc.src.errno.errno
+)
+
+add_entrypoint_object(
+  sem_destroy
+  SRCS
+    sem_destroy.cpp
+  HDRS
+    ../sem_destroy.h
+  DEPENDS
+    .semaphore
+    libc.hdr.errno_macros
+    libc.hdr.types.sem_t
+    libc.src.errno.errno
+)
+
+add_entrypoint_object(
+  sem_getvalue
+  SRCS
+    sem_getvalue.cpp
+  HDRS
+    ../sem_getvalue.h
+  DEPENDS
+    .semaphore
+    libc.hdr.errno_macros
+    libc.hdr.types.sem_t
+    libc.src.errno.errno
+)
+
+add_entrypoint_object(
+  sem_open
+  SRCS
+    sem_open.cpp
+  HDRS
+    ../sem_open.h
+  DEPENDS
+    .named_semaphore
+    .semaphore
+    libc.hdr.fcntl_macros
+    libc.hdr.semaphore_macros
+    libc.hdr.types.mode_t
+    libc.hdr.types.sem_t
+    libc.src.errno.errno
+)
+
+add_entrypoint_object(
+  sem_close
+  SRCS
+    sem_close.cpp
+  HDRS
+    ../sem_close.h
+  DEPENDS
+    .named_semaphore
+    .semaphore
+    libc.hdr.errno_macros
+    libc.hdr.types.sem_t
+    libc.src.errno.errno
+)
+
+add_entrypoint_object(
+  sem_unlink
+  SRCS
+    sem_unlink.cpp
+  HDRS
+    ../sem_unlink.h
+  DEPENDS
+    .named_semaphore
+    .semaphore
+    libc.src.errno.errno
+)
+
+add_entrypoint_object(
+  sem_post
+  SRCS
+    sem_post.cpp
+  HDRS
+    ../sem_post.h
+  DEPENDS
+    .semaphore
+    libc.hdr.errno_macros
+    libc.hdr.types.sem_t
+    libc.src.errno.errno
+)
+
+add_entrypoint_object(
+  sem_wait
+  SRCS
+    sem_wait.cpp
+  HDRS
+    ../sem_wait.h
+  DEPENDS
+    .semaphore
+    libc.hdr.errno_macros
+    libc.hdr.types.sem_t
+    libc.src.errno.errno
+)
+
+add_entrypoint_object(
+  sem_trywait
+  SRCS
+    sem_trywait.cpp
+  HDRS
+    ../sem_trywait.h
+  DEPENDS
+    .semaphore
+    libc.hdr.errno_macros
+    libc.hdr.types.sem_t
+    libc.src.errno.errno
+)
+
+add_entrypoint_object(
+  sem_timedwait
+  SRCS
+    sem_timedwait.cpp
+  HDRS
+    ../sem_timedwait.h
+  DEPENDS
+    .semaphore
+    libc.hdr.errno_macros
+    libc.hdr.types.sem_t
+    libc.hdr.types.struct_timespec
+    libc.src.errno.errno
+)
+
+add_entrypoint_object(
+  sem_clockwait
+  SRCS
+    sem_clockwait.cpp
+  HDRS
+    ../sem_clockwait.h
+  DEPENDS
+    .semaphore
+    libc.hdr.errno_macros
+    libc.hdr.types.clockid_t
+    libc.hdr.types.sem_t
+    libc.hdr.types.struct_timespec
+    libc.src.errno.errno
+)
diff --git a/libc/src/semaphore/linux/sem_clockwait.cpp b/libc/src/semaphore/linux/sem_clockwait.cpp
new file mode 100644
index 0000000000000..75def64792668
--- /dev/null
+++ b/libc/src/semaphore/linux/sem_clockwait.cpp
@@ -0,0 +1,38 @@
+//===-- Linux implementation of sem_clockwait -----------------------------===//
+//
+// 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/sem_clockwait.h"
+
+#include "hdr/errno_macros.h"
+#include "hdr/types/clockid_t.h"
+#include "hdr/types/sem_t.h"
+#include "hdr/types/struct_timespec.h"
+#include "src/__support/common.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+#include "src/semaphore/linux/semaphore.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, sem_clockwait,
+                   (sem_t *__restrict sem, clockid_t clock_id,
+                    const struct timespec *__restrict abstime)) {
+  Semaphore *semaphore = reinterpret_cast<Semaphore *>(sem);
+  if (!semaphore->is_valid()) {
+    libc_errno = EINVAL;
+    return -1;
+  }
+
+  if (int err = semaphore->clockwait(clock_id, abstime)) {
+    libc_errno = err;
+    return -1;
+  }
+  return 0;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/semaphore/linux/sem_close.cpp b/libc/src/semaphore/linux/sem_close.cpp
new file mode 100644
index 0000000000000..0dc5a3f074cd6
--- /dev/null
+++ b/libc/src/semaphore/linux/sem_close.cpp
@@ -0,0 +1,34 @@
+//===-- Linux implementation of sem_close ---------------------------------===//
+//
+// 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/sem_close.h"
+
+#include "hdr/errno_macros.h"
+#include "hdr/types/sem_t.h"
+#include "src/__support/common.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+#include "src/semaphore/linux/semaphore.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, sem_close, (sem_t * sem)) {
+  Semaphore *semaphore = reinterpret_cast<Semaphore *>(sem);
+  if (!semaphore->is_valid()) {
+    libc_errno = EINVAL;
+    return -1;
+  }
+
+  if (int err = Semaphore::close(semaphore)) {
+    libc_errno = err;
+    return -1;
+  }
+  return 0;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/semaphore/linux/sem_destroy.cpp b/libc/src/semaphore/linux/sem_destroy.cpp
new file mode 100644
index 0000000000000..3b299020ce336
--- /dev/null
+++ b/libc/src/semaphore/linux/sem_destroy.cpp
@@ -0,0 +1,31 @@
+//===-- Linux implementation of sem_destroy -------------------------------===//
+//
+// 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/sem_destroy.h"
+
+#include "hdr/errno_macros.h"
+#include "hdr/types/sem_t.h"
+#include "src/__support/common.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+#include "src/semaphore/linux/semaphore.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, sem_destroy, (sem_t * sem)) {
+  Semaphore *semaphore = reinterpret_cast<Semaphore *>(sem);
+  if (!semaphore->is_valid()) {
+    libc_errno = EINVAL;
+    return -1;
+  }
+
+  semaphore->destroy();
+  return 0;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/semaphore/linux/sem_getvalue.cpp b/libc/src/semaphore/linux/sem_getvalue.cpp
new file mode 100644
index 0000000000000..0ac5aa7d7696d
--- /dev/null
+++ b/libc/src/semaphore/linux/sem_getvalue.cpp
@@ -0,0 +1,32 @@
+//===-- Linux implementation of sem_getvalue ------------------------------===//
+//
+// 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/sem_getvalue.h"
+
+#include "hdr/errno_macros.h"
+#include "hdr/types/sem_t.h"
+#include "src/__support/common.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+#include "src/semaphore/linux/semaphore.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, sem_getvalue,
+                   (sem_t *__restrict sem, int *__restrict sval)) {
+  Semaphore *semaphore = reinterpret_cast<Semaphore *>(sem);
+  if (!semaphore->is_valid()) {
+    libc_errno = EINVAL;
+    return -1;
+  }
+
+  *sval = semaphore->getvalue();
+  return 0;...
[truncated]

``````````

</details>


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


More information about the libc-commits mailing list