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