[libc] Add a definition of pthread_attr_t and its getters and setters.

Not all attributes have been added to phtread_attr_t in this patch. They
will be added gradually in future patches.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D123423
This commit is contained in:
Siva Chandra Reddy 2022-04-08 08:07:22 +00:00
parent 1c8366f9f2
commit 0258f56646
32 changed files with 816 additions and 2 deletions

View File

@ -230,6 +230,16 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.threads.thrd_join
# pthread.h entrypoints
libc.src.pthread.pthread_attr_destroy
libc.src.pthread.pthread_attr_init
libc.src.pthread.pthread_attr_getdetachstate
libc.src.pthread.pthread_attr_getguardsize
libc.src.pthread.pthread_attr_getstack
libc.src.pthread.pthread_attr_getstacksize
libc.src.pthread.pthread_attr_setdetachstate
libc.src.pthread.pthread_attr_setguardsize
libc.src.pthread.pthread_attr_setstack
libc.src.pthread.pthread_attr_setstacksize
libc.src.pthread.pthread_mutexattr_destroy
libc.src.pthread.pthread_mutexattr_init
libc.src.pthread.pthread_mutexattr_getpshared

View File

@ -238,7 +238,10 @@ def ThreadsAPI : PublicAPI<"threads.h"> {
}
def PThreadAPI : PublicAPI<"pthread.h"> {
let Types = ["pthread_mutexattr_t"];
let Types = [
"pthread_attr_t",
"pthread_mutexattr_t"
];
}
def UniStdAPI : PublicAPI<"unistd.h"> {

View File

@ -223,6 +223,17 @@ if(LLVM_LIBC_FULL_BUILD)
# libc.src.assert.__assert_fail
# pthread.h entrypoints
libc.src.pthread.pthread_attr_destroy
libc.src.pthread.pthread_attr_init
libc.src.pthread.pthread_attr_getdetachstate
libc.src.pthread.pthread_attr_getguardsize
libc.src.pthread.pthread_attr_getstack
libc.src.pthread.pthread_attr_getstacksize
libc.src.pthread.pthread_attr_setdetachstate
libc.src.pthread.pthread_attr_setguardsize
libc.src.pthread.pthread_attr_setstack
libc.src.pthread.pthread_attr_setstacksize
libc.src.pthread.pthread_attr_init
libc.src.pthread.pthread_mutexattr_destroy
libc.src.pthread.pthread_mutexattr_init
libc.src.pthread.pthread_mutexattr_getpshared

View File

@ -161,6 +161,7 @@ add_gen_header(
GEN_HDR pthread.h
DEPENDS
.llvm_libc_common_h
.llvm-libc-types.pthread_attr_t
.llvm-libc-types.pthread_mutexattr_t
)

View File

@ -18,6 +18,7 @@ add_header(mode_t HDR mode_t.h)
add_header(mtx_t HDR mtx_t.h DEPENDS .__futex_word .__mutex_type)
add_header(off_t HDR off_t.h)
add_header(once_flag HDR once_flag.h DEPENDS .__futex_word)
add_header(pthread_attr_t HDR pthread_attr_t.h)
add_header(pthread_mutexattr_t HDR pthread_mutexattr_t.h)
add_header(size_t HDR size_t.h)
add_header(ssize_t HDR ssize_t.h)

View File

@ -0,0 +1,21 @@
//===-- Definition of pthread_attr_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_ATTR_T_H
#define __LLVM_LIBC_TYPES_PTHREAD_ATTR_T_H
#include <llvm-libc-types/size_t.h>
typedef struct {
int __detachstate;
void *__stack;
size_t __stacksize;
size_t __guardsize;
} pthread_attr_t;
#endif // __LLVM_LIBC_TYPES_PTHREAD_ATTR_T_H

View File

@ -11,7 +11,12 @@
#include <__llvm-libc-common.h>
#define PTHREAD_STACK_MIN (1 << 14) // 16KB
enum {
PTHREAD_CREATE_JOINABLE = 0x0,
PTHREAD_CREATE_DETACHED = 0x1,
PTHREAD_MUTEX_NORMAL = 0x0,
PTHREAD_MUTEX_ERRORCHECK = 0x1,
PTHREAD_MUTEX_RECURSIVE = 0x2,

View File

@ -20,6 +20,13 @@ def POSIX : StandardSpec<"POSIX"> {
NamedType ModeTType = NamedType<"mode_t">;
NamedType OffTType = NamedType<"off_t">;
NamedType SSizeTType = NamedType<"ssize_t">;
NamedType PThreadAttrTType = NamedType<"pthread_attr_t">;
PtrType PThreadAttrTPtr = PtrType<PThreadAttrTType>;
RestrictedPtrType RestrictedPThreadAttrTPtr = RestrictedPtrType<PThreadAttrTType>;
ConstType ConstPThreadAttrTPtr = ConstType<PThreadAttrTPtr>;
ConstType ConstRestrictedPThreadAttrTPtr = ConstType<RestrictedPThreadAttrTPtr>;
NamedType PThreadMutexAttrTType = NamedType<"pthread_mutexattr_t">;
PtrType PThreadMutexAttrTPtr = PtrType<PThreadMutexAttrTType>;
RestrictedPtrType RestrictedPThreadMutexAttrTPtr = RestrictedPtrType<PThreadMutexAttrTType>;
@ -379,9 +386,59 @@ def POSIX : StandardSpec<"POSIX"> {
HeaderSpec PThread = HeaderSpec<
"pthread.h",
[], // Macros
[PThreadMutexAttrTType], // Types
[PThreadAttrTType, PThreadMutexAttrTType], // Types
[], // Enumerations
[
FunctionSpec<
"pthread_attr_init",
RetValSpec<IntType>,
[ArgSpec<PThreadAttrTPtr>]
>,
FunctionSpec<
"pthread_attr_destroy",
RetValSpec<IntType>,
[ArgSpec<PThreadAttrTPtr>]
>,
FunctionSpec<
"pthread_attr_getdetachstate",
RetValSpec<IntType>,
[ArgSpec<ConstPThreadAttrTPtr>, ArgSpec<IntPtr>]
>,
FunctionSpec<
"pthread_attr_setdetachstate",
RetValSpec<IntType>,
[ArgSpec<PThreadAttrTPtr>, ArgSpec<IntType>]
>,
FunctionSpec<
"pthread_attr_getguardsize",
RetValSpec<IntType>,
[ArgSpec<ConstRestrictedPThreadAttrTPtr>, ArgSpec<RestrictedSizeTPtr>]
>,
FunctionSpec<
"pthread_attr_setguardsize",
RetValSpec<IntType>,
[ArgSpec<PThreadAttrTPtr>, ArgSpec<SizeTType>]
>,
FunctionSpec<
"pthread_attr_getstacksize",
RetValSpec<IntType>,
[ArgSpec<ConstRestrictedPThreadAttrTPtr>, ArgSpec<RestrictedSizeTPtr>]
>,
FunctionSpec<
"pthread_attr_setstacksize",
RetValSpec<IntType>,
[ArgSpec<PThreadAttrTPtr>, ArgSpec<SizeTType>]
>,
FunctionSpec<
"pthread_attr_getstack",
RetValSpec<IntType>,
[ArgSpec<ConstRestrictedPThreadAttrTPtr>, ArgSpec<RestrictedVoidPtrPtr>, ArgSpec<RestrictedSizeTPtr>]
>,
FunctionSpec<
"pthread_attr_setstack",
RetValSpec<IntType>,
[ArgSpec<PThreadAttrTPtr>, ArgSpec<VoidPtr>, ArgSpec<SizeTType>]
>,
FunctionSpec<
"pthread_mutexattr_init",
RetValSpec<IntType>,

View File

@ -50,8 +50,14 @@ def CharType : NamedType<"char">;
// Common types
def VoidPtr : PtrType<VoidType>;
def VoidPtrPtr : PtrType<VoidPtr>;
def RestrictedVoidPtrPtr : RestrictedPtrType<VoidPtr>;
def ConstVoidPtr : ConstType<VoidPtr>;
def SizeTType : NamedType<"size_t">;
def SizeTPtr : PtrType<SizeTType>;
def RestrictedSizeTPtr : RestrictedPtrType<SizeTType>;
def LongDoublePtr : PtrType<LongDoubleType>;
def IntMaxTType : NamedType<"intmax_t">;

View File

@ -1,3 +1,103 @@
add_entrypoint_object(
pthread_attr_init
SRCS
pthread_attr_init.cpp
HDRS
pthread_attr_init.h
DEPENDS
libc.include.pthread
)
add_entrypoint_object(
pthread_attr_destroy
SRCS
pthread_attr_destroy.cpp
HDRS
pthread_attr_destroy.h
DEPENDS
libc.include.pthread
)
add_entrypoint_object(
pthread_attr_getdetachstate
SRCS
pthread_attr_getdetachstate.cpp
HDRS
pthread_attr_getdetachstate.h
DEPENDS
libc.include.pthread
)
add_entrypoint_object(
pthread_attr_setdetachstate
SRCS
pthread_attr_setdetachstate.cpp
HDRS
pthread_attr_setdetachstate.h
DEPENDS
libc.include.pthread
)
add_entrypoint_object(
pthread_attr_getguardsize
SRCS
pthread_attr_getguardsize.cpp
HDRS
pthread_attr_getguardsize.h
DEPENDS
libc.include.pthread
)
add_entrypoint_object(
pthread_attr_setguardsize
SRCS
pthread_attr_setguardsize.cpp
HDRS
pthread_attr_setguardsize.h
DEPENDS
libc.include.pthread
)
add_entrypoint_object(
pthread_attr_getstacksize
SRCS
pthread_attr_getstacksize.cpp
HDRS
pthread_attr_getstacksize.h
DEPENDS
libc.include.pthread
)
add_entrypoint_object(
pthread_attr_setstacksize
SRCS
pthread_attr_setstacksize.cpp
HDRS
pthread_attr_setstacksize.h
DEPENDS
libc.include.pthread
)
add_entrypoint_object(
pthread_attr_getstack
SRCS
pthread_attr_getstack.cpp
HDRS
pthread_attr_getstack.h
DEPENDS
libc.include.pthread
)
add_entrypoint_object(
pthread_attr_setstack
SRCS
pthread_attr_setstack.cpp
HDRS
pthread_attr_setstack.h
DEPENDS
libc.include.pthread
)
add_header_library(
pthread_mutexattr
HDRS

View File

@ -0,0 +1,22 @@
//===-- Implementation of the pthread_attr_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_attr_destroy.h"
#include "src/__support/common.h"
#include <pthread.h>
namespace __llvm_libc {
LLVM_LIBC_FUNCTION(int, pthread_attr_destroy, (pthread_attr_t * attr)) {
// There is nothing to cleanup.
return 0;
}
} // namespace __llvm_libc

View File

@ -0,0 +1,20 @@
//===-- Implementation header for pthread_attr_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_ATTR_DESTROY_H
#define LLVM_LIBC_SRC_PTHREAD_PTHREAD_ATTR_DESTROY_H
#include <pthread.h>
namespace __llvm_libc {
int pthread_attr_destroy(pthread_attr_t *attr);
} // namespace __llvm_libc
#endif // LLVM_LIBC_SRC_PTHREAD_PTHREAD_ATTR_DESTROY_H

View File

@ -0,0 +1,23 @@
//===-- Implementation of the pthread_attr_getdetachstate -----------------===//
//
// 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_attr_getdetachstate.h"
#include "src/__support/common.h"
#include <pthread.h>
namespace __llvm_libc {
LLVM_LIBC_FUNCTION(int, pthread_attr_getdetachstate,
(const pthread_attr_t *attr, int *detachstate)) {
*detachstate = attr->__detachstate;
return 0;
}
} // namespace __llvm_libc

View File

@ -0,0 +1,20 @@
//===-- Implementation header for pthread_attr_getdetachstate ---*- 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_ATTR_GETDETACHSTATE_H
#define LLVM_LIBC_SRC_PTHREAD_PTHREAD_ATTR_GETDETACHSTATE_H
#include <pthread.h>
namespace __llvm_libc {
int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detach_state);
} // namespace __llvm_libc
#endif // LLVM_LIBC_SRC_PTHREAD_PTHREAD_ATTR_GETDETACHSTATE_H

View File

@ -0,0 +1,24 @@
//===-- Implementation of the pthread_attr_getguardsize -----------------===//
//
// 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_attr_getguardsize.h"
#include "src/__support/common.h"
#include <pthread.h>
namespace __llvm_libc {
LLVM_LIBC_FUNCTION(int, pthread_attr_getguardsize,
(const pthread_attr_t *__restrict attr,
size_t *__restrict guardsize)) {
*guardsize = attr->__guardsize;
return 0;
}
} // namespace __llvm_libc

View File

@ -0,0 +1,21 @@
//===-- Implementation header for pthread_attr_getguardsize -----*- 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_ATTR_GETGUARDSIZE_H
#define LLVM_LIBC_SRC_PTHREAD_PTHREAD_ATTR_GETGUARDSIZE_H
#include <pthread.h>
namespace __llvm_libc {
int pthread_attr_getguardsize(const pthread_attr_t *__restrict attr,
size_t *__restrict guardsize);
} // namespace __llvm_libc
#endif // LLVM_LIBC_SRC_PTHREAD_PTHREAD_ATTR_GETGUARDSIZE_H

View File

@ -0,0 +1,25 @@
//===-- Implementation of the pthread_attr_getstack -----------------===//
//
// 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_attr_getstack.h"
#include "src/__support/common.h"
#include <pthread.h>
namespace __llvm_libc {
LLVM_LIBC_FUNCTION(int, pthread_attr_getstack,
(const pthread_attr_t *__restrict attr,
void **__restrict stack, size_t *__restrict stacksize)) {
*stack = attr->__stack;
*stacksize = attr->__stacksize;
return 0;
}
} // namespace __llvm_libc

View File

@ -0,0 +1,22 @@
//===-- Implementation header for pthread_attr_getstack ---------*- 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_ATTR_GETSTACK_H
#define LLVM_LIBC_SRC_PTHREAD_PTHREAD_ATTR_GETSTACK_H
#include <pthread.h>
namespace __llvm_libc {
int pthread_attr_getstack(const pthread_attr_t *__restrict attr,
void **__restrict stack,
size_t *__restrict stacksize);
} // namespace __llvm_libc
#endif // LLVM_LIBC_SRC_PTHREAD_PTHREAD_ATTR_GETSTACK_H

View File

@ -0,0 +1,24 @@
//===-- Implementation of the pthread_attr_getstacksize -----------------===//
//
// 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_attr_getstacksize.h"
#include "src/__support/common.h"
#include <pthread.h>
namespace __llvm_libc {
LLVM_LIBC_FUNCTION(int, pthread_attr_getstacksize,
(const pthread_attr_t *__restrict attr,
size_t *__restrict stacksize)) {
*stacksize = attr->__stacksize;
return 0;
}
} // namespace __llvm_libc

View File

@ -0,0 +1,21 @@
//===-- Implementation header for pthread_attr_getstacksize -----*- 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_ATTR_GETSTACKSIZE_H
#define LLVM_LIBC_SRC_PTHREAD_PTHREAD_ATTR_GETSTACKSIZE_H
#include <pthread.h>
namespace __llvm_libc {
int pthread_attr_getstacksize(const pthread_attr_t *__restrict attr,
size_t *__restrict stacksize);
} // namespace __llvm_libc
#endif // LLVM_LIBC_SRC_PTHREAD_PTHREAD_ATTR_GETSTACKSIZE_H

View File

@ -0,0 +1,28 @@
//===-- Implementation of the pthread_attr_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_attr_init.h"
#include "src/__support/common.h"
#include <linux/param.h> // For EXEC_PAGESIZE.
#include <pthread.h>
namespace __llvm_libc {
LLVM_LIBC_FUNCTION(int, pthread_attr_init, (pthread_attr_t * attr)) {
*attr = pthread_attr_t{
false, // Not detached
nullptr, // Let the thread manage its stack
1 << 16, // 64KB stack size
EXEC_PAGESIZE, // Default page size for the guard size.
};
return 0;
}
} // namespace __llvm_libc

View File

@ -0,0 +1,20 @@
//===-- Implementation header for pthread_attr_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_ATTR_INIT_H
#define LLVM_LIBC_SRC_PTHREAD_PTHREAD_ATTR_INIT_H
#include <pthread.h>
namespace __llvm_libc {
int pthread_attr_init(pthread_attr_t *attr);
} // namespace __llvm_libc
#endif // LLVM_LIBC_SRC_PTHREAD_PTHREAD_ATTR_INIT_H

View File

@ -0,0 +1,27 @@
//===-- Implementation of the pthread_attr_setdetachstate -----------------===//
//
// 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_attr_setdetachstate.h"
#include "src/__support/common.h"
#include <errno.h>
#include <pthread.h>
namespace __llvm_libc {
LLVM_LIBC_FUNCTION(int, pthread_attr_setdetachstate,
(pthread_attr_t * attr, int detachstate)) {
if (detachstate != PTHREAD_CREATE_DETACHED &&
detachstate != PTHREAD_CREATE_JOINABLE)
return EINVAL;
attr->__detachstate = detachstate;
return 0;
}
} // namespace __llvm_libc

View File

@ -0,0 +1,20 @@
//===-- Implementation header for pthread_attr_setdetachstate ---*- 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_ATTR_SETDETACHSTATE_H
#define LLVM_LIBC_SRC_PTHREAD_PTHREAD_ATTR_SETDETACHSTATE_H
#include <pthread.h>
namespace __llvm_libc {
int pthread_attr_setdetachstate(pthread_attr_t *attr, int detach_state);
} // namespace __llvm_libc
#endif // LLVM_LIBC_SRC_PTHREAD_PTHREAD_ATTR_SETDETACHSTATE_H

View File

@ -0,0 +1,27 @@
//===-- Implementation of the pthread_attr_setguardsize -----------------===//
//
// 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_attr_setguardsize.h"
#include "src/__support/common.h"
#include <errno.h>
#include <linux/param.h> // For EXEC_PAGESIZE.
#include <pthread.h>
namespace __llvm_libc {
LLVM_LIBC_FUNCTION(int, pthread_attr_setguardsize,
(pthread_attr_t *__restrict attr, size_t guardsize)) {
if (guardsize % EXEC_PAGESIZE != 0)
return EINVAL;
attr->__guardsize = guardsize;
return 0;
}
} // namespace __llvm_libc

View File

@ -0,0 +1,20 @@
//===-- Implementation header for pthread_attr_setguardsize -----*- 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_ATTR_SETGUARDSIZE_H
#define LLVM_LIBC_SRC_PTHREAD_PTHREAD_ATTR_SETGUARDSIZE_H
#include <pthread.h>
namespace __llvm_libc {
int pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize);
} // namespace __llvm_libc
#endif // LLVM_LIBC_SRC_PTHREAD_PTHREAD_ATTR_SETGUARDSIZE_H

View File

@ -0,0 +1,33 @@
//===-- Implementation of the pthread_attr_setstack -----------------===//
//
// 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_attr_setstack.h"
#include "src/__support/common.h"
#include <errno.h>
#include <linux/param.h> // For EXEC_PAGESIZE.
#include <pthread.h>
#include <stdint.h>
namespace __llvm_libc {
LLVM_LIBC_FUNCTION(int, pthread_attr_setstack,
(pthread_attr_t *__restrict attr, void *stack,
size_t stacksize)) {
if (stacksize < PTHREAD_STACK_MIN)
return EINVAL;
uintptr_t stackaddr = reinterpret_cast<uintptr_t>(stack);
if ((stackaddr % 16 != 0) || ((stackaddr + stacksize) % 16 != 0))
return EINVAL;
attr->__stack = stack;
attr->__stacksize = stacksize;
return 0;
}
} // namespace __llvm_libc

View File

@ -0,0 +1,20 @@
//===-- Implementation header for pthread_attr_setstack ---------*- 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_ATTR_SETSTACK_H
#define LLVM_LIBC_SRC_PTHREAD_PTHREAD_ATTR_SETSTACK_H
#include <pthread.h>
namespace __llvm_libc {
int pthread_attr_setstack(pthread_attr_t *attr, void *stack, size_t stacksize);
} // namespace __llvm_libc
#endif // LLVM_LIBC_SRC_PTHREAD_PTHREAD_ATTR_SETSTACK_H

View File

@ -0,0 +1,28 @@
//===-- Implementation of the pthread_attr_setstacksize -----------------===//
//
// 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_attr_setstacksize.h"
#include "src/__support/common.h"
#include <errno.h>
#include <linux/param.h> // For EXEC_PAGESIZE.
#include <pthread.h>
namespace __llvm_libc {
LLVM_LIBC_FUNCTION(int, pthread_attr_setstacksize,
(pthread_attr_t *__restrict attr, size_t stacksize)) {
if (stacksize < PTHREAD_STACK_MIN)
return EINVAL;
attr->__stack = nullptr;
attr->__stacksize = stacksize;
return 0;
}
} // namespace __llvm_libc

View File

@ -0,0 +1,20 @@
//===-- Implementation header for pthread_attr_setstacksize -----*- 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_ATTR_SETSTACKSIZE_H
#define LLVM_LIBC_SRC_PTHREAD_PTHREAD_ATTR_SETSTACKSIZE_H
#include <pthread.h>
namespace __llvm_libc {
int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize);
} // namespace __llvm_libc
#endif // LLVM_LIBC_SRC_PTHREAD_PTHREAD_ATTR_SETSTACKSIZE_H

View File

@ -1,5 +1,26 @@
add_libc_testsuite(libc_pthread_unittests)
add_libc_unittest(
pthread_attr_test
SUITE
libc_pthread_unittests
SRCS
pthread_attr_test.cpp
DEPENDS
libc.include.errno
libc.include.pthread
libc.src.pthread.pthread_attr_destroy
libc.src.pthread.pthread_attr_init
libc.src.pthread.pthread_attr_getdetachstate
libc.src.pthread.pthread_attr_getguardsize
libc.src.pthread.pthread_attr_getstacksize
libc.src.pthread.pthread_attr_getstack
libc.src.pthread.pthread_attr_setdetachstate
libc.src.pthread.pthread_attr_setguardsize
libc.src.pthread.pthread_attr_setstacksize
libc.src.pthread.pthread_attr_setstack
)
add_libc_unittest(
pthread_mutexattr_test
SUITE

View File

@ -0,0 +1,113 @@
//===-- Unittests for pthread_attr_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_attr_destroy.h"
#include "src/pthread/pthread_attr_getdetachstate.h"
#include "src/pthread/pthread_attr_getguardsize.h"
#include "src/pthread/pthread_attr_getstack.h"
#include "src/pthread/pthread_attr_getstacksize.h"
#include "src/pthread/pthread_attr_init.h"
#include "src/pthread/pthread_attr_setdetachstate.h"
#include "src/pthread/pthread_attr_setguardsize.h"
#include "src/pthread/pthread_attr_setstack.h"
#include "src/pthread/pthread_attr_setstacksize.h"
#include "utils/UnitTest/Test.h"
#include <errno.h>
#include <linux/param.h> // For EXEC_PAGESIZE.
#include <pthread.h>
TEST(LlvmLibcPThreadAttrTest, InitAndDestroy) {
pthread_attr_t attr;
ASSERT_EQ(__llvm_libc::pthread_attr_init(&attr), 0);
int detachstate;
ASSERT_EQ(__llvm_libc::pthread_attr_getdetachstate(&attr, &detachstate), 0);
ASSERT_EQ(detachstate, int(PTHREAD_CREATE_JOINABLE));
size_t guardsize;
ASSERT_EQ(__llvm_libc::pthread_attr_getguardsize(&attr, &guardsize), 0);
ASSERT_EQ(guardsize, size_t(EXEC_PAGESIZE));
ASSERT_EQ(__llvm_libc::pthread_attr_destroy(&attr), 0);
}
TEST(LlvmLibcPThreadattrTest, SetAndGetDetachState) {
pthread_attr_t attr;
ASSERT_EQ(__llvm_libc::pthread_attr_init(&attr), 0);
int detachstate;
__llvm_libc::pthread_attr_getdetachstate(&attr, &detachstate);
ASSERT_EQ(detachstate, int(PTHREAD_CREATE_JOINABLE));
ASSERT_EQ(
__llvm_libc::pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED),
0);
ASSERT_EQ(__llvm_libc::pthread_attr_getdetachstate(&attr, &detachstate), 0);
ASSERT_EQ(detachstate, int(PTHREAD_CREATE_DETACHED));
ASSERT_EQ(__llvm_libc::pthread_attr_setdetachstate(&attr, 0xBAD), EINVAL);
ASSERT_EQ(__llvm_libc::pthread_attr_destroy(&attr), 0);
}
TEST(LlvmLibcPThreadattrTest, SetAndGetGuardSize) {
pthread_attr_t attr;
ASSERT_EQ(__llvm_libc::pthread_attr_init(&attr), 0);
size_t guardsize;
ASSERT_EQ(__llvm_libc::pthread_attr_getguardsize(&attr, &guardsize), 0);
ASSERT_EQ(guardsize, size_t(EXEC_PAGESIZE));
ASSERT_EQ(__llvm_libc::pthread_attr_setguardsize(&attr, 2 * EXEC_PAGESIZE),
0);
ASSERT_EQ(__llvm_libc::pthread_attr_getguardsize(&attr, &guardsize), 0);
ASSERT_EQ(guardsize, size_t(2 * EXEC_PAGESIZE));
ASSERT_EQ(__llvm_libc::pthread_attr_setguardsize(&attr, EXEC_PAGESIZE / 2),
EINVAL);
ASSERT_EQ(__llvm_libc::pthread_attr_destroy(&attr), 0);
}
TEST(LlvmLibcPThreadattrTest, SetAndGetStackSize) {
pthread_attr_t attr;
ASSERT_EQ(__llvm_libc::pthread_attr_init(&attr), 0);
size_t stacksize;
ASSERT_EQ(
__llvm_libc::pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN << 2), 0);
ASSERT_EQ(__llvm_libc::pthread_attr_getstacksize(&attr, &stacksize), 0);
ASSERT_EQ(stacksize, size_t(PTHREAD_STACK_MIN << 2));
ASSERT_EQ(
__llvm_libc::pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN / 2),
EINVAL);
ASSERT_EQ(__llvm_libc::pthread_attr_destroy(&attr), 0);
}
TEST(LlvmLibcPThreadattrTest, SetAndGetStack) {
pthread_attr_t attr;
ASSERT_EQ(__llvm_libc::pthread_attr_init(&attr), 0);
void *stack;
size_t stacksize;
ASSERT_EQ(
__llvm_libc::pthread_attr_setstack(&attr, 0, PTHREAD_STACK_MIN << 2), 0);
ASSERT_EQ(__llvm_libc::pthread_attr_getstack(&attr, &stack, &stacksize), 0);
ASSERT_EQ(stacksize, size_t(PTHREAD_STACK_MIN << 2));
ASSERT_EQ(reinterpret_cast<uintptr_t>(stack), uintptr_t(0));
ASSERT_EQ(__llvm_libc::pthread_attr_setstack(
&attr, reinterpret_cast<void *>(1), PTHREAD_STACK_MIN << 2),
EINVAL);
ASSERT_EQ(__llvm_libc::pthread_attr_setstack(&attr, 0, PTHREAD_STACK_MIN / 2),
EINVAL);
ASSERT_EQ(__llvm_libc::pthread_attr_destroy(&attr), 0);
}