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> |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 26 | #include <sys/syscall.h> |
Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 27 | #include <time.h> |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 28 | #include <unistd.h> |
Elliott Hughes | bfeab1b | 2012-09-05 17:47:37 -0700 | [diff] [blame] | 29 | |
Elliott Hughes | 27a9aed | 2014-09-04 16:09:25 -0700 | [diff] [blame] | 30 | #include "private/ScopeGuard.h" |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 31 | #include "ScopedSignalHandler.h" |
| 32 | |
Elliott Hughes | bfeab1b | 2012-09-05 17:47:37 -0700 | [diff] [blame] | 33 | TEST(pthread, pthread_key_create) { |
| 34 | pthread_key_t key; |
| 35 | ASSERT_EQ(0, pthread_key_create(&key, NULL)); |
| 36 | ASSERT_EQ(0, pthread_key_delete(key)); |
| 37 | // Can't delete a key that's already been deleted. |
| 38 | ASSERT_EQ(EINVAL, pthread_key_delete(key)); |
| 39 | } |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 40 | |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 41 | TEST(pthread, pthread_key_create_lots) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 42 | #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] | 43 | // POSIX says PTHREAD_KEYS_MAX should be at least 128. |
| 44 | ASSERT_GE(PTHREAD_KEYS_MAX, 128); |
Elliott Hughes | 718a5b5 | 2014-01-28 17:02:03 -0800 | [diff] [blame] | 45 | |
| 46 | int sysconf_max = sysconf(_SC_THREAD_KEYS_MAX); |
| 47 | |
Elliott Hughes | 1887621 | 2013-12-12 11:02:41 -0800 | [diff] [blame] | 48 | // sysconf shouldn't return a smaller value. |
Elliott Hughes | 718a5b5 | 2014-01-28 17:02:03 -0800 | [diff] [blame] | 49 | ASSERT_GE(sysconf_max, PTHREAD_KEYS_MAX); |
Elliott Hughes | 1887621 | 2013-12-12 11:02:41 -0800 | [diff] [blame] | 50 | |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 51 | // We can allocate _SC_THREAD_KEYS_MAX keys. |
Elliott Hughes | 718a5b5 | 2014-01-28 17:02:03 -0800 | [diff] [blame] | 52 | sysconf_max -= 2; // (Except that gtest takes two for itself.) |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 53 | std::vector<pthread_key_t> keys; |
Elliott Hughes | 718a5b5 | 2014-01-28 17:02:03 -0800 | [diff] [blame] | 54 | for (int i = 0; i < sysconf_max; ++i) { |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 55 | pthread_key_t key; |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 56 | // 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] | 57 | ASSERT_EQ(0, pthread_key_create(&key, NULL)) << i << " of " << sysconf_max; |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 58 | keys.push_back(key); |
| 59 | } |
| 60 | |
| 61 | // ...and that really is the maximum. |
| 62 | pthread_key_t key; |
| 63 | ASSERT_EQ(EAGAIN, pthread_key_create(&key, NULL)); |
| 64 | |
| 65 | // (Don't leak all those keys!) |
| 66 | for (size_t i = 0; i < keys.size(); ++i) { |
| 67 | ASSERT_EQ(0, pthread_key_delete(keys[i])); |
| 68 | } |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 69 | #else // __BIONIC__ |
| 70 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 71 | #endif // __BIONIC__ |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Elliott Hughes | ebb770f | 2014-06-25 13:46:46 -0700 | [diff] [blame] | 74 | TEST(pthread, pthread_key_delete) { |
| 75 | void* expected = reinterpret_cast<void*>(1234); |
| 76 | pthread_key_t key; |
| 77 | ASSERT_EQ(0, pthread_key_create(&key, NULL)); |
| 78 | ASSERT_EQ(0, pthread_setspecific(key, expected)); |
| 79 | ASSERT_EQ(expected, pthread_getspecific(key)); |
| 80 | ASSERT_EQ(0, pthread_key_delete(key)); |
| 81 | // After deletion, pthread_getspecific returns NULL. |
| 82 | ASSERT_EQ(NULL, pthread_getspecific(key)); |
| 83 | // And you can't use pthread_setspecific with the deleted key. |
| 84 | ASSERT_EQ(EINVAL, pthread_setspecific(key, expected)); |
| 85 | } |
| 86 | |
Elliott Hughes | 40a5217 | 2014-07-30 14:48:10 -0700 | [diff] [blame] | 87 | TEST(pthread, pthread_key_fork) { |
| 88 | void* expected = reinterpret_cast<void*>(1234); |
| 89 | pthread_key_t key; |
| 90 | ASSERT_EQ(0, pthread_key_create(&key, NULL)); |
| 91 | ASSERT_EQ(0, pthread_setspecific(key, expected)); |
| 92 | ASSERT_EQ(expected, pthread_getspecific(key)); |
| 93 | |
| 94 | pid_t pid = fork(); |
| 95 | ASSERT_NE(-1, pid) << strerror(errno); |
| 96 | |
| 97 | if (pid == 0) { |
| 98 | // The surviving thread inherits all the forking thread's TLS values... |
| 99 | ASSERT_EQ(expected, pthread_getspecific(key)); |
| 100 | _exit(99); |
| 101 | } |
| 102 | |
| 103 | int status; |
| 104 | ASSERT_EQ(pid, waitpid(pid, &status, 0)); |
| 105 | ASSERT_TRUE(WIFEXITED(status)); |
| 106 | ASSERT_EQ(99, WEXITSTATUS(status)); |
| 107 | |
| 108 | ASSERT_EQ(expected, pthread_getspecific(key)); |
Dan Albert | 1d53ae2 | 2014-09-02 15:24:26 -0700 | [diff] [blame] | 109 | ASSERT_EQ(0, pthread_key_delete(key)); |
Elliott Hughes | 40a5217 | 2014-07-30 14:48:10 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | static void* DirtyKeyFn(void* key) { |
| 113 | return pthread_getspecific(*reinterpret_cast<pthread_key_t*>(key)); |
| 114 | } |
| 115 | |
| 116 | TEST(pthread, pthread_key_dirty) { |
| 117 | pthread_key_t key; |
| 118 | ASSERT_EQ(0, pthread_key_create(&key, NULL)); |
| 119 | |
| 120 | size_t stack_size = 128 * 1024; |
| 121 | void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); |
| 122 | ASSERT_NE(MAP_FAILED, stack); |
| 123 | memset(stack, 0xff, stack_size); |
| 124 | |
| 125 | pthread_attr_t attr; |
| 126 | ASSERT_EQ(0, pthread_attr_init(&attr)); |
| 127 | ASSERT_EQ(0, pthread_attr_setstack(&attr, stack, stack_size)); |
| 128 | |
| 129 | pthread_t t; |
| 130 | ASSERT_EQ(0, pthread_create(&t, &attr, DirtyKeyFn, &key)); |
| 131 | |
| 132 | void* result; |
| 133 | ASSERT_EQ(0, pthread_join(t, &result)); |
| 134 | ASSERT_EQ(nullptr, result); // Not ~0! |
| 135 | |
| 136 | ASSERT_EQ(0, munmap(stack, stack_size)); |
Dan Albert | 1d53ae2 | 2014-09-02 15:24:26 -0700 | [diff] [blame] | 137 | ASSERT_EQ(0, pthread_key_delete(key)); |
Elliott Hughes | 40a5217 | 2014-07-30 14:48:10 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 140 | static void* IdFn(void* arg) { |
| 141 | return arg; |
| 142 | } |
| 143 | |
| 144 | static void* SleepFn(void* arg) { |
Elliott Hughes | 5b9310e | 2013-10-02 16:59:05 -0700 | [diff] [blame] | 145 | sleep(reinterpret_cast<uintptr_t>(arg)); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 146 | return NULL; |
| 147 | } |
| 148 | |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 149 | static void* SpinFn(void* arg) { |
| 150 | volatile bool* b = reinterpret_cast<volatile bool*>(arg); |
| 151 | while (!*b) { |
| 152 | } |
| 153 | return NULL; |
| 154 | } |
| 155 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 156 | static void* JoinFn(void* arg) { |
| 157 | return reinterpret_cast<void*>(pthread_join(reinterpret_cast<pthread_t>(arg), NULL)); |
| 158 | } |
| 159 | |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 160 | static void AssertDetached(pthread_t t, bool is_detached) { |
| 161 | pthread_attr_t attr; |
| 162 | ASSERT_EQ(0, pthread_getattr_np(t, &attr)); |
| 163 | int detach_state; |
| 164 | ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &detach_state)); |
| 165 | pthread_attr_destroy(&attr); |
| 166 | ASSERT_EQ(is_detached, (detach_state == PTHREAD_CREATE_DETACHED)); |
| 167 | } |
| 168 | |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 169 | static void MakeDeadThread(pthread_t& t) { |
| 170 | ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, NULL)); |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 171 | ASSERT_EQ(0, pthread_join(t, NULL)); |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 172 | } |
| 173 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 174 | TEST(pthread, pthread_create) { |
| 175 | void* expected_result = reinterpret_cast<void*>(123); |
| 176 | // Can we create a thread? |
| 177 | pthread_t t; |
| 178 | ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, expected_result)); |
| 179 | // If we join, do we get the expected value back? |
| 180 | void* result; |
| 181 | ASSERT_EQ(0, pthread_join(t, &result)); |
| 182 | ASSERT_EQ(expected_result, result); |
| 183 | } |
| 184 | |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 185 | TEST(pthread, pthread_create_EAGAIN) { |
| 186 | pthread_attr_t attributes; |
| 187 | ASSERT_EQ(0, pthread_attr_init(&attributes)); |
| 188 | ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, static_cast<size_t>(-1) & ~(getpagesize() - 1))); |
| 189 | |
| 190 | pthread_t t; |
| 191 | ASSERT_EQ(EAGAIN, pthread_create(&t, &attributes, IdFn, NULL)); |
| 192 | } |
| 193 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 194 | TEST(pthread, pthread_no_join_after_detach) { |
| 195 | pthread_t t1; |
| 196 | ASSERT_EQ(0, pthread_create(&t1, NULL, SleepFn, reinterpret_cast<void*>(5))); |
| 197 | |
| 198 | // After a pthread_detach... |
| 199 | ASSERT_EQ(0, pthread_detach(t1)); |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 200 | AssertDetached(t1, true); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 201 | |
| 202 | // ...pthread_join should fail. |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 203 | ASSERT_EQ(EINVAL, pthread_join(t1, NULL)); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | TEST(pthread, pthread_no_op_detach_after_join) { |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 207 | bool done = false; |
| 208 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 209 | pthread_t t1; |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 210 | ASSERT_EQ(0, pthread_create(&t1, NULL, SpinFn, &done)); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 211 | |
| 212 | // If thread 2 is already waiting to join thread 1... |
| 213 | pthread_t t2; |
| 214 | ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1))); |
| 215 | |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 216 | sleep(1); // (Give t2 a chance to call pthread_join.) |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 217 | |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 218 | // ...a call to pthread_detach on thread 1 will "succeed" (silently fail)... |
| 219 | ASSERT_EQ(0, pthread_detach(t1)); |
| 220 | AssertDetached(t1, false); |
| 221 | |
| 222 | done = true; |
| 223 | |
| 224 | // ...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] | 225 | void* join_result; |
| 226 | ASSERT_EQ(0, pthread_join(t2, &join_result)); |
Elliott Hughes | 5b9310e | 2013-10-02 16:59:05 -0700 | [diff] [blame] | 227 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result)); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 228 | } |
Elliott Hughes | 14f1959 | 2012-10-29 10:19:44 -0700 | [diff] [blame] | 229 | |
| 230 | TEST(pthread, pthread_join_self) { |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 231 | ASSERT_EQ(EDEADLK, pthread_join(pthread_self(), NULL)); |
Elliott Hughes | 14f1959 | 2012-10-29 10:19:44 -0700 | [diff] [blame] | 232 | } |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 233 | |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 234 | struct TestBug37410 { |
| 235 | pthread_t main_thread; |
| 236 | pthread_mutex_t mutex; |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 237 | |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 238 | static void main() { |
| 239 | TestBug37410 data; |
| 240 | data.main_thread = pthread_self(); |
| 241 | ASSERT_EQ(0, pthread_mutex_init(&data.mutex, NULL)); |
| 242 | ASSERT_EQ(0, pthread_mutex_lock(&data.mutex)); |
| 243 | |
| 244 | pthread_t t; |
| 245 | ASSERT_EQ(0, pthread_create(&t, NULL, TestBug37410::thread_fn, reinterpret_cast<void*>(&data))); |
| 246 | |
| 247 | // Wait for the thread to be running... |
| 248 | ASSERT_EQ(0, pthread_mutex_lock(&data.mutex)); |
| 249 | ASSERT_EQ(0, pthread_mutex_unlock(&data.mutex)); |
| 250 | |
| 251 | // ...and exit. |
| 252 | pthread_exit(NULL); |
| 253 | } |
| 254 | |
| 255 | private: |
| 256 | static void* thread_fn(void* arg) { |
| 257 | TestBug37410* data = reinterpret_cast<TestBug37410*>(arg); |
| 258 | |
| 259 | // Let the main thread know we're running. |
| 260 | pthread_mutex_unlock(&data->mutex); |
| 261 | |
| 262 | // And wait for the main thread to exit. |
| 263 | pthread_join(data->main_thread, NULL); |
| 264 | |
| 265 | return NULL; |
| 266 | } |
| 267 | }; |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 268 | |
Elliott Hughes | 7fd803c | 2013-02-14 16:33:52 -0800 | [diff] [blame] | 269 | // Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to |
| 270 | // run this test (which exits normally) in its own process. |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 271 | TEST(pthread_DeathTest, pthread_bug_37410) { |
| 272 | // http://code.google.com/p/android/issues/detail?id=37410 |
| 273 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 274 | ASSERT_EXIT(TestBug37410::main(), ::testing::ExitedWithCode(0), ""); |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 275 | } |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 276 | |
| 277 | static void* SignalHandlerFn(void* arg) { |
| 278 | sigset_t wait_set; |
| 279 | sigfillset(&wait_set); |
| 280 | return reinterpret_cast<void*>(sigwait(&wait_set, reinterpret_cast<int*>(arg))); |
| 281 | } |
| 282 | |
| 283 | TEST(pthread, pthread_sigmask) { |
Elliott Hughes | 19e6232 | 2013-10-15 11:23:57 -0700 | [diff] [blame] | 284 | // Check that SIGUSR1 isn't blocked. |
| 285 | sigset_t original_set; |
| 286 | sigemptyset(&original_set); |
| 287 | ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &original_set)); |
| 288 | ASSERT_FALSE(sigismember(&original_set, SIGUSR1)); |
| 289 | |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 290 | // Block SIGUSR1. |
| 291 | sigset_t set; |
| 292 | sigemptyset(&set); |
| 293 | sigaddset(&set, SIGUSR1); |
| 294 | ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &set, NULL)); |
| 295 | |
Elliott Hughes | 19e6232 | 2013-10-15 11:23:57 -0700 | [diff] [blame] | 296 | // Check that SIGUSR1 is blocked. |
| 297 | sigset_t final_set; |
| 298 | sigemptyset(&final_set); |
| 299 | ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &final_set)); |
| 300 | ASSERT_TRUE(sigismember(&final_set, SIGUSR1)); |
| 301 | // ...and that sigprocmask agrees with pthread_sigmask. |
| 302 | sigemptyset(&final_set); |
| 303 | ASSERT_EQ(0, sigprocmask(SIG_BLOCK, NULL, &final_set)); |
| 304 | ASSERT_TRUE(sigismember(&final_set, SIGUSR1)); |
| 305 | |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 306 | // Spawn a thread that calls sigwait and tells us what it received. |
| 307 | pthread_t signal_thread; |
| 308 | int received_signal = -1; |
| 309 | ASSERT_EQ(0, pthread_create(&signal_thread, NULL, SignalHandlerFn, &received_signal)); |
| 310 | |
| 311 | // Send that thread SIGUSR1. |
| 312 | pthread_kill(signal_thread, SIGUSR1); |
| 313 | |
| 314 | // See what it got. |
| 315 | void* join_result; |
| 316 | ASSERT_EQ(0, pthread_join(signal_thread, &join_result)); |
| 317 | ASSERT_EQ(SIGUSR1, received_signal); |
Elliott Hughes | 5b9310e | 2013-10-02 16:59:05 -0700 | [diff] [blame] | 318 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result)); |
Elliott Hughes | 19e6232 | 2013-10-15 11:23:57 -0700 | [diff] [blame] | 319 | |
| 320 | // Restore the original signal mask. |
| 321 | ASSERT_EQ(0, pthread_sigmask(SIG_SETMASK, &original_set, NULL)); |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 322 | } |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 323 | |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 324 | TEST(pthread, pthread_setname_np__too_long) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 325 | #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] | 326 | 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] | 327 | #else // __BIONIC__ |
| 328 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 329 | #endif // __BIONIC__ |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | TEST(pthread, pthread_setname_np__self) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 333 | #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] | 334 | ASSERT_EQ(0, pthread_setname_np(pthread_self(), "short 1")); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 335 | #else // __BIONIC__ |
| 336 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 337 | #endif // __BIONIC__ |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | TEST(pthread, pthread_setname_np__other) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 341 | #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] | 342 | // Emulator kernels don't currently support setting the name of other threads. |
| 343 | char* filename = NULL; |
| 344 | asprintf(&filename, "/proc/self/task/%d/comm", gettid()); |
| 345 | struct stat sb; |
| 346 | bool has_comm = (stat(filename, &sb) != -1); |
| 347 | free(filename); |
| 348 | |
| 349 | if (has_comm) { |
| 350 | pthread_t t1; |
| 351 | ASSERT_EQ(0, pthread_create(&t1, NULL, SleepFn, reinterpret_cast<void*>(5))); |
| 352 | ASSERT_EQ(0, pthread_setname_np(t1, "short 2")); |
| 353 | } else { |
| 354 | fprintf(stderr, "skipping test: this kernel doesn't have /proc/self/task/tid/comm files!\n"); |
| 355 | } |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 356 | #else // __BIONIC__ |
| 357 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 358 | #endif // __BIONIC__ |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | TEST(pthread, pthread_setname_np__no_such_thread) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 362 | #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] | 363 | pthread_t dead_thread; |
| 364 | MakeDeadThread(dead_thread); |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 365 | |
| 366 | // Call pthread_setname_np after thread has already exited. |
Elliott Hughes | a41ba2f | 2013-03-21 20:02:35 -0700 | [diff] [blame] | 367 | ASSERT_EQ(ESRCH, pthread_setname_np(dead_thread, "short 3")); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 368 | #else // __BIONIC__ |
| 369 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 370 | #endif // __BIONIC__ |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 371 | } |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 372 | |
| 373 | TEST(pthread, pthread_kill__0) { |
| 374 | // Signal 0 just tests that the thread exists, so it's safe to call on ourselves. |
| 375 | ASSERT_EQ(0, pthread_kill(pthread_self(), 0)); |
| 376 | } |
| 377 | |
| 378 | TEST(pthread, pthread_kill__invalid_signal) { |
| 379 | ASSERT_EQ(EINVAL, pthread_kill(pthread_self(), -1)); |
| 380 | } |
| 381 | |
Elliott Hughes | fae89fc | 2013-02-21 11:22:23 -0800 | [diff] [blame] | 382 | static void pthread_kill__in_signal_handler_helper(int signal_number) { |
| 383 | static int count = 0; |
| 384 | ASSERT_EQ(SIGALRM, signal_number); |
| 385 | if (++count == 1) { |
| 386 | // Can we call pthread_kill from a signal handler? |
| 387 | ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM)); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | TEST(pthread, pthread_kill__in_signal_handler) { |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 392 | ScopedSignalHandler ssh(SIGALRM, pthread_kill__in_signal_handler_helper); |
Elliott Hughes | fae89fc | 2013-02-21 11:22:23 -0800 | [diff] [blame] | 393 | ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM)); |
| 394 | } |
| 395 | |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 396 | TEST(pthread, pthread_detach__no_such_thread) { |
| 397 | pthread_t dead_thread; |
| 398 | MakeDeadThread(dead_thread); |
| 399 | |
| 400 | ASSERT_EQ(ESRCH, pthread_detach(dead_thread)); |
| 401 | } |
| 402 | |
Elliott Hughes | 04620a3 | 2014-03-07 17:59:05 -0800 | [diff] [blame] | 403 | TEST(pthread, pthread_detach__leak) { |
Christopher Ferris | e380960 | 2014-08-06 14:15:01 -0700 | [diff] [blame] | 404 | size_t initial_bytes = 0; |
| 405 | // Run this loop more than once since the first loop causes some memory |
| 406 | // to be allocated permenantly. Run an extra loop to help catch any subtle |
| 407 | // memory leaks. |
| 408 | for (size_t loop = 0; loop < 3; loop++) { |
| 409 | // Set the initial bytes on the second loop since the memory in use |
| 410 | // should have stabilized. |
| 411 | if (loop == 1) { |
| 412 | initial_bytes = mallinfo().uordblks; |
| 413 | } |
Elliott Hughes | 04620a3 | 2014-03-07 17:59:05 -0800 | [diff] [blame] | 414 | |
Christopher Ferris | e380960 | 2014-08-06 14:15:01 -0700 | [diff] [blame] | 415 | pthread_attr_t attr; |
| 416 | ASSERT_EQ(0, pthread_attr_init(&attr)); |
| 417 | ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE)); |
Elliott Hughes | 04620a3 | 2014-03-07 17:59:05 -0800 | [diff] [blame] | 418 | |
Christopher Ferris | e380960 | 2014-08-06 14:15:01 -0700 | [diff] [blame] | 419 | std::vector<pthread_t> threads; |
| 420 | for (size_t i = 0; i < 32; ++i) { |
| 421 | pthread_t t; |
| 422 | ASSERT_EQ(0, pthread_create(&t, &attr, IdFn, NULL)); |
| 423 | threads.push_back(t); |
| 424 | } |
Elliott Hughes | 04620a3 | 2014-03-07 17:59:05 -0800 | [diff] [blame] | 425 | |
Christopher Ferris | e380960 | 2014-08-06 14:15:01 -0700 | [diff] [blame] | 426 | sleep(1); |
Elliott Hughes | 04620a3 | 2014-03-07 17:59:05 -0800 | [diff] [blame] | 427 | |
Christopher Ferris | e380960 | 2014-08-06 14:15:01 -0700 | [diff] [blame] | 428 | for (size_t i = 0; i < 32; ++i) { |
| 429 | ASSERT_EQ(0, pthread_detach(threads[i])) << i; |
| 430 | } |
Elliott Hughes | 04620a3 | 2014-03-07 17:59:05 -0800 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | size_t final_bytes = mallinfo().uordblks; |
Elliott Hughes | 04620a3 | 2014-03-07 17:59:05 -0800 | [diff] [blame] | 434 | int leaked_bytes = (final_bytes - initial_bytes); |
| 435 | |
| 436 | // User code (like this test) doesn't know how large pthread_internal_t is. |
| 437 | // We can be pretty sure it's more than 128 bytes. |
| 438 | ASSERT_LT(leaked_bytes, 32 /*threads*/ * 128 /*bytes*/); |
| 439 | } |
| 440 | |
Jeff Hao | 9b06cc3 | 2013-08-15 14:51:16 -0700 | [diff] [blame] | 441 | TEST(pthread, pthread_getcpuclockid__clock_gettime) { |
| 442 | pthread_t t; |
| 443 | ASSERT_EQ(0, pthread_create(&t, NULL, SleepFn, reinterpret_cast<void*>(5))); |
| 444 | |
| 445 | clockid_t c; |
| 446 | ASSERT_EQ(0, pthread_getcpuclockid(t, &c)); |
| 447 | timespec ts; |
| 448 | ASSERT_EQ(0, clock_gettime(c, &ts)); |
| 449 | } |
| 450 | |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 451 | TEST(pthread, pthread_getcpuclockid__no_such_thread) { |
| 452 | pthread_t dead_thread; |
| 453 | MakeDeadThread(dead_thread); |
| 454 | |
| 455 | clockid_t c; |
| 456 | ASSERT_EQ(ESRCH, pthread_getcpuclockid(dead_thread, &c)); |
| 457 | } |
| 458 | |
| 459 | TEST(pthread, pthread_getschedparam__no_such_thread) { |
| 460 | pthread_t dead_thread; |
| 461 | MakeDeadThread(dead_thread); |
| 462 | |
| 463 | int policy; |
| 464 | sched_param param; |
| 465 | ASSERT_EQ(ESRCH, pthread_getschedparam(dead_thread, &policy, ¶m)); |
| 466 | } |
| 467 | |
| 468 | TEST(pthread, pthread_setschedparam__no_such_thread) { |
| 469 | pthread_t dead_thread; |
| 470 | MakeDeadThread(dead_thread); |
| 471 | |
| 472 | int policy = 0; |
| 473 | sched_param param; |
| 474 | ASSERT_EQ(ESRCH, pthread_setschedparam(dead_thread, policy, ¶m)); |
| 475 | } |
| 476 | |
| 477 | TEST(pthread, pthread_join__no_such_thread) { |
| 478 | pthread_t dead_thread; |
| 479 | MakeDeadThread(dead_thread); |
| 480 | |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 481 | ASSERT_EQ(ESRCH, pthread_join(dead_thread, NULL)); |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | TEST(pthread, pthread_kill__no_such_thread) { |
| 485 | pthread_t dead_thread; |
| 486 | MakeDeadThread(dead_thread); |
| 487 | |
| 488 | ASSERT_EQ(ESRCH, pthread_kill(dead_thread, 0)); |
| 489 | } |
msg555 | 0f020d1 | 2013-06-06 14:59:28 -0400 | [diff] [blame] | 490 | |
| 491 | TEST(pthread, pthread_join__multijoin) { |
| 492 | bool done = false; |
| 493 | |
| 494 | pthread_t t1; |
| 495 | ASSERT_EQ(0, pthread_create(&t1, NULL, SpinFn, &done)); |
| 496 | |
| 497 | pthread_t t2; |
| 498 | ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1))); |
| 499 | |
| 500 | sleep(1); // (Give t2 a chance to call pthread_join.) |
| 501 | |
| 502 | // Multiple joins to the same thread should fail. |
| 503 | ASSERT_EQ(EINVAL, pthread_join(t1, NULL)); |
| 504 | |
| 505 | done = true; |
| 506 | |
| 507 | // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes). |
| 508 | void* join_result; |
| 509 | ASSERT_EQ(0, pthread_join(t2, &join_result)); |
Elliott Hughes | 5b9310e | 2013-10-02 16:59:05 -0700 | [diff] [blame] | 510 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result)); |
msg555 | 0f020d1 | 2013-06-06 14:59:28 -0400 | [diff] [blame] | 511 | } |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 512 | |
Elliott Hughes | 70b24b1 | 2013-11-15 11:51:07 -0800 | [diff] [blame] | 513 | TEST(pthread, pthread_join__race) { |
| 514 | // http://b/11693195 --- pthread_join could return before the thread had actually exited. |
| 515 | // If the joiner unmapped the thread's stack, that could lead to SIGSEGV in the thread. |
| 516 | for (size_t i = 0; i < 1024; ++i) { |
| 517 | size_t stack_size = 64*1024; |
| 518 | void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0); |
| 519 | |
| 520 | pthread_attr_t a; |
| 521 | pthread_attr_init(&a); |
| 522 | pthread_attr_setstack(&a, stack, stack_size); |
| 523 | |
| 524 | pthread_t t; |
| 525 | ASSERT_EQ(0, pthread_create(&t, &a, IdFn, NULL)); |
| 526 | ASSERT_EQ(0, pthread_join(t, NULL)); |
| 527 | ASSERT_EQ(0, munmap(stack, stack_size)); |
| 528 | } |
| 529 | } |
| 530 | |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 531 | static void* GetActualGuardSizeFn(void* arg) { |
| 532 | pthread_attr_t attributes; |
| 533 | pthread_getattr_np(pthread_self(), &attributes); |
| 534 | pthread_attr_getguardsize(&attributes, reinterpret_cast<size_t*>(arg)); |
| 535 | return NULL; |
| 536 | } |
| 537 | |
| 538 | static size_t GetActualGuardSize(const pthread_attr_t& attributes) { |
| 539 | size_t result; |
| 540 | pthread_t t; |
| 541 | pthread_create(&t, &attributes, GetActualGuardSizeFn, &result); |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 542 | pthread_join(t, NULL); |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 543 | return result; |
| 544 | } |
| 545 | |
| 546 | static void* GetActualStackSizeFn(void* arg) { |
| 547 | pthread_attr_t attributes; |
| 548 | pthread_getattr_np(pthread_self(), &attributes); |
| 549 | pthread_attr_getstacksize(&attributes, reinterpret_cast<size_t*>(arg)); |
| 550 | return NULL; |
| 551 | } |
| 552 | |
| 553 | static size_t GetActualStackSize(const pthread_attr_t& attributes) { |
| 554 | size_t result; |
| 555 | pthread_t t; |
| 556 | pthread_create(&t, &attributes, GetActualStackSizeFn, &result); |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 557 | pthread_join(t, NULL); |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 558 | return result; |
| 559 | } |
| 560 | |
| 561 | TEST(pthread, pthread_attr_setguardsize) { |
| 562 | pthread_attr_t attributes; |
| 563 | ASSERT_EQ(0, pthread_attr_init(&attributes)); |
| 564 | |
| 565 | // Get the default guard size. |
| 566 | size_t default_guard_size; |
| 567 | ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &default_guard_size)); |
| 568 | |
| 569 | // No such thing as too small: will be rounded up to one page by pthread_create. |
| 570 | ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 128)); |
| 571 | size_t guard_size; |
| 572 | ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size)); |
| 573 | ASSERT_EQ(128U, guard_size); |
| 574 | ASSERT_EQ(4096U, GetActualGuardSize(attributes)); |
| 575 | |
| 576 | // Large enough and a multiple of the page size. |
| 577 | ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024)); |
| 578 | ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size)); |
| 579 | ASSERT_EQ(32*1024U, guard_size); |
| 580 | |
| 581 | // Large enough but not a multiple of the page size; will be rounded up by pthread_create. |
| 582 | ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024 + 1)); |
| 583 | ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size)); |
| 584 | ASSERT_EQ(32*1024U + 1, guard_size); |
| 585 | } |
| 586 | |
| 587 | TEST(pthread, pthread_attr_setstacksize) { |
| 588 | pthread_attr_t attributes; |
| 589 | ASSERT_EQ(0, pthread_attr_init(&attributes)); |
| 590 | |
| 591 | // Get the default stack size. |
| 592 | size_t default_stack_size; |
| 593 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &default_stack_size)); |
| 594 | |
| 595 | // Too small. |
| 596 | ASSERT_EQ(EINVAL, pthread_attr_setstacksize(&attributes, 128)); |
| 597 | size_t stack_size; |
| 598 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size)); |
| 599 | ASSERT_EQ(default_stack_size, stack_size); |
| 600 | ASSERT_GE(GetActualStackSize(attributes), default_stack_size); |
| 601 | |
| 602 | // Large enough and a multiple of the page size. |
| 603 | ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024)); |
| 604 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size)); |
| 605 | ASSERT_EQ(32*1024U, stack_size); |
| 606 | ASSERT_EQ(GetActualStackSize(attributes), 32*1024U); |
| 607 | |
| 608 | // Large enough but not a multiple of the page size; will be rounded up by pthread_create. |
| 609 | ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024 + 1)); |
| 610 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size)); |
| 611 | ASSERT_EQ(32*1024U + 1, stack_size); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 612 | #if defined(__BIONIC__) |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 613 | // Bionic rounds up, which is what POSIX allows. |
| 614 | ASSERT_EQ(GetActualStackSize(attributes), (32 + 4)*1024U); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 615 | #else // __BIONIC__ |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 616 | // glibc rounds down, in violation of POSIX. They document this in their BUGS section. |
| 617 | ASSERT_EQ(GetActualStackSize(attributes), 32*1024U); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 618 | #endif // __BIONIC__ |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 619 | } |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 620 | |
| 621 | TEST(pthread, pthread_rwlock_smoke) { |
| 622 | pthread_rwlock_t l; |
| 623 | ASSERT_EQ(0, pthread_rwlock_init(&l, NULL)); |
| 624 | |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 625 | // Single read lock |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 626 | ASSERT_EQ(0, pthread_rwlock_rdlock(&l)); |
| 627 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 628 | |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 629 | // Multiple read lock |
| 630 | ASSERT_EQ(0, pthread_rwlock_rdlock(&l)); |
| 631 | ASSERT_EQ(0, pthread_rwlock_rdlock(&l)); |
| 632 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 633 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 634 | |
| 635 | // Write lock |
Calin Juravle | 92687e4 | 2014-05-22 19:21:22 +0100 | [diff] [blame] | 636 | ASSERT_EQ(0, pthread_rwlock_wrlock(&l)); |
| 637 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 638 | |
| 639 | // Try writer lock |
| 640 | ASSERT_EQ(0, pthread_rwlock_trywrlock(&l)); |
| 641 | ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l)); |
| 642 | ASSERT_EQ(EBUSY, pthread_rwlock_tryrdlock(&l)); |
| 643 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 644 | |
| 645 | // Try reader lock |
| 646 | ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l)); |
| 647 | ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l)); |
| 648 | ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l)); |
| 649 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 650 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 651 | |
| 652 | // Try writer lock after unlock |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 653 | ASSERT_EQ(0, pthread_rwlock_wrlock(&l)); |
| 654 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 655 | |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 656 | #ifdef __BIONIC__ |
| 657 | // EDEADLK in "read after write" |
| 658 | ASSERT_EQ(0, pthread_rwlock_wrlock(&l)); |
| 659 | ASSERT_EQ(EDEADLK, pthread_rwlock_rdlock(&l)); |
| 660 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 661 | |
| 662 | // EDEADLK in "write after write" |
| 663 | ASSERT_EQ(0, pthread_rwlock_wrlock(&l)); |
| 664 | ASSERT_EQ(EDEADLK, pthread_rwlock_wrlock(&l)); |
| 665 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 666 | #endif |
| 667 | |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 668 | ASSERT_EQ(0, pthread_rwlock_destroy(&l)); |
| 669 | } |
| 670 | |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 671 | static int g_once_fn_call_count = 0; |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 672 | static void OnceFn() { |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 673 | ++g_once_fn_call_count; |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | TEST(pthread, pthread_once_smoke) { |
| 677 | pthread_once_t once_control = PTHREAD_ONCE_INIT; |
| 678 | ASSERT_EQ(0, pthread_once(&once_control, OnceFn)); |
| 679 | ASSERT_EQ(0, pthread_once(&once_control, OnceFn)); |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 680 | ASSERT_EQ(1, g_once_fn_call_count); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 681 | } |
| 682 | |
Elliott Hughes | 3694ec6 | 2014-05-14 11:46:08 -0700 | [diff] [blame] | 683 | static std::string pthread_once_1934122_result = ""; |
| 684 | |
| 685 | static void Routine2() { |
| 686 | pthread_once_1934122_result += "2"; |
| 687 | } |
| 688 | |
| 689 | static void Routine1() { |
| 690 | pthread_once_t once_control_2 = PTHREAD_ONCE_INIT; |
| 691 | pthread_once_1934122_result += "1"; |
| 692 | pthread_once(&once_control_2, &Routine2); |
| 693 | } |
| 694 | |
| 695 | TEST(pthread, pthread_once_1934122) { |
| 696 | // Very old versions of Android couldn't call pthread_once from a |
| 697 | // pthread_once init routine. http://b/1934122. |
| 698 | pthread_once_t once_control_1 = PTHREAD_ONCE_INIT; |
| 699 | ASSERT_EQ(0, pthread_once(&once_control_1, &Routine1)); |
| 700 | ASSERT_EQ("12", pthread_once_1934122_result); |
| 701 | } |
| 702 | |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 703 | static int g_atfork_prepare_calls = 0; |
| 704 | static void AtForkPrepare1() { g_atfork_prepare_calls = (g_atfork_prepare_calls << 4) | 1; } |
| 705 | static void AtForkPrepare2() { g_atfork_prepare_calls = (g_atfork_prepare_calls << 4) | 2; } |
| 706 | static int g_atfork_parent_calls = 0; |
| 707 | static void AtForkParent1() { g_atfork_parent_calls = (g_atfork_parent_calls << 4) | 1; } |
| 708 | static void AtForkParent2() { g_atfork_parent_calls = (g_atfork_parent_calls << 4) | 2; } |
| 709 | static int g_atfork_child_calls = 0; |
| 710 | static void AtForkChild1() { g_atfork_child_calls = (g_atfork_child_calls << 4) | 1; } |
| 711 | static void AtForkChild2() { g_atfork_child_calls = (g_atfork_child_calls << 4) | 2; } |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 712 | |
| 713 | TEST(pthread, pthread_atfork) { |
| 714 | ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1)); |
| 715 | ASSERT_EQ(0, pthread_atfork(AtForkPrepare2, AtForkParent2, AtForkChild2)); |
| 716 | |
| 717 | int pid = fork(); |
| 718 | ASSERT_NE(-1, pid) << strerror(errno); |
| 719 | |
| 720 | // Child and parent calls are made in the order they were registered. |
| 721 | if (pid == 0) { |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 722 | ASSERT_EQ(0x12, g_atfork_child_calls); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 723 | _exit(0); |
| 724 | } |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 725 | ASSERT_EQ(0x12, g_atfork_parent_calls); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 726 | |
| 727 | // Prepare calls are made in the reverse order. |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 728 | ASSERT_EQ(0x21, g_atfork_prepare_calls); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | TEST(pthread, pthread_attr_getscope) { |
| 732 | pthread_attr_t attr; |
| 733 | ASSERT_EQ(0, pthread_attr_init(&attr)); |
| 734 | |
| 735 | int scope; |
| 736 | ASSERT_EQ(0, pthread_attr_getscope(&attr, &scope)); |
| 737 | ASSERT_EQ(PTHREAD_SCOPE_SYSTEM, scope); |
| 738 | } |
Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 739 | |
| 740 | TEST(pthread, pthread_condattr_init) { |
| 741 | pthread_condattr_t attr; |
| 742 | pthread_condattr_init(&attr); |
| 743 | |
| 744 | clockid_t clock; |
| 745 | ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock)); |
| 746 | ASSERT_EQ(CLOCK_REALTIME, clock); |
| 747 | |
| 748 | int pshared; |
| 749 | ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared)); |
| 750 | ASSERT_EQ(PTHREAD_PROCESS_PRIVATE, pshared); |
| 751 | } |
| 752 | |
| 753 | TEST(pthread, pthread_condattr_setclock) { |
| 754 | pthread_condattr_t attr; |
| 755 | pthread_condattr_init(&attr); |
| 756 | |
| 757 | ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_REALTIME)); |
| 758 | clockid_t clock; |
| 759 | ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock)); |
| 760 | ASSERT_EQ(CLOCK_REALTIME, clock); |
| 761 | |
| 762 | ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC)); |
| 763 | ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock)); |
| 764 | ASSERT_EQ(CLOCK_MONOTONIC, clock); |
| 765 | |
| 766 | ASSERT_EQ(EINVAL, pthread_condattr_setclock(&attr, CLOCK_PROCESS_CPUTIME_ID)); |
| 767 | } |
| 768 | |
| 769 | TEST(pthread, pthread_cond_broadcast__preserves_condattr_flags) { |
| 770 | #if defined(__BIONIC__) // This tests a bionic implementation detail. |
| 771 | pthread_condattr_t attr; |
| 772 | pthread_condattr_init(&attr); |
| 773 | |
| 774 | ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC)); |
| 775 | ASSERT_EQ(0, pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED)); |
| 776 | |
| 777 | pthread_cond_t cond_var; |
| 778 | ASSERT_EQ(0, pthread_cond_init(&cond_var, &attr)); |
| 779 | |
| 780 | ASSERT_EQ(0, pthread_cond_signal(&cond_var)); |
| 781 | ASSERT_EQ(0, pthread_cond_broadcast(&cond_var)); |
| 782 | |
| 783 | attr = static_cast<pthread_condattr_t>(cond_var.value); |
| 784 | clockid_t clock; |
| 785 | ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock)); |
| 786 | ASSERT_EQ(CLOCK_MONOTONIC, clock); |
| 787 | int pshared; |
| 788 | ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared)); |
| 789 | ASSERT_EQ(PTHREAD_PROCESS_SHARED, pshared); |
| 790 | #else // __BIONIC__ |
| 791 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 792 | #endif // __BIONIC__ |
| 793 | } |
Elliott Hughes | 0e714a5 | 2014-03-03 16:42:47 -0800 | [diff] [blame] | 794 | |
| 795 | TEST(pthread, pthread_mutex_timedlock) { |
| 796 | pthread_mutex_t m; |
| 797 | ASSERT_EQ(0, pthread_mutex_init(&m, NULL)); |
| 798 | |
| 799 | // If the mutex is already locked, pthread_mutex_timedlock should time out. |
| 800 | ASSERT_EQ(0, pthread_mutex_lock(&m)); |
| 801 | |
| 802 | timespec ts; |
| 803 | ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts)); |
| 804 | ts.tv_nsec += 1; |
| 805 | ASSERT_EQ(ETIMEDOUT, pthread_mutex_timedlock(&m, &ts)); |
| 806 | |
| 807 | // If the mutex is unlocked, pthread_mutex_timedlock should succeed. |
| 808 | ASSERT_EQ(0, pthread_mutex_unlock(&m)); |
| 809 | |
| 810 | ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts)); |
| 811 | ts.tv_nsec += 1; |
| 812 | ASSERT_EQ(0, pthread_mutex_timedlock(&m, &ts)); |
| 813 | |
| 814 | ASSERT_EQ(0, pthread_mutex_unlock(&m)); |
| 815 | ASSERT_EQ(0, pthread_mutex_destroy(&m)); |
| 816 | } |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 817 | |
| 818 | TEST(pthread, pthread_attr_getstack__main_thread) { |
| 819 | // This test is only meaningful for the main thread, so make sure we're running on it! |
| 820 | ASSERT_EQ(getpid(), syscall(__NR_gettid)); |
| 821 | |
| 822 | // Get the main thread's attributes. |
| 823 | pthread_attr_t attributes; |
| 824 | ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes)); |
| 825 | |
| 826 | // Check that we correctly report that the main thread has no guard page. |
| 827 | size_t guard_size; |
| 828 | ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size)); |
| 829 | ASSERT_EQ(0U, guard_size); // The main thread has no guard page. |
| 830 | |
| 831 | // Get the stack base and the stack size (both ways). |
| 832 | void* stack_base; |
| 833 | size_t stack_size; |
| 834 | ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size)); |
| 835 | size_t stack_size2; |
| 836 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2)); |
| 837 | |
| 838 | // The two methods of asking for the stack size should agree. |
| 839 | EXPECT_EQ(stack_size, stack_size2); |
| 840 | |
| 841 | // What does /proc/self/maps' [stack] line say? |
Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 842 | void* maps_stack_hi = NULL; |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 843 | FILE* fp = fopen("/proc/self/maps", "r"); |
| 844 | ASSERT_TRUE(fp != NULL); |
| 845 | char line[BUFSIZ]; |
| 846 | while (fgets(line, sizeof(line), fp) != NULL) { |
| 847 | uintptr_t lo, hi; |
| 848 | char name[10]; |
| 849 | sscanf(line, "%" PRIxPTR "-%" PRIxPTR " %*4s %*x %*x:%*x %*d %10s", &lo, &hi, name); |
| 850 | if (strcmp(name, "[stack]") == 0) { |
Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 851 | maps_stack_hi = reinterpret_cast<void*>(hi); |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 852 | break; |
| 853 | } |
| 854 | } |
| 855 | fclose(fp); |
| 856 | |
Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 857 | // The stack size should correspond to RLIMIT_STACK. |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 858 | rlimit rl; |
Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 859 | ASSERT_EQ(0, getrlimit(RLIMIT_STACK, &rl)); |
Elliott Hughes | 27a9aed | 2014-09-04 16:09:25 -0700 | [diff] [blame] | 860 | uint64_t original_rlim_cur = rl.rlim_cur; |
| 861 | #if defined(__BIONIC__) |
| 862 | if (rl.rlim_cur == RLIM_INFINITY) { |
| 863 | rl.rlim_cur = 8 * 1024 * 1024; // Bionic reports unlimited stacks as 8MiB. |
| 864 | } |
| 865 | #endif |
Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 866 | EXPECT_EQ(rl.rlim_cur, stack_size); |
| 867 | |
Dmitriy Ivanov | d9ff722 | 2014-09-08 16:22:22 -0700 | [diff] [blame] | 868 | auto guard = make_scope_guard([&rl, original_rlim_cur]() { |
Elliott Hughes | 27a9aed | 2014-09-04 16:09:25 -0700 | [diff] [blame] | 869 | rl.rlim_cur = original_rlim_cur; |
| 870 | ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl)); |
| 871 | }); |
| 872 | |
Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 873 | // The high address of the /proc/self/maps [stack] region should equal stack_base + stack_size. |
| 874 | // Remember that the stack grows down (and is mapped in on demand), so the low address of the |
| 875 | // region isn't very interesting. |
| 876 | EXPECT_EQ(maps_stack_hi, reinterpret_cast<uint8_t*>(stack_base) + stack_size); |
| 877 | |
| 878 | // |
| 879 | // What if RLIMIT_STACK is smaller than the stack's current extent? |
| 880 | // |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 881 | rl.rlim_cur = rl.rlim_max = 1024; // 1KiB. We know the stack must be at least a page already. |
| 882 | rl.rlim_max = RLIM_INFINITY; |
| 883 | ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl)); |
| 884 | |
| 885 | ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes)); |
| 886 | ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size)); |
| 887 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2)); |
| 888 | |
| 889 | EXPECT_EQ(stack_size, stack_size2); |
| 890 | ASSERT_EQ(1024U, stack_size); |
| 891 | |
| 892 | // |
Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 893 | // What if RLIMIT_STACK isn't a whole number of pages? |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 894 | // |
| 895 | rl.rlim_cur = rl.rlim_max = 6666; // Not a whole number of pages. |
| 896 | rl.rlim_max = RLIM_INFINITY; |
| 897 | ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl)); |
| 898 | |
| 899 | ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes)); |
| 900 | ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size)); |
| 901 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2)); |
| 902 | |
| 903 | EXPECT_EQ(stack_size, stack_size2); |
| 904 | ASSERT_EQ(6666U, stack_size); |
| 905 | } |
Elliott Hughes | 8fb639c | 2014-09-12 14:43:07 -0700 | [diff] [blame] | 906 | |
| 907 | #if defined(__BIONIC__) |
| 908 | static void* pthread_gettid_np_helper(void* arg) { |
| 909 | *reinterpret_cast<pid_t*>(arg) = gettid(); |
| 910 | return NULL; |
| 911 | } |
| 912 | #endif |
| 913 | |
| 914 | TEST(pthread, pthread_gettid_np) { |
| 915 | #if defined(__BIONIC__) |
| 916 | ASSERT_EQ(gettid(), pthread_gettid_np(pthread_self())); |
| 917 | |
| 918 | pid_t t_gettid_result; |
| 919 | pthread_t t; |
| 920 | pthread_create(&t, NULL, pthread_gettid_np_helper, &t_gettid_result); |
| 921 | |
| 922 | pid_t t_pthread_gettid_np_result = pthread_gettid_np(t); |
| 923 | |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 924 | pthread_join(t, NULL); |
Elliott Hughes | 8fb639c | 2014-09-12 14:43:07 -0700 | [diff] [blame] | 925 | |
| 926 | ASSERT_EQ(t_gettid_result, t_pthread_gettid_np_result); |
| 927 | #else |
| 928 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 929 | #endif |
| 930 | } |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 931 | |
| 932 | static size_t cleanup_counter = 0; |
| 933 | |
| 934 | void AbortCleanupRoutine(void*) { |
| 935 | abort(); |
| 936 | } |
| 937 | |
| 938 | void CountCleanupRoutine(void*) { |
| 939 | ++cleanup_counter; |
| 940 | } |
| 941 | |
| 942 | void PthreadCleanupTester() { |
| 943 | pthread_cleanup_push(CountCleanupRoutine, NULL); |
| 944 | pthread_cleanup_push(CountCleanupRoutine, NULL); |
| 945 | pthread_cleanup_push(AbortCleanupRoutine, NULL); |
| 946 | |
| 947 | pthread_cleanup_pop(0); // Pop the abort without executing it. |
| 948 | pthread_cleanup_pop(1); // Pop one count while executing it. |
| 949 | ASSERT_EQ(1U, cleanup_counter); |
| 950 | // Exit while the other count is still on the cleanup stack. |
| 951 | pthread_exit(NULL); |
| 952 | |
| 953 | // Calls to pthread_cleanup_pop/pthread_cleanup_push must always be balanced. |
| 954 | pthread_cleanup_pop(0); |
| 955 | } |
| 956 | |
| 957 | void* PthreadCleanupStartRoutine(void*) { |
| 958 | PthreadCleanupTester(); |
| 959 | return NULL; |
| 960 | } |
| 961 | |
| 962 | TEST(pthread, pthread_cleanup_push__pthread_cleanup_pop) { |
| 963 | pthread_t t; |
| 964 | ASSERT_EQ(0, pthread_create(&t, NULL, PthreadCleanupStartRoutine, NULL)); |
| 965 | pthread_join(t, NULL); |
| 966 | ASSERT_EQ(2U, cleanup_counter); |
| 967 | } |