1 /*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "gtest/gtest.h"
18
19 #include <errno.h>
20 #include <pthread.h>
21
ThreadCreateJoinFunc(void * arg)22 void* ThreadCreateJoinFunc(void* arg) {
23 *reinterpret_cast<int*>(arg) = 1;
24 return nullptr;
25 }
26
TEST(Thread,CreateJoin)27 TEST(Thread, CreateJoin) {
28 pthread_t thread;
29 int test_variable = 0;
30 ASSERT_EQ(pthread_create(
31 &thread, nullptr, ThreadCreateJoinFunc, reinterpret_cast<void*>(&test_variable)),
32 0);
33 ASSERT_EQ(pthread_join(thread, nullptr), 0);
34 ASSERT_EQ(test_variable, 1);
35 }
36
IncrementCounter(void * arg)37 void IncrementCounter(void* arg) {
38 int* counter = reinterpret_cast<int*>(arg);
39 (*counter)++;
40 }
41
ThreadKeyFunc(void * arg)42 void* ThreadKeyFunc(void* arg) {
43 pthread_key_t* key = reinterpret_cast<pthread_key_t*>(arg);
44 if (pthread_getspecific(*key) != nullptr) {
45 return nullptr;
46 }
47 int* count = new int(0);
48 if (pthread_setspecific(*key, count) != 0) {
49 delete count;
50 return nullptr;
51 }
52 return reinterpret_cast<void*>(count);
53 }
54
CleanupHandler(void * arg)55 void CleanupHandler(void* arg) {
56 int* cleanup = reinterpret_cast<int*>(arg);
57 *cleanup = 239;
58 }
59
ThreadCleanupFunc(void * arg)60 void* ThreadCleanupFunc(void* arg) {
61 int* var = reinterpret_cast<int*>(arg);
62
63 *var = 0;
64 pthread_cleanup_push(CleanupHandler, var);
65 pthread_cleanup_pop(1);
66 EXPECT_EQ(*var, 239);
67
68 *var = 1;
69 pthread_cleanup_push(CleanupHandler, var);
70 pthread_cleanup_pop(0);
71 EXPECT_EQ(*var, 1);
72
73 *var = 2;
74 pthread_cleanup_push(CleanupHandler, var);
75 pthread_exit(nullptr);
76 pthread_cleanup_pop(0);
77
78 return nullptr;
79 }
80
TEST(Thread,Keys)81 TEST(Thread, Keys) {
82 pthread_key_t key;
83 pthread_t thread;
84 int count = 0;
85 int* thread_count;
86 ASSERT_EQ(pthread_key_create(&key, IncrementCounter), 0);
87 ASSERT_EQ(pthread_setspecific(key, &count), 0);
88 ASSERT_EQ(reinterpret_cast<void*>(&count), pthread_getspecific(key));
89 ASSERT_EQ(pthread_create(&thread, nullptr, ThreadKeyFunc, reinterpret_cast<void*>(&key)), 0);
90 ASSERT_EQ(pthread_join(thread, reinterpret_cast<void**>(&thread_count)), 0);
91 // delete does not call destructor.
92 ASSERT_EQ(pthread_key_delete(key), 0);
93 EXPECT_EQ(count, 0);
94 ASSERT_NE(thread_count, nullptr);
95 EXPECT_EQ(*thread_count, 1);
96 delete thread_count;
97 }
98
99 int g_thread_once_var = 0;
100
ThreadOnceFunction()101 void ThreadOnceFunction() {
102 g_thread_once_var++;
103 }
104
TEST(Thread,Once)105 TEST(Thread, Once) {
106 if (g_thread_once_var > 0) {
107 GTEST_SKIP() << "This test cannot be repeated";
108 }
109 static pthread_once_t once = PTHREAD_ONCE_INIT;
110 ASSERT_EQ(pthread_once(&once, ThreadOnceFunction), 0);
111 ASSERT_EQ(g_thread_once_var, 1);
112 ASSERT_EQ(pthread_once(&once, ThreadOnceFunction), 0);
113 ASSERT_EQ(g_thread_once_var, 1);
114 }
115
TEST(Thread,PThreadAttr)116 TEST(Thread, PThreadAttr) {
117 pthread_attr_t attr;
118 int state;
119 size_t stack_size;
120 pthread_attr_init(&attr);
121 ASSERT_EQ(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED), 0);
122 ASSERT_EQ(pthread_attr_getdetachstate(&attr, &state), 0);
123 EXPECT_EQ(state, PTHREAD_CREATE_DETACHED);
124 ASSERT_EQ(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE), 0);
125 ASSERT_EQ(pthread_attr_getdetachstate(&attr, &state), 0);
126 EXPECT_EQ(state, PTHREAD_CREATE_JOINABLE);
127 ASSERT_EQ(pthread_attr_setstacksize(&attr, 16 * 1024U), 0);
128 ASSERT_EQ(pthread_attr_getstacksize(&attr, &stack_size), 0);
129 ASSERT_EQ(stack_size, 16 * 1024U);
130 ASSERT_EQ(pthread_attr_destroy(&attr), 0);
131 }
132
TEST(Thread,CreateWithAttrs)133 TEST(Thread, CreateWithAttrs) {
134 pthread_t thread;
135 pthread_attr_t attr;
136 int var = 0;
137 pthread_attr_init(&attr);
138 ASSERT_EQ(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE), 0);
139 ASSERT_EQ(pthread_attr_setstacksize(&attr, 16 * 1024), 0);
140 ASSERT_EQ(pthread_create(&thread, &attr, ThreadCreateJoinFunc, &var), 0);
141 ASSERT_EQ(pthread_attr_destroy(&attr), 0);
142 ASSERT_EQ(pthread_join(thread, nullptr), 0);
143 ASSERT_EQ(var, 1);
144 }
145
TEST(Thread,PushPop)146 TEST(Thread, PushPop) {
147 int var = 0;
148 pthread_t thread;
149 ASSERT_EQ(pthread_create(&thread, nullptr, ThreadCleanupFunc, &var), 0);
150 ASSERT_EQ(pthread_join(thread, nullptr), 0);
151 EXPECT_EQ(var, 239);
152 }
153
StoreTid(void * param)154 void* StoreTid(void* param) {
155 int* tid_ptr = reinterpret_cast<int*>(param);
156 *tid_ptr = gettid();
157 return nullptr;
158 }
159
TEST(Thread,GetTid)160 TEST(Thread, GetTid) {
161 pid_t tid = gettid();
162 ASSERT_GT(tid, 0);
163 ASSERT_EQ(tid, gettid());
164 pid_t background_tid = 0;
165 pthread_t thread;
166 ASSERT_EQ(pthread_create(&thread, nullptr, StoreTid, reinterpret_cast<void*>(&background_tid)),
167 0);
168 ASSERT_EQ(pthread_join(thread, nullptr), 0);
169 ASSERT_NE(background_tid, tid);
170 }
171
TEST(Thread,GetSetPriority)172 TEST(Thread, GetSetPriority) {
173 int orig_priority = getpriority(PRIO_PROCESS, gettid());
174 ASSERT_LE(orig_priority, 19); // The lowest priority.
175 ASSERT_GE(orig_priority, -20); // The highest priority.
176
177 // Make sure there is room to lower the priority in the test.
178 // Priority grows toward the negative numbers.
179 // Note, that we may not have the permission (CAP_SYS_NICE) to set a higher priority.
180 if (orig_priority + 2 > 19) {
181 GTEST_SKIP() << "No room to further lower the priority, skipping";
182 }
183
184 ASSERT_EQ(setpriority(PRIO_PROCESS, 0, orig_priority + 1), 0);
185 ASSERT_EQ(getpriority(PRIO_PROCESS, gettid()), orig_priority + 1);
186 ASSERT_EQ(setpriority(PRIO_PROCESS, gettid(), orig_priority + 2), 0);
187 ASSERT_EQ(getpriority(PRIO_PROCESS, gettid()), orig_priority + 2);
188
189 // -1 |who| must fail.
190 errno = 0;
191 ASSERT_EQ(setpriority(PRIO_PROCESS, -1, 0), -1);
192 ASSERT_EQ(errno, ESRCH);
193
194 // Try to restore the original priority. May fail if we don't have the permission.
195 setpriority(PRIO_PROCESS, gettid(), orig_priority);
196 }
197