blob: 33317d995d36d6664ef77f80e73842a0ba940c79 [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);
Elliott Hughes718a5b52014-01-28 17:02:03 -080038
39 int sysconf_max = sysconf(_SC_THREAD_KEYS_MAX);
40
Elliott Hughes18876212013-12-12 11:02:41 -080041 // sysconf shouldn't return a smaller value.
Elliott Hughes718a5b52014-01-28 17:02:03 -080042 ASSERT_GE(sysconf_max, PTHREAD_KEYS_MAX);
Elliott Hughes18876212013-12-12 11:02:41 -080043
Elliott Hughes44b53ad2013-02-11 20:18:47 +000044 // We can allocate _SC_THREAD_KEYS_MAX keys.
Elliott Hughes718a5b52014-01-28 17:02:03 -080045 sysconf_max -= 2; // (Except that gtest takes two for itself.)
Elliott Hughes44b53ad2013-02-11 20:18:47 +000046 std::vector<pthread_key_t> keys;
Elliott Hughes718a5b52014-01-28 17:02:03 -080047 for (int i = 0; i < sysconf_max; ++i) {
Elliott Hughes44b53ad2013-02-11 20:18:47 +000048 pthread_key_t key;
Elliott Hughes3e898472013-02-12 16:40:24 +000049 // If this fails, it's likely that GLOBAL_INIT_THREAD_LOCAL_BUFFER_COUNT is wrong.
Elliott Hughes718a5b52014-01-28 17:02:03 -080050 ASSERT_EQ(0, pthread_key_create(&key, NULL)) << i << " of " << sysconf_max;
Elliott Hughes44b53ad2013-02-11 20:18:47 +000051 keys.push_back(key);
52 }
53
54 // ...and that really is the maximum.
55 pthread_key_t key;
56 ASSERT_EQ(EAGAIN, pthread_key_create(&key, NULL));
57
58 // (Don't leak all those keys!)
59 for (size_t i = 0; i < keys.size(); ++i) {
60 ASSERT_EQ(0, pthread_key_delete(keys[i]));
61 }
62}
Elliott Hughes3e898472013-02-12 16:40:24 +000063#endif
Elliott Hughes44b53ad2013-02-11 20:18:47 +000064
Elliott Hughes4d014e12012-09-07 16:47:54 -070065static void* IdFn(void* arg) {
66 return arg;
67}
68
69static void* SleepFn(void* arg) {
Elliott Hughes5b9310e2013-10-02 16:59:05 -070070 sleep(reinterpret_cast<uintptr_t>(arg));
Elliott Hughes4d014e12012-09-07 16:47:54 -070071 return NULL;
72}
73
Sergey Melnikov10ce9692012-10-26 14:06:43 +040074static void* SpinFn(void* arg) {
75 volatile bool* b = reinterpret_cast<volatile bool*>(arg);
76 while (!*b) {
77 }
78 return NULL;
79}
80
Elliott Hughes4d014e12012-09-07 16:47:54 -070081static void* JoinFn(void* arg) {
82 return reinterpret_cast<void*>(pthread_join(reinterpret_cast<pthread_t>(arg), NULL));
83}
84
Sergey Melnikov10ce9692012-10-26 14:06:43 +040085static void AssertDetached(pthread_t t, bool is_detached) {
86 pthread_attr_t attr;
87 ASSERT_EQ(0, pthread_getattr_np(t, &attr));
88 int detach_state;
89 ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &detach_state));
90 pthread_attr_destroy(&attr);
91 ASSERT_EQ(is_detached, (detach_state == PTHREAD_CREATE_DETACHED));
92}
93
Elliott Hughes9d23e042013-02-15 19:21:51 -080094static void MakeDeadThread(pthread_t& t) {
95 ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, NULL));
96 void* result;
97 ASSERT_EQ(0, pthread_join(t, &result));
98}
99
Elliott Hughes4d014e12012-09-07 16:47:54 -0700100TEST(pthread, pthread_create) {
101 void* expected_result = reinterpret_cast<void*>(123);
102 // Can we create a thread?
103 pthread_t t;
104 ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, expected_result));
105 // If we join, do we get the expected value back?
106 void* result;
107 ASSERT_EQ(0, pthread_join(t, &result));
108 ASSERT_EQ(expected_result, result);
109}
110
Elliott Hughes3e898472013-02-12 16:40:24 +0000111TEST(pthread, pthread_create_EAGAIN) {
112 pthread_attr_t attributes;
113 ASSERT_EQ(0, pthread_attr_init(&attributes));
114 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, static_cast<size_t>(-1) & ~(getpagesize() - 1)));
115
116 pthread_t t;
117 ASSERT_EQ(EAGAIN, pthread_create(&t, &attributes, IdFn, NULL));
118}
119
Elliott Hughes4d014e12012-09-07 16:47:54 -0700120TEST(pthread, pthread_no_join_after_detach) {
121 pthread_t t1;
122 ASSERT_EQ(0, pthread_create(&t1, NULL, SleepFn, reinterpret_cast<void*>(5)));
123
124 // After a pthread_detach...
125 ASSERT_EQ(0, pthread_detach(t1));
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400126 AssertDetached(t1, true);
Elliott Hughes4d014e12012-09-07 16:47:54 -0700127
128 // ...pthread_join should fail.
129 void* result;
130 ASSERT_EQ(EINVAL, pthread_join(t1, &result));
131}
132
133TEST(pthread, pthread_no_op_detach_after_join) {
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400134 bool done = false;
135
Elliott Hughes4d014e12012-09-07 16:47:54 -0700136 pthread_t t1;
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400137 ASSERT_EQ(0, pthread_create(&t1, NULL, SpinFn, &done));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700138
139 // If thread 2 is already waiting to join thread 1...
140 pthread_t t2;
141 ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
142
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400143 sleep(1); // (Give t2 a chance to call pthread_join.)
Elliott Hughes4d014e12012-09-07 16:47:54 -0700144
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400145 // ...a call to pthread_detach on thread 1 will "succeed" (silently fail)...
146 ASSERT_EQ(0, pthread_detach(t1));
147 AssertDetached(t1, false);
148
149 done = true;
150
151 // ...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 -0700152 void* join_result;
153 ASSERT_EQ(0, pthread_join(t2, &join_result));
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700154 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700155}
Elliott Hughes14f19592012-10-29 10:19:44 -0700156
157TEST(pthread, pthread_join_self) {
158 void* result;
159 ASSERT_EQ(EDEADLK, pthread_join(pthread_self(), &result));
160}
Elliott Hughes4f251be2012-11-01 16:33:29 -0700161
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800162struct TestBug37410 {
163 pthread_t main_thread;
164 pthread_mutex_t mutex;
Elliott Hughes4f251be2012-11-01 16:33:29 -0700165
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800166 static void main() {
167 TestBug37410 data;
168 data.main_thread = pthread_self();
169 ASSERT_EQ(0, pthread_mutex_init(&data.mutex, NULL));
170 ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
171
172 pthread_t t;
173 ASSERT_EQ(0, pthread_create(&t, NULL, TestBug37410::thread_fn, reinterpret_cast<void*>(&data)));
174
175 // Wait for the thread to be running...
176 ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
177 ASSERT_EQ(0, pthread_mutex_unlock(&data.mutex));
178
179 // ...and exit.
180 pthread_exit(NULL);
181 }
182
183 private:
184 static void* thread_fn(void* arg) {
185 TestBug37410* data = reinterpret_cast<TestBug37410*>(arg);
186
187 // Let the main thread know we're running.
188 pthread_mutex_unlock(&data->mutex);
189
190 // And wait for the main thread to exit.
191 pthread_join(data->main_thread, NULL);
192
193 return NULL;
194 }
195};
Elliott Hughes4f251be2012-11-01 16:33:29 -0700196
Elliott Hughes7fd803c2013-02-14 16:33:52 -0800197// Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to
198// run this test (which exits normally) in its own process.
Elliott Hughes4f251be2012-11-01 16:33:29 -0700199TEST(pthread_DeathTest, pthread_bug_37410) {
200 // http://code.google.com/p/android/issues/detail?id=37410
201 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800202 ASSERT_EXIT(TestBug37410::main(), ::testing::ExitedWithCode(0), "");
Elliott Hughes4f251be2012-11-01 16:33:29 -0700203}
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800204
205static void* SignalHandlerFn(void* arg) {
206 sigset_t wait_set;
207 sigfillset(&wait_set);
208 return reinterpret_cast<void*>(sigwait(&wait_set, reinterpret_cast<int*>(arg)));
209}
210
211TEST(pthread, pthread_sigmask) {
Elliott Hughes19e62322013-10-15 11:23:57 -0700212 // Check that SIGUSR1 isn't blocked.
213 sigset_t original_set;
214 sigemptyset(&original_set);
215 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &original_set));
216 ASSERT_FALSE(sigismember(&original_set, SIGUSR1));
217
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800218 // Block SIGUSR1.
219 sigset_t set;
220 sigemptyset(&set);
221 sigaddset(&set, SIGUSR1);
222 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &set, NULL));
223
Elliott Hughes19e62322013-10-15 11:23:57 -0700224 // Check that SIGUSR1 is blocked.
225 sigset_t final_set;
226 sigemptyset(&final_set);
227 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &final_set));
228 ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
229 // ...and that sigprocmask agrees with pthread_sigmask.
230 sigemptyset(&final_set);
231 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, NULL, &final_set));
232 ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
233
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800234 // Spawn a thread that calls sigwait and tells us what it received.
235 pthread_t signal_thread;
236 int received_signal = -1;
237 ASSERT_EQ(0, pthread_create(&signal_thread, NULL, SignalHandlerFn, &received_signal));
238
239 // Send that thread SIGUSR1.
240 pthread_kill(signal_thread, SIGUSR1);
241
242 // See what it got.
243 void* join_result;
244 ASSERT_EQ(0, pthread_join(signal_thread, &join_result));
245 ASSERT_EQ(SIGUSR1, received_signal);
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700246 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
Elliott Hughes19e62322013-10-15 11:23:57 -0700247
248 // Restore the original signal mask.
249 ASSERT_EQ(0, pthread_sigmask(SIG_SETMASK, &original_set, NULL));
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800250}
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800251
Elliott Hughes3e898472013-02-12 16:40:24 +0000252#if __BIONIC__
Elliott Hughes70b24b12013-11-15 11:51:07 -0800253extern "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);
254TEST(pthread, __bionic_clone) {
255 // Check that our hand-written clone assembler sets errno correctly on failure.
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800256 uintptr_t fake_child_stack[16];
257 errno = 0;
Chris Dearmandd003642014-01-04 12:57:39 +0000258 ASSERT_EQ(-1, __bionic_clone(CLONE_THREAD, &fake_child_stack[16], NULL, NULL, NULL, NULL, NULL));
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800259 ASSERT_EQ(EINVAL, errno);
260}
261#endif
Elliott Hughes3e898472013-02-12 16:40:24 +0000262
Elliott Hughes9701d4b2013-02-12 17:20:42 -0800263#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 +0000264TEST(pthread, pthread_setname_np__too_long) {
265 ASSERT_EQ(ERANGE, pthread_setname_np(pthread_self(), "this name is far too long for linux"));
266}
Elliott Hughes9701d4b2013-02-12 17:20:42 -0800267#endif
Elliott Hughes3e898472013-02-12 16:40:24 +0000268
Elliott Hughes9701d4b2013-02-12 17:20:42 -0800269#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 +0000270TEST(pthread, pthread_setname_np__self) {
271 ASSERT_EQ(0, pthread_setname_np(pthread_self(), "short 1"));
272}
Elliott Hughes9701d4b2013-02-12 17:20:42 -0800273#endif
Elliott Hughes3e898472013-02-12 16:40:24 +0000274
Elliott Hughes9701d4b2013-02-12 17:20:42 -0800275#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 +0000276TEST(pthread, pthread_setname_np__other) {
Elliott Hughes40eabe22013-02-14 18:59:37 -0800277 // Emulator kernels don't currently support setting the name of other threads.
278 char* filename = NULL;
279 asprintf(&filename, "/proc/self/task/%d/comm", gettid());
280 struct stat sb;
281 bool has_comm = (stat(filename, &sb) != -1);
282 free(filename);
283
284 if (has_comm) {
285 pthread_t t1;
286 ASSERT_EQ(0, pthread_create(&t1, NULL, SleepFn, reinterpret_cast<void*>(5)));
287 ASSERT_EQ(0, pthread_setname_np(t1, "short 2"));
288 } else {
289 fprintf(stderr, "skipping test: this kernel doesn't have /proc/self/task/tid/comm files!\n");
290 }
Elliott Hughes3e898472013-02-12 16:40:24 +0000291}
Elliott Hughes9701d4b2013-02-12 17:20:42 -0800292#endif
Elliott Hughes3e898472013-02-12 16:40:24 +0000293
Elliott Hughes9701d4b2013-02-12 17:20:42 -0800294#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 +0000295TEST(pthread, pthread_setname_np__no_such_thread) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800296 pthread_t dead_thread;
297 MakeDeadThread(dead_thread);
Elliott Hughes3e898472013-02-12 16:40:24 +0000298
299 // Call pthread_setname_np after thread has already exited.
Elliott Hughesa41ba2f2013-03-21 20:02:35 -0700300 ASSERT_EQ(ESRCH, pthread_setname_np(dead_thread, "short 3"));
Elliott Hughes3e898472013-02-12 16:40:24 +0000301}
Elliott Hughes9701d4b2013-02-12 17:20:42 -0800302#endif
Elliott Hughes9d23e042013-02-15 19:21:51 -0800303
304TEST(pthread, pthread_kill__0) {
305 // Signal 0 just tests that the thread exists, so it's safe to call on ourselves.
306 ASSERT_EQ(0, pthread_kill(pthread_self(), 0));
307}
308
309TEST(pthread, pthread_kill__invalid_signal) {
310 ASSERT_EQ(EINVAL, pthread_kill(pthread_self(), -1));
311}
312
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800313static void pthread_kill__in_signal_handler_helper(int signal_number) {
314 static int count = 0;
315 ASSERT_EQ(SIGALRM, signal_number);
316 if (++count == 1) {
317 // Can we call pthread_kill from a signal handler?
318 ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
319 }
320}
321
322TEST(pthread, pthread_kill__in_signal_handler) {
323 struct sigaction action;
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700324 struct sigaction original_action;
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800325 sigemptyset(&action.sa_mask);
326 action.sa_flags = 0;
327 action.sa_handler = pthread_kill__in_signal_handler_helper;
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700328 ASSERT_EQ(0, sigaction(SIGALRM, &action, &original_action));
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800329 ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700330 ASSERT_EQ(0, sigaction(SIGALRM, &original_action, NULL));
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800331}
332
Elliott Hughes9d23e042013-02-15 19:21:51 -0800333TEST(pthread, pthread_detach__no_such_thread) {
334 pthread_t dead_thread;
335 MakeDeadThread(dead_thread);
336
337 ASSERT_EQ(ESRCH, pthread_detach(dead_thread));
338}
339
Jeff Hao9b06cc32013-08-15 14:51:16 -0700340TEST(pthread, pthread_getcpuclockid__clock_gettime) {
341 pthread_t t;
342 ASSERT_EQ(0, pthread_create(&t, NULL, SleepFn, reinterpret_cast<void*>(5)));
343
344 clockid_t c;
345 ASSERT_EQ(0, pthread_getcpuclockid(t, &c));
346 timespec ts;
347 ASSERT_EQ(0, clock_gettime(c, &ts));
348}
349
Elliott Hughes9d23e042013-02-15 19:21:51 -0800350TEST(pthread, pthread_getcpuclockid__no_such_thread) {
351 pthread_t dead_thread;
352 MakeDeadThread(dead_thread);
353
354 clockid_t c;
355 ASSERT_EQ(ESRCH, pthread_getcpuclockid(dead_thread, &c));
356}
357
358TEST(pthread, pthread_getschedparam__no_such_thread) {
359 pthread_t dead_thread;
360 MakeDeadThread(dead_thread);
361
362 int policy;
363 sched_param param;
364 ASSERT_EQ(ESRCH, pthread_getschedparam(dead_thread, &policy, &param));
365}
366
367TEST(pthread, pthread_setschedparam__no_such_thread) {
368 pthread_t dead_thread;
369 MakeDeadThread(dead_thread);
370
371 int policy = 0;
372 sched_param param;
373 ASSERT_EQ(ESRCH, pthread_setschedparam(dead_thread, policy, &param));
374}
375
376TEST(pthread, pthread_join__no_such_thread) {
377 pthread_t dead_thread;
378 MakeDeadThread(dead_thread);
379
380 void* result;
381 ASSERT_EQ(ESRCH, pthread_join(dead_thread, &result));
382}
383
384TEST(pthread, pthread_kill__no_such_thread) {
385 pthread_t dead_thread;
386 MakeDeadThread(dead_thread);
387
388 ASSERT_EQ(ESRCH, pthread_kill(dead_thread, 0));
389}
msg5550f020d12013-06-06 14:59:28 -0400390
391TEST(pthread, pthread_join__multijoin) {
392 bool done = false;
393
394 pthread_t t1;
395 ASSERT_EQ(0, pthread_create(&t1, NULL, SpinFn, &done));
396
397 pthread_t t2;
398 ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
399
400 sleep(1); // (Give t2 a chance to call pthread_join.)
401
402 // Multiple joins to the same thread should fail.
403 ASSERT_EQ(EINVAL, pthread_join(t1, NULL));
404
405 done = true;
406
407 // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes).
408 void* join_result;
409 ASSERT_EQ(0, pthread_join(t2, &join_result));
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700410 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
msg5550f020d12013-06-06 14:59:28 -0400411}
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700412
Elliott Hughes70b24b12013-11-15 11:51:07 -0800413TEST(pthread, pthread_join__race) {
414 // http://b/11693195 --- pthread_join could return before the thread had actually exited.
415 // If the joiner unmapped the thread's stack, that could lead to SIGSEGV in the thread.
416 for (size_t i = 0; i < 1024; ++i) {
417 size_t stack_size = 64*1024;
418 void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
419
420 pthread_attr_t a;
421 pthread_attr_init(&a);
422 pthread_attr_setstack(&a, stack, stack_size);
423
424 pthread_t t;
425 ASSERT_EQ(0, pthread_create(&t, &a, IdFn, NULL));
426 ASSERT_EQ(0, pthread_join(t, NULL));
427 ASSERT_EQ(0, munmap(stack, stack_size));
428 }
429}
430
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700431static void* GetActualGuardSizeFn(void* arg) {
432 pthread_attr_t attributes;
433 pthread_getattr_np(pthread_self(), &attributes);
434 pthread_attr_getguardsize(&attributes, reinterpret_cast<size_t*>(arg));
435 return NULL;
436}
437
438static size_t GetActualGuardSize(const pthread_attr_t& attributes) {
439 size_t result;
440 pthread_t t;
441 pthread_create(&t, &attributes, GetActualGuardSizeFn, &result);
442 void* join_result;
443 pthread_join(t, &join_result);
444 return result;
445}
446
447static void* GetActualStackSizeFn(void* arg) {
448 pthread_attr_t attributes;
449 pthread_getattr_np(pthread_self(), &attributes);
450 pthread_attr_getstacksize(&attributes, reinterpret_cast<size_t*>(arg));
451 return NULL;
452}
453
454static size_t GetActualStackSize(const pthread_attr_t& attributes) {
455 size_t result;
456 pthread_t t;
457 pthread_create(&t, &attributes, GetActualStackSizeFn, &result);
458 void* join_result;
459 pthread_join(t, &join_result);
460 return result;
461}
462
463TEST(pthread, pthread_attr_setguardsize) {
464 pthread_attr_t attributes;
465 ASSERT_EQ(0, pthread_attr_init(&attributes));
466
467 // Get the default guard size.
468 size_t default_guard_size;
469 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &default_guard_size));
470
471 // No such thing as too small: will be rounded up to one page by pthread_create.
472 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 128));
473 size_t guard_size;
474 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
475 ASSERT_EQ(128U, guard_size);
476 ASSERT_EQ(4096U, GetActualGuardSize(attributes));
477
478 // Large enough and a multiple of the page size.
479 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024));
480 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
481 ASSERT_EQ(32*1024U, guard_size);
482
483 // Large enough but not a multiple of the page size; will be rounded up by pthread_create.
484 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024 + 1));
485 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
486 ASSERT_EQ(32*1024U + 1, guard_size);
487}
488
489TEST(pthread, pthread_attr_setstacksize) {
490 pthread_attr_t attributes;
491 ASSERT_EQ(0, pthread_attr_init(&attributes));
492
493 // Get the default stack size.
494 size_t default_stack_size;
495 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &default_stack_size));
496
497 // Too small.
498 ASSERT_EQ(EINVAL, pthread_attr_setstacksize(&attributes, 128));
499 size_t stack_size;
500 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
501 ASSERT_EQ(default_stack_size, stack_size);
502 ASSERT_GE(GetActualStackSize(attributes), default_stack_size);
503
504 // Large enough and a multiple of the page size.
505 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024));
506 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
507 ASSERT_EQ(32*1024U, stack_size);
508 ASSERT_EQ(GetActualStackSize(attributes), 32*1024U);
509
510 // Large enough but not a multiple of the page size; will be rounded up by pthread_create.
511 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024 + 1));
512 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
513 ASSERT_EQ(32*1024U + 1, stack_size);
514#if __BIONIC__
515 // Bionic rounds up, which is what POSIX allows.
516 ASSERT_EQ(GetActualStackSize(attributes), (32 + 4)*1024U);
517#else
518 // glibc rounds down, in violation of POSIX. They document this in their BUGS section.
519 ASSERT_EQ(GetActualStackSize(attributes), 32*1024U);
520#endif
521}
Elliott Hughesc3f11402013-10-30 14:40:09 -0700522
523TEST(pthread, pthread_rwlock_smoke) {
524 pthread_rwlock_t l;
525 ASSERT_EQ(0, pthread_rwlock_init(&l, NULL));
526
527 ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
528 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
529
530 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
531 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
532
533 ASSERT_EQ(0, pthread_rwlock_destroy(&l));
534}
535
536static int gOnceFnCallCount = 0;
537static void OnceFn() {
538 ++gOnceFnCallCount;
539}
540
541TEST(pthread, pthread_once_smoke) {
542 pthread_once_t once_control = PTHREAD_ONCE_INIT;
543 ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
544 ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
545 ASSERT_EQ(1, gOnceFnCallCount);
546}
547
548static int gAtForkPrepareCalls = 0;
549static void AtForkPrepare1() { gAtForkPrepareCalls = (gAtForkPrepareCalls << 4) | 1; }
550static void AtForkPrepare2() { gAtForkPrepareCalls = (gAtForkPrepareCalls << 4) | 2; }
551static int gAtForkParentCalls = 0;
552static void AtForkParent1() { gAtForkParentCalls = (gAtForkParentCalls << 4) | 1; }
553static void AtForkParent2() { gAtForkParentCalls = (gAtForkParentCalls << 4) | 2; }
554static int gAtForkChildCalls = 0;
555static void AtForkChild1() { gAtForkChildCalls = (gAtForkChildCalls << 4) | 1; }
556static void AtForkChild2() { gAtForkChildCalls = (gAtForkChildCalls << 4) | 2; }
557
558TEST(pthread, pthread_atfork) {
559 ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1));
560 ASSERT_EQ(0, pthread_atfork(AtForkPrepare2, AtForkParent2, AtForkChild2));
561
562 int pid = fork();
563 ASSERT_NE(-1, pid) << strerror(errno);
564
565 // Child and parent calls are made in the order they were registered.
566 if (pid == 0) {
567 ASSERT_EQ(0x12, gAtForkChildCalls);
568 _exit(0);
569 }
570 ASSERT_EQ(0x12, gAtForkParentCalls);
571
572 // Prepare calls are made in the reverse order.
573 ASSERT_EQ(0x21, gAtForkPrepareCalls);
574}
575
576TEST(pthread, pthread_attr_getscope) {
577 pthread_attr_t attr;
578 ASSERT_EQ(0, pthread_attr_init(&attr));
579
580 int scope;
581 ASSERT_EQ(0, pthread_attr_getscope(&attr, &scope));
582 ASSERT_EQ(PTHREAD_SCOPE_SYSTEM, scope);
583}