24 changes: 24 additions & 0 deletions libc/src/pthread/pthread_condattr_getpshared.cpp
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions libc/src/pthread/pthread_condattr_getpshared.h
Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions libc/src/pthread/pthread_condattr_init.cpp
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions libc/src/pthread/pthread_condattr_init.h
Original file line number Diff line number Diff line change
@@ -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
30 changes: 30 additions & 0 deletions libc/src/pthread/pthread_condattr_setclock.cpp
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions libc/src/pthread/pthread_condattr_setclock.h
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions libc/src/pthread/pthread_condattr_setpshared.cpp
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions libc/src/pthread/pthread_condattr_setpshared.h
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions libc/test/src/pthread/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
71 changes: 71 additions & 0 deletions libc/test/src/pthread/pthread_condattr_test.cpp
Original file line number Diff line number Diff line change
@@ -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);
}