blob: d481a1d86fe18bd5881df54ca521dd00dc1d1f90 [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
28TEST(pthread, pthread_key_create) {
29 pthread_key_t key;
30 ASSERT_EQ(0, pthread_key_create(&key, NULL));
31 ASSERT_EQ(0, pthread_key_delete(key));
32 // Can't delete a key that's already been deleted.
33 ASSERT_EQ(EINVAL, pthread_key_delete(key));
34}
Elliott Hughes4d014e12012-09-07 16:47:54 -070035
Elliott Hughes44b53ad2013-02-11 20:18:47 +000036TEST(pthread, pthread_key_create_lots) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -080037#if defined(__BIONIC__) // glibc uses keys internally that its sysconf value doesn't account for.
Elliott Hughes18876212013-12-12 11:02:41 -080038 // POSIX says PTHREAD_KEYS_MAX should be at least 128.
39 ASSERT_GE(PTHREAD_KEYS_MAX, 128);
Elliott Hughes718a5b52014-01-28 17:02:03 -080040
41 int sysconf_max = sysconf(_SC_THREAD_KEYS_MAX);
42
Elliott Hughes18876212013-12-12 11:02:41 -080043 // sysconf shouldn't return a smaller value.
Elliott Hughes718a5b52014-01-28 17:02:03 -080044 ASSERT_GE(sysconf_max, PTHREAD_KEYS_MAX);
Elliott Hughes18876212013-12-12 11:02:41 -080045
Elliott Hughes44b53ad2013-02-11 20:18:47 +000046 // We can allocate _SC_THREAD_KEYS_MAX keys.
Elliott Hughes718a5b52014-01-28 17:02:03 -080047 sysconf_max -= 2; // (Except that gtest takes two for itself.)
Elliott Hughes44b53ad2013-02-11 20:18:47 +000048 std::vector<pthread_key_t> keys;
Elliott Hughes718a5b52014-01-28 17:02:03 -080049 for (int i = 0; i < sysconf_max; ++i) {
Elliott Hughes44b53ad2013-02-11 20:18:47 +000050 pthread_key_t key;
Elliott Hughes3e898472013-02-12 16:40:24 +000051 // If this fails, it's likely that GLOBAL_INIT_THREAD_LOCAL_BUFFER_COUNT is wrong.
Elliott Hughes718a5b52014-01-28 17:02:03 -080052 ASSERT_EQ(0, pthread_key_create(&key, NULL)) << i << " of " << sysconf_max;
Elliott Hughes44b53ad2013-02-11 20:18:47 +000053 keys.push_back(key);
54 }
55
56 // ...and that really is the maximum.
57 pthread_key_t key;
58 ASSERT_EQ(EAGAIN, pthread_key_create(&key, NULL));
59
60 // (Don't leak all those keys!)
61 for (size_t i = 0; i < keys.size(); ++i) {
62 ASSERT_EQ(0, pthread_key_delete(keys[i]));
63 }
Christopher Ferrisf04935c2013-12-20 18:43:21 -080064#else // __BIONIC__
65 GTEST_LOG_(INFO) << "This test does nothing.\n";
66#endif // __BIONIC__
Elliott Hughes44b53ad2013-02-11 20:18:47 +000067}
68
Elliott Hughes4d014e12012-09-07 16:47:54 -070069static void* IdFn(void* arg) {
70 return arg;
71}
72
73static void* SleepFn(void* arg) {
Elliott Hughes5b9310e2013-10-02 16:59:05 -070074 sleep(reinterpret_cast<uintptr_t>(arg));
Elliott Hughes4d014e12012-09-07 16:47:54 -070075 return NULL;
76}
77
Sergey Melnikov10ce9692012-10-26 14:06:43 +040078static void* SpinFn(void* arg) {
79 volatile bool* b = reinterpret_cast<volatile bool*>(arg);
80 while (!*b) {
81 }
82 return NULL;
83}
84
Elliott Hughes4d014e12012-09-07 16:47:54 -070085static void* JoinFn(void* arg) {
86 return reinterpret_cast<void*>(pthread_join(reinterpret_cast<pthread_t>(arg), NULL));
87}
88
Sergey Melnikov10ce9692012-10-26 14:06:43 +040089static void AssertDetached(pthread_t t, bool is_detached) {
90 pthread_attr_t attr;
91 ASSERT_EQ(0, pthread_getattr_np(t, &attr));
92 int detach_state;
93 ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &detach_state));
94 pthread_attr_destroy(&attr);
95 ASSERT_EQ(is_detached, (detach_state == PTHREAD_CREATE_DETACHED));
96}
97
Elliott Hughes9d23e042013-02-15 19:21:51 -080098static void MakeDeadThread(pthread_t& t) {
99 ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, NULL));
100 void* result;
101 ASSERT_EQ(0, pthread_join(t, &result));
102}
103
Elliott Hughes4d014e12012-09-07 16:47:54 -0700104TEST(pthread, pthread_create) {
105 void* expected_result = reinterpret_cast<void*>(123);
106 // Can we create a thread?
107 pthread_t t;
108 ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, expected_result));
109 // If we join, do we get the expected value back?
110 void* result;
111 ASSERT_EQ(0, pthread_join(t, &result));
112 ASSERT_EQ(expected_result, result);
113}
114
Elliott Hughes3e898472013-02-12 16:40:24 +0000115TEST(pthread, pthread_create_EAGAIN) {
116 pthread_attr_t attributes;
117 ASSERT_EQ(0, pthread_attr_init(&attributes));
118 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, static_cast<size_t>(-1) & ~(getpagesize() - 1)));
119
120 pthread_t t;
121 ASSERT_EQ(EAGAIN, pthread_create(&t, &attributes, IdFn, NULL));
122}
123
Elliott Hughes4d014e12012-09-07 16:47:54 -0700124TEST(pthread, pthread_no_join_after_detach) {
125 pthread_t t1;
126 ASSERT_EQ(0, pthread_create(&t1, NULL, SleepFn, reinterpret_cast<void*>(5)));
127
128 // After a pthread_detach...
129 ASSERT_EQ(0, pthread_detach(t1));
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400130 AssertDetached(t1, true);
Elliott Hughes4d014e12012-09-07 16:47:54 -0700131
132 // ...pthread_join should fail.
133 void* result;
134 ASSERT_EQ(EINVAL, pthread_join(t1, &result));
135}
136
137TEST(pthread, pthread_no_op_detach_after_join) {
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400138 bool done = false;
139
Elliott Hughes4d014e12012-09-07 16:47:54 -0700140 pthread_t t1;
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400141 ASSERT_EQ(0, pthread_create(&t1, NULL, SpinFn, &done));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700142
143 // If thread 2 is already waiting to join thread 1...
144 pthread_t t2;
145 ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
146
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400147 sleep(1); // (Give t2 a chance to call pthread_join.)
Elliott Hughes4d014e12012-09-07 16:47:54 -0700148
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400149 // ...a call to pthread_detach on thread 1 will "succeed" (silently fail)...
150 ASSERT_EQ(0, pthread_detach(t1));
151 AssertDetached(t1, false);
152
153 done = true;
154
155 // ...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 -0700156 void* join_result;
157 ASSERT_EQ(0, pthread_join(t2, &join_result));
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700158 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700159}
Elliott Hughes14f19592012-10-29 10:19:44 -0700160
161TEST(pthread, pthread_join_self) {
162 void* result;
163 ASSERT_EQ(EDEADLK, pthread_join(pthread_self(), &result));
164}
Elliott Hughes4f251be2012-11-01 16:33:29 -0700165
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800166struct TestBug37410 {
167 pthread_t main_thread;
168 pthread_mutex_t mutex;
Elliott Hughes4f251be2012-11-01 16:33:29 -0700169
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800170 static void main() {
171 TestBug37410 data;
172 data.main_thread = pthread_self();
173 ASSERT_EQ(0, pthread_mutex_init(&data.mutex, NULL));
174 ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
175
176 pthread_t t;
177 ASSERT_EQ(0, pthread_create(&t, NULL, TestBug37410::thread_fn, reinterpret_cast<void*>(&data)));
178
179 // Wait for the thread to be running...
180 ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
181 ASSERT_EQ(0, pthread_mutex_unlock(&data.mutex));
182
183 // ...and exit.
184 pthread_exit(NULL);
185 }
186
187 private:
188 static void* thread_fn(void* arg) {
189 TestBug37410* data = reinterpret_cast<TestBug37410*>(arg);
190
191 // Let the main thread know we're running.
192 pthread_mutex_unlock(&data->mutex);
193
194 // And wait for the main thread to exit.
195 pthread_join(data->main_thread, NULL);
196
197 return NULL;
198 }
199};
Elliott Hughes4f251be2012-11-01 16:33:29 -0700200
Elliott Hughes7fd803c2013-02-14 16:33:52 -0800201// Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to
202// run this test (which exits normally) in its own process.
Elliott Hughes4f251be2012-11-01 16:33:29 -0700203TEST(pthread_DeathTest, pthread_bug_37410) {
204 // http://code.google.com/p/android/issues/detail?id=37410
205 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800206 ASSERT_EXIT(TestBug37410::main(), ::testing::ExitedWithCode(0), "");
Elliott Hughes4f251be2012-11-01 16:33:29 -0700207}
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800208
209static void* SignalHandlerFn(void* arg) {
210 sigset_t wait_set;
211 sigfillset(&wait_set);
212 return reinterpret_cast<void*>(sigwait(&wait_set, reinterpret_cast<int*>(arg)));
213}
214
215TEST(pthread, pthread_sigmask) {
Elliott Hughes19e62322013-10-15 11:23:57 -0700216 // Check that SIGUSR1 isn't blocked.
217 sigset_t original_set;
218 sigemptyset(&original_set);
219 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &original_set));
220 ASSERT_FALSE(sigismember(&original_set, SIGUSR1));
221
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800222 // Block SIGUSR1.
223 sigset_t set;
224 sigemptyset(&set);
225 sigaddset(&set, SIGUSR1);
226 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &set, NULL));
227
Elliott Hughes19e62322013-10-15 11:23:57 -0700228 // Check that SIGUSR1 is blocked.
229 sigset_t final_set;
230 sigemptyset(&final_set);
231 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &final_set));
232 ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
233 // ...and that sigprocmask agrees with pthread_sigmask.
234 sigemptyset(&final_set);
235 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, NULL, &final_set));
236 ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
237
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800238 // Spawn a thread that calls sigwait and tells us what it received.
239 pthread_t signal_thread;
240 int received_signal = -1;
241 ASSERT_EQ(0, pthread_create(&signal_thread, NULL, SignalHandlerFn, &received_signal));
242
243 // Send that thread SIGUSR1.
244 pthread_kill(signal_thread, SIGUSR1);
245
246 // See what it got.
247 void* join_result;
248 ASSERT_EQ(0, pthread_join(signal_thread, &join_result));
249 ASSERT_EQ(SIGUSR1, received_signal);
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700250 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
Elliott Hughes19e62322013-10-15 11:23:57 -0700251
252 // Restore the original signal mask.
253 ASSERT_EQ(0, pthread_sigmask(SIG_SETMASK, &original_set, NULL));
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800254}
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800255
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800256#if defined(__BIONIC__)
Elliott Hughes70b24b12013-11-15 11:51:07 -0800257extern "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 -0800258#endif // __BIONIC__
259
Elliott Hughes70b24b12013-11-15 11:51:07 -0800260TEST(pthread, __bionic_clone) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800261#if defined(__BIONIC__)
Elliott Hughes70b24b12013-11-15 11:51:07 -0800262 // Check that our hand-written clone assembler sets errno correctly on failure.
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800263 uintptr_t fake_child_stack[16];
264 errno = 0;
Chris Dearmandd003642014-01-04 12:57:39 +0000265 ASSERT_EQ(-1, __bionic_clone(CLONE_THREAD, &fake_child_stack[16], NULL, NULL, NULL, NULL, NULL));
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800266 ASSERT_EQ(EINVAL, errno);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800267#else // __BIONIC__
268 GTEST_LOG_(INFO) << "This test does nothing.\n";
269#endif // __BIONIC__
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800270}
Elliott Hughes3e898472013-02-12 16:40:24 +0000271
272TEST(pthread, pthread_setname_np__too_long) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800273#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 +0000274 ASSERT_EQ(ERANGE, pthread_setname_np(pthread_self(), "this name is far too long for linux"));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800275#else // __BIONIC__
276 GTEST_LOG_(INFO) << "This test does nothing.\n";
277#endif // __BIONIC__
Elliott Hughes3e898472013-02-12 16:40:24 +0000278}
279
280TEST(pthread, pthread_setname_np__self) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800281#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 +0000282 ASSERT_EQ(0, pthread_setname_np(pthread_self(), "short 1"));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800283#else // __BIONIC__
284 GTEST_LOG_(INFO) << "This test does nothing.\n";
285#endif // __BIONIC__
Elliott Hughes3e898472013-02-12 16:40:24 +0000286}
287
288TEST(pthread, pthread_setname_np__other) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800289#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 -0800290 // Emulator kernels don't currently support setting the name of other threads.
291 char* filename = NULL;
292 asprintf(&filename, "/proc/self/task/%d/comm", gettid());
293 struct stat sb;
294 bool has_comm = (stat(filename, &sb) != -1);
295 free(filename);
296
297 if (has_comm) {
298 pthread_t t1;
299 ASSERT_EQ(0, pthread_create(&t1, NULL, SleepFn, reinterpret_cast<void*>(5)));
300 ASSERT_EQ(0, pthread_setname_np(t1, "short 2"));
301 } else {
302 fprintf(stderr, "skipping test: this kernel doesn't have /proc/self/task/tid/comm files!\n");
303 }
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800304#else // __BIONIC__
305 GTEST_LOG_(INFO) << "This test does nothing.\n";
306#endif // __BIONIC__
Elliott Hughes3e898472013-02-12 16:40:24 +0000307}
308
309TEST(pthread, pthread_setname_np__no_such_thread) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800310#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 -0800311 pthread_t dead_thread;
312 MakeDeadThread(dead_thread);
Elliott Hughes3e898472013-02-12 16:40:24 +0000313
314 // Call pthread_setname_np after thread has already exited.
Elliott Hughesa41ba2f2013-03-21 20:02:35 -0700315 ASSERT_EQ(ESRCH, pthread_setname_np(dead_thread, "short 3"));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800316#else // __BIONIC__
317 GTEST_LOG_(INFO) << "This test does nothing.\n";
318#endif // __BIONIC__
Elliott Hughes3e898472013-02-12 16:40:24 +0000319}
Elliott Hughes9d23e042013-02-15 19:21:51 -0800320
321TEST(pthread, pthread_kill__0) {
322 // Signal 0 just tests that the thread exists, so it's safe to call on ourselves.
323 ASSERT_EQ(0, pthread_kill(pthread_self(), 0));
324}
325
326TEST(pthread, pthread_kill__invalid_signal) {
327 ASSERT_EQ(EINVAL, pthread_kill(pthread_self(), -1));
328}
329
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800330static void pthread_kill__in_signal_handler_helper(int signal_number) {
331 static int count = 0;
332 ASSERT_EQ(SIGALRM, signal_number);
333 if (++count == 1) {
334 // Can we call pthread_kill from a signal handler?
335 ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
336 }
337}
338
339TEST(pthread, pthread_kill__in_signal_handler) {
340 struct sigaction action;
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700341 struct sigaction original_action;
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800342 sigemptyset(&action.sa_mask);
343 action.sa_flags = 0;
344 action.sa_handler = pthread_kill__in_signal_handler_helper;
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700345 ASSERT_EQ(0, sigaction(SIGALRM, &action, &original_action));
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800346 ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
Elliott Hughesc7e9b232013-10-16 22:27:54 -0700347 ASSERT_EQ(0, sigaction(SIGALRM, &original_action, NULL));
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800348}
349
Elliott Hughes9d23e042013-02-15 19:21:51 -0800350TEST(pthread, pthread_detach__no_such_thread) {
351 pthread_t dead_thread;
352 MakeDeadThread(dead_thread);
353
354 ASSERT_EQ(ESRCH, pthread_detach(dead_thread));
355}
356
Jeff Hao9b06cc32013-08-15 14:51:16 -0700357TEST(pthread, pthread_getcpuclockid__clock_gettime) {
358 pthread_t t;
359 ASSERT_EQ(0, pthread_create(&t, NULL, SleepFn, reinterpret_cast<void*>(5)));
360
361 clockid_t c;
362 ASSERT_EQ(0, pthread_getcpuclockid(t, &c));
363 timespec ts;
364 ASSERT_EQ(0, clock_gettime(c, &ts));
365}
366
Elliott Hughes9d23e042013-02-15 19:21:51 -0800367TEST(pthread, pthread_getcpuclockid__no_such_thread) {
368 pthread_t dead_thread;
369 MakeDeadThread(dead_thread);
370
371 clockid_t c;
372 ASSERT_EQ(ESRCH, pthread_getcpuclockid(dead_thread, &c));
373}
374
375TEST(pthread, pthread_getschedparam__no_such_thread) {
376 pthread_t dead_thread;
377 MakeDeadThread(dead_thread);
378
379 int policy;
380 sched_param param;
381 ASSERT_EQ(ESRCH, pthread_getschedparam(dead_thread, &policy, &param));
382}
383
384TEST(pthread, pthread_setschedparam__no_such_thread) {
385 pthread_t dead_thread;
386 MakeDeadThread(dead_thread);
387
388 int policy = 0;
389 sched_param param;
390 ASSERT_EQ(ESRCH, pthread_setschedparam(dead_thread, policy, &param));
391}
392
393TEST(pthread, pthread_join__no_such_thread) {
394 pthread_t dead_thread;
395 MakeDeadThread(dead_thread);
396
397 void* result;
398 ASSERT_EQ(ESRCH, pthread_join(dead_thread, &result));
399}
400
401TEST(pthread, pthread_kill__no_such_thread) {
402 pthread_t dead_thread;
403 MakeDeadThread(dead_thread);
404
405 ASSERT_EQ(ESRCH, pthread_kill(dead_thread, 0));
406}
msg5550f020d12013-06-06 14:59:28 -0400407
408TEST(pthread, pthread_join__multijoin) {
409 bool done = false;
410
411 pthread_t t1;
412 ASSERT_EQ(0, pthread_create(&t1, NULL, SpinFn, &done));
413
414 pthread_t t2;
415 ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
416
417 sleep(1); // (Give t2 a chance to call pthread_join.)
418
419 // Multiple joins to the same thread should fail.
420 ASSERT_EQ(EINVAL, pthread_join(t1, NULL));
421
422 done = true;
423
424 // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes).
425 void* join_result;
426 ASSERT_EQ(0, pthread_join(t2, &join_result));
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700427 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
msg5550f020d12013-06-06 14:59:28 -0400428}
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700429
Elliott Hughes70b24b12013-11-15 11:51:07 -0800430TEST(pthread, pthread_join__race) {
431 // http://b/11693195 --- pthread_join could return before the thread had actually exited.
432 // If the joiner unmapped the thread's stack, that could lead to SIGSEGV in the thread.
433 for (size_t i = 0; i < 1024; ++i) {
434 size_t stack_size = 64*1024;
435 void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
436
437 pthread_attr_t a;
438 pthread_attr_init(&a);
439 pthread_attr_setstack(&a, stack, stack_size);
440
441 pthread_t t;
442 ASSERT_EQ(0, pthread_create(&t, &a, IdFn, NULL));
443 ASSERT_EQ(0, pthread_join(t, NULL));
444 ASSERT_EQ(0, munmap(stack, stack_size));
445 }
446}
447
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700448static void* GetActualGuardSizeFn(void* arg) {
449 pthread_attr_t attributes;
450 pthread_getattr_np(pthread_self(), &attributes);
451 pthread_attr_getguardsize(&attributes, reinterpret_cast<size_t*>(arg));
452 return NULL;
453}
454
455static size_t GetActualGuardSize(const pthread_attr_t& attributes) {
456 size_t result;
457 pthread_t t;
458 pthread_create(&t, &attributes, GetActualGuardSizeFn, &result);
459 void* join_result;
460 pthread_join(t, &join_result);
461 return result;
462}
463
464static void* GetActualStackSizeFn(void* arg) {
465 pthread_attr_t attributes;
466 pthread_getattr_np(pthread_self(), &attributes);
467 pthread_attr_getstacksize(&attributes, reinterpret_cast<size_t*>(arg));
468 return NULL;
469}
470
471static size_t GetActualStackSize(const pthread_attr_t& attributes) {
472 size_t result;
473 pthread_t t;
474 pthread_create(&t, &attributes, GetActualStackSizeFn, &result);
475 void* join_result;
476 pthread_join(t, &join_result);
477 return result;
478}
479
480TEST(pthread, pthread_attr_setguardsize) {
481 pthread_attr_t attributes;
482 ASSERT_EQ(0, pthread_attr_init(&attributes));
483
484 // Get the default guard size.
485 size_t default_guard_size;
486 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &default_guard_size));
487
488 // No such thing as too small: will be rounded up to one page by pthread_create.
489 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 128));
490 size_t guard_size;
491 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
492 ASSERT_EQ(128U, guard_size);
493 ASSERT_EQ(4096U, GetActualGuardSize(attributes));
494
495 // Large enough and a multiple of the page size.
496 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024));
497 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
498 ASSERT_EQ(32*1024U, guard_size);
499
500 // Large enough but not a multiple of the page size; will be rounded up by pthread_create.
501 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024 + 1));
502 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
503 ASSERT_EQ(32*1024U + 1, guard_size);
504}
505
506TEST(pthread, pthread_attr_setstacksize) {
507 pthread_attr_t attributes;
508 ASSERT_EQ(0, pthread_attr_init(&attributes));
509
510 // Get the default stack size.
511 size_t default_stack_size;
512 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &default_stack_size));
513
514 // Too small.
515 ASSERT_EQ(EINVAL, pthread_attr_setstacksize(&attributes, 128));
516 size_t stack_size;
517 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
518 ASSERT_EQ(default_stack_size, stack_size);
519 ASSERT_GE(GetActualStackSize(attributes), default_stack_size);
520
521 // Large enough and a multiple of the page size.
522 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024));
523 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
524 ASSERT_EQ(32*1024U, stack_size);
525 ASSERT_EQ(GetActualStackSize(attributes), 32*1024U);
526
527 // Large enough but not a multiple of the page size; will be rounded up by pthread_create.
528 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024 + 1));
529 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
530 ASSERT_EQ(32*1024U + 1, stack_size);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800531#if defined(__BIONIC__)
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700532 // Bionic rounds up, which is what POSIX allows.
533 ASSERT_EQ(GetActualStackSize(attributes), (32 + 4)*1024U);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800534#else // __BIONIC__
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700535 // glibc rounds down, in violation of POSIX. They document this in their BUGS section.
536 ASSERT_EQ(GetActualStackSize(attributes), 32*1024U);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800537#endif // __BIONIC__
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700538}
Elliott Hughesc3f11402013-10-30 14:40:09 -0700539
540TEST(pthread, pthread_rwlock_smoke) {
541 pthread_rwlock_t l;
542 ASSERT_EQ(0, pthread_rwlock_init(&l, NULL));
543
544 ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
545 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
546
547 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
548 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
549
550 ASSERT_EQ(0, pthread_rwlock_destroy(&l));
551}
552
553static int gOnceFnCallCount = 0;
554static void OnceFn() {
555 ++gOnceFnCallCount;
556}
557
558TEST(pthread, pthread_once_smoke) {
559 pthread_once_t once_control = PTHREAD_ONCE_INIT;
560 ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
561 ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
562 ASSERT_EQ(1, gOnceFnCallCount);
563}
564
565static int gAtForkPrepareCalls = 0;
566static void AtForkPrepare1() { gAtForkPrepareCalls = (gAtForkPrepareCalls << 4) | 1; }
567static void AtForkPrepare2() { gAtForkPrepareCalls = (gAtForkPrepareCalls << 4) | 2; }
568static int gAtForkParentCalls = 0;
569static void AtForkParent1() { gAtForkParentCalls = (gAtForkParentCalls << 4) | 1; }
570static void AtForkParent2() { gAtForkParentCalls = (gAtForkParentCalls << 4) | 2; }
571static int gAtForkChildCalls = 0;
572static void AtForkChild1() { gAtForkChildCalls = (gAtForkChildCalls << 4) | 1; }
573static void AtForkChild2() { gAtForkChildCalls = (gAtForkChildCalls << 4) | 2; }
574
575TEST(pthread, pthread_atfork) {
576 ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1));
577 ASSERT_EQ(0, pthread_atfork(AtForkPrepare2, AtForkParent2, AtForkChild2));
578
579 int pid = fork();
580 ASSERT_NE(-1, pid) << strerror(errno);
581
582 // Child and parent calls are made in the order they were registered.
583 if (pid == 0) {
584 ASSERT_EQ(0x12, gAtForkChildCalls);
585 _exit(0);
586 }
587 ASSERT_EQ(0x12, gAtForkParentCalls);
588
589 // Prepare calls are made in the reverse order.
590 ASSERT_EQ(0x21, gAtForkPrepareCalls);
591}
592
593TEST(pthread, pthread_attr_getscope) {
594 pthread_attr_t attr;
595 ASSERT_EQ(0, pthread_attr_init(&attr));
596
597 int scope;
598 ASSERT_EQ(0, pthread_attr_getscope(&attr, &scope));
599 ASSERT_EQ(PTHREAD_SCOPE_SYSTEM, scope);
600}
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000601
602TEST(pthread, pthread_condattr_init) {
603 pthread_condattr_t attr;
604 pthread_condattr_init(&attr);
605
606 clockid_t clock;
607 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
608 ASSERT_EQ(CLOCK_REALTIME, clock);
609
610 int pshared;
611 ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared));
612 ASSERT_EQ(PTHREAD_PROCESS_PRIVATE, pshared);
613}
614
615TEST(pthread, pthread_condattr_setclock) {
616 pthread_condattr_t attr;
617 pthread_condattr_init(&attr);
618
619 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_REALTIME));
620 clockid_t clock;
621 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
622 ASSERT_EQ(CLOCK_REALTIME, clock);
623
624 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC));
625 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
626 ASSERT_EQ(CLOCK_MONOTONIC, clock);
627
628 ASSERT_EQ(EINVAL, pthread_condattr_setclock(&attr, CLOCK_PROCESS_CPUTIME_ID));
629}
630
631TEST(pthread, pthread_cond_broadcast__preserves_condattr_flags) {
632#if defined(__BIONIC__) // This tests a bionic implementation detail.
633 pthread_condattr_t attr;
634 pthread_condattr_init(&attr);
635
636 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC));
637 ASSERT_EQ(0, pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED));
638
639 pthread_cond_t cond_var;
640 ASSERT_EQ(0, pthread_cond_init(&cond_var, &attr));
641
642 ASSERT_EQ(0, pthread_cond_signal(&cond_var));
643 ASSERT_EQ(0, pthread_cond_broadcast(&cond_var));
644
645 attr = static_cast<pthread_condattr_t>(cond_var.value);
646 clockid_t clock;
647 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
648 ASSERT_EQ(CLOCK_MONOTONIC, clock);
649 int pshared;
650 ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared));
651 ASSERT_EQ(PTHREAD_PROCESS_SHARED, pshared);
652#else // __BIONIC__
653 GTEST_LOG_(INFO) << "This test does nothing.\n";
654#endif // __BIONIC__
655}