blob: b0c95fef4e13d1253df0fd1cd88e05ed608e1644 [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 Hughes04620a32014-03-07 17:59:05 -080022#include <malloc.h>
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070023#include <pthread.h>
Christopher Ferrisf04935c2013-12-20 18:43:21 -080024#include <signal.h>
Yabin Cui140f3672015-02-03 10:32:00 -080025#include <stdio.h>
Elliott Hughes70b24b12013-11-15 11:51:07 -080026#include <sys/mman.h>
Elliott Hughes4d098ca2016-04-11 12:43:05 -070027#include <sys/prctl.h>
Elliott Hughes57b7a612014-08-25 17:26:50 -070028#include <sys/syscall.h>
Narayan Kamath51e6cb32014-03-03 15:38:51 +000029#include <time.h>
Elliott Hughes4d014e12012-09-07 16:47:54 -070030#include <unistd.h>
Yabin Cui33ac04a2015-09-22 11:16:15 -070031#include <unwind.h>
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070032
Yabin Cui08ee8d22015-02-11 17:04:36 -080033#include <atomic>
Yabin Cuib5845722015-03-16 22:46:42 -070034#include <vector>
Yabin Cui08ee8d22015-02-11 17:04:36 -080035
Yabin Cuic9a659c2015-11-05 15:36:08 -080036#include "private/bionic_constants.h"
Yabin Cui17393b02015-03-21 15:08:25 -070037#include "private/bionic_macros.h"
38#include "private/ScopeGuard.h"
39#include "BionicDeathTest.h"
40#include "ScopedSignalHandler.h"
Elliott Hughes15dfd632015-09-22 16:40:14 -070041#include "utils.h"
42
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070043TEST(pthread, pthread_key_create) {
44 pthread_key_t key;
45 ASSERT_EQ(0, pthread_key_create(&key, NULL));
46 ASSERT_EQ(0, pthread_key_delete(key));
47 // Can't delete a key that's already been deleted.
48 ASSERT_EQ(EINVAL, pthread_key_delete(key));
49}
Elliott Hughes4d014e12012-09-07 16:47:54 -070050
Dan Albertc4bcc752014-09-30 11:48:24 -070051TEST(pthread, pthread_keys_max) {
Yabin Cui6c238f22014-12-11 20:50:41 -080052 // POSIX says PTHREAD_KEYS_MAX should be at least _POSIX_THREAD_KEYS_MAX.
53 ASSERT_GE(PTHREAD_KEYS_MAX, _POSIX_THREAD_KEYS_MAX);
Dan Albertc4bcc752014-09-30 11:48:24 -070054}
Elliott Hughes718a5b52014-01-28 17:02:03 -080055
Yabin Cui6c238f22014-12-11 20:50:41 -080056TEST(pthread, sysconf_SC_THREAD_KEYS_MAX_eq_PTHREAD_KEYS_MAX) {
Dan Albertc4bcc752014-09-30 11:48:24 -070057 int sysconf_max = sysconf(_SC_THREAD_KEYS_MAX);
Yabin Cui6c238f22014-12-11 20:50:41 -080058 ASSERT_EQ(sysconf_max, PTHREAD_KEYS_MAX);
Dan Albertc4bcc752014-09-30 11:48:24 -070059}
60
61TEST(pthread, pthread_key_many_distinct) {
Yabin Cui6c238f22014-12-11 20:50:41 -080062 // As gtest uses pthread keys, we can't allocate exactly PTHREAD_KEYS_MAX
63 // pthread keys, but We should be able to allocate at least this many keys.
64 int nkeys = PTHREAD_KEYS_MAX / 2;
Dan Albertc4bcc752014-09-30 11:48:24 -070065 std::vector<pthread_key_t> keys;
66
67 auto scope_guard = make_scope_guard([&keys]{
Elliott Hughes0b2acdf2015-10-02 18:25:19 -070068 for (const auto& key : keys) {
Dan Albertc4bcc752014-09-30 11:48:24 -070069 EXPECT_EQ(0, pthread_key_delete(key));
70 }
71 });
72
73 for (int i = 0; i < nkeys; ++i) {
74 pthread_key_t key;
Elliott Hughes61706932015-03-31 10:56:58 -070075 // If this fails, it's likely that LIBC_PTHREAD_KEY_RESERVED_COUNT is wrong.
Dan Albertc4bcc752014-09-30 11:48:24 -070076 ASSERT_EQ(0, pthread_key_create(&key, NULL)) << i << " of " << nkeys;
77 keys.push_back(key);
78 ASSERT_EQ(0, pthread_setspecific(key, reinterpret_cast<void*>(i)));
79 }
80
81 for (int i = keys.size() - 1; i >= 0; --i) {
82 ASSERT_EQ(reinterpret_cast<void*>(i), pthread_getspecific(keys.back()));
83 pthread_key_t key = keys.back();
84 keys.pop_back();
85 ASSERT_EQ(0, pthread_key_delete(key));
86 }
87}
88
Yabin Cui6c238f22014-12-11 20:50:41 -080089TEST(pthread, pthread_key_not_exceed_PTHREAD_KEYS_MAX) {
Elliott Hughes44b53ad2013-02-11 20:18:47 +000090 std::vector<pthread_key_t> keys;
Dan Albertc4bcc752014-09-30 11:48:24 -070091 int rv = 0;
Yabin Cui6c238f22014-12-11 20:50:41 -080092
93 // Pthread keys are used by gtest, so PTHREAD_KEYS_MAX should
94 // be more than we are allowed to allocate now.
95 for (int i = 0; i < PTHREAD_KEYS_MAX; i++) {
Elliott Hughes44b53ad2013-02-11 20:18:47 +000096 pthread_key_t key;
Dan Albertc4bcc752014-09-30 11:48:24 -070097 rv = pthread_key_create(&key, NULL);
98 if (rv == EAGAIN) {
99 break;
100 }
101 EXPECT_EQ(0, rv);
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000102 keys.push_back(key);
103 }
104
Dan Albertc4bcc752014-09-30 11:48:24 -0700105 // Don't leak keys.
Elliott Hughes0b2acdf2015-10-02 18:25:19 -0700106 for (const auto& key : keys) {
Dan Albertc4bcc752014-09-30 11:48:24 -0700107 EXPECT_EQ(0, pthread_key_delete(key));
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000108 }
Dan Albertc4bcc752014-09-30 11:48:24 -0700109 keys.clear();
110
111 // We should have eventually reached the maximum number of keys and received
112 // EAGAIN.
113 ASSERT_EQ(EAGAIN, rv);
Elliott Hughes44b53ad2013-02-11 20:18:47 +0000114}
115
Elliott Hughesebb770f2014-06-25 13:46:46 -0700116TEST(pthread, pthread_key_delete) {
117 void* expected = reinterpret_cast<void*>(1234);
118 pthread_key_t key;
119 ASSERT_EQ(0, pthread_key_create(&key, NULL));
120 ASSERT_EQ(0, pthread_setspecific(key, expected));
121 ASSERT_EQ(expected, pthread_getspecific(key));
122 ASSERT_EQ(0, pthread_key_delete(key));
123 // After deletion, pthread_getspecific returns NULL.
124 ASSERT_EQ(NULL, pthread_getspecific(key));
125 // And you can't use pthread_setspecific with the deleted key.
126 ASSERT_EQ(EINVAL, pthread_setspecific(key, expected));
127}
128
Elliott Hughes40a52172014-07-30 14:48:10 -0700129TEST(pthread, pthread_key_fork) {
130 void* expected = reinterpret_cast<void*>(1234);
131 pthread_key_t key;
132 ASSERT_EQ(0, pthread_key_create(&key, NULL));
133 ASSERT_EQ(0, pthread_setspecific(key, expected));
134 ASSERT_EQ(expected, pthread_getspecific(key));
135
136 pid_t pid = fork();
137 ASSERT_NE(-1, pid) << strerror(errno);
138
139 if (pid == 0) {
140 // The surviving thread inherits all the forking thread's TLS values...
141 ASSERT_EQ(expected, pthread_getspecific(key));
142 _exit(99);
143 }
144
Elliott Hughes33697a02016-01-26 13:04:57 -0800145 AssertChildExited(pid, 99);
Elliott Hughes40a52172014-07-30 14:48:10 -0700146
147 ASSERT_EQ(expected, pthread_getspecific(key));
Dan Albert1d53ae22014-09-02 15:24:26 -0700148 ASSERT_EQ(0, pthread_key_delete(key));
Elliott Hughes40a52172014-07-30 14:48:10 -0700149}
150
151static void* DirtyKeyFn(void* key) {
152 return pthread_getspecific(*reinterpret_cast<pthread_key_t*>(key));
153}
154
155TEST(pthread, pthread_key_dirty) {
156 pthread_key_t key;
157 ASSERT_EQ(0, pthread_key_create(&key, NULL));
158
Yabin Cuia36158a2015-11-16 21:06:16 -0800159 size_t stack_size = 640 * 1024;
Elliott Hughes40a52172014-07-30 14:48:10 -0700160 void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
161 ASSERT_NE(MAP_FAILED, stack);
162 memset(stack, 0xff, stack_size);
163
164 pthread_attr_t attr;
165 ASSERT_EQ(0, pthread_attr_init(&attr));
166 ASSERT_EQ(0, pthread_attr_setstack(&attr, stack, stack_size));
167
168 pthread_t t;
169 ASSERT_EQ(0, pthread_create(&t, &attr, DirtyKeyFn, &key));
170
171 void* result;
172 ASSERT_EQ(0, pthread_join(t, &result));
173 ASSERT_EQ(nullptr, result); // Not ~0!
174
175 ASSERT_EQ(0, munmap(stack, stack_size));
Dan Albert1d53ae22014-09-02 15:24:26 -0700176 ASSERT_EQ(0, pthread_key_delete(key));
Elliott Hughes40a52172014-07-30 14:48:10 -0700177}
178
Yabin Cui5ddbb3f2015-03-05 20:35:32 -0800179TEST(pthread, static_pthread_key_used_before_creation) {
180#if defined(__BIONIC__)
181 // See http://b/19625804. The bug is about a static/global pthread key being used before creation.
182 // So here tests if the static/global default value 0 can be detected as invalid key.
183 static pthread_key_t key;
184 ASSERT_EQ(nullptr, pthread_getspecific(key));
185 ASSERT_EQ(EINVAL, pthread_setspecific(key, nullptr));
186 ASSERT_EQ(EINVAL, pthread_key_delete(key));
187#else
188 GTEST_LOG_(INFO) << "This test tests bionic pthread key implementation detail.\n";
189#endif
190}
191
Elliott Hughes4d014e12012-09-07 16:47:54 -0700192static void* IdFn(void* arg) {
193 return arg;
194}
195
Yabin Cui63481602014-12-01 17:41:04 -0800196class SpinFunctionHelper {
197 public:
198 SpinFunctionHelper() {
199 SpinFunctionHelper::spin_flag_ = true;
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400200 }
Yabin Cui63481602014-12-01 17:41:04 -0800201 ~SpinFunctionHelper() {
202 UnSpin();
203 }
204 auto GetFunction() -> void* (*)(void*) {
205 return SpinFunctionHelper::SpinFn;
206 }
207
208 void UnSpin() {
209 SpinFunctionHelper::spin_flag_ = false;
210 }
211
212 private:
213 static void* SpinFn(void*) {
214 while (spin_flag_) {}
215 return NULL;
216 }
Yabin Cuia36158a2015-11-16 21:06:16 -0800217 static std::atomic<bool> spin_flag_;
Yabin Cui63481602014-12-01 17:41:04 -0800218};
219
220// It doesn't matter if spin_flag_ is used in several tests,
221// because it is always set to false after each test. Each thread
222// loops on spin_flag_ can find it becomes false at some time.
Yabin Cuia36158a2015-11-16 21:06:16 -0800223std::atomic<bool> SpinFunctionHelper::spin_flag_;
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400224
Elliott Hughes4d014e12012-09-07 16:47:54 -0700225static void* JoinFn(void* arg) {
226 return reinterpret_cast<void*>(pthread_join(reinterpret_cast<pthread_t>(arg), NULL));
227}
228
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400229static void AssertDetached(pthread_t t, bool is_detached) {
230 pthread_attr_t attr;
231 ASSERT_EQ(0, pthread_getattr_np(t, &attr));
232 int detach_state;
233 ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &detach_state));
234 pthread_attr_destroy(&attr);
235 ASSERT_EQ(is_detached, (detach_state == PTHREAD_CREATE_DETACHED));
236}
237
Elliott Hughes9d23e042013-02-15 19:21:51 -0800238static void MakeDeadThread(pthread_t& t) {
239 ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, NULL));
Elliott Hughes34c987a2014-09-22 16:01:26 -0700240 ASSERT_EQ(0, pthread_join(t, NULL));
Elliott Hughes9d23e042013-02-15 19:21:51 -0800241}
242
Elliott Hughes4d014e12012-09-07 16:47:54 -0700243TEST(pthread, pthread_create) {
244 void* expected_result = reinterpret_cast<void*>(123);
245 // Can we create a thread?
246 pthread_t t;
247 ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, expected_result));
248 // If we join, do we get the expected value back?
249 void* result;
250 ASSERT_EQ(0, pthread_join(t, &result));
251 ASSERT_EQ(expected_result, result);
252}
253
Elliott Hughes3e898472013-02-12 16:40:24 +0000254TEST(pthread, pthread_create_EAGAIN) {
255 pthread_attr_t attributes;
256 ASSERT_EQ(0, pthread_attr_init(&attributes));
257 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, static_cast<size_t>(-1) & ~(getpagesize() - 1)));
258
259 pthread_t t;
260 ASSERT_EQ(EAGAIN, pthread_create(&t, &attributes, IdFn, NULL));
261}
262
Elliott Hughes4d014e12012-09-07 16:47:54 -0700263TEST(pthread, pthread_no_join_after_detach) {
Elliott Hughes725b2a92016-03-23 11:20:47 -0700264 SpinFunctionHelper spin_helper;
Yabin Cui63481602014-12-01 17:41:04 -0800265
Elliott Hughes4d014e12012-09-07 16:47:54 -0700266 pthread_t t1;
Elliott Hughes725b2a92016-03-23 11:20:47 -0700267 ASSERT_EQ(0, pthread_create(&t1, NULL, spin_helper.GetFunction(), NULL));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700268
269 // After a pthread_detach...
270 ASSERT_EQ(0, pthread_detach(t1));
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400271 AssertDetached(t1, true);
Elliott Hughes4d014e12012-09-07 16:47:54 -0700272
273 // ...pthread_join should fail.
Elliott Hughes34c987a2014-09-22 16:01:26 -0700274 ASSERT_EQ(EINVAL, pthread_join(t1, NULL));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700275}
276
277TEST(pthread, pthread_no_op_detach_after_join) {
Elliott Hughes725b2a92016-03-23 11:20:47 -0700278 SpinFunctionHelper spin_helper;
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400279
Elliott Hughes4d014e12012-09-07 16:47:54 -0700280 pthread_t t1;
Elliott Hughes725b2a92016-03-23 11:20:47 -0700281 ASSERT_EQ(0, pthread_create(&t1, NULL, spin_helper.GetFunction(), NULL));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700282
283 // If thread 2 is already waiting to join thread 1...
284 pthread_t t2;
285 ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
286
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400287 sleep(1); // (Give t2 a chance to call pthread_join.)
Elliott Hughes4d014e12012-09-07 16:47:54 -0700288
Yabin Cuibbb04322015-03-19 15:19:25 -0700289#if defined(__BIONIC__)
290 ASSERT_EQ(EINVAL, pthread_detach(t1));
291#else
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400292 ASSERT_EQ(0, pthread_detach(t1));
Yabin Cuibbb04322015-03-19 15:19:25 -0700293#endif
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400294 AssertDetached(t1, false);
295
Elliott Hughes725b2a92016-03-23 11:20:47 -0700296 spin_helper.UnSpin();
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400297
298 // ...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 -0700299 void* join_result;
300 ASSERT_EQ(0, pthread_join(t2, &join_result));
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700301 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700302}
Elliott Hughes14f19592012-10-29 10:19:44 -0700303
304TEST(pthread, pthread_join_self) {
Elliott Hughes34c987a2014-09-22 16:01:26 -0700305 ASSERT_EQ(EDEADLK, pthread_join(pthread_self(), NULL));
Elliott Hughes14f19592012-10-29 10:19:44 -0700306}
Elliott Hughes4f251be2012-11-01 16:33:29 -0700307
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800308struct TestBug37410 {
309 pthread_t main_thread;
310 pthread_mutex_t mutex;
Elliott Hughes4f251be2012-11-01 16:33:29 -0700311
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800312 static void main() {
313 TestBug37410 data;
314 data.main_thread = pthread_self();
315 ASSERT_EQ(0, pthread_mutex_init(&data.mutex, NULL));
316 ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
317
318 pthread_t t;
319 ASSERT_EQ(0, pthread_create(&t, NULL, TestBug37410::thread_fn, reinterpret_cast<void*>(&data)));
320
321 // Wait for the thread to be running...
322 ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
323 ASSERT_EQ(0, pthread_mutex_unlock(&data.mutex));
324
325 // ...and exit.
326 pthread_exit(NULL);
327 }
328
329 private:
330 static void* thread_fn(void* arg) {
331 TestBug37410* data = reinterpret_cast<TestBug37410*>(arg);
332
333 // Let the main thread know we're running.
334 pthread_mutex_unlock(&data->mutex);
335
336 // And wait for the main thread to exit.
337 pthread_join(data->main_thread, NULL);
338
339 return NULL;
340 }
341};
Elliott Hughes4f251be2012-11-01 16:33:29 -0700342
Elliott Hughes7fd803c2013-02-14 16:33:52 -0800343// Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to
344// run this test (which exits normally) in its own process.
Yabin Cui9df70402014-11-05 18:01:01 -0800345
346class pthread_DeathTest : public BionicDeathTest {};
347
348TEST_F(pthread_DeathTest, pthread_bug_37410) {
Elliott Hughes4f251be2012-11-01 16:33:29 -0700349 // http://code.google.com/p/android/issues/detail?id=37410
Elliott Hughes877ec6d2013-11-15 17:40:18 -0800350 ASSERT_EXIT(TestBug37410::main(), ::testing::ExitedWithCode(0), "");
Elliott Hughes4f251be2012-11-01 16:33:29 -0700351}
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800352
353static void* SignalHandlerFn(void* arg) {
354 sigset_t wait_set;
355 sigfillset(&wait_set);
356 return reinterpret_cast<void*>(sigwait(&wait_set, reinterpret_cast<int*>(arg)));
357}
358
359TEST(pthread, pthread_sigmask) {
Elliott Hughes19e62322013-10-15 11:23:57 -0700360 // Check that SIGUSR1 isn't blocked.
361 sigset_t original_set;
362 sigemptyset(&original_set);
363 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &original_set));
364 ASSERT_FALSE(sigismember(&original_set, SIGUSR1));
365
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800366 // Block SIGUSR1.
367 sigset_t set;
368 sigemptyset(&set);
369 sigaddset(&set, SIGUSR1);
370 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &set, NULL));
371
Elliott Hughes19e62322013-10-15 11:23:57 -0700372 // Check that SIGUSR1 is blocked.
373 sigset_t final_set;
374 sigemptyset(&final_set);
375 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &final_set));
376 ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
377 // ...and that sigprocmask agrees with pthread_sigmask.
378 sigemptyset(&final_set);
379 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, NULL, &final_set));
380 ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
381
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800382 // Spawn a thread that calls sigwait and tells us what it received.
383 pthread_t signal_thread;
384 int received_signal = -1;
385 ASSERT_EQ(0, pthread_create(&signal_thread, NULL, SignalHandlerFn, &received_signal));
386
387 // Send that thread SIGUSR1.
388 pthread_kill(signal_thread, SIGUSR1);
389
390 // See what it got.
391 void* join_result;
392 ASSERT_EQ(0, pthread_join(signal_thread, &join_result));
393 ASSERT_EQ(SIGUSR1, received_signal);
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700394 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
Elliott Hughes19e62322013-10-15 11:23:57 -0700395
396 // Restore the original signal mask.
397 ASSERT_EQ(0, pthread_sigmask(SIG_SETMASK, &original_set, NULL));
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800398}
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800399
Elliott Hughes725b2a92016-03-23 11:20:47 -0700400static void test_pthread_setname_np__pthread_getname_np(pthread_t t) {
401 ASSERT_EQ(0, pthread_setname_np(t, "short"));
402 char name[32];
403 ASSERT_EQ(0, pthread_getname_np(t, name, sizeof(name)));
404 ASSERT_STREQ("short", name);
405
Elliott Hughesd1aea302015-04-25 10:05:24 -0700406 // The limit is 15 characters --- the kernel's buffer is 16, but includes a NUL.
Elliott Hughes725b2a92016-03-23 11:20:47 -0700407 ASSERT_EQ(0, pthread_setname_np(t, "123456789012345"));
408 ASSERT_EQ(0, pthread_getname_np(t, name, sizeof(name)));
409 ASSERT_STREQ("123456789012345", name);
410
411 ASSERT_EQ(ERANGE, pthread_setname_np(t, "1234567890123456"));
412
413 // The passed-in buffer should be at least 16 bytes.
414 ASSERT_EQ(0, pthread_getname_np(t, name, 16));
415 ASSERT_EQ(ERANGE, pthread_getname_np(t, name, 15));
Elliott Hughes3e898472013-02-12 16:40:24 +0000416}
417
Elliott Hughes725b2a92016-03-23 11:20:47 -0700418TEST(pthread, pthread_setname_np__pthread_getname_np__self) {
419 test_pthread_setname_np__pthread_getname_np(pthread_self());
Elliott Hughes3e898472013-02-12 16:40:24 +0000420}
421
Elliott Hughes725b2a92016-03-23 11:20:47 -0700422TEST(pthread, pthread_setname_np__pthread_getname_np__other) {
423 SpinFunctionHelper spin_helper;
Yabin Cui63481602014-12-01 17:41:04 -0800424
Elliott Hughes725b2a92016-03-23 11:20:47 -0700425 pthread_t t;
Elliott Hughes4d098ca2016-04-11 12:43:05 -0700426 ASSERT_EQ(0, pthread_create(&t, nullptr, spin_helper.GetFunction(), nullptr));
427 test_pthread_setname_np__pthread_getname_np(t);
428 spin_helper.UnSpin();
429 ASSERT_EQ(0, pthread_join(t, nullptr));
430}
431
432// http://b/28051133: a kernel misfeature means that you can't change the
433// name of another thread if you've set PR_SET_DUMPABLE to 0.
434TEST(pthread, pthread_setname_np__pthread_getname_np__other_PR_SET_DUMPABLE) {
435 ASSERT_EQ(0, prctl(PR_SET_DUMPABLE, 0)) << strerror(errno);
436
437 SpinFunctionHelper spin_helper;
438
439 pthread_t t;
440 ASSERT_EQ(0, pthread_create(&t, nullptr, spin_helper.GetFunction(), nullptr));
Elliott Hughes725b2a92016-03-23 11:20:47 -0700441 test_pthread_setname_np__pthread_getname_np(t);
442 spin_helper.UnSpin();
443 ASSERT_EQ(0, pthread_join(t, nullptr));
Elliott Hughes3e898472013-02-12 16:40:24 +0000444}
445
Elliott Hughes725b2a92016-03-23 11:20:47 -0700446TEST(pthread, pthread_setname_np__pthread_getname_np__no_such_thread) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800447 pthread_t dead_thread;
448 MakeDeadThread(dead_thread);
Elliott Hughes3e898472013-02-12 16:40:24 +0000449
Elliott Hughes725b2a92016-03-23 11:20:47 -0700450 // Call pthread_getname_np and pthread_setname_np after the thread has already exited.
Elliott Hughes68d98d82014-11-12 21:03:26 -0800451 ASSERT_EQ(ENOENT, pthread_setname_np(dead_thread, "short 3"));
Elliott Hughes725b2a92016-03-23 11:20:47 -0700452 char name[64];
453 ASSERT_EQ(ENOENT, pthread_getname_np(dead_thread, name, sizeof(name)));
Elliott Hughes3e898472013-02-12 16:40:24 +0000454}
Elliott Hughes9d23e042013-02-15 19:21:51 -0800455
456TEST(pthread, pthread_kill__0) {
457 // Signal 0 just tests that the thread exists, so it's safe to call on ourselves.
458 ASSERT_EQ(0, pthread_kill(pthread_self(), 0));
459}
460
461TEST(pthread, pthread_kill__invalid_signal) {
462 ASSERT_EQ(EINVAL, pthread_kill(pthread_self(), -1));
463}
464
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800465static void pthread_kill__in_signal_handler_helper(int signal_number) {
466 static int count = 0;
467 ASSERT_EQ(SIGALRM, signal_number);
468 if (++count == 1) {
469 // Can we call pthread_kill from a signal handler?
470 ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
471 }
472}
473
474TEST(pthread, pthread_kill__in_signal_handler) {
Elliott Hughes4b558f52014-03-04 15:58:02 -0800475 ScopedSignalHandler ssh(SIGALRM, pthread_kill__in_signal_handler_helper);
Elliott Hughesfae89fc2013-02-21 11:22:23 -0800476 ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
477}
478
Yabin Cui220b99b2015-03-26 18:13:07 +0000479TEST(pthread, pthread_detach__no_such_thread) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800480 pthread_t dead_thread;
481 MakeDeadThread(dead_thread);
482
483 ASSERT_EQ(ESRCH, pthread_detach(dead_thread));
484}
485
Jeff Hao9b06cc32013-08-15 14:51:16 -0700486TEST(pthread, pthread_getcpuclockid__clock_gettime) {
Elliott Hughes725b2a92016-03-23 11:20:47 -0700487 SpinFunctionHelper spin_helper;
Yabin Cui63481602014-12-01 17:41:04 -0800488
Jeff Hao9b06cc32013-08-15 14:51:16 -0700489 pthread_t t;
Elliott Hughes725b2a92016-03-23 11:20:47 -0700490 ASSERT_EQ(0, pthread_create(&t, NULL, spin_helper.GetFunction(), NULL));
Jeff Hao9b06cc32013-08-15 14:51:16 -0700491
492 clockid_t c;
493 ASSERT_EQ(0, pthread_getcpuclockid(t, &c));
494 timespec ts;
495 ASSERT_EQ(0, clock_gettime(c, &ts));
Elliott Hughes725b2a92016-03-23 11:20:47 -0700496 spin_helper.UnSpin();
Yabin Cuia36158a2015-11-16 21:06:16 -0800497 ASSERT_EQ(0, pthread_join(t, nullptr));
Jeff Hao9b06cc32013-08-15 14:51:16 -0700498}
499
Yabin Cui220b99b2015-03-26 18:13:07 +0000500TEST(pthread, pthread_getcpuclockid__no_such_thread) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800501 pthread_t dead_thread;
502 MakeDeadThread(dead_thread);
503
504 clockid_t c;
505 ASSERT_EQ(ESRCH, pthread_getcpuclockid(dead_thread, &c));
506}
507
Yabin Cui220b99b2015-03-26 18:13:07 +0000508TEST(pthread, pthread_getschedparam__no_such_thread) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800509 pthread_t dead_thread;
510 MakeDeadThread(dead_thread);
511
512 int policy;
513 sched_param param;
514 ASSERT_EQ(ESRCH, pthread_getschedparam(dead_thread, &policy, &param));
515}
516
Yabin Cui220b99b2015-03-26 18:13:07 +0000517TEST(pthread, pthread_setschedparam__no_such_thread) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800518 pthread_t dead_thread;
519 MakeDeadThread(dead_thread);
520
521 int policy = 0;
522 sched_param param;
523 ASSERT_EQ(ESRCH, pthread_setschedparam(dead_thread, policy, &param));
524}
525
Yabin Cui220b99b2015-03-26 18:13:07 +0000526TEST(pthread, pthread_join__no_such_thread) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800527 pthread_t dead_thread;
528 MakeDeadThread(dead_thread);
529
Elliott Hughes34c987a2014-09-22 16:01:26 -0700530 ASSERT_EQ(ESRCH, pthread_join(dead_thread, NULL));
Elliott Hughes9d23e042013-02-15 19:21:51 -0800531}
532
Yabin Cui220b99b2015-03-26 18:13:07 +0000533TEST(pthread, pthread_kill__no_such_thread) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800534 pthread_t dead_thread;
535 MakeDeadThread(dead_thread);
536
537 ASSERT_EQ(ESRCH, pthread_kill(dead_thread, 0));
538}
msg5550f020d12013-06-06 14:59:28 -0400539
540TEST(pthread, pthread_join__multijoin) {
Elliott Hughes725b2a92016-03-23 11:20:47 -0700541 SpinFunctionHelper spin_helper;
msg5550f020d12013-06-06 14:59:28 -0400542
543 pthread_t t1;
Elliott Hughes725b2a92016-03-23 11:20:47 -0700544 ASSERT_EQ(0, pthread_create(&t1, NULL, spin_helper.GetFunction(), NULL));
msg5550f020d12013-06-06 14:59:28 -0400545
546 pthread_t t2;
547 ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
548
549 sleep(1); // (Give t2 a chance to call pthread_join.)
550
551 // Multiple joins to the same thread should fail.
552 ASSERT_EQ(EINVAL, pthread_join(t1, NULL));
553
Elliott Hughes725b2a92016-03-23 11:20:47 -0700554 spin_helper.UnSpin();
msg5550f020d12013-06-06 14:59:28 -0400555
556 // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes).
557 void* join_result;
558 ASSERT_EQ(0, pthread_join(t2, &join_result));
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700559 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
msg5550f020d12013-06-06 14:59:28 -0400560}
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700561
Elliott Hughes70b24b12013-11-15 11:51:07 -0800562TEST(pthread, pthread_join__race) {
563 // http://b/11693195 --- pthread_join could return before the thread had actually exited.
564 // If the joiner unmapped the thread's stack, that could lead to SIGSEGV in the thread.
565 for (size_t i = 0; i < 1024; ++i) {
Yabin Cuia36158a2015-11-16 21:06:16 -0800566 size_t stack_size = 640*1024;
Elliott Hughes70b24b12013-11-15 11:51:07 -0800567 void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
568
569 pthread_attr_t a;
570 pthread_attr_init(&a);
571 pthread_attr_setstack(&a, stack, stack_size);
572
573 pthread_t t;
574 ASSERT_EQ(0, pthread_create(&t, &a, IdFn, NULL));
575 ASSERT_EQ(0, pthread_join(t, NULL));
576 ASSERT_EQ(0, munmap(stack, stack_size));
577 }
578}
579
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700580static void* GetActualGuardSizeFn(void* arg) {
581 pthread_attr_t attributes;
582 pthread_getattr_np(pthread_self(), &attributes);
583 pthread_attr_getguardsize(&attributes, reinterpret_cast<size_t*>(arg));
584 return NULL;
585}
586
587static size_t GetActualGuardSize(const pthread_attr_t& attributes) {
588 size_t result;
589 pthread_t t;
590 pthread_create(&t, &attributes, GetActualGuardSizeFn, &result);
Elliott Hughes34c987a2014-09-22 16:01:26 -0700591 pthread_join(t, NULL);
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700592 return result;
593}
594
595static void* GetActualStackSizeFn(void* arg) {
596 pthread_attr_t attributes;
597 pthread_getattr_np(pthread_self(), &attributes);
598 pthread_attr_getstacksize(&attributes, reinterpret_cast<size_t*>(arg));
599 return NULL;
600}
601
602static size_t GetActualStackSize(const pthread_attr_t& attributes) {
603 size_t result;
604 pthread_t t;
605 pthread_create(&t, &attributes, GetActualStackSizeFn, &result);
Elliott Hughes34c987a2014-09-22 16:01:26 -0700606 pthread_join(t, NULL);
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700607 return result;
608}
609
610TEST(pthread, pthread_attr_setguardsize) {
611 pthread_attr_t attributes;
612 ASSERT_EQ(0, pthread_attr_init(&attributes));
613
614 // Get the default guard size.
615 size_t default_guard_size;
616 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &default_guard_size));
617
618 // No such thing as too small: will be rounded up to one page by pthread_create.
619 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 128));
620 size_t guard_size;
621 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
622 ASSERT_EQ(128U, guard_size);
623 ASSERT_EQ(4096U, GetActualGuardSize(attributes));
624
625 // Large enough and a multiple of the page size.
626 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024));
627 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
628 ASSERT_EQ(32*1024U, guard_size);
629
630 // Large enough but not a multiple of the page size; will be rounded up by pthread_create.
631 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024 + 1));
632 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
633 ASSERT_EQ(32*1024U + 1, guard_size);
634}
635
636TEST(pthread, pthread_attr_setstacksize) {
637 pthread_attr_t attributes;
638 ASSERT_EQ(0, pthread_attr_init(&attributes));
639
640 // Get the default stack size.
641 size_t default_stack_size;
642 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &default_stack_size));
643
644 // Too small.
645 ASSERT_EQ(EINVAL, pthread_attr_setstacksize(&attributes, 128));
646 size_t stack_size;
647 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
648 ASSERT_EQ(default_stack_size, stack_size);
649 ASSERT_GE(GetActualStackSize(attributes), default_stack_size);
650
Yabin Cui917d3902015-01-08 12:32:42 -0800651 // Large enough and a multiple of the page size; may be rounded up by pthread_create.
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700652 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024));
653 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
654 ASSERT_EQ(32*1024U, stack_size);
Yabin Cui917d3902015-01-08 12:32:42 -0800655 ASSERT_GE(GetActualStackSize(attributes), 32*1024U);
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700656
Yabin Cui917d3902015-01-08 12:32:42 -0800657 // Large enough but not aligned; will be rounded up by pthread_create.
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700658 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024 + 1));
659 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
660 ASSERT_EQ(32*1024U + 1, stack_size);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800661#if defined(__BIONIC__)
Yabin Cui917d3902015-01-08 12:32:42 -0800662 ASSERT_GT(GetActualStackSize(attributes), 32*1024U + 1);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800663#else // __BIONIC__
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700664 // glibc rounds down, in violation of POSIX. They document this in their BUGS section.
665 ASSERT_EQ(GetActualStackSize(attributes), 32*1024U);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800666#endif // __BIONIC__
Elliott Hughesb95cf0d2013-07-15 14:51:07 -0700667}
Elliott Hughesc3f11402013-10-30 14:40:09 -0700668
Yabin Cui76615da2015-03-17 14:22:09 -0700669TEST(pthread, pthread_rwlockattr_smoke) {
670 pthread_rwlockattr_t attr;
671 ASSERT_EQ(0, pthread_rwlockattr_init(&attr));
672
673 int pshared_value_array[] = {PTHREAD_PROCESS_PRIVATE, PTHREAD_PROCESS_SHARED};
674 for (size_t i = 0; i < sizeof(pshared_value_array) / sizeof(pshared_value_array[0]); ++i) {
675 ASSERT_EQ(0, pthread_rwlockattr_setpshared(&attr, pshared_value_array[i]));
676 int pshared;
677 ASSERT_EQ(0, pthread_rwlockattr_getpshared(&attr, &pshared));
678 ASSERT_EQ(pshared_value_array[i], pshared);
679 }
680
681 int kind_array[] = {PTHREAD_RWLOCK_PREFER_READER_NP,
682 PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP};
683 for (size_t i = 0; i < sizeof(kind_array) / sizeof(kind_array[0]); ++i) {
684 ASSERT_EQ(0, pthread_rwlockattr_setkind_np(&attr, kind_array[i]));
685 int kind;
686 ASSERT_EQ(0, pthread_rwlockattr_getkind_np(&attr, &kind));
687 ASSERT_EQ(kind_array[i], kind);
688 }
689
690 ASSERT_EQ(0, pthread_rwlockattr_destroy(&attr));
691}
692
693TEST(pthread, pthread_rwlock_init_same_as_PTHREAD_RWLOCK_INITIALIZER) {
694 pthread_rwlock_t lock1 = PTHREAD_RWLOCK_INITIALIZER;
695 pthread_rwlock_t lock2;
696 ASSERT_EQ(0, pthread_rwlock_init(&lock2, NULL));
697 ASSERT_EQ(0, memcmp(&lock1, &lock2, sizeof(lock1)));
698}
699
Elliott Hughesc3f11402013-10-30 14:40:09 -0700700TEST(pthread, pthread_rwlock_smoke) {
701 pthread_rwlock_t l;
702 ASSERT_EQ(0, pthread_rwlock_init(&l, NULL));
703
Calin Juravle76f352e2014-05-19 13:41:10 +0100704 // Single read lock
Elliott Hughesc3f11402013-10-30 14:40:09 -0700705 ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
706 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
707
Calin Juravle76f352e2014-05-19 13:41:10 +0100708 // Multiple read lock
709 ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
710 ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
711 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
712 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
713
714 // Write lock
Calin Juravle92687e42014-05-22 19:21:22 +0100715 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
716 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
Calin Juravle76f352e2014-05-19 13:41:10 +0100717
718 // Try writer lock
719 ASSERT_EQ(0, pthread_rwlock_trywrlock(&l));
720 ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l));
721 ASSERT_EQ(EBUSY, pthread_rwlock_tryrdlock(&l));
722 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
723
724 // Try reader lock
725 ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l));
726 ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l));
727 ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l));
728 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
729 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
730
731 // Try writer lock after unlock
Elliott Hughesc3f11402013-10-30 14:40:09 -0700732 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
733 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
734
Calin Juravle76f352e2014-05-19 13:41:10 +0100735 // EDEADLK in "read after write"
736 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
737 ASSERT_EQ(EDEADLK, pthread_rwlock_rdlock(&l));
738 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
739
740 // EDEADLK in "write after write"
741 ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
742 ASSERT_EQ(EDEADLK, pthread_rwlock_wrlock(&l));
743 ASSERT_EQ(0, pthread_rwlock_unlock(&l));
Calin Juravle76f352e2014-05-19 13:41:10 +0100744
Elliott Hughesc3f11402013-10-30 14:40:09 -0700745 ASSERT_EQ(0, pthread_rwlock_destroy(&l));
746}
747
Yabin Cui08ee8d22015-02-11 17:04:36 -0800748struct RwlockWakeupHelperArg {
749 pthread_rwlock_t lock;
750 enum Progress {
751 LOCK_INITIALIZED,
752 LOCK_WAITING,
753 LOCK_RELEASED,
Yabin Cuic9a659c2015-11-05 15:36:08 -0800754 LOCK_ACCESSED,
755 LOCK_TIMEDOUT,
Yabin Cui08ee8d22015-02-11 17:04:36 -0800756 };
757 std::atomic<Progress> progress;
Yabin Cuif7969852015-04-02 17:47:48 -0700758 std::atomic<pid_t> tid;
Yabin Cuic9a659c2015-11-05 15:36:08 -0800759 std::function<int (pthread_rwlock_t*)> trylock_function;
760 std::function<int (pthread_rwlock_t*)> lock_function;
761 std::function<int (pthread_rwlock_t*, const timespec*)> timed_lock_function;
Yabin Cui08ee8d22015-02-11 17:04:36 -0800762};
763
Yabin Cuic9a659c2015-11-05 15:36:08 -0800764static void pthread_rwlock_wakeup_helper(RwlockWakeupHelperArg* arg) {
Yabin Cuif7969852015-04-02 17:47:48 -0700765 arg->tid = gettid();
Yabin Cui08ee8d22015-02-11 17:04:36 -0800766 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_INITIALIZED, arg->progress);
767 arg->progress = RwlockWakeupHelperArg::LOCK_WAITING;
768
Yabin Cuic9a659c2015-11-05 15:36:08 -0800769 ASSERT_EQ(EBUSY, arg->trylock_function(&arg->lock));
770 ASSERT_EQ(0, arg->lock_function(&arg->lock));
Yabin Cui08ee8d22015-02-11 17:04:36 -0800771 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_RELEASED, arg->progress);
772 ASSERT_EQ(0, pthread_rwlock_unlock(&arg->lock));
773
774 arg->progress = RwlockWakeupHelperArg::LOCK_ACCESSED;
775}
776
Yabin Cuic9a659c2015-11-05 15:36:08 -0800777static void test_pthread_rwlock_reader_wakeup_writer(std::function<int (pthread_rwlock_t*)> lock_function) {
Yabin Cui08ee8d22015-02-11 17:04:36 -0800778 RwlockWakeupHelperArg wakeup_arg;
779 ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, NULL));
780 ASSERT_EQ(0, pthread_rwlock_rdlock(&wakeup_arg.lock));
781 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED;
Yabin Cuif7969852015-04-02 17:47:48 -0700782 wakeup_arg.tid = 0;
Yabin Cuic9a659c2015-11-05 15:36:08 -0800783 wakeup_arg.trylock_function = pthread_rwlock_trywrlock;
784 wakeup_arg.lock_function = lock_function;
Yabin Cui08ee8d22015-02-11 17:04:36 -0800785
786 pthread_t thread;
787 ASSERT_EQ(0, pthread_create(&thread, NULL,
Yabin Cuic9a659c2015-11-05 15:36:08 -0800788 reinterpret_cast<void* (*)(void*)>(pthread_rwlock_wakeup_helper), &wakeup_arg));
Yabin Cuif7969852015-04-02 17:47:48 -0700789 WaitUntilThreadSleep(wakeup_arg.tid);
790 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress);
791
Yabin Cui08ee8d22015-02-11 17:04:36 -0800792 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_RELEASED;
793 ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock));
794
795 ASSERT_EQ(0, pthread_join(thread, NULL));
796 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_ACCESSED, wakeup_arg.progress);
797 ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock));
798}
799
Yabin Cuic9a659c2015-11-05 15:36:08 -0800800TEST(pthread, pthread_rwlock_reader_wakeup_writer) {
801 test_pthread_rwlock_reader_wakeup_writer(pthread_rwlock_wrlock);
Yabin Cui08ee8d22015-02-11 17:04:36 -0800802}
803
Yabin Cuic9a659c2015-11-05 15:36:08 -0800804TEST(pthread, pthread_rwlock_reader_wakeup_writer_timedwait) {
805 timespec ts;
806 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
807 ts.tv_sec += 1;
808 test_pthread_rwlock_reader_wakeup_writer([&](pthread_rwlock_t* lock) {
809 return pthread_rwlock_timedwrlock(lock, &ts);
810 });
811}
812
813static void test_pthread_rwlock_writer_wakeup_reader(std::function<int (pthread_rwlock_t*)> lock_function) {
Yabin Cui08ee8d22015-02-11 17:04:36 -0800814 RwlockWakeupHelperArg wakeup_arg;
815 ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, NULL));
816 ASSERT_EQ(0, pthread_rwlock_wrlock(&wakeup_arg.lock));
817 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED;
Yabin Cuif7969852015-04-02 17:47:48 -0700818 wakeup_arg.tid = 0;
Yabin Cuic9a659c2015-11-05 15:36:08 -0800819 wakeup_arg.trylock_function = pthread_rwlock_tryrdlock;
820 wakeup_arg.lock_function = lock_function;
Yabin Cui08ee8d22015-02-11 17:04:36 -0800821
822 pthread_t thread;
823 ASSERT_EQ(0, pthread_create(&thread, NULL,
Yabin Cuic9a659c2015-11-05 15:36:08 -0800824 reinterpret_cast<void* (*)(void*)>(pthread_rwlock_wakeup_helper), &wakeup_arg));
Yabin Cuif7969852015-04-02 17:47:48 -0700825 WaitUntilThreadSleep(wakeup_arg.tid);
826 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress);
827
Yabin Cui08ee8d22015-02-11 17:04:36 -0800828 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_RELEASED;
829 ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock));
830
831 ASSERT_EQ(0, pthread_join(thread, NULL));
832 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_ACCESSED, wakeup_arg.progress);
833 ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock));
834}
835
Yabin Cuic9a659c2015-11-05 15:36:08 -0800836TEST(pthread, pthread_rwlock_writer_wakeup_reader) {
837 test_pthread_rwlock_writer_wakeup_reader(pthread_rwlock_rdlock);
838}
839
840TEST(pthread, pthread_rwlock_writer_wakeup_reader_timedwait) {
841 timespec ts;
842 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
843 ts.tv_sec += 1;
844 test_pthread_rwlock_writer_wakeup_reader([&](pthread_rwlock_t* lock) {
845 return pthread_rwlock_timedrdlock(lock, &ts);
846 });
847}
848
849static void pthread_rwlock_wakeup_timeout_helper(RwlockWakeupHelperArg* arg) {
850 arg->tid = gettid();
851 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_INITIALIZED, arg->progress);
852 arg->progress = RwlockWakeupHelperArg::LOCK_WAITING;
853
854 ASSERT_EQ(EBUSY, arg->trylock_function(&arg->lock));
855
856 timespec ts;
857 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
858 ASSERT_EQ(ETIMEDOUT, arg->timed_lock_function(&arg->lock, &ts));
859 ts.tv_nsec = -1;
860 ASSERT_EQ(EINVAL, arg->timed_lock_function(&arg->lock, &ts));
861 ts.tv_nsec = NS_PER_S;
862 ASSERT_EQ(EINVAL, arg->timed_lock_function(&arg->lock, &ts));
863 ts.tv_nsec = NS_PER_S - 1;
864 ts.tv_sec = -1;
865 ASSERT_EQ(ETIMEDOUT, arg->timed_lock_function(&arg->lock, &ts));
866 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
867 ts.tv_sec += 1;
868 ASSERT_EQ(ETIMEDOUT, arg->timed_lock_function(&arg->lock, &ts));
869 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, arg->progress);
870 arg->progress = RwlockWakeupHelperArg::LOCK_TIMEDOUT;
871}
872
873TEST(pthread, pthread_rwlock_timedrdlock_timeout) {
874 RwlockWakeupHelperArg wakeup_arg;
875 ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, nullptr));
876 ASSERT_EQ(0, pthread_rwlock_wrlock(&wakeup_arg.lock));
877 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED;
878 wakeup_arg.tid = 0;
879 wakeup_arg.trylock_function = pthread_rwlock_tryrdlock;
880 wakeup_arg.timed_lock_function = pthread_rwlock_timedrdlock;
881
882 pthread_t thread;
883 ASSERT_EQ(0, pthread_create(&thread, nullptr,
884 reinterpret_cast<void* (*)(void*)>(pthread_rwlock_wakeup_timeout_helper), &wakeup_arg));
885 WaitUntilThreadSleep(wakeup_arg.tid);
886 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress);
887
888 ASSERT_EQ(0, pthread_join(thread, nullptr));
889 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_TIMEDOUT, wakeup_arg.progress);
890 ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock));
891 ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock));
892}
893
894TEST(pthread, pthread_rwlock_timedwrlock_timeout) {
895 RwlockWakeupHelperArg wakeup_arg;
896 ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, nullptr));
897 ASSERT_EQ(0, pthread_rwlock_rdlock(&wakeup_arg.lock));
898 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED;
899 wakeup_arg.tid = 0;
900 wakeup_arg.trylock_function = pthread_rwlock_trywrlock;
901 wakeup_arg.timed_lock_function = pthread_rwlock_timedwrlock;
902
903 pthread_t thread;
904 ASSERT_EQ(0, pthread_create(&thread, nullptr,
905 reinterpret_cast<void* (*)(void*)>(pthread_rwlock_wakeup_timeout_helper), &wakeup_arg));
906 WaitUntilThreadSleep(wakeup_arg.tid);
907 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress);
908
909 ASSERT_EQ(0, pthread_join(thread, nullptr));
910 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_TIMEDOUT, wakeup_arg.progress);
911 ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock));
912 ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock));
913}
914
Yabin Cui76615da2015-03-17 14:22:09 -0700915class RwlockKindTestHelper {
916 private:
917 struct ThreadArg {
918 RwlockKindTestHelper* helper;
919 std::atomic<pid_t>& tid;
920
921 ThreadArg(RwlockKindTestHelper* helper, std::atomic<pid_t>& tid)
922 : helper(helper), tid(tid) { }
923 };
924
925 public:
926 pthread_rwlock_t lock;
927
928 public:
Chih-Hung Hsieh62e3a072016-05-03 12:08:05 -0700929 explicit RwlockKindTestHelper(int kind_type) {
Yabin Cui76615da2015-03-17 14:22:09 -0700930 InitRwlock(kind_type);
931 }
932
933 ~RwlockKindTestHelper() {
934 DestroyRwlock();
935 }
936
937 void CreateWriterThread(pthread_t& thread, std::atomic<pid_t>& tid) {
938 tid = 0;
939 ThreadArg* arg = new ThreadArg(this, tid);
940 ASSERT_EQ(0, pthread_create(&thread, NULL,
941 reinterpret_cast<void* (*)(void*)>(WriterThreadFn), arg));
942 }
943
944 void CreateReaderThread(pthread_t& thread, std::atomic<pid_t>& tid) {
945 tid = 0;
946 ThreadArg* arg = new ThreadArg(this, tid);
947 ASSERT_EQ(0, pthread_create(&thread, NULL,
948 reinterpret_cast<void* (*)(void*)>(ReaderThreadFn), arg));
949 }
950
951 private:
952 void InitRwlock(int kind_type) {
953 pthread_rwlockattr_t attr;
954 ASSERT_EQ(0, pthread_rwlockattr_init(&attr));
955 ASSERT_EQ(0, pthread_rwlockattr_setkind_np(&attr, kind_type));
956 ASSERT_EQ(0, pthread_rwlock_init(&lock, &attr));
957 ASSERT_EQ(0, pthread_rwlockattr_destroy(&attr));
958 }
959
960 void DestroyRwlock() {
961 ASSERT_EQ(0, pthread_rwlock_destroy(&lock));
962 }
963
964 static void WriterThreadFn(ThreadArg* arg) {
965 arg->tid = gettid();
966
967 RwlockKindTestHelper* helper = arg->helper;
968 ASSERT_EQ(0, pthread_rwlock_wrlock(&helper->lock));
969 ASSERT_EQ(0, pthread_rwlock_unlock(&helper->lock));
970 delete arg;
971 }
972
973 static void ReaderThreadFn(ThreadArg* arg) {
974 arg->tid = gettid();
975
976 RwlockKindTestHelper* helper = arg->helper;
977 ASSERT_EQ(0, pthread_rwlock_rdlock(&helper->lock));
978 ASSERT_EQ(0, pthread_rwlock_unlock(&helper->lock));
979 delete arg;
980 }
981};
982
983TEST(pthread, pthread_rwlock_kind_PTHREAD_RWLOCK_PREFER_READER_NP) {
984 RwlockKindTestHelper helper(PTHREAD_RWLOCK_PREFER_READER_NP);
985 ASSERT_EQ(0, pthread_rwlock_rdlock(&helper.lock));
986
987 pthread_t writer_thread;
988 std::atomic<pid_t> writer_tid;
989 helper.CreateWriterThread(writer_thread, writer_tid);
990 WaitUntilThreadSleep(writer_tid);
991
992 pthread_t reader_thread;
993 std::atomic<pid_t> reader_tid;
994 helper.CreateReaderThread(reader_thread, reader_tid);
995 ASSERT_EQ(0, pthread_join(reader_thread, NULL));
996
997 ASSERT_EQ(0, pthread_rwlock_unlock(&helper.lock));
998 ASSERT_EQ(0, pthread_join(writer_thread, NULL));
999}
1000
1001TEST(pthread, pthread_rwlock_kind_PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP) {
1002 RwlockKindTestHelper helper(PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
1003 ASSERT_EQ(0, pthread_rwlock_rdlock(&helper.lock));
1004
1005 pthread_t writer_thread;
1006 std::atomic<pid_t> writer_tid;
1007 helper.CreateWriterThread(writer_thread, writer_tid);
1008 WaitUntilThreadSleep(writer_tid);
1009
1010 pthread_t reader_thread;
1011 std::atomic<pid_t> reader_tid;
1012 helper.CreateReaderThread(reader_thread, reader_tid);
1013 WaitUntilThreadSleep(reader_tid);
1014
1015 ASSERT_EQ(0, pthread_rwlock_unlock(&helper.lock));
1016 ASSERT_EQ(0, pthread_join(writer_thread, NULL));
1017 ASSERT_EQ(0, pthread_join(reader_thread, NULL));
1018}
1019
Elliott Hughes1728b232014-05-14 10:02:03 -07001020static int g_once_fn_call_count = 0;
Elliott Hughesc3f11402013-10-30 14:40:09 -07001021static void OnceFn() {
Elliott Hughes1728b232014-05-14 10:02:03 -07001022 ++g_once_fn_call_count;
Elliott Hughesc3f11402013-10-30 14:40:09 -07001023}
1024
1025TEST(pthread, pthread_once_smoke) {
1026 pthread_once_t once_control = PTHREAD_ONCE_INIT;
1027 ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
1028 ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
Elliott Hughes1728b232014-05-14 10:02:03 -07001029 ASSERT_EQ(1, g_once_fn_call_count);
Elliott Hughesc3f11402013-10-30 14:40:09 -07001030}
1031
Elliott Hughes3694ec62014-05-14 11:46:08 -07001032static std::string pthread_once_1934122_result = "";
1033
1034static void Routine2() {
1035 pthread_once_1934122_result += "2";
1036}
1037
1038static void Routine1() {
1039 pthread_once_t once_control_2 = PTHREAD_ONCE_INIT;
1040 pthread_once_1934122_result += "1";
1041 pthread_once(&once_control_2, &Routine2);
1042}
1043
1044TEST(pthread, pthread_once_1934122) {
1045 // Very old versions of Android couldn't call pthread_once from a
1046 // pthread_once init routine. http://b/1934122.
1047 pthread_once_t once_control_1 = PTHREAD_ONCE_INIT;
1048 ASSERT_EQ(0, pthread_once(&once_control_1, &Routine1));
1049 ASSERT_EQ("12", pthread_once_1934122_result);
1050}
1051
Elliott Hughes1728b232014-05-14 10:02:03 -07001052static int g_atfork_prepare_calls = 0;
Dmitriy Ivanovea295f62014-11-20 20:47:02 -08001053static void AtForkPrepare1() { g_atfork_prepare_calls = (g_atfork_prepare_calls * 10) + 1; }
1054static void AtForkPrepare2() { g_atfork_prepare_calls = (g_atfork_prepare_calls * 10) + 2; }
Elliott Hughes1728b232014-05-14 10:02:03 -07001055static int g_atfork_parent_calls = 0;
Dmitriy Ivanovea295f62014-11-20 20:47:02 -08001056static void AtForkParent1() { g_atfork_parent_calls = (g_atfork_parent_calls * 10) + 1; }
1057static void AtForkParent2() { g_atfork_parent_calls = (g_atfork_parent_calls * 10) + 2; }
Elliott Hughes1728b232014-05-14 10:02:03 -07001058static int g_atfork_child_calls = 0;
Dmitriy Ivanovea295f62014-11-20 20:47:02 -08001059static void AtForkChild1() { g_atfork_child_calls = (g_atfork_child_calls * 10) + 1; }
1060static void AtForkChild2() { g_atfork_child_calls = (g_atfork_child_calls * 10) + 2; }
Elliott Hughesc3f11402013-10-30 14:40:09 -07001061
Dmitriy Ivanov00e37812014-11-20 16:53:47 -08001062TEST(pthread, pthread_atfork_smoke) {
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -07001063 ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1));
1064 ASSERT_EQ(0, pthread_atfork(AtForkPrepare2, AtForkParent2, AtForkChild2));
Elliott Hughesc3f11402013-10-30 14:40:09 -07001065
Elliott Hughes33697a02016-01-26 13:04:57 -08001066 pid_t pid = fork();
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -07001067 ASSERT_NE(-1, pid) << strerror(errno);
Elliott Hughesc3f11402013-10-30 14:40:09 -07001068
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -07001069 // Child and parent calls are made in the order they were registered.
1070 if (pid == 0) {
Dmitriy Ivanovea295f62014-11-20 20:47:02 -08001071 ASSERT_EQ(12, g_atfork_child_calls);
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -07001072 _exit(0);
1073 }
Dmitriy Ivanovea295f62014-11-20 20:47:02 -08001074 ASSERT_EQ(12, g_atfork_parent_calls);
Elliott Hughesc3f11402013-10-30 14:40:09 -07001075
Dmitriy Ivanovcb0443c2015-03-16 14:15:46 -07001076 // Prepare calls are made in the reverse order.
Dmitriy Ivanovea295f62014-11-20 20:47:02 -08001077 ASSERT_EQ(21, g_atfork_prepare_calls);
Elliott Hughes33697a02016-01-26 13:04:57 -08001078 AssertChildExited(pid, 0);
Dmitriy Ivanovea295f62014-11-20 20:47:02 -08001079}
1080
Elliott Hughesc3f11402013-10-30 14:40:09 -07001081TEST(pthread, pthread_attr_getscope) {
1082 pthread_attr_t attr;
1083 ASSERT_EQ(0, pthread_attr_init(&attr));
1084
1085 int scope;
1086 ASSERT_EQ(0, pthread_attr_getscope(&attr, &scope));
1087 ASSERT_EQ(PTHREAD_SCOPE_SYSTEM, scope);
1088}
Narayan Kamath51e6cb32014-03-03 15:38:51 +00001089
1090TEST(pthread, pthread_condattr_init) {
1091 pthread_condattr_t attr;
1092 pthread_condattr_init(&attr);
1093
1094 clockid_t clock;
1095 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
1096 ASSERT_EQ(CLOCK_REALTIME, clock);
1097
1098 int pshared;
1099 ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared));
1100 ASSERT_EQ(PTHREAD_PROCESS_PRIVATE, pshared);
1101}
1102
1103TEST(pthread, pthread_condattr_setclock) {
1104 pthread_condattr_t attr;
1105 pthread_condattr_init(&attr);
1106
1107 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_REALTIME));
1108 clockid_t clock;
1109 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
1110 ASSERT_EQ(CLOCK_REALTIME, clock);
1111
1112 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC));
1113 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
1114 ASSERT_EQ(CLOCK_MONOTONIC, clock);
1115
1116 ASSERT_EQ(EINVAL, pthread_condattr_setclock(&attr, CLOCK_PROCESS_CPUTIME_ID));
1117}
1118
1119TEST(pthread, pthread_cond_broadcast__preserves_condattr_flags) {
Yabin Cui32651b82015-03-13 20:30:00 -07001120#if defined(__BIONIC__)
Narayan Kamath51e6cb32014-03-03 15:38:51 +00001121 pthread_condattr_t attr;
1122 pthread_condattr_init(&attr);
1123
1124 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC));
1125 ASSERT_EQ(0, pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED));
1126
1127 pthread_cond_t cond_var;
1128 ASSERT_EQ(0, pthread_cond_init(&cond_var, &attr));
1129
1130 ASSERT_EQ(0, pthread_cond_signal(&cond_var));
1131 ASSERT_EQ(0, pthread_cond_broadcast(&cond_var));
1132
Yabin Cui32651b82015-03-13 20:30:00 -07001133 attr = static_cast<pthread_condattr_t>(*reinterpret_cast<uint32_t*>(cond_var.__private));
Narayan Kamath51e6cb32014-03-03 15:38:51 +00001134 clockid_t clock;
1135 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
1136 ASSERT_EQ(CLOCK_MONOTONIC, clock);
1137 int pshared;
1138 ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared));
1139 ASSERT_EQ(PTHREAD_PROCESS_SHARED, pshared);
Yabin Cui32651b82015-03-13 20:30:00 -07001140#else // !defined(__BIONIC__)
1141 GTEST_LOG_(INFO) << "This tests a bionic implementation detail.\n";
1142#endif // !defined(__BIONIC__)
1143}
1144
1145class pthread_CondWakeupTest : public ::testing::Test {
1146 protected:
1147 pthread_mutex_t mutex;
1148 pthread_cond_t cond;
1149
1150 enum Progress {
1151 INITIALIZED,
1152 WAITING,
1153 SIGNALED,
1154 FINISHED,
1155 };
1156 std::atomic<Progress> progress;
1157 pthread_t thread;
Yabin Cuic9a659c2015-11-05 15:36:08 -08001158 std::function<int (pthread_cond_t* cond, pthread_mutex_t* mutex)> wait_function;
Yabin Cui32651b82015-03-13 20:30:00 -07001159
1160 protected:
Yabin Cuic9a659c2015-11-05 15:36:08 -08001161 void SetUp() override {
1162 ASSERT_EQ(0, pthread_mutex_init(&mutex, nullptr));
1163 }
1164
1165 void InitCond(clockid_t clock=CLOCK_REALTIME) {
1166 pthread_condattr_t attr;
1167 ASSERT_EQ(0, pthread_condattr_init(&attr));
1168 ASSERT_EQ(0, pthread_condattr_setclock(&attr, clock));
1169 ASSERT_EQ(0, pthread_cond_init(&cond, &attr));
1170 ASSERT_EQ(0, pthread_condattr_destroy(&attr));
1171 }
1172
1173 void StartWaitingThread(std::function<int (pthread_cond_t* cond, pthread_mutex_t* mutex)> wait_function) {
Yabin Cui32651b82015-03-13 20:30:00 -07001174 progress = INITIALIZED;
Yabin Cuic9a659c2015-11-05 15:36:08 -08001175 this->wait_function = wait_function;
1176 ASSERT_EQ(0, pthread_create(&thread, NULL, reinterpret_cast<void* (*)(void*)>(WaitThreadFn), this));
1177 while (progress != WAITING) {
Yabin Cui32651b82015-03-13 20:30:00 -07001178 usleep(5000);
1179 }
1180 usleep(5000);
1181 }
1182
Yabin Cuic9a659c2015-11-05 15:36:08 -08001183 void TearDown() override {
1184 ASSERT_EQ(0, pthread_join(thread, nullptr));
1185 ASSERT_EQ(FINISHED, progress);
1186 ASSERT_EQ(0, pthread_cond_destroy(&cond));
1187 ASSERT_EQ(0, pthread_mutex_destroy(&mutex));
1188 }
1189
Yabin Cui32651b82015-03-13 20:30:00 -07001190 private:
1191 static void WaitThreadFn(pthread_CondWakeupTest* test) {
1192 ASSERT_EQ(0, pthread_mutex_lock(&test->mutex));
1193 test->progress = WAITING;
1194 while (test->progress == WAITING) {
Yabin Cuic9a659c2015-11-05 15:36:08 -08001195 ASSERT_EQ(0, test->wait_function(&test->cond, &test->mutex));
Yabin Cui32651b82015-03-13 20:30:00 -07001196 }
1197 ASSERT_EQ(SIGNALED, test->progress);
1198 test->progress = FINISHED;
1199 ASSERT_EQ(0, pthread_mutex_unlock(&test->mutex));
1200 }
1201};
1202
Yabin Cuic9a659c2015-11-05 15:36:08 -08001203TEST_F(pthread_CondWakeupTest, signal_wait) {
1204 InitCond();
1205 StartWaitingThread([](pthread_cond_t* cond, pthread_mutex_t* mutex) {
1206 return pthread_cond_wait(cond, mutex);
1207 });
Yabin Cui32651b82015-03-13 20:30:00 -07001208 progress = SIGNALED;
Yabin Cuic9a659c2015-11-05 15:36:08 -08001209 ASSERT_EQ(0, pthread_cond_signal(&cond));
Yabin Cui32651b82015-03-13 20:30:00 -07001210}
1211
Yabin Cuic9a659c2015-11-05 15:36:08 -08001212TEST_F(pthread_CondWakeupTest, broadcast_wait) {
1213 InitCond();
1214 StartWaitingThread([](pthread_cond_t* cond, pthread_mutex_t* mutex) {
1215 return pthread_cond_wait(cond, mutex);
1216 });
Yabin Cui32651b82015-03-13 20:30:00 -07001217 progress = SIGNALED;
Yabin Cuic9a659c2015-11-05 15:36:08 -08001218 ASSERT_EQ(0, pthread_cond_broadcast(&cond));
Narayan Kamath51e6cb32014-03-03 15:38:51 +00001219}
Elliott Hughes0e714a52014-03-03 16:42:47 -08001220
Yabin Cuic9a659c2015-11-05 15:36:08 -08001221TEST_F(pthread_CondWakeupTest, signal_timedwait_CLOCK_REALTIME) {
1222 InitCond(CLOCK_REALTIME);
Elliott Hughes0e714a52014-03-03 16:42:47 -08001223 timespec ts;
1224 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
Yabin Cuic9a659c2015-11-05 15:36:08 -08001225 ts.tv_sec += 1;
1226 StartWaitingThread([&](pthread_cond_t* cond, pthread_mutex_t* mutex) {
1227 return pthread_cond_timedwait(cond, mutex, &ts);
1228 });
1229 progress = SIGNALED;
1230 ASSERT_EQ(0, pthread_cond_signal(&cond));
1231}
Elliott Hughes0e714a52014-03-03 16:42:47 -08001232
Yabin Cuic9a659c2015-11-05 15:36:08 -08001233TEST_F(pthread_CondWakeupTest, signal_timedwait_CLOCK_MONOTONIC) {
1234 InitCond(CLOCK_MONOTONIC);
1235 timespec ts;
1236 ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts));
1237 ts.tv_sec += 1;
1238 StartWaitingThread([&](pthread_cond_t* cond, pthread_mutex_t* mutex) {
1239 return pthread_cond_timedwait(cond, mutex, &ts);
1240 });
1241 progress = SIGNALED;
1242 ASSERT_EQ(0, pthread_cond_signal(&cond));
1243}
Elliott Hughes0e714a52014-03-03 16:42:47 -08001244
Yabin Cuic9a659c2015-11-05 15:36:08 -08001245TEST(pthread, pthread_cond_timedwait_timeout) {
1246 pthread_mutex_t mutex;
1247 ASSERT_EQ(0, pthread_mutex_init(&mutex, nullptr));
1248 pthread_cond_t cond;
1249 ASSERT_EQ(0, pthread_cond_init(&cond, nullptr));
1250 ASSERT_EQ(0, pthread_mutex_lock(&mutex));
1251 timespec ts;
Elliott Hughes0e714a52014-03-03 16:42:47 -08001252 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
Yabin Cuic9a659c2015-11-05 15:36:08 -08001253 ASSERT_EQ(ETIMEDOUT, pthread_cond_timedwait(&cond, &mutex, &ts));
1254 ts.tv_nsec = -1;
1255 ASSERT_EQ(EINVAL, pthread_cond_timedwait(&cond, &mutex, &ts));
1256 ts.tv_nsec = NS_PER_S;
1257 ASSERT_EQ(EINVAL, pthread_cond_timedwait(&cond, &mutex, &ts));
1258 ts.tv_nsec = NS_PER_S - 1;
1259 ts.tv_sec = -1;
1260 ASSERT_EQ(ETIMEDOUT, pthread_cond_timedwait(&cond, &mutex, &ts));
1261 ASSERT_EQ(0, pthread_mutex_unlock(&mutex));
Elliott Hughes0e714a52014-03-03 16:42:47 -08001262}
Elliott Hughes57b7a612014-08-25 17:26:50 -07001263
1264TEST(pthread, pthread_attr_getstack__main_thread) {
1265 // This test is only meaningful for the main thread, so make sure we're running on it!
1266 ASSERT_EQ(getpid(), syscall(__NR_gettid));
1267
1268 // Get the main thread's attributes.
1269 pthread_attr_t attributes;
1270 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
1271
1272 // Check that we correctly report that the main thread has no guard page.
1273 size_t guard_size;
1274 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
1275 ASSERT_EQ(0U, guard_size); // The main thread has no guard page.
1276
1277 // Get the stack base and the stack size (both ways).
1278 void* stack_base;
1279 size_t stack_size;
1280 ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
1281 size_t stack_size2;
1282 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
1283
1284 // The two methods of asking for the stack size should agree.
1285 EXPECT_EQ(stack_size, stack_size2);
1286
Yabin Cuib0c6f2db2015-05-19 15:09:23 -07001287#if defined(__BIONIC__)
Elliott Hughes57b7a612014-08-25 17:26:50 -07001288 // What does /proc/self/maps' [stack] line say?
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001289 void* maps_stack_hi = NULL;
Elliott Hughes15dfd632015-09-22 16:40:14 -07001290 std::vector<map_record> maps;
1291 ASSERT_TRUE(Maps::parse_maps(&maps));
Elliott Hughes0b2acdf2015-10-02 18:25:19 -07001292 for (const auto& map : maps) {
Elliott Hughes15dfd632015-09-22 16:40:14 -07001293 if (map.pathname == "[stack]") {
1294 maps_stack_hi = reinterpret_cast<void*>(map.addr_end);
Elliott Hughes57b7a612014-08-25 17:26:50 -07001295 break;
1296 }
1297 }
Elliott Hughes57b7a612014-08-25 17:26:50 -07001298
Yabin Cuib0c6f2db2015-05-19 15:09:23 -07001299 // The high address of the /proc/self/maps [stack] region should equal stack_base + stack_size.
1300 // Remember that the stack grows down (and is mapped in on demand), so the low address of the
1301 // region isn't very interesting.
1302 EXPECT_EQ(maps_stack_hi, reinterpret_cast<uint8_t*>(stack_base) + stack_size);
1303
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001304 // The stack size should correspond to RLIMIT_STACK.
Elliott Hughes57b7a612014-08-25 17:26:50 -07001305 rlimit rl;
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001306 ASSERT_EQ(0, getrlimit(RLIMIT_STACK, &rl));
Elliott Hughes27a9aed2014-09-04 16:09:25 -07001307 uint64_t original_rlim_cur = rl.rlim_cur;
Elliott Hughes27a9aed2014-09-04 16:09:25 -07001308 if (rl.rlim_cur == RLIM_INFINITY) {
1309 rl.rlim_cur = 8 * 1024 * 1024; // Bionic reports unlimited stacks as 8MiB.
1310 }
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001311 EXPECT_EQ(rl.rlim_cur, stack_size);
1312
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -07001313 auto guard = make_scope_guard([&rl, original_rlim_cur]() {
Elliott Hughes27a9aed2014-09-04 16:09:25 -07001314 rl.rlim_cur = original_rlim_cur;
1315 ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
1316 });
1317
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001318 //
1319 // What if RLIMIT_STACK is smaller than the stack's current extent?
1320 //
Elliott Hughes57b7a612014-08-25 17:26:50 -07001321 rl.rlim_cur = rl.rlim_max = 1024; // 1KiB. We know the stack must be at least a page already.
1322 rl.rlim_max = RLIM_INFINITY;
1323 ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
1324
1325 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
1326 ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
1327 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
1328
1329 EXPECT_EQ(stack_size, stack_size2);
1330 ASSERT_EQ(1024U, stack_size);
1331
1332 //
Elliott Hughes9e4ffa72014-08-27 15:32:01 -07001333 // What if RLIMIT_STACK isn't a whole number of pages?
Elliott Hughes57b7a612014-08-25 17:26:50 -07001334 //
1335 rl.rlim_cur = rl.rlim_max = 6666; // Not a whole number of pages.
1336 rl.rlim_max = RLIM_INFINITY;
1337 ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
1338
1339 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
1340 ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
1341 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
1342
1343 EXPECT_EQ(stack_size, stack_size2);
1344 ASSERT_EQ(6666U, stack_size);
Yabin Cuib0c6f2db2015-05-19 15:09:23 -07001345#endif
Elliott Hughes57b7a612014-08-25 17:26:50 -07001346}
Elliott Hughes8fb639c2014-09-12 14:43:07 -07001347
Mor-sarid, Nitzan56933322015-09-11 05:31:36 +00001348struct GetStackSignalHandlerArg {
1349 volatile bool done;
Chih-Hung Hsieh9af13d22016-06-02 14:40:09 -07001350 void* signal_stack_base;
1351 size_t signal_stack_size;
Mor-sarid, Nitzan56933322015-09-11 05:31:36 +00001352 void* main_stack_base;
1353 size_t main_stack_size;
1354};
1355
1356static GetStackSignalHandlerArg getstack_signal_handler_arg;
1357
1358static void getstack_signal_handler(int sig) {
1359 ASSERT_EQ(SIGUSR1, sig);
1360 // Use sleep() to make current thread be switched out by the kernel to provoke the error.
1361 sleep(1);
1362 pthread_attr_t attr;
1363 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attr));
1364 void* stack_base;
1365 size_t stack_size;
1366 ASSERT_EQ(0, pthread_attr_getstack(&attr, &stack_base, &stack_size));
Chih-Hung Hsieh9af13d22016-06-02 14:40:09 -07001367
1368 // Verify if the stack used by the signal handler is the alternate stack just registered.
1369 ASSERT_LE(getstack_signal_handler_arg.signal_stack_base, &attr);
1370 ASSERT_LT(static_cast<void*>(&attr),
1371 static_cast<char*>(getstack_signal_handler_arg.signal_stack_base) +
1372 getstack_signal_handler_arg.signal_stack_size);
1373
1374 // Verify if the main thread's stack got in the signal handler is correct.
1375 ASSERT_EQ(getstack_signal_handler_arg.main_stack_base, stack_base);
1376 ASSERT_LE(getstack_signal_handler_arg.main_stack_size, stack_size);
1377
Mor-sarid, Nitzan56933322015-09-11 05:31:36 +00001378 getstack_signal_handler_arg.done = true;
1379}
1380
1381// The previous code obtained the main thread's stack by reading the entry in
1382// /proc/self/task/<pid>/maps that was labeled [stack]. Unfortunately, on x86/x86_64, the kernel
1383// relies on sp0 in task state segment(tss) to label the stack map with [stack]. If the kernel
1384// switches a process while the main thread is in an alternate stack, then the kernel will label
1385// the wrong map with [stack]. This test verifies that when the above situation happens, the main
1386// thread's stack is found correctly.
1387TEST(pthread, pthread_attr_getstack_in_signal_handler) {
Yabin Cui61e4d462016-03-07 17:44:58 -08001388 // This test is only meaningful for the main thread, so make sure we're running on it!
1389 ASSERT_EQ(getpid(), syscall(__NR_gettid));
1390
Mor-sarid, Nitzan56933322015-09-11 05:31:36 +00001391 const size_t sig_stack_size = 16 * 1024;
1392 void* sig_stack = mmap(NULL, sig_stack_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS,
1393 -1, 0);
1394 ASSERT_NE(MAP_FAILED, sig_stack);
1395 stack_t ss;
1396 ss.ss_sp = sig_stack;
1397 ss.ss_size = sig_stack_size;
1398 ss.ss_flags = 0;
1399 stack_t oss;
1400 ASSERT_EQ(0, sigaltstack(&ss, &oss));
1401
Yabin Cui61e4d462016-03-07 17:44:58 -08001402 pthread_attr_t attr;
1403 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attr));
1404 void* main_stack_base;
1405 size_t main_stack_size;
1406 ASSERT_EQ(0, pthread_attr_getstack(&attr, &main_stack_base, &main_stack_size));
1407
Mor-sarid, Nitzan56933322015-09-11 05:31:36 +00001408 ScopedSignalHandler handler(SIGUSR1, getstack_signal_handler, SA_ONSTACK);
1409 getstack_signal_handler_arg.done = false;
Chih-Hung Hsieh9af13d22016-06-02 14:40:09 -07001410 getstack_signal_handler_arg.signal_stack_base = sig_stack;
1411 getstack_signal_handler_arg.signal_stack_size = sig_stack_size;
1412 getstack_signal_handler_arg.main_stack_base = main_stack_base;
1413 getstack_signal_handler_arg.main_stack_size = main_stack_size;
Mor-sarid, Nitzan56933322015-09-11 05:31:36 +00001414 kill(getpid(), SIGUSR1);
1415 ASSERT_EQ(true, getstack_signal_handler_arg.done);
1416
Mor-sarid, Nitzan56933322015-09-11 05:31:36 +00001417 ASSERT_EQ(0, sigaltstack(&oss, nullptr));
1418 ASSERT_EQ(0, munmap(sig_stack, sig_stack_size));
1419}
1420
Yabin Cui917d3902015-01-08 12:32:42 -08001421static void pthread_attr_getstack_18908062_helper(void*) {
1422 char local_variable;
1423 pthread_attr_t attributes;
1424 pthread_getattr_np(pthread_self(), &attributes);
1425 void* stack_base;
1426 size_t stack_size;
1427 pthread_attr_getstack(&attributes, &stack_base, &stack_size);
1428
1429 // Test whether &local_variable is in [stack_base, stack_base + stack_size).
1430 ASSERT_LE(reinterpret_cast<char*>(stack_base), &local_variable);
1431 ASSERT_LT(&local_variable, reinterpret_cast<char*>(stack_base) + stack_size);
1432}
1433
1434// Check whether something on stack is in the range of
1435// [stack_base, stack_base + stack_size). see b/18908062.
1436TEST(pthread, pthread_attr_getstack_18908062) {
1437 pthread_t t;
1438 ASSERT_EQ(0, pthread_create(&t, NULL,
1439 reinterpret_cast<void* (*)(void*)>(pthread_attr_getstack_18908062_helper),
1440 NULL));
1441 pthread_join(t, NULL);
1442}
1443
Elliott Hughes8fb639c2014-09-12 14:43:07 -07001444#if defined(__BIONIC__)
Elliott Hughesf2083612015-11-11 13:32:28 -08001445static pthread_mutex_t pthread_gettid_np_mutex = PTHREAD_MUTEX_INITIALIZER;
1446
Elliott Hughes8fb639c2014-09-12 14:43:07 -07001447static void* pthread_gettid_np_helper(void* arg) {
1448 *reinterpret_cast<pid_t*>(arg) = gettid();
Elliott Hughesf2083612015-11-11 13:32:28 -08001449
1450 // Wait for our parent to call pthread_gettid_np on us before exiting.
1451 pthread_mutex_lock(&pthread_gettid_np_mutex);
1452 pthread_mutex_unlock(&pthread_gettid_np_mutex);
Elliott Hughes8fb639c2014-09-12 14:43:07 -07001453 return NULL;
1454}
1455#endif
1456
1457TEST(pthread, pthread_gettid_np) {
1458#if defined(__BIONIC__)
1459 ASSERT_EQ(gettid(), pthread_gettid_np(pthread_self()));
1460
Elliott Hughesf2083612015-11-11 13:32:28 -08001461 // Ensure the other thread doesn't exit until after we've called
1462 // pthread_gettid_np on it.
1463 pthread_mutex_lock(&pthread_gettid_np_mutex);
1464
Elliott Hughes8fb639c2014-09-12 14:43:07 -07001465 pid_t t_gettid_result;
1466 pthread_t t;
1467 pthread_create(&t, NULL, pthread_gettid_np_helper, &t_gettid_result);
1468
1469 pid_t t_pthread_gettid_np_result = pthread_gettid_np(t);
1470
Elliott Hughesf2083612015-11-11 13:32:28 -08001471 // Release the other thread and wait for it to exit.
1472 pthread_mutex_unlock(&pthread_gettid_np_mutex);
Elliott Hughes34c987a2014-09-22 16:01:26 -07001473 pthread_join(t, NULL);
Elliott Hughes8fb639c2014-09-12 14:43:07 -07001474
1475 ASSERT_EQ(t_gettid_result, t_pthread_gettid_np_result);
1476#else
1477 GTEST_LOG_(INFO) << "This test does nothing.\n";
1478#endif
1479}
Elliott Hughes34c987a2014-09-22 16:01:26 -07001480
1481static size_t cleanup_counter = 0;
1482
Derek Xue41996952014-09-25 11:05:32 +01001483static void AbortCleanupRoutine(void*) {
Elliott Hughes34c987a2014-09-22 16:01:26 -07001484 abort();
1485}
1486
Derek Xue41996952014-09-25 11:05:32 +01001487static void CountCleanupRoutine(void*) {
Elliott Hughes34c987a2014-09-22 16:01:26 -07001488 ++cleanup_counter;
1489}
1490
Derek Xue41996952014-09-25 11:05:32 +01001491static void PthreadCleanupTester() {
Elliott Hughes34c987a2014-09-22 16:01:26 -07001492 pthread_cleanup_push(CountCleanupRoutine, NULL);
1493 pthread_cleanup_push(CountCleanupRoutine, NULL);
1494 pthread_cleanup_push(AbortCleanupRoutine, NULL);
1495
1496 pthread_cleanup_pop(0); // Pop the abort without executing it.
1497 pthread_cleanup_pop(1); // Pop one count while executing it.
1498 ASSERT_EQ(1U, cleanup_counter);
1499 // Exit while the other count is still on the cleanup stack.
1500 pthread_exit(NULL);
1501
1502 // Calls to pthread_cleanup_pop/pthread_cleanup_push must always be balanced.
1503 pthread_cleanup_pop(0);
1504}
1505
Derek Xue41996952014-09-25 11:05:32 +01001506static void* PthreadCleanupStartRoutine(void*) {
Elliott Hughes34c987a2014-09-22 16:01:26 -07001507 PthreadCleanupTester();
1508 return NULL;
1509}
1510
1511TEST(pthread, pthread_cleanup_push__pthread_cleanup_pop) {
1512 pthread_t t;
1513 ASSERT_EQ(0, pthread_create(&t, NULL, PthreadCleanupStartRoutine, NULL));
1514 pthread_join(t, NULL);
1515 ASSERT_EQ(2U, cleanup_counter);
1516}
Derek Xue41996952014-09-25 11:05:32 +01001517
1518TEST(pthread, PTHREAD_MUTEX_DEFAULT_is_PTHREAD_MUTEX_NORMAL) {
1519 ASSERT_EQ(PTHREAD_MUTEX_NORMAL, PTHREAD_MUTEX_DEFAULT);
1520}
1521
1522TEST(pthread, pthread_mutexattr_gettype) {
1523 pthread_mutexattr_t attr;
1524 ASSERT_EQ(0, pthread_mutexattr_init(&attr));
1525
1526 int attr_type;
1527
1528 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL));
1529 ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type));
1530 ASSERT_EQ(PTHREAD_MUTEX_NORMAL, attr_type);
1531
1532 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK));
1533 ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type));
1534 ASSERT_EQ(PTHREAD_MUTEX_ERRORCHECK, attr_type);
1535
1536 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE));
1537 ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type));
1538 ASSERT_EQ(PTHREAD_MUTEX_RECURSIVE, attr_type);
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001539
1540 ASSERT_EQ(0, pthread_mutexattr_destroy(&attr));
1541}
1542
Yabin Cui17393b02015-03-21 15:08:25 -07001543struct PthreadMutex {
1544 pthread_mutex_t lock;
1545
Chih-Hung Hsieh62e3a072016-05-03 12:08:05 -07001546 explicit PthreadMutex(int mutex_type) {
Yabin Cui17393b02015-03-21 15:08:25 -07001547 init(mutex_type);
1548 }
1549
1550 ~PthreadMutex() {
1551 destroy();
1552 }
1553
1554 private:
1555 void init(int mutex_type) {
1556 pthread_mutexattr_t attr;
1557 ASSERT_EQ(0, pthread_mutexattr_init(&attr));
1558 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, mutex_type));
1559 ASSERT_EQ(0, pthread_mutex_init(&lock, &attr));
1560 ASSERT_EQ(0, pthread_mutexattr_destroy(&attr));
1561 }
1562
1563 void destroy() {
1564 ASSERT_EQ(0, pthread_mutex_destroy(&lock));
1565 }
1566
1567 DISALLOW_COPY_AND_ASSIGN(PthreadMutex);
1568};
Derek Xue41996952014-09-25 11:05:32 +01001569
1570TEST(pthread, pthread_mutex_lock_NORMAL) {
Yabin Cui17393b02015-03-21 15:08:25 -07001571 PthreadMutex m(PTHREAD_MUTEX_NORMAL);
Derek Xue41996952014-09-25 11:05:32 +01001572
Yabin Cui17393b02015-03-21 15:08:25 -07001573 ASSERT_EQ(0, pthread_mutex_lock(&m.lock));
1574 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
Elliott Hughesd31d4c12015-12-14 17:35:10 -08001575 ASSERT_EQ(0, pthread_mutex_trylock(&m.lock));
1576 ASSERT_EQ(EBUSY, pthread_mutex_trylock(&m.lock));
1577 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
Derek Xue41996952014-09-25 11:05:32 +01001578}
1579
1580TEST(pthread, pthread_mutex_lock_ERRORCHECK) {
Yabin Cui17393b02015-03-21 15:08:25 -07001581 PthreadMutex m(PTHREAD_MUTEX_ERRORCHECK);
Derek Xue41996952014-09-25 11:05:32 +01001582
Yabin Cui17393b02015-03-21 15:08:25 -07001583 ASSERT_EQ(0, pthread_mutex_lock(&m.lock));
1584 ASSERT_EQ(EDEADLK, pthread_mutex_lock(&m.lock));
1585 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
1586 ASSERT_EQ(0, pthread_mutex_trylock(&m.lock));
1587 ASSERT_EQ(EBUSY, pthread_mutex_trylock(&m.lock));
1588 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
1589 ASSERT_EQ(EPERM, pthread_mutex_unlock(&m.lock));
Derek Xue41996952014-09-25 11:05:32 +01001590}
1591
1592TEST(pthread, pthread_mutex_lock_RECURSIVE) {
Yabin Cui17393b02015-03-21 15:08:25 -07001593 PthreadMutex m(PTHREAD_MUTEX_RECURSIVE);
Derek Xue41996952014-09-25 11:05:32 +01001594
Yabin Cui17393b02015-03-21 15:08:25 -07001595 ASSERT_EQ(0, pthread_mutex_lock(&m.lock));
1596 ASSERT_EQ(0, pthread_mutex_lock(&m.lock));
1597 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
1598 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
1599 ASSERT_EQ(0, pthread_mutex_trylock(&m.lock));
Elliott Hughesd31d4c12015-12-14 17:35:10 -08001600 ASSERT_EQ(0, pthread_mutex_trylock(&m.lock));
1601 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
Yabin Cui17393b02015-03-21 15:08:25 -07001602 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
1603 ASSERT_EQ(EPERM, pthread_mutex_unlock(&m.lock));
1604}
1605
1606TEST(pthread, pthread_mutex_init_same_as_static_initializers) {
1607 pthread_mutex_t lock_normal = PTHREAD_MUTEX_INITIALIZER;
1608 PthreadMutex m1(PTHREAD_MUTEX_NORMAL);
1609 ASSERT_EQ(0, memcmp(&lock_normal, &m1.lock, sizeof(pthread_mutex_t)));
1610 pthread_mutex_destroy(&lock_normal);
1611
1612 pthread_mutex_t lock_errorcheck = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
1613 PthreadMutex m2(PTHREAD_MUTEX_ERRORCHECK);
1614 ASSERT_EQ(0, memcmp(&lock_errorcheck, &m2.lock, sizeof(pthread_mutex_t)));
1615 pthread_mutex_destroy(&lock_errorcheck);
1616
1617 pthread_mutex_t lock_recursive = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
1618 PthreadMutex m3(PTHREAD_MUTEX_RECURSIVE);
1619 ASSERT_EQ(0, memcmp(&lock_recursive, &m3.lock, sizeof(pthread_mutex_t)));
1620 ASSERT_EQ(0, pthread_mutex_destroy(&lock_recursive));
Derek Xue41996952014-09-25 11:05:32 +01001621}
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001622class MutexWakeupHelper {
1623 private:
Yabin Cui17393b02015-03-21 15:08:25 -07001624 PthreadMutex m;
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001625 enum Progress {
1626 LOCK_INITIALIZED,
1627 LOCK_WAITING,
1628 LOCK_RELEASED,
1629 LOCK_ACCESSED
1630 };
1631 std::atomic<Progress> progress;
Yabin Cuif7969852015-04-02 17:47:48 -07001632 std::atomic<pid_t> tid;
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001633
1634 static void thread_fn(MutexWakeupHelper* helper) {
Yabin Cuif7969852015-04-02 17:47:48 -07001635 helper->tid = gettid();
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001636 ASSERT_EQ(LOCK_INITIALIZED, helper->progress);
1637 helper->progress = LOCK_WAITING;
1638
Yabin Cui17393b02015-03-21 15:08:25 -07001639 ASSERT_EQ(0, pthread_mutex_lock(&helper->m.lock));
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001640 ASSERT_EQ(LOCK_RELEASED, helper->progress);
Yabin Cui17393b02015-03-21 15:08:25 -07001641 ASSERT_EQ(0, pthread_mutex_unlock(&helper->m.lock));
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001642
1643 helper->progress = LOCK_ACCESSED;
1644 }
1645
1646 public:
Chih-Hung Hsieh62e3a072016-05-03 12:08:05 -07001647 explicit MutexWakeupHelper(int mutex_type) : m(mutex_type) {
Yabin Cui17393b02015-03-21 15:08:25 -07001648 }
1649
1650 void test() {
1651 ASSERT_EQ(0, pthread_mutex_lock(&m.lock));
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001652 progress = LOCK_INITIALIZED;
Yabin Cuif7969852015-04-02 17:47:48 -07001653 tid = 0;
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001654
1655 pthread_t thread;
1656 ASSERT_EQ(0, pthread_create(&thread, NULL,
1657 reinterpret_cast<void* (*)(void*)>(MutexWakeupHelper::thread_fn), this));
1658
Yabin Cuif7969852015-04-02 17:47:48 -07001659 WaitUntilThreadSleep(tid);
1660 ASSERT_EQ(LOCK_WAITING, progress);
1661
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001662 progress = LOCK_RELEASED;
Yabin Cui17393b02015-03-21 15:08:25 -07001663 ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001664
1665 ASSERT_EQ(0, pthread_join(thread, NULL));
1666 ASSERT_EQ(LOCK_ACCESSED, progress);
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001667 }
1668};
1669
1670TEST(pthread, pthread_mutex_NORMAL_wakeup) {
Yabin Cui17393b02015-03-21 15:08:25 -07001671 MutexWakeupHelper helper(PTHREAD_MUTEX_NORMAL);
1672 helper.test();
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001673}
1674
1675TEST(pthread, pthread_mutex_ERRORCHECK_wakeup) {
Yabin Cui17393b02015-03-21 15:08:25 -07001676 MutexWakeupHelper helper(PTHREAD_MUTEX_ERRORCHECK);
1677 helper.test();
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001678}
1679
1680TEST(pthread, pthread_mutex_RECURSIVE_wakeup) {
Yabin Cui17393b02015-03-21 15:08:25 -07001681 MutexWakeupHelper helper(PTHREAD_MUTEX_RECURSIVE);
1682 helper.test();
Yabin Cui5b8e7cd2015-03-04 17:36:59 -08001683}
1684
Yabin Cui140f3672015-02-03 10:32:00 -08001685TEST(pthread, pthread_mutex_owner_tid_limit) {
Yabin Cuie69c2452015-02-13 16:21:25 -08001686#if defined(__BIONIC__) && !defined(__LP64__)
Yabin Cui140f3672015-02-03 10:32:00 -08001687 FILE* fp = fopen("/proc/sys/kernel/pid_max", "r");
1688 ASSERT_TRUE(fp != NULL);
1689 long pid_max;
1690 ASSERT_EQ(1, fscanf(fp, "%ld", &pid_max));
1691 fclose(fp);
Yabin Cuie69c2452015-02-13 16:21:25 -08001692 // Bionic's pthread_mutex implementation on 32-bit devices uses 16 bits to represent owner tid.
Yabin Cui140f3672015-02-03 10:32:00 -08001693 ASSERT_LE(pid_max, 65536);
Yabin Cuie69c2452015-02-13 16:21:25 -08001694#else
1695 GTEST_LOG_(INFO) << "This test does nothing as 32-bit tid is supported by pthread_mutex.\n";
1696#endif
Yabin Cui140f3672015-02-03 10:32:00 -08001697}
Yabin Cuib5845722015-03-16 22:46:42 -07001698
Yabin Cuic9a659c2015-11-05 15:36:08 -08001699TEST(pthread, pthread_mutex_timedlock) {
1700 pthread_mutex_t m;
1701 ASSERT_EQ(0, pthread_mutex_init(&m, nullptr));
1702
1703 // If the mutex is already locked, pthread_mutex_timedlock should time out.
1704 ASSERT_EQ(0, pthread_mutex_lock(&m));
1705
1706 timespec ts;
1707 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
1708 ASSERT_EQ(ETIMEDOUT, pthread_mutex_timedlock(&m, &ts));
1709 ts.tv_nsec = -1;
1710 ASSERT_EQ(EINVAL, pthread_mutex_timedlock(&m, &ts));
1711 ts.tv_nsec = NS_PER_S;
1712 ASSERT_EQ(EINVAL, pthread_mutex_timedlock(&m, &ts));
1713 ts.tv_nsec = NS_PER_S - 1;
1714 ts.tv_sec = -1;
1715 ASSERT_EQ(ETIMEDOUT, pthread_mutex_timedlock(&m, &ts));
1716
1717 // If the mutex is unlocked, pthread_mutex_timedlock should succeed.
1718 ASSERT_EQ(0, pthread_mutex_unlock(&m));
1719
1720 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
1721 ts.tv_sec += 1;
1722 ASSERT_EQ(0, pthread_mutex_timedlock(&m, &ts));
1723
1724 ASSERT_EQ(0, pthread_mutex_unlock(&m));
1725 ASSERT_EQ(0, pthread_mutex_destroy(&m));
1726}
1727
Yabin Cuib5845722015-03-16 22:46:42 -07001728class StrictAlignmentAllocator {
1729 public:
1730 void* allocate(size_t size, size_t alignment) {
1731 char* p = new char[size + alignment * 2];
1732 allocated_array.push_back(p);
1733 while (!is_strict_aligned(p, alignment)) {
1734 ++p;
1735 }
1736 return p;
1737 }
1738
1739 ~StrictAlignmentAllocator() {
Elliott Hughes0b2acdf2015-10-02 18:25:19 -07001740 for (const auto& p : allocated_array) {
1741 delete[] p;
Yabin Cuib5845722015-03-16 22:46:42 -07001742 }
1743 }
1744
1745 private:
1746 bool is_strict_aligned(char* p, size_t alignment) {
1747 return (reinterpret_cast<uintptr_t>(p) % (alignment * 2)) == alignment;
1748 }
1749
1750 std::vector<char*> allocated_array;
1751};
1752
1753TEST(pthread, pthread_types_allow_four_bytes_alignment) {
1754#if defined(__BIONIC__)
1755 // For binary compatibility with old version, we need to allow 4-byte aligned data for pthread types.
1756 StrictAlignmentAllocator allocator;
1757 pthread_mutex_t* mutex = reinterpret_cast<pthread_mutex_t*>(
1758 allocator.allocate(sizeof(pthread_mutex_t), 4));
1759 ASSERT_EQ(0, pthread_mutex_init(mutex, NULL));
1760 ASSERT_EQ(0, pthread_mutex_lock(mutex));
1761 ASSERT_EQ(0, pthread_mutex_unlock(mutex));
1762 ASSERT_EQ(0, pthread_mutex_destroy(mutex));
1763
1764 pthread_cond_t* cond = reinterpret_cast<pthread_cond_t*>(
1765 allocator.allocate(sizeof(pthread_cond_t), 4));
1766 ASSERT_EQ(0, pthread_cond_init(cond, NULL));
1767 ASSERT_EQ(0, pthread_cond_signal(cond));
1768 ASSERT_EQ(0, pthread_cond_broadcast(cond));
1769 ASSERT_EQ(0, pthread_cond_destroy(cond));
1770
1771 pthread_rwlock_t* rwlock = reinterpret_cast<pthread_rwlock_t*>(
1772 allocator.allocate(sizeof(pthread_rwlock_t), 4));
1773 ASSERT_EQ(0, pthread_rwlock_init(rwlock, NULL));
1774 ASSERT_EQ(0, pthread_rwlock_rdlock(rwlock));
1775 ASSERT_EQ(0, pthread_rwlock_unlock(rwlock));
1776 ASSERT_EQ(0, pthread_rwlock_wrlock(rwlock));
1777 ASSERT_EQ(0, pthread_rwlock_unlock(rwlock));
1778 ASSERT_EQ(0, pthread_rwlock_destroy(rwlock));
1779
1780#else
1781 GTEST_LOG_(INFO) << "This test tests bionic implementation details.";
1782#endif
1783}
Christopher Ferris60907c72015-06-09 18:46:15 -07001784
1785TEST(pthread, pthread_mutex_lock_null_32) {
1786#if defined(__BIONIC__) && !defined(__LP64__)
Dan Albertbaa2a972015-08-13 16:58:50 -07001787 // For LP32, the pthread lock/unlock functions allow a NULL mutex and return
1788 // EINVAL in that case: http://b/19995172.
1789 //
1790 // We decorate the public defintion with _Nonnull so that people recompiling
1791 // their code with get a warning and might fix their bug, but need to pass
1792 // NULL here to test that we remain compatible.
1793 pthread_mutex_t* null_value = nullptr;
1794 ASSERT_EQ(EINVAL, pthread_mutex_lock(null_value));
Christopher Ferris60907c72015-06-09 18:46:15 -07001795#else
1796 GTEST_LOG_(INFO) << "This test tests bionic implementation details on 32 bit devices.";
1797#endif
1798}
1799
1800TEST(pthread, pthread_mutex_unlock_null_32) {
1801#if defined(__BIONIC__) && !defined(__LP64__)
Dan Albertbaa2a972015-08-13 16:58:50 -07001802 // For LP32, the pthread lock/unlock functions allow a NULL mutex and return
1803 // EINVAL in that case: http://b/19995172.
1804 //
1805 // We decorate the public defintion with _Nonnull so that people recompiling
1806 // their code with get a warning and might fix their bug, but need to pass
1807 // NULL here to test that we remain compatible.
1808 pthread_mutex_t* null_value = nullptr;
1809 ASSERT_EQ(EINVAL, pthread_mutex_unlock(null_value));
Christopher Ferris60907c72015-06-09 18:46:15 -07001810#else
1811 GTEST_LOG_(INFO) << "This test tests bionic implementation details on 32 bit devices.";
1812#endif
1813}
1814
1815TEST_F(pthread_DeathTest, pthread_mutex_lock_null_64) {
1816#if defined(__BIONIC__) && defined(__LP64__)
1817 pthread_mutex_t* null_value = nullptr;
1818 ASSERT_EXIT(pthread_mutex_lock(null_value), testing::KilledBySignal(SIGSEGV), "");
1819#else
1820 GTEST_LOG_(INFO) << "This test tests bionic implementation details on 64 bit devices.";
1821#endif
1822}
1823
1824TEST_F(pthread_DeathTest, pthread_mutex_unlock_null_64) {
1825#if defined(__BIONIC__) && defined(__LP64__)
1826 pthread_mutex_t* null_value = nullptr;
1827 ASSERT_EXIT(pthread_mutex_unlock(null_value), testing::KilledBySignal(SIGSEGV), "");
1828#else
1829 GTEST_LOG_(INFO) << "This test tests bionic implementation details on 64 bit devices.";
1830#endif
1831}
Yabin Cui33ac04a2015-09-22 11:16:15 -07001832
1833extern _Unwind_Reason_Code FrameCounter(_Unwind_Context* ctx, void* arg);
1834
1835static volatile bool signal_handler_on_altstack_done;
1836
1837static void SignalHandlerOnAltStack(int signo, siginfo_t*, void*) {
1838 ASSERT_EQ(SIGUSR1, signo);
1839 // Check if we have enough stack space for unwinding.
1840 int count = 0;
1841 _Unwind_Backtrace(FrameCounter, &count);
1842 ASSERT_GT(count, 0);
1843 // Check if we have enough stack space for logging.
1844 std::string s(2048, '*');
1845 GTEST_LOG_(INFO) << s;
1846 signal_handler_on_altstack_done = true;
1847}
1848
1849TEST(pthread, big_enough_signal_stack_for_64bit_arch) {
1850 signal_handler_on_altstack_done = false;
1851 ScopedSignalHandler handler(SIGUSR1, SignalHandlerOnAltStack, SA_SIGINFO | SA_ONSTACK);
1852 kill(getpid(), SIGUSR1);
1853 ASSERT_TRUE(signal_handler_on_altstack_done);
1854}
Yabin Cuie7c2fff2015-11-05 22:06:09 -08001855
1856TEST(pthread, pthread_barrierattr_smoke) {
1857 pthread_barrierattr_t attr;
1858 ASSERT_EQ(0, pthread_barrierattr_init(&attr));
1859 int pshared;
1860 ASSERT_EQ(0, pthread_barrierattr_getpshared(&attr, &pshared));
1861 ASSERT_EQ(PTHREAD_PROCESS_PRIVATE, pshared);
1862 ASSERT_EQ(0, pthread_barrierattr_setpshared(&attr, PTHREAD_PROCESS_SHARED));
1863 ASSERT_EQ(0, pthread_barrierattr_getpshared(&attr, &pshared));
1864 ASSERT_EQ(PTHREAD_PROCESS_SHARED, pshared);
1865 ASSERT_EQ(0, pthread_barrierattr_destroy(&attr));
1866}
1867
Yabin Cui81d27972016-03-22 13:45:55 -07001868struct BarrierTestHelperData {
1869 size_t thread_count;
1870 pthread_barrier_t barrier;
1871 std::atomic<int> finished_mask;
1872 std::atomic<int> serial_thread_count;
Yabin Cuie7c2fff2015-11-05 22:06:09 -08001873 size_t iteration_count;
Yabin Cui81d27972016-03-22 13:45:55 -07001874 std::atomic<size_t> finished_iteration_count;
1875
1876 BarrierTestHelperData(size_t thread_count, size_t iteration_count)
1877 : thread_count(thread_count), finished_mask(0), serial_thread_count(0),
1878 iteration_count(iteration_count), finished_iteration_count(0) {
1879 }
1880};
1881
1882struct BarrierTestHelperArg {
1883 int id;
1884 BarrierTestHelperData* data;
Yabin Cuie7c2fff2015-11-05 22:06:09 -08001885};
1886
1887static void BarrierTestHelper(BarrierTestHelperArg* arg) {
Yabin Cui81d27972016-03-22 13:45:55 -07001888 for (size_t i = 0; i < arg->data->iteration_count; ++i) {
1889 int result = pthread_barrier_wait(&arg->data->barrier);
1890 if (result == PTHREAD_BARRIER_SERIAL_THREAD) {
1891 arg->data->serial_thread_count++;
1892 } else {
1893 ASSERT_EQ(0, result);
1894 }
1895 arg->data->finished_mask |= (1 << arg->id);
1896 if (arg->data->finished_mask == ((1 << arg->data->thread_count) - 1)) {
1897 ASSERT_EQ(1, arg->data->serial_thread_count);
1898 arg->data->finished_iteration_count++;
1899 arg->data->finished_mask = 0;
1900 arg->data->serial_thread_count = 0;
1901 }
Yabin Cuie7c2fff2015-11-05 22:06:09 -08001902 }
1903}
1904
1905TEST(pthread, pthread_barrier_smoke) {
1906 const size_t BARRIER_ITERATION_COUNT = 10;
1907 const size_t BARRIER_THREAD_COUNT = 10;
Yabin Cui81d27972016-03-22 13:45:55 -07001908 BarrierTestHelperData data(BARRIER_THREAD_COUNT, BARRIER_ITERATION_COUNT);
1909 ASSERT_EQ(0, pthread_barrier_init(&data.barrier, nullptr, data.thread_count));
1910 std::vector<pthread_t> threads(data.thread_count);
Yabin Cuie7c2fff2015-11-05 22:06:09 -08001911 std::vector<BarrierTestHelperArg> args(threads.size());
1912 for (size_t i = 0; i < threads.size(); ++i) {
Yabin Cui81d27972016-03-22 13:45:55 -07001913 args[i].id = i;
1914 args[i].data = &data;
Yabin Cuie7c2fff2015-11-05 22:06:09 -08001915 ASSERT_EQ(0, pthread_create(&threads[i], nullptr,
1916 reinterpret_cast<void* (*)(void*)>(BarrierTestHelper), &args[i]));
1917 }
Yabin Cuie7c2fff2015-11-05 22:06:09 -08001918 for (size_t i = 0; i < threads.size(); ++i) {
1919 ASSERT_EQ(0, pthread_join(threads[i], nullptr));
1920 }
Yabin Cui81d27972016-03-22 13:45:55 -07001921 ASSERT_EQ(data.iteration_count, data.finished_iteration_count);
1922 ASSERT_EQ(0, pthread_barrier_destroy(&data.barrier));
1923}
1924
1925struct BarrierDestroyTestArg {
1926 std::atomic<int> tid;
1927 pthread_barrier_t* barrier;
1928};
1929
1930static void BarrierDestroyTestHelper(BarrierDestroyTestArg* arg) {
1931 arg->tid = gettid();
1932 ASSERT_EQ(0, pthread_barrier_wait(arg->barrier));
Yabin Cuie7c2fff2015-11-05 22:06:09 -08001933}
1934
1935TEST(pthread, pthread_barrier_destroy) {
1936 pthread_barrier_t barrier;
1937 ASSERT_EQ(0, pthread_barrier_init(&barrier, nullptr, 2));
1938 pthread_t thread;
Yabin Cui81d27972016-03-22 13:45:55 -07001939 BarrierDestroyTestArg arg;
Yabin Cuie7c2fff2015-11-05 22:06:09 -08001940 arg.tid = 0;
1941 arg.barrier = &barrier;
Yabin Cuie7c2fff2015-11-05 22:06:09 -08001942 ASSERT_EQ(0, pthread_create(&thread, nullptr,
Yabin Cui81d27972016-03-22 13:45:55 -07001943 reinterpret_cast<void* (*)(void*)>(BarrierDestroyTestHelper), &arg));
Yabin Cuie7c2fff2015-11-05 22:06:09 -08001944 WaitUntilThreadSleep(arg.tid);
1945 ASSERT_EQ(EBUSY, pthread_barrier_destroy(&barrier));
1946 ASSERT_EQ(PTHREAD_BARRIER_SERIAL_THREAD, pthread_barrier_wait(&barrier));
1947 // Verify if the barrier can be destroyed directly after pthread_barrier_wait().
1948 ASSERT_EQ(0, pthread_barrier_destroy(&barrier));
1949 ASSERT_EQ(0, pthread_join(thread, nullptr));
1950#if defined(__BIONIC__)
1951 ASSERT_EQ(EINVAL, pthread_barrier_destroy(&barrier));
1952#endif
1953}
1954
1955struct BarrierOrderingTestHelperArg {
1956 pthread_barrier_t* barrier;
1957 size_t* array;
1958 size_t array_length;
1959 size_t id;
1960};
1961
1962void BarrierOrderingTestHelper(BarrierOrderingTestHelperArg* arg) {
1963 const size_t ITERATION_COUNT = 10000;
1964 for (size_t i = 1; i <= ITERATION_COUNT; ++i) {
1965 arg->array[arg->id] = i;
Yabin Cuic9a659c2015-11-05 15:36:08 -08001966 int result = pthread_barrier_wait(arg->barrier);
1967 ASSERT_TRUE(result == 0 || result == PTHREAD_BARRIER_SERIAL_THREAD);
Yabin Cuie7c2fff2015-11-05 22:06:09 -08001968 for (size_t j = 0; j < arg->array_length; ++j) {
1969 ASSERT_EQ(i, arg->array[j]);
1970 }
Yabin Cuic9a659c2015-11-05 15:36:08 -08001971 result = pthread_barrier_wait(arg->barrier);
1972 ASSERT_TRUE(result == 0 || result == PTHREAD_BARRIER_SERIAL_THREAD);
Yabin Cuie7c2fff2015-11-05 22:06:09 -08001973 }
1974}
1975
1976TEST(pthread, pthread_barrier_check_ordering) {
1977 const size_t THREAD_COUNT = 4;
1978 pthread_barrier_t barrier;
1979 ASSERT_EQ(0, pthread_barrier_init(&barrier, nullptr, THREAD_COUNT));
1980 size_t array[THREAD_COUNT];
1981 std::vector<pthread_t> threads(THREAD_COUNT);
1982 std::vector<BarrierOrderingTestHelperArg> args(THREAD_COUNT);
1983 for (size_t i = 0; i < THREAD_COUNT; ++i) {
1984 args[i].barrier = &barrier;
1985 args[i].array = array;
1986 args[i].array_length = THREAD_COUNT;
1987 args[i].id = i;
1988 ASSERT_EQ(0, pthread_create(&threads[i], nullptr,
1989 reinterpret_cast<void* (*)(void*)>(BarrierOrderingTestHelper),
1990 &args[i]));
1991 }
1992 for (size_t i = 0; i < THREAD_COUNT; ++i) {
1993 ASSERT_EQ(0, pthread_join(threads[i], nullptr));
1994 }
1995}
Yabin Cuife3a83a2015-11-17 16:03:18 -08001996
1997TEST(pthread, pthread_spinlock_smoke) {
1998 pthread_spinlock_t lock;
1999 ASSERT_EQ(0, pthread_spin_init(&lock, 0));
2000 ASSERT_EQ(0, pthread_spin_trylock(&lock));
2001 ASSERT_EQ(0, pthread_spin_unlock(&lock));
2002 ASSERT_EQ(0, pthread_spin_lock(&lock));
2003 ASSERT_EQ(EBUSY, pthread_spin_trylock(&lock));
2004 ASSERT_EQ(0, pthread_spin_unlock(&lock));
2005 ASSERT_EQ(0, pthread_spin_destroy(&lock));
2006}