pthread_test.cpp revision 34c987a6dd6816eff98bc25f627659550c2338dc
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)); 17134c987a6dd6816eff98bc25f627659550c2338dcElliott Hughes ASSERT_EQ(0, pthread_join(t, NULL)); 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. 20334c987a6dd6816eff98bc25f627659550c2338dcElliott Hughes ASSERT_EQ(EINVAL, pthread_join(t1, NULL)); 2044d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes} 2054d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes 2064d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott HughesTEST(pthread, pthread_no_op_detach_after_join) { 20710ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov bool done = false; 20810ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov 2094d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes pthread_t t1; 21010ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov ASSERT_EQ(0, pthread_create(&t1, NULL, SpinFn, &done)); 2114d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes 2124d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes // If thread 2 is already waiting to join thread 1... 2134d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes pthread_t t2; 2144d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1))); 2154d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes 21610ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov sleep(1); // (Give t2 a chance to call pthread_join.) 21710ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov 21810ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov // ...a call to pthread_detach on thread 1 will "succeed" (silently fail)... 2194d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes ASSERT_EQ(0, pthread_detach(t1)); 22010ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov AssertDetached(t1, false); 22110ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov 22210ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov done = true; 2234d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes 22410ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes). 2254d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes void* join_result; 2264d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes ASSERT_EQ(0, pthread_join(t2, &join_result)); 2275b9310e502003e584bcb3a028ca3db7aa4d3f01bElliott Hughes ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result)); 2284d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes} 22914f19592ae7c819855052bcebc79de87069c2954Elliott Hughes 23014f19592ae7c819855052bcebc79de87069c2954Elliott HughesTEST(pthread, pthread_join_self) { 23134c987a6dd6816eff98bc25f627659550c2338dcElliott Hughes ASSERT_EQ(EDEADLK, pthread_join(pthread_self(), NULL)); 23214f19592ae7c819855052bcebc79de87069c2954Elliott Hughes} 2334f251bee5d51228217c1bf4dfc9219f3058bd3edElliott Hughes 234877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughesstruct TestBug37410 { 235877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes pthread_t main_thread; 236877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes pthread_mutex_t mutex; 2374f251bee5d51228217c1bf4dfc9219f3058bd3edElliott Hughes 238877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes static void main() { 239877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes TestBug37410 data; 240877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes data.main_thread = pthread_self(); 241877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes ASSERT_EQ(0, pthread_mutex_init(&data.mutex, NULL)); 242877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes ASSERT_EQ(0, pthread_mutex_lock(&data.mutex)); 243877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes 244877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes pthread_t t; 245877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes ASSERT_EQ(0, pthread_create(&t, NULL, TestBug37410::thread_fn, reinterpret_cast<void*>(&data))); 246877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes 247877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes // Wait for the thread to be running... 248877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes ASSERT_EQ(0, pthread_mutex_lock(&data.mutex)); 249877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes ASSERT_EQ(0, pthread_mutex_unlock(&data.mutex)); 250877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes 251877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes // ...and exit. 252877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes pthread_exit(NULL); 253877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes } 254877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes 255877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes private: 256877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes static void* thread_fn(void* arg) { 257877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes TestBug37410* data = reinterpret_cast<TestBug37410*>(arg); 258877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes 259877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes // Let the main thread know we're running. 260877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes pthread_mutex_unlock(&data->mutex); 261877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes 262877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes // And wait for the main thread to exit. 263877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes pthread_join(data->main_thread, NULL); 264877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes 265877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes return NULL; 266877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes } 267877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes}; 2684f251bee5d51228217c1bf4dfc9219f3058bd3edElliott Hughes 2697fd803cdfa873c01138dcbd614ec92418169b1c2Elliott Hughes// Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to 2707fd803cdfa873c01138dcbd614ec92418169b1c2Elliott Hughes// run this test (which exits normally) in its own process. 2714f251bee5d51228217c1bf4dfc9219f3058bd3edElliott HughesTEST(pthread_DeathTest, pthread_bug_37410) { 2724f251bee5d51228217c1bf4dfc9219f3058bd3edElliott Hughes // http://code.google.com/p/android/issues/detail?id=37410 2734f251bee5d51228217c1bf4dfc9219f3058bd3edElliott Hughes ::testing::FLAGS_gtest_death_test_style = "threadsafe"; 274877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes ASSERT_EXIT(TestBug37410::main(), ::testing::ExitedWithCode(0), ""); 2754f251bee5d51228217c1bf4dfc9219f3058bd3edElliott Hughes} 276c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes 277c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughesstatic void* SignalHandlerFn(void* arg) { 278c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes sigset_t wait_set; 279c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes sigfillset(&wait_set); 280c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes return reinterpret_cast<void*>(sigwait(&wait_set, reinterpret_cast<int*>(arg))); 281c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes} 282c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes 283c5d028fc913de84a781bd61084bf7ae2182fd48eElliott HughesTEST(pthread, pthread_sigmask) { 28419e62325c268a668692e2b65fde2284079f369aaElliott Hughes // Check that SIGUSR1 isn't blocked. 28519e62325c268a668692e2b65fde2284079f369aaElliott Hughes sigset_t original_set; 28619e62325c268a668692e2b65fde2284079f369aaElliott Hughes sigemptyset(&original_set); 28719e62325c268a668692e2b65fde2284079f369aaElliott Hughes ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &original_set)); 28819e62325c268a668692e2b65fde2284079f369aaElliott Hughes ASSERT_FALSE(sigismember(&original_set, SIGUSR1)); 28919e62325c268a668692e2b65fde2284079f369aaElliott Hughes 290c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes // Block SIGUSR1. 291c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes sigset_t set; 292c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes sigemptyset(&set); 293c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes sigaddset(&set, SIGUSR1); 294c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &set, NULL)); 295c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes 29619e62325c268a668692e2b65fde2284079f369aaElliott Hughes // Check that SIGUSR1 is blocked. 29719e62325c268a668692e2b65fde2284079f369aaElliott Hughes sigset_t final_set; 29819e62325c268a668692e2b65fde2284079f369aaElliott Hughes sigemptyset(&final_set); 29919e62325c268a668692e2b65fde2284079f369aaElliott Hughes ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &final_set)); 30019e62325c268a668692e2b65fde2284079f369aaElliott Hughes ASSERT_TRUE(sigismember(&final_set, SIGUSR1)); 30119e62325c268a668692e2b65fde2284079f369aaElliott Hughes // ...and that sigprocmask agrees with pthread_sigmask. 30219e62325c268a668692e2b65fde2284079f369aaElliott Hughes sigemptyset(&final_set); 30319e62325c268a668692e2b65fde2284079f369aaElliott Hughes ASSERT_EQ(0, sigprocmask(SIG_BLOCK, NULL, &final_set)); 30419e62325c268a668692e2b65fde2284079f369aaElliott Hughes ASSERT_TRUE(sigismember(&final_set, SIGUSR1)); 30519e62325c268a668692e2b65fde2284079f369aaElliott Hughes 306c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes // Spawn a thread that calls sigwait and tells us what it received. 307c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes pthread_t signal_thread; 308c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes int received_signal = -1; 309c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes ASSERT_EQ(0, pthread_create(&signal_thread, NULL, SignalHandlerFn, &received_signal)); 310c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes 311c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes // Send that thread SIGUSR1. 312c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes pthread_kill(signal_thread, SIGUSR1); 313c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes 314c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes // See what it got. 315c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes void* join_result; 316c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes ASSERT_EQ(0, pthread_join(signal_thread, &join_result)); 317c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes ASSERT_EQ(SIGUSR1, received_signal); 3185b9310e502003e584bcb3a028ca3db7aa4d3f01bElliott Hughes ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result)); 31919e62325c268a668692e2b65fde2284079f369aaElliott Hughes 32019e62325c268a668692e2b65fde2284079f369aaElliott Hughes // Restore the original signal mask. 32119e62325c268a668692e2b65fde2284079f369aaElliott Hughes ASSERT_EQ(0, pthread_sigmask(SIG_SETMASK, &original_set, NULL)); 322c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes} 3235e3fc43ddeada547a155c6f561a12ff0b16e02d3Elliott Hughes 3243e898476c7230b60a0f76968e64ff25f475b48c0Elliott HughesTEST(pthread, pthread_setname_np__too_long) { 325f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#if defined(__BIONIC__) // Not all build servers have a new enough glibc? TODO: remove when they're on gprecise. 3263e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes ASSERT_EQ(ERANGE, pthread_setname_np(pthread_self(), "this name is far too long for linux")); 327f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#else // __BIONIC__ 328f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris GTEST_LOG_(INFO) << "This test does nothing.\n"; 329f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#endif // __BIONIC__ 3303e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes} 3313e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes 3323e898476c7230b60a0f76968e64ff25f475b48c0Elliott HughesTEST(pthread, pthread_setname_np__self) { 333f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#if defined(__BIONIC__) // Not all build servers have a new enough glibc? TODO: remove when they're on gprecise. 3343e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes ASSERT_EQ(0, pthread_setname_np(pthread_self(), "short 1")); 335f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#else // __BIONIC__ 336f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris GTEST_LOG_(INFO) << "This test does nothing.\n"; 337f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#endif // __BIONIC__ 3383e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes} 3393e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes 3403e898476c7230b60a0f76968e64ff25f475b48c0Elliott HughesTEST(pthread, pthread_setname_np__other) { 341f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#if defined(__BIONIC__) // Not all build servers have a new enough glibc? TODO: remove when they're on gprecise. 34240eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes // Emulator kernels don't currently support setting the name of other threads. 34340eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes char* filename = NULL; 34440eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes asprintf(&filename, "/proc/self/task/%d/comm", gettid()); 34540eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes struct stat sb; 34640eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes bool has_comm = (stat(filename, &sb) != -1); 34740eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes free(filename); 34840eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes 34940eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes if (has_comm) { 35040eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes pthread_t t1; 35140eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes ASSERT_EQ(0, pthread_create(&t1, NULL, SleepFn, reinterpret_cast<void*>(5))); 35240eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes ASSERT_EQ(0, pthread_setname_np(t1, "short 2")); 35340eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes } else { 35440eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes fprintf(stderr, "skipping test: this kernel doesn't have /proc/self/task/tid/comm files!\n"); 35540eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes } 356f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#else // __BIONIC__ 357f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris GTEST_LOG_(INFO) << "This test does nothing.\n"; 358f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#endif // __BIONIC__ 3593e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes} 3603e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes 3613e898476c7230b60a0f76968e64ff25f475b48c0Elliott HughesTEST(pthread, pthread_setname_np__no_such_thread) { 362f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#if defined(__BIONIC__) // Not all build servers have a new enough glibc? TODO: remove when they're on gprecise. 3639d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes pthread_t dead_thread; 3649d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes MakeDeadThread(dead_thread); 3653e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes 3663e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes // Call pthread_setname_np after thread has already exited. 367a41ba2f0bfc4fce1ce8f06a9c289102c440c929dElliott Hughes ASSERT_EQ(ESRCH, pthread_setname_np(dead_thread, "short 3")); 368f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#else // __BIONIC__ 369f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris GTEST_LOG_(INFO) << "This test does nothing.\n"; 370f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#endif // __BIONIC__ 3713e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes} 3729d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes 3739d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott HughesTEST(pthread, pthread_kill__0) { 3749d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes // Signal 0 just tests that the thread exists, so it's safe to call on ourselves. 3759d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes ASSERT_EQ(0, pthread_kill(pthread_self(), 0)); 3769d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes} 3779d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes 3789d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott HughesTEST(pthread, pthread_kill__invalid_signal) { 3799d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes ASSERT_EQ(EINVAL, pthread_kill(pthread_self(), -1)); 3809d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes} 3819d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes 382fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughesstatic void pthread_kill__in_signal_handler_helper(int signal_number) { 383fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes static int count = 0; 384fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes ASSERT_EQ(SIGALRM, signal_number); 385fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes if (++count == 1) { 386fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes // Can we call pthread_kill from a signal handler? 387fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM)); 388fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes } 389fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes} 390fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes 391fae89fc4042ee4c360842234dfda7831c313bd44Elliott HughesTEST(pthread, pthread_kill__in_signal_handler) { 3924b558f50a42c97d461f1dede5aaaae490ea99e2eElliott Hughes ScopedSignalHandler ssh(SIGALRM, pthread_kill__in_signal_handler_helper); 393fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM)); 394fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes} 395fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes 3969d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott HughesTEST(pthread, pthread_detach__no_such_thread) { 3979d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes pthread_t dead_thread; 3989d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes MakeDeadThread(dead_thread); 3999d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes 4009d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes ASSERT_EQ(ESRCH, pthread_detach(dead_thread)); 4019d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes} 4029d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes 40304620a3cd7bdea0d1b421c8772ba3f06839bbe9cElliott HughesTEST(pthread, pthread_detach__leak) { 404e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris size_t initial_bytes = 0; 405e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris // Run this loop more than once since the first loop causes some memory 406e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris // to be allocated permenantly. Run an extra loop to help catch any subtle 407e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris // memory leaks. 408e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris for (size_t loop = 0; loop < 3; loop++) { 409e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris // Set the initial bytes on the second loop since the memory in use 410e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris // should have stabilized. 411e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris if (loop == 1) { 412e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris initial_bytes = mallinfo().uordblks; 413e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris } 414e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris 415e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris pthread_attr_t attr; 416e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris ASSERT_EQ(0, pthread_attr_init(&attr)); 417e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE)); 418e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris 419e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris std::vector<pthread_t> threads; 420e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris for (size_t i = 0; i < 32; ++i) { 421e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris pthread_t t; 422e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris ASSERT_EQ(0, pthread_create(&t, &attr, IdFn, NULL)); 423e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris threads.push_back(t); 424e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris } 425e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris 426e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris sleep(1); 427e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris 428e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris for (size_t i = 0; i < 32; ++i) { 429e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris ASSERT_EQ(0, pthread_detach(threads[i])) << i; 430e380960813bbb6e05d820eb75885556a1c4bf6acChristopher Ferris } 43104620a3cd7bdea0d1b421c8772ba3f06839bbe9cElliott Hughes } 43204620a3cd7bdea0d1b421c8772ba3f06839bbe9cElliott Hughes 43304620a3cd7bdea0d1b421c8772ba3f06839bbe9cElliott Hughes size_t final_bytes = mallinfo().uordblks; 43404620a3cd7bdea0d1b421c8772ba3f06839bbe9cElliott Hughes int leaked_bytes = (final_bytes - initial_bytes); 43504620a3cd7bdea0d1b421c8772ba3f06839bbe9cElliott Hughes 43604620a3cd7bdea0d1b421c8772ba3f06839bbe9cElliott Hughes // User code (like this test) doesn't know how large pthread_internal_t is. 43704620a3cd7bdea0d1b421c8772ba3f06839bbe9cElliott Hughes // We can be pretty sure it's more than 128 bytes. 43804620a3cd7bdea0d1b421c8772ba3f06839bbe9cElliott Hughes ASSERT_LT(leaked_bytes, 32 /*threads*/ * 128 /*bytes*/); 43904620a3cd7bdea0d1b421c8772ba3f06839bbe9cElliott Hughes} 44004620a3cd7bdea0d1b421c8772ba3f06839bbe9cElliott Hughes 4419b06cc3c1b2c4e2b08582f3fc9393a05aa589766Jeff HaoTEST(pthread, pthread_getcpuclockid__clock_gettime) { 4429b06cc3c1b2c4e2b08582f3fc9393a05aa589766Jeff Hao pthread_t t; 4439b06cc3c1b2c4e2b08582f3fc9393a05aa589766Jeff Hao ASSERT_EQ(0, pthread_create(&t, NULL, SleepFn, reinterpret_cast<void*>(5))); 4449b06cc3c1b2c4e2b08582f3fc9393a05aa589766Jeff Hao 4459b06cc3c1b2c4e2b08582f3fc9393a05aa589766Jeff Hao clockid_t c; 4469b06cc3c1b2c4e2b08582f3fc9393a05aa589766Jeff Hao ASSERT_EQ(0, pthread_getcpuclockid(t, &c)); 4479b06cc3c1b2c4e2b08582f3fc9393a05aa589766Jeff Hao timespec ts; 4489b06cc3c1b2c4e2b08582f3fc9393a05aa589766Jeff Hao ASSERT_EQ(0, clock_gettime(c, &ts)); 4499b06cc3c1b2c4e2b08582f3fc9393a05aa589766Jeff Hao} 4509b06cc3c1b2c4e2b08582f3fc9393a05aa589766Jeff Hao 4519d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott HughesTEST(pthread, pthread_getcpuclockid__no_such_thread) { 4529d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes pthread_t dead_thread; 4539d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes MakeDeadThread(dead_thread); 4549d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes 4559d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes clockid_t c; 4569d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes ASSERT_EQ(ESRCH, pthread_getcpuclockid(dead_thread, &c)); 4579d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes} 4589d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes 4599d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott HughesTEST(pthread, pthread_getschedparam__no_such_thread) { 4609d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes pthread_t dead_thread; 4619d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes MakeDeadThread(dead_thread); 4629d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes 4639d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes int policy; 4649d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes sched_param param; 4659d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes ASSERT_EQ(ESRCH, pthread_getschedparam(dead_thread, &policy, ¶m)); 4669d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes} 4679d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes 4689d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott HughesTEST(pthread, pthread_setschedparam__no_such_thread) { 4699d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes pthread_t dead_thread; 4709d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes MakeDeadThread(dead_thread); 4719d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes 4729d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes int policy = 0; 4739d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes sched_param param; 4749d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes ASSERT_EQ(ESRCH, pthread_setschedparam(dead_thread, policy, ¶m)); 4759d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes} 4769d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes 4779d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott HughesTEST(pthread, pthread_join__no_such_thread) { 4789d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes pthread_t dead_thread; 4799d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes MakeDeadThread(dead_thread); 4809d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes 48134c987a6dd6816eff98bc25f627659550c2338dcElliott Hughes ASSERT_EQ(ESRCH, pthread_join(dead_thread, NULL)); 4829d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes} 4839d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes 4849d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott HughesTEST(pthread, pthread_kill__no_such_thread) { 4859d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes pthread_t dead_thread; 4869d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes MakeDeadThread(dead_thread); 4879d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes 4889d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes ASSERT_EQ(ESRCH, pthread_kill(dead_thread, 0)); 4899d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes} 4900f020d18b138e24b1fe34074808e07ac412f35a4msg 4910f020d18b138e24b1fe34074808e07ac412f35a4msgTEST(pthread, pthread_join__multijoin) { 4920f020d18b138e24b1fe34074808e07ac412f35a4msg bool done = false; 4930f020d18b138e24b1fe34074808e07ac412f35a4msg 4940f020d18b138e24b1fe34074808e07ac412f35a4msg pthread_t t1; 4950f020d18b138e24b1fe34074808e07ac412f35a4msg ASSERT_EQ(0, pthread_create(&t1, NULL, SpinFn, &done)); 4960f020d18b138e24b1fe34074808e07ac412f35a4msg 4970f020d18b138e24b1fe34074808e07ac412f35a4msg pthread_t t2; 4980f020d18b138e24b1fe34074808e07ac412f35a4msg ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1))); 4990f020d18b138e24b1fe34074808e07ac412f35a4msg 5000f020d18b138e24b1fe34074808e07ac412f35a4msg sleep(1); // (Give t2 a chance to call pthread_join.) 5010f020d18b138e24b1fe34074808e07ac412f35a4msg 5020f020d18b138e24b1fe34074808e07ac412f35a4msg // Multiple joins to the same thread should fail. 5030f020d18b138e24b1fe34074808e07ac412f35a4msg ASSERT_EQ(EINVAL, pthread_join(t1, NULL)); 5040f020d18b138e24b1fe34074808e07ac412f35a4msg 5050f020d18b138e24b1fe34074808e07ac412f35a4msg done = true; 5060f020d18b138e24b1fe34074808e07ac412f35a4msg 5070f020d18b138e24b1fe34074808e07ac412f35a4msg // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes). 5080f020d18b138e24b1fe34074808e07ac412f35a4msg void* join_result; 5090f020d18b138e24b1fe34074808e07ac412f35a4msg ASSERT_EQ(0, pthread_join(t2, &join_result)); 5105b9310e502003e584bcb3a028ca3db7aa4d3f01bElliott Hughes ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result)); 5110f020d18b138e24b1fe34074808e07ac412f35a4msg} 512b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes 51370b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott HughesTEST(pthread, pthread_join__race) { 51470b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes // http://b/11693195 --- pthread_join could return before the thread had actually exited. 51570b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes // If the joiner unmapped the thread's stack, that could lead to SIGSEGV in the thread. 51670b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes for (size_t i = 0; i < 1024; ++i) { 51770b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes size_t stack_size = 64*1024; 51870b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0); 51970b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes 52070b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes pthread_attr_t a; 52170b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes pthread_attr_init(&a); 52270b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes pthread_attr_setstack(&a, stack, stack_size); 52370b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes 52470b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes pthread_t t; 52570b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes ASSERT_EQ(0, pthread_create(&t, &a, IdFn, NULL)); 52670b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes ASSERT_EQ(0, pthread_join(t, NULL)); 52770b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes ASSERT_EQ(0, munmap(stack, stack_size)); 52870b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes } 52970b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes} 53070b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes 531b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughesstatic void* GetActualGuardSizeFn(void* arg) { 532b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes pthread_attr_t attributes; 533b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes pthread_getattr_np(pthread_self(), &attributes); 534b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes pthread_attr_getguardsize(&attributes, reinterpret_cast<size_t*>(arg)); 535b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes return NULL; 536b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes} 537b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes 538b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughesstatic size_t GetActualGuardSize(const pthread_attr_t& attributes) { 539b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes size_t result; 540b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes pthread_t t; 541b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes pthread_create(&t, &attributes, GetActualGuardSizeFn, &result); 54234c987a6dd6816eff98bc25f627659550c2338dcElliott Hughes pthread_join(t, NULL); 543b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes return result; 544b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes} 545b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes 546b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughesstatic void* GetActualStackSizeFn(void* arg) { 547b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes pthread_attr_t attributes; 548b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes pthread_getattr_np(pthread_self(), &attributes); 549b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes pthread_attr_getstacksize(&attributes, reinterpret_cast<size_t*>(arg)); 550b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes return NULL; 551b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes} 552b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes 553b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughesstatic size_t GetActualStackSize(const pthread_attr_t& attributes) { 554b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes size_t result; 555b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes pthread_t t; 556b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes pthread_create(&t, &attributes, GetActualStackSizeFn, &result); 55734c987a6dd6816eff98bc25f627659550c2338dcElliott Hughes pthread_join(t, NULL); 558b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes return result; 559b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes} 560b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes 561b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott HughesTEST(pthread, pthread_attr_setguardsize) { 562b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes pthread_attr_t attributes; 563b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes ASSERT_EQ(0, pthread_attr_init(&attributes)); 564b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes 565b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes // Get the default guard size. 566b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes size_t default_guard_size; 567b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &default_guard_size)); 568b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes 569b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes // No such thing as too small: will be rounded up to one page by pthread_create. 570b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 128)); 571b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes size_t guard_size; 572b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size)); 573b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes ASSERT_EQ(128U, guard_size); 574b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes ASSERT_EQ(4096U, GetActualGuardSize(attributes)); 575b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes 576b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes // Large enough and a multiple of the page size. 577b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024)); 578b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size)); 579b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes ASSERT_EQ(32*1024U, guard_size); 580b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes 581b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes // Large enough but not a multiple of the page size; will be rounded up by pthread_create. 582b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024 + 1)); 583b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size)); 584b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes ASSERT_EQ(32*1024U + 1, guard_size); 585b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes} 586b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes 587b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott HughesTEST(pthread, pthread_attr_setstacksize) { 588b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes pthread_attr_t attributes; 589b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes ASSERT_EQ(0, pthread_attr_init(&attributes)); 590b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes 591b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes // Get the default stack size. 592b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes size_t default_stack_size; 593b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &default_stack_size)); 594b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes 595b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes // Too small. 596b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes ASSERT_EQ(EINVAL, pthread_attr_setstacksize(&attributes, 128)); 597b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes size_t stack_size; 598b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size)); 599b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes ASSERT_EQ(default_stack_size, stack_size); 600b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes ASSERT_GE(GetActualStackSize(attributes), default_stack_size); 601b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes 602b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes // Large enough and a multiple of the page size. 603b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024)); 604b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size)); 605b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes ASSERT_EQ(32*1024U, stack_size); 606b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes ASSERT_EQ(GetActualStackSize(attributes), 32*1024U); 607b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes 608b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes // Large enough but not a multiple of the page size; will be rounded up by pthread_create. 609b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024 + 1)); 610b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size)); 611b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes ASSERT_EQ(32*1024U + 1, stack_size); 612f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#if defined(__BIONIC__) 613b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes // Bionic rounds up, which is what POSIX allows. 614b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes ASSERT_EQ(GetActualStackSize(attributes), (32 + 4)*1024U); 615f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#else // __BIONIC__ 616b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes // glibc rounds down, in violation of POSIX. They document this in their BUGS section. 617b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes ASSERT_EQ(GetActualStackSize(attributes), 32*1024U); 618f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#endif // __BIONIC__ 619b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes} 620c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes 621c3f114037dbf028896310609fd28cf2b3da99c4dElliott HughesTEST(pthread, pthread_rwlock_smoke) { 622c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes pthread_rwlock_t l; 623c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes ASSERT_EQ(0, pthread_rwlock_init(&l, NULL)); 624c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes 62576f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle // Single read lock 626c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes ASSERT_EQ(0, pthread_rwlock_rdlock(&l)); 627c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes ASSERT_EQ(0, pthread_rwlock_unlock(&l)); 628c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes 62976f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle // Multiple read lock 63076f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle ASSERT_EQ(0, pthread_rwlock_rdlock(&l)); 63176f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle ASSERT_EQ(0, pthread_rwlock_rdlock(&l)); 63276f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle ASSERT_EQ(0, pthread_rwlock_unlock(&l)); 63376f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle ASSERT_EQ(0, pthread_rwlock_unlock(&l)); 63476f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle 63576f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle // Write lock 63692687e41bcf108957944dafa80a9bfda219bfb0fCalin Juravle ASSERT_EQ(0, pthread_rwlock_wrlock(&l)); 63792687e41bcf108957944dafa80a9bfda219bfb0fCalin Juravle ASSERT_EQ(0, pthread_rwlock_unlock(&l)); 63876f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle 63976f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle // Try writer lock 64076f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle ASSERT_EQ(0, pthread_rwlock_trywrlock(&l)); 64176f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l)); 64276f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle ASSERT_EQ(EBUSY, pthread_rwlock_tryrdlock(&l)); 64376f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle ASSERT_EQ(0, pthread_rwlock_unlock(&l)); 64476f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle 64576f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle // Try reader lock 64676f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l)); 64776f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l)); 64876f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l)); 64976f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle ASSERT_EQ(0, pthread_rwlock_unlock(&l)); 65076f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle ASSERT_EQ(0, pthread_rwlock_unlock(&l)); 65176f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle 65276f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle // Try writer lock after unlock 65376f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle ASSERT_EQ(0, pthread_rwlock_wrlock(&l)); 65476f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle ASSERT_EQ(0, pthread_rwlock_unlock(&l)); 65576f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle 65676f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle#ifdef __BIONIC__ 65776f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle // EDEADLK in "read after write" 65876f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle ASSERT_EQ(0, pthread_rwlock_wrlock(&l)); 65976f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle ASSERT_EQ(EDEADLK, pthread_rwlock_rdlock(&l)); 66076f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle ASSERT_EQ(0, pthread_rwlock_unlock(&l)); 66176f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle 66276f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle // EDEADLK in "write after write" 663c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes ASSERT_EQ(0, pthread_rwlock_wrlock(&l)); 66476f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle ASSERT_EQ(EDEADLK, pthread_rwlock_wrlock(&l)); 665c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes ASSERT_EQ(0, pthread_rwlock_unlock(&l)); 66676f352eec12d8938101e5ae33429c72797c3aa23Calin Juravle#endif 667c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes 668c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes ASSERT_EQ(0, pthread_rwlock_destroy(&l)); 669c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes} 670c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes 6711728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic int g_once_fn_call_count = 0; 672c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughesstatic void OnceFn() { 6731728b2396591853345507a063ed6075dfd251706Elliott Hughes ++g_once_fn_call_count; 674c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes} 675c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes 676c3f114037dbf028896310609fd28cf2b3da99c4dElliott HughesTEST(pthread, pthread_once_smoke) { 677c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes pthread_once_t once_control = PTHREAD_ONCE_INIT; 678c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes ASSERT_EQ(0, pthread_once(&once_control, OnceFn)); 679c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes ASSERT_EQ(0, pthread_once(&once_control, OnceFn)); 6801728b2396591853345507a063ed6075dfd251706Elliott Hughes ASSERT_EQ(1, g_once_fn_call_count); 681c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes} 682c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes 6833694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughesstatic std::string pthread_once_1934122_result = ""; 6843694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes 6853694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughesstatic void Routine2() { 6863694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes pthread_once_1934122_result += "2"; 6873694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes} 6883694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes 6893694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughesstatic void Routine1() { 6903694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes pthread_once_t once_control_2 = PTHREAD_ONCE_INIT; 6913694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes pthread_once_1934122_result += "1"; 6923694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes pthread_once(&once_control_2, &Routine2); 6933694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes} 6943694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes 6953694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott HughesTEST(pthread, pthread_once_1934122) { 6963694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes // Very old versions of Android couldn't call pthread_once from a 6973694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes // pthread_once init routine. http://b/1934122. 6983694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes pthread_once_t once_control_1 = PTHREAD_ONCE_INIT; 6993694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes ASSERT_EQ(0, pthread_once(&once_control_1, &Routine1)); 7003694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes ASSERT_EQ("12", pthread_once_1934122_result); 7013694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes} 7023694ec6c4b644064f7e00b898cd11e138e4f6c09Elliott Hughes 7031728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic int g_atfork_prepare_calls = 0; 7041728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic void AtForkPrepare1() { g_atfork_prepare_calls = (g_atfork_prepare_calls << 4) | 1; } 7051728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic void AtForkPrepare2() { g_atfork_prepare_calls = (g_atfork_prepare_calls << 4) | 2; } 7061728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic int g_atfork_parent_calls = 0; 7071728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic void AtForkParent1() { g_atfork_parent_calls = (g_atfork_parent_calls << 4) | 1; } 7081728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic void AtForkParent2() { g_atfork_parent_calls = (g_atfork_parent_calls << 4) | 2; } 7091728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic int g_atfork_child_calls = 0; 7101728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic void AtForkChild1() { g_atfork_child_calls = (g_atfork_child_calls << 4) | 1; } 7111728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic void AtForkChild2() { g_atfork_child_calls = (g_atfork_child_calls << 4) | 2; } 712c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes 713c3f114037dbf028896310609fd28cf2b3da99c4dElliott HughesTEST(pthread, pthread_atfork) { 714c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1)); 715c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes ASSERT_EQ(0, pthread_atfork(AtForkPrepare2, AtForkParent2, AtForkChild2)); 716c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes 717c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes int pid = fork(); 718c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes ASSERT_NE(-1, pid) << strerror(errno); 719c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes 720c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes // Child and parent calls are made in the order they were registered. 721c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes if (pid == 0) { 7221728b2396591853345507a063ed6075dfd251706Elliott Hughes ASSERT_EQ(0x12, g_atfork_child_calls); 723c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes _exit(0); 724c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes } 7251728b2396591853345507a063ed6075dfd251706Elliott Hughes ASSERT_EQ(0x12, g_atfork_parent_calls); 726c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes 727c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes // Prepare calls are made in the reverse order. 7281728b2396591853345507a063ed6075dfd251706Elliott Hughes ASSERT_EQ(0x21, g_atfork_prepare_calls); 729c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes} 730c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes 731c3f114037dbf028896310609fd28cf2b3da99c4dElliott HughesTEST(pthread, pthread_attr_getscope) { 732c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes pthread_attr_t attr; 733c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes ASSERT_EQ(0, pthread_attr_init(&attr)); 734c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes 735c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes int scope; 736c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes ASSERT_EQ(0, pthread_attr_getscope(&attr, &scope)); 737c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes ASSERT_EQ(PTHREAD_SCOPE_SYSTEM, scope); 738c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes} 73951e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath 74051e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan KamathTEST(pthread, pthread_condattr_init) { 74151e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath pthread_condattr_t attr; 74251e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath pthread_condattr_init(&attr); 74351e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath 74451e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath clockid_t clock; 74551e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock)); 74651e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath ASSERT_EQ(CLOCK_REALTIME, clock); 74751e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath 74851e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath int pshared; 74951e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared)); 75051e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath ASSERT_EQ(PTHREAD_PROCESS_PRIVATE, pshared); 75151e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath} 75251e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath 75351e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan KamathTEST(pthread, pthread_condattr_setclock) { 75451e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath pthread_condattr_t attr; 75551e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath pthread_condattr_init(&attr); 75651e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath 75751e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_REALTIME)); 75851e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath clockid_t clock; 75951e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock)); 76051e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath ASSERT_EQ(CLOCK_REALTIME, clock); 76151e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath 76251e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC)); 76351e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock)); 76451e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath ASSERT_EQ(CLOCK_MONOTONIC, clock); 76551e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath 76651e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath ASSERT_EQ(EINVAL, pthread_condattr_setclock(&attr, CLOCK_PROCESS_CPUTIME_ID)); 76751e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath} 76851e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath 76951e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan KamathTEST(pthread, pthread_cond_broadcast__preserves_condattr_flags) { 77051e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath#if defined(__BIONIC__) // This tests a bionic implementation detail. 77151e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath pthread_condattr_t attr; 77251e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath pthread_condattr_init(&attr); 77351e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath 77451e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC)); 77551e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath ASSERT_EQ(0, pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED)); 77651e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath 77751e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath pthread_cond_t cond_var; 77851e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath ASSERT_EQ(0, pthread_cond_init(&cond_var, &attr)); 77951e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath 78051e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath ASSERT_EQ(0, pthread_cond_signal(&cond_var)); 78151e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath ASSERT_EQ(0, pthread_cond_broadcast(&cond_var)); 78251e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath 78351e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath attr = static_cast<pthread_condattr_t>(cond_var.value); 78451e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath clockid_t clock; 78551e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock)); 78651e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath ASSERT_EQ(CLOCK_MONOTONIC, clock); 78751e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath int pshared; 78851e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared)); 78951e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath ASSERT_EQ(PTHREAD_PROCESS_SHARED, pshared); 79051e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath#else // __BIONIC__ 79151e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath GTEST_LOG_(INFO) << "This test does nothing.\n"; 79251e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath#endif // __BIONIC__ 79351e6cb33e3d7c2f44864d356a2a8e66317688f55Narayan Kamath} 7940e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes 7950e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott HughesTEST(pthread, pthread_mutex_timedlock) { 7960e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes pthread_mutex_t m; 7970e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes ASSERT_EQ(0, pthread_mutex_init(&m, NULL)); 7980e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes 7990e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes // If the mutex is already locked, pthread_mutex_timedlock should time out. 8000e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes ASSERT_EQ(0, pthread_mutex_lock(&m)); 8010e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes 8020e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes timespec ts; 8030e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts)); 8040e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes ts.tv_nsec += 1; 8050e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes ASSERT_EQ(ETIMEDOUT, pthread_mutex_timedlock(&m, &ts)); 8060e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes 8070e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes // If the mutex is unlocked, pthread_mutex_timedlock should succeed. 8080e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes ASSERT_EQ(0, pthread_mutex_unlock(&m)); 8090e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes 8100e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts)); 8110e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes ts.tv_nsec += 1; 8120e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes ASSERT_EQ(0, pthread_mutex_timedlock(&m, &ts)); 8130e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes 8140e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes ASSERT_EQ(0, pthread_mutex_unlock(&m)); 8150e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes ASSERT_EQ(0, pthread_mutex_destroy(&m)); 8160e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes} 81757b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes 81857b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott HughesTEST(pthread, pthread_attr_getstack__main_thread) { 81957b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes // This test is only meaningful for the main thread, so make sure we're running on it! 82057b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes ASSERT_EQ(getpid(), syscall(__NR_gettid)); 82157b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes 82257b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes // Get the main thread's attributes. 82357b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes pthread_attr_t attributes; 82457b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes)); 82557b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes 82657b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes // Check that we correctly report that the main thread has no guard page. 82757b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes size_t guard_size; 82857b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size)); 82957b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes ASSERT_EQ(0U, guard_size); // The main thread has no guard page. 83057b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes 83157b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes // Get the stack base and the stack size (both ways). 83257b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes void* stack_base; 83357b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes size_t stack_size; 83457b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size)); 83557b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes size_t stack_size2; 83657b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2)); 83757b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes 83857b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes // The two methods of asking for the stack size should agree. 83957b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes EXPECT_EQ(stack_size, stack_size2); 84057b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes 84157b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes // What does /proc/self/maps' [stack] line say? 8429e4ffa7032eaab308876b8e3da86b05c3c613878Elliott Hughes void* maps_stack_hi = NULL; 84357b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes FILE* fp = fopen("/proc/self/maps", "r"); 84457b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes ASSERT_TRUE(fp != NULL); 84557b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes char line[BUFSIZ]; 84657b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes while (fgets(line, sizeof(line), fp) != NULL) { 84757b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes uintptr_t lo, hi; 84857b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes char name[10]; 84957b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes sscanf(line, "%" PRIxPTR "-%" PRIxPTR " %*4s %*x %*x:%*x %*d %10s", &lo, &hi, name); 85057b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes if (strcmp(name, "[stack]") == 0) { 8519e4ffa7032eaab308876b8e3da86b05c3c613878Elliott Hughes maps_stack_hi = reinterpret_cast<void*>(hi); 85257b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes break; 85357b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes } 85457b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes } 85557b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes fclose(fp); 85657b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes 8579e4ffa7032eaab308876b8e3da86b05c3c613878Elliott Hughes // The stack size should correspond to RLIMIT_STACK. 8589e4ffa7032eaab308876b8e3da86b05c3c613878Elliott Hughes rlimit rl; 8599e4ffa7032eaab308876b8e3da86b05c3c613878Elliott Hughes ASSERT_EQ(0, getrlimit(RLIMIT_STACK, &rl)); 86027a9aed81978af792cb06035a1619c8141a5fb5bElliott Hughes uint64_t original_rlim_cur = rl.rlim_cur; 86127a9aed81978af792cb06035a1619c8141a5fb5bElliott Hughes#if defined(__BIONIC__) 86227a9aed81978af792cb06035a1619c8141a5fb5bElliott Hughes if (rl.rlim_cur == RLIM_INFINITY) { 86327a9aed81978af792cb06035a1619c8141a5fb5bElliott Hughes rl.rlim_cur = 8 * 1024 * 1024; // Bionic reports unlimited stacks as 8MiB. 86427a9aed81978af792cb06035a1619c8141a5fb5bElliott Hughes } 86527a9aed81978af792cb06035a1619c8141a5fb5bElliott Hughes#endif 8669e4ffa7032eaab308876b8e3da86b05c3c613878Elliott Hughes EXPECT_EQ(rl.rlim_cur, stack_size); 86757b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes 868d9ff7226613014056c9edd79a68dc5af939107a0Dmitriy Ivanov auto guard = make_scope_guard([&rl, original_rlim_cur]() { 86927a9aed81978af792cb06035a1619c8141a5fb5bElliott Hughes rl.rlim_cur = original_rlim_cur; 87027a9aed81978af792cb06035a1619c8141a5fb5bElliott Hughes ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl)); 87127a9aed81978af792cb06035a1619c8141a5fb5bElliott Hughes }); 87227a9aed81978af792cb06035a1619c8141a5fb5bElliott Hughes 8739e4ffa7032eaab308876b8e3da86b05c3c613878Elliott Hughes // The high address of the /proc/self/maps [stack] region should equal stack_base + stack_size. 8749e4ffa7032eaab308876b8e3da86b05c3c613878Elliott Hughes // Remember that the stack grows down (and is mapped in on demand), so the low address of the 8759e4ffa7032eaab308876b8e3da86b05c3c613878Elliott Hughes // region isn't very interesting. 8769e4ffa7032eaab308876b8e3da86b05c3c613878Elliott Hughes EXPECT_EQ(maps_stack_hi, reinterpret_cast<uint8_t*>(stack_base) + stack_size); 87757b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes 87857b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes // 8799e4ffa7032eaab308876b8e3da86b05c3c613878Elliott Hughes // What if RLIMIT_STACK is smaller than the stack's current extent? 88057b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes // 88157b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes rl.rlim_cur = rl.rlim_max = 1024; // 1KiB. We know the stack must be at least a page already. 88257b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes rl.rlim_max = RLIM_INFINITY; 88357b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl)); 88457b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes 88557b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes)); 88657b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size)); 88757b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2)); 88857b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes 88957b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes EXPECT_EQ(stack_size, stack_size2); 89057b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes ASSERT_EQ(1024U, stack_size); 89157b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes 89257b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes // 8939e4ffa7032eaab308876b8e3da86b05c3c613878Elliott Hughes // What if RLIMIT_STACK isn't a whole number of pages? 89457b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes // 89557b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes rl.rlim_cur = rl.rlim_max = 6666; // Not a whole number of pages. 89657b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes rl.rlim_max = RLIM_INFINITY; 89757b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl)); 89857b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes 89957b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes)); 90057b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size)); 90157b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2)); 90257b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes 90357b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes EXPECT_EQ(stack_size, stack_size2); 90457b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes ASSERT_EQ(6666U, stack_size); 90557b7a6110e7e8b446fc23cce4765ff625ee0a105Elliott Hughes} 9068fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughes 9078fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughes#if defined(__BIONIC__) 9088fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughesstatic void* pthread_gettid_np_helper(void* arg) { 9098fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughes *reinterpret_cast<pid_t*>(arg) = gettid(); 9108fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughes return NULL; 9118fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughes} 9128fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughes#endif 9138fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughes 9148fb639ca9118a6522723d0bc09db59b432a803a9Elliott HughesTEST(pthread, pthread_gettid_np) { 9158fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughes#if defined(__BIONIC__) 9168fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughes ASSERT_EQ(gettid(), pthread_gettid_np(pthread_self())); 9178fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughes 9188fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughes pid_t t_gettid_result; 9198fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughes pthread_t t; 9208fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughes pthread_create(&t, NULL, pthread_gettid_np_helper, &t_gettid_result); 9218fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughes 9228fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughes pid_t t_pthread_gettid_np_result = pthread_gettid_np(t); 9238fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughes 92434c987a6dd6816eff98bc25f627659550c2338dcElliott Hughes pthread_join(t, NULL); 9258fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughes 9268fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughes ASSERT_EQ(t_gettid_result, t_pthread_gettid_np_result); 9278fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughes#else 9288fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughes GTEST_LOG_(INFO) << "This test does nothing.\n"; 9298fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughes#endif 9308fb639ca9118a6522723d0bc09db59b432a803a9Elliott Hughes} 93134c987a6dd6816eff98bc25f627659550c2338dcElliott Hughes 93234c987a6dd6816eff98bc25f627659550c2338dcElliott Hughesstatic size_t cleanup_counter = 0; 93334c987a6dd6816eff98bc25f627659550c2338dcElliott Hughes 93434c987a6dd6816eff98bc25f627659550c2338dcElliott Hughesvoid AbortCleanupRoutine(void*) { 93534c987a6dd6816eff98bc25f627659550c2338dcElliott Hughes abort(); 93634c987a6dd6816eff98bc25f627659550c2338dcElliott Hughes} 93734c987a6dd6816eff98bc25f627659550c2338dcElliott Hughes 93834c987a6dd6816eff98bc25f627659550c2338dcElliott Hughesvoid CountCleanupRoutine(void*) { 93934c987a6dd6816eff98bc25f627659550c2338dcElliott Hughes ++cleanup_counter; 94034c987a6dd6816eff98bc25f627659550c2338dcElliott Hughes} 94134c987a6dd6816eff98bc25f627659550c2338dcElliott Hughes 94234c987a6dd6816eff98bc25f627659550c2338dcElliott Hughesvoid PthreadCleanupTester() { 94334c987a6dd6816eff98bc25f627659550c2338dcElliott Hughes pthread_cleanup_push(CountCleanupRoutine, NULL); 94434c987a6dd6816eff98bc25f627659550c2338dcElliott Hughes pthread_cleanup_push(CountCleanupRoutine, NULL); 94534c987a6dd6816eff98bc25f627659550c2338dcElliott Hughes pthread_cleanup_push(AbortCleanupRoutine, NULL); 94634c987a6dd6816eff98bc25f627659550c2338dcElliott Hughes 94734c987a6dd6816eff98bc25f627659550c2338dcElliott Hughes pthread_cleanup_pop(0); // Pop the abort without executing it. 94834c987a6dd6816eff98bc25f627659550c2338dcElliott Hughes pthread_cleanup_pop(1); // Pop one count while executing it. 94934c987a6dd6816eff98bc25f627659550c2338dcElliott Hughes ASSERT_EQ(1U, cleanup_counter); 95034c987a6dd6816eff98bc25f627659550c2338dcElliott Hughes // Exit while the other count is still on the cleanup stack. 95134c987a6dd6816eff98bc25f627659550c2338dcElliott Hughes pthread_exit(NULL); 95234c987a6dd6816eff98bc25f627659550c2338dcElliott Hughes 95334c987a6dd6816eff98bc25f627659550c2338dcElliott Hughes // Calls to pthread_cleanup_pop/pthread_cleanup_push must always be balanced. 95434c987a6dd6816eff98bc25f627659550c2338dcElliott Hughes pthread_cleanup_pop(0); 95534c987a6dd6816eff98bc25f627659550c2338dcElliott Hughes} 95634c987a6dd6816eff98bc25f627659550c2338dcElliott Hughes 95734c987a6dd6816eff98bc25f627659550c2338dcElliott Hughesvoid* PthreadCleanupStartRoutine(void*) { 95834c987a6dd6816eff98bc25f627659550c2338dcElliott Hughes PthreadCleanupTester(); 95934c987a6dd6816eff98bc25f627659550c2338dcElliott Hughes return NULL; 96034c987a6dd6816eff98bc25f627659550c2338dcElliott Hughes} 96134c987a6dd6816eff98bc25f627659550c2338dcElliott Hughes 96234c987a6dd6816eff98bc25f627659550c2338dcElliott HughesTEST(pthread, pthread_cleanup_push__pthread_cleanup_pop) { 96334c987a6dd6816eff98bc25f627659550c2338dcElliott Hughes pthread_t t; 96434c987a6dd6816eff98bc25f627659550c2338dcElliott Hughes ASSERT_EQ(0, pthread_create(&t, NULL, PthreadCleanupStartRoutine, NULL)); 96534c987a6dd6816eff98bc25f627659550c2338dcElliott Hughes pthread_join(t, NULL); 96634c987a6dd6816eff98bc25f627659550c2338dcElliott Hughes ASSERT_EQ(2U, cleanup_counter); 96734c987a6dd6816eff98bc25f627659550c2338dcElliott Hughes} 968