blob: 1e85a54a769e13a883fba51e8925fb817c92d0ca [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>
Narayan Kamath51e6cb32014-03-03 15:38:51 +000025#include <time.h>
Elliott Hughes4d014e12012-09-07 16:47:54 -070026#include <unistd.h>
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070027
Elliott Hughes4b558f52014-03-04 15:58:02 -080028#include "ScopedSignalHandler.h"
29
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070030TEST(pthread, pthread_key_create) {
31 pthread_key_t key;
32 ASSERT_EQ(0, pthread_key_create(&key, NULL));
33 ASSERT_EQ(0, pthread_key_delete(key));
34 // Can't delete a key that's already been deleted.
35 ASSERT_EQ(EINVAL, pthread_key_delete(key));
36}
Elliott Hughes4d014e12012-09-07 16:47:54 -070037
Elliott Hughes44b53ad2013-02-11 20:18:47 +000038TEST(pthread, pthread_key_create_lots) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -080039#if defined(__BIONIC__) // glibc uses keys internally that its sysconf value doesn't account for.
Elliott Hughes18876212013-12-12 11:02:41 -080040 // POSIX says PTHREAD_KEYS_MAX should be at least 128.
41 ASSERT_GE(PTHREAD_KEYS_MAX, 128);
Elliott Hughes718a5b52014-01-28 17:02:03 -080042
43 int sysconf_max = sysconf(_SC_THREAD_KEYS_MAX);
44
Elliott Hughes18876212013-12-12 11:02:41 -080045 // sysconf shouldn't return a smaller value.
Elliott Hughes718a5b52014-01-28 17:02:03 -080046 ASSERT_GE(sysconf_max, PTHREAD_KEYS_MAX);
Elliott Hughes18876212013-12-12 11:02:41 -080047
Elliott Hughes44b53ad2013-02-11 20:18:47 +000048 // We can allocate _SC_THREAD_KEYS_MAX keys.
Elliott Hughes718a5b52014-01-28 17:02:03 -080049 sysconf_max -= 2; // (Except that gtest takes two for itself.)
Elliott Hughes44b53ad2013-02-11 20:18:47 +000050 std::vector<pthread_key_t> keys;
Elliott Hughes718a5b52014-01-28 17:02:03 -080051 for (int i = 0; i < sysconf_max; ++i) {
Elliott Hughes44b53ad2013-02-11 20:18:47 +000052 pthread_key_t key;
Elliott Hughes3e898472013-02-12 16:40:24 +000053 // If this fails, it's likely that GLOBAL_INIT_THREAD_LOCAL_BUFFER_COUNT is wrong.
Elliott Hughes718a5b52014-01-28 17:02:03 -080054 ASSERT_EQ(0, pthread_key_create(&key, NULL)) << i << " of " << sysconf_max;
Elliott Hughes44b53ad2013-02-11 20:18:47 +000055 keys.push_back(key);
56 }
57
58 // ...and that really is the maximum.
59 pthread_key_t key;
60 ASSERT_EQ(EAGAIN, pthread_key_create(&key, NULL));
61
62 // (Don't leak all those keys!)
63 for (size_t i = 0; i < keys.size(); ++i) {
64 ASSERT_EQ(0, pthread_key_delete(keys[i]));
65 }
Christopher Ferrisf04935c2013-12-20 18:43:21 -080066#else // __BIONIC__
67 GTEST_LOG_(INFO) << "This test does nothing.\n";
68#endif // __BIONIC__
Elliott Hughes44b53ad2013-02-11 20:18:47 +000069}
70
Elliott Hughes4d014e12012-09-07 16:47:54 -070071static void* IdFn(void* arg) {
72 return arg;
73}
74
75static void* SleepFn(void* arg) {
Elliott Hughes5b9310e2013-10-02 16:59:05 -070076 sleep(reinterpret_cast<uintptr_t>(arg));
Elliott Hughes4d014e12012-09-07 16:47:54 -070077 return NULL;
78}
79
Sergey Melnikov10ce9692012-10-26 14:06:43 +040080static void* SpinFn(void* arg) {
81 volatile bool* b = reinterpret_cast<volatile bool*>(arg);
82 while (!*b) {
83 }
84 return NULL;
85}
86
Elliott Hughes4d014e12012-09-07 16:47:54 -070087static void* JoinFn(void* arg) {
88 return reinterpret_cast<void*>(pthread_join(reinterpret_cast<pthread_t>(arg), NULL));
89}
90
Sergey Melnikov10ce9692012-10-26 14:06:43 +040091static void AssertDetached(pthread_t t, bool is_detached) {
92 pthread_attr_t attr;
93 ASSERT_EQ(0, pthread_getattr_np(t, &attr));
94 int detach_state;
95 ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &detach_state));
96 pthread_attr_destroy(&attr);
97 ASSERT_EQ(is_detached, (detach_state == PTHREAD_CREATE_DETACHED));
98}
99
Elliott Hughes9d23e042013-02-15 19:21:51 -0800100static void MakeDeadThread(pthread_t& t) {
101 ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, NULL));
102 void* result;
103 ASSERT_EQ(0, pthread_join(t, &result));
104}
105
Elliott Hughes4d014e12012-09-07 16:47:54 -0700106TEST(pthread, pthread_create) {
107 void* expected_result = reinterpret_cast<void*>(123);
108 // Can we create a thread?
109 pthread_t t;
110 ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, expected_result));
111 // If we join, do we get the expected value back?
112 void* result;
113 ASSERT_EQ(0, pthread_join(t, &result));
114 ASSERT_EQ(expected_result, result);
115}
116
Elliott Hughes3e898472013-02-12 16:40:24 +0000117TEST(pthread, pthread_create_EAGAIN) {
118 pthread_attr_t attributes;
119 ASSERT_EQ(0, pthread_attr_init(&attributes));
120 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, static_cast<size_t>(-1) & ~(getpagesize() - 1)));
121
122 pthread_t t;
123 ASSERT_EQ(EAGAIN, pthread_create(&t, &attributes, IdFn, NULL));
124}
125
Elliott Hughes4d014e12012-09-07 16:47:54 -0700126TEST(pthread, pthread_no_join_after_detach) {
127 pthread_t t1;
128 ASSERT_EQ(0, pthread_create(&t1, NULL, SleepFn, reinterpret_cast<void*>(5)));
129
130 // After a pthread_detach...
131 ASSERT_EQ(0, pthread_detach(t1));
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400132 AssertDetached(t1, true);
Elliott Hughes4d014e12012-09-07 16:47:54 -0700133
134 // ...pthread_join should fail.
135 void* result;
136 ASSERT_EQ(EINVAL, pthread_join(t1, &result));
137}
138
139TEST(pthread, pthread_no_op_detach_after_join) {
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400140 bool done = false;
141
Elliott Hughes4d014e12012-09-07 16:47:54 -0700142 pthread_t t1;
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400143 ASSERT_EQ(0, pthread_create(&t1, NULL, SpinFn, &done));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700144
145 // If thread 2 is already waiting to join thread 1...
146 pthread_t t2;
147 ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
148
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400149 sleep(1); // (Give t2 a chance to call pthread_join.)
Elliott Hughes4d014e12012-09-07 16:47:54 -0700150
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400151 // ...a call to pthread_detach on thread 1 will "succeed" (silently fail)...
152 ASSERT_EQ(0, pthread_detach(t1));
153 AssertDetached(t1, false);
154
155 done = true;
156
157 // ...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 -0700158 void* join_result;
159 ASSERT_EQ(0, pthread_join(t2, &join_result));
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700160 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700161}
Elliott Hughes14f19592012-10-29 10:19:44 -0700162
163TEST(pthread, pthread_join_self) {
164 void* result;
165 ASSERT_EQ(EDEADLK, pthread_join(pthread_self(), &result));
166}
Elliott Hughes4f251be2012-11-01 16:33:29 -0700167
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800168struct TestBug37410 {
169 pthread_t main_thread;
170 pthread_mutex_t mutex;
Elliott Hughes4f251be2012-11-01 16:33:29 -0700171
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800172 static void main() {
173 TestBug37410 data;
174 data.main_thread = pthread_self();
175 ASSERT_EQ(0, pthread_mutex_init(&data.mutex, NULL));
176 ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
177
178 pthread_t t;
179 ASSERT_EQ(0, pthread_create(&t, NULL, TestBug37410::thread_fn, reinterpret_cast<void*>(&data)));
180
181 // Wait for the thread to be running...
182 ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
183 ASSERT_EQ(0, pthread_mutex_unlock(&data.mutex));
184
185 // ...and exit.
186 pthread_exit(NULL);
187 }
188
189 private:
190 static void* thread_fn(void* arg) {
191 TestBug37410* data = reinterpret_cast<TestBug37410*>(arg);
192
193 // Let the main thread know we're running.
194 pthread_mutex_unlock(&data->mutex);
195
196 // And wait for the main thread to exit.
197 pthread_join(data->main_thread, NULL);
198
199 return NULL;
200 }
201};
Elliott Hughes4f251be2012-11-01 16:33:29 -0700202
Elliott Hughes7fd803c2013-02-14 16:33:52 -0800203// Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to
204// run this test (which exits normally) in its own process.
Elliott Hughes4f251be2012-11-01 16:33:29 -0700205TEST(pthread_DeathTest, pthread_bug_37410) {
206 // http://code.google.com/p/android/issues/detail?id=37410
207 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800208 ASSERT_EXIT(TestBug37410::main(), ::testing::ExitedWithCode(0), "");
Elliott Hughes4f251be2012-11-01 16:33:29 -0700209}
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800210
211static void* SignalHandlerFn(void* arg) {
212 sigset_t wait_set;
213 sigfillset(&wait_set);
214 return reinterpret_cast<void*>(sigwait(&wait_set, reinterpret_cast<int*>(arg)));
215}
216
217TEST(pthread, pthread_sigmask) {
Elliott Hughes19e62322013-10-15 11:23:57 -0700218 // Check that SIGUSR1 isn't blocked.
219 sigset_t original_set;
220 sigemptyset(&original_set);
221 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &original_set));
222 ASSERT_FALSE(sigismember(&original_set, SIGUSR1));
223
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800224 // Block SIGUSR1.
225 sigset_t set;
226 sigemptyset(&set);
227 sigaddset(&set, SIGUSR1);
228 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &set, NULL));
229
Elliott Hughes19e62322013-10-15 11:23:57 -0700230 // Check that SIGUSR1 is blocked.
231 sigset_t final_set;
232 sigemptyset(&final_set);
233 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &final_set));
234 ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
235 // ...and that sigprocmask agrees with pthread_sigmask.
236 sigemptyset(&final_set);
237 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, NULL, &final_set));
238 ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
239
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800240 // Spawn a thread that calls sigwait and tells us what it received.
241 pthread_t signal_thread;
242 int received_signal = -1;
243 ASSERT_EQ(0, pthread_create(&signal_thread, NULL, SignalHandlerFn, &received_signal));
244
245 // Send that thread SIGUSR1.
246 pthread_kill(signal_thread, SIGUSR1);
247
248 // See what it got.
249 void* join_result;
250 ASSERT_EQ(0, pthread_join(signal_thread, &join_result));
251 ASSERT_EQ(SIGUSR1, received_signal);
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700252 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
Elliott Hughes19e62322013-10-15 11:23:57 -0700253
254 // Restore the original signal mask.
255 ASSERT_EQ(0, pthread_sigmask(SIG_SETMASK, &original_set, NULL));
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800256}
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800257
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800258#if defined(__BIONIC__)
Elliott Hughes70b24b12013-11-15 11:51:07 -0800259extern "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 -0800260#endif // __BIONIC__
261
Elliott Hughes70b24b12013-11-15 11:51:07 -0800262TEST(pthread, __bionic_clone) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800263#if defined(__BIONIC__)
Elliott Hughes70b24b12013-11-15 11:51:07 -0800264 // Check that our hand-written clone assembler sets errno correctly on failure.
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800265 uintptr_t fake_child_stack[16];
266 errno = 0;
Chris Dearmandd003642014-01-04 12:57:39 +0000267 ASSERT_EQ(-1, __bionic_clone(CLONE_THREAD, &fake_child_stack[16], NULL, NULL, NULL, NULL, NULL));
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800268 ASSERT_EQ(EINVAL, errno);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800269#else // __BIONIC__
270 GTEST_LOG_(INFO) << "This test does nothing.\n";
271#endif // __BIONIC__
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800272}
Elliott Hughes3e898472013-02-12 16:40:24 +0000273
274TEST(pthread, pthread_setname_np__too_long) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800275#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 +0000276 ASSERT_EQ(ERANGE, pthread_setname_np(pthread_self(), "this name is far too long for linux"));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800277#else // __BIONIC__
278 GTEST_LOG_(INFO) << "This test does nothing.\n";
279#endif // __BIONIC__
Elliott Hughes3e898472013-02-12 16:40:24 +0000280}
281
282TEST(pthread, pthread_setname_np__self) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800283#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 +0000284 ASSERT_EQ(0, pthread_setname_np(pthread_self(), "short 1"));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800285#else // __BIONIC__
286 GTEST_LOG_(INFO) << "This test does nothing.\n";
287#endif // __BIONIC__
Elliott Hughes3e898472013-02-12 16:40:24 +0000288}
289
290TEST(pthread, pthread_setname_np__other) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800291#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 -0800292 // Emulator kernels don't currently support setting the name of other threads.
293 char* filename = NULL;
294 asprintf(&filename, "/proc/self/task/%d/comm", gettid());
295 struct stat sb;
296 bool has_comm = (stat(filename, &sb) != -1);
297 free(filename);
298
299 if (has_comm) {
300 pthread_t t1;
301 ASSERT_EQ(0, pthread_create(&t1, NULL, SleepFn, reinterpret_cast<void*>(5)));
302 ASSERT_EQ(0, pthread_setname_np(t1, "short 2"));
303 } else {
304 fprintf(stderr, "skipping test: this kernel doesn't have /proc/self/task/tid/comm files!\n");
305 }
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800306#else // __BIONIC__
307 GTEST_LOG_(INFO) << "This test does nothing.\n";
308#endif // __BIONIC__
Elliott Hughes3e898472013-02-12 16:40:24 +0000309}
310
311TEST(pthread, pthread_setname_np__no_such_thread) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800312#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 -0800313 pthread_t dead_thread;
314 MakeDeadThread(dead_thread);
Elliott Hughes3e898472013-02-12 16:40:24 +0000315
316 // Call pthread_setname_np after thread has already exited.
Elliott Hughesa41ba2f2013-03-21 20:02:35 -0700317 ASSERT_EQ(ESRCH, pthread_setname_np(dead_thread, "short 3"));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800318#else // __BIONIC__
319 GTEST_LOG_(INFO) << "This test does nothing.\n";
320#endif // __BIONIC__
Elliott Hughes3e898472013-02-12 16:40:24 +0000321}
Elliott Hughes9d23e042013-02-15 19:21:51 -0800322
323TEST(pthread, pthread_kill__0) {
324 // Signal 0 just tests that the thread exists, so it's safe to call on ourselves.
325 ASSERT_EQ(0, pthread_kill(pthread_self(), 0));
326}
327
328TEST(pthread, pthread_kill__invalid_signal) {
329 ASSERT_EQ(EINVAL, pthread_kill(pthread_self(), -1));
330}
331
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800332static void pthread_kill__in_signal_handler_helper(int signal_number) {
333 static int count = 0;
334 ASSERT_EQ(SIGALRM, signal_number);
335 if (++count == 1) {
336 // Can we call pthread_kill from a signal handler?
337 ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
338 }
339}
340
341TEST(pthread, pthread_kill__in_signal_handler) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800342 ScopedSignalHandler ssh(SIGALRM, pthread_kill__in_signal_handler_helper);
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800343 ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
344}
345
Elliott Hughes9d23e042013-02-15 19:21:51 -0800346TEST(pthread, pthread_detach__no_such_thread) {
347 pthread_t dead_thread;
348 MakeDeadThread(dead_thread);
349
350 ASSERT_EQ(ESRCH, pthread_detach(dead_thread));
351}
352
Jeff Hao9b06cc32013-08-15 14:51:16 -0700353TEST(pthread, pthread_getcpuclockid__clock_gettime) {
354 pthread_t t;
355 ASSERT_EQ(0, pthread_create(&t, NULL, SleepFn, reinterpret_cast<void*>(5)));
356
357 clockid_t c;
358 ASSERT_EQ(0, pthread_getcpuclockid(t, &c));
359 timespec ts;
360 ASSERT_EQ(0, clock_gettime(c, &ts));
361}
362
Elliott Hughes9d23e042013-02-15 19:21:51 -0800363TEST(pthread, pthread_getcpuclockid__no_such_thread) {
364 pthread_t dead_thread;
365 MakeDeadThread(dead_thread);
366
367 clockid_t c;
368 ASSERT_EQ(ESRCH, pthread_getcpuclockid(dead_thread, &c));
369}
370
371TEST(pthread, pthread_getschedparam__no_such_thread) {
372 pthread_t dead_thread;
373 MakeDeadThread(dead_thread);
374
375 int policy;
376 sched_param param;
377 ASSERT_EQ(ESRCH, pthread_getschedparam(dead_thread, &policy, &param));
378}
379
380TEST(pthread, pthread_setschedparam__no_such_thread) {
381 pthread_t dead_thread;
382 MakeDeadThread(dead_thread);
383
384 int policy = 0;
385 sched_param param;
386 ASSERT_EQ(ESRCH, pthread_setschedparam(dead_thread, policy, &param));
387}
388
389TEST(pthread, pthread_join__no_such_thread) {
390 pthread_t dead_thread;
391 MakeDeadThread(dead_thread);
392
393 void* result;
394 ASSERT_EQ(ESRCH, pthread_join(dead_thread, &result));
395}
396
397TEST(pthread, pthread_kill__no_such_thread) {
398 pthread_t dead_thread;
399 MakeDeadThread(dead_thread);
400
401 ASSERT_EQ(ESRCH, pthread_kill(dead_thread, 0));
402}
msg5550f020d12013-06-06 14:59:28 -0400403
404TEST(pthread, pthread_join__multijoin) {
405 bool done = false;
406
407 pthread_t t1;
408 ASSERT_EQ(0, pthread_create(&t1, NULL, SpinFn, &done));
409
410 pthread_t t2;
411 ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
412
413 sleep(1); // (Give t2 a chance to call pthread_join.)
414
415 // Multiple joins to the same thread should fail.
416 ASSERT_EQ(EINVAL, pthread_join(t1, NULL));
417
418 done = true;
419
420 // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes).
421 void* join_result;
422 ASSERT_EQ(0, pthread_join(t2, &join_result));
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700423 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
msg5550f020d12013-06-06 14:59:28 -0400424}
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700425
Elliott Hughes70b24b12013-11-15 11:51:07 -0800426TEST(pthread, pthread_join__race) {
427 // http://b/11693195 --- pthread_join could return before the thread had actually exited.
428 // If the joiner unmapped the thread's stack, that could lead to SIGSEGV in the thread.
429 for (size_t i = 0; i < 1024; ++i) {
430 size_t stack_size = 64*1024;
431 void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
432
433 pthread_attr_t a;
434 pthread_attr_init(&a);
435 pthread_attr_setstack(&a, stack, stack_size);
436
437 pthread_t t;
438 ASSERT_EQ(0, pthread_create(&t, &a, IdFn, NULL));
439 ASSERT_EQ(0, pthread_join(t, NULL));
440 ASSERT_EQ(0, munmap(stack, stack_size));
441 }
442}
443
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700444static void* GetActualGuardSizeFn(void* arg) {
445 pthread_attr_t attributes;
446 pthread_getattr_np(pthread_self(), &attributes);
447 pthread_attr_getguardsize(&attributes, reinterpret_cast<size_t*>(arg));
448 return NULL;
449}
450
451static size_t GetActualGuardSize(const pthread_attr_t& attributes) {
452 size_t result;
453 pthread_t t;
454 pthread_create(&t, &attributes, GetActualGuardSizeFn, &result);
455 void* join_result;
456 pthread_join(t, &join_result);
457 return result;
458}
459
460static void* GetActualStackSizeFn(void* arg) {
461 pthread_attr_t attributes;
462 pthread_getattr_np(pthread_self(), &attributes);
463 pthread_attr_getstacksize(&attributes, reinterpret_cast<size_t*>(arg));
464 return NULL;
465}
466
467static size_t GetActualStackSize(const pthread_attr_t& attributes) {
468 size_t result;
469 pthread_t t;
470 pthread_create(&t, &attributes, GetActualStackSizeFn, &result);
471 void* join_result;
472 pthread_join(t, &join_result);
473 return result;
474}
475
476TEST(pthread, pthread_attr_setguardsize) {
477 pthread_attr_t attributes;
478 ASSERT_EQ(0, pthread_attr_init(&attributes));
479
480 // Get the default guard size.
481 size_t default_guard_size;
482 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &default_guard_size));
483
484 // No such thing as too small: will be rounded up to one page by pthread_create.
485 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 128));
486 size_t guard_size;
487 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
488 ASSERT_EQ(128U, guard_size);
489 ASSERT_EQ(4096U, GetActualGuardSize(attributes));
490
491 // Large enough and a multiple of the page size.
492 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024));
493 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
494 ASSERT_EQ(32*1024U, guard_size);
495
496 // Large enough but not a multiple of the page size; will be rounded up by pthread_create.
497 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024 + 1));
498 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
499 ASSERT_EQ(32*1024U + 1, guard_size);
500}
501
502TEST(pthread, pthread_attr_setstacksize) {
503 pthread_attr_t attributes;
504 ASSERT_EQ(0, pthread_attr_init(&attributes));
505
506 // Get the default stack size.
507 size_t default_stack_size;
508 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &default_stack_size));
509
510 // Too small.
511 ASSERT_EQ(EINVAL, pthread_attr_setstacksize(&attributes, 128));
512 size_t stack_size;
513 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
514 ASSERT_EQ(default_stack_size, stack_size);
515 ASSERT_GE(GetActualStackSize(attributes), default_stack_size);
516
517 // Large enough and a multiple of the page size.
518 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024));
519 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
520 ASSERT_EQ(32*1024U, stack_size);
521 ASSERT_EQ(GetActualStackSize(attributes), 32*1024U);
522
523 // Large enough but not a multiple of the page size; will be rounded up by pthread_create.
524 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024 + 1));
525 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
526 ASSERT_EQ(32*1024U + 1, stack_size);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800527#if defined(__BIONIC__)
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700528 // Bionic rounds up, which is what POSIX allows.
529 ASSERT_EQ(GetActualStackSize(attributes), (32 + 4)*1024U);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800530#else // __BIONIC__
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700531 // glibc rounds down, in violation of POSIX. They document this in their BUGS section.
532 ASSERT_EQ(GetActualStackSize(attributes), 32*1024U);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800533#endif // __BIONIC__
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700534}
Elliott Hughesc3f11402013-10-30 14:40:09 -0700535
536TEST(pthread, pthread_rwlock_smoke) {
537 pthread_rwlock_t l;
538 ASSERT_EQ(0, pthread_rwlock_init(&l, NULL));
539
540 ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
541 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
542
543 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
544 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
545
546 ASSERT_EQ(0, pthread_rwlock_destroy(&l));
547}
548
549static int gOnceFnCallCount = 0;
550static void OnceFn() {
551 ++gOnceFnCallCount;
552}
553
554TEST(pthread, pthread_once_smoke) {
555 pthread_once_t once_control = PTHREAD_ONCE_INIT;
556 ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
557 ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
558 ASSERT_EQ(1, gOnceFnCallCount);
559}
560
561static int gAtForkPrepareCalls = 0;
562static void AtForkPrepare1() { gAtForkPrepareCalls = (gAtForkPrepareCalls << 4) | 1; }
563static void AtForkPrepare2() { gAtForkPrepareCalls = (gAtForkPrepareCalls << 4) | 2; }
564static int gAtForkParentCalls = 0;
565static void AtForkParent1() { gAtForkParentCalls = (gAtForkParentCalls << 4) | 1; }
566static void AtForkParent2() { gAtForkParentCalls = (gAtForkParentCalls << 4) | 2; }
567static int gAtForkChildCalls = 0;
568static void AtForkChild1() { gAtForkChildCalls = (gAtForkChildCalls << 4) | 1; }
569static void AtForkChild2() { gAtForkChildCalls = (gAtForkChildCalls << 4) | 2; }
570
571TEST(pthread, pthread_atfork) {
572 ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1));
573 ASSERT_EQ(0, pthread_atfork(AtForkPrepare2, AtForkParent2, AtForkChild2));
574
575 int pid = fork();
576 ASSERT_NE(-1, pid) << strerror(errno);
577
578 // Child and parent calls are made in the order they were registered.
579 if (pid == 0) {
580 ASSERT_EQ(0x12, gAtForkChildCalls);
581 _exit(0);
582 }
583 ASSERT_EQ(0x12, gAtForkParentCalls);
584
585 // Prepare calls are made in the reverse order.
586 ASSERT_EQ(0x21, gAtForkPrepareCalls);
587}
588
589TEST(pthread, pthread_attr_getscope) {
590 pthread_attr_t attr;
591 ASSERT_EQ(0, pthread_attr_init(&attr));
592
593 int scope;
594 ASSERT_EQ(0, pthread_attr_getscope(&attr, &scope));
595 ASSERT_EQ(PTHREAD_SCOPE_SYSTEM, scope);
596}
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000597
598TEST(pthread, pthread_condattr_init) {
599 pthread_condattr_t attr;
600 pthread_condattr_init(&attr);
601
602 clockid_t clock;
603 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
604 ASSERT_EQ(CLOCK_REALTIME, clock);
605
606 int pshared;
607 ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared));
608 ASSERT_EQ(PTHREAD_PROCESS_PRIVATE, pshared);
609}
610
611TEST(pthread, pthread_condattr_setclock) {
612 pthread_condattr_t attr;
613 pthread_condattr_init(&attr);
614
615 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_REALTIME));
616 clockid_t clock;
617 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
618 ASSERT_EQ(CLOCK_REALTIME, clock);
619
620 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC));
621 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
622 ASSERT_EQ(CLOCK_MONOTONIC, clock);
623
624 ASSERT_EQ(EINVAL, pthread_condattr_setclock(&attr, CLOCK_PROCESS_CPUTIME_ID));
625}
626
627TEST(pthread, pthread_cond_broadcast__preserves_condattr_flags) {
628#if defined(__BIONIC__) // This tests a bionic implementation detail.
629 pthread_condattr_t attr;
630 pthread_condattr_init(&attr);
631
632 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC));
633 ASSERT_EQ(0, pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED));
634
635 pthread_cond_t cond_var;
636 ASSERT_EQ(0, pthread_cond_init(&cond_var, &attr));
637
638 ASSERT_EQ(0, pthread_cond_signal(&cond_var));
639 ASSERT_EQ(0, pthread_cond_broadcast(&cond_var));
640
641 attr = static_cast<pthread_condattr_t>(cond_var.value);
642 clockid_t clock;
643 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
644 ASSERT_EQ(CLOCK_MONOTONIC, clock);
645 int pshared;
646 ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared));
647 ASSERT_EQ(PTHREAD_PROCESS_SHARED, pshared);
648#else // __BIONIC__
649 GTEST_LOG_(INFO) << "This test does nothing.\n";
650#endif // __BIONIC__
651}
Elliott Hughes0e714a52014-03-03 16:42:47 -0800652
653TEST(pthread, pthread_mutex_timedlock) {
654 pthread_mutex_t m;
655 ASSERT_EQ(0, pthread_mutex_init(&m, NULL));
656
657 // If the mutex is already locked, pthread_mutex_timedlock should time out.
658 ASSERT_EQ(0, pthread_mutex_lock(&m));
659
660 timespec ts;
661 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
662 ts.tv_nsec += 1;
663 ASSERT_EQ(ETIMEDOUT, pthread_mutex_timedlock(&m, &ts));
664
665 // If the mutex is unlocked, pthread_mutex_timedlock should succeed.
666 ASSERT_EQ(0, pthread_mutex_unlock(&m));
667
668 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
669 ts.tv_nsec += 1;
670 ASSERT_EQ(0, pthread_mutex_timedlock(&m, &ts));
671
672 ASSERT_EQ(0, pthread_mutex_unlock(&m));
673 ASSERT_EQ(0, pthread_mutex_destroy(&m));
674}