pthread_test.cpp revision cb0443c0fa07e4c049f426e3041894df522732df
1/* 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 "private/ScopeGuard.h" 20#include "BionicDeathTest.h" 21#include "ScopedSignalHandler.h" 22 23#include <errno.h> 24#include <inttypes.h> 25#include <limits.h> 26#include <malloc.h> 27#include <pthread.h> 28#include <signal.h> 29#include <stdio.h> 30#include <sys/mman.h> 31#include <sys/syscall.h> 32#include <time.h> 33#include <unistd.h> 34 35#include <atomic> 36 37TEST(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} 44 45TEST(pthread, pthread_keys_max) { 46 // POSIX says PTHREAD_KEYS_MAX should be at least _POSIX_THREAD_KEYS_MAX. 47 ASSERT_GE(PTHREAD_KEYS_MAX, _POSIX_THREAD_KEYS_MAX); 48} 49 50TEST(pthread, sysconf_SC_THREAD_KEYS_MAX_eq_PTHREAD_KEYS_MAX) { 51 int sysconf_max = sysconf(_SC_THREAD_KEYS_MAX); 52 ASSERT_EQ(sysconf_max, PTHREAD_KEYS_MAX); 53} 54 55TEST(pthread, pthread_key_many_distinct) { 56 // 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; 59 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 84TEST(pthread, pthread_key_not_exceed_PTHREAD_KEYS_MAX) { 85 std::vector<pthread_key_t> keys; 86 int rv = 0; 87 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++) { 91 pthread_key_t key; 92 rv = pthread_key_create(&key, NULL); 93 if (rv == EAGAIN) { 94 break; 95 } 96 EXPECT_EQ(0, rv); 97 keys.push_back(key); 98 } 99 100 // Don't leak keys. 101 for (auto key : keys) { 102 EXPECT_EQ(0, pthread_key_delete(key)); 103 } 104 keys.clear(); 105 106 // We should have eventually reached the maximum number of keys and received 107 // EAGAIN. 108 ASSERT_EQ(EAGAIN, rv); 109} 110 111TEST(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 124TEST(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)); 146 ASSERT_EQ(0, pthread_key_delete(key)); 147} 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)); 174 ASSERT_EQ(0, pthread_key_delete(key)); 175} 176 177static void* IdFn(void* arg) { 178 return arg; 179} 180 181class SpinFunctionHelper { 182 public: 183 SpinFunctionHelper() { 184 SpinFunctionHelper::spin_flag_ = true; 185 } 186 ~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; 209 210static void* JoinFn(void* arg) { 211 return reinterpret_cast<void*>(pthread_join(reinterpret_cast<pthread_t>(arg), NULL)); 212} 213 214static 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 223static void MakeDeadThread(pthread_t& t) { 224 ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, NULL)); 225 ASSERT_EQ(0, pthread_join(t, NULL)); 226} 227 228TEST(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 239TEST(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 248TEST(pthread, pthread_no_join_after_detach) { 249 SpinFunctionHelper spinhelper; 250 251 pthread_t t1; 252 ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL)); 253 254 // After a pthread_detach... 255 ASSERT_EQ(0, pthread_detach(t1)); 256 AssertDetached(t1, true); 257 258 // ...pthread_join should fail. 259 ASSERT_EQ(EINVAL, pthread_join(t1, NULL)); 260} 261 262TEST(pthread, pthread_no_op_detach_after_join) { 263 SpinFunctionHelper spinhelper; 264 265 pthread_t t1; 266 ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL)); 267 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 272 sleep(1); // (Give t2 a chance to call pthread_join.) 273 274 // ...a call to pthread_detach on thread 1 will "succeed" (silently fail)... 275 ASSERT_EQ(0, pthread_detach(t1)); 276 AssertDetached(t1, false); 277 278 spinhelper.UnSpin(); 279 280 // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes). 281 void* join_result; 282 ASSERT_EQ(0, pthread_join(t2, &join_result)); 283 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result)); 284} 285 286TEST(pthread, pthread_join_self) { 287 ASSERT_EQ(EDEADLK, pthread_join(pthread_self(), NULL)); 288} 289 290struct TestBug37410 { 291 pthread_t main_thread; 292 pthread_mutex_t mutex; 293 294 static void main() { 295 TestBug37410 data; 296 data.main_thread = pthread_self(); 297 ASSERT_EQ(0, pthread_mutex_init(&data.mutex, NULL)); 298 ASSERT_EQ(0, pthread_mutex_lock(&data.mutex)); 299 300 pthread_t t; 301 ASSERT_EQ(0, pthread_create(&t, NULL, TestBug37410::thread_fn, reinterpret_cast<void*>(&data))); 302 303 // Wait for the thread to be running... 304 ASSERT_EQ(0, pthread_mutex_lock(&data.mutex)); 305 ASSERT_EQ(0, pthread_mutex_unlock(&data.mutex)); 306 307 // ...and exit. 308 pthread_exit(NULL); 309 } 310 311 private: 312 static void* thread_fn(void* arg) { 313 TestBug37410* data = reinterpret_cast<TestBug37410*>(arg); 314 315 // Let the main thread know we're running. 316 pthread_mutex_unlock(&data->mutex); 317 318 // And wait for the main thread to exit. 319 pthread_join(data->main_thread, NULL); 320 321 return NULL; 322 } 323}; 324 325// Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to 326// run this test (which exits normally) in its own process. 327 328class pthread_DeathTest : public BionicDeathTest {}; 329 330TEST_F(pthread_DeathTest, pthread_bug_37410) { 331 // http://code.google.com/p/android/issues/detail?id=37410 332 ASSERT_EXIT(TestBug37410::main(), ::testing::ExitedWithCode(0), ""); 333} 334 335static void* SignalHandlerFn(void* arg) { 336 sigset_t wait_set; 337 sigfillset(&wait_set); 338 return reinterpret_cast<void*>(sigwait(&wait_set, reinterpret_cast<int*>(arg))); 339} 340 341TEST(pthread, pthread_sigmask) { 342 // Check that SIGUSR1 isn't blocked. 343 sigset_t original_set; 344 sigemptyset(&original_set); 345 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &original_set)); 346 ASSERT_FALSE(sigismember(&original_set, SIGUSR1)); 347 348 // Block SIGUSR1. 349 sigset_t set; 350 sigemptyset(&set); 351 sigaddset(&set, SIGUSR1); 352 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &set, NULL)); 353 354 // Check that SIGUSR1 is blocked. 355 sigset_t final_set; 356 sigemptyset(&final_set); 357 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &final_set)); 358 ASSERT_TRUE(sigismember(&final_set, SIGUSR1)); 359 // ...and that sigprocmask agrees with pthread_sigmask. 360 sigemptyset(&final_set); 361 ASSERT_EQ(0, sigprocmask(SIG_BLOCK, NULL, &final_set)); 362 ASSERT_TRUE(sigismember(&final_set, SIGUSR1)); 363 364 // Spawn a thread that calls sigwait and tells us what it received. 365 pthread_t signal_thread; 366 int received_signal = -1; 367 ASSERT_EQ(0, pthread_create(&signal_thread, NULL, SignalHandlerFn, &received_signal)); 368 369 // Send that thread SIGUSR1. 370 pthread_kill(signal_thread, SIGUSR1); 371 372 // See what it got. 373 void* join_result; 374 ASSERT_EQ(0, pthread_join(signal_thread, &join_result)); 375 ASSERT_EQ(SIGUSR1, received_signal); 376 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result)); 377 378 // Restore the original signal mask. 379 ASSERT_EQ(0, pthread_sigmask(SIG_SETMASK, &original_set, NULL)); 380} 381 382TEST(pthread, pthread_setname_np__too_long) { 383 ASSERT_EQ(ERANGE, pthread_setname_np(pthread_self(), "this name is far too long for linux")); 384} 385 386TEST(pthread, pthread_setname_np__self) { 387 ASSERT_EQ(0, pthread_setname_np(pthread_self(), "short 1")); 388} 389 390TEST(pthread, pthread_setname_np__other) { 391 SpinFunctionHelper spinhelper; 392 393 pthread_t t1; 394 ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL)); 395 ASSERT_EQ(0, pthread_setname_np(t1, "short 2")); 396} 397 398TEST(pthread, pthread_setname_np__no_such_thread) { 399 pthread_t dead_thread; 400 MakeDeadThread(dead_thread); 401 402 // Call pthread_setname_np after thread has already exited. 403 ASSERT_EQ(ENOENT, pthread_setname_np(dead_thread, "short 3")); 404} 405 406TEST(pthread, pthread_kill__0) { 407 // Signal 0 just tests that the thread exists, so it's safe to call on ourselves. 408 ASSERT_EQ(0, pthread_kill(pthread_self(), 0)); 409} 410 411TEST(pthread, pthread_kill__invalid_signal) { 412 ASSERT_EQ(EINVAL, pthread_kill(pthread_self(), -1)); 413} 414 415static void pthread_kill__in_signal_handler_helper(int signal_number) { 416 static int count = 0; 417 ASSERT_EQ(SIGALRM, signal_number); 418 if (++count == 1) { 419 // Can we call pthread_kill from a signal handler? 420 ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM)); 421 } 422} 423 424TEST(pthread, pthread_kill__in_signal_handler) { 425 ScopedSignalHandler ssh(SIGALRM, pthread_kill__in_signal_handler_helper); 426 ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM)); 427} 428 429TEST(pthread, pthread_detach__no_such_thread) { 430 pthread_t dead_thread; 431 MakeDeadThread(dead_thread); 432 433 ASSERT_EQ(ESRCH, pthread_detach(dead_thread)); 434} 435 436TEST(pthread, pthread_detach_no_leak) { 437 size_t initial_bytes = 0; 438 // Run this loop more than once since the first loop causes some memory 439 // to be allocated permenantly. Run an extra loop to help catch any subtle 440 // memory leaks. 441 for (size_t loop = 0; loop < 3; loop++) { 442 // Set the initial bytes on the second loop since the memory in use 443 // should have stabilized. 444 if (loop == 1) { 445 initial_bytes = mallinfo().uordblks; 446 } 447 448 pthread_attr_t attr; 449 ASSERT_EQ(0, pthread_attr_init(&attr)); 450 ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE)); 451 452 std::vector<pthread_t> threads; 453 for (size_t i = 0; i < 32; ++i) { 454 pthread_t t; 455 ASSERT_EQ(0, pthread_create(&t, &attr, IdFn, NULL)); 456 threads.push_back(t); 457 } 458 459 sleep(1); 460 461 for (size_t i = 0; i < 32; ++i) { 462 ASSERT_EQ(0, pthread_detach(threads[i])) << i; 463 } 464 } 465 466 size_t final_bytes = mallinfo().uordblks; 467 int leaked_bytes = (final_bytes - initial_bytes); 468 469 ASSERT_EQ(0, leaked_bytes); 470} 471 472TEST(pthread, pthread_getcpuclockid__clock_gettime) { 473 SpinFunctionHelper spinhelper; 474 475 pthread_t t; 476 ASSERT_EQ(0, pthread_create(&t, NULL, spinhelper.GetFunction(), NULL)); 477 478 clockid_t c; 479 ASSERT_EQ(0, pthread_getcpuclockid(t, &c)); 480 timespec ts; 481 ASSERT_EQ(0, clock_gettime(c, &ts)); 482} 483 484TEST(pthread, pthread_getcpuclockid__no_such_thread) { 485 pthread_t dead_thread; 486 MakeDeadThread(dead_thread); 487 488 clockid_t c; 489 ASSERT_EQ(ESRCH, pthread_getcpuclockid(dead_thread, &c)); 490} 491 492TEST(pthread, pthread_getschedparam__no_such_thread) { 493 pthread_t dead_thread; 494 MakeDeadThread(dead_thread); 495 496 int policy; 497 sched_param param; 498 ASSERT_EQ(ESRCH, pthread_getschedparam(dead_thread, &policy, ¶m)); 499} 500 501TEST(pthread, pthread_setschedparam__no_such_thread) { 502 pthread_t dead_thread; 503 MakeDeadThread(dead_thread); 504 505 int policy = 0; 506 sched_param param; 507 ASSERT_EQ(ESRCH, pthread_setschedparam(dead_thread, policy, ¶m)); 508} 509 510TEST(pthread, pthread_join__no_such_thread) { 511 pthread_t dead_thread; 512 MakeDeadThread(dead_thread); 513 514 ASSERT_EQ(ESRCH, pthread_join(dead_thread, NULL)); 515} 516 517TEST(pthread, pthread_kill__no_such_thread) { 518 pthread_t dead_thread; 519 MakeDeadThread(dead_thread); 520 521 ASSERT_EQ(ESRCH, pthread_kill(dead_thread, 0)); 522} 523 524TEST(pthread, pthread_join__multijoin) { 525 SpinFunctionHelper spinhelper; 526 527 pthread_t t1; 528 ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL)); 529 530 pthread_t t2; 531 ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1))); 532 533 sleep(1); // (Give t2 a chance to call pthread_join.) 534 535 // Multiple joins to the same thread should fail. 536 ASSERT_EQ(EINVAL, pthread_join(t1, NULL)); 537 538 spinhelper.UnSpin(); 539 540 // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes). 541 void* join_result; 542 ASSERT_EQ(0, pthread_join(t2, &join_result)); 543 ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result)); 544} 545 546TEST(pthread, pthread_join__race) { 547 // http://b/11693195 --- pthread_join could return before the thread had actually exited. 548 // If the joiner unmapped the thread's stack, that could lead to SIGSEGV in the thread. 549 for (size_t i = 0; i < 1024; ++i) { 550 size_t stack_size = 64*1024; 551 void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0); 552 553 pthread_attr_t a; 554 pthread_attr_init(&a); 555 pthread_attr_setstack(&a, stack, stack_size); 556 557 pthread_t t; 558 ASSERT_EQ(0, pthread_create(&t, &a, IdFn, NULL)); 559 ASSERT_EQ(0, pthread_join(t, NULL)); 560 ASSERT_EQ(0, munmap(stack, stack_size)); 561 } 562} 563 564static void* GetActualGuardSizeFn(void* arg) { 565 pthread_attr_t attributes; 566 pthread_getattr_np(pthread_self(), &attributes); 567 pthread_attr_getguardsize(&attributes, reinterpret_cast<size_t*>(arg)); 568 return NULL; 569} 570 571static size_t GetActualGuardSize(const pthread_attr_t& attributes) { 572 size_t result; 573 pthread_t t; 574 pthread_create(&t, &attributes, GetActualGuardSizeFn, &result); 575 pthread_join(t, NULL); 576 return result; 577} 578 579static void* GetActualStackSizeFn(void* arg) { 580 pthread_attr_t attributes; 581 pthread_getattr_np(pthread_self(), &attributes); 582 pthread_attr_getstacksize(&attributes, reinterpret_cast<size_t*>(arg)); 583 return NULL; 584} 585 586static size_t GetActualStackSize(const pthread_attr_t& attributes) { 587 size_t result; 588 pthread_t t; 589 pthread_create(&t, &attributes, GetActualStackSizeFn, &result); 590 pthread_join(t, NULL); 591 return result; 592} 593 594TEST(pthread, pthread_attr_setguardsize) { 595 pthread_attr_t attributes; 596 ASSERT_EQ(0, pthread_attr_init(&attributes)); 597 598 // Get the default guard size. 599 size_t default_guard_size; 600 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &default_guard_size)); 601 602 // No such thing as too small: will be rounded up to one page by pthread_create. 603 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 128)); 604 size_t guard_size; 605 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size)); 606 ASSERT_EQ(128U, guard_size); 607 ASSERT_EQ(4096U, GetActualGuardSize(attributes)); 608 609 // Large enough and a multiple of the page size. 610 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024)); 611 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size)); 612 ASSERT_EQ(32*1024U, guard_size); 613 614 // Large enough but not a multiple of the page size; will be rounded up by pthread_create. 615 ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024 + 1)); 616 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size)); 617 ASSERT_EQ(32*1024U + 1, guard_size); 618} 619 620TEST(pthread, pthread_attr_setstacksize) { 621 pthread_attr_t attributes; 622 ASSERT_EQ(0, pthread_attr_init(&attributes)); 623 624 // Get the default stack size. 625 size_t default_stack_size; 626 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &default_stack_size)); 627 628 // Too small. 629 ASSERT_EQ(EINVAL, pthread_attr_setstacksize(&attributes, 128)); 630 size_t stack_size; 631 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size)); 632 ASSERT_EQ(default_stack_size, stack_size); 633 ASSERT_GE(GetActualStackSize(attributes), default_stack_size); 634 635 // Large enough and a multiple of the page size; may be rounded up by pthread_create. 636 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024)); 637 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size)); 638 ASSERT_EQ(32*1024U, stack_size); 639 ASSERT_GE(GetActualStackSize(attributes), 32*1024U); 640 641 // Large enough but not aligned; will be rounded up by pthread_create. 642 ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024 + 1)); 643 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size)); 644 ASSERT_EQ(32*1024U + 1, stack_size); 645#if defined(__BIONIC__) 646 ASSERT_GT(GetActualStackSize(attributes), 32*1024U + 1); 647#else // __BIONIC__ 648 // glibc rounds down, in violation of POSIX. They document this in their BUGS section. 649 ASSERT_EQ(GetActualStackSize(attributes), 32*1024U); 650#endif // __BIONIC__ 651} 652 653TEST(pthread, pthread_rwlock_smoke) { 654 pthread_rwlock_t l; 655 ASSERT_EQ(0, pthread_rwlock_init(&l, NULL)); 656 657 // Single read lock 658 ASSERT_EQ(0, pthread_rwlock_rdlock(&l)); 659 ASSERT_EQ(0, pthread_rwlock_unlock(&l)); 660 661 // Multiple read lock 662 ASSERT_EQ(0, pthread_rwlock_rdlock(&l)); 663 ASSERT_EQ(0, pthread_rwlock_rdlock(&l)); 664 ASSERT_EQ(0, pthread_rwlock_unlock(&l)); 665 ASSERT_EQ(0, pthread_rwlock_unlock(&l)); 666 667 // Write lock 668 ASSERT_EQ(0, pthread_rwlock_wrlock(&l)); 669 ASSERT_EQ(0, pthread_rwlock_unlock(&l)); 670 671 // Try writer lock 672 ASSERT_EQ(0, pthread_rwlock_trywrlock(&l)); 673 ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l)); 674 ASSERT_EQ(EBUSY, pthread_rwlock_tryrdlock(&l)); 675 ASSERT_EQ(0, pthread_rwlock_unlock(&l)); 676 677 // Try reader lock 678 ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l)); 679 ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l)); 680 ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l)); 681 ASSERT_EQ(0, pthread_rwlock_unlock(&l)); 682 ASSERT_EQ(0, pthread_rwlock_unlock(&l)); 683 684 // Try writer lock after unlock 685 ASSERT_EQ(0, pthread_rwlock_wrlock(&l)); 686 ASSERT_EQ(0, pthread_rwlock_unlock(&l)); 687 688#ifdef __BIONIC__ 689 // EDEADLK in "read after write" 690 ASSERT_EQ(0, pthread_rwlock_wrlock(&l)); 691 ASSERT_EQ(EDEADLK, pthread_rwlock_rdlock(&l)); 692 ASSERT_EQ(0, pthread_rwlock_unlock(&l)); 693 694 // EDEADLK in "write after write" 695 ASSERT_EQ(0, pthread_rwlock_wrlock(&l)); 696 ASSERT_EQ(EDEADLK, pthread_rwlock_wrlock(&l)); 697 ASSERT_EQ(0, pthread_rwlock_unlock(&l)); 698#endif 699 700 ASSERT_EQ(0, pthread_rwlock_destroy(&l)); 701} 702 703struct RwlockWakeupHelperArg { 704 pthread_rwlock_t lock; 705 enum Progress { 706 LOCK_INITIALIZED, 707 LOCK_WAITING, 708 LOCK_RELEASED, 709 LOCK_ACCESSED 710 }; 711 std::atomic<Progress> progress; 712}; 713 714static void pthread_rwlock_reader_wakeup_writer_helper(RwlockWakeupHelperArg* arg) { 715 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_INITIALIZED, arg->progress); 716 arg->progress = RwlockWakeupHelperArg::LOCK_WAITING; 717 718 ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&arg->lock)); 719 ASSERT_EQ(0, pthread_rwlock_wrlock(&arg->lock)); 720 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_RELEASED, arg->progress); 721 ASSERT_EQ(0, pthread_rwlock_unlock(&arg->lock)); 722 723 arg->progress = RwlockWakeupHelperArg::LOCK_ACCESSED; 724} 725 726TEST(pthread, pthread_rwlock_reader_wakeup_writer) { 727 RwlockWakeupHelperArg wakeup_arg; 728 ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, NULL)); 729 ASSERT_EQ(0, pthread_rwlock_rdlock(&wakeup_arg.lock)); 730 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED; 731 732 pthread_t thread; 733 ASSERT_EQ(0, pthread_create(&thread, NULL, 734 reinterpret_cast<void* (*)(void*)>(pthread_rwlock_reader_wakeup_writer_helper), &wakeup_arg)); 735 sleep(1); 736 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress); 737 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_RELEASED; 738 ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock)); 739 740 ASSERT_EQ(0, pthread_join(thread, NULL)); 741 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_ACCESSED, wakeup_arg.progress); 742 ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock)); 743} 744 745static void pthread_rwlock_writer_wakeup_reader_helper(RwlockWakeupHelperArg* arg) { 746 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_INITIALIZED, arg->progress); 747 arg->progress = RwlockWakeupHelperArg::LOCK_WAITING; 748 749 ASSERT_EQ(EBUSY, pthread_rwlock_tryrdlock(&arg->lock)); 750 ASSERT_EQ(0, pthread_rwlock_rdlock(&arg->lock)); 751 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_RELEASED, arg->progress); 752 ASSERT_EQ(0, pthread_rwlock_unlock(&arg->lock)); 753 754 arg->progress = RwlockWakeupHelperArg::LOCK_ACCESSED; 755} 756 757TEST(pthread, pthread_rwlock_writer_wakeup_reader) { 758 RwlockWakeupHelperArg wakeup_arg; 759 ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, NULL)); 760 ASSERT_EQ(0, pthread_rwlock_wrlock(&wakeup_arg.lock)); 761 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED; 762 763 pthread_t thread; 764 ASSERT_EQ(0, pthread_create(&thread, NULL, 765 reinterpret_cast<void* (*)(void*)>(pthread_rwlock_writer_wakeup_reader_helper), &wakeup_arg)); 766 sleep(1); 767 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress); 768 wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_RELEASED; 769 ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock)); 770 771 ASSERT_EQ(0, pthread_join(thread, NULL)); 772 ASSERT_EQ(RwlockWakeupHelperArg::LOCK_ACCESSED, wakeup_arg.progress); 773 ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock)); 774} 775 776static int g_once_fn_call_count = 0; 777static void OnceFn() { 778 ++g_once_fn_call_count; 779} 780 781TEST(pthread, pthread_once_smoke) { 782 pthread_once_t once_control = PTHREAD_ONCE_INIT; 783 ASSERT_EQ(0, pthread_once(&once_control, OnceFn)); 784 ASSERT_EQ(0, pthread_once(&once_control, OnceFn)); 785 ASSERT_EQ(1, g_once_fn_call_count); 786} 787 788static std::string pthread_once_1934122_result = ""; 789 790static void Routine2() { 791 pthread_once_1934122_result += "2"; 792} 793 794static void Routine1() { 795 pthread_once_t once_control_2 = PTHREAD_ONCE_INIT; 796 pthread_once_1934122_result += "1"; 797 pthread_once(&once_control_2, &Routine2); 798} 799 800TEST(pthread, pthread_once_1934122) { 801 // Very old versions of Android couldn't call pthread_once from a 802 // pthread_once init routine. http://b/1934122. 803 pthread_once_t once_control_1 = PTHREAD_ONCE_INIT; 804 ASSERT_EQ(0, pthread_once(&once_control_1, &Routine1)); 805 ASSERT_EQ("12", pthread_once_1934122_result); 806} 807 808static int g_atfork_prepare_calls = 0; 809static void AtForkPrepare1() { g_atfork_prepare_calls = (g_atfork_prepare_calls << 4) | 1; } 810static void AtForkPrepare2() { g_atfork_prepare_calls = (g_atfork_prepare_calls << 4) | 2; } 811static int g_atfork_parent_calls = 0; 812static void AtForkParent1() { g_atfork_parent_calls = (g_atfork_parent_calls << 4) | 1; } 813static void AtForkParent2() { g_atfork_parent_calls = (g_atfork_parent_calls << 4) | 2; } 814static int g_atfork_child_calls = 0; 815static void AtForkChild1() { g_atfork_child_calls = (g_atfork_child_calls << 4) | 1; } 816static void AtForkChild2() { g_atfork_child_calls = (g_atfork_child_calls << 4) | 2; } 817 818TEST(pthread, pthread_atfork_smoke) { 819 ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1)); 820 ASSERT_EQ(0, pthread_atfork(AtForkPrepare2, AtForkParent2, AtForkChild2)); 821 822 int pid = fork(); 823 ASSERT_NE(-1, pid) << strerror(errno); 824 825 // Child and parent calls are made in the order they were registered. 826 if (pid == 0) { 827 ASSERT_EQ(0x12, g_atfork_child_calls); 828 _exit(0); 829 } 830 ASSERT_EQ(0x12, g_atfork_parent_calls); 831 832 // Prepare calls are made in the reverse order. 833 ASSERT_EQ(0x21, g_atfork_prepare_calls); 834} 835 836TEST(pthread, pthread_attr_getscope) { 837 pthread_attr_t attr; 838 ASSERT_EQ(0, pthread_attr_init(&attr)); 839 840 int scope; 841 ASSERT_EQ(0, pthread_attr_getscope(&attr, &scope)); 842 ASSERT_EQ(PTHREAD_SCOPE_SYSTEM, scope); 843} 844 845TEST(pthread, pthread_condattr_init) { 846 pthread_condattr_t attr; 847 pthread_condattr_init(&attr); 848 849 clockid_t clock; 850 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock)); 851 ASSERT_EQ(CLOCK_REALTIME, clock); 852 853 int pshared; 854 ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared)); 855 ASSERT_EQ(PTHREAD_PROCESS_PRIVATE, pshared); 856} 857 858TEST(pthread, pthread_condattr_setclock) { 859 pthread_condattr_t attr; 860 pthread_condattr_init(&attr); 861 862 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_REALTIME)); 863 clockid_t clock; 864 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock)); 865 ASSERT_EQ(CLOCK_REALTIME, clock); 866 867 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC)); 868 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock)); 869 ASSERT_EQ(CLOCK_MONOTONIC, clock); 870 871 ASSERT_EQ(EINVAL, pthread_condattr_setclock(&attr, CLOCK_PROCESS_CPUTIME_ID)); 872} 873 874TEST(pthread, pthread_cond_broadcast__preserves_condattr_flags) { 875#if defined(__BIONIC__) 876 pthread_condattr_t attr; 877 pthread_condattr_init(&attr); 878 879 ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC)); 880 ASSERT_EQ(0, pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED)); 881 882 pthread_cond_t cond_var; 883 ASSERT_EQ(0, pthread_cond_init(&cond_var, &attr)); 884 885 ASSERT_EQ(0, pthread_cond_signal(&cond_var)); 886 ASSERT_EQ(0, pthread_cond_broadcast(&cond_var)); 887 888 attr = static_cast<pthread_condattr_t>(*reinterpret_cast<uint32_t*>(cond_var.__private)); 889 clockid_t clock; 890 ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock)); 891 ASSERT_EQ(CLOCK_MONOTONIC, clock); 892 int pshared; 893 ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared)); 894 ASSERT_EQ(PTHREAD_PROCESS_SHARED, pshared); 895#else // !defined(__BIONIC__) 896 GTEST_LOG_(INFO) << "This tests a bionic implementation detail.\n"; 897#endif // !defined(__BIONIC__) 898} 899 900class pthread_CondWakeupTest : public ::testing::Test { 901 protected: 902 pthread_mutex_t mutex; 903 pthread_cond_t cond; 904 905 enum Progress { 906 INITIALIZED, 907 WAITING, 908 SIGNALED, 909 FINISHED, 910 }; 911 std::atomic<Progress> progress; 912 pthread_t thread; 913 914 protected: 915 virtual void SetUp() { 916 ASSERT_EQ(0, pthread_mutex_init(&mutex, NULL)); 917 ASSERT_EQ(0, pthread_cond_init(&cond, NULL)); 918 progress = INITIALIZED; 919 ASSERT_EQ(0, 920 pthread_create(&thread, NULL, reinterpret_cast<void* (*)(void*)>(WaitThreadFn), this)); 921 } 922 923 virtual void TearDown() { 924 ASSERT_EQ(0, pthread_join(thread, NULL)); 925 ASSERT_EQ(FINISHED, progress); 926 ASSERT_EQ(0, pthread_cond_destroy(&cond)); 927 ASSERT_EQ(0, pthread_mutex_destroy(&mutex)); 928 } 929 930 void SleepUntilProgress(Progress expected_progress) { 931 while (progress != expected_progress) { 932 usleep(5000); 933 } 934 usleep(5000); 935 } 936 937 private: 938 static void WaitThreadFn(pthread_CondWakeupTest* test) { 939 ASSERT_EQ(0, pthread_mutex_lock(&test->mutex)); 940 test->progress = WAITING; 941 while (test->progress == WAITING) { 942 ASSERT_EQ(0, pthread_cond_wait(&test->cond, &test->mutex)); 943 } 944 ASSERT_EQ(SIGNALED, test->progress); 945 test->progress = FINISHED; 946 ASSERT_EQ(0, pthread_mutex_unlock(&test->mutex)); 947 } 948}; 949 950TEST_F(pthread_CondWakeupTest, signal) { 951 SleepUntilProgress(WAITING); 952 progress = SIGNALED; 953 pthread_cond_signal(&cond); 954} 955 956TEST_F(pthread_CondWakeupTest, broadcast) { 957 SleepUntilProgress(WAITING); 958 progress = SIGNALED; 959 pthread_cond_broadcast(&cond); 960} 961 962TEST(pthread, pthread_mutex_timedlock) { 963 pthread_mutex_t m; 964 ASSERT_EQ(0, pthread_mutex_init(&m, NULL)); 965 966 // If the mutex is already locked, pthread_mutex_timedlock should time out. 967 ASSERT_EQ(0, pthread_mutex_lock(&m)); 968 969 timespec ts; 970 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts)); 971 ts.tv_nsec += 1; 972 ASSERT_EQ(ETIMEDOUT, pthread_mutex_timedlock(&m, &ts)); 973 974 // If the mutex is unlocked, pthread_mutex_timedlock should succeed. 975 ASSERT_EQ(0, pthread_mutex_unlock(&m)); 976 977 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts)); 978 ts.tv_nsec += 1; 979 ASSERT_EQ(0, pthread_mutex_timedlock(&m, &ts)); 980 981 ASSERT_EQ(0, pthread_mutex_unlock(&m)); 982 ASSERT_EQ(0, pthread_mutex_destroy(&m)); 983} 984 985TEST(pthread, pthread_attr_getstack__main_thread) { 986 // This test is only meaningful for the main thread, so make sure we're running on it! 987 ASSERT_EQ(getpid(), syscall(__NR_gettid)); 988 989 // Get the main thread's attributes. 990 pthread_attr_t attributes; 991 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes)); 992 993 // Check that we correctly report that the main thread has no guard page. 994 size_t guard_size; 995 ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size)); 996 ASSERT_EQ(0U, guard_size); // The main thread has no guard page. 997 998 // Get the stack base and the stack size (both ways). 999 void* stack_base; 1000 size_t stack_size; 1001 ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size)); 1002 size_t stack_size2; 1003 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2)); 1004 1005 // The two methods of asking for the stack size should agree. 1006 EXPECT_EQ(stack_size, stack_size2); 1007 1008 // What does /proc/self/maps' [stack] line say? 1009 void* maps_stack_hi = NULL; 1010 FILE* fp = fopen("/proc/self/maps", "r"); 1011 ASSERT_TRUE(fp != NULL); 1012 char line[BUFSIZ]; 1013 while (fgets(line, sizeof(line), fp) != NULL) { 1014 uintptr_t lo, hi; 1015 char name[10]; 1016 sscanf(line, "%" PRIxPTR "-%" PRIxPTR " %*4s %*x %*x:%*x %*d %10s", &lo, &hi, name); 1017 if (strcmp(name, "[stack]") == 0) { 1018 maps_stack_hi = reinterpret_cast<void*>(hi); 1019 break; 1020 } 1021 } 1022 fclose(fp); 1023 1024 // The stack size should correspond to RLIMIT_STACK. 1025 rlimit rl; 1026 ASSERT_EQ(0, getrlimit(RLIMIT_STACK, &rl)); 1027 uint64_t original_rlim_cur = rl.rlim_cur; 1028#if defined(__BIONIC__) 1029 if (rl.rlim_cur == RLIM_INFINITY) { 1030 rl.rlim_cur = 8 * 1024 * 1024; // Bionic reports unlimited stacks as 8MiB. 1031 } 1032#endif 1033 EXPECT_EQ(rl.rlim_cur, stack_size); 1034 1035 auto guard = make_scope_guard([&rl, original_rlim_cur]() { 1036 rl.rlim_cur = original_rlim_cur; 1037 ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl)); 1038 }); 1039 1040 // The high address of the /proc/self/maps [stack] region should equal stack_base + stack_size. 1041 // Remember that the stack grows down (and is mapped in on demand), so the low address of the 1042 // region isn't very interesting. 1043 EXPECT_EQ(maps_stack_hi, reinterpret_cast<uint8_t*>(stack_base) + stack_size); 1044 1045 // 1046 // What if RLIMIT_STACK is smaller than the stack's current extent? 1047 // 1048 rl.rlim_cur = rl.rlim_max = 1024; // 1KiB. We know the stack must be at least a page already. 1049 rl.rlim_max = RLIM_INFINITY; 1050 ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl)); 1051 1052 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes)); 1053 ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size)); 1054 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2)); 1055 1056 EXPECT_EQ(stack_size, stack_size2); 1057 ASSERT_EQ(1024U, stack_size); 1058 1059 // 1060 // What if RLIMIT_STACK isn't a whole number of pages? 1061 // 1062 rl.rlim_cur = rl.rlim_max = 6666; // Not a whole number of pages. 1063 rl.rlim_max = RLIM_INFINITY; 1064 ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl)); 1065 1066 ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes)); 1067 ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size)); 1068 ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2)); 1069 1070 EXPECT_EQ(stack_size, stack_size2); 1071 ASSERT_EQ(6666U, stack_size); 1072} 1073 1074static void pthread_attr_getstack_18908062_helper(void*) { 1075 char local_variable; 1076 pthread_attr_t attributes; 1077 pthread_getattr_np(pthread_self(), &attributes); 1078 void* stack_base; 1079 size_t stack_size; 1080 pthread_attr_getstack(&attributes, &stack_base, &stack_size); 1081 1082 // Test whether &local_variable is in [stack_base, stack_base + stack_size). 1083 ASSERT_LE(reinterpret_cast<char*>(stack_base), &local_variable); 1084 ASSERT_LT(&local_variable, reinterpret_cast<char*>(stack_base) + stack_size); 1085} 1086 1087// Check whether something on stack is in the range of 1088// [stack_base, stack_base + stack_size). see b/18908062. 1089TEST(pthread, pthread_attr_getstack_18908062) { 1090 pthread_t t; 1091 ASSERT_EQ(0, pthread_create(&t, NULL, 1092 reinterpret_cast<void* (*)(void*)>(pthread_attr_getstack_18908062_helper), 1093 NULL)); 1094 pthread_join(t, NULL); 1095} 1096 1097#if defined(__BIONIC__) 1098static void* pthread_gettid_np_helper(void* arg) { 1099 *reinterpret_cast<pid_t*>(arg) = gettid(); 1100 return NULL; 1101} 1102#endif 1103 1104TEST(pthread, pthread_gettid_np) { 1105#if defined(__BIONIC__) 1106 ASSERT_EQ(gettid(), pthread_gettid_np(pthread_self())); 1107 1108 pid_t t_gettid_result; 1109 pthread_t t; 1110 pthread_create(&t, NULL, pthread_gettid_np_helper, &t_gettid_result); 1111 1112 pid_t t_pthread_gettid_np_result = pthread_gettid_np(t); 1113 1114 pthread_join(t, NULL); 1115 1116 ASSERT_EQ(t_gettid_result, t_pthread_gettid_np_result); 1117#else 1118 GTEST_LOG_(INFO) << "This test does nothing.\n"; 1119#endif 1120} 1121 1122static size_t cleanup_counter = 0; 1123 1124static void AbortCleanupRoutine(void*) { 1125 abort(); 1126} 1127 1128static void CountCleanupRoutine(void*) { 1129 ++cleanup_counter; 1130} 1131 1132static void PthreadCleanupTester() { 1133 pthread_cleanup_push(CountCleanupRoutine, NULL); 1134 pthread_cleanup_push(CountCleanupRoutine, NULL); 1135 pthread_cleanup_push(AbortCleanupRoutine, NULL); 1136 1137 pthread_cleanup_pop(0); // Pop the abort without executing it. 1138 pthread_cleanup_pop(1); // Pop one count while executing it. 1139 ASSERT_EQ(1U, cleanup_counter); 1140 // Exit while the other count is still on the cleanup stack. 1141 pthread_exit(NULL); 1142 1143 // Calls to pthread_cleanup_pop/pthread_cleanup_push must always be balanced. 1144 pthread_cleanup_pop(0); 1145} 1146 1147static void* PthreadCleanupStartRoutine(void*) { 1148 PthreadCleanupTester(); 1149 return NULL; 1150} 1151 1152TEST(pthread, pthread_cleanup_push__pthread_cleanup_pop) { 1153 pthread_t t; 1154 ASSERT_EQ(0, pthread_create(&t, NULL, PthreadCleanupStartRoutine, NULL)); 1155 pthread_join(t, NULL); 1156 ASSERT_EQ(2U, cleanup_counter); 1157} 1158 1159TEST(pthread, PTHREAD_MUTEX_DEFAULT_is_PTHREAD_MUTEX_NORMAL) { 1160 ASSERT_EQ(PTHREAD_MUTEX_NORMAL, PTHREAD_MUTEX_DEFAULT); 1161} 1162 1163TEST(pthread, pthread_mutexattr_gettype) { 1164 pthread_mutexattr_t attr; 1165 ASSERT_EQ(0, pthread_mutexattr_init(&attr)); 1166 1167 int attr_type; 1168 1169 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL)); 1170 ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type)); 1171 ASSERT_EQ(PTHREAD_MUTEX_NORMAL, attr_type); 1172 1173 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK)); 1174 ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type)); 1175 ASSERT_EQ(PTHREAD_MUTEX_ERRORCHECK, attr_type); 1176 1177 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE)); 1178 ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type)); 1179 ASSERT_EQ(PTHREAD_MUTEX_RECURSIVE, attr_type); 1180} 1181 1182TEST(pthread, pthread_mutex_lock_NORMAL) { 1183 pthread_mutexattr_t attr; 1184 ASSERT_EQ(0, pthread_mutexattr_init(&attr)); 1185 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL)); 1186 1187 pthread_mutex_t lock; 1188 ASSERT_EQ(0, pthread_mutex_init(&lock, &attr)); 1189 1190 ASSERT_EQ(0, pthread_mutex_lock(&lock)); 1191 ASSERT_EQ(0, pthread_mutex_unlock(&lock)); 1192 ASSERT_EQ(0, pthread_mutex_destroy(&lock)); 1193} 1194 1195TEST(pthread, pthread_mutex_lock_ERRORCHECK) { 1196 pthread_mutexattr_t attr; 1197 ASSERT_EQ(0, pthread_mutexattr_init(&attr)); 1198 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK)); 1199 1200 pthread_mutex_t lock; 1201 ASSERT_EQ(0, pthread_mutex_init(&lock, &attr)); 1202 1203 ASSERT_EQ(0, pthread_mutex_lock(&lock)); 1204 ASSERT_EQ(EDEADLK, pthread_mutex_lock(&lock)); 1205 ASSERT_EQ(0, pthread_mutex_unlock(&lock)); 1206 ASSERT_EQ(0, pthread_mutex_trylock(&lock)); 1207 ASSERT_EQ(EBUSY, pthread_mutex_trylock(&lock)); 1208 ASSERT_EQ(0, pthread_mutex_unlock(&lock)); 1209 ASSERT_EQ(EPERM, pthread_mutex_unlock(&lock)); 1210 ASSERT_EQ(0, pthread_mutex_destroy(&lock)); 1211} 1212 1213TEST(pthread, pthread_mutex_lock_RECURSIVE) { 1214 pthread_mutexattr_t attr; 1215 ASSERT_EQ(0, pthread_mutexattr_init(&attr)); 1216 ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE)); 1217 1218 pthread_mutex_t lock; 1219 ASSERT_EQ(0, pthread_mutex_init(&lock, &attr)); 1220 1221 ASSERT_EQ(0, pthread_mutex_lock(&lock)); 1222 ASSERT_EQ(0, pthread_mutex_lock(&lock)); 1223 ASSERT_EQ(0, pthread_mutex_unlock(&lock)); 1224 ASSERT_EQ(0, pthread_mutex_unlock(&lock)); 1225 ASSERT_EQ(0, pthread_mutex_trylock(&lock)); 1226 ASSERT_EQ(0, pthread_mutex_unlock(&lock)); 1227 ASSERT_EQ(EPERM, pthread_mutex_unlock(&lock)); 1228 ASSERT_EQ(0, pthread_mutex_destroy(&lock)); 1229} 1230 1231TEST(pthread, pthread_mutex_owner_tid_limit) { 1232 FILE* fp = fopen("/proc/sys/kernel/pid_max", "r"); 1233 ASSERT_TRUE(fp != NULL); 1234 long pid_max; 1235 ASSERT_EQ(1, fscanf(fp, "%ld", &pid_max)); 1236 fclose(fp); 1237 // Current pthread_mutex uses 16 bits to represent owner tid. 1238 // Change the implementation if we need to support higher value than 65535. 1239 ASSERT_LE(pid_max, 65536); 1240} 1241