blob: c7dbdc78c433d668ea383ecd6725ba6b049b5fc5 [file] [log] [blame]
Elliott Hughesbfeab1b2012-09-05 17:47:37 -07001/*
2 * Copyright (C) 2012 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>
Elliott Hughesb95cf0d2013-07-15 14:51:07 -070020#include <limits.h>
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070021#include <pthread.h>
Elliott Hughes4d014e12012-09-07 16:47:54 -070022#include <unistd.h>
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070023
24TEST(pthread, pthread_key_create) {
25 pthread_key_t key;
26 ASSERT_EQ(0, pthread_key_create(&key, NULL));
27 ASSERT_EQ(0, pthread_key_delete(key));
28 // Can't delete a key that's already been deleted.
29 ASSERT_EQ(EINVAL, pthread_key_delete(key));
30}
Elliott Hughes4d014e12012-09-07 16:47:54 -070031
Elliott Hughes3e898472013-02-12 16:40:24 +000032#if !defined(__GLIBC__) // glibc uses keys internally that its sysconf value doesn't account for.
Elliott Hughes44b53ad2013-02-11 20:18:47 +000033TEST(pthread, pthread_key_create_lots) {
34 // We can allocate _SC_THREAD_KEYS_MAX keys.
35 std::vector<pthread_key_t> keys;
36 for (int i = 0; i < sysconf(_SC_THREAD_KEYS_MAX); ++i) {
37 pthread_key_t key;
Elliott Hughes3e898472013-02-12 16:40:24 +000038 // If this fails, it's likely that GLOBAL_INIT_THREAD_LOCAL_BUFFER_COUNT is wrong.
39 ASSERT_EQ(0, pthread_key_create(&key, NULL)) << i << " of " << sysconf(_SC_THREAD_KEYS_MAX);
Elliott Hughes44b53ad2013-02-11 20:18:47 +000040 keys.push_back(key);
41 }
42
43 // ...and that really is the maximum.
44 pthread_key_t key;
45 ASSERT_EQ(EAGAIN, pthread_key_create(&key, NULL));
46
47 // (Don't leak all those keys!)
48 for (size_t i = 0; i < keys.size(); ++i) {
49 ASSERT_EQ(0, pthread_key_delete(keys[i]));
50 }
51}
Elliott Hughes3e898472013-02-12 16:40:24 +000052#endif
Elliott Hughes44b53ad2013-02-11 20:18:47 +000053
Elliott Hughes4d014e12012-09-07 16:47:54 -070054static void* IdFn(void* arg) {
55 return arg;
56}
57
58static void* SleepFn(void* arg) {
59 sleep(reinterpret_cast<unsigned int>(arg));
60 return NULL;
61}
62
Sergey Melnikov10ce9692012-10-26 14:06:43 +040063static void* SpinFn(void* arg) {
64 volatile bool* b = reinterpret_cast<volatile bool*>(arg);
65 while (!*b) {
66 }
67 return NULL;
68}
69
Elliott Hughes4d014e12012-09-07 16:47:54 -070070static void* JoinFn(void* arg) {
71 return reinterpret_cast<void*>(pthread_join(reinterpret_cast<pthread_t>(arg), NULL));
72}
73
Sergey Melnikov10ce9692012-10-26 14:06:43 +040074static void AssertDetached(pthread_t t, bool is_detached) {
75 pthread_attr_t attr;
76 ASSERT_EQ(0, pthread_getattr_np(t, &attr));
77 int detach_state;
78 ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &detach_state));
79 pthread_attr_destroy(&attr);
80 ASSERT_EQ(is_detached, (detach_state == PTHREAD_CREATE_DETACHED));
81}
82
Elliott Hughes9d23e042013-02-15 19:21:51 -080083static void MakeDeadThread(pthread_t& t) {
84 ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, NULL));
85 void* result;
86 ASSERT_EQ(0, pthread_join(t, &result));
87}
88
Elliott Hughes4d014e12012-09-07 16:47:54 -070089TEST(pthread, pthread_create) {
90 void* expected_result = reinterpret_cast<void*>(123);
91 // Can we create a thread?
92 pthread_t t;
93 ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, expected_result));
94 // If we join, do we get the expected value back?
95 void* result;
96 ASSERT_EQ(0, pthread_join(t, &result));
97 ASSERT_EQ(expected_result, result);
98}
99
Elliott Hughes3e898472013-02-12 16:40:24 +0000100TEST(pthread, pthread_create_EAGAIN) {
101 pthread_attr_t attributes;
102 ASSERT_EQ(0, pthread_attr_init(&attributes));
103 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, static_cast<size_t>(-1) & ~(getpagesize() - 1)));
104
105 pthread_t t;
106 ASSERT_EQ(EAGAIN, pthread_create(&t, &attributes, IdFn, NULL));
107}
108
Elliott Hughes4d014e12012-09-07 16:47:54 -0700109TEST(pthread, pthread_no_join_after_detach) {
110 pthread_t t1;
111 ASSERT_EQ(0, pthread_create(&t1, NULL, SleepFn, reinterpret_cast<void*>(5)));
112
113 // After a pthread_detach...
114 ASSERT_EQ(0, pthread_detach(t1));
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400115 AssertDetached(t1, true);
Elliott Hughes4d014e12012-09-07 16:47:54 -0700116
117 // ...pthread_join should fail.
118 void* result;
119 ASSERT_EQ(EINVAL, pthread_join(t1, &result));
120}
121
122TEST(pthread, pthread_no_op_detach_after_join) {
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400123 bool done = false;
124
Elliott Hughes4d014e12012-09-07 16:47:54 -0700125 pthread_t t1;
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400126 ASSERT_EQ(0, pthread_create(&t1, NULL, SpinFn, &done));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700127
128 // If thread 2 is already waiting to join thread 1...
129 pthread_t t2;
130 ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
131
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400132 sleep(1); // (Give t2 a chance to call pthread_join.)
Elliott Hughes4d014e12012-09-07 16:47:54 -0700133
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400134 // ...a call to pthread_detach on thread 1 will "succeed" (silently fail)...
135 ASSERT_EQ(0, pthread_detach(t1));
136 AssertDetached(t1, false);
137
138 done = true;
139
140 // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes).
Elliott Hughes4d014e12012-09-07 16:47:54 -0700141 void* join_result;
142 ASSERT_EQ(0, pthread_join(t2, &join_result));
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400143 ASSERT_EQ(0, reinterpret_cast<int>(join_result));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700144}
Elliott Hughes14f19592012-10-29 10:19:44 -0700145
146TEST(pthread, pthread_join_self) {
147 void* result;
148 ASSERT_EQ(EDEADLK, pthread_join(pthread_self(), &result));
149}
Elliott Hughes4f251be2012-11-01 16:33:29 -0700150
151#if __BIONIC__ // For some reason, gtest on bionic can cope with this but gtest on glibc can't.
152
153static void TestBug37410() {
154 pthread_t t1;
155 ASSERT_EQ(0, pthread_create(&t1, NULL, JoinFn, reinterpret_cast<void*>(pthread_self())));
156 pthread_exit(NULL);
157}
158
Elliott Hughes7fd803c2013-02-14 16:33:52 -0800159// Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to
160// run this test (which exits normally) in its own process.
Elliott Hughes4f251be2012-11-01 16:33:29 -0700161TEST(pthread_DeathTest, pthread_bug_37410) {
162 // http://code.google.com/p/android/issues/detail?id=37410
163 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
Elliott Hughes7fd803c2013-02-14 16:33:52 -0800164 ASSERT_EXIT(TestBug37410(), ::testing::ExitedWithCode(0), "");
Elliott Hughes4f251be2012-11-01 16:33:29 -0700165}
166#endif
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800167
168static void* SignalHandlerFn(void* arg) {
169 sigset_t wait_set;
170 sigfillset(&wait_set);
171 return reinterpret_cast<void*>(sigwait(&wait_set, reinterpret_cast<int*>(arg)));
172}
173
174TEST(pthread, pthread_sigmask) {
175 // Block SIGUSR1.
176 sigset_t set;
177 sigemptyset(&set);
178 sigaddset(&set, SIGUSR1);
179 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &set, NULL));
180
181 // Spawn a thread that calls sigwait and tells us what it received.
182 pthread_t signal_thread;
183 int received_signal = -1;
184 ASSERT_EQ(0, pthread_create(&signal_thread, NULL, SignalHandlerFn, &received_signal));
185
186 // Send that thread SIGUSR1.
187 pthread_kill(signal_thread, SIGUSR1);
188
189 // See what it got.
190 void* join_result;
191 ASSERT_EQ(0, pthread_join(signal_thread, &join_result));
192 ASSERT_EQ(SIGUSR1, received_signal);
193 ASSERT_EQ(0, reinterpret_cast<int>(join_result));
194}
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800195
Elliott Hughes3e898472013-02-12 16:40:24 +0000196#if __BIONIC__
Elliott Hughes40eabe22013-02-14 18:59:37 -0800197extern "C" int __pthread_clone(void* (*fn)(void*), void* child_stack, int flags, void* arg);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800198TEST(pthread, __pthread_clone) {
199 uintptr_t fake_child_stack[16];
200 errno = 0;
201 ASSERT_EQ(-1, __pthread_clone(NULL, &fake_child_stack[0], CLONE_THREAD, NULL));
202 ASSERT_EQ(EINVAL, errno);
203}
204#endif
Elliott Hughes3e898472013-02-12 16:40:24 +0000205
Elliott Hughes9701d4b2013-02-12 17:20:42 -0800206#if __BIONIC__ // Not all build servers have a new enough glibc? TODO: remove when they're on gprecise.
Elliott Hughes3e898472013-02-12 16:40:24 +0000207TEST(pthread, pthread_setname_np__too_long) {
208 ASSERT_EQ(ERANGE, pthread_setname_np(pthread_self(), "this name is far too long for linux"));
209}
Elliott Hughes9701d4b2013-02-12 17:20:42 -0800210#endif
Elliott Hughes3e898472013-02-12 16:40:24 +0000211
Elliott Hughes9701d4b2013-02-12 17:20:42 -0800212#if __BIONIC__ // Not all build servers have a new enough glibc? TODO: remove when they're on gprecise.
Elliott Hughes3e898472013-02-12 16:40:24 +0000213TEST(pthread, pthread_setname_np__self) {
214 ASSERT_EQ(0, pthread_setname_np(pthread_self(), "short 1"));
215}
Elliott Hughes9701d4b2013-02-12 17:20:42 -0800216#endif
Elliott Hughes3e898472013-02-12 16:40:24 +0000217
Elliott Hughes9701d4b2013-02-12 17:20:42 -0800218#if __BIONIC__ // Not all build servers have a new enough glibc? TODO: remove when they're on gprecise.
Elliott Hughes3e898472013-02-12 16:40:24 +0000219TEST(pthread, pthread_setname_np__other) {
Elliott Hughes40eabe22013-02-14 18:59:37 -0800220 // Emulator kernels don't currently support setting the name of other threads.
221 char* filename = NULL;
222 asprintf(&filename, "/proc/self/task/%d/comm", gettid());
223 struct stat sb;
224 bool has_comm = (stat(filename, &sb) != -1);
225 free(filename);
226
227 if (has_comm) {
228 pthread_t t1;
229 ASSERT_EQ(0, pthread_create(&t1, NULL, SleepFn, reinterpret_cast<void*>(5)));
230 ASSERT_EQ(0, pthread_setname_np(t1, "short 2"));
231 } else {
232 fprintf(stderr, "skipping test: this kernel doesn't have /proc/self/task/tid/comm files!\n");
233 }
Elliott Hughes3e898472013-02-12 16:40:24 +0000234}
Elliott Hughes9701d4b2013-02-12 17:20:42 -0800235#endif
Elliott Hughes3e898472013-02-12 16:40:24 +0000236
Elliott Hughes9701d4b2013-02-12 17:20:42 -0800237#if __BIONIC__ // Not all build servers have a new enough glibc? TODO: remove when they're on gprecise.
Elliott Hughes3e898472013-02-12 16:40:24 +0000238TEST(pthread, pthread_setname_np__no_such_thread) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800239 pthread_t dead_thread;
240 MakeDeadThread(dead_thread);
Elliott Hughes3e898472013-02-12 16:40:24 +0000241
242 // Call pthread_setname_np after thread has already exited.
Elliott Hughesa41ba2f2013-03-21 20:02:35 -0700243 ASSERT_EQ(ESRCH, pthread_setname_np(dead_thread, "short 3"));
Elliott Hughes3e898472013-02-12 16:40:24 +0000244}
Elliott Hughes9701d4b2013-02-12 17:20:42 -0800245#endif
Elliott Hughes9d23e042013-02-15 19:21:51 -0800246
247TEST(pthread, pthread_kill__0) {
248 // Signal 0 just tests that the thread exists, so it's safe to call on ourselves.
249 ASSERT_EQ(0, pthread_kill(pthread_self(), 0));
250}
251
252TEST(pthread, pthread_kill__invalid_signal) {
253 ASSERT_EQ(EINVAL, pthread_kill(pthread_self(), -1));
254}
255
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800256static void pthread_kill__in_signal_handler_helper(int signal_number) {
257 static int count = 0;
258 ASSERT_EQ(SIGALRM, signal_number);
259 if (++count == 1) {
260 // Can we call pthread_kill from a signal handler?
261 ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
262 }
263}
264
265TEST(pthread, pthread_kill__in_signal_handler) {
266 struct sigaction action;
267 sigemptyset(&action.sa_mask);
268 action.sa_flags = 0;
269 action.sa_handler = pthread_kill__in_signal_handler_helper;
270 sigaction(SIGALRM, &action, NULL);
271 ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
272}
273
Elliott Hughes9d23e042013-02-15 19:21:51 -0800274TEST(pthread, pthread_detach__no_such_thread) {
275 pthread_t dead_thread;
276 MakeDeadThread(dead_thread);
277
278 ASSERT_EQ(ESRCH, pthread_detach(dead_thread));
279}
280
281TEST(pthread, pthread_getcpuclockid__no_such_thread) {
282 pthread_t dead_thread;
283 MakeDeadThread(dead_thread);
284
285 clockid_t c;
286 ASSERT_EQ(ESRCH, pthread_getcpuclockid(dead_thread, &c));
287}
288
289TEST(pthread, pthread_getschedparam__no_such_thread) {
290 pthread_t dead_thread;
291 MakeDeadThread(dead_thread);
292
293 int policy;
294 sched_param param;
295 ASSERT_EQ(ESRCH, pthread_getschedparam(dead_thread, &policy, &param));
296}
297
298TEST(pthread, pthread_setschedparam__no_such_thread) {
299 pthread_t dead_thread;
300 MakeDeadThread(dead_thread);
301
302 int policy = 0;
303 sched_param param;
304 ASSERT_EQ(ESRCH, pthread_setschedparam(dead_thread, policy, &param));
305}
306
307TEST(pthread, pthread_join__no_such_thread) {
308 pthread_t dead_thread;
309 MakeDeadThread(dead_thread);
310
311 void* result;
312 ASSERT_EQ(ESRCH, pthread_join(dead_thread, &result));
313}
314
315TEST(pthread, pthread_kill__no_such_thread) {
316 pthread_t dead_thread;
317 MakeDeadThread(dead_thread);
318
319 ASSERT_EQ(ESRCH, pthread_kill(dead_thread, 0));
320}
msg5550f020d12013-06-06 14:59:28 -0400321
322TEST(pthread, pthread_join__multijoin) {
323 bool done = false;
324
325 pthread_t t1;
326 ASSERT_EQ(0, pthread_create(&t1, NULL, SpinFn, &done));
327
328 pthread_t t2;
329 ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
330
331 sleep(1); // (Give t2 a chance to call pthread_join.)
332
333 // Multiple joins to the same thread should fail.
334 ASSERT_EQ(EINVAL, pthread_join(t1, NULL));
335
336 done = true;
337
338 // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes).
339 void* join_result;
340 ASSERT_EQ(0, pthread_join(t2, &join_result));
341 ASSERT_EQ(0, reinterpret_cast<int>(join_result));
342}
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700343
344static void* GetActualGuardSizeFn(void* arg) {
345 pthread_attr_t attributes;
346 pthread_getattr_np(pthread_self(), &attributes);
347 pthread_attr_getguardsize(&attributes, reinterpret_cast<size_t*>(arg));
348 return NULL;
349}
350
351static size_t GetActualGuardSize(const pthread_attr_t& attributes) {
352 size_t result;
353 pthread_t t;
354 pthread_create(&t, &attributes, GetActualGuardSizeFn, &result);
355 void* join_result;
356 pthread_join(t, &join_result);
357 return result;
358}
359
360static void* GetActualStackSizeFn(void* arg) {
361 pthread_attr_t attributes;
362 pthread_getattr_np(pthread_self(), &attributes);
363 pthread_attr_getstacksize(&attributes, reinterpret_cast<size_t*>(arg));
364 return NULL;
365}
366
367static size_t GetActualStackSize(const pthread_attr_t& attributes) {
368 size_t result;
369 pthread_t t;
370 pthread_create(&t, &attributes, GetActualStackSizeFn, &result);
371 void* join_result;
372 pthread_join(t, &join_result);
373 return result;
374}
375
376TEST(pthread, pthread_attr_setguardsize) {
377 pthread_attr_t attributes;
378 ASSERT_EQ(0, pthread_attr_init(&attributes));
379
380 // Get the default guard size.
381 size_t default_guard_size;
382 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &default_guard_size));
383
384 // No such thing as too small: will be rounded up to one page by pthread_create.
385 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 128));
386 size_t guard_size;
387 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
388 ASSERT_EQ(128U, guard_size);
389 ASSERT_EQ(4096U, GetActualGuardSize(attributes));
390
391 // Large enough and a multiple of the page size.
392 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024));
393 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
394 ASSERT_EQ(32*1024U, guard_size);
395
396 // Large enough but not a multiple of the page size; will be rounded up by pthread_create.
397 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024 + 1));
398 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
399 ASSERT_EQ(32*1024U + 1, guard_size);
400}
401
402TEST(pthread, pthread_attr_setstacksize) {
403 pthread_attr_t attributes;
404 ASSERT_EQ(0, pthread_attr_init(&attributes));
405
406 // Get the default stack size.
407 size_t default_stack_size;
408 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &default_stack_size));
409
410 // Too small.
411 ASSERT_EQ(EINVAL, pthread_attr_setstacksize(&attributes, 128));
412 size_t stack_size;
413 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
414 ASSERT_EQ(default_stack_size, stack_size);
415 ASSERT_GE(GetActualStackSize(attributes), default_stack_size);
416
417 // Large enough and a multiple of the page size.
418 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024));
419 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
420 ASSERT_EQ(32*1024U, stack_size);
421 ASSERT_EQ(GetActualStackSize(attributes), 32*1024U);
422
423 // Large enough but not a multiple of the page size; will be rounded up by pthread_create.
424 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024 + 1));
425 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
426 ASSERT_EQ(32*1024U + 1, stack_size);
427#if __BIONIC__
428 // Bionic rounds up, which is what POSIX allows.
429 ASSERT_EQ(GetActualStackSize(attributes), (32 + 4)*1024U);
430#else
431 // glibc rounds down, in violation of POSIX. They document this in their BUGS section.
432 ASSERT_EQ(GetActualStackSize(attributes), 32*1024U);
433#endif
434}