pthread_test.cpp revision 1d53ae2a01df5c85d23b01e44880103e118712f3
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
304b558f50a42c97d461f1dede5aaaae490ea99e2eElliott Hughes#include "ScopedSignalHandler.h"
314b558f50a42c97d461f1dede5aaaae490ea99e2eElliott Hughes
32bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott HughesTEST(pthread, pthread_key_create) {
33bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes  pthread_key_t key;
34bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes  ASSERT_EQ(0, pthread_key_create(&key, NULL));
35bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes  ASSERT_EQ(0, pthread_key_delete(key));
36bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes  // Can't delete a key that's already been deleted.
37bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes  ASSERT_EQ(EINVAL, pthread_key_delete(key));
38bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes}
394d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes
4044b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott HughesTEST(pthread, pthread_key_create_lots) {
41f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#if defined(__BIONIC__) // glibc uses keys internally that its sysconf value doesn't account for.
421887621de8a48eece8a05f2400ddd783b9833147Elliott Hughes  // POSIX says PTHREAD_KEYS_MAX should be at least 128.
431887621de8a48eece8a05f2400ddd783b9833147Elliott Hughes  ASSERT_GE(PTHREAD_KEYS_MAX, 128);
44718a5b5495ae7726aabd2f8a748da9f391d12b98Elliott Hughes
45718a5b5495ae7726aabd2f8a748da9f391d12b98Elliott Hughes  int sysconf_max = sysconf(_SC_THREAD_KEYS_MAX);
46718a5b5495ae7726aabd2f8a748da9f391d12b98Elliott Hughes
471887621de8a48eece8a05f2400ddd783b9833147Elliott Hughes  // sysconf shouldn't return a smaller value.
48718a5b5495ae7726aabd2f8a748da9f391d12b98Elliott Hughes  ASSERT_GE(sysconf_max, PTHREAD_KEYS_MAX);
491887621de8a48eece8a05f2400ddd783b9833147Elliott Hughes
5044b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes  // We can allocate _SC_THREAD_KEYS_MAX keys.
51718a5b5495ae7726aabd2f8a748da9f391d12b98Elliott Hughes  sysconf_max -= 2; // (Except that gtest takes two for itself.)
5244b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes  std::vector<pthread_key_t> keys;
53718a5b5495ae7726aabd2f8a748da9f391d12b98Elliott Hughes  for (int i = 0; i < sysconf_max; ++i) {
5444b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes    pthread_key_t key;
553e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes    // If this fails, it's likely that GLOBAL_INIT_THREAD_LOCAL_BUFFER_COUNT is wrong.
56718a5b5495ae7726aabd2f8a748da9f391d12b98Elliott Hughes    ASSERT_EQ(0, pthread_key_create(&key, NULL)) << i << " of " << sysconf_max;
5744b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes    keys.push_back(key);
5844b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes  }
5944b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes
6044b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes  // ...and that really is the maximum.
6144b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes  pthread_key_t key;
6244b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes  ASSERT_EQ(EAGAIN, pthread_key_create(&key, NULL));
6344b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes
6444b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes  // (Don't leak all those keys!)
6544b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes  for (size_t i = 0; i < keys.size(); ++i) {
6644b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes    ASSERT_EQ(0, pthread_key_delete(keys[i]));
6744b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes  }
68f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#else // __BIONIC__
69f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris  GTEST_LOG_(INFO) << "This test does nothing.\n";
70f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#endif // __BIONIC__
7144b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes}
7244b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes
73ebb770f90d9a8d7f75a9d8b0e6a96ded96c617afElliott HughesTEST(pthread, pthread_key_delete) {
74ebb770f90d9a8d7f75a9d8b0e6a96ded96c617afElliott Hughes  void* expected = reinterpret_cast<void*>(1234);
75ebb770f90d9a8d7f75a9d8b0e6a96ded96c617afElliott Hughes  pthread_key_t key;
76ebb770f90d9a8d7f75a9d8b0e6a96ded96c617afElliott Hughes  ASSERT_EQ(0, pthread_key_create(&key, NULL));
77ebb770f90d9a8d7f75a9d8b0e6a96ded96c617afElliott Hughes  ASSERT_EQ(0, pthread_setspecific(key, expected));
78ebb770f90d9a8d7f75a9d8b0e6a96ded96c617afElliott Hughes  ASSERT_EQ(expected, pthread_getspecific(key));
79ebb770f90d9a8d7f75a9d8b0e6a96ded96c617afElliott Hughes  ASSERT_EQ(0, pthread_key_delete(key));
80ebb770f90d9a8d7f75a9d8b0e6a96ded96c617afElliott Hughes  // After deletion, pthread_getspecific returns NULL.
81ebb770f90d9a8d7f75a9d8b0e6a96ded96c617afElliott Hughes  ASSERT_EQ(NULL, pthread_getspecific(key));
82ebb770f90d9a8d7f75a9d8b0e6a96ded96c617afElliott Hughes  // And you can't use pthread_setspecific with the deleted key.
83ebb770f90d9a8d7f75a9d8b0e6a96ded96c617afElliott Hughes  ASSERT_EQ(EINVAL, pthread_setspecific(key, expected));
84ebb770f90d9a8d7f75a9d8b0e6a96ded96c617afElliott Hughes}
85ebb770f90d9a8d7f75a9d8b0e6a96ded96c617afElliott Hughes
8640a521744825b6060960c296d5fb3da4c6593d94Elliott HughesTEST(pthread, pthread_key_fork) {
8740a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  void* expected = reinterpret_cast<void*>(1234);
8840a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  pthread_key_t key;
8940a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  ASSERT_EQ(0, pthread_key_create(&key, NULL));
9040a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  ASSERT_EQ(0, pthread_setspecific(key, expected));
9140a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  ASSERT_EQ(expected, pthread_getspecific(key));
9240a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes
9340a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  pid_t pid = fork();
9440a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  ASSERT_NE(-1, pid) << strerror(errno);
9540a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes
9640a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  if (pid == 0) {
9740a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes    // The surviving thread inherits all the forking thread's TLS values...
9840a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes    ASSERT_EQ(expected, pthread_getspecific(key));
9940a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes    _exit(99);
10040a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  }
10140a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes
10240a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  int status;
10340a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  ASSERT_EQ(pid, waitpid(pid, &status, 0));
10440a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  ASSERT_TRUE(WIFEXITED(status));
10540a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  ASSERT_EQ(99, WEXITSTATUS(status));
10640a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes
10740a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  ASSERT_EQ(expected, pthread_getspecific(key));
1081d53ae2a01df5c85d23b01e44880103e118712f3Dan Albert  ASSERT_EQ(0, pthread_key_delete(key));
10940a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes}
11040a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes
11140a521744825b6060960c296d5fb3da4c6593d94Elliott Hughesstatic void* DirtyKeyFn(void* key) {
11240a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  return pthread_getspecific(*reinterpret_cast<pthread_key_t*>(key));
11340a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes}
11440a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes
11540a521744825b6060960c296d5fb3da4c6593d94Elliott HughesTEST(pthread, pthread_key_dirty) {
11640a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  pthread_key_t key;
11740a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  ASSERT_EQ(0, pthread_key_create(&key, NULL));
11840a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes
11940a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  size_t stack_size = 128 * 1024;
12040a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
12140a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  ASSERT_NE(MAP_FAILED, stack);
12240a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  memset(stack, 0xff, stack_size);
12340a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes
12440a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  pthread_attr_t attr;
12540a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  ASSERT_EQ(0, pthread_attr_init(&attr));
12640a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  ASSERT_EQ(0, pthread_attr_setstack(&attr, stack, stack_size));
12740a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes
12840a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  pthread_t t;
12940a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  ASSERT_EQ(0, pthread_create(&t, &attr, DirtyKeyFn, &key));
13040a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes
13140a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  void* result;
13240a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  ASSERT_EQ(0, pthread_join(t, &result));
13340a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  ASSERT_EQ(nullptr, result); // Not ~0!
13440a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes
13540a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes  ASSERT_EQ(0, munmap(stack, stack_size));
1361d53ae2a01df5c85d23b01e44880103e118712f3Dan Albert  ASSERT_EQ(0, pthread_key_delete(key));
13740a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes}
13840a521744825b6060960c296d5fb3da4c6593d94Elliott Hughes
1394d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughesstatic void* IdFn(void* arg) {
1404d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  return arg;
1414d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes}
1424d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes
1434d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughesstatic void* SleepFn(void* arg) {
1445b9310e502003e584bcb3a028ca3db7aa4d3f01bElliott Hughes  sleep(reinterpret_cast<uintptr_t>(arg));
1454d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  return NULL;
1464d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes}
1474d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes
14810ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikovstatic void* SpinFn(void* arg) {
14910ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  volatile bool* b = reinterpret_cast<volatile bool*>(arg);
15010ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  while (!*b) {
15110ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  }
15210ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  return NULL;
15310ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov}
15410ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov
1554d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughesstatic void* JoinFn(void* arg) {
1564d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  return reinterpret_cast<void*>(pthread_join(reinterpret_cast<pthread_t>(arg), NULL));
1574d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes}
1584d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes
15910ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikovstatic void AssertDetached(pthread_t t, bool is_detached) {
16010ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  pthread_attr_t attr;
16110ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  ASSERT_EQ(0, pthread_getattr_np(t, &attr));
16210ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  int detach_state;
16310ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &detach_state));
16410ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  pthread_attr_destroy(&attr);
16510ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  ASSERT_EQ(is_detached, (detach_state == PTHREAD_CREATE_DETACHED));
16610ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov}
16710ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov
1689d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughesstatic void MakeDeadThread(pthread_t& t) {
1699d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, NULL));
1709d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  void* result;
1719d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  ASSERT_EQ(0, pthread_join(t, &result));
1729d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes}
1739d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes
1744d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott HughesTEST(pthread, pthread_create) {
1754d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  void* expected_result = reinterpret_cast<void*>(123);
1764d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  // Can we create a thread?
1774d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  pthread_t t;
1784d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, expected_result));
1794d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  // If we join, do we get the expected value back?
1804d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  void* result;
1814d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  ASSERT_EQ(0, pthread_join(t, &result));
1824d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  ASSERT_EQ(expected_result, result);
1834d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes}
1844d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes
1853e898476c7230b60a0f76968e64ff25f475b48c0Elliott HughesTEST(pthread, pthread_create_EAGAIN) {
1863e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes  pthread_attr_t attributes;
1873e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes  ASSERT_EQ(0, pthread_attr_init(&attributes));
1883e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes  ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, static_cast<size_t>(-1) & ~(getpagesize() - 1)));
1893e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes
1903e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes  pthread_t t;
1913e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes  ASSERT_EQ(EAGAIN, pthread_create(&t, &attributes, IdFn, NULL));
1923e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes}
1933e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes
1944d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott HughesTEST(pthread, pthread_no_join_after_detach) {
1954d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  pthread_t t1;
1964d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  ASSERT_EQ(0, pthread_create(&t1, NULL, SleepFn, reinterpret_cast<void*>(5)));
1974d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes
1984d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  // After a pthread_detach...
1994d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  ASSERT_EQ(0, pthread_detach(t1));
20010ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  AssertDetached(t1, true);
2014d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes
2024d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  // ...pthread_join should fail.
2034d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  void* result;
2044d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  ASSERT_EQ(EINVAL, pthread_join(t1, &result));
2054d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes}
2064d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes
2074d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott HughesTEST(pthread, pthread_no_op_detach_after_join) {
20810ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  bool done = false;
20910ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov
2104d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  pthread_t t1;
21110ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  ASSERT_EQ(0, pthread_create(&t1, NULL, SpinFn, &done));
2124d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes
2134d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  // If thread 2 is already waiting to join thread 1...
2144d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  pthread_t t2;
2154d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
2164d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes
21710ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  sleep(1); // (Give t2 a chance to call pthread_join.)
21810ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov
21910ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  // ...a call to pthread_detach on thread 1 will "succeed" (silently fail)...
2204d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  ASSERT_EQ(0, pthread_detach(t1));
22110ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  AssertDetached(t1, false);
22210ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov
22310ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  done = true;
2244d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes
22510ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes).
2264d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  void* join_result;
2274d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  ASSERT_EQ(0, pthread_join(t2, &join_result));
2285b9310e502003e584bcb3a028ca3db7aa4d3f01bElliott Hughes  ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
2294d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes}
23014f19592ae7c819855052bcebc79de87069c2954Elliott Hughes
23114f19592ae7c819855052bcebc79de87069c2954Elliott HughesTEST(pthread, pthread_join_self) {
23214f19592ae7c819855052bcebc79de87069c2954Elliott Hughes  void* result;
23314f19592ae7c819855052bcebc79de87069c2954Elliott Hughes  ASSERT_EQ(EDEADLK, pthread_join(pthread_self(), &result));
23414f19592ae7c819855052bcebc79de87069c2954Elliott Hughes}
2354f251bee5d51228217c1bf4dfc9219f3058bd3edElliott Hughes
236877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughesstruct TestBug37410 {
237877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes  pthread_t main_thread;
238877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes  pthread_mutex_t mutex;
2394f251bee5d51228217c1bf4dfc9219f3058bd3edElliott Hughes
240877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes  static void main() {
241877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    TestBug37410 data;
242877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    data.main_thread = pthread_self();
243877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    ASSERT_EQ(0, pthread_mutex_init(&data.mutex, NULL));
244877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
245877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes
246877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    pthread_t t;
247877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    ASSERT_EQ(0, pthread_create(&t, NULL, TestBug37410::thread_fn, reinterpret_cast<void*>(&data)));
248877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes
249877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    // Wait for the thread to be running...
250877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
251877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    ASSERT_EQ(0, pthread_mutex_unlock(&data.mutex));
252877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes
253877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    // ...and exit.
254877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    pthread_exit(NULL);
255877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes  }
256877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes
257877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes private:
258877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes  static void* thread_fn(void* arg) {
259877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    TestBug37410* data = reinterpret_cast<TestBug37410*>(arg);
260877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes
261877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    // Let the main thread know we're running.
262877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    pthread_mutex_unlock(&data->mutex);
263877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes
264877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    // And wait for the main thread to exit.
265877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    pthread_join(data->main_thread, NULL);
266877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes
267877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    return NULL;
268877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes  }
269877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes};
2704f251bee5d51228217c1bf4dfc9219f3058bd3edElliott Hughes
2717fd803cdfa873c01138dcbd614ec92418169b1c2Elliott Hughes// Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to
2727fd803cdfa873c01138dcbd614ec92418169b1c2Elliott Hughes// run this test (which exits normally) in its own process.
2734f251bee5d51228217c1bf4dfc9219f3058bd3edElliott HughesTEST(pthread_DeathTest, pthread_bug_37410) {
2744f251bee5d51228217c1bf4dfc9219f3058bd3edElliott Hughes  // http://code.google.com/p/android/issues/detail?id=37410
2754f251bee5d51228217c1bf4dfc9219f3058bd3edElliott Hughes  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
276877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes  ASSERT_EXIT(TestBug37410::main(), ::testing::ExitedWithCode(0), "");
2774f251bee5d51228217c1bf4dfc9219f3058bd3edElliott Hughes}
278c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes
279c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughesstatic void* SignalHandlerFn(void* arg) {
280c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  sigset_t wait_set;
281c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  sigfillset(&wait_set);
282c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  return reinterpret_cast<void*>(sigwait(&wait_set, reinterpret_cast<int*>(arg)));
283c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes}
284c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes
285c5d028fc913de84a781bd61084bf7ae2182fd48eElliott HughesTEST(pthread, pthread_sigmask) {
28619e62325c268a668692e2b65fde2284079f369aaElliott Hughes  // Check that SIGUSR1 isn't blocked.
28719e62325c268a668692e2b65fde2284079f369aaElliott Hughes  sigset_t original_set;
28819e62325c268a668692e2b65fde2284079f369aaElliott Hughes  sigemptyset(&original_set);
28919e62325c268a668692e2b65fde2284079f369aaElliott Hughes  ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &original_set));
29019e62325c268a668692e2b65fde2284079f369aaElliott Hughes  ASSERT_FALSE(sigismember(&original_set, SIGUSR1));
29119e62325c268a668692e2b65fde2284079f369aaElliott Hughes
292c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  // Block SIGUSR1.
293c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  sigset_t set;
294c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  sigemptyset(&set);
295c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  sigaddset(&set, SIGUSR1);
296c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &set, NULL));
297c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes
29819e62325c268a668692e2b65fde2284079f369aaElliott Hughes  // Check that SIGUSR1 is blocked.
29919e62325c268a668692e2b65fde2284079f369aaElliott Hughes  sigset_t final_set;
30019e62325c268a668692e2b65fde2284079f369aaElliott Hughes  sigemptyset(&final_set);
30119e62325c268a668692e2b65fde2284079f369aaElliott Hughes  ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &final_set));
30219e62325c268a668692e2b65fde2284079f369aaElliott Hughes  ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
30319e62325c268a668692e2b65fde2284079f369aaElliott Hughes  // ...and that sigprocmask agrees with pthread_sigmask.
30419e62325c268a668692e2b65fde2284079f369aaElliott Hughes  sigemptyset(&final_set);
30519e62325c268a668692e2b65fde2284079f369aaElliott Hughes  ASSERT_EQ(0, sigprocmask(SIG_BLOCK, NULL, &final_set));
30619e62325c268a668692e2b65fde2284079f369aaElliott Hughes  ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
30719e62325c268a668692e2b65fde2284079f369aaElliott Hughes
308c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  // Spawn a thread that calls sigwait and tells us what it received.
309c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  pthread_t signal_thread;
310c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  int received_signal = -1;
311c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  ASSERT_EQ(0, pthread_create(&signal_thread, NULL, SignalHandlerFn, &received_signal));
312c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes
313c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  // Send that thread SIGUSR1.
314c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  pthread_kill(signal_thread, SIGUSR1);
315c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes
316c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  // See what it got.
317c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  void* join_result;
318c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  ASSERT_EQ(0, pthread_join(signal_thread, &join_result));
319c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  ASSERT_EQ(SIGUSR1, received_signal);
3205b9310e502003e584bcb3a028ca3db7aa4d3f01bElliott Hughes  ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
32119e62325c268a668692e2b65fde2284079f369aaElliott Hughes
32219e62325c268a668692e2b65fde2284079f369aaElliott Hughes  // Restore the original signal mask.
32319e62325c268a668692e2b65fde2284079f369aaElliott Hughes  ASSERT_EQ(0, pthread_sigmask(SIG_SETMASK, &original_set, NULL));
324c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes}
3255e3fc43ddeada547a155c6f561a12ff0b16e02d3Elliott Hughes
3263e898476c7230b60a0f76968e64ff25f475b48c0Elliott HughesTEST(pthread, pthread_setname_np__too_long) {
327f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#if defined(__BIONIC__) // Not all build servers have a new enough glibc? TODO: remove when they're on gprecise.
3283e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes  ASSERT_EQ(ERANGE, pthread_setname_np(pthread_self(), "this name is far too long for linux"));
329f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#else // __BIONIC__
330f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris  GTEST_LOG_(INFO) << "This test does nothing.\n";
331f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#endif // __BIONIC__
3323e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes}
3333e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes
3343e898476c7230b60a0f76968e64ff25f475b48c0Elliott HughesTEST(pthread, pthread_setname_np__self) {
335f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#if defined(__BIONIC__) // Not all build servers have a new enough glibc? TODO: remove when they're on gprecise.
3363e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes  ASSERT_EQ(0, pthread_setname_np(pthread_self(), "short 1"));
337f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#else // __BIONIC__
338f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris  GTEST_LOG_(INFO) << "This test does nothing.\n";
339f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#endif // __BIONIC__
3403e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes}
3413e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes
3423e898476c7230b60a0f76968e64ff25f475b48c0Elliott HughesTEST(pthread, pthread_setname_np__other) {
343f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#if defined(__BIONIC__) // Not all build servers have a new enough glibc? TODO: remove when they're on gprecise.
34440eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes  // Emulator kernels don't currently support setting the name of other threads.
34540eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes  char* filename = NULL;
34640eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes  asprintf(&filename, "/proc/self/task/%d/comm", gettid());
34740eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes  struct stat sb;
34840eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes  bool has_comm = (stat(filename, &sb) != -1);
34940eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes  free(filename);
35040eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes
35140eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes  if (has_comm) {
35240eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes    pthread_t t1;
35340eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes    ASSERT_EQ(0, pthread_create(&t1, NULL, SleepFn, reinterpret_cast<void*>(5)));
35440eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes    ASSERT_EQ(0, pthread_setname_np(t1, "short 2"));
35540eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes  } else {
35640eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes    fprintf(stderr, "skipping test: this kernel doesn't have /proc/self/task/tid/comm files!\n");
35740eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes  }
358f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#else // __BIONIC__
359f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris  GTEST_LOG_(INFO) << "This test does nothing.\n";
360f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#endif // __BIONIC__
3613e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes}
3623e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes
3633e898476c7230b60a0f76968e64ff25f475b48c0Elliott HughesTEST(pthread, pthread_setname_np__no_such_thread) {
364f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#if defined(__BIONIC__) // Not all build servers have a new enough glibc? TODO: remove when they're on gprecise.
3659d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  pthread_t dead_thread;
3669d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  MakeDeadThread(dead_thread);
3673e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes
3683e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes  // Call pthread_setname_np after thread has already exited.
369a41ba2f0bfc4fce1ce8f06a9c289102c440c929dElliott Hughes  ASSERT_EQ(ESRCH, pthread_setname_np(dead_thread, "short 3"));
370f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#else // __BIONIC__
371f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris  GTEST_LOG_(INFO) << "This test does nothing.\n";
372f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#endif // __BIONIC__
3733e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes}
3749d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes
3759d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott HughesTEST(pthread, pthread_kill__0) {
3769d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  // Signal 0 just tests that the thread exists, so it's safe to call on ourselves.
3779d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  ASSERT_EQ(0, pthread_kill(pthread_self(), 0));
3789d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes}
3799d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes
3809d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott HughesTEST(pthread, pthread_kill__invalid_signal) {
3819d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  ASSERT_EQ(EINVAL, pthread_kill(pthread_self(), -1));
3829d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes}
3839d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes
384fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughesstatic void pthread_kill__in_signal_handler_helper(int signal_number) {
385fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes  static int count = 0;
386fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes  ASSERT_EQ(SIGALRM, signal_number);
387fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes  if (++count == 1) {
388fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes    // Can we call pthread_kill from a signal handler?
389fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes    ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
390fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes  }
391fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes}
392fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes
393fae89fc4042ee4c360842234dfda7831c313bd44Elliott HughesTEST(pthread, pthread_kill__in_signal_handler) {
3944b558f50a42c97d461f1dede5aaaae490ea99e2eElliott Hughes  ScopedSignalHandler ssh(SIGALRM, pthread_kill__in_signal_handler_helper);
395fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes  ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
396fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes}
397fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes
3989d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott HughesTEST(pthread, pthread_detach__no_such_thread) {
3999d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  pthread_t dead_thread;
4009d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  MakeDeadThread(dead_thread);
4019d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes
4029d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  ASSERT_EQ(ESRCH, pthread_detach(dead_thread));
4039d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes}
4049d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes
40504620a3cd7bdea0d1b421c8772ba3f06839bbe9cElliott HughesTEST(pthread, pthread_detach__leak) {
406e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris  size_t initial_bytes = 0;
407e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris  // Run this loop more than once since the first loop causes some memory
408e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris  // to be allocated permenantly. Run an extra loop to help catch any subtle
409e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris  // memory leaks.
410e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris  for (size_t loop = 0; loop < 3; loop++) {
411e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris    // Set the initial bytes on the second loop since the memory in use
412e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris    // should have stabilized.
413e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris    if (loop == 1) {
414e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris      initial_bytes = mallinfo().uordblks;
415e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris    }
416e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris
417e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris    pthread_attr_t attr;
418e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris    ASSERT_EQ(0, pthread_attr_init(&attr));
419e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris    ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE));
420e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris
421e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris    std::vector<pthread_t> threads;
422e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris    for (size_t i = 0; i < 32; ++i) {
423e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris      pthread_t t;
424e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris      ASSERT_EQ(0, pthread_create(&t, &attr, IdFn, NULL));
425e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris      threads.push_back(t);
426e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris    }
427e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris
428e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris    sleep(1);
429e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris
430e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris    for (size_t i = 0; i < 32; ++i) {
431e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris      ASSERT_EQ(0, pthread_detach(threads[i])) << i;
432e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris    }
43304620a3cd7bdea0d1b421c8772ba3f06839bbe9cElliott Hughes  }
43404620a3cd7bdea0d1b421c8772ba3f06839bbe9cElliott Hughes
43504620a3cd7bdea0d1b421c8772ba3f06839bbe9cElliott Hughes  size_t final_bytes = mallinfo().uordblks;
43604620a3cd7bdea0d1b421c8772ba3f06839bbe9cElliott Hughes  int leaked_bytes = (final_bytes - initial_bytes);
43704620a3cd7bdea0d1b421c8772ba3f06839bbe9cElliott Hughes
43804620a3cd7bdea0d1b421c8772ba3f06839bbe9cElliott Hughes  // User code (like this test) doesn't know how large pthread_internal_t is.
43904620a3cd7bdea0d1b421c8772ba3f06839bbe9cElliott Hughes  // We can be pretty sure it's more than 128 bytes.
44004620a3cd7bdea0d1b421c8772ba3f06839bbe9cElliott Hughes  ASSERT_LT(leaked_bytes, 32 /*threads*/ * 128 /*bytes*/);
44104620a3cd7bdea0d1b421c8772ba3f06839bbe9cElliott Hughes}
44204620a3cd7bdea0d1b421c8772ba3f06839bbe9cElliott Hughes
4439b06cc3c1b2c4e2b08582f3fc9393a05aa589766Jeff HaoTEST(pthread, pthread_getcpuclockid__clock_gettime) {
4449b06cc3c1b2c4e2b08582f3fc9393a05aa589766Jeff Hao  pthread_t t;
4459b06cc3c1b2c4e2b08582f3fc9393a05aa589766Jeff Hao  ASSERT_EQ(0, pthread_create(&t, NULL, SleepFn, reinterpret_cast<void*>(5)));
4469b06cc3c1b2c4e2b08582f3fc9393a05aa589766Jeff Hao
4479b06cc3c1b2c4e2b08582f3fc9393a05aa589766Jeff Hao  clockid_t c;
4489b06cc3c1b2c4e2b08582f3fc9393a05aa589766Jeff Hao  ASSERT_EQ(0, pthread_getcpuclockid(t, &c));
4499b06cc3c1b2c4e2b08582f3fc9393a05aa589766Jeff Hao  timespec ts;
4509b06cc3c1b2c4e2b08582f3fc9393a05aa589766Jeff Hao  ASSERT_EQ(0, clock_gettime(c, &ts));
4519b06cc3c1b2c4e2b08582f3fc9393a05aa589766Jeff Hao}
4529b06cc3c1b2c4e2b08582f3fc9393a05aa589766Jeff Hao
4539d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott HughesTEST(pthread, pthread_getcpuclockid__no_such_thread) {
4549d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  pthread_t dead_thread;
4559d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  MakeDeadThread(dead_thread);
4569d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes
4579d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  clockid_t c;
4589d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  ASSERT_EQ(ESRCH, pthread_getcpuclockid(dead_thread, &c));
4599d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes}
4609d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes
4619d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott HughesTEST(pthread, pthread_getschedparam__no_such_thread) {
4629d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  pthread_t dead_thread;
4639d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  MakeDeadThread(dead_thread);
4649d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes
4659d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  int policy;
4669d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  sched_param param;
4679d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  ASSERT_EQ(ESRCH, pthread_getschedparam(dead_thread, &policy, &param));
4689d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes}
4699d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes
4709d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott HughesTEST(pthread, pthread_setschedparam__no_such_thread) {
4719d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  pthread_t dead_thread;
4729d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  MakeDeadThread(dead_thread);
4739d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes
4749d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  int policy = 0;
4759d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  sched_param param;
4769d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  ASSERT_EQ(ESRCH, pthread_setschedparam(dead_thread, policy, &param));
4779d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes}
4789d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes
4799d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott HughesTEST(pthread, pthread_join__no_such_thread) {
4809d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  pthread_t dead_thread;
4819d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  MakeDeadThread(dead_thread);
4829d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes
4839d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  void* result;
4849d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  ASSERT_EQ(ESRCH, pthread_join(dead_thread, &result));
4859d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes}
4869d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes
4879d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott HughesTEST(pthread, pthread_kill__no_such_thread) {
4889d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  pthread_t dead_thread;
4899d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  MakeDeadThread(dead_thread);
4909d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes
4919d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  ASSERT_EQ(ESRCH, pthread_kill(dead_thread, 0));
4929d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes}
4930f020d18b138e24b1fe34074808e07ac412f35a4msg
4940f020d18b138e24b1fe34074808e07ac412f35a4msgTEST(pthread, pthread_join__multijoin) {
4950f020d18b138e24b1fe34074808e07ac412f35a4msg  bool done = false;
4960f020d18b138e24b1fe34074808e07ac412f35a4msg
4970f020d18b138e24b1fe34074808e07ac412f35a4msg  pthread_t t1;
4980f020d18b138e24b1fe34074808e07ac412f35a4msg  ASSERT_EQ(0, pthread_create(&t1, NULL, SpinFn, &done));
4990f020d18b138e24b1fe34074808e07ac412f35a4msg
5000f020d18b138e24b1fe34074808e07ac412f35a4msg  pthread_t t2;
5010f020d18b138e24b1fe34074808e07ac412f35a4msg  ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
5020f020d18b138e24b1fe34074808e07ac412f35a4msg
5030f020d18b138e24b1fe34074808e07ac412f35a4msg  sleep(1); // (Give t2 a chance to call pthread_join.)
5040f020d18b138e24b1fe34074808e07ac412f35a4msg
5050f020d18b138e24b1fe34074808e07ac412f35a4msg  // Multiple joins to the same thread should fail.
5060f020d18b138e24b1fe34074808e07ac412f35a4msg  ASSERT_EQ(EINVAL, pthread_join(t1, NULL));
5070f020d18b138e24b1fe34074808e07ac412f35a4msg
5080f020d18b138e24b1fe34074808e07ac412f35a4msg  done = true;
5090f020d18b138e24b1fe34074808e07ac412f35a4msg
5100f020d18b138e24b1fe34074808e07ac412f35a4msg  // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes).
5110f020d18b138e24b1fe34074808e07ac412f35a4msg  void* join_result;
5120f020d18b138e24b1fe34074808e07ac412f35a4msg  ASSERT_EQ(0, pthread_join(t2, &join_result));
5135b9310e502003e584bcb3a028ca3db7aa4d3f01bElliott Hughes  ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
5140f020d18b138e24b1fe34074808e07ac412f35a4msg}
515b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes
51670b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott HughesTEST(pthread, pthread_join__race) {
51770b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes  // http://b/11693195 --- pthread_join could return before the thread had actually exited.
51870b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes  // If the joiner unmapped the thread's stack, that could lead to SIGSEGV in the thread.
51970b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes  for (size_t i = 0; i < 1024; ++i) {
52070b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes    size_t stack_size = 64*1024;
52170b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes    void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
52270b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes
52370b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes    pthread_attr_t a;
52470b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes    pthread_attr_init(&a);
52570b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes    pthread_attr_setstack(&a, stack, stack_size);
52670b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes
52770b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes    pthread_t t;
52870b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes    ASSERT_EQ(0, pthread_create(&t, &a, IdFn, NULL));
52970b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes    ASSERT_EQ(0, pthread_join(t, NULL));
53070b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes    ASSERT_EQ(0, munmap(stack, stack_size));
53170b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes  }
53270b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes}
53370b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes
534b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughesstatic void* GetActualGuardSizeFn(void* arg) {
535b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  pthread_attr_t attributes;
536b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  pthread_getattr_np(pthread_self(), &attributes);
537b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  pthread_attr_getguardsize(&attributes, reinterpret_cast<size_t*>(arg));
538b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  return NULL;
539b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes}
540b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes
541b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughesstatic size_t GetActualGuardSize(const pthread_attr_t& attributes) {
542b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  size_t result;
543b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  pthread_t t;
544b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  pthread_create(&t, &attributes, GetActualGuardSizeFn, &result);
545b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  void* join_result;
546b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  pthread_join(t, &join_result);
547b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  return result;
548b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes}
549b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes
550b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughesstatic void* GetActualStackSizeFn(void* arg) {
551b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  pthread_attr_t attributes;
552b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  pthread_getattr_np(pthread_self(), &attributes);
553b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  pthread_attr_getstacksize(&attributes, reinterpret_cast<size_t*>(arg));
554b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  return NULL;
555b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes}
556b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes
557b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughesstatic size_t GetActualStackSize(const pthread_attr_t& attributes) {
558b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  size_t result;
559b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  pthread_t t;
560b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  pthread_create(&t, &attributes, GetActualStackSizeFn, &result);
561b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  void* join_result;
562b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  pthread_join(t, &join_result);
563b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  return result;
564b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes}
565b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes
566b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott HughesTEST(pthread, pthread_attr_setguardsize) {
567b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  pthread_attr_t attributes;
568b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(0, pthread_attr_init(&attributes));
569b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes
570b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  // Get the default guard size.
571b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  size_t default_guard_size;
572b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &default_guard_size));
573b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes
574b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  // No such thing as too small: will be rounded up to one page by pthread_create.
575b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 128));
576b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  size_t guard_size;
577b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
578b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(128U, guard_size);
579b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(4096U, GetActualGuardSize(attributes));
580b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes
581b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  // Large enough and a multiple of the page size.
582b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024));
583b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
584b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(32*1024U, guard_size);
585b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes
586b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  // Large enough but not a multiple of the page size; will be rounded up by pthread_create.
587b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024 + 1));
588b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
589b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(32*1024U + 1, guard_size);
590b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes}
591b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes
592b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott HughesTEST(pthread, pthread_attr_setstacksize) {
593b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  pthread_attr_t attributes;
594b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(0, pthread_attr_init(&attributes));
595b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes
596b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  // Get the default stack size.
597b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  size_t default_stack_size;
598b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &default_stack_size));
599b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes
600b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  // Too small.
601b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(EINVAL, pthread_attr_setstacksize(&attributes, 128));
602b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  size_t stack_size;
603b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
604b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(default_stack_size, stack_size);
605b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_GE(GetActualStackSize(attributes), default_stack_size);
606b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes
607b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  // Large enough and a multiple of the page size.
608b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024));
609b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
610b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(32*1024U, stack_size);
611b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(GetActualStackSize(attributes), 32*1024U);
612b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes
613b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  // Large enough but not a multiple of the page size; will be rounded up by pthread_create.
614b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024 + 1));
615b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
616b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(32*1024U + 1, stack_size);
617f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#if defined(__BIONIC__)
618b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  // Bionic rounds up, which is what POSIX allows.
619b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(GetActualStackSize(attributes), (32 + 4)*1024U);
620f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#else // __BIONIC__
621b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  // glibc rounds down, in violation of POSIX. They document this in their BUGS section.
622b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(GetActualStackSize(attributes), 32*1024U);
623f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#endif // __BIONIC__
624b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes}
625c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
626c3f114037dbf028896310609fd28cf2b3da99c4dElliott HughesTEST(pthread, pthread_rwlock_smoke) {
627c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  pthread_rwlock_t l;
628c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  ASSERT_EQ(0, pthread_rwlock_init(&l, NULL));
629c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
63076f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  // Single read lock
631c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
632c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  ASSERT_EQ(0, pthread_rwlock_unlock(&l));
633c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
63476f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  // Multiple read lock
63576f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
63676f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
63776f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  ASSERT_EQ(0, pthread_rwlock_unlock(&l));
63876f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  ASSERT_EQ(0, pthread_rwlock_unlock(&l));
63976f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle
64076f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  // Write lock
64192687e41bcf108957944dafa80a9bfda219bfb0fCalin Juravle  ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
64292687e41bcf108957944dafa80a9bfda219bfb0fCalin Juravle  ASSERT_EQ(0, pthread_rwlock_unlock(&l));
64376f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle
64476f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  // Try writer lock
64576f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  ASSERT_EQ(0, pthread_rwlock_trywrlock(&l));
64676f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l));
64776f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  ASSERT_EQ(EBUSY, pthread_rwlock_tryrdlock(&l));
64876f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  ASSERT_EQ(0, pthread_rwlock_unlock(&l));
64976f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle
65076f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  // Try reader lock
65176f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l));
65276f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l));
65376f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l));
65476f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  ASSERT_EQ(0, pthread_rwlock_unlock(&l));
65576f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  ASSERT_EQ(0, pthread_rwlock_unlock(&l));
65676f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle
65776f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  // Try writer lock after unlock
65876f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
65976f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  ASSERT_EQ(0, pthread_rwlock_unlock(&l));
66076f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle
66176f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle#ifdef __BIONIC__
66276f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  // EDEADLK in "read after write"
66376f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
66476f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  ASSERT_EQ(EDEADLK, pthread_rwlock_rdlock(&l));
66576f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  ASSERT_EQ(0, pthread_rwlock_unlock(&l));
66676f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle
66776f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  // EDEADLK in "write after write"
668c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
66976f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle  ASSERT_EQ(EDEADLK, pthread_rwlock_wrlock(&l));
670c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  ASSERT_EQ(0, pthread_rwlock_unlock(&l));
67176f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle#endif
672c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
673c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  ASSERT_EQ(0, pthread_rwlock_destroy(&l));
674c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes}
675c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
6761728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic int g_once_fn_call_count = 0;
677c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughesstatic void OnceFn() {
6781728b2396591853345507a063ed6075dfd251706Elliott Hughes  ++g_once_fn_call_count;
679c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes}
680c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
681c3f114037dbf028896310609fd28cf2b3da99c4dElliott HughesTEST(pthread, pthread_once_smoke) {
682c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  pthread_once_t once_control = PTHREAD_ONCE_INIT;
683c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
684c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
6851728b2396591853345507a063ed6075dfd251706Elliott Hughes  ASSERT_EQ(1, g_once_fn_call_count);
686c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes}
687c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
6883694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughesstatic std::string pthread_once_1934122_result = "";
6893694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes
6903694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughesstatic void Routine2() {
6913694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes  pthread_once_1934122_result += "2";
6923694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes}
6933694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes
6943694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughesstatic void Routine1() {
6953694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes  pthread_once_t once_control_2 = PTHREAD_ONCE_INIT;
6963694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes  pthread_once_1934122_result += "1";
6973694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes  pthread_once(&once_control_2, &Routine2);
6983694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes}
6993694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes
7003694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott HughesTEST(pthread, pthread_once_1934122) {
7013694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes  // Very old versions of Android couldn't call pthread_once from a
7023694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes  // pthread_once init routine. http://b/1934122.
7033694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes  pthread_once_t once_control_1 = PTHREAD_ONCE_INIT;
7043694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes  ASSERT_EQ(0, pthread_once(&once_control_1, &Routine1));
7053694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes  ASSERT_EQ("12", pthread_once_1934122_result);
7063694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes}
7073694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes
7081728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic int g_atfork_prepare_calls = 0;
7091728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic void AtForkPrepare1() { g_atfork_prepare_calls = (g_atfork_prepare_calls << 4) | 1; }
7101728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic void AtForkPrepare2() { g_atfork_prepare_calls = (g_atfork_prepare_calls << 4) | 2; }
7111728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic int g_atfork_parent_calls = 0;
7121728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic void AtForkParent1() { g_atfork_parent_calls = (g_atfork_parent_calls << 4) | 1; }
7131728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic void AtForkParent2() { g_atfork_parent_calls = (g_atfork_parent_calls << 4) | 2; }
7141728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic int g_atfork_child_calls = 0;
7151728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic void AtForkChild1() { g_atfork_child_calls = (g_atfork_child_calls << 4) | 1; }
7161728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic void AtForkChild2() { g_atfork_child_calls = (g_atfork_child_calls << 4) | 2; }
717c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
718c3f114037dbf028896310609fd28cf2b3da99c4dElliott HughesTEST(pthread, pthread_atfork) {
719c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1));
720c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  ASSERT_EQ(0, pthread_atfork(AtForkPrepare2, AtForkParent2, AtForkChild2));
721c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
722c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  int pid = fork();
723c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  ASSERT_NE(-1, pid) << strerror(errno);
724c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
725c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  // Child and parent calls are made in the order they were registered.
726c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  if (pid == 0) {
7271728b2396591853345507a063ed6075dfd251706Elliott Hughes    ASSERT_EQ(0x12, g_atfork_child_calls);
728c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes    _exit(0);
729c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  }
7301728b2396591853345507a063ed6075dfd251706Elliott Hughes  ASSERT_EQ(0x12, g_atfork_parent_calls);
731c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
732c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  // Prepare calls are made in the reverse order.
7331728b2396591853345507a063ed6075dfd251706Elliott Hughes  ASSERT_EQ(0x21, g_atfork_prepare_calls);
734c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes}
735c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
736c3f114037dbf028896310609fd28cf2b3da99c4dElliott HughesTEST(pthread, pthread_attr_getscope) {
737c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  pthread_attr_t attr;
738c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  ASSERT_EQ(0, pthread_attr_init(&attr));
739c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
740c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  int scope;
741c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  ASSERT_EQ(0, pthread_attr_getscope(&attr, &scope));
742c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  ASSERT_EQ(PTHREAD_SCOPE_SYSTEM, scope);
743c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes}
74451e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath
74551e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan KamathTEST(pthread, pthread_condattr_init) {
74651e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  pthread_condattr_t attr;
74751e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  pthread_condattr_init(&attr);
74851e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath
74951e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  clockid_t clock;
75051e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
75151e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  ASSERT_EQ(CLOCK_REALTIME, clock);
75251e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath
75351e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  int pshared;
75451e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared));
75551e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  ASSERT_EQ(PTHREAD_PROCESS_PRIVATE, pshared);
75651e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath}
75751e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath
75851e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan KamathTEST(pthread, pthread_condattr_setclock) {
75951e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  pthread_condattr_t attr;
76051e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  pthread_condattr_init(&attr);
76151e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath
76251e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_REALTIME));
76351e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  clockid_t clock;
76451e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
76551e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  ASSERT_EQ(CLOCK_REALTIME, clock);
76651e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath
76751e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC));
76851e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
76951e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  ASSERT_EQ(CLOCK_MONOTONIC, clock);
77051e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath
77151e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  ASSERT_EQ(EINVAL, pthread_condattr_setclock(&attr, CLOCK_PROCESS_CPUTIME_ID));
77251e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath}
77351e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath
77451e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan KamathTEST(pthread, pthread_cond_broadcast__preserves_condattr_flags) {
77551e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath#if defined(__BIONIC__) // This tests a bionic implementation detail.
77651e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  pthread_condattr_t attr;
77751e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  pthread_condattr_init(&attr);
77851e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath
77951e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC));
78051e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  ASSERT_EQ(0, pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED));
78151e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath
78251e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  pthread_cond_t cond_var;
78351e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  ASSERT_EQ(0, pthread_cond_init(&cond_var, &attr));
78451e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath
78551e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  ASSERT_EQ(0, pthread_cond_signal(&cond_var));
78651e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  ASSERT_EQ(0, pthread_cond_broadcast(&cond_var));
78751e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath
78851e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  attr = static_cast<pthread_condattr_t>(cond_var.value);
78951e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  clockid_t clock;
79051e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
79151e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  ASSERT_EQ(CLOCK_MONOTONIC, clock);
79251e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  int pshared;
79351e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared));
79451e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  ASSERT_EQ(PTHREAD_PROCESS_SHARED, pshared);
79551e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath#else // __BIONIC__
79651e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath  GTEST_LOG_(INFO) << "This test does nothing.\n";
79751e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath#endif // __BIONIC__
79851e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath}
7990e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes
8000e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott HughesTEST(pthread, pthread_mutex_timedlock) {
8010e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes  pthread_mutex_t m;
8020e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes  ASSERT_EQ(0, pthread_mutex_init(&m, NULL));
8030e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes
8040e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes  // If the mutex is already locked, pthread_mutex_timedlock should time out.
8050e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes  ASSERT_EQ(0, pthread_mutex_lock(&m));
8060e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes
8070e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes  timespec ts;
8080e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes  ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
8090e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes  ts.tv_nsec += 1;
8100e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes  ASSERT_EQ(ETIMEDOUT, pthread_mutex_timedlock(&m, &ts));
8110e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes
8120e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes  // If the mutex is unlocked, pthread_mutex_timedlock should succeed.
8130e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes  ASSERT_EQ(0, pthread_mutex_unlock(&m));
8140e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes
8150e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes  ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
8160e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes  ts.tv_nsec += 1;
8170e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes  ASSERT_EQ(0, pthread_mutex_timedlock(&m, &ts));
8180e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes
8190e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes  ASSERT_EQ(0, pthread_mutex_unlock(&m));
8200e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes  ASSERT_EQ(0, pthread_mutex_destroy(&m));
8210e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes}
82257b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes
82357b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott HughesTEST(pthread, pthread_attr_getstack__main_thread) {
82457b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  // This test is only meaningful for the main thread, so make sure we're running on it!
82557b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  ASSERT_EQ(getpid(), syscall(__NR_gettid));
82657b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes
82757b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  // Get the main thread's attributes.
82857b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  pthread_attr_t attributes;
82957b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
83057b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes
83157b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  // Check that we correctly report that the main thread has no guard page.
83257b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  size_t guard_size;
83357b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
83457b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  ASSERT_EQ(0U, guard_size); // The main thread has no guard page.
83557b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes
83657b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  // Get the stack base and the stack size (both ways).
83757b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  void* stack_base;
83857b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  size_t stack_size;
83957b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
84057b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  size_t stack_size2;
84157b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
84257b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes
84357b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  // The two methods of asking for the stack size should agree.
84457b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  EXPECT_EQ(stack_size, stack_size2);
84557b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes
84657b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  // What does /proc/self/maps' [stack] line say?
8479e4ffa7032eaab308876b8e3da86b05c3c613878Elliott Hughes  void* maps_stack_hi = NULL;
84857b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  FILE* fp = fopen("/proc/self/maps", "r");
84957b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  ASSERT_TRUE(fp != NULL);
85057b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  char line[BUFSIZ];
85157b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  while (fgets(line, sizeof(line), fp) != NULL) {
85257b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes    uintptr_t lo, hi;
85357b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes    char name[10];
85457b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes    sscanf(line, "%" PRIxPTR "-%" PRIxPTR " %*4s %*x %*x:%*x %*d %10s", &lo, &hi, name);
85557b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes    if (strcmp(name, "[stack]") == 0) {
8569e4ffa7032eaab308876b8e3da86b05c3c613878Elliott Hughes      maps_stack_hi = reinterpret_cast<void*>(hi);
85757b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes      break;
85857b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes    }
85957b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  }
86057b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  fclose(fp);
86157b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes
8629e4ffa7032eaab308876b8e3da86b05c3c613878Elliott Hughes  // The stack size should correspond to RLIMIT_STACK.
8639e4ffa7032eaab308876b8e3da86b05c3c613878Elliott Hughes  rlimit rl;
8649e4ffa7032eaab308876b8e3da86b05c3c613878Elliott Hughes  ASSERT_EQ(0, getrlimit(RLIMIT_STACK, &rl));
8659e4ffa7032eaab308876b8e3da86b05c3c613878Elliott Hughes  EXPECT_EQ(rl.rlim_cur, stack_size);
86657b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes
8679e4ffa7032eaab308876b8e3da86b05c3c613878Elliott Hughes  // The high address of the /proc/self/maps [stack] region should equal stack_base + stack_size.
8689e4ffa7032eaab308876b8e3da86b05c3c613878Elliott Hughes  // Remember that the stack grows down (and is mapped in on demand), so the low address of the
8699e4ffa7032eaab308876b8e3da86b05c3c613878Elliott Hughes  // region isn't very interesting.
8709e4ffa7032eaab308876b8e3da86b05c3c613878Elliott Hughes  EXPECT_EQ(maps_stack_hi, reinterpret_cast<uint8_t*>(stack_base) + stack_size);
87157b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes
87257b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  //
8739e4ffa7032eaab308876b8e3da86b05c3c613878Elliott Hughes  // What if RLIMIT_STACK is smaller than the stack's current extent?
87457b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  //
87557b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  rl.rlim_cur = rl.rlim_max = 1024; // 1KiB. We know the stack must be at least a page already.
87657b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  rl.rlim_max = RLIM_INFINITY;
87757b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
87857b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes
87957b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
88057b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
88157b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
88257b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes
88357b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  EXPECT_EQ(stack_size, stack_size2);
88457b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  ASSERT_EQ(1024U, stack_size);
88557b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes
88657b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  //
8879e4ffa7032eaab308876b8e3da86b05c3c613878Elliott Hughes  // What if RLIMIT_STACK isn't a whole number of pages?
88857b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  //
88957b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  rl.rlim_cur = rl.rlim_max = 6666; // Not a whole number of pages.
89057b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  rl.rlim_max = RLIM_INFINITY;
89157b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
89257b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes
89357b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes));
89457b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size));
89557b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2));
89657b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes
89757b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  EXPECT_EQ(stack_size, stack_size2);
89857b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes  ASSERT_EQ(6666U, stack_size);
89957b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes}
900