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