1 //===-- Unittests for pthread_attr_t --------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "src/pthread/pthread_attr_destroy.h"
10 #include "src/pthread/pthread_attr_getdetachstate.h"
11 #include "src/pthread/pthread_attr_getguardsize.h"
12 #include "src/pthread/pthread_attr_getstack.h"
13 #include "src/pthread/pthread_attr_getstacksize.h"
14 #include "src/pthread/pthread_attr_init.h"
15 #include "src/pthread/pthread_attr_setdetachstate.h"
16 #include "src/pthread/pthread_attr_setguardsize.h"
17 #include "src/pthread/pthread_attr_setstack.h"
18 #include "src/pthread/pthread_attr_setstacksize.h"
20 #include "test/UnitTest/Test.h"
23 #include <linux/param.h> // For EXEC_PAGESIZE.
26 TEST(LlvmLibcPThreadAttrTest
, InitAndDestroy
) {
28 ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_init(&attr
), 0);
31 ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_getdetachstate(&attr
, &detachstate
),
33 ASSERT_EQ(detachstate
, int(PTHREAD_CREATE_JOINABLE
));
36 ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_getguardsize(&attr
, &guardsize
), 0);
37 ASSERT_EQ(guardsize
, size_t(EXEC_PAGESIZE
));
39 ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_destroy(&attr
), 0);
42 TEST(LlvmLibcPThreadattrTest
, SetAndGetDetachState
) {
44 ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_init(&attr
), 0);
47 LIBC_NAMESPACE::pthread_attr_getdetachstate(&attr
, &detachstate
);
48 ASSERT_EQ(detachstate
, int(PTHREAD_CREATE_JOINABLE
));
49 ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_setdetachstate(
50 &attr
, PTHREAD_CREATE_DETACHED
),
52 ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_getdetachstate(&attr
, &detachstate
),
54 ASSERT_EQ(detachstate
, int(PTHREAD_CREATE_DETACHED
));
56 ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_setdetachstate(&attr
, 0xBAD), EINVAL
);
58 ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_destroy(&attr
), 0);
61 TEST(LlvmLibcPThreadattrTest
, SetAndGetGuardSize
) {
63 ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_init(&attr
), 0);
66 ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_getguardsize(&attr
, &guardsize
), 0);
67 ASSERT_EQ(guardsize
, size_t(EXEC_PAGESIZE
));
68 ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_setguardsize(&attr
, 2 * EXEC_PAGESIZE
),
70 ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_getguardsize(&attr
, &guardsize
), 0);
71 ASSERT_EQ(guardsize
, size_t(2 * EXEC_PAGESIZE
));
73 ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_setguardsize(&attr
, EXEC_PAGESIZE
/ 2),
76 ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_destroy(&attr
), 0);
79 TEST(LlvmLibcPThreadattrTest
, SetAndGetStackSize
) {
81 ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_init(&attr
), 0);
85 LIBC_NAMESPACE::pthread_attr_setstacksize(&attr
, PTHREAD_STACK_MIN
<< 2),
87 ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_getstacksize(&attr
, &stacksize
), 0);
88 ASSERT_EQ(stacksize
, size_t(PTHREAD_STACK_MIN
<< 2));
91 LIBC_NAMESPACE::pthread_attr_setstacksize(&attr
, PTHREAD_STACK_MIN
/ 2),
94 ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_destroy(&attr
), 0);
97 TEST(LlvmLibcPThreadattrTest
, SetAndGetStack
) {
99 ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_init(&attr
), 0);
104 LIBC_NAMESPACE::pthread_attr_setstack(&attr
, 0, PTHREAD_STACK_MIN
<< 2),
106 ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_getstack(&attr
, &stack
, &stacksize
),
108 ASSERT_EQ(stacksize
, size_t(PTHREAD_STACK_MIN
<< 2));
109 ASSERT_EQ(reinterpret_cast<uintptr_t>(stack
), uintptr_t(0));
111 ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_setstack(
112 &attr
, reinterpret_cast<void *>(1), PTHREAD_STACK_MIN
<< 2),
115 LIBC_NAMESPACE::pthread_attr_setstack(&attr
, 0, PTHREAD_STACK_MIN
/ 2),
118 ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_destroy(&attr
), 0);