Elliott Hughes | bfeab1b | 2012-09-05 17:47:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <gtest/gtest.h> |
| 18 | |
| 19 | #include <errno.h> |
| 20 | #include <pthread.h> |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 21 | #include <unistd.h> |
Elliott Hughes | bfeab1b | 2012-09-05 17:47:37 -0700 | [diff] [blame] | 22 | |
| 23 | TEST(pthread, pthread_key_create) { |
| 24 | pthread_key_t key; |
| 25 | ASSERT_EQ(0, pthread_key_create(&key, NULL)); |
| 26 | ASSERT_EQ(0, pthread_key_delete(key)); |
| 27 | // Can't delete a key that's already been deleted. |
| 28 | ASSERT_EQ(EINVAL, pthread_key_delete(key)); |
| 29 | } |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 30 | |
Elliott Hughes | ad59322 | 2013-02-08 15:46:37 -0800 | [diff] [blame] | 31 | TEST(pthread, pthread_key_create_lots) { |
| 32 | // We can allocate _SC_THREAD_KEYS_MAX keys. |
| 33 | std::vector<pthread_key_t> keys; |
| 34 | for (int i = 0; i < sysconf(_SC_THREAD_KEYS_MAX); ++i) { |
| 35 | pthread_key_t key; |
| 36 | ASSERT_EQ(0, pthread_key_create(&key, NULL)); |
| 37 | keys.push_back(key); |
| 38 | } |
| 39 | |
| 40 | // ...and that really is the maximum. |
| 41 | pthread_key_t key; |
| 42 | ASSERT_EQ(EAGAIN, pthread_key_create(&key, NULL)); |
| 43 | |
| 44 | // (Don't leak all those keys!) |
| 45 | for (size_t i = 0; i < keys.size(); ++i) { |
| 46 | ASSERT_EQ(0, pthread_key_delete(keys[i])); |
| 47 | } |
| 48 | } |
| 49 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 50 | static void* IdFn(void* arg) { |
| 51 | return arg; |
| 52 | } |
| 53 | |
| 54 | static void* SleepFn(void* arg) { |
| 55 | sleep(reinterpret_cast<unsigned int>(arg)); |
| 56 | return NULL; |
| 57 | } |
| 58 | |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 59 | static void* SpinFn(void* arg) { |
| 60 | volatile bool* b = reinterpret_cast<volatile bool*>(arg); |
| 61 | while (!*b) { |
| 62 | } |
| 63 | return NULL; |
| 64 | } |
| 65 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 66 | static void* JoinFn(void* arg) { |
| 67 | return reinterpret_cast<void*>(pthread_join(reinterpret_cast<pthread_t>(arg), NULL)); |
| 68 | } |
| 69 | |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 70 | static void AssertDetached(pthread_t t, bool is_detached) { |
| 71 | pthread_attr_t attr; |
| 72 | ASSERT_EQ(0, pthread_getattr_np(t, &attr)); |
| 73 | int detach_state; |
| 74 | ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &detach_state)); |
| 75 | pthread_attr_destroy(&attr); |
| 76 | ASSERT_EQ(is_detached, (detach_state == PTHREAD_CREATE_DETACHED)); |
| 77 | } |
| 78 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 79 | TEST(pthread, pthread_create) { |
| 80 | void* expected_result = reinterpret_cast<void*>(123); |
| 81 | // Can we create a thread? |
| 82 | pthread_t t; |
| 83 | ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, expected_result)); |
| 84 | // If we join, do we get the expected value back? |
| 85 | void* result; |
| 86 | ASSERT_EQ(0, pthread_join(t, &result)); |
| 87 | ASSERT_EQ(expected_result, result); |
| 88 | } |
| 89 | |
| 90 | TEST(pthread, pthread_no_join_after_detach) { |
| 91 | pthread_t t1; |
| 92 | ASSERT_EQ(0, pthread_create(&t1, NULL, SleepFn, reinterpret_cast<void*>(5))); |
| 93 | |
| 94 | // After a pthread_detach... |
| 95 | ASSERT_EQ(0, pthread_detach(t1)); |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 96 | AssertDetached(t1, true); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 97 | |
| 98 | // ...pthread_join should fail. |
| 99 | void* result; |
| 100 | ASSERT_EQ(EINVAL, pthread_join(t1, &result)); |
| 101 | } |
| 102 | |
| 103 | TEST(pthread, pthread_no_op_detach_after_join) { |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 104 | bool done = false; |
| 105 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 106 | pthread_t t1; |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 107 | ASSERT_EQ(0, pthread_create(&t1, NULL, SpinFn, &done)); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 108 | |
| 109 | // If thread 2 is already waiting to join thread 1... |
| 110 | pthread_t t2; |
| 111 | ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1))); |
| 112 | |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 113 | sleep(1); // (Give t2 a chance to call pthread_join.) |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 114 | |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 115 | // ...a call to pthread_detach on thread 1 will "succeed" (silently fail)... |
| 116 | ASSERT_EQ(0, pthread_detach(t1)); |
| 117 | AssertDetached(t1, false); |
| 118 | |
| 119 | done = true; |
| 120 | |
| 121 | // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes). |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 122 | void* join_result; |
| 123 | ASSERT_EQ(0, pthread_join(t2, &join_result)); |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 124 | ASSERT_EQ(0, reinterpret_cast<int>(join_result)); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 125 | } |
Elliott Hughes | 14f1959 | 2012-10-29 10:19:44 -0700 | [diff] [blame] | 126 | |
| 127 | TEST(pthread, pthread_join_self) { |
| 128 | void* result; |
| 129 | ASSERT_EQ(EDEADLK, pthread_join(pthread_self(), &result)); |
| 130 | } |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 131 | |
| 132 | #if __BIONIC__ // For some reason, gtest on bionic can cope with this but gtest on glibc can't. |
| 133 | |
| 134 | static void TestBug37410() { |
| 135 | pthread_t t1; |
| 136 | ASSERT_EQ(0, pthread_create(&t1, NULL, JoinFn, reinterpret_cast<void*>(pthread_self()))); |
| 137 | pthread_exit(NULL); |
| 138 | } |
| 139 | |
| 140 | // We have to say "DeathTest" here so gtest knows to run this test (which exits) |
| 141 | // in its own process. |
| 142 | TEST(pthread_DeathTest, pthread_bug_37410) { |
| 143 | // http://code.google.com/p/android/issues/detail?id=37410 |
| 144 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 145 | EXPECT_EXIT(TestBug37410(), ::testing::ExitedWithCode(0), ""); |
| 146 | } |
| 147 | #endif |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 148 | |
| 149 | static void* SignalHandlerFn(void* arg) { |
| 150 | sigset_t wait_set; |
| 151 | sigfillset(&wait_set); |
| 152 | return reinterpret_cast<void*>(sigwait(&wait_set, reinterpret_cast<int*>(arg))); |
| 153 | } |
| 154 | |
| 155 | TEST(pthread, pthread_sigmask) { |
| 156 | // Block SIGUSR1. |
| 157 | sigset_t set; |
| 158 | sigemptyset(&set); |
| 159 | sigaddset(&set, SIGUSR1); |
| 160 | ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &set, NULL)); |
| 161 | |
| 162 | // Spawn a thread that calls sigwait and tells us what it received. |
| 163 | pthread_t signal_thread; |
| 164 | int received_signal = -1; |
| 165 | ASSERT_EQ(0, pthread_create(&signal_thread, NULL, SignalHandlerFn, &received_signal)); |
| 166 | |
| 167 | // Send that thread SIGUSR1. |
| 168 | pthread_kill(signal_thread, SIGUSR1); |
| 169 | |
| 170 | // See what it got. |
| 171 | void* join_result; |
| 172 | ASSERT_EQ(0, pthread_join(signal_thread, &join_result)); |
| 173 | ASSERT_EQ(SIGUSR1, received_signal); |
| 174 | ASSERT_EQ(0, reinterpret_cast<int>(join_result)); |
| 175 | } |