20 changes: 20 additions & 0 deletions libc/src/pthread/pthread_mutexattr_destroy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation header for pthread_mutexattr_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_MUTEXATTR_DESTROY_H
#define LLVM_LIBC_SRC_PTHREAD_PTHREAD_MUTEXATTR_DESTROY_H

#include <pthread.h>

namespace __llvm_libc {

int pthread_mutexattr_destroy(pthread_mutexattr_t *attr);

} // namespace __llvm_libc

#endif // LLVM_LIBC_SRC_PTHREAD_PTHREAD_MUTEXATTR_DESTROY_H
26 changes: 26 additions & 0 deletions libc/src/pthread/pthread_mutexattr_getpshared.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//===-- Implementation of the pthread_mutexattr_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_mutexattr_getpshared.h"
#include "pthread_mutexattr.h"

#include "src/__support/common.h"

#include <errno.h>

namespace __llvm_libc {

LLVM_LIBC_FUNCTION(int, pthread_mutexattr_getpshared,
(const pthread_mutexattr_t *__restrict attr,
int *__restrict pshared)) {
*pshared = (*attr & unsigned(PThreadMutexAttrPos::PSHARED_MASK)) >>
unsigned(PThreadMutexAttrPos::PSHARED_SHIFT);
return 0;
}

} // namespace __llvm_libc
21 changes: 21 additions & 0 deletions libc/src/pthread/pthread_mutexattr_getpshared.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- Implementation header for pthread_mutexattr_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_MUTEXATTR_GETPSHARED_H
#define LLVM_LIBC_SRC_PTHREAD_PTHREAD_MUTEXATTR_GETPSHARED_H

#include <pthread.h>

namespace __llvm_libc {

int pthread_mutexattr_getpshared(const pthread_mutexattr_t *__restrict attr,
int *__restrict pshared);

} // namespace __llvm_libc

#endif // LLVM_LIBC_SRC_PTHREAD_PTHREAD_MUTEXATTR_GETPSHARED_H
26 changes: 26 additions & 0 deletions libc/src/pthread/pthread_mutexattr_getrobust.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//===-- Implementation of the pthread_mutexattr_getrobust -----------------===//
//
// 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_mutexattr_getrobust.h"
#include "pthread_mutexattr.h"

#include "src/__support/common.h"

#include <errno.h>

namespace __llvm_libc {

LLVM_LIBC_FUNCTION(int, pthread_mutexattr_getrobust,
(const pthread_mutexattr_t *__restrict attr,
int *__restrict robust)) {
*robust = (*attr & unsigned(PThreadMutexAttrPos::ROBUST_MASK)) >>
unsigned(PThreadMutexAttrPos::ROBUST_SHIFT);
return 0;
}

} // namespace __llvm_libc
21 changes: 21 additions & 0 deletions libc/src/pthread/pthread_mutexattr_getrobust.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- Implementation header for pthread_mutexattr_getrobust ---*- 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_MUTEXATTR_GETROBUST_H
#define LLVM_LIBC_SRC_PTHREAD_PTHREAD_MUTEXATTR_GETROBUST_H

#include <pthread.h>

namespace __llvm_libc {

int pthread_mutexattr_getrobust(const pthread_mutexattr_t *__restrict attr,
int *__restrict robust);

} // namespace __llvm_libc

#endif // LLVM_LIBC_SRC_PTHREAD_PTHREAD_MUTEXATTR_GETROBUST_H
26 changes: 26 additions & 0 deletions libc/src/pthread/pthread_mutexattr_gettype.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//===-- Implementation of the pthread_mutexattr_gettype -------------------===//
//
// 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_mutexattr_gettype.h"
#include "pthread_mutexattr.h"

#include "src/__support/common.h"

#include <errno.h>

namespace __llvm_libc {

LLVM_LIBC_FUNCTION(int, pthread_mutexattr_gettype,
(const pthread_mutexattr_t *__restrict attr,
int *__restrict type)) {
*type = (*attr & unsigned(PThreadMutexAttrPos::TYPE_MASK)) >>
unsigned(PThreadMutexAttrPos::TYPE_SHIFT);
return 0;
}

} // namespace __llvm_libc
21 changes: 21 additions & 0 deletions libc/src/pthread/pthread_mutexattr_gettype.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- Implementation header for pthread_mutexattr_gettype -----*- 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_MUTEXATTR_GETTYPE_H
#define LLVM_LIBC_SRC_PTHREAD_PTHREAD_MUTEXATTR_GETTYPE_H

#include <pthread.h>

namespace __llvm_libc {

int pthread_mutexattr_gettype(const pthread_mutexattr_t *__restrict attr,
int *__restrict type);

} // namespace __llvm_libc

#endif // LLVM_LIBC_SRC_PTHREAD_PTHREAD_MUTEXATTR_GETTYPE_H
23 changes: 23 additions & 0 deletions libc/src/pthread/pthread_mutexattr_init.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//===-- Implementation of the pthread_mutexattr_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_mutexattr_init.h"
#include "pthread_mutexattr.h"

#include "src/__support/common.h"

namespace __llvm_libc {

LLVM_LIBC_FUNCTION(int, pthread_mutexattr_init, (pthread_mutexattr_t * attr)) {
// Set the default attributes and mark the attribute object as initiliazed
// by setting the first bit.
*attr = DEFAULT_MUTEXATTR;
return 0;
}

} // namespace __llvm_libc
20 changes: 20 additions & 0 deletions libc/src/pthread/pthread_mutexattr_init.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation header for pthread_mutexattr_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_MUTEXATTR_INIT_H
#define LLVM_LIBC_SRC_PTHREAD_PTHREAD_MUTEXATTR_INIT_H

#include <pthread.h>

namespace __llvm_libc {

int pthread_mutexattr_init(pthread_mutexattr_t *attr);

} // namespace __llvm_libc

#endif // LLVM_LIBC_SRC_PTHREAD_PTHREAD_MUTEXATTR_INIT_H
29 changes: 29 additions & 0 deletions libc/src/pthread/pthread_mutexattr_setpshared.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//===-- Implementation of the pthread_mutexattr_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_mutexattr_setpshared.h"
#include "pthread_mutexattr.h"

#include "src/__support/common.h"

#include <errno.h>
#include <pthread.h>

namespace __llvm_libc {

LLVM_LIBC_FUNCTION(int, pthread_mutexattr_setpshared,
(pthread_mutexattr_t *__restrict attr, int pshared)) {
if (pshared != PTHREAD_PROCESS_PRIVATE && pshared != PTHREAD_PROCESS_SHARED)
return EINVAL;
pthread_mutexattr_t old = *attr;
old &= ~unsigned(PThreadMutexAttrPos::PSHARED_MASK);
*attr = old | (pshared << unsigned(PThreadMutexAttrPos::PSHARED_SHIFT));
return 0;
}

} // namespace __llvm_libc
21 changes: 21 additions & 0 deletions libc/src/pthread/pthread_mutexattr_setpshared.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- Implementation header for pthread_mutexattr_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_MUTEXATTR_SETpshared_H
#define LLVM_LIBC_SRC_PTHREAD_PTHREAD_MUTEXATTR_SETpshared_H

#include <pthread.h>

namespace __llvm_libc {

int pthread_mutexattr_setpshared(pthread_mutexattr_t *__restrict attr,
int pshared);

} // namespace __llvm_libc

#endif // LLVM_LIBC_SRC_PTHREAD_PTHREAD_MUTEXATTR_SETpshared_H
29 changes: 29 additions & 0 deletions libc/src/pthread/pthread_mutexattr_setrobust.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//===-- Implementation of the pthread_mutexattr_setrobust -----------------===//
//
// 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_mutexattr_setrobust.h"
#include "pthread_mutexattr.h"

#include "src/__support/common.h"

#include <errno.h>
#include <pthread.h>

namespace __llvm_libc {

LLVM_LIBC_FUNCTION(int, pthread_mutexattr_setrobust,
(pthread_mutexattr_t *__restrict attr, int robust)) {
if (robust != PTHREAD_MUTEX_STALLED && robust != PTHREAD_MUTEX_ROBUST)
return EINVAL;
pthread_mutexattr_t old = *attr;
old &= ~unsigned(PThreadMutexAttrPos::ROBUST_MASK);
*attr = old | (robust << unsigned(PThreadMutexAttrPos::ROBUST_SHIFT));
return 0;
}

} // namespace __llvm_libc
21 changes: 21 additions & 0 deletions libc/src/pthread/pthread_mutexattr_setrobust.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- Implementation header for pthread_mutexattr_setrobust ---*- 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_MUTEXATTR_SETROBUST_H
#define LLVM_LIBC_SRC_PTHREAD_PTHREAD_MUTEXATTR_SETROBUST_H

#include <pthread.h>

namespace __llvm_libc {

int pthread_mutexattr_setrobust(pthread_mutexattr_t *__restrict attr,
int robust);

} // namespace __llvm_libc

#endif // LLVM_LIBC_SRC_PTHREAD_PTHREAD_MUTEXATTR_SETROBUST_H
31 changes: 31 additions & 0 deletions libc/src/pthread/pthread_mutexattr_settype.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//===-- Implementation of the pthread_mutexattr_settype -------------------===//
//
// 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_mutexattr_settype.h"
#include "pthread_mutexattr.h"

#include "src/__support/common.h"

#include <errno.h>
#include <pthread.h>

namespace __llvm_libc {

LLVM_LIBC_FUNCTION(int, pthread_mutexattr_settype,
(pthread_mutexattr_t *__restrict attr, int type)) {
if (type != PTHREAD_MUTEX_NORMAL && type != PTHREAD_MUTEX_ERRORCHECK &&
type != PTHREAD_MUTEX_RECURSIVE) {
return EINVAL;
}
pthread_mutexattr_t old = *attr;
old &= ~unsigned(PThreadMutexAttrPos::TYPE_MASK);
*attr = old | (type << unsigned(PThreadMutexAttrPos::TYPE_SHIFT));
return 0;
}

} // namespace __llvm_libc
20 changes: 20 additions & 0 deletions libc/src/pthread/pthread_mutexattr_settype.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation header for pthread_mutexattr_settype -----*- 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_MUTEXATTR_SETTYPE_H
#define LLVM_LIBC_SRC_PTHREAD_PTHREAD_MUTEXATTR_SETTYPE_H

#include <pthread.h>

namespace __llvm_libc {

int pthread_mutexattr_settype(pthread_mutexattr_t *__restrict attr, int type);

} // namespace __llvm_libc

#endif // LLVM_LIBC_SRC_PTHREAD_PTHREAD_MUTEXATTR_SETTYPE_H
4 changes: 4 additions & 0 deletions libc/test/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ add_subdirectory(stdio)
add_subdirectory(threads)
add_subdirectory(time)

if(${LIBC_TARGET_OS} STREQUAL "linux")
add_subdirectory(pthread)
endif()

set(public_test ${CMAKE_CURRENT_BINARY_DIR}/public_api_test.cpp)

set(entrypoints_name_list "")
Expand Down
20 changes: 20 additions & 0 deletions libc/test/src/pthread/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
add_libc_testsuite(libc_pthread_unittests)

add_libc_unittest(
pthread_mutexattr_test
SUITE
libc_pthread_unittests
SRCS
pthread_mutexattr_test.cpp
DEPENDS
libc.include.errno
libc.include.pthread
libc.src.pthread.pthread_mutexattr_destroy
libc.src.pthread.pthread_mutexattr_init
libc.src.pthread.pthread_mutexattr_getpshared
libc.src.pthread.pthread_mutexattr_getrobust
libc.src.pthread.pthread_mutexattr_gettype
libc.src.pthread.pthread_mutexattr_setpshared
libc.src.pthread.pthread_mutexattr_setrobust
libc.src.pthread.pthread_mutexattr_settype
)
91 changes: 91 additions & 0 deletions libc/test/src/pthread/pthread_mutexattr_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//===-- Unittests for pthread_mutexattr_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 "src/pthread/pthread_mutexattr_destroy.h"
#include "src/pthread/pthread_mutexattr_getpshared.h"
#include "src/pthread/pthread_mutexattr_getrobust.h"
#include "src/pthread/pthread_mutexattr_gettype.h"
#include "src/pthread/pthread_mutexattr_init.h"
#include "src/pthread/pthread_mutexattr_setpshared.h"
#include "src/pthread/pthread_mutexattr_setrobust.h"
#include "src/pthread/pthread_mutexattr_settype.h"
#include "utils/UnitTest/Test.h"

#include <errno.h>
#include <pthread.h>

TEST(LlvmLibcPThreadMutexAttrTest, InitAndDestroy) {
pthread_mutexattr_t attr;
ASSERT_EQ(__llvm_libc::pthread_mutexattr_init(&attr), 0);
ASSERT_EQ(__llvm_libc::pthread_mutexattr_destroy(&attr), 0);
}

TEST(LlvmLibcPThreadMutexAttrTest, SetAndGetType) {
int type;
pthread_mutexattr_t attr;
ASSERT_EQ(__llvm_libc::pthread_mutexattr_init(&attr), 0);
ASSERT_EQ(__llvm_libc::pthread_mutexattr_gettype(&attr, &type), 0);
ASSERT_EQ(type, int(PTHREAD_MUTEX_DEFAULT));

ASSERT_EQ(
__llvm_libc::pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE),
0);
ASSERT_EQ(__llvm_libc::pthread_mutexattr_gettype(&attr, &type), 0);
ASSERT_EQ(type, int(PTHREAD_MUTEX_RECURSIVE));

ASSERT_EQ(
__llvm_libc::pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK),
0);
ASSERT_EQ(__llvm_libc::pthread_mutexattr_gettype(&attr, &type), 0);
ASSERT_EQ(type, int(PTHREAD_MUTEX_ERRORCHECK));

ASSERT_EQ(__llvm_libc::pthread_mutexattr_settype(&attr, 0xBAD), EINVAL);
}

TEST(LlvmLibcPThreadMutexAttrTest, SetAndGetRobust) {
int robust;
pthread_mutexattr_t attr;
ASSERT_EQ(__llvm_libc::pthread_mutexattr_init(&attr), 0);
ASSERT_EQ(__llvm_libc::pthread_mutexattr_getrobust(&attr, &robust), 0);
ASSERT_EQ(robust, int(PTHREAD_MUTEX_STALLED));

ASSERT_EQ(
__llvm_libc::pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_ROBUST), 0);
ASSERT_EQ(__llvm_libc::pthread_mutexattr_getrobust(&attr, &robust), 0);
ASSERT_EQ(robust, int(PTHREAD_MUTEX_ROBUST));

ASSERT_EQ(
__llvm_libc::pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_STALLED),
0);
ASSERT_EQ(__llvm_libc::pthread_mutexattr_getrobust(&attr, &robust), 0);
ASSERT_EQ(robust, int(PTHREAD_MUTEX_STALLED));

ASSERT_EQ(__llvm_libc::pthread_mutexattr_setrobust(&attr, 0xBAD), EINVAL);
}

TEST(LlvmLibcPThreadMutexAttrTest, SetAndGetPShared) {
int pshared;
pthread_mutexattr_t attr;
ASSERT_EQ(__llvm_libc::pthread_mutexattr_init(&attr), 0);
ASSERT_EQ(__llvm_libc::pthread_mutexattr_getpshared(&attr, &pshared), 0);
ASSERT_EQ(pshared, int(PTHREAD_PROCESS_PRIVATE));

ASSERT_EQ(
__llvm_libc::pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED),
0);
ASSERT_EQ(__llvm_libc::pthread_mutexattr_getpshared(&attr, &pshared), 0);
ASSERT_EQ(pshared, int(PTHREAD_PROCESS_SHARED));

ASSERT_EQ(
__llvm_libc::pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE),
0);
ASSERT_EQ(__llvm_libc::pthread_mutexattr_getpshared(&attr, &pshared), 0);
ASSERT_EQ(pshared, int(PTHREAD_PROCESS_PRIVATE));

ASSERT_EQ(__llvm_libc::pthread_mutexattr_setpshared(&attr, 0xBAD), EINVAL);
}