blob: 251a230e94489ae095d9a2721e033d68cf07125e [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
Yabin Cui9df70402014-11-05 18:01:01 -080019#include "private/ScopeGuard.h"
20#include "BionicDeathTest.h"
21#include "ScopedSignalHandler.h"
22
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070023#include <errno.h>
Elliott Hughes5b9310e2013-10-02 16:59:05 -070024#include <inttypes.h>
Elliott Hughesb95cf0d2013-07-15 14:51:07 -070025#include <limits.h>
Elliott Hughes04620a32014-03-07 17:59:05 -080026#include <malloc.h>
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070027#include <pthread.h>
Christopher Ferrisf04935c2013-12-20 18:43:21 -080028#include <signal.h>
Yabin Cui140f3672015-02-03 10:32:00 -080029#include <stdio.h>
Elliott Hughes70b24b12013-11-15 11:51:07 -080030#include <sys/mman.h>
Elliott Hughes57b7a612014-08-25 17:26:50 -070031#include <sys/syscall.h>
Narayan Kamath51e6cb32014-03-03 15:38:51 +000032#include <time.h>
Elliott Hughes4d014e12012-09-07 16:47:54 -070033#include <unistd.h>
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070034
Yabin Cui08ee8d22015-02-11 17:04:36 -080035#include <atomic>
36
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070037TEST(pthread, pthread_key_create) {
38 pthread_key_t key;
39 ASSERT_EQ(0, pthread_key_create(&key, NULL));
40 ASSERT_EQ(0, pthread_key_delete(key));
41 // Can't delete a key that's already been deleted.
42 ASSERT_EQ(EINVAL, pthread_key_delete(key));
43}
Elliott Hughes4d014e12012-09-07 16:47:54 -070044
Dan Albertc4bcc752014-09-30 11:48:24 -070045TEST(pthread, pthread_keys_max) {
Yabin Cui6c238f22014-12-11 20:50:41 -080046 // POSIX says PTHREAD_KEYS_MAX should be at least _POSIX_THREAD_KEYS_MAX.
47 ASSERT_GE(PTHREAD_KEYS_MAX, _POSIX_THREAD_KEYS_MAX);
Dan Albertc4bcc752014-09-30 11:48:24 -070048}
Elliott Hughes718a5b52014-01-28 17:02:03 -080049
Yabin Cui6c238f22014-12-11 20:50:41 -080050TEST(pthread, sysconf_SC_THREAD_KEYS_MAX_eq_PTHREAD_KEYS_MAX) {
Dan Albertc4bcc752014-09-30 11:48:24 -070051 int sysconf_max = sysconf(_SC_THREAD_KEYS_MAX);
Yabin Cui6c238f22014-12-11 20:50:41 -080052 ASSERT_EQ(sysconf_max, PTHREAD_KEYS_MAX);
Dan Albertc4bcc752014-09-30 11:48:24 -070053}
54
55TEST(pthread, pthread_key_many_distinct) {
Yabin Cui6c238f22014-12-11 20:50:41 -080056 // As gtest uses pthread keys, we can't allocate exactly PTHREAD_KEYS_MAX
57 // pthread keys, but We should be able to allocate at least this many keys.
58 int nkeys = PTHREAD_KEYS_MAX / 2;
Dan Albertc4bcc752014-09-30 11:48:24 -070059 std::vector<pthread_key_t> keys;
60
61 auto scope_guard = make_scope_guard([&keys]{
62 for (auto key : keys) {
63 EXPECT_EQ(0, pthread_key_delete(key));
64 }
65 });
66
67 for (int i = 0; i < nkeys; ++i) {
68 pthread_key_t key;
69 // If this fails, it's likely that GLOBAL_INIT_THREAD_LOCAL_BUFFER_COUNT is
70 // wrong.
71 ASSERT_EQ(0, pthread_key_create(&key, NULL)) << i << " of " << nkeys;
72 keys.push_back(key);
73 ASSERT_EQ(0, pthread_setspecific(key, reinterpret_cast<void*>(i)));
74 }
75
76 for (int i = keys.size() - 1; i >= 0; --i) {
77 ASSERT_EQ(reinterpret_cast<void*>(i), pthread_getspecific(keys.back()));
78 pthread_key_t key = keys.back();
79 keys.pop_back();
80 ASSERT_EQ(0, pthread_key_delete(key));
81 }
82}
83
Yabin Cui6c238f22014-12-11 20:50:41 -080084TEST(pthread, pthread_key_not_exceed_PTHREAD_KEYS_MAX) {
Elliott Hughes44b53ad2013-02-11 20:18:47 +000085 std::vector<pthread_key_t> keys;
Dan Albertc4bcc752014-09-30 11:48:24 -070086 int rv = 0;
Yabin Cui6c238f22014-12-11 20:50:41 -080087
88 // Pthread keys are used by gtest, so PTHREAD_KEYS_MAX should
89 // be more than we are allowed to allocate now.
90 for (int i = 0; i < PTHREAD_KEYS_MAX; i++) {
Elliott Hughes44b53ad2013-02-11 20:18:47 +000091 pthread_key_t key;
Dan Albertc4bcc752014-09-30 11:48:24 -070092 rv = pthread_key_create(&key, NULL);
93 if (rv == EAGAIN) {
94 break;
95 }
96 EXPECT_EQ(0, rv);
Elliott Hughes44b53ad2013-02-11 20:18:47 +000097 keys.push_back(key);
98 }
99
Dan Albertc4bcc752014-09-30 11:48:24 -0700100 // Don't leak keys.
101 for (auto key : keys) {
102 EXPECT_EQ(0, pthread_key_delete(key));
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000103 }
Dan Albertc4bcc752014-09-30 11:48:24 -0700104 keys.clear();
105
106 // We should have eventually reached the maximum number of keys and received
107 // EAGAIN.
108 ASSERT_EQ(EAGAIN, rv);
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000109}
110
Elliott Hughesebb770f2014-06-25 13:46:46 -0700111TEST(pthread, pthread_key_delete) {
112 void* expected = reinterpret_cast<void*>(1234);
113 pthread_key_t key;
114 ASSERT_EQ(0, pthread_key_create(&key, NULL));
115 ASSERT_EQ(0, pthread_setspecific(key, expected));
116 ASSERT_EQ(expected, pthread_getspecific(key));
117 ASSERT_EQ(0, pthread_key_delete(key));
118 // After deletion, pthread_getspecific returns NULL.
119 ASSERT_EQ(NULL, pthread_getspecific(key));
120 // And you can't use pthread_setspecific with the deleted key.
121 ASSERT_EQ(EINVAL, pthread_setspecific(key, expected));
122}
123
Elliott Hughes40a52172014-07-30 14:48:10 -0700124TEST(pthread, pthread_key_fork) {
125 void* expected = reinterpret_cast<void*>(1234);
126 pthread_key_t key;
127 ASSERT_EQ(0, pthread_key_create(&key, NULL));
128 ASSERT_EQ(0, pthread_setspecific(key, expected));
129 ASSERT_EQ(expected, pthread_getspecific(key));
130
131 pid_t pid = fork();
132 ASSERT_NE(-1, pid) << strerror(errno);
133
134 if (pid == 0) {
135 // The surviving thread inherits all the forking thread's TLS values...
136 ASSERT_EQ(expected, pthread_getspecific(key));
137 _exit(99);
138 }
139
140 int status;
141 ASSERT_EQ(pid, waitpid(pid, &status, 0));
142 ASSERT_TRUE(WIFEXITED(status));
143 ASSERT_EQ(99, WEXITSTATUS(status));
144
145 ASSERT_EQ(expected, pthread_getspecific(key));
Dan Albert1d53ae22014-09-02 15:24:26 -0700146 ASSERT_EQ(0, pthread_key_delete(key));
Elliott Hughes40a52172014-07-30 14:48:10 -0700147}
148
149static void* DirtyKeyFn(void* key) {
150 return pthread_getspecific(*reinterpret_cast<pthread_key_t*>(key));
151}
152
153TEST(pthread, pthread_key_dirty) {
154 pthread_key_t key;
155 ASSERT_EQ(0, pthread_key_create(&key, NULL));
156
157 size_t stack_size = 128 * 1024;
158 void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
159 ASSERT_NE(MAP_FAILED, stack);
160 memset(stack, 0xff, stack_size);
161
162 pthread_attr_t attr;
163 ASSERT_EQ(0, pthread_attr_init(&attr));
164 ASSERT_EQ(0, pthread_attr_setstack(&attr, stack, stack_size));
165
166 pthread_t t;
167 ASSERT_EQ(0, pthread_create(&t, &attr, DirtyKeyFn, &key));
168
169 void* result;
170 ASSERT_EQ(0, pthread_join(t, &result));
171 ASSERT_EQ(nullptr, result); // Not ~0!
172
173 ASSERT_EQ(0, munmap(stack, stack_size));
Dan Albert1d53ae22014-09-02 15:24:26 -0700174 ASSERT_EQ(0, pthread_key_delete(key));
Elliott Hughes40a52172014-07-30 14:48:10 -0700175}
176
Elliott Hughes4d014e12012-09-07 16:47:54 -0700177static void* IdFn(void* arg) {
178 return arg;
179}
180
Yabin Cui63481602014-12-01 17:41:04 -0800181class SpinFunctionHelper {
182 public:
183 SpinFunctionHelper() {
184 SpinFunctionHelper::spin_flag_ = true;
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400185 }
Yabin Cui63481602014-12-01 17:41:04 -0800186 ~SpinFunctionHelper() {
187 UnSpin();
188 }
189 auto GetFunction() -> void* (*)(void*) {
190 return SpinFunctionHelper::SpinFn;
191 }
192
193 void UnSpin() {
194 SpinFunctionHelper::spin_flag_ = false;
195 }
196
197 private:
198 static void* SpinFn(void*) {
199 while (spin_flag_) {}
200 return NULL;
201 }
202 static volatile bool spin_flag_;
203};
204
205// It doesn't matter if spin_flag_ is used in several tests,
206// because it is always set to false after each test. Each thread
207// loops on spin_flag_ can find it becomes false at some time.
208volatile bool SpinFunctionHelper::spin_flag_ = false;
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400209
Elliott Hughes4d014e12012-09-07 16:47:54 -0700210static void* JoinFn(void* arg) {
211 return reinterpret_cast<void*>(pthread_join(reinterpret_cast<pthread_t>(arg), NULL));
212}
213
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400214static void AssertDetached(pthread_t t, bool is_detached) {
215 pthread_attr_t attr;
216 ASSERT_EQ(0, pthread_getattr_np(t, &attr));
217 int detach_state;
218 ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &detach_state));
219 pthread_attr_destroy(&attr);
220 ASSERT_EQ(is_detached, (detach_state == PTHREAD_CREATE_DETACHED));
221}
222
Elliott Hughes9d23e042013-02-15 19:21:51 -0800223static void MakeDeadThread(pthread_t& t) {
224 ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, NULL));
Elliott Hughes34c987a2014-09-22 16:01:26 -0700225 ASSERT_EQ(0, pthread_join(t, NULL));
Elliott Hughes9d23e042013-02-15 19:21:51 -0800226}
227
Elliott Hughes4d014e12012-09-07 16:47:54 -0700228TEST(pthread, pthread_create) {
229 void* expected_result = reinterpret_cast<void*>(123);
230 // Can we create a thread?
231 pthread_t t;
232 ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, expected_result));
233 // If we join, do we get the expected value back?
234 void* result;
235 ASSERT_EQ(0, pthread_join(t, &result));
236 ASSERT_EQ(expected_result, result);
237}
238
Elliott Hughes3e898472013-02-12 16:40:24 +0000239TEST(pthread, pthread_create_EAGAIN) {
240 pthread_attr_t attributes;
241 ASSERT_EQ(0, pthread_attr_init(&attributes));
242 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, static_cast<size_t>(-1) & ~(getpagesize() - 1)));
243
244 pthread_t t;
245 ASSERT_EQ(EAGAIN, pthread_create(&t, &attributes, IdFn, NULL));
246}
247
Elliott Hughes4d014e12012-09-07 16:47:54 -0700248TEST(pthread, pthread_no_join_after_detach) {
Yabin Cui63481602014-12-01 17:41:04 -0800249 SpinFunctionHelper spinhelper;
250
Elliott Hughes4d014e12012-09-07 16:47:54 -0700251 pthread_t t1;
Yabin Cui63481602014-12-01 17:41:04 -0800252 ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700253
254 // After a pthread_detach...
255 ASSERT_EQ(0, pthread_detach(t1));
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400256 AssertDetached(t1, true);
Elliott Hughes4d014e12012-09-07 16:47:54 -0700257
258 // ...pthread_join should fail.
Elliott Hughes34c987a2014-09-22 16:01:26 -0700259 ASSERT_EQ(EINVAL, pthread_join(t1, NULL));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700260}
261
262TEST(pthread, pthread_no_op_detach_after_join) {
Yabin Cui63481602014-12-01 17:41:04 -0800263 SpinFunctionHelper spinhelper;
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400264
Elliott Hughes4d014e12012-09-07 16:47:54 -0700265 pthread_t t1;
Yabin Cui63481602014-12-01 17:41:04 -0800266 ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700267
268 // If thread 2 is already waiting to join thread 1...
269 pthread_t t2;
270 ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
271
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400272 sleep(1); // (Give t2 a chance to call pthread_join.)
Elliott Hughes4d014e12012-09-07 16:47:54 -0700273
Yabin Cuibbb04322015-03-19 15:19:25 -0700274#if defined(__BIONIC__)
275 ASSERT_EQ(EINVAL, pthread_detach(t1));
276#else
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400277 ASSERT_EQ(0, pthread_detach(t1));
Yabin Cuibbb04322015-03-19 15:19:25 -0700278#endif
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400279 AssertDetached(t1, false);
280
Yabin Cui63481602014-12-01 17:41:04 -0800281 spinhelper.UnSpin();
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400282
283 // ...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 -0700284 void* join_result;
285 ASSERT_EQ(0, pthread_join(t2, &join_result));
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700286 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700287}
Elliott Hughes14f19592012-10-29 10:19:44 -0700288
289TEST(pthread, pthread_join_self) {
Elliott Hughes34c987a2014-09-22 16:01:26 -0700290 ASSERT_EQ(EDEADLK, pthread_join(pthread_self(), NULL));
Elliott Hughes14f19592012-10-29 10:19:44 -0700291}
Elliott Hughes4f251be2012-11-01 16:33:29 -0700292
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800293struct TestBug37410 {
294 pthread_t main_thread;
295 pthread_mutex_t mutex;
Elliott Hughes4f251be2012-11-01 16:33:29 -0700296
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800297 static void main() {
298 TestBug37410 data;
299 data.main_thread = pthread_self();
300 ASSERT_EQ(0, pthread_mutex_init(&data.mutex, NULL));
301 ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
302
303 pthread_t t;
304 ASSERT_EQ(0, pthread_create(&t, NULL, TestBug37410::thread_fn, reinterpret_cast<void*>(&data)));
305
306 // Wait for the thread to be running...
307 ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
308 ASSERT_EQ(0, pthread_mutex_unlock(&data.mutex));
309
310 // ...and exit.
311 pthread_exit(NULL);
312 }
313
314 private:
315 static void* thread_fn(void* arg) {
316 TestBug37410* data = reinterpret_cast<TestBug37410*>(arg);
317
318 // Let the main thread know we're running.
319 pthread_mutex_unlock(&data->mutex);
320
321 // And wait for the main thread to exit.
322 pthread_join(data->main_thread, NULL);
323
324 return NULL;
325 }
326};
Elliott Hughes4f251be2012-11-01 16:33:29 -0700327
Elliott Hughes7fd803c2013-02-14 16:33:52 -0800328// Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to
329// run this test (which exits normally) in its own process.
Yabin Cui9df70402014-11-05 18:01:01 -0800330
331class pthread_DeathTest : public BionicDeathTest {};
332
333TEST_F(pthread_DeathTest, pthread_bug_37410) {
Elliott Hughes4f251be2012-11-01 16:33:29 -0700334 // http://code.google.com/p/android/issues/detail?id=37410
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800335 ASSERT_EXIT(TestBug37410::main(), ::testing::ExitedWithCode(0), "");
Elliott Hughes4f251be2012-11-01 16:33:29 -0700336}
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800337
338static void* SignalHandlerFn(void* arg) {
339 sigset_t wait_set;
340 sigfillset(&wait_set);
341 return reinterpret_cast<void*>(sigwait(&wait_set, reinterpret_cast<int*>(arg)));
342}
343
344TEST(pthread, pthread_sigmask) {
Elliott Hughes19e62322013-10-15 11:23:57 -0700345 // Check that SIGUSR1 isn't blocked.
346 sigset_t original_set;
347 sigemptyset(&original_set);
348 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &original_set));
349 ASSERT_FALSE(sigismember(&original_set, SIGUSR1));
350
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800351 // Block SIGUSR1.
352 sigset_t set;
353 sigemptyset(&set);
354 sigaddset(&set, SIGUSR1);
355 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &set, NULL));
356
Elliott Hughes19e62322013-10-15 11:23:57 -0700357 // Check that SIGUSR1 is blocked.
358 sigset_t final_set;
359 sigemptyset(&final_set);
360 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &final_set));
361 ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
362 // ...and that sigprocmask agrees with pthread_sigmask.
363 sigemptyset(&final_set);
364 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, NULL, &final_set));
365 ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
366
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800367 // Spawn a thread that calls sigwait and tells us what it received.
368 pthread_t signal_thread;
369 int received_signal = -1;
370 ASSERT_EQ(0, pthread_create(&signal_thread, NULL, SignalHandlerFn, &received_signal));
371
372 // Send that thread SIGUSR1.
373 pthread_kill(signal_thread, SIGUSR1);
374
375 // See what it got.
376 void* join_result;
377 ASSERT_EQ(0, pthread_join(signal_thread, &join_result));
378 ASSERT_EQ(SIGUSR1, received_signal);
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700379 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
Elliott Hughes19e62322013-10-15 11:23:57 -0700380
381 // Restore the original signal mask.
382 ASSERT_EQ(0, pthread_sigmask(SIG_SETMASK, &original_set, NULL));
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800383}
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800384
Elliott Hughes3e898472013-02-12 16:40:24 +0000385TEST(pthread, pthread_setname_np__too_long) {
386 ASSERT_EQ(ERANGE, pthread_setname_np(pthread_self(), "this name is far too long for linux"));
387}
388
389TEST(pthread, pthread_setname_np__self) {
390 ASSERT_EQ(0, pthread_setname_np(pthread_self(), "short 1"));
391}
392
393TEST(pthread, pthread_setname_np__other) {
Yabin Cui63481602014-12-01 17:41:04 -0800394 SpinFunctionHelper spinhelper;
395
Elliott Hughesed29e852014-10-27 12:01:51 -0700396 pthread_t t1;
Yabin Cui63481602014-12-01 17:41:04 -0800397 ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL));
Elliott Hughesed29e852014-10-27 12:01:51 -0700398 ASSERT_EQ(0, pthread_setname_np(t1, "short 2"));
Elliott Hughes3e898472013-02-12 16:40:24 +0000399}
400
401TEST(pthread, pthread_setname_np__no_such_thread) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800402 pthread_t dead_thread;
403 MakeDeadThread(dead_thread);
Elliott Hughes3e898472013-02-12 16:40:24 +0000404
405 // Call pthread_setname_np after thread has already exited.
Elliott Hughes68d98d82014-11-12 21:03:26 -0800406 ASSERT_EQ(ENOENT, pthread_setname_np(dead_thread, "short 3"));
Elliott Hughes3e898472013-02-12 16:40:24 +0000407}
Elliott Hughes9d23e042013-02-15 19:21:51 -0800408
409TEST(pthread, pthread_kill__0) {
410 // Signal 0 just tests that the thread exists, so it's safe to call on ourselves.
411 ASSERT_EQ(0, pthread_kill(pthread_self(), 0));
412}
413
414TEST(pthread, pthread_kill__invalid_signal) {
415 ASSERT_EQ(EINVAL, pthread_kill(pthread_self(), -1));
416}
417
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800418static void pthread_kill__in_signal_handler_helper(int signal_number) {
419 static int count = 0;
420 ASSERT_EQ(SIGALRM, signal_number);
421 if (++count == 1) {
422 // Can we call pthread_kill from a signal handler?
423 ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
424 }
425}
426
427TEST(pthread, pthread_kill__in_signal_handler) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800428 ScopedSignalHandler ssh(SIGALRM, pthread_kill__in_signal_handler_helper);
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800429 ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
430}
431
Elliott Hughes9d23e042013-02-15 19:21:51 -0800432TEST(pthread, pthread_detach__no_such_thread) {
433 pthread_t dead_thread;
434 MakeDeadThread(dead_thread);
435
436 ASSERT_EQ(ESRCH, pthread_detach(dead_thread));
437}
438
Yabin Cui19e246d2014-12-18 14:22:09 -0800439TEST(pthread, pthread_detach_no_leak) {
Christopher Ferrise3809602014-08-06 14:15:01 -0700440 size_t initial_bytes = 0;
441 // Run this loop more than once since the first loop causes some memory
442 // to be allocated permenantly. Run an extra loop to help catch any subtle
443 // memory leaks.
444 for (size_t loop = 0; loop < 3; loop++) {
445 // Set the initial bytes on the second loop since the memory in use
446 // should have stabilized.
447 if (loop == 1) {
448 initial_bytes = mallinfo().uordblks;
449 }
Elliott Hughes04620a32014-03-07 17:59:05 -0800450
Christopher Ferrise3809602014-08-06 14:15:01 -0700451 pthread_attr_t attr;
452 ASSERT_EQ(0, pthread_attr_init(&attr));
453 ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE));
Elliott Hughes04620a32014-03-07 17:59:05 -0800454
Christopher Ferrise3809602014-08-06 14:15:01 -0700455 std::vector<pthread_t> threads;
456 for (size_t i = 0; i < 32; ++i) {
457 pthread_t t;
458 ASSERT_EQ(0, pthread_create(&t, &attr, IdFn, NULL));
459 threads.push_back(t);
460 }
Elliott Hughes04620a32014-03-07 17:59:05 -0800461
Christopher Ferrise3809602014-08-06 14:15:01 -0700462 sleep(1);
Elliott Hughes04620a32014-03-07 17:59:05 -0800463
Christopher Ferrise3809602014-08-06 14:15:01 -0700464 for (size_t i = 0; i < 32; ++i) {
465 ASSERT_EQ(0, pthread_detach(threads[i])) << i;
466 }
Elliott Hughes04620a32014-03-07 17:59:05 -0800467 }
468
469 size_t final_bytes = mallinfo().uordblks;
Elliott Hughes04620a32014-03-07 17:59:05 -0800470 int leaked_bytes = (final_bytes - initial_bytes);
471
Yabin Cui19e246d2014-12-18 14:22:09 -0800472 ASSERT_EQ(0, leaked_bytes);
Elliott Hughes04620a32014-03-07 17:59:05 -0800473}
474
Jeff Hao9b06cc32013-08-15 14:51:16 -0700475TEST(pthread, pthread_getcpuclockid__clock_gettime) {
Yabin Cui63481602014-12-01 17:41:04 -0800476 SpinFunctionHelper spinhelper;
477
Jeff Hao9b06cc32013-08-15 14:51:16 -0700478 pthread_t t;
Yabin Cui63481602014-12-01 17:41:04 -0800479 ASSERT_EQ(0, pthread_create(&t, NULL, spinhelper.GetFunction(), NULL));
Jeff Hao9b06cc32013-08-15 14:51:16 -0700480
481 clockid_t c;
482 ASSERT_EQ(0, pthread_getcpuclockid(t, &c));
483 timespec ts;
484 ASSERT_EQ(0, clock_gettime(c, &ts));
485}
486
Elliott Hughes9d23e042013-02-15 19:21:51 -0800487TEST(pthread, pthread_getcpuclockid__no_such_thread) {
488 pthread_t dead_thread;
489 MakeDeadThread(dead_thread);
490
491 clockid_t c;
492 ASSERT_EQ(ESRCH, pthread_getcpuclockid(dead_thread, &c));
493}
494
495TEST(pthread, pthread_getschedparam__no_such_thread) {
496 pthread_t dead_thread;
497 MakeDeadThread(dead_thread);
498
499 int policy;
500 sched_param param;
501 ASSERT_EQ(ESRCH, pthread_getschedparam(dead_thread, &policy, &param));
502}
503
504TEST(pthread, pthread_setschedparam__no_such_thread) {
505 pthread_t dead_thread;
506 MakeDeadThread(dead_thread);
507
508 int policy = 0;
509 sched_param param;
510 ASSERT_EQ(ESRCH, pthread_setschedparam(dead_thread, policy, &param));
511}
512
513TEST(pthread, pthread_join__no_such_thread) {
514 pthread_t dead_thread;
515 MakeDeadThread(dead_thread);
516
Elliott Hughes34c987a2014-09-22 16:01:26 -0700517 ASSERT_EQ(ESRCH, pthread_join(dead_thread, NULL));
Elliott Hughes9d23e042013-02-15 19:21:51 -0800518}
519
520TEST(pthread, pthread_kill__no_such_thread) {
521 pthread_t dead_thread;
522 MakeDeadThread(dead_thread);
523
524 ASSERT_EQ(ESRCH, pthread_kill(dead_thread, 0));
525}
msg5550f020d12013-06-06 14:59:28 -0400526
527TEST(pthread, pthread_join__multijoin) {
Yabin Cui63481602014-12-01 17:41:04 -0800528 SpinFunctionHelper spinhelper;
msg5550f020d12013-06-06 14:59:28 -0400529
530 pthread_t t1;
Yabin Cui63481602014-12-01 17:41:04 -0800531 ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL));
msg5550f020d12013-06-06 14:59:28 -0400532
533 pthread_t t2;
534 ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
535
536 sleep(1); // (Give t2 a chance to call pthread_join.)
537
538 // Multiple joins to the same thread should fail.
539 ASSERT_EQ(EINVAL, pthread_join(t1, NULL));
540
Yabin Cui63481602014-12-01 17:41:04 -0800541 spinhelper.UnSpin();
msg5550f020d12013-06-06 14:59:28 -0400542
543 // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes).
544 void* join_result;
545 ASSERT_EQ(0, pthread_join(t2, &join_result));
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700546 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
msg5550f020d12013-06-06 14:59:28 -0400547}
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700548
Elliott Hughes70b24b12013-11-15 11:51:07 -0800549TEST(pthread, pthread_join__race) {
550 // http://b/11693195 --- pthread_join could return before the thread had actually exited.
551 // If the joiner unmapped the thread's stack, that could lead to SIGSEGV in the thread.
552 for (size_t i = 0; i < 1024; ++i) {
553 size_t stack_size = 64*1024;
554 void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
555
556 pthread_attr_t a;
557 pthread_attr_init(&a);
558 pthread_attr_setstack(&a, stack, stack_size);
559
560 pthread_t t;
561 ASSERT_EQ(0, pthread_create(&t, &a, IdFn, NULL));
562 ASSERT_EQ(0, pthread_join(t, NULL));
563 ASSERT_EQ(0, munmap(stack, stack_size));
564 }
565}
566
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700567static void* GetActualGuardSizeFn(void* arg) {
568 pthread_attr_t attributes;
569 pthread_getattr_np(pthread_self(), &attributes);
570 pthread_attr_getguardsize(&attributes, reinterpret_cast<size_t*>(arg));
571 return NULL;
572}
573
574static size_t GetActualGuardSize(const pthread_attr_t& attributes) {
575 size_t result;
576 pthread_t t;
577 pthread_create(&t, &attributes, GetActualGuardSizeFn, &result);
Elliott Hughes34c987a2014-09-22 16:01:26 -0700578 pthread_join(t, NULL);
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700579 return result;
580}
581
582static void* GetActualStackSizeFn(void* arg) {
583 pthread_attr_t attributes;
584 pthread_getattr_np(pthread_self(), &attributes);
585 pthread_attr_getstacksize(&attributes, reinterpret_cast<size_t*>(arg));
586 return NULL;
587}
588
589static size_t GetActualStackSize(const pthread_attr_t& attributes) {
590 size_t result;
591 pthread_t t;
592 pthread_create(&t, &attributes, GetActualStackSizeFn, &result);
Elliott Hughes34c987a2014-09-22 16:01:26 -0700593 pthread_join(t, NULL);
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700594 return result;
595}
596
597TEST(pthread, pthread_attr_setguardsize) {
598 pthread_attr_t attributes;
599 ASSERT_EQ(0, pthread_attr_init(&attributes));
600
601 // Get the default guard size.
602 size_t default_guard_size;
603 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &default_guard_size));
604
605 // No such thing as too small: will be rounded up to one page by pthread_create.
606 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 128));
607 size_t guard_size;
608 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
609 ASSERT_EQ(128U, guard_size);
610 ASSERT_EQ(4096U, GetActualGuardSize(attributes));
611
612 // Large enough and a multiple of the page size.
613 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024));
614 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
615 ASSERT_EQ(32*1024U, guard_size);
616
617 // Large enough but not a multiple of the page size; will be rounded up by pthread_create.
618 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024 + 1));
619 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
620 ASSERT_EQ(32*1024U + 1, guard_size);
621}
622
623TEST(pthread, pthread_attr_setstacksize) {
624 pthread_attr_t attributes;
625 ASSERT_EQ(0, pthread_attr_init(&attributes));
626
627 // Get the default stack size.
628 size_t default_stack_size;
629 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &default_stack_size));
630
631 // Too small.
632 ASSERT_EQ(EINVAL, pthread_attr_setstacksize(&attributes, 128));
633 size_t stack_size;
634 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
635 ASSERT_EQ(default_stack_size, stack_size);
636 ASSERT_GE(GetActualStackSize(attributes), default_stack_size);
637
Yabin Cui917d3902015-01-08 12:32:42 -0800638 // Large enough and a multiple of the page size; may be rounded up by pthread_create.
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700639 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024));
640 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
641 ASSERT_EQ(32*1024U, stack_size);
Yabin Cui917d3902015-01-08 12:32:42 -0800642 ASSERT_GE(GetActualStackSize(attributes), 32*1024U);
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700643
Yabin Cui917d3902015-01-08 12:32:42 -0800644 // Large enough but not aligned; will be rounded up by pthread_create.
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700645 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024 + 1));
646 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
647 ASSERT_EQ(32*1024U + 1, stack_size);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800648#if defined(__BIONIC__)
Yabin Cui917d3902015-01-08 12:32:42 -0800649 ASSERT_GT(GetActualStackSize(attributes), 32*1024U + 1);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800650#else // __BIONIC__
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700651 // glibc rounds down, in violation of POSIX. They document this in their BUGS section.
652 ASSERT_EQ(GetActualStackSize(attributes), 32*1024U);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800653#endif // __BIONIC__
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700654}
Elliott Hughesc3f11402013-10-30 14:40:09 -0700655
656TEST(pthread, pthread_rwlock_smoke) {
657 pthread_rwlock_t l;
658 ASSERT_EQ(0, pthread_rwlock_init(&l, NULL));
659
Calin Juravle76f352e2014-05-19 13:41:10 +0100660 // Single read lock
Elliott Hughesc3f11402013-10-30 14:40:09 -0700661 ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
662 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
663
Calin Juravle76f352e2014-05-19 13:41:10 +0100664 // Multiple read lock
665 ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
666 ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
667 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
668 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
669
670 // Write lock
Calin Juravle92687e42014-05-22 19:21:22 +0100671 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
672 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
Calin Juravle76f352e2014-05-19 13:41:10 +0100673
674 // Try writer lock
675 ASSERT_EQ(0, pthread_rwlock_trywrlock(&l));
676 ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l));
677 ASSERT_EQ(EBUSY, pthread_rwlock_tryrdlock(&l));
678 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
679
680 // Try reader lock
681 ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l));
682 ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l));
683 ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l));
684 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
685 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
686
687 // Try writer lock after unlock
Elliott Hughesc3f11402013-10-30 14:40:09 -0700688 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
689 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
690
Calin Juravle76f352e2014-05-19 13:41:10 +0100691#ifdef __BIONIC__
692 // EDEADLK in "read after write"
693 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
694 ASSERT_EQ(EDEADLK, pthread_rwlock_rdlock(&l));
695 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
696
697 // EDEADLK in "write after write"
698 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
699 ASSERT_EQ(EDEADLK, pthread_rwlock_wrlock(&l));
700 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
701#endif
702
Elliott Hughesc3f11402013-10-30 14:40:09 -0700703 ASSERT_EQ(0, pthread_rwlock_destroy(&l));
704}
705
Yabin Cui08ee8d22015-02-11 17:04:36 -0800706struct RwlockWakeupHelperArg {
707 pthread_rwlock_t lock;
708 enum Progress {
709 LOCK_INITIALIZED,
710 LOCK_WAITING,
711 LOCK_RELEASED,
712 LOCK_ACCESSED
713 };
714 std::atomic<Progress> progress;
715};
716
717static void pthread_rwlock_reader_wakeup_writer_helper(RwlockWakeupHelperArg* arg) {
718 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_INITIALIZED, arg->progress);
719 arg->progress = RwlockWakeupHelperArg::LOCK_WAITING;
720
721 ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&arg->lock));
722 ASSERT_EQ(0, pthread_rwlock_wrlock(&arg->lock));
723 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_RELEASED, arg->progress);
724 ASSERT_EQ(0, pthread_rwlock_unlock(&arg->lock));
725
726 arg->progress = RwlockWakeupHelperArg::LOCK_ACCESSED;
727}
728
729TEST(pthread, pthread_rwlock_reader_wakeup_writer) {
730 RwlockWakeupHelperArg wakeup_arg;
731 ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, NULL));
732 ASSERT_EQ(0, pthread_rwlock_rdlock(&wakeup_arg.lock));
733 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED;
734
735 pthread_t thread;
736 ASSERT_EQ(0, pthread_create(&thread, NULL,
737 reinterpret_cast<void* (*)(void*)>(pthread_rwlock_reader_wakeup_writer_helper), &wakeup_arg));
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800738 while (wakeup_arg.progress != RwlockWakeupHelperArg::LOCK_WAITING) {
739 usleep(5000);
740 }
741 usleep(5000);
Yabin Cui08ee8d22015-02-11 17:04:36 -0800742 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_RELEASED;
743 ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock));
744
745 ASSERT_EQ(0, pthread_join(thread, NULL));
746 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_ACCESSED, wakeup_arg.progress);
747 ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock));
748}
749
750static void pthread_rwlock_writer_wakeup_reader_helper(RwlockWakeupHelperArg* arg) {
751 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_INITIALIZED, arg->progress);
752 arg->progress = RwlockWakeupHelperArg::LOCK_WAITING;
753
754 ASSERT_EQ(EBUSY, pthread_rwlock_tryrdlock(&arg->lock));
755 ASSERT_EQ(0, pthread_rwlock_rdlock(&arg->lock));
756 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_RELEASED, arg->progress);
757 ASSERT_EQ(0, pthread_rwlock_unlock(&arg->lock));
758
759 arg->progress = RwlockWakeupHelperArg::LOCK_ACCESSED;
760}
761
762TEST(pthread, pthread_rwlock_writer_wakeup_reader) {
763 RwlockWakeupHelperArg wakeup_arg;
764 ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, NULL));
765 ASSERT_EQ(0, pthread_rwlock_wrlock(&wakeup_arg.lock));
766 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED;
767
768 pthread_t thread;
769 ASSERT_EQ(0, pthread_create(&thread, NULL,
770 reinterpret_cast<void* (*)(void*)>(pthread_rwlock_writer_wakeup_reader_helper), &wakeup_arg));
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800771 while (wakeup_arg.progress != RwlockWakeupHelperArg::LOCK_WAITING) {
772 usleep(5000);
773 }
774 usleep(5000);
Yabin Cui08ee8d22015-02-11 17:04:36 -0800775 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_RELEASED;
776 ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock));
777
778 ASSERT_EQ(0, pthread_join(thread, NULL));
779 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_ACCESSED, wakeup_arg.progress);
780 ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock));
781}
782
Elliott Hughes1728b232014-05-14 10:02:03 -0700783static int g_once_fn_call_count = 0;
Elliott Hughesc3f11402013-10-30 14:40:09 -0700784static void OnceFn() {
Elliott Hughes1728b232014-05-14 10:02:03 -0700785 ++g_once_fn_call_count;
Elliott Hughesc3f11402013-10-30 14:40:09 -0700786}
787
788TEST(pthread, pthread_once_smoke) {
789 pthread_once_t once_control = PTHREAD_ONCE_INIT;
790 ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
791 ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
Elliott Hughes1728b232014-05-14 10:02:03 -0700792 ASSERT_EQ(1, g_once_fn_call_count);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700793}
794
Elliott Hughes3694ec62014-05-14 11:46:08 -0700795static std::string pthread_once_1934122_result = "";
796
797static void Routine2() {
798 pthread_once_1934122_result += "2";
799}
800
801static void Routine1() {
802 pthread_once_t once_control_2 = PTHREAD_ONCE_INIT;
803 pthread_once_1934122_result += "1";
804 pthread_once(&once_control_2, &Routine2);
805}
806
807TEST(pthread, pthread_once_1934122) {
808 // Very old versions of Android couldn't call pthread_once from a
809 // pthread_once init routine. http://b/1934122.
810 pthread_once_t once_control_1 = PTHREAD_ONCE_INIT;
811 ASSERT_EQ(0, pthread_once(&once_control_1, &Routine1));
812 ASSERT_EQ("12", pthread_once_1934122_result);
813}
814
Elliott Hughes1728b232014-05-14 10:02:03 -0700815static int g_atfork_prepare_calls = 0;
816static void AtForkPrepare1() { g_atfork_prepare_calls = (g_atfork_prepare_calls << 4) | 1; }
817static void AtForkPrepare2() { g_atfork_prepare_calls = (g_atfork_prepare_calls << 4) | 2; }
818static int g_atfork_parent_calls = 0;
819static void AtForkParent1() { g_atfork_parent_calls = (g_atfork_parent_calls << 4) | 1; }
820static void AtForkParent2() { g_atfork_parent_calls = (g_atfork_parent_calls << 4) | 2; }
821static int g_atfork_child_calls = 0;
822static void AtForkChild1() { g_atfork_child_calls = (g_atfork_child_calls << 4) | 1; }
823static void AtForkChild2() { g_atfork_child_calls = (g_atfork_child_calls << 4) | 2; }
Elliott Hughesc3f11402013-10-30 14:40:09 -0700824
Dmitriy Ivanov00e37812014-11-20 16:53:47 -0800825TEST(pthread, pthread_atfork_smoke) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700826 ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1));
827 ASSERT_EQ(0, pthread_atfork(AtForkPrepare2, AtForkParent2, AtForkChild2));
Elliott Hughesc3f11402013-10-30 14:40:09 -0700828
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700829 int pid = fork();
830 ASSERT_NE(-1, pid) << strerror(errno);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700831
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700832 // Child and parent calls are made in the order they were registered.
833 if (pid == 0) {
834 ASSERT_EQ(0x12, g_atfork_child_calls);
835 _exit(0);
836 }
837 ASSERT_EQ(0x12, g_atfork_parent_calls);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700838
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -0700839 // Prepare calls are made in the reverse order.
840 ASSERT_EQ(0x21, g_atfork_prepare_calls);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700841}
842
843TEST(pthread, pthread_attr_getscope) {
844 pthread_attr_t attr;
845 ASSERT_EQ(0, pthread_attr_init(&attr));
846
847 int scope;
848 ASSERT_EQ(0, pthread_attr_getscope(&attr, &scope));
849 ASSERT_EQ(PTHREAD_SCOPE_SYSTEM, scope);
850}
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000851
852TEST(pthread, pthread_condattr_init) {
853 pthread_condattr_t attr;
854 pthread_condattr_init(&attr);
855
856 clockid_t clock;
857 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
858 ASSERT_EQ(CLOCK_REALTIME, clock);
859
860 int pshared;
861 ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared));
862 ASSERT_EQ(PTHREAD_PROCESS_PRIVATE, pshared);
863}
864
865TEST(pthread, pthread_condattr_setclock) {
866 pthread_condattr_t attr;
867 pthread_condattr_init(&attr);
868
869 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_REALTIME));
870 clockid_t clock;
871 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
872 ASSERT_EQ(CLOCK_REALTIME, clock);
873
874 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC));
875 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
876 ASSERT_EQ(CLOCK_MONOTONIC, clock);
877
878 ASSERT_EQ(EINVAL, pthread_condattr_setclock(&attr, CLOCK_PROCESS_CPUTIME_ID));
879}
880
881TEST(pthread, pthread_cond_broadcast__preserves_condattr_flags) {
Yabin Cui32651b82015-03-13 20:30:00 -0700882#if defined(__BIONIC__)
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000883 pthread_condattr_t attr;
884 pthread_condattr_init(&attr);
885
886 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC));
887 ASSERT_EQ(0, pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED));
888
889 pthread_cond_t cond_var;
890 ASSERT_EQ(0, pthread_cond_init(&cond_var, &attr));
891
892 ASSERT_EQ(0, pthread_cond_signal(&cond_var));
893 ASSERT_EQ(0, pthread_cond_broadcast(&cond_var));
894
Yabin Cui32651b82015-03-13 20:30:00 -0700895 attr = static_cast<pthread_condattr_t>(*reinterpret_cast<uint32_t*>(cond_var.__private));
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000896 clockid_t clock;
897 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
898 ASSERT_EQ(CLOCK_MONOTONIC, clock);
899 int pshared;
900 ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared));
901 ASSERT_EQ(PTHREAD_PROCESS_SHARED, pshared);
Yabin Cui32651b82015-03-13 20:30:00 -0700902#else // !defined(__BIONIC__)
903 GTEST_LOG_(INFO) << "This tests a bionic implementation detail.\n";
904#endif // !defined(__BIONIC__)
905}
906
907class pthread_CondWakeupTest : public ::testing::Test {
908 protected:
909 pthread_mutex_t mutex;
910 pthread_cond_t cond;
911
912 enum Progress {
913 INITIALIZED,
914 WAITING,
915 SIGNALED,
916 FINISHED,
917 };
918 std::atomic<Progress> progress;
919 pthread_t thread;
920
921 protected:
922 virtual void SetUp() {
923 ASSERT_EQ(0, pthread_mutex_init(&mutex, NULL));
924 ASSERT_EQ(0, pthread_cond_init(&cond, NULL));
925 progress = INITIALIZED;
926 ASSERT_EQ(0,
927 pthread_create(&thread, NULL, reinterpret_cast<void* (*)(void*)>(WaitThreadFn), this));
928 }
929
930 virtual void TearDown() {
931 ASSERT_EQ(0, pthread_join(thread, NULL));
932 ASSERT_EQ(FINISHED, progress);
933 ASSERT_EQ(0, pthread_cond_destroy(&cond));
934 ASSERT_EQ(0, pthread_mutex_destroy(&mutex));
935 }
936
937 void SleepUntilProgress(Progress expected_progress) {
938 while (progress != expected_progress) {
939 usleep(5000);
940 }
941 usleep(5000);
942 }
943
944 private:
945 static void WaitThreadFn(pthread_CondWakeupTest* test) {
946 ASSERT_EQ(0, pthread_mutex_lock(&test->mutex));
947 test->progress = WAITING;
948 while (test->progress == WAITING) {
949 ASSERT_EQ(0, pthread_cond_wait(&test->cond, &test->mutex));
950 }
951 ASSERT_EQ(SIGNALED, test->progress);
952 test->progress = FINISHED;
953 ASSERT_EQ(0, pthread_mutex_unlock(&test->mutex));
954 }
955};
956
957TEST_F(pthread_CondWakeupTest, signal) {
958 SleepUntilProgress(WAITING);
959 progress = SIGNALED;
960 pthread_cond_signal(&cond);
961}
962
963TEST_F(pthread_CondWakeupTest, broadcast) {
964 SleepUntilProgress(WAITING);
965 progress = SIGNALED;
966 pthread_cond_broadcast(&cond);
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000967}
Elliott Hughes0e714a52014-03-03 16:42:47 -0800968
969TEST(pthread, pthread_mutex_timedlock) {
970 pthread_mutex_t m;
971 ASSERT_EQ(0, pthread_mutex_init(&m, NULL));
972
973 // If the mutex is already locked, pthread_mutex_timedlock should time out.
974 ASSERT_EQ(0, pthread_mutex_lock(&m));
975
976 timespec ts;
977 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
978 ts.tv_nsec += 1;
979 ASSERT_EQ(ETIMEDOUT, pthread_mutex_timedlock(&m, &ts));
980
981 // If the mutex is unlocked, pthread_mutex_timedlock should succeed.
982 ASSERT_EQ(0, pthread_mutex_unlock(&m));
983
984 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
985 ts.tv_nsec += 1;
986 ASSERT_EQ(0, pthread_mutex_timedlock(&m, &ts));
987
988 ASSERT_EQ(0, pthread_mutex_unlock(&m));
989 ASSERT_EQ(0, pthread_mutex_destroy(&m));
990}
Elliott Hughes57b7a612014-08-25 17:26:50 -0700991
992TEST(pthread, pthread_attr_getstack__main_thread) {
993 // This test is only meaningful for the main thread, so make sure we're running on it!
994 ASSERT_EQ(getpid(), syscall(__NR_gettid));
995
996 // Get the main thread's attributes.
997 pthread_attr_t attributes;
998 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
999
1000 // Check that we correctly report that the main thread has no guard page.
1001 size_t guard_size;
1002 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
1003 ASSERT_EQ(0U, guard_size); // The main thread has no guard page.
1004
1005 // Get the stack base and the stack size (both ways).
1006 void* stack_base;
1007 size_t stack_size;
1008 ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
1009 size_t stack_size2;
1010 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
1011
1012 // The two methods of asking for the stack size should agree.
1013 EXPECT_EQ(stack_size, stack_size2);
1014
1015 // What does /proc/self/maps' [stack] line say?
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001016 void* maps_stack_hi = NULL;
Elliott Hughes57b7a612014-08-25 17:26:50 -07001017 FILE* fp = fopen("/proc/self/maps", "r");
1018 ASSERT_TRUE(fp != NULL);
1019 char line[BUFSIZ];
1020 while (fgets(line, sizeof(line), fp) != NULL) {
1021 uintptr_t lo, hi;
1022 char name[10];
1023 sscanf(line, "%" PRIxPTR "-%" PRIxPTR " %*4s %*x %*x:%*x %*d %10s", &lo, &hi, name);
1024 if (strcmp(name, "[stack]") == 0) {
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001025 maps_stack_hi = reinterpret_cast<void*>(hi);
Elliott Hughes57b7a612014-08-25 17:26:50 -07001026 break;
1027 }
1028 }
1029 fclose(fp);
1030
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001031 // The stack size should correspond to RLIMIT_STACK.
Elliott Hughes57b7a612014-08-25 17:26:50 -07001032 rlimit rl;
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001033 ASSERT_EQ(0, getrlimit(RLIMIT_STACK, &rl));
Elliott Hughes27a9aed2014-09-04 16:09:25 -07001034 uint64_t original_rlim_cur = rl.rlim_cur;
1035#if defined(__BIONIC__)
1036 if (rl.rlim_cur == RLIM_INFINITY) {
1037 rl.rlim_cur = 8 * 1024 * 1024; // Bionic reports unlimited stacks as 8MiB.
1038 }
1039#endif
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001040 EXPECT_EQ(rl.rlim_cur, stack_size);
1041
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -07001042 auto guard = make_scope_guard([&rl, original_rlim_cur]() {
Elliott Hughes27a9aed2014-09-04 16:09:25 -07001043 rl.rlim_cur = original_rlim_cur;
1044 ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
1045 });
1046
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001047 // The high address of the /proc/self/maps [stack] region should equal stack_base + stack_size.
1048 // Remember that the stack grows down (and is mapped in on demand), so the low address of the
1049 // region isn't very interesting.
1050 EXPECT_EQ(maps_stack_hi, reinterpret_cast<uint8_t*>(stack_base) + stack_size);
1051
1052 //
1053 // What if RLIMIT_STACK is smaller than the stack's current extent?
1054 //
Elliott Hughes57b7a612014-08-25 17:26:50 -07001055 rl.rlim_cur = rl.rlim_max = 1024; // 1KiB. We know the stack must be at least a page already.
1056 rl.rlim_max = RLIM_INFINITY;
1057 ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
1058
1059 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
1060 ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
1061 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
1062
1063 EXPECT_EQ(stack_size, stack_size2);
1064 ASSERT_EQ(1024U, stack_size);
1065
1066 //
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001067 // What if RLIMIT_STACK isn't a whole number of pages?
Elliott Hughes57b7a612014-08-25 17:26:50 -07001068 //
1069 rl.rlim_cur = rl.rlim_max = 6666; // Not a whole number of pages.
1070 rl.rlim_max = RLIM_INFINITY;
1071 ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
1072
1073 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
1074 ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
1075 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
1076
1077 EXPECT_EQ(stack_size, stack_size2);
1078 ASSERT_EQ(6666U, stack_size);
1079}
Elliott Hughes8fb639c2014-09-12 14:43:07 -07001080
Yabin Cui917d3902015-01-08 12:32:42 -08001081static void pthread_attr_getstack_18908062_helper(void*) {
1082 char local_variable;
1083 pthread_attr_t attributes;
1084 pthread_getattr_np(pthread_self(), &attributes);
1085 void* stack_base;
1086 size_t stack_size;
1087 pthread_attr_getstack(&attributes, &stack_base, &stack_size);
1088
1089 // Test whether &local_variable is in [stack_base, stack_base + stack_size).
1090 ASSERT_LE(reinterpret_cast<char*>(stack_base), &local_variable);
1091 ASSERT_LT(&local_variable, reinterpret_cast<char*>(stack_base) + stack_size);
1092}
1093
1094// Check whether something on stack is in the range of
1095// [stack_base, stack_base + stack_size). see b/18908062.
1096TEST(pthread, pthread_attr_getstack_18908062) {
1097 pthread_t t;
1098 ASSERT_EQ(0, pthread_create(&t, NULL,
1099 reinterpret_cast<void* (*)(void*)>(pthread_attr_getstack_18908062_helper),
1100 NULL));
1101 pthread_join(t, NULL);
1102}
1103
Elliott Hughes8fb639c2014-09-12 14:43:07 -07001104#if defined(__BIONIC__)
1105static void* pthread_gettid_np_helper(void* arg) {
1106 *reinterpret_cast<pid_t*>(arg) = gettid();
1107 return NULL;
1108}
1109#endif
1110
1111TEST(pthread, pthread_gettid_np) {
1112#if defined(__BIONIC__)
1113 ASSERT_EQ(gettid(), pthread_gettid_np(pthread_self()));
1114
1115 pid_t t_gettid_result;
1116 pthread_t t;
1117 pthread_create(&t, NULL, pthread_gettid_np_helper, &t_gettid_result);
1118
1119 pid_t t_pthread_gettid_np_result = pthread_gettid_np(t);
1120
Elliott Hughes34c987a2014-09-22 16:01:26 -07001121 pthread_join(t, NULL);
Elliott Hughes8fb639c2014-09-12 14:43:07 -07001122
1123 ASSERT_EQ(t_gettid_result, t_pthread_gettid_np_result);
1124#else
1125 GTEST_LOG_(INFO) << "This test does nothing.\n";
1126#endif
1127}
Elliott Hughes34c987a2014-09-22 16:01:26 -07001128
1129static size_t cleanup_counter = 0;
1130
Derek Xue41996952014-09-25 11:05:32 +01001131static void AbortCleanupRoutine(void*) {
Elliott Hughes34c987a2014-09-22 16:01:26 -07001132 abort();
1133}
1134
Derek Xue41996952014-09-25 11:05:32 +01001135static void CountCleanupRoutine(void*) {
Elliott Hughes34c987a2014-09-22 16:01:26 -07001136 ++cleanup_counter;
1137}
1138
Derek Xue41996952014-09-25 11:05:32 +01001139static void PthreadCleanupTester() {
Elliott Hughes34c987a2014-09-22 16:01:26 -07001140 pthread_cleanup_push(CountCleanupRoutine, NULL);
1141 pthread_cleanup_push(CountCleanupRoutine, NULL);
1142 pthread_cleanup_push(AbortCleanupRoutine, NULL);
1143
1144 pthread_cleanup_pop(0); // Pop the abort without executing it.
1145 pthread_cleanup_pop(1); // Pop one count while executing it.
1146 ASSERT_EQ(1U, cleanup_counter);
1147 // Exit while the other count is still on the cleanup stack.
1148 pthread_exit(NULL);
1149
1150 // Calls to pthread_cleanup_pop/pthread_cleanup_push must always be balanced.
1151 pthread_cleanup_pop(0);
1152}
1153
Derek Xue41996952014-09-25 11:05:32 +01001154static void* PthreadCleanupStartRoutine(void*) {
Elliott Hughes34c987a2014-09-22 16:01:26 -07001155 PthreadCleanupTester();
1156 return NULL;
1157}
1158
1159TEST(pthread, pthread_cleanup_push__pthread_cleanup_pop) {
1160 pthread_t t;
1161 ASSERT_EQ(0, pthread_create(&t, NULL, PthreadCleanupStartRoutine, NULL));
1162 pthread_join(t, NULL);
1163 ASSERT_EQ(2U, cleanup_counter);
1164}
Derek Xue41996952014-09-25 11:05:32 +01001165
1166TEST(pthread, PTHREAD_MUTEX_DEFAULT_is_PTHREAD_MUTEX_NORMAL) {
1167 ASSERT_EQ(PTHREAD_MUTEX_NORMAL, PTHREAD_MUTEX_DEFAULT);
1168}
1169
1170TEST(pthread, pthread_mutexattr_gettype) {
1171 pthread_mutexattr_t attr;
1172 ASSERT_EQ(0, pthread_mutexattr_init(&attr));
1173
1174 int attr_type;
1175
1176 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL));
1177 ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type));
1178 ASSERT_EQ(PTHREAD_MUTEX_NORMAL, attr_type);
1179
1180 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK));
1181 ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type));
1182 ASSERT_EQ(PTHREAD_MUTEX_ERRORCHECK, attr_type);
1183
1184 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE));
1185 ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type));
1186 ASSERT_EQ(PTHREAD_MUTEX_RECURSIVE, attr_type);
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001187
1188 ASSERT_EQ(0, pthread_mutexattr_destroy(&attr));
1189}
1190
1191static void CreateMutex(pthread_mutex_t& mutex, int mutex_type) {
1192 pthread_mutexattr_t attr;
1193 ASSERT_EQ(0, pthread_mutexattr_init(&attr));
1194 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, mutex_type));
1195 ASSERT_EQ(0, pthread_mutex_init(&mutex, &attr));
1196 ASSERT_EQ(0, pthread_mutexattr_destroy(&attr));
Derek Xue41996952014-09-25 11:05:32 +01001197}
1198
1199TEST(pthread, pthread_mutex_lock_NORMAL) {
Derek Xue41996952014-09-25 11:05:32 +01001200 pthread_mutex_t lock;
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001201 CreateMutex(lock, PTHREAD_MUTEX_NORMAL);
Derek Xue41996952014-09-25 11:05:32 +01001202
1203 ASSERT_EQ(0, pthread_mutex_lock(&lock));
1204 ASSERT_EQ(0, pthread_mutex_unlock(&lock));
1205 ASSERT_EQ(0, pthread_mutex_destroy(&lock));
1206}
1207
1208TEST(pthread, pthread_mutex_lock_ERRORCHECK) {
Derek Xue41996952014-09-25 11:05:32 +01001209 pthread_mutex_t lock;
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001210 CreateMutex(lock, PTHREAD_MUTEX_ERRORCHECK);
Derek Xue41996952014-09-25 11:05:32 +01001211
1212 ASSERT_EQ(0, pthread_mutex_lock(&lock));
1213 ASSERT_EQ(EDEADLK, pthread_mutex_lock(&lock));
1214 ASSERT_EQ(0, pthread_mutex_unlock(&lock));
1215 ASSERT_EQ(0, pthread_mutex_trylock(&lock));
1216 ASSERT_EQ(EBUSY, pthread_mutex_trylock(&lock));
1217 ASSERT_EQ(0, pthread_mutex_unlock(&lock));
1218 ASSERT_EQ(EPERM, pthread_mutex_unlock(&lock));
1219 ASSERT_EQ(0, pthread_mutex_destroy(&lock));
1220}
1221
1222TEST(pthread, pthread_mutex_lock_RECURSIVE) {
Derek Xue41996952014-09-25 11:05:32 +01001223 pthread_mutex_t lock;
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001224 CreateMutex(lock, PTHREAD_MUTEX_RECURSIVE);
Derek Xue41996952014-09-25 11:05:32 +01001225
1226 ASSERT_EQ(0, pthread_mutex_lock(&lock));
1227 ASSERT_EQ(0, pthread_mutex_lock(&lock));
1228 ASSERT_EQ(0, pthread_mutex_unlock(&lock));
1229 ASSERT_EQ(0, pthread_mutex_unlock(&lock));
1230 ASSERT_EQ(0, pthread_mutex_trylock(&lock));
1231 ASSERT_EQ(0, pthread_mutex_unlock(&lock));
1232 ASSERT_EQ(EPERM, pthread_mutex_unlock(&lock));
1233 ASSERT_EQ(0, pthread_mutex_destroy(&lock));
1234}
Yabin Cui140f3672015-02-03 10:32:00 -08001235
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001236class MutexWakeupHelper {
1237 private:
1238 pthread_mutex_t mutex;
1239 enum Progress {
1240 LOCK_INITIALIZED,
1241 LOCK_WAITING,
1242 LOCK_RELEASED,
1243 LOCK_ACCESSED
1244 };
1245 std::atomic<Progress> progress;
1246
1247 static void thread_fn(MutexWakeupHelper* helper) {
1248 ASSERT_EQ(LOCK_INITIALIZED, helper->progress);
1249 helper->progress = LOCK_WAITING;
1250
1251 ASSERT_EQ(0, pthread_mutex_lock(&helper->mutex));
1252 ASSERT_EQ(LOCK_RELEASED, helper->progress);
1253 ASSERT_EQ(0, pthread_mutex_unlock(&helper->mutex));
1254
1255 helper->progress = LOCK_ACCESSED;
1256 }
1257
1258 public:
1259 void test(int mutex_type) {
1260 CreateMutex(mutex, mutex_type);
1261 ASSERT_EQ(0, pthread_mutex_lock(&mutex));
1262 progress = LOCK_INITIALIZED;
1263
1264 pthread_t thread;
1265 ASSERT_EQ(0, pthread_create(&thread, NULL,
1266 reinterpret_cast<void* (*)(void*)>(MutexWakeupHelper::thread_fn), this));
1267
1268 while (progress != LOCK_WAITING) {
1269 usleep(5000);
1270 }
1271 usleep(5000);
1272 progress = LOCK_RELEASED;
1273 ASSERT_EQ(0, pthread_mutex_unlock(&mutex));
1274
1275 ASSERT_EQ(0, pthread_join(thread, NULL));
1276 ASSERT_EQ(LOCK_ACCESSED, progress);
1277 ASSERT_EQ(0, pthread_mutex_destroy(&mutex));
1278 }
1279};
1280
1281TEST(pthread, pthread_mutex_NORMAL_wakeup) {
1282 MutexWakeupHelper helper;
1283 helper.test(PTHREAD_MUTEX_NORMAL);
1284}
1285
1286TEST(pthread, pthread_mutex_ERRORCHECK_wakeup) {
1287 MutexWakeupHelper helper;
1288 helper.test(PTHREAD_MUTEX_ERRORCHECK);
1289}
1290
1291TEST(pthread, pthread_mutex_RECURSIVE_wakeup) {
1292 MutexWakeupHelper helper;
1293 helper.test(PTHREAD_MUTEX_RECURSIVE);
1294}
1295
Yabin Cui140f3672015-02-03 10:32:00 -08001296TEST(pthread, pthread_mutex_owner_tid_limit) {
1297 FILE* fp = fopen("/proc/sys/kernel/pid_max", "r");
1298 ASSERT_TRUE(fp != NULL);
1299 long pid_max;
1300 ASSERT_EQ(1, fscanf(fp, "%ld", &pid_max));
1301 fclose(fp);
1302 // Current pthread_mutex uses 16 bits to represent owner tid.
1303 // Change the implementation if we need to support higher value than 65535.
1304 ASSERT_LE(pid_max, 65536);
1305}