1 | //===-- Unittests for pthread_attr_t --------------------------------------===// |
2 | // |
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 |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
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" |
19 | |
20 | #include "test/UnitTest/Test.h" |
21 | |
22 | #include <errno.h> |
23 | #include <linux/param.h> // For EXEC_PAGESIZE. |
24 | #include <pthread.h> |
25 | |
26 | TEST(LlvmLibcPThreadAttrTest, InitAndDestroy) { |
27 | pthread_attr_t attr; |
28 | ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_init(&attr), 0); |
29 | |
30 | int detachstate; |
31 | ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_getdetachstate(&attr, &detachstate), |
32 | 0); |
33 | ASSERT_EQ(detachstate, int(PTHREAD_CREATE_JOINABLE)); |
34 | |
35 | size_t guardsize; |
36 | ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_getguardsize(&attr, &guardsize), 0); |
37 | ASSERT_EQ(guardsize, size_t(EXEC_PAGESIZE)); |
38 | |
39 | ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_destroy(&attr), 0); |
40 | } |
41 | |
42 | TEST(LlvmLibcPThreadattrTest, SetAndGetDetachState) { |
43 | pthread_attr_t attr; |
44 | ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_init(&attr), 0); |
45 | |
46 | int detachstate; |
47 | LIBC_NAMESPACE::pthread_attr_getdetachstate(attr: &attr, detach_state: &detachstate); |
48 | ASSERT_EQ(detachstate, int(PTHREAD_CREATE_JOINABLE)); |
49 | ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_setdetachstate( |
50 | &attr, PTHREAD_CREATE_DETACHED), |
51 | 0); |
52 | ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_getdetachstate(&attr, &detachstate), |
53 | 0); |
54 | ASSERT_EQ(detachstate, int(PTHREAD_CREATE_DETACHED)); |
55 | |
56 | ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_setdetachstate(&attr, 0xBAD), EINVAL); |
57 | |
58 | ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_destroy(&attr), 0); |
59 | } |
60 | |
61 | TEST(LlvmLibcPThreadattrTest, SetAndGetGuardSize) { |
62 | pthread_attr_t attr; |
63 | ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_init(&attr), 0); |
64 | |
65 | size_t guardsize; |
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), |
69 | 0); |
70 | ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_getguardsize(&attr, &guardsize), 0); |
71 | ASSERT_EQ(guardsize, size_t(2 * EXEC_PAGESIZE)); |
72 | |
73 | ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_setguardsize(&attr, EXEC_PAGESIZE / 2), |
74 | EINVAL); |
75 | |
76 | ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_destroy(&attr), 0); |
77 | } |
78 | |
79 | TEST(LlvmLibcPThreadattrTest, SetAndGetStackSize) { |
80 | pthread_attr_t attr; |
81 | ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_init(&attr), 0); |
82 | |
83 | size_t stacksize; |
84 | ASSERT_EQ( |
85 | LIBC_NAMESPACE::pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN << 2), |
86 | 0); |
87 | ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_getstacksize(&attr, &stacksize), 0); |
88 | ASSERT_EQ(stacksize, size_t(PTHREAD_STACK_MIN << 2)); |
89 | |
90 | ASSERT_EQ( |
91 | LIBC_NAMESPACE::pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN / 2), |
92 | EINVAL); |
93 | |
94 | ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_destroy(&attr), 0); |
95 | } |
96 | |
97 | TEST(LlvmLibcPThreadattrTest, SetAndGetStack) { |
98 | pthread_attr_t attr; |
99 | ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_init(&attr), 0); |
100 | |
101 | void *stack; |
102 | size_t stacksize; |
103 | ASSERT_EQ( |
104 | LIBC_NAMESPACE::pthread_attr_setstack(&attr, 0, PTHREAD_STACK_MIN << 2), |
105 | 0); |
106 | ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_getstack(&attr, &stack, &stacksize), |
107 | 0); |
108 | ASSERT_EQ(stacksize, size_t(PTHREAD_STACK_MIN << 2)); |
109 | ASSERT_EQ(reinterpret_cast<uintptr_t>(stack), uintptr_t(0)); |
110 | |
111 | ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_setstack( |
112 | &attr, reinterpret_cast<void *>(1), PTHREAD_STACK_MIN << 2), |
113 | EINVAL); |
114 | ASSERT_EQ( |
115 | LIBC_NAMESPACE::pthread_attr_setstack(&attr, 0, PTHREAD_STACK_MIN / 2), |
116 | EINVAL); |
117 | |
118 | ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_destroy(&attr), 0); |
119 | } |
120 | |