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