blob: f6f61b1228d289c24d6be78ecccbaef472304702 [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 Hughes5b9310e2013-10-02 16:59:05 -070020#include <inttypes.h>
Elliott Hughesb95cf0d2013-07-15 14:51:07 -070021#include <limits.h>
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070022#include <pthread.h>
Elliott Hughes70b24b12013-11-15 11:51:07 -080023#include <sys/mman.h>
Elliott Hughes4d014e12012-09-07 16:47:54 -070024#include <unistd.h>
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070025
26TEST(pthread, pthread_key_create) {
27 pthread_key_t key;
28 ASSERT_EQ(0, pthread_key_create(&key, NULL));
29 ASSERT_EQ(0, pthread_key_delete(key));
30 // Can't delete a key that's already been deleted.
31 ASSERT_EQ(EINVAL, pthread_key_delete(key));
32}
Elliott Hughes4d014e12012-09-07 16:47:54 -070033
Elliott Hughes3e898472013-02-12 16:40:24 +000034#if !defined(__GLIBC__) // glibc uses keys internally that its sysconf value doesn't account for.
Elliott Hughes44b53ad2013-02-11 20:18:47 +000035TEST(pthread, pthread_key_create_lots) {
36 // We can allocate _SC_THREAD_KEYS_MAX keys.
37 std::vector<pthread_key_t> keys;
38 for (int i = 0; i < sysconf(_SC_THREAD_KEYS_MAX); ++i) {
39 pthread_key_t key;
Elliott Hughes3e898472013-02-12 16:40:24 +000040 // If this fails, it's likely that GLOBAL_INIT_THREAD_LOCAL_BUFFER_COUNT is wrong.
41 ASSERT_EQ(0, pthread_key_create(&key, NULL)) << i << " of " << sysconf(_SC_THREAD_KEYS_MAX);
Elliott Hughes44b53ad2013-02-11 20:18:47 +000042 keys.push_back(key);
43 }
44
45 // ...and that really is the maximum.
46 pthread_key_t key;
47 ASSERT_EQ(EAGAIN, pthread_key_create(&key, NULL));
48
49 // (Don't leak all those keys!)
50 for (size_t i = 0; i < keys.size(); ++i) {
51 ASSERT_EQ(0, pthread_key_delete(keys[i]));
52 }
53}
Elliott Hughes3e898472013-02-12 16:40:24 +000054#endif
Elliott Hughes44b53ad2013-02-11 20:18:47 +000055
Elliott Hughes4d014e12012-09-07 16:47:54 -070056static void* IdFn(void* arg) {
57 return arg;
58}
59
60static void* SleepFn(void* arg) {
Elliott Hughes5b9310e2013-10-02 16:59:05 -070061 sleep(reinterpret_cast<uintptr_t>(arg));
Elliott Hughes4d014e12012-09-07 16:47:54 -070062 return NULL;
63}
64
Sergey Melnikov10ce9692012-10-26 14:06:43 +040065static void* SpinFn(void* arg) {
66 volatile bool* b = reinterpret_cast<volatile bool*>(arg);
67 while (!*b) {
68 }
69 return NULL;
70}
71
Elliott Hughes4d014e12012-09-07 16:47:54 -070072static void* JoinFn(void* arg) {
73 return reinterpret_cast<void*>(pthread_join(reinterpret_cast<pthread_t>(arg), NULL));
74}
75
Sergey Melnikov10ce9692012-10-26 14:06:43 +040076static void AssertDetached(pthread_t t, bool is_detached) {
77 pthread_attr_t attr;
78 ASSERT_EQ(0, pthread_getattr_np(t, &attr));
79 int detach_state;
80 ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &detach_state));
81 pthread_attr_destroy(&attr);
82 ASSERT_EQ(is_detached, (detach_state == PTHREAD_CREATE_DETACHED));
83}
84
Elliott Hughes9d23e042013-02-15 19:21:51 -080085static void MakeDeadThread(pthread_t& t) {
86 ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, NULL));
87 void* result;
88 ASSERT_EQ(0, pthread_join(t, &result));
89}
90
Elliott Hughes4d014e12012-09-07 16:47:54 -070091TEST(pthread, pthread_create) {
92 void* expected_result = reinterpret_cast<void*>(123);
93 // Can we create a thread?
94 pthread_t t;
95 ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, expected_result));
96 // If we join, do we get the expected value back?
97 void* result;
98 ASSERT_EQ(0, pthread_join(t, &result));
99 ASSERT_EQ(expected_result, result);
100}
101
Elliott Hughes3e898472013-02-12 16:40:24 +0000102TEST(pthread, pthread_create_EAGAIN) {
103 pthread_attr_t attributes;
104 ASSERT_EQ(0, pthread_attr_init(&attributes));
105 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, static_cast<size_t>(-1) & ~(getpagesize() - 1)));
106
107 pthread_t t;
108 ASSERT_EQ(EAGAIN, pthread_create(&t, &attributes, IdFn, NULL));
109}
110
Elliott Hughes4d014e12012-09-07 16:47:54 -0700111TEST(pthread, pthread_no_join_after_detach) {
112 pthread_t t1;
113 ASSERT_EQ(0, pthread_create(&t1, NULL, SleepFn, reinterpret_cast<void*>(5)));
114
115 // After a pthread_detach...
116 ASSERT_EQ(0, pthread_detach(t1));
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400117 AssertDetached(t1, true);
Elliott Hughes4d014e12012-09-07 16:47:54 -0700118
119 // ...pthread_join should fail.
120 void* result;
121 ASSERT_EQ(EINVAL, pthread_join(t1, &result));
122}
123
124TEST(pthread, pthread_no_op_detach_after_join) {
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400125 bool done = false;
126
Elliott Hughes4d014e12012-09-07 16:47:54 -0700127 pthread_t t1;
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400128 ASSERT_EQ(0, pthread_create(&t1, NULL, SpinFn, &done));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700129
130 // If thread 2 is already waiting to join thread 1...
131 pthread_t t2;
132 ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
133
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400134 sleep(1); // (Give t2 a chance to call pthread_join.)
Elliott Hughes4d014e12012-09-07 16:47:54 -0700135
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400136 // ...a call to pthread_detach on thread 1 will "succeed" (silently fail)...
137 ASSERT_EQ(0, pthread_detach(t1));
138 AssertDetached(t1, false);
139
140 done = true;
141
142 // ...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 -0700143 void* join_result;
144 ASSERT_EQ(0, pthread_join(t2, &join_result));
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700145 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700146}
Elliott Hughes14f19592012-10-29 10:19:44 -0700147
148TEST(pthread, pthread_join_self) {
149 void* result;
150 ASSERT_EQ(EDEADLK, pthread_join(pthread_self(), &result));
151}
Elliott Hughes4f251be2012-11-01 16:33:29 -0700152
153#if __BIONIC__ // For some reason, gtest on bionic can cope with this but gtest on glibc can't.
154
155static void TestBug37410() {
156 pthread_t t1;
157 ASSERT_EQ(0, pthread_create(&t1, NULL, JoinFn, reinterpret_cast<void*>(pthread_self())));
158 pthread_exit(NULL);
159}
160
Elliott Hughes7fd803c2013-02-14 16:33:52 -0800161// Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to
162// run this test (which exits normally) in its own process.
Elliott Hughes4f251be2012-11-01 16:33:29 -0700163TEST(pthread_DeathTest, pthread_bug_37410) {
164 // http://code.google.com/p/android/issues/detail?id=37410
165 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
Elliott Hughes7fd803c2013-02-14 16:33:52 -0800166 ASSERT_EXIT(TestBug37410(), ::testing::ExitedWithCode(0), "");
Elliott Hughes4f251be2012-11-01 16:33:29 -0700167}
168#endif
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800169
170static void* SignalHandlerFn(void* arg) {
171 sigset_t wait_set;
172 sigfillset(&wait_set);
173 return reinterpret_cast<void*>(sigwait(&wait_set, reinterpret_cast<int*>(arg)));
174}
175
176TEST(pthread, pthread_sigmask) {
Elliott Hughes19e62322013-10-15 11:23:57 -0700177 // Check that SIGUSR1 isn't blocked.
178 sigset_t original_set;
179 sigemptyset(&original_set);
180 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &original_set));
181 ASSERT_FALSE(sigismember(&original_set, SIGUSR1));
182
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800183 // Block SIGUSR1.
184 sigset_t set;
185 sigemptyset(&set);
186 sigaddset(&set, SIGUSR1);
187 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &set, NULL));
188
Elliott Hughes19e62322013-10-15 11:23:57 -0700189 // Check that SIGUSR1 is blocked.
190 sigset_t final_set;
191 sigemptyset(&final_set);
192 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &final_set));
193 ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
194 // ...and that sigprocmask agrees with pthread_sigmask.
195 sigemptyset(&final_set);
196 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, NULL, &final_set));
197 ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
198
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800199 // Spawn a thread that calls sigwait and tells us what it received.
200 pthread_t signal_thread;
201 int received_signal = -1;
202 ASSERT_EQ(0, pthread_create(&signal_thread, NULL, SignalHandlerFn, &received_signal));
203
204 // Send that thread SIGUSR1.
205 pthread_kill(signal_thread, SIGUSR1);
206
207 // See what it got.
208 void* join_result;
209 ASSERT_EQ(0, pthread_join(signal_thread, &join_result));
210 ASSERT_EQ(SIGUSR1, received_signal);
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700211 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
Elliott Hughes19e62322013-10-15 11:23:57 -0700212
213 // Restore the original signal mask.
214 ASSERT_EQ(0, pthread_sigmask(SIG_SETMASK, &original_set, NULL));
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800215}
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800216
Elliott Hughes3e898472013-02-12 16:40:24 +0000217#if __BIONIC__
Elliott Hughes70b24b12013-11-15 11:51:07 -0800218extern "C" pid_t __bionic_clone(int flags, void* child_stack, pid_t* parent_tid, void* tls, pid_t* child_tid, int (*fn)(void*), void* arg);
219TEST(pthread, __bionic_clone) {
220 // Check that our hand-written clone assembler sets errno correctly on failure.
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800221 uintptr_t fake_child_stack[16];
222 errno = 0;
Elliott Hughes70b24b12013-11-15 11:51:07 -0800223 ASSERT_EQ(-1, __bionic_clone(CLONE_THREAD, &fake_child_stack[0], NULL, NULL, NULL, NULL, NULL));
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800224 ASSERT_EQ(EINVAL, errno);
225}
226#endif
Elliott Hughes3e898472013-02-12 16:40:24 +0000227
Elliott Hughes9701d4b2013-02-12 17:20:42 -0800228#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 +0000229TEST(pthread, pthread_setname_np__too_long) {
230 ASSERT_EQ(ERANGE, pthread_setname_np(pthread_self(), "this name is far too long for linux"));
231}
Elliott Hughes9701d4b2013-02-12 17:20:42 -0800232#endif
Elliott Hughes3e898472013-02-12 16:40:24 +0000233
Elliott Hughes9701d4b2013-02-12 17:20:42 -0800234#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 +0000235TEST(pthread, pthread_setname_np__self) {
236 ASSERT_EQ(0, pthread_setname_np(pthread_self(), "short 1"));
237}
Elliott Hughes9701d4b2013-02-12 17:20:42 -0800238#endif
Elliott Hughes3e898472013-02-12 16:40:24 +0000239
Elliott Hughes9701d4b2013-02-12 17:20:42 -0800240#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 +0000241TEST(pthread, pthread_setname_np__other) {
Elliott Hughes40eabe22013-02-14 18:59:37 -0800242 // Emulator kernels don't currently support setting the name of other threads.
243 char* filename = NULL;
244 asprintf(&filename, "/proc/self/task/%d/comm", gettid());
245 struct stat sb;
246 bool has_comm = (stat(filename, &sb) != -1);
247 free(filename);
248
249 if (has_comm) {
250 pthread_t t1;
251 ASSERT_EQ(0, pthread_create(&t1, NULL, SleepFn, reinterpret_cast<void*>(5)));
252 ASSERT_EQ(0, pthread_setname_np(t1, "short 2"));
253 } else {
254 fprintf(stderr, "skipping test: this kernel doesn't have /proc/self/task/tid/comm files!\n");
255 }
Elliott Hughes3e898472013-02-12 16:40:24 +0000256}
Elliott Hughes9701d4b2013-02-12 17:20:42 -0800257#endif
Elliott Hughes3e898472013-02-12 16:40:24 +0000258
Elliott Hughes9701d4b2013-02-12 17:20:42 -0800259#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 +0000260TEST(pthread, pthread_setname_np__no_such_thread) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800261 pthread_t dead_thread;
262 MakeDeadThread(dead_thread);
Elliott Hughes3e898472013-02-12 16:40:24 +0000263
264 // Call pthread_setname_np after thread has already exited.
Elliott Hughesa41ba2f2013-03-21 20:02:35 -0700265 ASSERT_EQ(ESRCH, pthread_setname_np(dead_thread, "short 3"));
Elliott Hughes3e898472013-02-12 16:40:24 +0000266}
Elliott Hughes9701d4b2013-02-12 17:20:42 -0800267#endif
Elliott Hughes9d23e042013-02-15 19:21:51 -0800268
269TEST(pthread, pthread_kill__0) {
270 // Signal 0 just tests that the thread exists, so it's safe to call on ourselves.
271 ASSERT_EQ(0, pthread_kill(pthread_self(), 0));
272}
273
274TEST(pthread, pthread_kill__invalid_signal) {
275 ASSERT_EQ(EINVAL, pthread_kill(pthread_self(), -1));
276}
277
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800278static void pthread_kill__in_signal_handler_helper(int signal_number) {
279 static int count = 0;
280 ASSERT_EQ(SIGALRM, signal_number);
281 if (++count == 1) {
282 // Can we call pthread_kill from a signal handler?
283 ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
284 }
285}
286
287TEST(pthread, pthread_kill__in_signal_handler) {
288 struct sigaction action;
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700289 struct sigaction original_action;
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800290 sigemptyset(&action.sa_mask);
291 action.sa_flags = 0;
292 action.sa_handler = pthread_kill__in_signal_handler_helper;
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700293 ASSERT_EQ(0, sigaction(SIGALRM, &action, &original_action));
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800294 ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700295 ASSERT_EQ(0, sigaction(SIGALRM, &original_action, NULL));
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800296}
297
Elliott Hughes9d23e042013-02-15 19:21:51 -0800298TEST(pthread, pthread_detach__no_such_thread) {
299 pthread_t dead_thread;
300 MakeDeadThread(dead_thread);
301
302 ASSERT_EQ(ESRCH, pthread_detach(dead_thread));
303}
304
Jeff Hao9b06cc32013-08-15 14:51:16 -0700305TEST(pthread, pthread_getcpuclockid__clock_gettime) {
306 pthread_t t;
307 ASSERT_EQ(0, pthread_create(&t, NULL, SleepFn, reinterpret_cast<void*>(5)));
308
309 clockid_t c;
310 ASSERT_EQ(0, pthread_getcpuclockid(t, &c));
311 timespec ts;
312 ASSERT_EQ(0, clock_gettime(c, &ts));
313}
314
Elliott Hughes9d23e042013-02-15 19:21:51 -0800315TEST(pthread, pthread_getcpuclockid__no_such_thread) {
316 pthread_t dead_thread;
317 MakeDeadThread(dead_thread);
318
319 clockid_t c;
320 ASSERT_EQ(ESRCH, pthread_getcpuclockid(dead_thread, &c));
321}
322
323TEST(pthread, pthread_getschedparam__no_such_thread) {
324 pthread_t dead_thread;
325 MakeDeadThread(dead_thread);
326
327 int policy;
328 sched_param param;
329 ASSERT_EQ(ESRCH, pthread_getschedparam(dead_thread, &policy, &param));
330}
331
332TEST(pthread, pthread_setschedparam__no_such_thread) {
333 pthread_t dead_thread;
334 MakeDeadThread(dead_thread);
335
336 int policy = 0;
337 sched_param param;
338 ASSERT_EQ(ESRCH, pthread_setschedparam(dead_thread, policy, &param));
339}
340
341TEST(pthread, pthread_join__no_such_thread) {
342 pthread_t dead_thread;
343 MakeDeadThread(dead_thread);
344
345 void* result;
346 ASSERT_EQ(ESRCH, pthread_join(dead_thread, &result));
347}
348
349TEST(pthread, pthread_kill__no_such_thread) {
350 pthread_t dead_thread;
351 MakeDeadThread(dead_thread);
352
353 ASSERT_EQ(ESRCH, pthread_kill(dead_thread, 0));
354}
msg5550f020d12013-06-06 14:59:28 -0400355
356TEST(pthread, pthread_join__multijoin) {
357 bool done = false;
358
359 pthread_t t1;
360 ASSERT_EQ(0, pthread_create(&t1, NULL, SpinFn, &done));
361
362 pthread_t t2;
363 ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
364
365 sleep(1); // (Give t2 a chance to call pthread_join.)
366
367 // Multiple joins to the same thread should fail.
368 ASSERT_EQ(EINVAL, pthread_join(t1, NULL));
369
370 done = true;
371
372 // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes).
373 void* join_result;
374 ASSERT_EQ(0, pthread_join(t2, &join_result));
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700375 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
msg5550f020d12013-06-06 14:59:28 -0400376}
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700377
Elliott Hughes70b24b12013-11-15 11:51:07 -0800378TEST(pthread, pthread_join__race) {
379 // http://b/11693195 --- pthread_join could return before the thread had actually exited.
380 // If the joiner unmapped the thread's stack, that could lead to SIGSEGV in the thread.
381 for (size_t i = 0; i < 1024; ++i) {
382 size_t stack_size = 64*1024;
383 void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
384
385 pthread_attr_t a;
386 pthread_attr_init(&a);
387 pthread_attr_setstack(&a, stack, stack_size);
388
389 pthread_t t;
390 ASSERT_EQ(0, pthread_create(&t, &a, IdFn, NULL));
391 ASSERT_EQ(0, pthread_join(t, NULL));
392 ASSERT_EQ(0, munmap(stack, stack_size));
393 }
394}
395
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700396static void* GetActualGuardSizeFn(void* arg) {
397 pthread_attr_t attributes;
398 pthread_getattr_np(pthread_self(), &attributes);
399 pthread_attr_getguardsize(&attributes, reinterpret_cast<size_t*>(arg));
400 return NULL;
401}
402
403static size_t GetActualGuardSize(const pthread_attr_t& attributes) {
404 size_t result;
405 pthread_t t;
406 pthread_create(&t, &attributes, GetActualGuardSizeFn, &result);
407 void* join_result;
408 pthread_join(t, &join_result);
409 return result;
410}
411
412static void* GetActualStackSizeFn(void* arg) {
413 pthread_attr_t attributes;
414 pthread_getattr_np(pthread_self(), &attributes);
415 pthread_attr_getstacksize(&attributes, reinterpret_cast<size_t*>(arg));
416 return NULL;
417}
418
419static size_t GetActualStackSize(const pthread_attr_t& attributes) {
420 size_t result;
421 pthread_t t;
422 pthread_create(&t, &attributes, GetActualStackSizeFn, &result);
423 void* join_result;
424 pthread_join(t, &join_result);
425 return result;
426}
427
428TEST(pthread, pthread_attr_setguardsize) {
429 pthread_attr_t attributes;
430 ASSERT_EQ(0, pthread_attr_init(&attributes));
431
432 // Get the default guard size.
433 size_t default_guard_size;
434 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &default_guard_size));
435
436 // No such thing as too small: will be rounded up to one page by pthread_create.
437 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 128));
438 size_t guard_size;
439 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
440 ASSERT_EQ(128U, guard_size);
441 ASSERT_EQ(4096U, GetActualGuardSize(attributes));
442
443 // Large enough and a multiple of the page size.
444 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024));
445 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
446 ASSERT_EQ(32*1024U, guard_size);
447
448 // Large enough but not a multiple of the page size; will be rounded up by pthread_create.
449 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024 + 1));
450 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
451 ASSERT_EQ(32*1024U + 1, guard_size);
452}
453
454TEST(pthread, pthread_attr_setstacksize) {
455 pthread_attr_t attributes;
456 ASSERT_EQ(0, pthread_attr_init(&attributes));
457
458 // Get the default stack size.
459 size_t default_stack_size;
460 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &default_stack_size));
461
462 // Too small.
463 ASSERT_EQ(EINVAL, pthread_attr_setstacksize(&attributes, 128));
464 size_t stack_size;
465 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
466 ASSERT_EQ(default_stack_size, stack_size);
467 ASSERT_GE(GetActualStackSize(attributes), default_stack_size);
468
469 // Large enough and a multiple of the page size.
470 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024));
471 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
472 ASSERT_EQ(32*1024U, stack_size);
473 ASSERT_EQ(GetActualStackSize(attributes), 32*1024U);
474
475 // Large enough but not a multiple of the page size; will be rounded up by pthread_create.
476 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024 + 1));
477 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
478 ASSERT_EQ(32*1024U + 1, stack_size);
479#if __BIONIC__
480 // Bionic rounds up, which is what POSIX allows.
481 ASSERT_EQ(GetActualStackSize(attributes), (32 + 4)*1024U);
482#else
483 // glibc rounds down, in violation of POSIX. They document this in their BUGS section.
484 ASSERT_EQ(GetActualStackSize(attributes), 32*1024U);
485#endif
486}
Elliott Hughesc3f11402013-10-30 14:40:09 -0700487
488TEST(pthread, pthread_rwlock_smoke) {
489 pthread_rwlock_t l;
490 ASSERT_EQ(0, pthread_rwlock_init(&l, NULL));
491
492 ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
493 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
494
495 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
496 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
497
498 ASSERT_EQ(0, pthread_rwlock_destroy(&l));
499}
500
501static int gOnceFnCallCount = 0;
502static void OnceFn() {
503 ++gOnceFnCallCount;
504}
505
506TEST(pthread, pthread_once_smoke) {
507 pthread_once_t once_control = PTHREAD_ONCE_INIT;
508 ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
509 ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
510 ASSERT_EQ(1, gOnceFnCallCount);
511}
512
513static int gAtForkPrepareCalls = 0;
514static void AtForkPrepare1() { gAtForkPrepareCalls = (gAtForkPrepareCalls << 4) | 1; }
515static void AtForkPrepare2() { gAtForkPrepareCalls = (gAtForkPrepareCalls << 4) | 2; }
516static int gAtForkParentCalls = 0;
517static void AtForkParent1() { gAtForkParentCalls = (gAtForkParentCalls << 4) | 1; }
518static void AtForkParent2() { gAtForkParentCalls = (gAtForkParentCalls << 4) | 2; }
519static int gAtForkChildCalls = 0;
520static void AtForkChild1() { gAtForkChildCalls = (gAtForkChildCalls << 4) | 1; }
521static void AtForkChild2() { gAtForkChildCalls = (gAtForkChildCalls << 4) | 2; }
522
523TEST(pthread, pthread_atfork) {
524 ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1));
525 ASSERT_EQ(0, pthread_atfork(AtForkPrepare2, AtForkParent2, AtForkChild2));
526
527 int pid = fork();
528 ASSERT_NE(-1, pid) << strerror(errno);
529
530 // Child and parent calls are made in the order they were registered.
531 if (pid == 0) {
532 ASSERT_EQ(0x12, gAtForkChildCalls);
533 _exit(0);
534 }
535 ASSERT_EQ(0x12, gAtForkParentCalls);
536
537 // Prepare calls are made in the reverse order.
538 ASSERT_EQ(0x21, gAtForkPrepareCalls);
539}
540
541TEST(pthread, pthread_attr_getscope) {
542 pthread_attr_t attr;
543 ASSERT_EQ(0, pthread_attr_init(&attr));
544
545 int scope;
546 ASSERT_EQ(0, pthread_attr_getscope(&attr, &scope));
547 ASSERT_EQ(PTHREAD_SCOPE_SYSTEM, scope);
548}