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