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> |
Elliott Hughes | 5b9310e | 2013-10-02 16:59:05 -0700 | [diff] [blame] | 20 | #include <inttypes.h> |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 21 | #include <limits.h> |
Elliott Hughes | 04620a3 | 2014-03-07 17:59:05 -0800 | [diff] [blame] | 22 | #include <malloc.h> |
Elliott Hughes | bfeab1b | 2012-09-05 17:47:37 -0700 | [diff] [blame] | 23 | #include <pthread.h> |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 24 | #include <signal.h> |
Elliott Hughes | 70b24b1 | 2013-11-15 11:51:07 -0800 | [diff] [blame] | 25 | #include <sys/mman.h> |
Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 26 | #include <time.h> |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 27 | #include <unistd.h> |
Elliott Hughes | bfeab1b | 2012-09-05 17:47:37 -0700 | [diff] [blame] | 28 | |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 29 | #include "ScopedSignalHandler.h" |
| 30 | |
Elliott Hughes | bfeab1b | 2012-09-05 17:47:37 -0700 | [diff] [blame] | 31 | TEST(pthread, pthread_key_create) { |
| 32 | pthread_key_t key; |
| 33 | ASSERT_EQ(0, pthread_key_create(&key, NULL)); |
| 34 | ASSERT_EQ(0, pthread_key_delete(key)); |
| 35 | // Can't delete a key that's already been deleted. |
| 36 | ASSERT_EQ(EINVAL, pthread_key_delete(key)); |
| 37 | } |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 38 | |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 39 | TEST(pthread, pthread_key_create_lots) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 40 | #if defined(__BIONIC__) // glibc uses keys internally that its sysconf value doesn't account for. |
Elliott Hughes | 1887621 | 2013-12-12 11:02:41 -0800 | [diff] [blame] | 41 | // POSIX says PTHREAD_KEYS_MAX should be at least 128. |
| 42 | ASSERT_GE(PTHREAD_KEYS_MAX, 128); |
Elliott Hughes | 718a5b5 | 2014-01-28 17:02:03 -0800 | [diff] [blame] | 43 | |
| 44 | int sysconf_max = sysconf(_SC_THREAD_KEYS_MAX); |
| 45 | |
Elliott Hughes | 1887621 | 2013-12-12 11:02:41 -0800 | [diff] [blame] | 46 | // sysconf shouldn't return a smaller value. |
Elliott Hughes | 718a5b5 | 2014-01-28 17:02:03 -0800 | [diff] [blame] | 47 | ASSERT_GE(sysconf_max, PTHREAD_KEYS_MAX); |
Elliott Hughes | 1887621 | 2013-12-12 11:02:41 -0800 | [diff] [blame] | 48 | |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 49 | // We can allocate _SC_THREAD_KEYS_MAX keys. |
Elliott Hughes | 718a5b5 | 2014-01-28 17:02:03 -0800 | [diff] [blame] | 50 | sysconf_max -= 2; // (Except that gtest takes two for itself.) |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 51 | std::vector<pthread_key_t> keys; |
Elliott Hughes | 718a5b5 | 2014-01-28 17:02:03 -0800 | [diff] [blame] | 52 | for (int i = 0; i < sysconf_max; ++i) { |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 53 | pthread_key_t key; |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 54 | // If this fails, it's likely that GLOBAL_INIT_THREAD_LOCAL_BUFFER_COUNT is wrong. |
Elliott Hughes | 718a5b5 | 2014-01-28 17:02:03 -0800 | [diff] [blame] | 55 | ASSERT_EQ(0, pthread_key_create(&key, NULL)) << i << " of " << sysconf_max; |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 56 | keys.push_back(key); |
| 57 | } |
| 58 | |
| 59 | // ...and that really is the maximum. |
| 60 | pthread_key_t key; |
| 61 | ASSERT_EQ(EAGAIN, pthread_key_create(&key, NULL)); |
| 62 | |
| 63 | // (Don't leak all those keys!) |
| 64 | for (size_t i = 0; i < keys.size(); ++i) { |
| 65 | ASSERT_EQ(0, pthread_key_delete(keys[i])); |
| 66 | } |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 67 | #else // __BIONIC__ |
| 68 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 69 | #endif // __BIONIC__ |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 72 | static void* IdFn(void* arg) { |
| 73 | return arg; |
| 74 | } |
| 75 | |
| 76 | static void* SleepFn(void* arg) { |
Elliott Hughes | 5b9310e | 2013-10-02 16:59:05 -0700 | [diff] [blame] | 77 | sleep(reinterpret_cast<uintptr_t>(arg)); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 78 | return NULL; |
| 79 | } |
| 80 | |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 81 | static void* SpinFn(void* arg) { |
| 82 | volatile bool* b = reinterpret_cast<volatile bool*>(arg); |
| 83 | while (!*b) { |
| 84 | } |
| 85 | return NULL; |
| 86 | } |
| 87 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 88 | static void* JoinFn(void* arg) { |
| 89 | return reinterpret_cast<void*>(pthread_join(reinterpret_cast<pthread_t>(arg), NULL)); |
| 90 | } |
| 91 | |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 92 | static void AssertDetached(pthread_t t, bool is_detached) { |
| 93 | pthread_attr_t attr; |
| 94 | ASSERT_EQ(0, pthread_getattr_np(t, &attr)); |
| 95 | int detach_state; |
| 96 | ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &detach_state)); |
| 97 | pthread_attr_destroy(&attr); |
| 98 | ASSERT_EQ(is_detached, (detach_state == PTHREAD_CREATE_DETACHED)); |
| 99 | } |
| 100 | |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 101 | static void MakeDeadThread(pthread_t& t) { |
| 102 | ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, NULL)); |
| 103 | void* result; |
| 104 | ASSERT_EQ(0, pthread_join(t, &result)); |
| 105 | } |
| 106 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 107 | TEST(pthread, pthread_create) { |
| 108 | void* expected_result = reinterpret_cast<void*>(123); |
| 109 | // Can we create a thread? |
| 110 | pthread_t t; |
| 111 | ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, expected_result)); |
| 112 | // If we join, do we get the expected value back? |
| 113 | void* result; |
| 114 | ASSERT_EQ(0, pthread_join(t, &result)); |
| 115 | ASSERT_EQ(expected_result, result); |
| 116 | } |
| 117 | |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 118 | TEST(pthread, pthread_create_EAGAIN) { |
| 119 | pthread_attr_t attributes; |
| 120 | ASSERT_EQ(0, pthread_attr_init(&attributes)); |
| 121 | ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, static_cast<size_t>(-1) & ~(getpagesize() - 1))); |
| 122 | |
| 123 | pthread_t t; |
| 124 | ASSERT_EQ(EAGAIN, pthread_create(&t, &attributes, IdFn, NULL)); |
| 125 | } |
| 126 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 127 | TEST(pthread, pthread_no_join_after_detach) { |
| 128 | pthread_t t1; |
| 129 | ASSERT_EQ(0, pthread_create(&t1, NULL, SleepFn, reinterpret_cast<void*>(5))); |
| 130 | |
| 131 | // After a pthread_detach... |
| 132 | ASSERT_EQ(0, pthread_detach(t1)); |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 133 | AssertDetached(t1, true); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 134 | |
| 135 | // ...pthread_join should fail. |
| 136 | void* result; |
| 137 | ASSERT_EQ(EINVAL, pthread_join(t1, &result)); |
| 138 | } |
| 139 | |
| 140 | TEST(pthread, pthread_no_op_detach_after_join) { |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 141 | bool done = false; |
| 142 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 143 | pthread_t t1; |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 144 | ASSERT_EQ(0, pthread_create(&t1, NULL, SpinFn, &done)); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 145 | |
| 146 | // If thread 2 is already waiting to join thread 1... |
| 147 | pthread_t t2; |
| 148 | ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1))); |
| 149 | |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 150 | sleep(1); // (Give t2 a chance to call pthread_join.) |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 151 | |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 152 | // ...a call to pthread_detach on thread 1 will "succeed" (silently fail)... |
| 153 | ASSERT_EQ(0, pthread_detach(t1)); |
| 154 | AssertDetached(t1, false); |
| 155 | |
| 156 | done = true; |
| 157 | |
| 158 | // ...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] | 159 | void* join_result; |
| 160 | ASSERT_EQ(0, pthread_join(t2, &join_result)); |
Elliott Hughes | 5b9310e | 2013-10-02 16:59:05 -0700 | [diff] [blame] | 161 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result)); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 162 | } |
Elliott Hughes | 14f1959 | 2012-10-29 10:19:44 -0700 | [diff] [blame] | 163 | |
| 164 | TEST(pthread, pthread_join_self) { |
| 165 | void* result; |
| 166 | ASSERT_EQ(EDEADLK, pthread_join(pthread_self(), &result)); |
| 167 | } |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 168 | |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 169 | struct TestBug37410 { |
| 170 | pthread_t main_thread; |
| 171 | pthread_mutex_t mutex; |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 172 | |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 173 | static void main() { |
| 174 | TestBug37410 data; |
| 175 | data.main_thread = pthread_self(); |
| 176 | ASSERT_EQ(0, pthread_mutex_init(&data.mutex, NULL)); |
| 177 | ASSERT_EQ(0, pthread_mutex_lock(&data.mutex)); |
| 178 | |
| 179 | pthread_t t; |
| 180 | ASSERT_EQ(0, pthread_create(&t, NULL, TestBug37410::thread_fn, reinterpret_cast<void*>(&data))); |
| 181 | |
| 182 | // Wait for the thread to be running... |
| 183 | ASSERT_EQ(0, pthread_mutex_lock(&data.mutex)); |
| 184 | ASSERT_EQ(0, pthread_mutex_unlock(&data.mutex)); |
| 185 | |
| 186 | // ...and exit. |
| 187 | pthread_exit(NULL); |
| 188 | } |
| 189 | |
| 190 | private: |
| 191 | static void* thread_fn(void* arg) { |
| 192 | TestBug37410* data = reinterpret_cast<TestBug37410*>(arg); |
| 193 | |
| 194 | // Let the main thread know we're running. |
| 195 | pthread_mutex_unlock(&data->mutex); |
| 196 | |
| 197 | // And wait for the main thread to exit. |
| 198 | pthread_join(data->main_thread, NULL); |
| 199 | |
| 200 | return NULL; |
| 201 | } |
| 202 | }; |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 203 | |
Elliott Hughes | 7fd803c | 2013-02-14 16:33:52 -0800 | [diff] [blame] | 204 | // Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to |
| 205 | // run this test (which exits normally) in its own process. |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 206 | TEST(pthread_DeathTest, pthread_bug_37410) { |
| 207 | // http://code.google.com/p/android/issues/detail?id=37410 |
| 208 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 209 | ASSERT_EXIT(TestBug37410::main(), ::testing::ExitedWithCode(0), ""); |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 210 | } |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 211 | |
| 212 | static void* SignalHandlerFn(void* arg) { |
| 213 | sigset_t wait_set; |
| 214 | sigfillset(&wait_set); |
| 215 | return reinterpret_cast<void*>(sigwait(&wait_set, reinterpret_cast<int*>(arg))); |
| 216 | } |
| 217 | |
| 218 | TEST(pthread, pthread_sigmask) { |
Elliott Hughes | 19e6232 | 2013-10-15 11:23:57 -0700 | [diff] [blame] | 219 | // Check that SIGUSR1 isn't blocked. |
| 220 | sigset_t original_set; |
| 221 | sigemptyset(&original_set); |
| 222 | ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &original_set)); |
| 223 | ASSERT_FALSE(sigismember(&original_set, SIGUSR1)); |
| 224 | |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 225 | // Block SIGUSR1. |
| 226 | sigset_t set; |
| 227 | sigemptyset(&set); |
| 228 | sigaddset(&set, SIGUSR1); |
| 229 | ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &set, NULL)); |
| 230 | |
Elliott Hughes | 19e6232 | 2013-10-15 11:23:57 -0700 | [diff] [blame] | 231 | // Check that SIGUSR1 is blocked. |
| 232 | sigset_t final_set; |
| 233 | sigemptyset(&final_set); |
| 234 | ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &final_set)); |
| 235 | ASSERT_TRUE(sigismember(&final_set, SIGUSR1)); |
| 236 | // ...and that sigprocmask agrees with pthread_sigmask. |
| 237 | sigemptyset(&final_set); |
| 238 | ASSERT_EQ(0, sigprocmask(SIG_BLOCK, NULL, &final_set)); |
| 239 | ASSERT_TRUE(sigismember(&final_set, SIGUSR1)); |
| 240 | |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 241 | // Spawn a thread that calls sigwait and tells us what it received. |
| 242 | pthread_t signal_thread; |
| 243 | int received_signal = -1; |
| 244 | ASSERT_EQ(0, pthread_create(&signal_thread, NULL, SignalHandlerFn, &received_signal)); |
| 245 | |
| 246 | // Send that thread SIGUSR1. |
| 247 | pthread_kill(signal_thread, SIGUSR1); |
| 248 | |
| 249 | // See what it got. |
| 250 | void* join_result; |
| 251 | ASSERT_EQ(0, pthread_join(signal_thread, &join_result)); |
| 252 | ASSERT_EQ(SIGUSR1, received_signal); |
Elliott Hughes | 5b9310e | 2013-10-02 16:59:05 -0700 | [diff] [blame] | 253 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result)); |
Elliott Hughes | 19e6232 | 2013-10-15 11:23:57 -0700 | [diff] [blame] | 254 | |
| 255 | // Restore the original signal mask. |
| 256 | ASSERT_EQ(0, pthread_sigmask(SIG_SETMASK, &original_set, NULL)); |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 257 | } |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 258 | |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 259 | TEST(pthread, pthread_setname_np__too_long) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 260 | #if defined(__BIONIC__) // Not all build servers have a new enough glibc? TODO: remove when they're on gprecise. |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 261 | ASSERT_EQ(ERANGE, pthread_setname_np(pthread_self(), "this name is far too long for linux")); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 262 | #else // __BIONIC__ |
| 263 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 264 | #endif // __BIONIC__ |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | TEST(pthread, pthread_setname_np__self) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 268 | #if defined(__BIONIC__) // Not all build servers have a new enough glibc? TODO: remove when they're on gprecise. |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 269 | ASSERT_EQ(0, pthread_setname_np(pthread_self(), "short 1")); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 270 | #else // __BIONIC__ |
| 271 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 272 | #endif // __BIONIC__ |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | TEST(pthread, pthread_setname_np__other) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 276 | #if defined(__BIONIC__) // Not all build servers have a new enough glibc? TODO: remove when they're on gprecise. |
Elliott Hughes | 40eabe2 | 2013-02-14 18:59:37 -0800 | [diff] [blame] | 277 | // Emulator kernels don't currently support setting the name of other threads. |
| 278 | char* filename = NULL; |
| 279 | asprintf(&filename, "/proc/self/task/%d/comm", gettid()); |
| 280 | struct stat sb; |
| 281 | bool has_comm = (stat(filename, &sb) != -1); |
| 282 | free(filename); |
| 283 | |
| 284 | if (has_comm) { |
| 285 | pthread_t t1; |
| 286 | ASSERT_EQ(0, pthread_create(&t1, NULL, SleepFn, reinterpret_cast<void*>(5))); |
| 287 | ASSERT_EQ(0, pthread_setname_np(t1, "short 2")); |
| 288 | } else { |
| 289 | fprintf(stderr, "skipping test: this kernel doesn't have /proc/self/task/tid/comm files!\n"); |
| 290 | } |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 291 | #else // __BIONIC__ |
| 292 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 293 | #endif // __BIONIC__ |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | TEST(pthread, pthread_setname_np__no_such_thread) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 297 | #if defined(__BIONIC__) // Not all build servers have a new enough glibc? TODO: remove when they're on gprecise. |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 298 | pthread_t dead_thread; |
| 299 | MakeDeadThread(dead_thread); |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 300 | |
| 301 | // Call pthread_setname_np after thread has already exited. |
Elliott Hughes | a41ba2f | 2013-03-21 20:02:35 -0700 | [diff] [blame] | 302 | ASSERT_EQ(ESRCH, pthread_setname_np(dead_thread, "short 3")); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 303 | #else // __BIONIC__ |
| 304 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 305 | #endif // __BIONIC__ |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 306 | } |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 307 | |
| 308 | TEST(pthread, pthread_kill__0) { |
| 309 | // Signal 0 just tests that the thread exists, so it's safe to call on ourselves. |
| 310 | ASSERT_EQ(0, pthread_kill(pthread_self(), 0)); |
| 311 | } |
| 312 | |
| 313 | TEST(pthread, pthread_kill__invalid_signal) { |
| 314 | ASSERT_EQ(EINVAL, pthread_kill(pthread_self(), -1)); |
| 315 | } |
| 316 | |
Elliott Hughes | fae89fc | 2013-02-21 11:22:23 -0800 | [diff] [blame] | 317 | static void pthread_kill__in_signal_handler_helper(int signal_number) { |
| 318 | static int count = 0; |
| 319 | ASSERT_EQ(SIGALRM, signal_number); |
| 320 | if (++count == 1) { |
| 321 | // Can we call pthread_kill from a signal handler? |
| 322 | ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM)); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | TEST(pthread, pthread_kill__in_signal_handler) { |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 327 | ScopedSignalHandler ssh(SIGALRM, pthread_kill__in_signal_handler_helper); |
Elliott Hughes | fae89fc | 2013-02-21 11:22:23 -0800 | [diff] [blame] | 328 | ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM)); |
| 329 | } |
| 330 | |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 331 | TEST(pthread, pthread_detach__no_such_thread) { |
| 332 | pthread_t dead_thread; |
| 333 | MakeDeadThread(dead_thread); |
| 334 | |
| 335 | ASSERT_EQ(ESRCH, pthread_detach(dead_thread)); |
| 336 | } |
| 337 | |
Elliott Hughes | 04620a3 | 2014-03-07 17:59:05 -0800 | [diff] [blame] | 338 | TEST(pthread, pthread_detach__leak) { |
| 339 | size_t initial_bytes = mallinfo().uordblks; |
| 340 | |
| 341 | pthread_attr_t attr; |
| 342 | ASSERT_EQ(0, pthread_attr_init(&attr)); |
| 343 | ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE)); |
| 344 | |
| 345 | std::vector<pthread_t> threads; |
| 346 | for (size_t i = 0; i < 32; ++i) { |
| 347 | pthread_t t; |
| 348 | ASSERT_EQ(0, pthread_create(&t, &attr, IdFn, NULL)); |
| 349 | threads.push_back(t); |
| 350 | } |
| 351 | |
| 352 | sleep(1); |
| 353 | |
| 354 | for (size_t i = 0; i < 32; ++i) { |
| 355 | ASSERT_EQ(0, pthread_detach(threads[i])) << i; |
| 356 | } |
| 357 | |
| 358 | size_t final_bytes = mallinfo().uordblks; |
| 359 | |
| 360 | int leaked_bytes = (final_bytes - initial_bytes); |
| 361 | |
| 362 | // User code (like this test) doesn't know how large pthread_internal_t is. |
| 363 | // We can be pretty sure it's more than 128 bytes. |
| 364 | ASSERT_LT(leaked_bytes, 32 /*threads*/ * 128 /*bytes*/); |
| 365 | } |
| 366 | |
Jeff Hao | 9b06cc3 | 2013-08-15 14:51:16 -0700 | [diff] [blame] | 367 | TEST(pthread, pthread_getcpuclockid__clock_gettime) { |
| 368 | pthread_t t; |
| 369 | ASSERT_EQ(0, pthread_create(&t, NULL, SleepFn, reinterpret_cast<void*>(5))); |
| 370 | |
| 371 | clockid_t c; |
| 372 | ASSERT_EQ(0, pthread_getcpuclockid(t, &c)); |
| 373 | timespec ts; |
| 374 | ASSERT_EQ(0, clock_gettime(c, &ts)); |
| 375 | } |
| 376 | |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 377 | TEST(pthread, pthread_getcpuclockid__no_such_thread) { |
| 378 | pthread_t dead_thread; |
| 379 | MakeDeadThread(dead_thread); |
| 380 | |
| 381 | clockid_t c; |
| 382 | ASSERT_EQ(ESRCH, pthread_getcpuclockid(dead_thread, &c)); |
| 383 | } |
| 384 | |
| 385 | TEST(pthread, pthread_getschedparam__no_such_thread) { |
| 386 | pthread_t dead_thread; |
| 387 | MakeDeadThread(dead_thread); |
| 388 | |
| 389 | int policy; |
| 390 | sched_param param; |
| 391 | ASSERT_EQ(ESRCH, pthread_getschedparam(dead_thread, &policy, ¶m)); |
| 392 | } |
| 393 | |
| 394 | TEST(pthread, pthread_setschedparam__no_such_thread) { |
| 395 | pthread_t dead_thread; |
| 396 | MakeDeadThread(dead_thread); |
| 397 | |
| 398 | int policy = 0; |
| 399 | sched_param param; |
| 400 | ASSERT_EQ(ESRCH, pthread_setschedparam(dead_thread, policy, ¶m)); |
| 401 | } |
| 402 | |
| 403 | TEST(pthread, pthread_join__no_such_thread) { |
| 404 | pthread_t dead_thread; |
| 405 | MakeDeadThread(dead_thread); |
| 406 | |
| 407 | void* result; |
| 408 | ASSERT_EQ(ESRCH, pthread_join(dead_thread, &result)); |
| 409 | } |
| 410 | |
| 411 | TEST(pthread, pthread_kill__no_such_thread) { |
| 412 | pthread_t dead_thread; |
| 413 | MakeDeadThread(dead_thread); |
| 414 | |
| 415 | ASSERT_EQ(ESRCH, pthread_kill(dead_thread, 0)); |
| 416 | } |
msg555 | 0f020d1 | 2013-06-06 14:59:28 -0400 | [diff] [blame] | 417 | |
| 418 | TEST(pthread, pthread_join__multijoin) { |
| 419 | bool done = false; |
| 420 | |
| 421 | pthread_t t1; |
| 422 | ASSERT_EQ(0, pthread_create(&t1, NULL, SpinFn, &done)); |
| 423 | |
| 424 | pthread_t t2; |
| 425 | ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1))); |
| 426 | |
| 427 | sleep(1); // (Give t2 a chance to call pthread_join.) |
| 428 | |
| 429 | // Multiple joins to the same thread should fail. |
| 430 | ASSERT_EQ(EINVAL, pthread_join(t1, NULL)); |
| 431 | |
| 432 | done = true; |
| 433 | |
| 434 | // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes). |
| 435 | void* join_result; |
| 436 | ASSERT_EQ(0, pthread_join(t2, &join_result)); |
Elliott Hughes | 5b9310e | 2013-10-02 16:59:05 -0700 | [diff] [blame] | 437 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result)); |
msg555 | 0f020d1 | 2013-06-06 14:59:28 -0400 | [diff] [blame] | 438 | } |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 439 | |
Elliott Hughes | 70b24b1 | 2013-11-15 11:51:07 -0800 | [diff] [blame] | 440 | TEST(pthread, pthread_join__race) { |
| 441 | // http://b/11693195 --- pthread_join could return before the thread had actually exited. |
| 442 | // If the joiner unmapped the thread's stack, that could lead to SIGSEGV in the thread. |
| 443 | for (size_t i = 0; i < 1024; ++i) { |
| 444 | size_t stack_size = 64*1024; |
| 445 | void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0); |
| 446 | |
| 447 | pthread_attr_t a; |
| 448 | pthread_attr_init(&a); |
| 449 | pthread_attr_setstack(&a, stack, stack_size); |
| 450 | |
| 451 | pthread_t t; |
| 452 | ASSERT_EQ(0, pthread_create(&t, &a, IdFn, NULL)); |
| 453 | ASSERT_EQ(0, pthread_join(t, NULL)); |
| 454 | ASSERT_EQ(0, munmap(stack, stack_size)); |
| 455 | } |
| 456 | } |
| 457 | |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 458 | static void* GetActualGuardSizeFn(void* arg) { |
| 459 | pthread_attr_t attributes; |
| 460 | pthread_getattr_np(pthread_self(), &attributes); |
| 461 | pthread_attr_getguardsize(&attributes, reinterpret_cast<size_t*>(arg)); |
| 462 | return NULL; |
| 463 | } |
| 464 | |
| 465 | static size_t GetActualGuardSize(const pthread_attr_t& attributes) { |
| 466 | size_t result; |
| 467 | pthread_t t; |
| 468 | pthread_create(&t, &attributes, GetActualGuardSizeFn, &result); |
| 469 | void* join_result; |
| 470 | pthread_join(t, &join_result); |
| 471 | return result; |
| 472 | } |
| 473 | |
| 474 | static void* GetActualStackSizeFn(void* arg) { |
| 475 | pthread_attr_t attributes; |
| 476 | pthread_getattr_np(pthread_self(), &attributes); |
| 477 | pthread_attr_getstacksize(&attributes, reinterpret_cast<size_t*>(arg)); |
| 478 | return NULL; |
| 479 | } |
| 480 | |
| 481 | static size_t GetActualStackSize(const pthread_attr_t& attributes) { |
| 482 | size_t result; |
| 483 | pthread_t t; |
| 484 | pthread_create(&t, &attributes, GetActualStackSizeFn, &result); |
| 485 | void* join_result; |
| 486 | pthread_join(t, &join_result); |
| 487 | return result; |
| 488 | } |
| 489 | |
| 490 | TEST(pthread, pthread_attr_setguardsize) { |
| 491 | pthread_attr_t attributes; |
| 492 | ASSERT_EQ(0, pthread_attr_init(&attributes)); |
| 493 | |
| 494 | // Get the default guard size. |
| 495 | size_t default_guard_size; |
| 496 | ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &default_guard_size)); |
| 497 | |
| 498 | // No such thing as too small: will be rounded up to one page by pthread_create. |
| 499 | ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 128)); |
| 500 | size_t guard_size; |
| 501 | ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size)); |
| 502 | ASSERT_EQ(128U, guard_size); |
| 503 | ASSERT_EQ(4096U, GetActualGuardSize(attributes)); |
| 504 | |
| 505 | // Large enough and a multiple of the page size. |
| 506 | ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024)); |
| 507 | ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size)); |
| 508 | ASSERT_EQ(32*1024U, guard_size); |
| 509 | |
| 510 | // Large enough but not a multiple of the page size; will be rounded up by pthread_create. |
| 511 | ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024 + 1)); |
| 512 | ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size)); |
| 513 | ASSERT_EQ(32*1024U + 1, guard_size); |
| 514 | } |
| 515 | |
| 516 | TEST(pthread, pthread_attr_setstacksize) { |
| 517 | pthread_attr_t attributes; |
| 518 | ASSERT_EQ(0, pthread_attr_init(&attributes)); |
| 519 | |
| 520 | // Get the default stack size. |
| 521 | size_t default_stack_size; |
| 522 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &default_stack_size)); |
| 523 | |
| 524 | // Too small. |
| 525 | ASSERT_EQ(EINVAL, pthread_attr_setstacksize(&attributes, 128)); |
| 526 | size_t stack_size; |
| 527 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size)); |
| 528 | ASSERT_EQ(default_stack_size, stack_size); |
| 529 | ASSERT_GE(GetActualStackSize(attributes), default_stack_size); |
| 530 | |
| 531 | // Large enough and a multiple of the page size. |
| 532 | ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024)); |
| 533 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size)); |
| 534 | ASSERT_EQ(32*1024U, stack_size); |
| 535 | ASSERT_EQ(GetActualStackSize(attributes), 32*1024U); |
| 536 | |
| 537 | // Large enough but not a multiple of the page size; will be rounded up by pthread_create. |
| 538 | ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024 + 1)); |
| 539 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size)); |
| 540 | ASSERT_EQ(32*1024U + 1, stack_size); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 541 | #if defined(__BIONIC__) |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 542 | // Bionic rounds up, which is what POSIX allows. |
| 543 | ASSERT_EQ(GetActualStackSize(attributes), (32 + 4)*1024U); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 544 | #else // __BIONIC__ |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 545 | // glibc rounds down, in violation of POSIX. They document this in their BUGS section. |
| 546 | ASSERT_EQ(GetActualStackSize(attributes), 32*1024U); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 547 | #endif // __BIONIC__ |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 548 | } |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 549 | |
| 550 | TEST(pthread, pthread_rwlock_smoke) { |
| 551 | pthread_rwlock_t l; |
| 552 | ASSERT_EQ(0, pthread_rwlock_init(&l, NULL)); |
| 553 | |
| 554 | ASSERT_EQ(0, pthread_rwlock_rdlock(&l)); |
| 555 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 556 | |
| 557 | ASSERT_EQ(0, pthread_rwlock_wrlock(&l)); |
| 558 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 559 | |
| 560 | ASSERT_EQ(0, pthread_rwlock_destroy(&l)); |
| 561 | } |
| 562 | |
| 563 | static int gOnceFnCallCount = 0; |
| 564 | static void OnceFn() { |
| 565 | ++gOnceFnCallCount; |
| 566 | } |
| 567 | |
| 568 | TEST(pthread, pthread_once_smoke) { |
| 569 | pthread_once_t once_control = PTHREAD_ONCE_INIT; |
| 570 | ASSERT_EQ(0, pthread_once(&once_control, OnceFn)); |
| 571 | ASSERT_EQ(0, pthread_once(&once_control, OnceFn)); |
| 572 | ASSERT_EQ(1, gOnceFnCallCount); |
| 573 | } |
| 574 | |
| 575 | static int gAtForkPrepareCalls = 0; |
| 576 | static void AtForkPrepare1() { gAtForkPrepareCalls = (gAtForkPrepareCalls << 4) | 1; } |
| 577 | static void AtForkPrepare2() { gAtForkPrepareCalls = (gAtForkPrepareCalls << 4) | 2; } |
| 578 | static int gAtForkParentCalls = 0; |
| 579 | static void AtForkParent1() { gAtForkParentCalls = (gAtForkParentCalls << 4) | 1; } |
| 580 | static void AtForkParent2() { gAtForkParentCalls = (gAtForkParentCalls << 4) | 2; } |
| 581 | static int gAtForkChildCalls = 0; |
| 582 | static void AtForkChild1() { gAtForkChildCalls = (gAtForkChildCalls << 4) | 1; } |
| 583 | static void AtForkChild2() { gAtForkChildCalls = (gAtForkChildCalls << 4) | 2; } |
| 584 | |
| 585 | TEST(pthread, pthread_atfork) { |
| 586 | ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1)); |
| 587 | ASSERT_EQ(0, pthread_atfork(AtForkPrepare2, AtForkParent2, AtForkChild2)); |
| 588 | |
| 589 | int pid = fork(); |
| 590 | ASSERT_NE(-1, pid) << strerror(errno); |
| 591 | |
| 592 | // Child and parent calls are made in the order they were registered. |
| 593 | if (pid == 0) { |
| 594 | ASSERT_EQ(0x12, gAtForkChildCalls); |
| 595 | _exit(0); |
| 596 | } |
| 597 | ASSERT_EQ(0x12, gAtForkParentCalls); |
| 598 | |
| 599 | // Prepare calls are made in the reverse order. |
| 600 | ASSERT_EQ(0x21, gAtForkPrepareCalls); |
| 601 | } |
| 602 | |
| 603 | TEST(pthread, pthread_attr_getscope) { |
| 604 | pthread_attr_t attr; |
| 605 | ASSERT_EQ(0, pthread_attr_init(&attr)); |
| 606 | |
| 607 | int scope; |
| 608 | ASSERT_EQ(0, pthread_attr_getscope(&attr, &scope)); |
| 609 | ASSERT_EQ(PTHREAD_SCOPE_SYSTEM, scope); |
| 610 | } |
Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 611 | |
| 612 | TEST(pthread, pthread_condattr_init) { |
| 613 | pthread_condattr_t attr; |
| 614 | pthread_condattr_init(&attr); |
| 615 | |
| 616 | clockid_t clock; |
| 617 | ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock)); |
| 618 | ASSERT_EQ(CLOCK_REALTIME, clock); |
| 619 | |
| 620 | int pshared; |
| 621 | ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared)); |
| 622 | ASSERT_EQ(PTHREAD_PROCESS_PRIVATE, pshared); |
| 623 | } |
| 624 | |
| 625 | TEST(pthread, pthread_condattr_setclock) { |
| 626 | pthread_condattr_t attr; |
| 627 | pthread_condattr_init(&attr); |
| 628 | |
| 629 | ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_REALTIME)); |
| 630 | clockid_t clock; |
| 631 | ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock)); |
| 632 | ASSERT_EQ(CLOCK_REALTIME, clock); |
| 633 | |
| 634 | ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC)); |
| 635 | ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock)); |
| 636 | ASSERT_EQ(CLOCK_MONOTONIC, clock); |
| 637 | |
| 638 | ASSERT_EQ(EINVAL, pthread_condattr_setclock(&attr, CLOCK_PROCESS_CPUTIME_ID)); |
| 639 | } |
| 640 | |
| 641 | TEST(pthread, pthread_cond_broadcast__preserves_condattr_flags) { |
| 642 | #if defined(__BIONIC__) // This tests a bionic implementation detail. |
| 643 | pthread_condattr_t attr; |
| 644 | pthread_condattr_init(&attr); |
| 645 | |
| 646 | ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC)); |
| 647 | ASSERT_EQ(0, pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED)); |
| 648 | |
| 649 | pthread_cond_t cond_var; |
| 650 | ASSERT_EQ(0, pthread_cond_init(&cond_var, &attr)); |
| 651 | |
| 652 | ASSERT_EQ(0, pthread_cond_signal(&cond_var)); |
| 653 | ASSERT_EQ(0, pthread_cond_broadcast(&cond_var)); |
| 654 | |
| 655 | attr = static_cast<pthread_condattr_t>(cond_var.value); |
| 656 | clockid_t clock; |
| 657 | ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock)); |
| 658 | ASSERT_EQ(CLOCK_MONOTONIC, clock); |
| 659 | int pshared; |
| 660 | ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared)); |
| 661 | ASSERT_EQ(PTHREAD_PROCESS_SHARED, pshared); |
| 662 | #else // __BIONIC__ |
| 663 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 664 | #endif // __BIONIC__ |
| 665 | } |
Elliott Hughes | 0e714a5 | 2014-03-03 16:42:47 -0800 | [diff] [blame] | 666 | |
| 667 | TEST(pthread, pthread_mutex_timedlock) { |
| 668 | pthread_mutex_t m; |
| 669 | ASSERT_EQ(0, pthread_mutex_init(&m, NULL)); |
| 670 | |
| 671 | // If the mutex is already locked, pthread_mutex_timedlock should time out. |
| 672 | ASSERT_EQ(0, pthread_mutex_lock(&m)); |
| 673 | |
| 674 | timespec ts; |
| 675 | ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts)); |
| 676 | ts.tv_nsec += 1; |
| 677 | ASSERT_EQ(ETIMEDOUT, pthread_mutex_timedlock(&m, &ts)); |
| 678 | |
| 679 | // If the mutex is unlocked, pthread_mutex_timedlock should succeed. |
| 680 | ASSERT_EQ(0, pthread_mutex_unlock(&m)); |
| 681 | |
| 682 | ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts)); |
| 683 | ts.tv_nsec += 1; |
| 684 | ASSERT_EQ(0, pthread_mutex_timedlock(&m, &ts)); |
| 685 | |
| 686 | ASSERT_EQ(0, pthread_mutex_unlock(&m)); |
| 687 | ASSERT_EQ(0, pthread_mutex_destroy(&m)); |
| 688 | } |