[libc-commits] [libc] 06947b9 - [libc][POSIX][pthreads] implement pthread_condattr_t functions (#88987)
via libc-commits
libc-commits at lists.llvm.org
Wed Apr 17 09:31:34 PDT 2024
Author: Nick Desaulniers
Date: 2024-04-17T09:31:29-07:00
New Revision: 06947b9f8d258fe66fc69f1e7c0197cb621da3a5
URL: https://github.com/llvm/llvm-project/commit/06947b9f8d258fe66fc69f1e7c0197cb621da3a5
DIFF: https://github.com/llvm/llvm-project/commit/06947b9f8d258fe66fc69f1e7c0197cb621da3a5.diff
LOG: [libc][POSIX][pthreads] implement pthread_condattr_t functions (#88987)
Implement:
- pthread_condattr_destroy
- pthread_condattr_getclock
- pthread_condattr_getpshared
- pthread_condattr_init
- pthread_condattr_setclock
- pthread_condattr_setpshared
Fixes: #88581
Added:
libc/include/llvm-libc-types/pthread_condattr_t.h
libc/src/pthread/pthread_condattr_destroy.cpp
libc/src/pthread/pthread_condattr_destroy.h
libc/src/pthread/pthread_condattr_getclock.cpp
libc/src/pthread/pthread_condattr_getclock.h
libc/src/pthread/pthread_condattr_getpshared.cpp
libc/src/pthread/pthread_condattr_getpshared.h
libc/src/pthread/pthread_condattr_init.cpp
libc/src/pthread/pthread_condattr_init.h
libc/src/pthread/pthread_condattr_setclock.cpp
libc/src/pthread/pthread_condattr_setclock.h
libc/src/pthread/pthread_condattr_setpshared.cpp
libc/src/pthread/pthread_condattr_setpshared.h
libc/test/src/pthread/pthread_condattr_test.cpp
Modified:
libc/config/linux/api.td
libc/config/linux/x86_64/entrypoints.txt
libc/include/CMakeLists.txt
libc/include/llvm-libc-types/CMakeLists.txt
libc/include/pthread.h.def
libc/spec/posix.td
libc/src/pthread/CMakeLists.txt
libc/test/src/pthread/CMakeLists.txt
Removed:
################################################################################
diff --git a/libc/config/linux/api.td b/libc/config/linux/api.td
index 9964971f191b75..5fb92a9c299cc3 100644
--- a/libc/config/linux/api.td
+++ b/libc/config/linux/api.td
@@ -175,6 +175,7 @@ def PThreadAPI : PublicAPI<"pthread.h"> {
"__pthread_start_t",
"__pthread_tss_dtor_t",
"pthread_attr_t",
+ "pthread_condattr_t",
"pthread_mutex_t",
"pthread_mutexattr_t",
"pthread_t",
@@ -241,10 +242,30 @@ def SysSendfileAPI : PublicAPI<"sys/sendfile.h"> {
}
def SysTypesAPI : PublicAPI<"sys/types.h"> {
- let Types = ["blkcnt_t", "blksize_t", "clockid_t", "dev_t", "gid_t", "ino_t",
- "mode_t", "nlink_t", "off_t", "pid_t", "pthread_attr_t", "pthread_key_t",
- "pthread_mutex_t", "pthread_mutexattr_t", "pthread_once_t", "pthread_t",
- "size_t", "ssize_t", "suseconds_t", "time_t", "uid_t"];
+ let Types = [
+ "blkcnt_t",
+ "blksize_t",
+ "clockid_t",
+ "dev_t",
+ "gid_t",
+ "ino_t",
+ "mode_t",
+ "nlink_t",
+ "off_t",
+ "pid_t",
+ "pthread_attr_t",
+ "pthread_condattr_t",
+ "pthread_key_t",
+ "pthread_mutex_t",
+ "pthread_mutexattr_t",
+ "pthread_once_t",
+ "pthread_t",
+ "size_t",
+ "ssize_t",
+ "suseconds_t",
+ "time_t",
+ "uid_t"
+ ];
}
def SysUtsNameAPI : PublicAPI<"sys/utsname.h"> {
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 70f130a4399a36..2d8136536b218b 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -639,6 +639,12 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.pthread.pthread_attr_setguardsize
libc.src.pthread.pthread_attr_setstack
libc.src.pthread.pthread_attr_setstacksize
+ libc.src.pthread.pthread_condattr_destroy
+ libc.src.pthread.pthread_condattr_getclock
+ libc.src.pthread.pthread_condattr_getpshared
+ libc.src.pthread.pthread_condattr_init
+ libc.src.pthread.pthread_condattr_setclock
+ libc.src.pthread.pthread_condattr_setpshared
libc.src.pthread.pthread_create
libc.src.pthread.pthread_detach
libc.src.pthread.pthread_equal
diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index b85366c8deafe0..f5ba2791af3fb8 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -321,6 +321,7 @@ add_gen_header(
.llvm-libc-types.__pthread_start_t
.llvm-libc-types.__pthread_tss_dtor_t
.llvm-libc-types.pthread_attr_t
+ .llvm-libc-types.pthread_condattr_t
.llvm-libc-types.pthread_mutex_t
.llvm-libc-types.pthread_mutexattr_t
.llvm-libc-types.pthread_t
diff --git a/libc/include/llvm-libc-types/CMakeLists.txt b/libc/include/llvm-libc-types/CMakeLists.txt
index 93a79e1477b337..f26fc0729dc94c 100644
--- a/libc/include/llvm-libc-types/CMakeLists.txt
+++ b/libc/include/llvm-libc-types/CMakeLists.txt
@@ -49,11 +49,12 @@ add_header(pid_t HDR pid_t.h)
add_header(posix_spawn_file_actions_t HDR posix_spawn_file_actions_t.h)
add_header(posix_spawnattr_t HDR posix_spawnattr_t.h)
add_header(pthread_attr_t HDR pthread_attr_t.h DEPENDS .size_t)
+add_header(pthread_condattr_t HDR pthread_condattr_t.h DEPENDS .clockid_t)
add_header(pthread_key_t HDR pthread_key_t.h)
add_header(pthread_mutex_t HDR pthread_mutex_t.h DEPENDS .__futex_word .__mutex_type)
-add_header(pthread_t HDR pthread_t.h DEPENDS .__thread_type)
add_header(pthread_mutexattr_t HDR pthread_mutexattr_t.h)
add_header(pthread_once_t HDR pthread_once_t.h DEPENDS .__futex_word)
+add_header(pthread_t HDR pthread_t.h DEPENDS .__thread_type)
add_header(rlim_t HDR rlim_t.h)
add_header(time_t HDR time_t.h)
add_header(stack_t HDR stack_t.h)
diff --git a/libc/include/llvm-libc-types/pthread_condattr_t.h b/libc/include/llvm-libc-types/pthread_condattr_t.h
new file mode 100644
index 00000000000000..b91fc2950aa3f2
--- /dev/null
+++ b/libc/include/llvm-libc-types/pthread_condattr_t.h
@@ -0,0 +1,18 @@
+//===-- Definition of pthread_condattr_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_PTHREAD_CONDATTR_T_H
+#define LLVM_LIBC_TYPES_PTHREAD_CONDATTR_T_H
+
+#include "clockid_t.h"
+
+typedef struct {
+ clockid_t clock;
+ int pshared;
+} pthread_condattr_t;
+
+#endif // LLVM_LIBC_TYPES_PTHREAD_CONDATTR_T_H
diff --git a/libc/include/pthread.h.def b/libc/include/pthread.h.def
index abeb839ee83d16..a94d770657e100 100644
--- a/libc/include/pthread.h.def
+++ b/libc/include/pthread.h.def
@@ -11,6 +11,9 @@
#include "__llvm-libc-common.h"
+// TODO: move to a pthreads-macros.h file:
+// https://github.com/llvm/llvm-project/issues/88997
+
#define PTHREAD_STACK_MIN (1 << 14) // 16KB
#define PTHREAD_MUTEX_INITIALIZER {0}
@@ -32,6 +35,9 @@ enum {
PTHREAD_MUTEX_ROBUST = 0x1,
};
+#define PTHREAD_PROCESS_PRIVATE 0
+#define PTHREAD_PROCESS_SHARED 1
+
%%public_api()
#endif // LLVM_LIBC_PTHREAD_H
diff --git a/libc/spec/posix.td b/libc/spec/posix.td
index 7095a3964ee3fb..0c88dbd848a3fb 100644
--- a/libc/spec/posix.td
+++ b/libc/spec/posix.td
@@ -26,6 +26,7 @@ def UidT : NamedType<"uid_t">;
def GidT : NamedType<"gid_t">;
def DevT : NamedType<"dev_t">;
def ClockIdT : NamedType<"clockid_t">;
+def RestrictedClockIdTPtr : RestrictedPtrType<ClockIdT>;
def BlkSizeT : NamedType<"blksize_t">;
def BlkCntT : NamedType<"blkcnt_t">;
def NLinkT : NamedType<"nlink_t">;
@@ -105,6 +106,10 @@ def POSIX : StandardSpec<"POSIX"> {
ConstType ConstPThreadAttrTPtr = ConstType<PThreadAttrTPtr>;
ConstType ConstRestrictedPThreadAttrTPtr = ConstType<RestrictedPThreadAttrTPtr>;
+ NamedType PThreadCondAttrTType = NamedType<"pthread_condattr_t">;
+ PtrType PThreadCondAttrTPtr = PtrType<PThreadCondAttrTType>;
+ ConstType ConstRestrictedPThreadCondAttrTPtr = ConstType<RestrictedPtrType<PThreadCondAttrTType>>;
+
NamedType PThreadMutexAttrTType = NamedType<"pthread_mutexattr_t">;
PtrType PThreadMutexAttrTPtr = PtrType<PThreadMutexAttrTType>;
RestrictedPtrType RestrictedPThreadMutexAttrTPtr = RestrictedPtrType<PThreadMutexAttrTType>;
@@ -980,7 +985,9 @@ def POSIX : StandardSpec<"POSIX"> {
[], // Macros
[
AtForkCallbackT,
+ ClockIdT,
PThreadAttrTType,
+ PThreadCondAttrTType,
PThreadKeyT,
PThreadMutexAttrTType,
PThreadMutexTType,
@@ -1047,6 +1054,36 @@ def POSIX : StandardSpec<"POSIX"> {
RetValSpec<IntType>,
[ArgSpec<PThreadAttrTPtr>, ArgSpec<VoidPtr>, ArgSpec<SizeTType>]
>,
+ FunctionSpec<
+ "pthread_condattr_destroy",
+ RetValSpec<IntType>,
+ [ArgSpec<PThreadCondAttrTPtr>]
+ >,
+ FunctionSpec<
+ "pthread_condattr_getclock",
+ RetValSpec<IntType>,
+ [ArgSpec<ConstRestrictedPThreadCondAttrTPtr>, ArgSpec<RestrictedClockIdTPtr>]
+ >,
+ FunctionSpec<
+ "pthread_condattr_getpshared",
+ RetValSpec<IntType>,
+ [ArgSpec<ConstRestrictedPThreadCondAttrTPtr>, ArgSpec<RestrictedIntPtr>]
+ >,
+ FunctionSpec<
+ "pthread_condattr_init",
+ RetValSpec<IntType>,
+ [ArgSpec<PThreadCondAttrTPtr>]
+ >,
+ FunctionSpec<
+ "pthread_condattr_setclock",
+ RetValSpec<IntType>,
+ [ArgSpec<PThreadCondAttrTPtr>, ArgSpec<ClockIdT>]
+ >,
+ FunctionSpec<
+ "pthread_condattr_setpshared",
+ RetValSpec<IntType>,
+ [ArgSpec<PThreadCondAttrTPtr>, ArgSpec<IntType>]
+ >,
FunctionSpec<
"pthread_create",
RetValSpec<IntType>,
@@ -1522,9 +1559,30 @@ def POSIX : StandardSpec<"POSIX"> {
HeaderSpec SysTypes = HeaderSpec<
"sys/types.h",
[], // Macros
- [BlkCntT, BlkSizeT, ClockIdT, DevT, GidT, InoT, ModeTType, NLinkT, OffTType, PidT,
- PThreadAttrTType, PThreadKeyT, PThreadMutexTType, PThreadMutexAttrTType, PThreadOnceT, PThreadTType,
- SizeTType, SSizeTType, SuSecondsT, TimeTType, UidT],
+ [
+ BlkCntT,
+ BlkSizeT,
+ ClockIdT,
+ DevT,
+ GidT,
+ InoT,
+ ModeTType,
+ NLinkT,
+ OffTType,
+ PThreadAttrTType,
+ PThreadCondAttrTType,
+ PThreadKeyT,
+ PThreadMutexAttrTType,
+ PThreadMutexTType,
+ PThreadOnceT,
+ PThreadTType,
+ PidT,
+ SSizeTType,
+ SizeTType,
+ SuSecondsT,
+ TimeTType,
+ UidT
+ ], // Types
[], // Enumerations
[] // Functions
>;
diff --git a/libc/src/pthread/CMakeLists.txt b/libc/src/pthread/CMakeLists.txt
index d5e6c802a84523..3d6cf6dde010b1 100644
--- a/libc/src/pthread/CMakeLists.txt
+++ b/libc/src/pthread/CMakeLists.txt
@@ -100,6 +100,71 @@ add_entrypoint_object(
libc.src.pthread.pthread_attr_setstacksize
)
+add_entrypoint_object(
+ pthread_condattr_destroy
+ SRCS
+ pthread_condattr_destroy.cpp
+ HDRS
+ pthread_condattr_destroy.h
+ DEPENDS
+ libc.include.pthread
+)
+
+add_entrypoint_object(
+ pthread_condattr_getclock
+ SRCS
+ pthread_condattr_getclock.cpp
+ HDRS
+ pthread_condattr_getclock.h
+ DEPENDS
+ libc.include.pthread
+ libc.include.sys_types
+)
+
+add_entrypoint_object(
+ pthread_condattr_getpshared
+ SRCS
+ pthread_condattr_getpshared.cpp
+ HDRS
+ pthread_condattr_getpshared.h
+ DEPENDS
+ libc.include.pthread
+)
+
+add_entrypoint_object(
+ pthread_condattr_init
+ SRCS
+ pthread_condattr_init.cpp
+ HDRS
+ pthread_condattr_init.h
+ DEPENDS
+ libc.include.pthread
+ libc.include.time
+)
+
+add_entrypoint_object(
+ pthread_condattr_setclock
+ SRCS
+ pthread_condattr_setclock.cpp
+ HDRS
+ pthread_condattr_setclock.h
+ DEPENDS
+ libc.include.errno
+ libc.include.pthread
+ libc.include.sys_types
+ libc.include.time
+)
+
+add_entrypoint_object(
+ pthread_condattr_setpshared
+ SRCS
+ pthread_condattr_setpshared.cpp
+ HDRS
+ pthread_condattr_setpshared.h
+ DEPENDS
+ libc.include.pthread
+)
+
add_header_library(
pthread_mutexattr
HDRS
diff --git a/libc/src/pthread/pthread_condattr_destroy.cpp b/libc/src/pthread/pthread_condattr_destroy.cpp
new file mode 100644
index 00000000000000..45cc011a4a92d8
--- /dev/null
+++ b/libc/src/pthread/pthread_condattr_destroy.cpp
@@ -0,0 +1,23 @@
+//===-- Implementation of the pthread_condattr_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 "pthread_condattr_destroy.h"
+
+#include "src/__support/common.h"
+
+#include <pthread.h>
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(int, pthread_condattr_destroy, (pthread_condattr_t * attr)) {
+ // Initializing a pthread_condattr_t acquires no resources, so this is a
+ // no-op.
+ return 0;
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/pthread/pthread_condattr_destroy.h b/libc/src/pthread/pthread_condattr_destroy.h
new file mode 100644
index 00000000000000..2910fa9f96168a
--- /dev/null
+++ b/libc/src/pthread/pthread_condattr_destroy.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for pthread_condattr_destroy ------*- C++ -*-===//
+//
+// 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_PTHREAD_PTHREAD_CONDATTR_DESTROY_H
+#define LLVM_LIBC_SRC_PTHREAD_PTHREAD_CONDATTR_DESTROY_H
+
+#include <pthread.h>
+
+namespace LIBC_NAMESPACE {
+
+int pthread_condattr_destroy(pthread_condattr_t *attr);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_PTHREAD_PTHREAD_CONDATTR_DESTROY_H
diff --git a/libc/src/pthread/pthread_condattr_getclock.cpp b/libc/src/pthread/pthread_condattr_getclock.cpp
new file mode 100644
index 00000000000000..a3a3963f4f429e
--- /dev/null
+++ b/libc/src/pthread/pthread_condattr_getclock.cpp
@@ -0,0 +1,25 @@
+//===-- Implementation of the pthread_condattr_getclock -------------------===//
+//
+// 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 "pthread_condattr_getclock.h"
+
+#include "src/__support/common.h"
+
+#include <pthread.h> // pthread_condattr_t
+#include <sys/types.h> // clockid_t
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(int, pthread_condattr_getclock,
+ (const pthread_condattr_t *__restrict attr,
+ clockid_t *__restrict clock_id)) {
+ *clock_id = attr->clock;
+ return 0;
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/pthread/pthread_condattr_getclock.h b/libc/src/pthread/pthread_condattr_getclock.h
new file mode 100644
index 00000000000000..d5878c4f45b537
--- /dev/null
+++ b/libc/src/pthread/pthread_condattr_getclock.h
@@ -0,0 +1,22 @@
+//===-- Implementation header for pthread_condattr_getclock -----*- C++ -*-===//
+//
+// 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_PTHREAD_PTHREAD_CONDATTR_GETCLOCK_H
+#define LLVM_LIBC_SRC_PTHREAD_PTHREAD_CONDATTR_GETCLOCK_H
+
+#include <pthread.h> // pthread_condattr_t
+#include <sys/types.h> // clockid_t
+
+namespace LIBC_NAMESPACE {
+
+int pthread_condattr_getclock(const pthread_condattr_t *__restrict attr,
+ clockid_t *__restrict clock_id);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_PTHREAD_PTHREAD_CONDATTR_GETCLOCK_H
diff --git a/libc/src/pthread/pthread_condattr_getpshared.cpp b/libc/src/pthread/pthread_condattr_getpshared.cpp
new file mode 100644
index 00000000000000..0c5fdc115c25d7
--- /dev/null
+++ b/libc/src/pthread/pthread_condattr_getpshared.cpp
@@ -0,0 +1,24 @@
+//===-- Implementation of the pthread_condattr_getpshared -----------------===//
+//
+// 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 "pthread_condattr_getpshared.h"
+
+#include "src/__support/common.h"
+
+#include <pthread.h>
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(int, pthread_condattr_getpshared,
+ (const pthread_condattr_t *__restrict attr,
+ int *__restrict pshared)) {
+ *pshared = attr->pshared;
+ return 0;
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/pthread/pthread_condattr_getpshared.h b/libc/src/pthread/pthread_condattr_getpshared.h
new file mode 100644
index 00000000000000..3d7a0c1d357c60
--- /dev/null
+++ b/libc/src/pthread/pthread_condattr_getpshared.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for pthread_condattr_getpshared ---*- C++ -*-===//
+//
+// 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_PTHREAD_PTHREAD_CONDATTR_PSHARED_H
+#define LLVM_LIBC_SRC_PTHREAD_PTHREAD_CONDATTR_PSHARED_H
+
+#include <pthread.h>
+
+namespace LIBC_NAMESPACE {
+
+int pthread_condattr_getpshared(const pthread_condattr_t *__restrict attr,
+ int *__restrict pshared);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_PTHREAD_PTHREAD_CONDATTR_PSHARED_H
diff --git a/libc/src/pthread/pthread_condattr_init.cpp b/libc/src/pthread/pthread_condattr_init.cpp
new file mode 100644
index 00000000000000..54633b2e3a5eaf
--- /dev/null
+++ b/libc/src/pthread/pthread_condattr_init.cpp
@@ -0,0 +1,24 @@
+//===-- Implementation of the pthread_condattr_init -----------------------===//
+//
+// 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 "pthread_condattr_init.h"
+
+#include "src/__support/common.h"
+
+#include <pthread.h> // pthread_condattr_t, PTHREAD_PROCESS_PRIVATE
+#include <time.h> // CLOCK_REALTIME
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(int, pthread_condattr_init, (pthread_condattr_t * attr)) {
+ attr->clock = CLOCK_REALTIME;
+ attr->pshared = PTHREAD_PROCESS_PRIVATE;
+ return 0;
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/pthread/pthread_condattr_init.h b/libc/src/pthread/pthread_condattr_init.h
new file mode 100644
index 00000000000000..9f3c06bb6f4aef
--- /dev/null
+++ b/libc/src/pthread/pthread_condattr_init.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for pthread_condattr_init ---------*- C++ -*-===//
+//
+// 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_PTHREAD_PTHREAD_CONDATTR_INIT_H
+#define LLVM_LIBC_SRC_PTHREAD_PTHREAD_CONDATTR_INIT_H
+
+#include <pthread.h>
+
+namespace LIBC_NAMESPACE {
+
+int pthread_condattr_init(pthread_condattr_t *attr);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_PTHREAD_PTHREAD_CONDATTR_INIT_H
diff --git a/libc/src/pthread/pthread_condattr_setclock.cpp b/libc/src/pthread/pthread_condattr_setclock.cpp
new file mode 100644
index 00000000000000..6eca8b30ef7f8e
--- /dev/null
+++ b/libc/src/pthread/pthread_condattr_setclock.cpp
@@ -0,0 +1,30 @@
+//===-- Implementation of the pthread_condattr_setclock -------------------===//
+//
+// 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 "pthread_condattr_setclock.h"
+
+#include "src/__support/common.h"
+
+#include <errno.h> // EINVAL
+#include <pthread.h> // pthread_condattr_t
+#include <sys/types.h> // clockid_t
+#include <time.h> // CLOCK_MONOTONIC, CLOCK_REALTIME
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(int, pthread_condattr_setclock,
+ (pthread_condattr_t * attr, clockid_t clock)) {
+
+ if (clock != CLOCK_MONOTONIC && clock != CLOCK_REALTIME)
+ return EINVAL;
+
+ attr->clock = clock;
+ return 0;
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/pthread/pthread_condattr_setclock.h b/libc/src/pthread/pthread_condattr_setclock.h
new file mode 100644
index 00000000000000..328766fe788336
--- /dev/null
+++ b/libc/src/pthread/pthread_condattr_setclock.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for pthread_condattr_setclock -----*- C++ -*-===//
+//
+// 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_PTHREAD_PTHREAD_CONDATTR_SETCLOCK_H
+#define LLVM_LIBC_SRC_PTHREAD_PTHREAD_CONDATTR_SETCLOCK_H
+
+#include <pthread.h>
+#include <sys/types.h> // clockid_t
+
+namespace LIBC_NAMESPACE {
+
+int pthread_condattr_setclock(pthread_condattr_t *attr, clockid_t clock);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_PTHREAD_PTHREAD_CONDATTR_SETCLOCK_H
diff --git a/libc/src/pthread/pthread_condattr_setpshared.cpp b/libc/src/pthread/pthread_condattr_setpshared.cpp
new file mode 100644
index 00000000000000..7f1560acad843e
--- /dev/null
+++ b/libc/src/pthread/pthread_condattr_setpshared.cpp
@@ -0,0 +1,28 @@
+//===-- Implementation of the pthread_condattr_setpshared -----------------===//
+//
+// 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 "pthread_condattr_setpshared.h"
+
+#include "src/__support/common.h"
+
+#include <errno.h> // EINVAL
+#include <pthread.h> // pthread_condattr_t, PTHREAD_PROCESS_SHARED, PTHREAD_PROCESS_PRIVATE
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(int, pthread_condattr_setpshared,
+ (pthread_condattr_t * attr, int pshared)) {
+
+ if (pshared != PTHREAD_PROCESS_SHARED && pshared != PTHREAD_PROCESS_PRIVATE)
+ return EINVAL;
+
+ attr->pshared = pshared;
+ return 0;
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/pthread/pthread_condattr_setpshared.h b/libc/src/pthread/pthread_condattr_setpshared.h
new file mode 100644
index 00000000000000..8083bdec78cc47
--- /dev/null
+++ b/libc/src/pthread/pthread_condattr_setpshared.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for pthread_condattr_setpshared ---*- C++ -*-===//
+//
+// 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_PTHREAD_PTHREAD_CONDATTR_SETPSHARED_H
+#define LLVM_LIBC_SRC_PTHREAD_PTHREAD_CONDATTR_SETPSHARED_H
+
+#include <pthread.h>
+
+namespace LIBC_NAMESPACE {
+
+int pthread_condattr_setpshared(pthread_condattr_t *attr, int pshared);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_PTHREAD_PTHREAD_CONDATTR_SETPSHARED_H
diff --git a/libc/test/src/pthread/CMakeLists.txt b/libc/test/src/pthread/CMakeLists.txt
index fb0d22ab9d2a57..46f38422a53745 100644
--- a/libc/test/src/pthread/CMakeLists.txt
+++ b/libc/test/src/pthread/CMakeLists.txt
@@ -39,3 +39,15 @@ add_libc_unittest(
libc.src.pthread.pthread_mutexattr_setrobust
libc.src.pthread.pthread_mutexattr_settype
)
+
+add_libc_unittest(
+ pthread_condattr_test
+ SUITE
+ libc_pthread_unittests
+ SRCS
+ pthread_condattr_test.cpp
+ DEPENDS
+ libc.include.errno
+ libc.include.pthread
+ libc.include.time
+ )
diff --git a/libc/test/src/pthread/pthread_condattr_test.cpp b/libc/test/src/pthread/pthread_condattr_test.cpp
new file mode 100644
index 00000000000000..accb62de92e45f
--- /dev/null
+++ b/libc/test/src/pthread/pthread_condattr_test.cpp
@@ -0,0 +1,71 @@
+//===-- Unittests for pthread_condattr_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
+//
+//===----------------------------------------------------------------------===//
+
+#include "test/UnitTest/Test.h"
+
+#include <errno.h>
+#include <pthread.h>
+#include <time.h>
+
+TEST(LlvmLibcPThreadCondAttrTest, InitAndDestroy) {
+ pthread_condattr_t cond;
+ ASSERT_EQ(pthread_condattr_init(&cond), 0);
+ ASSERT_EQ(pthread_condattr_destroy(&cond), 0);
+}
+
+TEST(LlvmLibcPThreadCondAttrTest, GetDefaultValues) {
+ pthread_condattr_t cond;
+
+ // Invalid clock id.
+ clockid_t clock = 7;
+ // Invalid value.
+ int pshared = 42;
+
+ ASSERT_EQ(pthread_condattr_init(&cond), 0);
+ ASSERT_EQ(pthread_condattr_getclock(&cond, &clock), 0);
+ ASSERT_EQ(clock, CLOCK_REALTIME);
+ ASSERT_EQ(pthread_condattr_getpshared(&cond, &pshared), 0);
+ ASSERT_EQ(pshared, PTHREAD_PROCESS_PRIVATE);
+ ASSERT_EQ(pthread_condattr_destroy(&cond), 0);
+}
+
+TEST(LlvmLibcPThreadCondAttrTest, SetGoodValues) {
+ pthread_condattr_t cond;
+
+ // Invalid clock id.
+ clockid_t clock = 7;
+ // Invalid value.
+ int pshared = 42;
+
+ ASSERT_EQ(pthread_condattr_init(&cond), 0);
+ ASSERT_EQ(pthread_condattr_setclock(&cond, CLOCK_MONOTONIC), 0);
+ ASSERT_EQ(pthread_condattr_getclock(&cond, &clock), 0);
+ ASSERT_EQ(clock, CLOCK_MONOTONIC);
+ ASSERT_EQ(pthread_condattr_setpshared(&cond, PTHREAD_PROCESS_SHARED), 0);
+ ASSERT_EQ(pthread_condattr_getpshared(&cond, &pshared), 0);
+ ASSERT_EQ(pshared, PTHREAD_PROCESS_SHARED);
+ ASSERT_EQ(pthread_condattr_destroy(&cond), 0);
+}
+
+TEST(LlvmLibcPThreadCondAttrTest, SetBadValues) {
+ pthread_condattr_t cond;
+
+ // Invalid clock id.
+ clockid_t clock = 7;
+ // Invalid value.
+ int pshared = 42;
+
+ ASSERT_EQ(pthread_condattr_init(&cond), 0);
+ ASSERT_EQ(pthread_condattr_setclock(&cond, clock), EINVAL);
+ ASSERT_EQ(pthread_condattr_getclock(&cond, &clock), 0);
+ ASSERT_EQ(clock, CLOCK_REALTIME);
+ ASSERT_EQ(pthread_condattr_setpshared(&cond, pshared), EINVAL);
+ ASSERT_EQ(pthread_condattr_getpshared(&cond, &pshared), 0);
+ ASSERT_EQ(pshared, PTHREAD_PROCESS_PRIVATE);
+ ASSERT_EQ(pthread_condattr_destroy(&cond), 0);
+}
More information about the libc-commits
mailing list