blob: e7a952aa2ad94d110171e78d53900ef92780f8a3 [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) {
Elliott Hughes18876212013-12-12 11:02:41 -080036 // POSIX says PTHREAD_KEYS_MAX should be at least 128.
37 ASSERT_GE(PTHREAD_KEYS_MAX, 128);
38 // sysconf shouldn't return a smaller value.
39 ASSERT_GE(sysconf(_SC_THREAD_KEYS_MAX), PTHREAD_KEYS_MAX);
40
Elliott Hughes44b53ad2013-02-11 20:18:47 +000041 // We can allocate _SC_THREAD_KEYS_MAX keys.
42 std::vector<pthread_key_t> keys;
43 for (int i = 0; i < sysconf(_SC_THREAD_KEYS_MAX); ++i) {
44 pthread_key_t key;
Elliott Hughes3e898472013-02-12 16:40:24 +000045 // If this fails, it's likely that GLOBAL_INIT_THREAD_LOCAL_BUFFER_COUNT is wrong.
46 ASSERT_EQ(0, pthread_key_create(&key, NULL)) << i << " of " << sysconf(_SC_THREAD_KEYS_MAX);
Elliott Hughes44b53ad2013-02-11 20:18:47 +000047 keys.push_back(key);
48 }
49
50 // ...and that really is the maximum.
51 pthread_key_t key;
52 ASSERT_EQ(EAGAIN, pthread_key_create(&key, NULL));
53
54 // (Don't leak all those keys!)
55 for (size_t i = 0; i < keys.size(); ++i) {
56 ASSERT_EQ(0, pthread_key_delete(keys[i]));
57 }
58}
Elliott Hughes3e898472013-02-12 16:40:24 +000059#endif
Elliott Hughes44b53ad2013-02-11 20:18:47 +000060
Elliott Hughes4d014e12012-09-07 16:47:54 -070061static void* IdFn(void* arg) {
62 return arg;
63}
64
65static void* SleepFn(void* arg) {
Elliott Hughes5b9310e2013-10-02 16:59:05 -070066 sleep(reinterpret_cast<uintptr_t>(arg));
Elliott Hughes4d014e12012-09-07 16:47:54 -070067 return NULL;
68}
69
Sergey Melnikov10ce9692012-10-26 14:06:43 +040070static void* SpinFn(void* arg) {
71 volatile bool* b = reinterpret_cast<volatile bool*>(arg);
72 while (!*b) {
73 }
74 return NULL;
75}
76
Elliott Hughes4d014e12012-09-07 16:47:54 -070077static void* JoinFn(void* arg) {
78 return reinterpret_cast<void*>(pthread_join(reinterpret_cast<pthread_t>(arg), NULL));
79}
80
Sergey Melnikov10ce9692012-10-26 14:06:43 +040081static void AssertDetached(pthread_t t, bool is_detached) {
82 pthread_attr_t attr;
83 ASSERT_EQ(0, pthread_getattr_np(t, &attr));
84 int detach_state;
85 ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &detach_state));
86 pthread_attr_destroy(&attr);
87 ASSERT_EQ(is_detached, (detach_state == PTHREAD_CREATE_DETACHED));
88}
89
Elliott Hughes9d23e042013-02-15 19:21:51 -080090static void MakeDeadThread(pthread_t& t) {
91 ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, NULL));
92 void* result;
93 ASSERT_EQ(0, pthread_join(t, &result));
94}
95
Elliott Hughes4d014e12012-09-07 16:47:54 -070096TEST(pthread, pthread_create) {
97 void* expected_result = reinterpret_cast<void*>(123);
98 // Can we create a thread?
99 pthread_t t;
100 ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, expected_result));
101 // If we join, do we get the expected value back?
102 void* result;
103 ASSERT_EQ(0, pthread_join(t, &result));
104 ASSERT_EQ(expected_result, result);
105}
106
Elliott Hughes3e898472013-02-12 16:40:24 +0000107TEST(pthread, pthread_create_EAGAIN) {
108 pthread_attr_t attributes;
109 ASSERT_EQ(0, pthread_attr_init(&attributes));
110 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, static_cast<size_t>(-1) & ~(getpagesize() - 1)));
111
112 pthread_t t;
113 ASSERT_EQ(EAGAIN, pthread_create(&t, &attributes, IdFn, NULL));
114}
115
Elliott Hughes4d014e12012-09-07 16:47:54 -0700116TEST(pthread, pthread_no_join_after_detach) {
117 pthread_t t1;
118 ASSERT_EQ(0, pthread_create(&t1, NULL, SleepFn, reinterpret_cast<void*>(5)));
119
120 // After a pthread_detach...
121 ASSERT_EQ(0, pthread_detach(t1));
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400122 AssertDetached(t1, true);
Elliott Hughes4d014e12012-09-07 16:47:54 -0700123
124 // ...pthread_join should fail.
125 void* result;
126 ASSERT_EQ(EINVAL, pthread_join(t1, &result));
127}
128
129TEST(pthread, pthread_no_op_detach_after_join) {
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400130 bool done = false;
131
Elliott Hughes4d014e12012-09-07 16:47:54 -0700132 pthread_t t1;
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400133 ASSERT_EQ(0, pthread_create(&t1, NULL, SpinFn, &done));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700134
135 // If thread 2 is already waiting to join thread 1...
136 pthread_t t2;
137 ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
138
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400139 sleep(1); // (Give t2 a chance to call pthread_join.)
Elliott Hughes4d014e12012-09-07 16:47:54 -0700140
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400141 // ...a call to pthread_detach on thread 1 will "succeed" (silently fail)...
142 ASSERT_EQ(0, pthread_detach(t1));
143 AssertDetached(t1, false);
144
145 done = true;
146
147 // ...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 -0700148 void* join_result;
149 ASSERT_EQ(0, pthread_join(t2, &join_result));
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700150 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700151}
Elliott Hughes14f19592012-10-29 10:19:44 -0700152
153TEST(pthread, pthread_join_self) {
154 void* result;
155 ASSERT_EQ(EDEADLK, pthread_join(pthread_self(), &result));
156}
Elliott Hughes4f251be2012-11-01 16:33:29 -0700157
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800158struct TestBug37410 {
159 pthread_t main_thread;
160 pthread_mutex_t mutex;
Elliott Hughes4f251be2012-11-01 16:33:29 -0700161
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800162 static void main() {
163 TestBug37410 data;
164 data.main_thread = pthread_self();
165 ASSERT_EQ(0, pthread_mutex_init(&data.mutex, NULL));
166 ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
167
168 pthread_t t;
169 ASSERT_EQ(0, pthread_create(&t, NULL, TestBug37410::thread_fn, reinterpret_cast<void*>(&data)));
170
171 // Wait for the thread to be running...
172 ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
173 ASSERT_EQ(0, pthread_mutex_unlock(&data.mutex));
174
175 // ...and exit.
176 pthread_exit(NULL);
177 }
178
179 private:
180 static void* thread_fn(void* arg) {
181 TestBug37410* data = reinterpret_cast<TestBug37410*>(arg);
182
183 // Let the main thread know we're running.
184 pthread_mutex_unlock(&data->mutex);
185
186 // And wait for the main thread to exit.
187 pthread_join(data->main_thread, NULL);
188
189 return NULL;
190 }
191};
Elliott Hughes4f251be2012-11-01 16:33:29 -0700192
Elliott Hughes7fd803c2013-02-14 16:33:52 -0800193// Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to
194// run this test (which exits normally) in its own process.
Elliott Hughes4f251be2012-11-01 16:33:29 -0700195TEST(pthread_DeathTest, pthread_bug_37410) {
196 // http://code.google.com/p/android/issues/detail?id=37410
197 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800198 ASSERT_EXIT(TestBug37410::main(), ::testing::ExitedWithCode(0), "");
Elliott Hughes4f251be2012-11-01 16:33:29 -0700199}
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800200
201static void* SignalHandlerFn(void* arg) {
202 sigset_t wait_set;
203 sigfillset(&wait_set);
204 return reinterpret_cast<void*>(sigwait(&wait_set, reinterpret_cast<int*>(arg)));
205}
206
207TEST(pthread, pthread_sigmask) {
Elliott Hughes19e62322013-10-15 11:23:57 -0700208 // Check that SIGUSR1 isn't blocked.
209 sigset_t original_set;
210 sigemptyset(&original_set);
211 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &original_set));
212 ASSERT_FALSE(sigismember(&original_set, SIGUSR1));
213
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800214 // Block SIGUSR1.
215 sigset_t set;
216 sigemptyset(&set);
217 sigaddset(&set, SIGUSR1);
218 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &set, NULL));
219
Elliott Hughes19e62322013-10-15 11:23:57 -0700220 // Check that SIGUSR1 is blocked.
221 sigset_t final_set;
222 sigemptyset(&final_set);
223 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &final_set));
224 ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
225 // ...and that sigprocmask agrees with pthread_sigmask.
226 sigemptyset(&final_set);
227 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, NULL, &final_set));
228 ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
229
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800230 // Spawn a thread that calls sigwait and tells us what it received.
231 pthread_t signal_thread;
232 int received_signal = -1;
233 ASSERT_EQ(0, pthread_create(&signal_thread, NULL, SignalHandlerFn, &received_signal));
234
235 // Send that thread SIGUSR1.
236 pthread_kill(signal_thread, SIGUSR1);
237
238 // See what it got.
239 void* join_result;
240 ASSERT_EQ(0, pthread_join(signal_thread, &join_result));
241 ASSERT_EQ(SIGUSR1, received_signal);
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700242 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
Elliott Hughes19e62322013-10-15 11:23:57 -0700243
244 // Restore the original signal mask.
245 ASSERT_EQ(0, pthread_sigmask(SIG_SETMASK, &original_set, NULL));
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800246}
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800247
Elliott Hughes3e898472013-02-12 16:40:24 +0000248#if __BIONIC__
Elliott Hughes70b24b12013-11-15 11:51:07 -0800249extern "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);
250TEST(pthread, __bionic_clone) {
251 // Check that our hand-written clone assembler sets errno correctly on failure.
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800252 uintptr_t fake_child_stack[16];
253 errno = 0;
Elliott Hughes70b24b12013-11-15 11:51:07 -0800254 ASSERT_EQ(-1, __bionic_clone(CLONE_THREAD, &fake_child_stack[0], NULL, NULL, NULL, NULL, NULL));
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800255 ASSERT_EQ(EINVAL, errno);
256}
257#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__too_long) {
261 ASSERT_EQ(ERANGE, pthread_setname_np(pthread_self(), "this name is far too long for linux"));
262}
Elliott Hughes9701d4b2013-02-12 17:20:42 -0800263#endif
Elliott Hughes3e898472013-02-12 16:40:24 +0000264
Elliott Hughes9701d4b2013-02-12 17:20:42 -0800265#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 +0000266TEST(pthread, pthread_setname_np__self) {
267 ASSERT_EQ(0, pthread_setname_np(pthread_self(), "short 1"));
268}
Elliott Hughes9701d4b2013-02-12 17:20:42 -0800269#endif
Elliott Hughes3e898472013-02-12 16:40:24 +0000270
Elliott Hughes9701d4b2013-02-12 17:20:42 -0800271#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 +0000272TEST(pthread, pthread_setname_np__other) {
Elliott Hughes40eabe22013-02-14 18:59:37 -0800273 // Emulator kernels don't currently support setting the name of other threads.
274 char* filename = NULL;
275 asprintf(&filename, "/proc/self/task/%d/comm", gettid());
276 struct stat sb;
277 bool has_comm = (stat(filename, &sb) != -1);
278 free(filename);
279
280 if (has_comm) {
281 pthread_t t1;
282 ASSERT_EQ(0, pthread_create(&t1, NULL, SleepFn, reinterpret_cast<void*>(5)));
283 ASSERT_EQ(0, pthread_setname_np(t1, "short 2"));
284 } else {
285 fprintf(stderr, "skipping test: this kernel doesn't have /proc/self/task/tid/comm files!\n");
286 }
Elliott Hughes3e898472013-02-12 16:40:24 +0000287}
Elliott Hughes9701d4b2013-02-12 17:20:42 -0800288#endif
Elliott Hughes3e898472013-02-12 16:40:24 +0000289
Elliott Hughes9701d4b2013-02-12 17:20:42 -0800290#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 +0000291TEST(pthread, pthread_setname_np__no_such_thread) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800292 pthread_t dead_thread;
293 MakeDeadThread(dead_thread);
Elliott Hughes3e898472013-02-12 16:40:24 +0000294
295 // Call pthread_setname_np after thread has already exited.
Elliott Hughesa41ba2f2013-03-21 20:02:35 -0700296 ASSERT_EQ(ESRCH, pthread_setname_np(dead_thread, "short 3"));
Elliott Hughes3e898472013-02-12 16:40:24 +0000297}
Elliott Hughes9701d4b2013-02-12 17:20:42 -0800298#endif
Elliott Hughes9d23e042013-02-15 19:21:51 -0800299
300TEST(pthread, pthread_kill__0) {
301 // Signal 0 just tests that the thread exists, so it's safe to call on ourselves.
302 ASSERT_EQ(0, pthread_kill(pthread_self(), 0));
303}
304
305TEST(pthread, pthread_kill__invalid_signal) {
306 ASSERT_EQ(EINVAL, pthread_kill(pthread_self(), -1));
307}
308
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800309static void pthread_kill__in_signal_handler_helper(int signal_number) {
310 static int count = 0;
311 ASSERT_EQ(SIGALRM, signal_number);
312 if (++count == 1) {
313 // Can we call pthread_kill from a signal handler?
314 ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
315 }
316}
317
318TEST(pthread, pthread_kill__in_signal_handler) {
319 struct sigaction action;
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700320 struct sigaction original_action;
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800321 sigemptyset(&action.sa_mask);
322 action.sa_flags = 0;
323 action.sa_handler = pthread_kill__in_signal_handler_helper;
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700324 ASSERT_EQ(0, sigaction(SIGALRM, &action, &original_action));
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800325 ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700326 ASSERT_EQ(0, sigaction(SIGALRM, &original_action, NULL));
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800327}
328
Elliott Hughes9d23e042013-02-15 19:21:51 -0800329TEST(pthread, pthread_detach__no_such_thread) {
330 pthread_t dead_thread;
331 MakeDeadThread(dead_thread);
332
333 ASSERT_EQ(ESRCH, pthread_detach(dead_thread));
334}
335
Jeff Hao9b06cc32013-08-15 14:51:16 -0700336TEST(pthread, pthread_getcpuclockid__clock_gettime) {
337 pthread_t t;
338 ASSERT_EQ(0, pthread_create(&t, NULL, SleepFn, reinterpret_cast<void*>(5)));
339
340 clockid_t c;
341 ASSERT_EQ(0, pthread_getcpuclockid(t, &c));
342 timespec ts;
343 ASSERT_EQ(0, clock_gettime(c, &ts));
344}
345
Elliott Hughes9d23e042013-02-15 19:21:51 -0800346TEST(pthread, pthread_getcpuclockid__no_such_thread) {
347 pthread_t dead_thread;
348 MakeDeadThread(dead_thread);
349
350 clockid_t c;
351 ASSERT_EQ(ESRCH, pthread_getcpuclockid(dead_thread, &c));
352}
353
354TEST(pthread, pthread_getschedparam__no_such_thread) {
355 pthread_t dead_thread;
356 MakeDeadThread(dead_thread);
357
358 int policy;
359 sched_param param;
360 ASSERT_EQ(ESRCH, pthread_getschedparam(dead_thread, &policy, &param));
361}
362
363TEST(pthread, pthread_setschedparam__no_such_thread) {
364 pthread_t dead_thread;
365 MakeDeadThread(dead_thread);
366
367 int policy = 0;
368 sched_param param;
369 ASSERT_EQ(ESRCH, pthread_setschedparam(dead_thread, policy, &param));
370}
371
372TEST(pthread, pthread_join__no_such_thread) {
373 pthread_t dead_thread;
374 MakeDeadThread(dead_thread);
375
376 void* result;
377 ASSERT_EQ(ESRCH, pthread_join(dead_thread, &result));
378}
379
380TEST(pthread, pthread_kill__no_such_thread) {
381 pthread_t dead_thread;
382 MakeDeadThread(dead_thread);
383
384 ASSERT_EQ(ESRCH, pthread_kill(dead_thread, 0));
385}
msg5550f020d12013-06-06 14:59:28 -0400386
387TEST(pthread, pthread_join__multijoin) {
388 bool done = false;
389
390 pthread_t t1;
391 ASSERT_EQ(0, pthread_create(&t1, NULL, SpinFn, &done));
392
393 pthread_t t2;
394 ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
395
396 sleep(1); // (Give t2 a chance to call pthread_join.)
397
398 // Multiple joins to the same thread should fail.
399 ASSERT_EQ(EINVAL, pthread_join(t1, NULL));
400
401 done = true;
402
403 // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes).
404 void* join_result;
405 ASSERT_EQ(0, pthread_join(t2, &join_result));
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700406 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
msg5550f020d12013-06-06 14:59:28 -0400407}
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700408
Elliott Hughes70b24b12013-11-15 11:51:07 -0800409TEST(pthread, pthread_join__race) {
410 // http://b/11693195 --- pthread_join could return before the thread had actually exited.
411 // If the joiner unmapped the thread's stack, that could lead to SIGSEGV in the thread.
412 for (size_t i = 0; i < 1024; ++i) {
413 size_t stack_size = 64*1024;
414 void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
415
416 pthread_attr_t a;
417 pthread_attr_init(&a);
418 pthread_attr_setstack(&a, stack, stack_size);
419
420 pthread_t t;
421 ASSERT_EQ(0, pthread_create(&t, &a, IdFn, NULL));
422 ASSERT_EQ(0, pthread_join(t, NULL));
423 ASSERT_EQ(0, munmap(stack, stack_size));
424 }
425}
426
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700427static void* GetActualGuardSizeFn(void* arg) {
428 pthread_attr_t attributes;
429 pthread_getattr_np(pthread_self(), &attributes);
430 pthread_attr_getguardsize(&attributes, reinterpret_cast<size_t*>(arg));
431 return NULL;
432}
433
434static size_t GetActualGuardSize(const pthread_attr_t& attributes) {
435 size_t result;
436 pthread_t t;
437 pthread_create(&t, &attributes, GetActualGuardSizeFn, &result);
438 void* join_result;
439 pthread_join(t, &join_result);
440 return result;
441}
442
443static void* GetActualStackSizeFn(void* arg) {
444 pthread_attr_t attributes;
445 pthread_getattr_np(pthread_self(), &attributes);
446 pthread_attr_getstacksize(&attributes, reinterpret_cast<size_t*>(arg));
447 return NULL;
448}
449
450static size_t GetActualStackSize(const pthread_attr_t& attributes) {
451 size_t result;
452 pthread_t t;
453 pthread_create(&t, &attributes, GetActualStackSizeFn, &result);
454 void* join_result;
455 pthread_join(t, &join_result);
456 return result;
457}
458
459TEST(pthread, pthread_attr_setguardsize) {
460 pthread_attr_t attributes;
461 ASSERT_EQ(0, pthread_attr_init(&attributes));
462
463 // Get the default guard size.
464 size_t default_guard_size;
465 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &default_guard_size));
466
467 // No such thing as too small: will be rounded up to one page by pthread_create.
468 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 128));
469 size_t guard_size;
470 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
471 ASSERT_EQ(128U, guard_size);
472 ASSERT_EQ(4096U, GetActualGuardSize(attributes));
473
474 // Large enough and a multiple of the page size.
475 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024));
476 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
477 ASSERT_EQ(32*1024U, guard_size);
478
479 // Large enough but not a multiple of the page size; will be rounded up by pthread_create.
480 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024 + 1));
481 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
482 ASSERT_EQ(32*1024U + 1, guard_size);
483}
484
485TEST(pthread, pthread_attr_setstacksize) {
486 pthread_attr_t attributes;
487 ASSERT_EQ(0, pthread_attr_init(&attributes));
488
489 // Get the default stack size.
490 size_t default_stack_size;
491 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &default_stack_size));
492
493 // Too small.
494 ASSERT_EQ(EINVAL, pthread_attr_setstacksize(&attributes, 128));
495 size_t stack_size;
496 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
497 ASSERT_EQ(default_stack_size, stack_size);
498 ASSERT_GE(GetActualStackSize(attributes), default_stack_size);
499
500 // Large enough and a multiple of the page size.
501 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024));
502 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
503 ASSERT_EQ(32*1024U, stack_size);
504 ASSERT_EQ(GetActualStackSize(attributes), 32*1024U);
505
506 // Large enough but not a multiple of the page size; will be rounded up by pthread_create.
507 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024 + 1));
508 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
509 ASSERT_EQ(32*1024U + 1, stack_size);
510#if __BIONIC__
511 // Bionic rounds up, which is what POSIX allows.
512 ASSERT_EQ(GetActualStackSize(attributes), (32 + 4)*1024U);
513#else
514 // glibc rounds down, in violation of POSIX. They document this in their BUGS section.
515 ASSERT_EQ(GetActualStackSize(attributes), 32*1024U);
516#endif
517}
Elliott Hughesc3f11402013-10-30 14:40:09 -0700518
519TEST(pthread, pthread_rwlock_smoke) {
520 pthread_rwlock_t l;
521 ASSERT_EQ(0, pthread_rwlock_init(&l, NULL));
522
523 ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
524 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
525
526 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
527 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
528
529 ASSERT_EQ(0, pthread_rwlock_destroy(&l));
530}
531
532static int gOnceFnCallCount = 0;
533static void OnceFn() {
534 ++gOnceFnCallCount;
535}
536
537TEST(pthread, pthread_once_smoke) {
538 pthread_once_t once_control = PTHREAD_ONCE_INIT;
539 ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
540 ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
541 ASSERT_EQ(1, gOnceFnCallCount);
542}
543
544static int gAtForkPrepareCalls = 0;
545static void AtForkPrepare1() { gAtForkPrepareCalls = (gAtForkPrepareCalls << 4) | 1; }
546static void AtForkPrepare2() { gAtForkPrepareCalls = (gAtForkPrepareCalls << 4) | 2; }
547static int gAtForkParentCalls = 0;
548static void AtForkParent1() { gAtForkParentCalls = (gAtForkParentCalls << 4) | 1; }
549static void AtForkParent2() { gAtForkParentCalls = (gAtForkParentCalls << 4) | 2; }
550static int gAtForkChildCalls = 0;
551static void AtForkChild1() { gAtForkChildCalls = (gAtForkChildCalls << 4) | 1; }
552static void AtForkChild2() { gAtForkChildCalls = (gAtForkChildCalls << 4) | 2; }
553
554TEST(pthread, pthread_atfork) {
555 ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1));
556 ASSERT_EQ(0, pthread_atfork(AtForkPrepare2, AtForkParent2, AtForkChild2));
557
558 int pid = fork();
559 ASSERT_NE(-1, pid) << strerror(errno);
560
561 // Child and parent calls are made in the order they were registered.
562 if (pid == 0) {
563 ASSERT_EQ(0x12, gAtForkChildCalls);
564 _exit(0);
565 }
566 ASSERT_EQ(0x12, gAtForkParentCalls);
567
568 // Prepare calls are made in the reverse order.
569 ASSERT_EQ(0x21, gAtForkPrepareCalls);
570}
571
572TEST(pthread, pthread_attr_getscope) {
573 pthread_attr_t attr;
574 ASSERT_EQ(0, pthread_attr_init(&attr));
575
576 int scope;
577 ASSERT_EQ(0, pthread_attr_getscope(&attr, &scope));
578 ASSERT_EQ(PTHREAD_SCOPE_SYSTEM, scope);
579}