blob: d2341188beff50668c19e8ad6e0e935d0dbb283c [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>
Christopher Ferrisf04935c2013-12-20 18:43:21 -080023#include <signal.h>
Elliott Hughes70b24b12013-11-15 11:51:07 -080024#include <sys/mman.h>
Elliott Hughes4d014e12012-09-07 16:47:54 -070025#include <unistd.h>
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070026
27TEST(pthread, pthread_key_create) {
28 pthread_key_t key;
29 ASSERT_EQ(0, pthread_key_create(&key, NULL));
30 ASSERT_EQ(0, pthread_key_delete(key));
31 // Can't delete a key that's already been deleted.
32 ASSERT_EQ(EINVAL, pthread_key_delete(key));
33}
Elliott Hughes4d014e12012-09-07 16:47:54 -070034
Elliott Hughes44b53ad2013-02-11 20:18:47 +000035TEST(pthread, pthread_key_create_lots) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -080036#if defined(__BIONIC__) // glibc uses keys internally that its sysconf value doesn't account for.
Elliott Hughes18876212013-12-12 11:02:41 -080037 // POSIX says PTHREAD_KEYS_MAX should be at least 128.
38 ASSERT_GE(PTHREAD_KEYS_MAX, 128);
Elliott Hughes718a5b52014-01-28 17:02:03 -080039
40 int sysconf_max = sysconf(_SC_THREAD_KEYS_MAX);
41
Elliott Hughes18876212013-12-12 11:02:41 -080042 // sysconf shouldn't return a smaller value.
Elliott Hughes718a5b52014-01-28 17:02:03 -080043 ASSERT_GE(sysconf_max, PTHREAD_KEYS_MAX);
Elliott Hughes18876212013-12-12 11:02:41 -080044
Elliott Hughes44b53ad2013-02-11 20:18:47 +000045 // We can allocate _SC_THREAD_KEYS_MAX keys.
Elliott Hughes718a5b52014-01-28 17:02:03 -080046 sysconf_max -= 2; // (Except that gtest takes two for itself.)
Elliott Hughes44b53ad2013-02-11 20:18:47 +000047 std::vector<pthread_key_t> keys;
Elliott Hughes718a5b52014-01-28 17:02:03 -080048 for (int i = 0; i < sysconf_max; ++i) {
Elliott Hughes44b53ad2013-02-11 20:18:47 +000049 pthread_key_t key;
Elliott Hughes3e898472013-02-12 16:40:24 +000050 // If this fails, it's likely that GLOBAL_INIT_THREAD_LOCAL_BUFFER_COUNT is wrong.
Elliott Hughes718a5b52014-01-28 17:02:03 -080051 ASSERT_EQ(0, pthread_key_create(&key, NULL)) << i << " of " << sysconf_max;
Elliott Hughes44b53ad2013-02-11 20:18:47 +000052 keys.push_back(key);
53 }
54
55 // ...and that really is the maximum.
56 pthread_key_t key;
57 ASSERT_EQ(EAGAIN, pthread_key_create(&key, NULL));
58
59 // (Don't leak all those keys!)
60 for (size_t i = 0; i < keys.size(); ++i) {
61 ASSERT_EQ(0, pthread_key_delete(keys[i]));
62 }
Christopher Ferrisf04935c2013-12-20 18:43:21 -080063#else // __BIONIC__
64 GTEST_LOG_(INFO) << "This test does nothing.\n";
65#endif // __BIONIC__
Elliott Hughes44b53ad2013-02-11 20:18:47 +000066}
67
Elliott Hughes4d014e12012-09-07 16:47:54 -070068static void* IdFn(void* arg) {
69 return arg;
70}
71
72static void* SleepFn(void* arg) {
Elliott Hughes5b9310e2013-10-02 16:59:05 -070073 sleep(reinterpret_cast<uintptr_t>(arg));
Elliott Hughes4d014e12012-09-07 16:47:54 -070074 return NULL;
75}
76
Sergey Melnikov10ce9692012-10-26 14:06:43 +040077static void* SpinFn(void* arg) {
78 volatile bool* b = reinterpret_cast<volatile bool*>(arg);
79 while (!*b) {
80 }
81 return NULL;
82}
83
Elliott Hughes4d014e12012-09-07 16:47:54 -070084static void* JoinFn(void* arg) {
85 return reinterpret_cast<void*>(pthread_join(reinterpret_cast<pthread_t>(arg), NULL));
86}
87
Sergey Melnikov10ce9692012-10-26 14:06:43 +040088static void AssertDetached(pthread_t t, bool is_detached) {
89 pthread_attr_t attr;
90 ASSERT_EQ(0, pthread_getattr_np(t, &attr));
91 int detach_state;
92 ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &detach_state));
93 pthread_attr_destroy(&attr);
94 ASSERT_EQ(is_detached, (detach_state == PTHREAD_CREATE_DETACHED));
95}
96
Elliott Hughes9d23e042013-02-15 19:21:51 -080097static void MakeDeadThread(pthread_t& t) {
98 ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, NULL));
99 void* result;
100 ASSERT_EQ(0, pthread_join(t, &result));
101}
102
Elliott Hughes4d014e12012-09-07 16:47:54 -0700103TEST(pthread, pthread_create) {
104 void* expected_result = reinterpret_cast<void*>(123);
105 // Can we create a thread?
106 pthread_t t;
107 ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, expected_result));
108 // If we join, do we get the expected value back?
109 void* result;
110 ASSERT_EQ(0, pthread_join(t, &result));
111 ASSERT_EQ(expected_result, result);
112}
113
Elliott Hughes3e898472013-02-12 16:40:24 +0000114TEST(pthread, pthread_create_EAGAIN) {
115 pthread_attr_t attributes;
116 ASSERT_EQ(0, pthread_attr_init(&attributes));
117 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, static_cast<size_t>(-1) & ~(getpagesize() - 1)));
118
119 pthread_t t;
120 ASSERT_EQ(EAGAIN, pthread_create(&t, &attributes, IdFn, NULL));
121}
122
Elliott Hughes4d014e12012-09-07 16:47:54 -0700123TEST(pthread, pthread_no_join_after_detach) {
124 pthread_t t1;
125 ASSERT_EQ(0, pthread_create(&t1, NULL, SleepFn, reinterpret_cast<void*>(5)));
126
127 // After a pthread_detach...
128 ASSERT_EQ(0, pthread_detach(t1));
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400129 AssertDetached(t1, true);
Elliott Hughes4d014e12012-09-07 16:47:54 -0700130
131 // ...pthread_join should fail.
132 void* result;
133 ASSERT_EQ(EINVAL, pthread_join(t1, &result));
134}
135
136TEST(pthread, pthread_no_op_detach_after_join) {
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400137 bool done = false;
138
Elliott Hughes4d014e12012-09-07 16:47:54 -0700139 pthread_t t1;
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400140 ASSERT_EQ(0, pthread_create(&t1, NULL, SpinFn, &done));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700141
142 // If thread 2 is already waiting to join thread 1...
143 pthread_t t2;
144 ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
145
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400146 sleep(1); // (Give t2 a chance to call pthread_join.)
Elliott Hughes4d014e12012-09-07 16:47:54 -0700147
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400148 // ...a call to pthread_detach on thread 1 will "succeed" (silently fail)...
149 ASSERT_EQ(0, pthread_detach(t1));
150 AssertDetached(t1, false);
151
152 done = true;
153
154 // ...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 -0700155 void* join_result;
156 ASSERT_EQ(0, pthread_join(t2, &join_result));
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700157 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700158}
Elliott Hughes14f19592012-10-29 10:19:44 -0700159
160TEST(pthread, pthread_join_self) {
161 void* result;
162 ASSERT_EQ(EDEADLK, pthread_join(pthread_self(), &result));
163}
Elliott Hughes4f251be2012-11-01 16:33:29 -0700164
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800165struct TestBug37410 {
166 pthread_t main_thread;
167 pthread_mutex_t mutex;
Elliott Hughes4f251be2012-11-01 16:33:29 -0700168
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800169 static void main() {
170 TestBug37410 data;
171 data.main_thread = pthread_self();
172 ASSERT_EQ(0, pthread_mutex_init(&data.mutex, NULL));
173 ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
174
175 pthread_t t;
176 ASSERT_EQ(0, pthread_create(&t, NULL, TestBug37410::thread_fn, reinterpret_cast<void*>(&data)));
177
178 // Wait for the thread to be running...
179 ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
180 ASSERT_EQ(0, pthread_mutex_unlock(&data.mutex));
181
182 // ...and exit.
183 pthread_exit(NULL);
184 }
185
186 private:
187 static void* thread_fn(void* arg) {
188 TestBug37410* data = reinterpret_cast<TestBug37410*>(arg);
189
190 // Let the main thread know we're running.
191 pthread_mutex_unlock(&data->mutex);
192
193 // And wait for the main thread to exit.
194 pthread_join(data->main_thread, NULL);
195
196 return NULL;
197 }
198};
Elliott Hughes4f251be2012-11-01 16:33:29 -0700199
Elliott Hughes7fd803c2013-02-14 16:33:52 -0800200// Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to
201// run this test (which exits normally) in its own process.
Elliott Hughes4f251be2012-11-01 16:33:29 -0700202TEST(pthread_DeathTest, pthread_bug_37410) {
203 // http://code.google.com/p/android/issues/detail?id=37410
204 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800205 ASSERT_EXIT(TestBug37410::main(), ::testing::ExitedWithCode(0), "");
Elliott Hughes4f251be2012-11-01 16:33:29 -0700206}
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800207
208static void* SignalHandlerFn(void* arg) {
209 sigset_t wait_set;
210 sigfillset(&wait_set);
211 return reinterpret_cast<void*>(sigwait(&wait_set, reinterpret_cast<int*>(arg)));
212}
213
214TEST(pthread, pthread_sigmask) {
Elliott Hughes19e62322013-10-15 11:23:57 -0700215 // Check that SIGUSR1 isn't blocked.
216 sigset_t original_set;
217 sigemptyset(&original_set);
218 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &original_set));
219 ASSERT_FALSE(sigismember(&original_set, SIGUSR1));
220
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800221 // Block SIGUSR1.
222 sigset_t set;
223 sigemptyset(&set);
224 sigaddset(&set, SIGUSR1);
225 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &set, NULL));
226
Elliott Hughes19e62322013-10-15 11:23:57 -0700227 // Check that SIGUSR1 is blocked.
228 sigset_t final_set;
229 sigemptyset(&final_set);
230 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &final_set));
231 ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
232 // ...and that sigprocmask agrees with pthread_sigmask.
233 sigemptyset(&final_set);
234 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, NULL, &final_set));
235 ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
236
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800237 // Spawn a thread that calls sigwait and tells us what it received.
238 pthread_t signal_thread;
239 int received_signal = -1;
240 ASSERT_EQ(0, pthread_create(&signal_thread, NULL, SignalHandlerFn, &received_signal));
241
242 // Send that thread SIGUSR1.
243 pthread_kill(signal_thread, SIGUSR1);
244
245 // See what it got.
246 void* join_result;
247 ASSERT_EQ(0, pthread_join(signal_thread, &join_result));
248 ASSERT_EQ(SIGUSR1, received_signal);
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700249 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
Elliott Hughes19e62322013-10-15 11:23:57 -0700250
251 // Restore the original signal mask.
252 ASSERT_EQ(0, pthread_sigmask(SIG_SETMASK, &original_set, NULL));
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800253}
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800254
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800255#if defined(__BIONIC__)
Elliott Hughes70b24b12013-11-15 11:51:07 -0800256extern "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);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800257#endif // __BIONIC__
258
Elliott Hughes70b24b12013-11-15 11:51:07 -0800259TEST(pthread, __bionic_clone) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800260#if defined(__BIONIC__)
Elliott Hughes70b24b12013-11-15 11:51:07 -0800261 // Check that our hand-written clone assembler sets errno correctly on failure.
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800262 uintptr_t fake_child_stack[16];
263 errno = 0;
Chris Dearmandd003642014-01-04 12:57:39 +0000264 ASSERT_EQ(-1, __bionic_clone(CLONE_THREAD, &fake_child_stack[16], NULL, NULL, NULL, NULL, NULL));
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800265 ASSERT_EQ(EINVAL, errno);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800266#else // __BIONIC__
267 GTEST_LOG_(INFO) << "This test does nothing.\n";
268#endif // __BIONIC__
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800269}
Elliott Hughes3e898472013-02-12 16:40:24 +0000270
271TEST(pthread, pthread_setname_np__too_long) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800272#if defined(__BIONIC__) // Not all build servers have a new enough glibc? TODO: remove when they're on gprecise.
Elliott Hughes3e898472013-02-12 16:40:24 +0000273 ASSERT_EQ(ERANGE, pthread_setname_np(pthread_self(), "this name is far too long for linux"));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800274#else // __BIONIC__
275 GTEST_LOG_(INFO) << "This test does nothing.\n";
276#endif // __BIONIC__
Elliott Hughes3e898472013-02-12 16:40:24 +0000277}
278
279TEST(pthread, pthread_setname_np__self) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800280#if defined(__BIONIC__) // Not all build servers have a new enough glibc? TODO: remove when they're on gprecise.
Elliott Hughes3e898472013-02-12 16:40:24 +0000281 ASSERT_EQ(0, pthread_setname_np(pthread_self(), "short 1"));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800282#else // __BIONIC__
283 GTEST_LOG_(INFO) << "This test does nothing.\n";
284#endif // __BIONIC__
Elliott Hughes3e898472013-02-12 16:40:24 +0000285}
286
287TEST(pthread, pthread_setname_np__other) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800288#if defined(__BIONIC__) // Not all build servers have a new enough glibc? TODO: remove when they're on gprecise.
Elliott Hughes40eabe22013-02-14 18:59:37 -0800289 // Emulator kernels don't currently support setting the name of other threads.
290 char* filename = NULL;
291 asprintf(&filename, "/proc/self/task/%d/comm", gettid());
292 struct stat sb;
293 bool has_comm = (stat(filename, &sb) != -1);
294 free(filename);
295
296 if (has_comm) {
297 pthread_t t1;
298 ASSERT_EQ(0, pthread_create(&t1, NULL, SleepFn, reinterpret_cast<void*>(5)));
299 ASSERT_EQ(0, pthread_setname_np(t1, "short 2"));
300 } else {
301 fprintf(stderr, "skipping test: this kernel doesn't have /proc/self/task/tid/comm files!\n");
302 }
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800303#else // __BIONIC__
304 GTEST_LOG_(INFO) << "This test does nothing.\n";
305#endif // __BIONIC__
Elliott Hughes3e898472013-02-12 16:40:24 +0000306}
307
308TEST(pthread, pthread_setname_np__no_such_thread) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800309#if defined(__BIONIC__) // Not all build servers have a new enough glibc? TODO: remove when they're on gprecise.
Elliott Hughes9d23e042013-02-15 19:21:51 -0800310 pthread_t dead_thread;
311 MakeDeadThread(dead_thread);
Elliott Hughes3e898472013-02-12 16:40:24 +0000312
313 // Call pthread_setname_np after thread has already exited.
Elliott Hughesa41ba2f2013-03-21 20:02:35 -0700314 ASSERT_EQ(ESRCH, pthread_setname_np(dead_thread, "short 3"));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800315#else // __BIONIC__
316 GTEST_LOG_(INFO) << "This test does nothing.\n";
317#endif // __BIONIC__
Elliott Hughes3e898472013-02-12 16:40:24 +0000318}
Elliott Hughes9d23e042013-02-15 19:21:51 -0800319
320TEST(pthread, pthread_kill__0) {
321 // Signal 0 just tests that the thread exists, so it's safe to call on ourselves.
322 ASSERT_EQ(0, pthread_kill(pthread_self(), 0));
323}
324
325TEST(pthread, pthread_kill__invalid_signal) {
326 ASSERT_EQ(EINVAL, pthread_kill(pthread_self(), -1));
327}
328
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800329static void pthread_kill__in_signal_handler_helper(int signal_number) {
330 static int count = 0;
331 ASSERT_EQ(SIGALRM, signal_number);
332 if (++count == 1) {
333 // Can we call pthread_kill from a signal handler?
334 ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
335 }
336}
337
338TEST(pthread, pthread_kill__in_signal_handler) {
339 struct sigaction action;
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700340 struct sigaction original_action;
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800341 sigemptyset(&action.sa_mask);
342 action.sa_flags = 0;
343 action.sa_handler = pthread_kill__in_signal_handler_helper;
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700344 ASSERT_EQ(0, sigaction(SIGALRM, &action, &original_action));
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800345 ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700346 ASSERT_EQ(0, sigaction(SIGALRM, &original_action, NULL));
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800347}
348
Elliott Hughes9d23e042013-02-15 19:21:51 -0800349TEST(pthread, pthread_detach__no_such_thread) {
350 pthread_t dead_thread;
351 MakeDeadThread(dead_thread);
352
353 ASSERT_EQ(ESRCH, pthread_detach(dead_thread));
354}
355
Jeff Hao9b06cc32013-08-15 14:51:16 -0700356TEST(pthread, pthread_getcpuclockid__clock_gettime) {
357 pthread_t t;
358 ASSERT_EQ(0, pthread_create(&t, NULL, SleepFn, reinterpret_cast<void*>(5)));
359
360 clockid_t c;
361 ASSERT_EQ(0, pthread_getcpuclockid(t, &c));
362 timespec ts;
363 ASSERT_EQ(0, clock_gettime(c, &ts));
364}
365
Elliott Hughes9d23e042013-02-15 19:21:51 -0800366TEST(pthread, pthread_getcpuclockid__no_such_thread) {
367 pthread_t dead_thread;
368 MakeDeadThread(dead_thread);
369
370 clockid_t c;
371 ASSERT_EQ(ESRCH, pthread_getcpuclockid(dead_thread, &c));
372}
373
374TEST(pthread, pthread_getschedparam__no_such_thread) {
375 pthread_t dead_thread;
376 MakeDeadThread(dead_thread);
377
378 int policy;
379 sched_param param;
380 ASSERT_EQ(ESRCH, pthread_getschedparam(dead_thread, &policy, &param));
381}
382
383TEST(pthread, pthread_setschedparam__no_such_thread) {
384 pthread_t dead_thread;
385 MakeDeadThread(dead_thread);
386
387 int policy = 0;
388 sched_param param;
389 ASSERT_EQ(ESRCH, pthread_setschedparam(dead_thread, policy, &param));
390}
391
392TEST(pthread, pthread_join__no_such_thread) {
393 pthread_t dead_thread;
394 MakeDeadThread(dead_thread);
395
396 void* result;
397 ASSERT_EQ(ESRCH, pthread_join(dead_thread, &result));
398}
399
400TEST(pthread, pthread_kill__no_such_thread) {
401 pthread_t dead_thread;
402 MakeDeadThread(dead_thread);
403
404 ASSERT_EQ(ESRCH, pthread_kill(dead_thread, 0));
405}
msg5550f020d12013-06-06 14:59:28 -0400406
407TEST(pthread, pthread_join__multijoin) {
408 bool done = false;
409
410 pthread_t t1;
411 ASSERT_EQ(0, pthread_create(&t1, NULL, SpinFn, &done));
412
413 pthread_t t2;
414 ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
415
416 sleep(1); // (Give t2 a chance to call pthread_join.)
417
418 // Multiple joins to the same thread should fail.
419 ASSERT_EQ(EINVAL, pthread_join(t1, NULL));
420
421 done = true;
422
423 // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes).
424 void* join_result;
425 ASSERT_EQ(0, pthread_join(t2, &join_result));
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700426 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
msg5550f020d12013-06-06 14:59:28 -0400427}
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700428
Elliott Hughes70b24b12013-11-15 11:51:07 -0800429TEST(pthread, pthread_join__race) {
430 // http://b/11693195 --- pthread_join could return before the thread had actually exited.
431 // If the joiner unmapped the thread's stack, that could lead to SIGSEGV in the thread.
432 for (size_t i = 0; i < 1024; ++i) {
433 size_t stack_size = 64*1024;
434 void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
435
436 pthread_attr_t a;
437 pthread_attr_init(&a);
438 pthread_attr_setstack(&a, stack, stack_size);
439
440 pthread_t t;
441 ASSERT_EQ(0, pthread_create(&t, &a, IdFn, NULL));
442 ASSERT_EQ(0, pthread_join(t, NULL));
443 ASSERT_EQ(0, munmap(stack, stack_size));
444 }
445}
446
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700447static void* GetActualGuardSizeFn(void* arg) {
448 pthread_attr_t attributes;
449 pthread_getattr_np(pthread_self(), &attributes);
450 pthread_attr_getguardsize(&attributes, reinterpret_cast<size_t*>(arg));
451 return NULL;
452}
453
454static size_t GetActualGuardSize(const pthread_attr_t& attributes) {
455 size_t result;
456 pthread_t t;
457 pthread_create(&t, &attributes, GetActualGuardSizeFn, &result);
458 void* join_result;
459 pthread_join(t, &join_result);
460 return result;
461}
462
463static void* GetActualStackSizeFn(void* arg) {
464 pthread_attr_t attributes;
465 pthread_getattr_np(pthread_self(), &attributes);
466 pthread_attr_getstacksize(&attributes, reinterpret_cast<size_t*>(arg));
467 return NULL;
468}
469
470static size_t GetActualStackSize(const pthread_attr_t& attributes) {
471 size_t result;
472 pthread_t t;
473 pthread_create(&t, &attributes, GetActualStackSizeFn, &result);
474 void* join_result;
475 pthread_join(t, &join_result);
476 return result;
477}
478
479TEST(pthread, pthread_attr_setguardsize) {
480 pthread_attr_t attributes;
481 ASSERT_EQ(0, pthread_attr_init(&attributes));
482
483 // Get the default guard size.
484 size_t default_guard_size;
485 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &default_guard_size));
486
487 // No such thing as too small: will be rounded up to one page by pthread_create.
488 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 128));
489 size_t guard_size;
490 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
491 ASSERT_EQ(128U, guard_size);
492 ASSERT_EQ(4096U, GetActualGuardSize(attributes));
493
494 // Large enough and a multiple of the page size.
495 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024));
496 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
497 ASSERT_EQ(32*1024U, guard_size);
498
499 // Large enough but not a multiple of the page size; will be rounded up by pthread_create.
500 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024 + 1));
501 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
502 ASSERT_EQ(32*1024U + 1, guard_size);
503}
504
505TEST(pthread, pthread_attr_setstacksize) {
506 pthread_attr_t attributes;
507 ASSERT_EQ(0, pthread_attr_init(&attributes));
508
509 // Get the default stack size.
510 size_t default_stack_size;
511 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &default_stack_size));
512
513 // Too small.
514 ASSERT_EQ(EINVAL, pthread_attr_setstacksize(&attributes, 128));
515 size_t stack_size;
516 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
517 ASSERT_EQ(default_stack_size, stack_size);
518 ASSERT_GE(GetActualStackSize(attributes), default_stack_size);
519
520 // Large enough and a multiple of the page size.
521 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024));
522 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
523 ASSERT_EQ(32*1024U, stack_size);
524 ASSERT_EQ(GetActualStackSize(attributes), 32*1024U);
525
526 // Large enough but not a multiple of the page size; will be rounded up by pthread_create.
527 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024 + 1));
528 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
529 ASSERT_EQ(32*1024U + 1, stack_size);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800530#if defined(__BIONIC__)
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700531 // Bionic rounds up, which is what POSIX allows.
532 ASSERT_EQ(GetActualStackSize(attributes), (32 + 4)*1024U);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800533#else // __BIONIC__
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700534 // glibc rounds down, in violation of POSIX. They document this in their BUGS section.
535 ASSERT_EQ(GetActualStackSize(attributes), 32*1024U);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800536#endif // __BIONIC__
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700537}
Elliott Hughesc3f11402013-10-30 14:40:09 -0700538
539TEST(pthread, pthread_rwlock_smoke) {
540 pthread_rwlock_t l;
541 ASSERT_EQ(0, pthread_rwlock_init(&l, NULL));
542
543 ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
544 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
545
546 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
547 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
548
549 ASSERT_EQ(0, pthread_rwlock_destroy(&l));
550}
551
552static int gOnceFnCallCount = 0;
553static void OnceFn() {
554 ++gOnceFnCallCount;
555}
556
557TEST(pthread, pthread_once_smoke) {
558 pthread_once_t once_control = PTHREAD_ONCE_INIT;
559 ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
560 ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
561 ASSERT_EQ(1, gOnceFnCallCount);
562}
563
564static int gAtForkPrepareCalls = 0;
565static void AtForkPrepare1() { gAtForkPrepareCalls = (gAtForkPrepareCalls << 4) | 1; }
566static void AtForkPrepare2() { gAtForkPrepareCalls = (gAtForkPrepareCalls << 4) | 2; }
567static int gAtForkParentCalls = 0;
568static void AtForkParent1() { gAtForkParentCalls = (gAtForkParentCalls << 4) | 1; }
569static void AtForkParent2() { gAtForkParentCalls = (gAtForkParentCalls << 4) | 2; }
570static int gAtForkChildCalls = 0;
571static void AtForkChild1() { gAtForkChildCalls = (gAtForkChildCalls << 4) | 1; }
572static void AtForkChild2() { gAtForkChildCalls = (gAtForkChildCalls << 4) | 2; }
573
574TEST(pthread, pthread_atfork) {
575 ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1));
576 ASSERT_EQ(0, pthread_atfork(AtForkPrepare2, AtForkParent2, AtForkChild2));
577
578 int pid = fork();
579 ASSERT_NE(-1, pid) << strerror(errno);
580
581 // Child and parent calls are made in the order they were registered.
582 if (pid == 0) {
583 ASSERT_EQ(0x12, gAtForkChildCalls);
584 _exit(0);
585 }
586 ASSERT_EQ(0x12, gAtForkParentCalls);
587
588 // Prepare calls are made in the reverse order.
589 ASSERT_EQ(0x21, gAtForkPrepareCalls);
590}
591
592TEST(pthread, pthread_attr_getscope) {
593 pthread_attr_t attr;
594 ASSERT_EQ(0, pthread_attr_init(&attr));
595
596 int scope;
597 ASSERT_EQ(0, pthread_attr_getscope(&attr, &scope));
598 ASSERT_EQ(PTHREAD_SCOPE_SYSTEM, scope);
599}