pthread_test.cpp revision 8fb639ca9118a6522723d0bc09db59b432a803a9
1bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes/*
2bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes * Copyright (C) 2012 The Android Open Source Project
3bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes *
4bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
5bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes * you may not use this file except in compliance with the License.
6bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes * You may obtain a copy of the License at
7bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes *
8bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
9bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes *
10bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes * Unless required by applicable law or agreed to in writing, software
11bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
12bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes * See the License for the specific language governing permissions and
14bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes * limitations under the License.
15bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes */
16bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes
17bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes#include <gtest/gtest.h>
18bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes
19bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes#include <errno.h>
205b9310e502003e584bcb3a028ca3db7aa4d3f01bElliott Hughes#include <inttypes.h>
21b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes#include <limits.h>
2204620a3cd7bdea0d1b421c8772ba3f06839bbe9cElliott Hughes#include <malloc.h>
23bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes#include <pthread.h>
24f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#include <signal.h>
2570b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes#include <sys/mman.h>
2657b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes#include <sys/syscall.h>
2751e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath#include <time.h>
284d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes#include <unistd.h>
29bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes
3027a9aed81978af792cb06035a1619c8141a5fb5bElliott Hughes#include "private/ScopeGuard.h"
314b558f50a42c97d461f1dede5aaaae490ea99e2eElliott Hughes#include "ScopedSignalHandler.h"
324b558f50a42c97d461f1dede5aaaae490ea99e2eElliott Hughes
33bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott HughesTEST(pthread, pthread_key_create) {
34bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes  pthread_key_t key;
35bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes  ASSERT_EQ(0, pthread_key_create(&key, NULL));
36bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes  ASSERT_EQ(0, pthread_key_delete(key));
37bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes  // Can't delete a key that's already been deleted.
38bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes  ASSERT_EQ(EINVAL, pthread_key_delete(key));
39bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes}
404d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes
4144b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott HughesTEST(pthread, pthread_key_create_lots) {
42f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#if defined(__BIONIC__) // glibc uses keys internally that its sysconf value doesn't account for.
431887621de8a48eece8a05f2400ddd783b9833147Elliott Hughes  // POSIX says PTHREAD_KEYS_MAX should be at least 128.
441887621de8a48eece8a05f2400ddd783b9833147Elliott Hughes  ASSERT_GE(PTHREAD_KEYS_MAX, 128);
45718a5b5495ae7726aabd2f8a748da9f391d12b98Elliott Hughes
46718a5b5495ae7726aabd2f8a748da9f391d12b98Elliott Hughes  int sysconf_max = sysconf(_SC_THREAD_KEYS_MAX);
47718a5b5495ae7726aabd2f8a748da9f391d12b98Elliott Hughes
481887621de8a48eece8a05f2400ddd783b9833147Elliott Hughes  // sysconf shouldn't return a smaller value.
49718a5b5495ae7726aabd2f8a748da9f391d12b98Elliott Hughes  ASSERT_GE(sysconf_max, PTHREAD_KEYS_MAX);
501887621de8a48eece8a05f2400ddd783b9833147Elliott Hughes
5144b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes  // We can allocate _SC_THREAD_KEYS_MAX keys.
52718a5b5495ae7726aabd2f8a748da9f391d12b98Elliott Hughes  sysconf_max -= 2; // (Except that gtest takes two for itself.)
5344b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes  std::vector<pthread_key_t> keys;
54718a5b5495ae7726aabd2f8a748da9f391d12b98Elliott Hughes  for (int i = 0; i < sysconf_max; ++i) {
5544b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes    pthread_key_t key;
563e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes    // If this fails, it's likely that GLOBAL_INIT_THREAD_LOCAL_BUFFER_COUNT is wrong.
57718a5b5495ae7726aabd2f8a748da9f391d12b98Elliott Hughes    ASSERT_EQ(0, pthread_key_create(&key, NULL)) << i << " of " << sysconf_max;
5844b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes    keys.push_back(key);
5944b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes  }
6044b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes
6144b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes  // ...and that really is the maximum.
6244b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes  pthread_key_t key;
6344b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes  ASSERT_EQ(EAGAIN, pthread_key_create(&key, NULL));
6444b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes
6544b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes  // (Don't leak all those keys!)
6644b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes  for (size_t i = 0; i < keys.size(); ++i) {
6744b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes    ASSERT_EQ(0, pthread_key_delete(keys[i]));
6844b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes  }
69f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#else // __BIONIC__
70f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris  GTEST_LOG_(INFO) << "This test does nothing.\n";
71f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#endif // __BIONIC__
7244b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes}
7344b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes
74ebb770f90d9a8d7f75a9d8b0e6a96ded96c617afElliott HughesTEST(pthread, pthread_key_delete) {
75ebb770f90d9a8d7f75a9d8b0e6a96ded96c617afElliott Hughes  void* expected = reinterpret_cast<void*>(1234);
76ebb770f90d9a8d7f75a9d8b0e6a96ded96c617afElliott Hughes  pthread_key_t key;
77ebb770f90d9a8d7f75a9d8b0e6a96ded96c617afElliott Hughes  ASSERT_EQ(0, pthread_key_create(&key, NULL));
78ebb770f90d9a8d7f75a9d8b0e6a96ded96c617afElliott Hughes  ASSERT_EQ(0, pthread_setspecific(key, expected));
79ebb770f90d9a8d7f75a9d8b0e6a96ded96c617afElliott Hughes  ASSERT_EQ(expected, pthread_getspecific(key));
80ebb770f90d9a8d7f75a9d8b0e6a96ded96c617afElliott Hughes  ASSERT_EQ(0, pthread_key_delete(key));
81ebb770f90d9a8d7f75a9d8b0e6a96ded96c617afElliott Hughes  // After deletion, pthread_getspecific returns NULL.
82ebb770f90d9a8d7f75a9d8b0e6a96ded96c617afElliott Hughes  ASSERT_EQ(NULL, pthread_getspecific(key));
83ebb770f90d9a8d7f75a9d8b0e6a96ded96c617afElliott Hughes  // And you can't use pthread_setspecific with the deleted key.
84ebb770f90d9a8d7f75a9d8b0e6a96ded96c617afElliott Hughes  ASSERT_EQ(EINVAL, pthread_setspecific(key, expected));
85ebb770f90d9a8d7f75a9d8b0e6a96ded96c617afElliott Hughes}
86ebb770f90d9a8d7f75a9d8b0e6a96ded96c617afElliott Hughes
8740a521744825b6060960c296d5fb3da4c6593d94Elliott HughesTEST(pthread, pthread_key_fork) {
8840a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  void* expected = reinterpret_cast<void*>(1234);
8940a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  pthread_key_t key;
9040a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  ASSERT_EQ(0, pthread_key_create(&key, NULL));
9140a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  ASSERT_EQ(0, pthread_setspecific(key, expected));
9240a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  ASSERT_EQ(expected, pthread_getspecific(key));
9340a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes
9440a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  pid_t pid = fork();
9540a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  ASSERT_NE(-1, pid) << strerror(errno);
9640a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes
9740a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  if (pid == 0) {
9840a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes    // The surviving thread inherits all the forking thread's TLS values...
9940a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes    ASSERT_EQ(expected, pthread_getspecific(key));
10040a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes    _exit(99);
10140a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  }
10240a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes
10340a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  int status;
10440a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  ASSERT_EQ(pid, waitpid(pid, &status, 0));
10540a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  ASSERT_TRUE(WIFEXITED(status));
10640a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  ASSERT_EQ(99, WEXITSTATUS(status));
10740a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes
10840a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  ASSERT_EQ(expected, pthread_getspecific(key));
1091d53ae2a01df5c85d23b01e44880103e118712f3Dan Albert  ASSERT_EQ(0, pthread_key_delete(key));
11040a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes}
11140a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes
11240a521744825b6060960c296d5fb3da4c6593d94Elliott Hughesstatic void* DirtyKeyFn(void* key) {
11340a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  return pthread_getspecific(*reinterpret_cast<pthread_key_t*>(key));
11440a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes}
11540a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes
11640a521744825b6060960c296d5fb3da4c6593d94Elliott HughesTEST(pthread, pthread_key_dirty) {
11740a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  pthread_key_t key;
11840a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  ASSERT_EQ(0, pthread_key_create(&key, NULL));
11940a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes
12040a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  size_t stack_size = 128 * 1024;
12140a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
12240a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  ASSERT_NE(MAP_FAILED, stack);
12340a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  memset(stack, 0xff, stack_size);
12440a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes
12540a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  pthread_attr_t attr;
12640a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  ASSERT_EQ(0, pthread_attr_init(&attr));
12740a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  ASSERT_EQ(0, pthread_attr_setstack(&attr, stack, stack_size));
12840a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes
12940a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  pthread_t t;
13040a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  ASSERT_EQ(0, pthread_create(&t, &attr, DirtyKeyFn, &key));
13140a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes
13240a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  void* result;
13340a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  ASSERT_EQ(0, pthread_join(t, &result));
13440a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  ASSERT_EQ(nullptr, result); // Not ~0!
13540a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes
13640a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  ASSERT_EQ(0, munmap(stack, stack_size));
1371d53ae2a01df5c85d23b01e44880103e118712f3Dan Albert  ASSERT_EQ(0, pthread_key_delete(key));
13840a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes}
13940a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes
1404d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughesstatic void* IdFn(void* arg) {
1414d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  return arg;
1424d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes}
1434d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes
1444d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughesstatic void* SleepFn(void* arg) {
1455b9310e502003e584bcb3a028ca3db7aa4d3f01bElliott Hughes  sleep(reinterpret_cast<uintptr_t>(arg));
1464d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  return NULL;
1474d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes}
1484d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes
14910ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikovstatic void* SpinFn(void* arg) {
15010ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  volatile bool* b = reinterpret_cast<volatile bool*>(arg);
15110ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  while (!*b) {
15210ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  }
15310ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  return NULL;
15410ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov}
15510ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov
1564d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughesstatic void* JoinFn(void* arg) {
1574d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  return reinterpret_cast<void*>(pthread_join(reinterpret_cast<pthread_t>(arg), NULL));
1584d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes}
1594d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes
16010ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikovstatic void AssertDetached(pthread_t t, bool is_detached) {
16110ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  pthread_attr_t attr;
16210ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  ASSERT_EQ(0, pthread_getattr_np(t, &attr));
16310ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  int detach_state;
16410ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &detach_state));
16510ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  pthread_attr_destroy(&attr);
16610ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  ASSERT_EQ(is_detached, (detach_state == PTHREAD_CREATE_DETACHED));
16710ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov}
16810ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov
1699d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughesstatic void MakeDeadThread(pthread_t& t) {
1709d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, NULL));
1719d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  void* result;
1729d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  ASSERT_EQ(0, pthread_join(t, &result));
1739d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes}
1749d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes
1754d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott HughesTEST(pthread, pthread_create) {
1764d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  void* expected_result = reinterpret_cast<void*>(123);
1774d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  // Can we create a thread?
1784d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  pthread_t t;
1794d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, expected_result));
1804d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  // If we join, do we get the expected value back?
1814d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  void* result;
1824d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  ASSERT_EQ(0, pthread_join(t, &result));
1834d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  ASSERT_EQ(expected_result, result);
1844d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes}
1854d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes
1863e898476c7230b60a0f76968e64ff25f475b48c0Elliott HughesTEST(pthread, pthread_create_EAGAIN) {
1873e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes  pthread_attr_t attributes;
1883e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes  ASSERT_EQ(0, pthread_attr_init(&attributes));
1893e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes  ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, static_cast<size_t>(-1) & ~(getpagesize() - 1)));
1903e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes
1913e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes  pthread_t t;
1923e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes  ASSERT_EQ(EAGAIN, pthread_create(&t, &attributes, IdFn, NULL));
1933e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes}
1943e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes
1954d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott HughesTEST(pthread, pthread_no_join_after_detach) {
1964d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  pthread_t t1;
1974d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  ASSERT_EQ(0, pthread_create(&t1, NULL, SleepFn, reinterpret_cast<void*>(5)));
1984d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes
1994d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  // After a pthread_detach...
2004d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  ASSERT_EQ(0, pthread_detach(t1));
20110ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  AssertDetached(t1, true);
2024d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes
2034d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  // ...pthread_join should fail.
2044d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  void* result;
2054d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  ASSERT_EQ(EINVAL, pthread_join(t1, &result));
2064d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes}
2074d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes
2084d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott HughesTEST(pthread, pthread_no_op_detach_after_join) {
20910ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  bool done = false;
21010ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov
2114d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  pthread_t t1;
21210ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  ASSERT_EQ(0, pthread_create(&t1, NULL, SpinFn, &done));
2134d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes
2144d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  // If thread 2 is already waiting to join thread 1...
2154d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  pthread_t t2;
2164d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
2174d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes
21810ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  sleep(1); // (Give t2 a chance to call pthread_join.)
21910ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov
22010ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  // ...a call to pthread_detach on thread 1 will "succeed" (silently fail)...
2214d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  ASSERT_EQ(0, pthread_detach(t1));
22210ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  AssertDetached(t1, false);
22310ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov
22410ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  done = true;
2254d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes
22610ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes).
2274d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  void* join_result;
2284d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  ASSERT_EQ(0, pthread_join(t2, &join_result));
2295b9310e502003e584bcb3a028ca3db7aa4d3f01bElliott Hughes  ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
2304d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes}
23114f19592ae7c819855052bcebc79de87069c2954Elliott Hughes
23214f19592ae7c819855052bcebc79de87069c2954Elliott HughesTEST(pthread, pthread_join_self) {
23314f19592ae7c819855052bcebc79de87069c2954Elliott Hughes  void* result;
23414f19592ae7c819855052bcebc79de87069c2954Elliott Hughes  ASSERT_EQ(EDEADLK, pthread_join(pthread_self(), &result));
23514f19592ae7c819855052bcebc79de87069c2954Elliott Hughes}
2364f251bee5d51228217c1bf4dfc9219f3058bd3edElliott Hughes
237877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughesstruct TestBug37410 {
238877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes  pthread_t main_thread;
239877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes  pthread_mutex_t mutex;
2404f251bee5d51228217c1bf4dfc9219f3058bd3edElliott Hughes
241877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes  static void main() {
242877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    TestBug37410 data;
243877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    data.main_thread = pthread_self();
244877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    ASSERT_EQ(0, pthread_mutex_init(&data.mutex, NULL));
245877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
246877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes
247877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    pthread_t t;
248877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    ASSERT_EQ(0, pthread_create(&t, NULL, TestBug37410::thread_fn, reinterpret_cast<void*>(&data)));
249877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes
250877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    // Wait for the thread to be running...
251877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
252877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    ASSERT_EQ(0, pthread_mutex_unlock(&data.mutex));
253877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes
254877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    // ...and exit.
255877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    pthread_exit(NULL);
256877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes  }
257877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes
258877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes private:
259877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes  static void* thread_fn(void* arg) {
260877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    TestBug37410* data = reinterpret_cast<TestBug37410*>(arg);
261877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes
262877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    // Let the main thread know we're running.
263877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    pthread_mutex_unlock(&data->mutex);
264877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes
265877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    // And wait for the main thread to exit.
266877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    pthread_join(data->main_thread, NULL);
267877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes
268877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    return NULL;
269877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes  }
270877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes};
2714f251bee5d51228217c1bf4dfc9219f3058bd3edElliott Hughes
2727fd803cdfa873c01138dcbd614ec92418169b1c2Elliott Hughes// Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to
2737fd803cdfa873c01138dcbd614ec92418169b1c2Elliott Hughes// run this test (which exits normally) in its own process.
2744f251bee5d51228217c1bf4dfc9219f3058bd3edElliott HughesTEST(pthread_DeathTest, pthread_bug_37410) {
2754f251bee5d51228217c1bf4dfc9219f3058bd3edElliott Hughes  // http://code.google.com/p/android/issues/detail?id=37410
2764f251bee5d51228217c1bf4dfc9219f3058bd3edElliott Hughes  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
277877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes  ASSERT_EXIT(TestBug37410::main(), ::testing::ExitedWithCode(0), "");
2784f251bee5d51228217c1bf4dfc9219f3058bd3edElliott Hughes}
279c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes
280c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughesstatic void* SignalHandlerFn(void* arg) {
281c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  sigset_t wait_set;
282c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  sigfillset(&wait_set);
283c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  return reinterpret_cast<void*>(sigwait(&wait_set, reinterpret_cast<int*>(arg)));
284c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes}
285c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes
286c5d028fc913de84a781bd61084bf7ae2182fd48eElliott HughesTEST(pthread, pthread_sigmask) {
28719e62325c268a668692e2b65fde2284079f369aaElliott Hughes  // Check that SIGUSR1 isn't blocked.
28819e62325c268a668692e2b65fde2284079f369aaElliott Hughes  sigset_t original_set;
28919e62325c268a668692e2b65fde2284079f369aaElliott Hughes  sigemptyset(&original_set);
29019e62325c268a668692e2b65fde2284079f369aaElliott Hughes  ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &original_set));
29119e62325c268a668692e2b65fde2284079f369aaElliott Hughes  ASSERT_FALSE(sigismember(&original_set, SIGUSR1));
29219e62325c268a668692e2b65fde2284079f369aaElliott Hughes
293c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  // Block SIGUSR1.
294c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  sigset_t set;
295c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  sigemptyset(&set);
296c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  sigaddset(&set, SIGUSR1);
297c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &set, NULL));
298c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes
29919e62325c268a668692e2b65fde2284079f369aaElliott Hughes  // Check that SIGUSR1 is blocked.
30019e62325c268a668692e2b65fde2284079f369aaElliott Hughes  sigset_t final_set;
30119e62325c268a668692e2b65fde2284079f369aaElliott Hughes  sigemptyset(&final_set);
30219e62325c268a668692e2b65fde2284079f369aaElliott Hughes  ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &final_set));
30319e62325c268a668692e2b65fde2284079f369aaElliott Hughes  ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
30419e62325c268a668692e2b65fde2284079f369aaElliott Hughes  // ...and that sigprocmask agrees with pthread_sigmask.
30519e62325c268a668692e2b65fde2284079f369aaElliott Hughes  sigemptyset(&final_set);
30619e62325c268a668692e2b65fde2284079f369aaElliott Hughes  ASSERT_EQ(0, sigprocmask(SIG_BLOCK, NULL, &final_set));
30719e62325c268a668692e2b65fde2284079f369aaElliott Hughes  ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
30819e62325c268a668692e2b65fde2284079f369aaElliott Hughes
309c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  // Spawn a thread that calls sigwait and tells us what it received.
310c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  pthread_t signal_thread;
311c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  int received_signal = -1;
312c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  ASSERT_EQ(0, pthread_create(&signal_thread, NULL, SignalHandlerFn, &received_signal));
313c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes
314c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  // Send that thread SIGUSR1.
315c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  pthread_kill(signal_thread, SIGUSR1);
316c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes
317c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  // See what it got.
318c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  void* join_result;
319c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  ASSERT_EQ(0, pthread_join(signal_thread, &join_result));
320c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  ASSERT_EQ(SIGUSR1, received_signal);
3215b9310e502003e584bcb3a028ca3db7aa4d3f01bElliott Hughes  ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
32219e62325c268a668692e2b65fde2284079f369aaElliott Hughes
32319e62325c268a668692e2b65fde2284079f369aaElliott Hughes  // Restore the original signal mask.
32419e62325c268a668692e2b65fde2284079f369aaElliott Hughes  ASSERT_EQ(0, pthread_sigmask(SIG_SETMASK, &original_set, NULL));
325c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes}
3265e3fc43ddeada547a155c6f561a12ff0b16e02d3Elliott Hughes
3273e898476c7230b60a0f76968e64ff25f475b48c0Elliott HughesTEST(pthread, pthread_setname_np__too_long) {
328f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#if defined(__BIONIC__) // Not all build servers have a new enough glibc? TODO: remove when they're on gprecise.
3293e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes  ASSERT_EQ(ERANGE, pthread_setname_np(pthread_self(), "this name is far too long for linux"));
330f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#else // __BIONIC__
331f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris  GTEST_LOG_(INFO) << "This test does nothing.\n";
332f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#endif // __BIONIC__
3333e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes}
3343e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes
3353e898476c7230b60a0f76968e64ff25f475b48c0Elliott HughesTEST(pthread, pthread_setname_np__self) {
336f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#if defined(__BIONIC__) // Not all build servers have a new enough glibc? TODO: remove when they're on gprecise.
3373e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes  ASSERT_EQ(0, pthread_setname_np(pthread_self(), "short 1"));
338f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#else // __BIONIC__
339f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris  GTEST_LOG_(INFO) << "This test does nothing.\n";
340f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#endif // __BIONIC__
3413e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes}
3423e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes
3433e898476c7230b60a0f76968e64ff25f475b48c0Elliott HughesTEST(pthread, pthread_setname_np__other) {
344f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#if defined(__BIONIC__) // Not all build servers have a new enough glibc? TODO: remove when they're on gprecise.
34540eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes  // Emulator kernels don't currently support setting the name of other threads.
34640eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes  char* filename = NULL;
34740eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes  asprintf(&filename, "/proc/self/task/%d/comm", gettid());
34840eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes  struct stat sb;
34940eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes  bool has_comm = (stat(filename, &sb) != -1);
35040eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes  free(filename);
35140eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes
35240eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes  if (has_comm) {
35340eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes    pthread_t t1;
35440eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes    ASSERT_EQ(0, pthread_create(&t1, NULL, SleepFn, reinterpret_cast<void*>(5)));
35540eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes    ASSERT_EQ(0, pthread_setname_np(t1, "short 2"));
35640eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes  } else {
35740eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes    fprintf(stderr, "skipping test: this kernel doesn't have /proc/self/task/tid/comm files!\n");
35840eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes  }
359f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#else // __BIONIC__
360f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris  GTEST_LOG_(INFO) << "This test does nothing.\n";
361f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#endif // __BIONIC__
3623e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes}
3633e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes
3643e898476c7230b60a0f76968e64ff25f475b48c0Elliott HughesTEST(pthread, pthread_setname_np__no_such_thread) {
365f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#if defined(__BIONIC__) // Not all build servers have a new enough glibc? TODO: remove when they're on gprecise.
3669d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  pthread_t dead_thread;
3679d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  MakeDeadThread(dead_thread);
3683e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes
3693e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes  // Call pthread_setname_np after thread has already exited.
370a41ba2f0bfc4fce1ce8f06a9c289102c440c929dElliott Hughes  ASSERT_EQ(ESRCH, pthread_setname_np(dead_thread, "short 3"));
371f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#else // __BIONIC__
372f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris  GTEST_LOG_(INFO) << "This test does nothing.\n";
373f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#endif // __BIONIC__
3743e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes}
3759d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes
3769d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott HughesTEST(pthread, pthread_kill__0) {
3779d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  // Signal 0 just tests that the thread exists, so it's safe to call on ourselves.
3789d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  ASSERT_EQ(0, pthread_kill(pthread_self(), 0));
3799d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes}
3809d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes
3819d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott HughesTEST(pthread, pthread_kill__invalid_signal) {
3829d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  ASSERT_EQ(EINVAL, pthread_kill(pthread_self(), -1));
3839d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes}
3849d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes
385fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughesstatic void pthread_kill__in_signal_handler_helper(int signal_number) {
386fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes  static int count = 0;
387fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes  ASSERT_EQ(SIGALRM, signal_number);
388fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes  if (++count == 1) {
389fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes    // Can we call pthread_kill from a signal handler?
390fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes    ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
391fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes  }
392fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes}
393fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes
394fae89fc4042ee4c360842234dfda7831c313bd44Elliott HughesTEST(pthread, pthread_kill__in_signal_handler) {
3954b558f50a42c97d461f1dede5aaaae490ea99e2eElliott Hughes  ScopedSignalHandler ssh(SIGALRM, pthread_kill__in_signal_handler_helper);
396fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes  ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
397fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes}
398fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes
3999d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott HughesTEST(pthread, pthread_detach__no_such_thread) {
4009d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  pthread_t dead_thread;
4019d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  MakeDeadThread(dead_thread);
4029d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes
4039d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  ASSERT_EQ(ESRCH, pthread_detach(dead_thread));
4049d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes}
4059d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes
40604620a3cd7bdea0d1b421c8772ba3f06839bbe9cElliott HughesTEST(pthread, pthread_detach__leak) {
407e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris  size_t initial_bytes = 0;
408e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris  // Run this loop more than once since the first loop causes some memory
409e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris  // to be allocated permenantly. Run an extra loop to help catch any subtle
410e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris  // memory leaks.
411e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris  for (size_t loop = 0; loop < 3; loop++) {
412e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris    // Set the initial bytes on the second loop since the memory in use
413e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris    // should have stabilized.
414e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris    if (loop == 1) {
415e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris      initial_bytes = mallinfo().uordblks;
416e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris    }
417e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris
418e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris    pthread_attr_t attr;
419e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris    ASSERT_EQ(0, pthread_attr_init(&attr));
420e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris    ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE));
421e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris
422e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris    std::vector<pthread_t> threads;
423e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris    for (size_t i = 0; i < 32; ++i) {
424e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris      pthread_t t;
425e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris      ASSERT_EQ(0, pthread_create(&t, &attr, IdFn, NULL));
426e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris      threads.push_back(t);
427e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris    }
428e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris
429e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris    sleep(1);
430e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris
431e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris    for (size_t i = 0; i < 32; ++i) {
432e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris      ASSERT_EQ(0, pthread_detach(threads[i])) << i;
433e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris    }
43404620a3cd7bdea0d1b421c8772ba3f06839bbe9cElliott Hughes  }
43504620a3cd7bdea0d1b421c8772ba3f06839bbe9cElliott Hughes
43604620a3cd7bdea0d1b421c8772ba3f06839bbe9cElliott Hughes  size_t final_bytes = mallinfo().uordblks;
43704620a3cd7bdea0d1b421c8772ba3f06839bbe9cElliott Hughes  int leaked_bytes = (final_bytes - initial_bytes);
43804620a3cd7bdea0d1b421c8772ba3f06839bbe9cElliott Hughes
43904620a3cd7bdea0d1b421c8772ba3f06839bbe9cElliott Hughes  // User code (like this test) doesn't know how large pthread_internal_t is.
44004620a3cd7bdea0d1b421c8772ba3f06839bbe9cElliott Hughes  // We can be pretty sure it's more than 128 bytes.
44104620a3cd7bdea0d1b421c8772ba3f06839bbe9cElliott Hughes  ASSERT_LT(leaked_bytes, 32 /*threads*/ * 128 /*bytes*/);
44204620a3cd7bdea0d1b421c8772ba3f06839bbe9cElliott Hughes}
44304620a3cd7bdea0d1b421c8772ba3f06839bbe9cElliott Hughes
4449b06cc3c1b2c4e2b08582f3fc9393a05aa589766Jeff HaoTEST(pthread, pthread_getcpuclockid__clock_gettime) {
4459b06cc3c1b2c4e2b08582f3fc9393a05aa589766Jeff Hao  pthread_t t;
4469b06cc3c1b2c4e2b08582f3fc9393a05aa589766Jeff Hao  ASSERT_EQ(0, pthread_create(&t, NULL, SleepFn, reinterpret_cast<void*>(5)));
4479b06cc3c1b2c4e2b08582f3fc9393a05aa589766Jeff Hao
4489b06cc3c1b2c4e2b08582f3fc9393a05aa589766Jeff Hao  clockid_t c;
4499b06cc3c1b2c4e2b08582f3fc9393a05aa589766Jeff Hao  ASSERT_EQ(0, pthread_getcpuclockid(t, &c));
4509b06cc3c1b2c4e2b08582f3fc9393a05aa589766Jeff Hao  timespec ts;
4519b06cc3c1b2c4e2b08582f3fc9393a05aa589766Jeff Hao  ASSERT_EQ(0, clock_gettime(c, &ts));
4529b06cc3c1b2c4e2b08582f3fc9393a05aa589766Jeff Hao}
4539b06cc3c1b2c4e2b08582f3fc9393a05aa589766Jeff Hao
4549d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott HughesTEST(pthread, pthread_getcpuclockid__no_such_thread) {
4559d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  pthread_t dead_thread;
4569d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  MakeDeadThread(dead_thread);
4579d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes
4589d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  clockid_t c;
4599d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  ASSERT_EQ(ESRCH, pthread_getcpuclockid(dead_thread, &c));
4609d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes}
4619d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes
4629d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott HughesTEST(pthread, pthread_getschedparam__no_such_thread) {
4639d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  pthread_t dead_thread;
4649d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  MakeDeadThread(dead_thread);
4659d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes
4669d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  int policy;
4679d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  sched_param param;
4689d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  ASSERT_EQ(ESRCH, pthread_getschedparam(dead_thread, &policy, &param));
4699d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes}
4709d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes
4719d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott HughesTEST(pthread, pthread_setschedparam__no_such_thread) {
4729d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  pthread_t dead_thread;
4739d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  MakeDeadThread(dead_thread);
4749d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes
4759d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  int policy = 0;
4769d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  sched_param param;
4779d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  ASSERT_EQ(ESRCH, pthread_setschedparam(dead_thread, policy, &param));
4789d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes}
4799d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes
4809d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott HughesTEST(pthread, pthread_join__no_such_thread) {
4819d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  pthread_t dead_thread;
4829d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  MakeDeadThread(dead_thread);
4839d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes
4849d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  void* result;
4859d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  ASSERT_EQ(ESRCH, pthread_join(dead_thread, &result));
4869d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes}
4879d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes
4889d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott HughesTEST(pthread, pthread_kill__no_such_thread) {
4899d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  pthread_t dead_thread;
4909d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  MakeDeadThread(dead_thread);
4919d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes
4929d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  ASSERT_EQ(ESRCH, pthread_kill(dead_thread, 0));
4939d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes}
4940f020d18b138e24b1fe34074808e07ac412f35a4msg
4950f020d18b138e24b1fe34074808e07ac412f35a4msgTEST(pthread, pthread_join__multijoin) {
4960f020d18b138e24b1fe34074808e07ac412f35a4msg  bool done = false;
4970f020d18b138e24b1fe34074808e07ac412f35a4msg
4980f020d18b138e24b1fe34074808e07ac412f35a4msg  pthread_t t1;
4990f020d18b138e24b1fe34074808e07ac412f35a4msg  ASSERT_EQ(0, pthread_create(&t1, NULL, SpinFn, &done));
5000f020d18b138e24b1fe34074808e07ac412f35a4msg
5010f020d18b138e24b1fe34074808e07ac412f35a4msg  pthread_t t2;
5020f020d18b138e24b1fe34074808e07ac412f35a4msg  ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
5030f020d18b138e24b1fe34074808e07ac412f35a4msg
5040f020d18b138e24b1fe34074808e07ac412f35a4msg  sleep(1); // (Give t2 a chance to call pthread_join.)
5050f020d18b138e24b1fe34074808e07ac412f35a4msg
5060f020d18b138e24b1fe34074808e07ac412f35a4msg  // Multiple joins to the same thread should fail.
5070f020d18b138e24b1fe34074808e07ac412f35a4msg  ASSERT_EQ(EINVAL, pthread_join(t1, NULL));
5080f020d18b138e24b1fe34074808e07ac412f35a4msg
5090f020d18b138e24b1fe34074808e07ac412f35a4msg  done = true;
5100f020d18b138e24b1fe34074808e07ac412f35a4msg
5110f020d18b138e24b1fe34074808e07ac412f35a4msg  // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes).
5120f020d18b138e24b1fe34074808e07ac412f35a4msg  void* join_result;
5130f020d18b138e24b1fe34074808e07ac412f35a4msg  ASSERT_EQ(0, pthread_join(t2, &join_result));
5145b9310e502003e584bcb3a028ca3db7aa4d3f01bElliott Hughes  ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
5150f020d18b138e24b1fe34074808e07ac412f35a4msg}
516b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes
51770b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott HughesTEST(pthread, pthread_join__race) {
51870b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes  // http://b/11693195 --- pthread_join could return before the thread had actually exited.
51970b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes  // If the joiner unmapped the thread's stack, that could lead to SIGSEGV in the thread.
52070b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes  for (size_t i = 0; i < 1024; ++i) {
52170b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes    size_t stack_size = 64*1024;
52270b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes    void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
52370b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes
52470b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes    pthread_attr_t a;
52570b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes    pthread_attr_init(&a);
52670b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes    pthread_attr_setstack(&a, stack, stack_size);
52770b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes
52870b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes    pthread_t t;
52970b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes    ASSERT_EQ(0, pthread_create(&t, &a, IdFn, NULL));
53070b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes    ASSERT_EQ(0, pthread_join(t, NULL));
53170b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes    ASSERT_EQ(0, munmap(stack, stack_size));
53270b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes  }
53370b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes}
53470b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes
535b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughesstatic void* GetActualGuardSizeFn(void* arg) {
536b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  pthread_attr_t attributes;
537b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  pthread_getattr_np(pthread_self(), &attributes);
538b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  pthread_attr_getguardsize(&attributes, reinterpret_cast<size_t*>(arg));
539b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  return NULL;
540b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes}
541b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes
542b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughesstatic size_t GetActualGuardSize(const pthread_attr_t& attributes) {
543b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  size_t result;
544b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  pthread_t t;
545b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  pthread_create(&t, &attributes, GetActualGuardSizeFn, &result);
546b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  void* join_result;
547b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  pthread_join(t, &join_result);
548b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  return result;
549b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes}
550b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes
551b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughesstatic void* GetActualStackSizeFn(void* arg) {
552b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  pthread_attr_t attributes;
553b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  pthread_getattr_np(pthread_self(), &attributes);
554b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  pthread_attr_getstacksize(&attributes, reinterpret_cast<size_t*>(arg));
555b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  return NULL;
556b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes}
557b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes
558b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughesstatic size_t GetActualStackSize(const pthread_attr_t& attributes) {
559b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  size_t result;
560b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  pthread_t t;
561b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  pthread_create(&t, &attributes, GetActualStackSizeFn, &result);
562b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  void* join_result;
563b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  pthread_join(t, &join_result);
564b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  return result;
565b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes}
566b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes
567b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott HughesTEST(pthread, pthread_attr_setguardsize) {
568b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  pthread_attr_t attributes;
569b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(0, pthread_attr_init(&attributes));
570b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes
571b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  // Get the default guard size.
572b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  size_t default_guard_size;
573b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &default_guard_size));
574b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes
575b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  // No such thing as too small: will be rounded up to one page by pthread_create.
576b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 128));
577b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  size_t guard_size;
578b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
579b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(128U, guard_size);
580b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(4096U, GetActualGuardSize(attributes));
581b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes
582b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  // Large enough and a multiple of the page size.
583b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024));
584b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
585b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(32*1024U, guard_size);
586b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes
587b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  // Large enough but not a multiple of the page size; will be rounded up by pthread_create.
588b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024 + 1));
589b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
590b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(32*1024U + 1, guard_size);
591b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes}
592b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes
593b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott HughesTEST(pthread, pthread_attr_setstacksize) {
594b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  pthread_attr_t attributes;
595b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(0, pthread_attr_init(&attributes));
596b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes
597b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  // Get the default stack size.
598b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  size_t default_stack_size;
599b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &default_stack_size));
600b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes
601b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  // Too small.
602b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(EINVAL, pthread_attr_setstacksize(&attributes, 128));
603b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  size_t stack_size;
604b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
605b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(default_stack_size, stack_size);
606b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_GE(GetActualStackSize(attributes), default_stack_size);
607b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes
608b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  // Large enough and a multiple of the page size.
609b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024));
610b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
611b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(32*1024U, stack_size);
612b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(GetActualStackSize(attributes), 32*1024U);
613b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes
614b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  // Large enough but not a multiple of the page size; will be rounded up by pthread_create.
615b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024 + 1));
616b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
617b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(32*1024U + 1, stack_size);
618f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#if defined(__BIONIC__)
619b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  // Bionic rounds up, which is what POSIX allows.
620b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(GetActualStackSize(attributes), (32 + 4)*1024U);
621f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#else // __BIONIC__
622b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  // glibc rounds down, in violation of POSIX. They document this in their BUGS section.
623b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(GetActualStackSize(attributes), 32*1024U);
624f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#endif // __BIONIC__
625b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes}
626c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
627c3f114037dbf028896310609fd28cf2b3da99c4dElliott HughesTEST(pthread, pthread_rwlock_smoke) {
628c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  pthread_rwlock_t l;
629c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  ASSERT_EQ(0, pthread_rwlock_init(&l, NULL));
630c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
63176f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  // Single read lock
632c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
633c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  ASSERT_EQ(0, pthread_rwlock_unlock(&l));
634c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
63576f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  // Multiple read lock
63676f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
63776f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
63876f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  ASSERT_EQ(0, pthread_rwlock_unlock(&l));
63976f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  ASSERT_EQ(0, pthread_rwlock_unlock(&l));
64076f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle
64176f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  // Write lock
64292687e41bcf108957944dafa80a9bfda219bfb0fCalin Juravle  ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
64392687e41bcf108957944dafa80a9bfda219bfb0fCalin Juravle  ASSERT_EQ(0, pthread_rwlock_unlock(&l));
64476f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle
64576f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  // Try writer lock
64676f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  ASSERT_EQ(0, pthread_rwlock_trywrlock(&l));
64776f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l));
64876f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  ASSERT_EQ(EBUSY, pthread_rwlock_tryrdlock(&l));
64976f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  ASSERT_EQ(0, pthread_rwlock_unlock(&l));
65076f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle
65176f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  // Try reader lock
65276f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l));
65376f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l));
65476f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l));
65576f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  ASSERT_EQ(0, pthread_rwlock_unlock(&l));
65676f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  ASSERT_EQ(0, pthread_rwlock_unlock(&l));
65776f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle
65876f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  // Try writer lock after unlock
65976f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
66076f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  ASSERT_EQ(0, pthread_rwlock_unlock(&l));
66176f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle
66276f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle#ifdef __BIONIC__
66376f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  // EDEADLK in "read after write"
66476f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
66576f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  ASSERT_EQ(EDEADLK, pthread_rwlock_rdlock(&l));
66676f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  ASSERT_EQ(0, pthread_rwlock_unlock(&l));
66776f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle
66876f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  // EDEADLK in "write after write"
669c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
67076f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  ASSERT_EQ(EDEADLK, pthread_rwlock_wrlock(&l));
671c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  ASSERT_EQ(0, pthread_rwlock_unlock(&l));
67276f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle#endif
673c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
674c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  ASSERT_EQ(0, pthread_rwlock_destroy(&l));
675c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes}
676c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
6771728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic int g_once_fn_call_count = 0;
678c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughesstatic void OnceFn() {
6791728b2396591853345507a063ed6075dfd251706Elliott Hughes  ++g_once_fn_call_count;
680c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes}
681c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
682c3f114037dbf028896310609fd28cf2b3da99c4dElliott HughesTEST(pthread, pthread_once_smoke) {
683c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  pthread_once_t once_control = PTHREAD_ONCE_INIT;
684c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
685c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
6861728b2396591853345507a063ed6075dfd251706Elliott Hughes  ASSERT_EQ(1, g_once_fn_call_count);
687c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes}
688c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
6893694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughesstatic std::string pthread_once_1934122_result = "";
6903694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes
6913694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughesstatic void Routine2() {
6923694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes  pthread_once_1934122_result += "2";
6933694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes}
6943694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes
6953694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughesstatic void Routine1() {
6963694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes  pthread_once_t once_control_2 = PTHREAD_ONCE_INIT;
6973694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes  pthread_once_1934122_result += "1";
6983694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes  pthread_once(&once_control_2, &Routine2);
6993694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes}
7003694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes
7013694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott HughesTEST(pthread, pthread_once_1934122) {
7023694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes  // Very old versions of Android couldn't call pthread_once from a
7033694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes  // pthread_once init routine. http://b/1934122.
7043694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes  pthread_once_t once_control_1 = PTHREAD_ONCE_INIT;
7053694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes  ASSERT_EQ(0, pthread_once(&once_control_1, &Routine1));
7063694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes  ASSERT_EQ("12", pthread_once_1934122_result);
7073694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes}
7083694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes
7091728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic int g_atfork_prepare_calls = 0;
7101728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic void AtForkPrepare1() { g_atfork_prepare_calls = (g_atfork_prepare_calls << 4) | 1; }
7111728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic void AtForkPrepare2() { g_atfork_prepare_calls = (g_atfork_prepare_calls << 4) | 2; }
7121728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic int g_atfork_parent_calls = 0;
7131728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic void AtForkParent1() { g_atfork_parent_calls = (g_atfork_parent_calls << 4) | 1; }
7141728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic void AtForkParent2() { g_atfork_parent_calls = (g_atfork_parent_calls << 4) | 2; }
7151728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic int g_atfork_child_calls = 0;
7161728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic void AtForkChild1() { g_atfork_child_calls = (g_atfork_child_calls << 4) | 1; }
7171728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic void AtForkChild2() { g_atfork_child_calls = (g_atfork_child_calls << 4) | 2; }
718c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
719c3f114037dbf028896310609fd28cf2b3da99c4dElliott HughesTEST(pthread, pthread_atfork) {
720c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1));
721c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  ASSERT_EQ(0, pthread_atfork(AtForkPrepare2, AtForkParent2, AtForkChild2));
722c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
723c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  int pid = fork();
724c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  ASSERT_NE(-1, pid) << strerror(errno);
725c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
726c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  // Child and parent calls are made in the order they were registered.
727c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  if (pid == 0) {
7281728b2396591853345507a063ed6075dfd251706Elliott Hughes    ASSERT_EQ(0x12, g_atfork_child_calls);
729c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes    _exit(0);
730c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  }
7311728b2396591853345507a063ed6075dfd251706Elliott Hughes  ASSERT_EQ(0x12, g_atfork_parent_calls);
732c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
733c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  // Prepare calls are made in the reverse order.
7341728b2396591853345507a063ed6075dfd251706Elliott Hughes  ASSERT_EQ(0x21, g_atfork_prepare_calls);
735c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes}
736c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
737c3f114037dbf028896310609fd28cf2b3da99c4dElliott HughesTEST(pthread, pthread_attr_getscope) {
738c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  pthread_attr_t attr;
739c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  ASSERT_EQ(0, pthread_attr_init(&attr));
740c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
741c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  int scope;
742c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  ASSERT_EQ(0, pthread_attr_getscope(&attr, &scope));
743c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  ASSERT_EQ(PTHREAD_SCOPE_SYSTEM, scope);
744c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes}
74551e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath
74651e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan KamathTEST(pthread, pthread_condattr_init) {
74751e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  pthread_condattr_t attr;
74851e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  pthread_condattr_init(&attr);
74951e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath
75051e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  clockid_t clock;
75151e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
75251e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  ASSERT_EQ(CLOCK_REALTIME, clock);
75351e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath
75451e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  int pshared;
75551e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared));
75651e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  ASSERT_EQ(PTHREAD_PROCESS_PRIVATE, pshared);
75751e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath}
75851e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath
75951e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan KamathTEST(pthread, pthread_condattr_setclock) {
76051e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  pthread_condattr_t attr;
76151e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  pthread_condattr_init(&attr);
76251e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath
76351e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_REALTIME));
76451e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  clockid_t clock;
76551e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
76651e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  ASSERT_EQ(CLOCK_REALTIME, clock);
76751e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath
76851e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC));
76951e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
77051e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  ASSERT_EQ(CLOCK_MONOTONIC, clock);
77151e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath
77251e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  ASSERT_EQ(EINVAL, pthread_condattr_setclock(&attr, CLOCK_PROCESS_CPUTIME_ID));
77351e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath}
77451e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath
77551e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan KamathTEST(pthread, pthread_cond_broadcast__preserves_condattr_flags) {
77651e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath#if defined(__BIONIC__) // This tests a bionic implementation detail.
77751e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  pthread_condattr_t attr;
77851e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  pthread_condattr_init(&attr);
77951e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath
78051e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC));
78151e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  ASSERT_EQ(0, pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED));
78251e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath
78351e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  pthread_cond_t cond_var;
78451e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  ASSERT_EQ(0, pthread_cond_init(&cond_var, &attr));
78551e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath
78651e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  ASSERT_EQ(0, pthread_cond_signal(&cond_var));
78751e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  ASSERT_EQ(0, pthread_cond_broadcast(&cond_var));
78851e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath
78951e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  attr = static_cast<pthread_condattr_t>(cond_var.value);
79051e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  clockid_t clock;
79151e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
79251e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  ASSERT_EQ(CLOCK_MONOTONIC, clock);
79351e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  int pshared;
79451e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared));
79551e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  ASSERT_EQ(PTHREAD_PROCESS_SHARED, pshared);
79651e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath#else // __BIONIC__
79751e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  GTEST_LOG_(INFO) << "This test does nothing.\n";
79851e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath#endif // __BIONIC__
79951e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath}
8000e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes
8010e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott HughesTEST(pthread, pthread_mutex_timedlock) {
8020e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes  pthread_mutex_t m;
8030e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes  ASSERT_EQ(0, pthread_mutex_init(&m, NULL));
8040e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes
8050e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes  // If the mutex is already locked, pthread_mutex_timedlock should time out.
8060e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes  ASSERT_EQ(0, pthread_mutex_lock(&m));
8070e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes
8080e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes  timespec ts;
8090e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes  ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
8100e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes  ts.tv_nsec += 1;
8110e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes  ASSERT_EQ(ETIMEDOUT, pthread_mutex_timedlock(&m, &ts));
8120e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes
8130e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes  // If the mutex is unlocked, pthread_mutex_timedlock should succeed.
8140e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes  ASSERT_EQ(0, pthread_mutex_unlock(&m));
8150e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes
8160e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes  ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
8170e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes  ts.tv_nsec += 1;
8180e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes  ASSERT_EQ(0, pthread_mutex_timedlock(&m, &ts));
8190e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes
8200e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes  ASSERT_EQ(0, pthread_mutex_unlock(&m));
8210e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes  ASSERT_EQ(0, pthread_mutex_destroy(&m));
8220e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes}
82357b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes
82457b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott HughesTEST(pthread, pthread_attr_getstack__main_thread) {
82557b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  // This test is only meaningful for the main thread, so make sure we're running on it!
82657b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  ASSERT_EQ(getpid(), syscall(__NR_gettid));
82757b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes
82857b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  // Get the main thread's attributes.
82957b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  pthread_attr_t attributes;
83057b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
83157b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes
83257b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  // Check that we correctly report that the main thread has no guard page.
83357b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  size_t guard_size;
83457b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
83557b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  ASSERT_EQ(0U, guard_size); // The main thread has no guard page.
83657b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes
83757b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  // Get the stack base and the stack size (both ways).
83857b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  void* stack_base;
83957b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  size_t stack_size;
84057b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
84157b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  size_t stack_size2;
84257b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
84357b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes
84457b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  // The two methods of asking for the stack size should agree.
84557b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  EXPECT_EQ(stack_size, stack_size2);
84657b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes
84757b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  // What does /proc/self/maps' [stack] line say?
8489e4ffa7032eaab308876b8e3da86b05c3c613878Elliott Hughes  void* maps_stack_hi = NULL;
84957b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  FILE* fp = fopen("/proc/self/maps", "r");
85057b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  ASSERT_TRUE(fp != NULL);
85157b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  char line[BUFSIZ];
85257b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  while (fgets(line, sizeof(line), fp) != NULL) {
85357b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes    uintptr_t lo, hi;
85457b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes    char name[10];
85557b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes    sscanf(line, "%" PRIxPTR "-%" PRIxPTR " %*4s %*x %*x:%*x %*d %10s", &lo, &hi, name);
85657b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes    if (strcmp(name, "[stack]") == 0) {
8579e4ffa7032eaab308876b8e3da86b05c3c613878Elliott Hughes      maps_stack_hi = reinterpret_cast<void*>(hi);
85857b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes      break;
85957b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes    }
86057b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  }
86157b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  fclose(fp);
86257b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes
8639e4ffa7032eaab308876b8e3da86b05c3c613878Elliott Hughes  // The stack size should correspond to RLIMIT_STACK.
8649e4ffa7032eaab308876b8e3da86b05c3c613878Elliott Hughes  rlimit rl;
8659e4ffa7032eaab308876b8e3da86b05c3c613878Elliott Hughes  ASSERT_EQ(0, getrlimit(RLIMIT_STACK, &rl));
86627a9aed81978af792cb06035a1619c8141a5fb5bElliott Hughes  uint64_t original_rlim_cur = rl.rlim_cur;
86727a9aed81978af792cb06035a1619c8141a5fb5bElliott Hughes#if defined(__BIONIC__)
86827a9aed81978af792cb06035a1619c8141a5fb5bElliott Hughes  if (rl.rlim_cur == RLIM_INFINITY) {
86927a9aed81978af792cb06035a1619c8141a5fb5bElliott Hughes    rl.rlim_cur = 8 * 1024 * 1024; // Bionic reports unlimited stacks as 8MiB.
87027a9aed81978af792cb06035a1619c8141a5fb5bElliott Hughes  }
87127a9aed81978af792cb06035a1619c8141a5fb5bElliott Hughes#endif
8729e4ffa7032eaab308876b8e3da86b05c3c613878Elliott Hughes  EXPECT_EQ(rl.rlim_cur, stack_size);
87357b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes
874d9ff7226613014056c9edd79a68dc5af939107a0Dmitriy Ivanov  auto guard = make_scope_guard([&rl, original_rlim_cur]() {
87527a9aed81978af792cb06035a1619c8141a5fb5bElliott Hughes    rl.rlim_cur = original_rlim_cur;
87627a9aed81978af792cb06035a1619c8141a5fb5bElliott Hughes    ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
87727a9aed81978af792cb06035a1619c8141a5fb5bElliott Hughes  });
87827a9aed81978af792cb06035a1619c8141a5fb5bElliott Hughes
8799e4ffa7032eaab308876b8e3da86b05c3c613878Elliott Hughes  // The high address of the /proc/self/maps [stack] region should equal stack_base + stack_size.
8809e4ffa7032eaab308876b8e3da86b05c3c613878Elliott Hughes  // Remember that the stack grows down (and is mapped in on demand), so the low address of the
8819e4ffa7032eaab308876b8e3da86b05c3c613878Elliott Hughes  // region isn't very interesting.
8829e4ffa7032eaab308876b8e3da86b05c3c613878Elliott Hughes  EXPECT_EQ(maps_stack_hi, reinterpret_cast<uint8_t*>(stack_base) + stack_size);
88357b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes
88457b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  //
8859e4ffa7032eaab308876b8e3da86b05c3c613878Elliott Hughes  // What if RLIMIT_STACK is smaller than the stack's current extent?
88657b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  //
88757b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  rl.rlim_cur = rl.rlim_max = 1024; // 1KiB. We know the stack must be at least a page already.
88857b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  rl.rlim_max = RLIM_INFINITY;
88957b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
89057b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes
89157b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
89257b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
89357b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
89457b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes
89557b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  EXPECT_EQ(stack_size, stack_size2);
89657b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  ASSERT_EQ(1024U, stack_size);
89757b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes
89857b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  //
8999e4ffa7032eaab308876b8e3da86b05c3c613878Elliott Hughes  // What if RLIMIT_STACK isn't a whole number of pages?
90057b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  //
90157b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  rl.rlim_cur = rl.rlim_max = 6666; // Not a whole number of pages.
90257b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  rl.rlim_max = RLIM_INFINITY;
90357b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
90457b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes
90557b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
90657b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
90757b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
90857b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes
90957b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  EXPECT_EQ(stack_size, stack_size2);
91057b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  ASSERT_EQ(6666U, stack_size);
91157b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes}
9128fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughes
9138fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughes#if defined(__BIONIC__)
9148fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughesstatic void* pthread_gettid_np_helper(void* arg) {
9158fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughes  *reinterpret_cast<pid_t*>(arg) = gettid();
9168fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughes  return NULL;
9178fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughes}
9188fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughes#endif
9198fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughes
9208fb639ca9118a6522723d0bc09db59b432a803a9Elliott HughesTEST(pthread, pthread_gettid_np) {
9218fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughes#if defined(__BIONIC__)
9228fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughes  ASSERT_EQ(gettid(), pthread_gettid_np(pthread_self()));
9238fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughes
9248fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughes  pid_t t_gettid_result;
9258fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughes  pthread_t t;
9268fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughes  pthread_create(&t, NULL, pthread_gettid_np_helper, &t_gettid_result);
9278fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughes
9288fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughes  pid_t t_pthread_gettid_np_result = pthread_gettid_np(t);
9298fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughes
9308fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughes  void* join_result;
9318fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughes  pthread_join(t, &join_result);
9328fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughes
9338fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughes  ASSERT_EQ(t_gettid_result, t_pthread_gettid_np_result);
9348fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughes#else
9358fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughes  GTEST_LOG_(INFO) << "This test does nothing.\n";
9368fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughes#endif
9378fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughes}
938