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> |
Yabin Cui | 140f367 | 2015-02-03 10:32:00 -0800 | [diff] [blame] | 25 | #include <stdio.h> |
Elliott Hughes | 70b24b1 | 2013-11-15 11:51:07 -0800 | [diff] [blame] | 26 | #include <sys/mman.h> |
Elliott Hughes | 4d098ca | 2016-04-11 12:43:05 -0700 | [diff] [blame] | 27 | #include <sys/prctl.h> |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 28 | #include <sys/syscall.h> |
Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 29 | #include <time.h> |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 30 | #include <unistd.h> |
Yabin Cui | 33ac04a | 2015-09-22 11:16:15 -0700 | [diff] [blame] | 31 | #include <unwind.h> |
Elliott Hughes | bfeab1b | 2012-09-05 17:47:37 -0700 | [diff] [blame] | 32 | |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 33 | #include <atomic> |
Yabin Cui | b584572 | 2015-03-16 22:46:42 -0700 | [diff] [blame] | 34 | #include <vector> |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 35 | |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 36 | #include "private/bionic_constants.h" |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 37 | #include "private/bionic_macros.h" |
| 38 | #include "private/ScopeGuard.h" |
| 39 | #include "BionicDeathTest.h" |
| 40 | #include "ScopedSignalHandler.h" |
Elliott Hughes | 15dfd63 | 2015-09-22 16:40:14 -0700 | [diff] [blame] | 41 | #include "utils.h" |
| 42 | |
Elliott Hughes | bfeab1b | 2012-09-05 17:47:37 -0700 | [diff] [blame] | 43 | TEST(pthread, pthread_key_create) { |
| 44 | pthread_key_t key; |
| 45 | ASSERT_EQ(0, pthread_key_create(&key, NULL)); |
| 46 | ASSERT_EQ(0, pthread_key_delete(key)); |
| 47 | // Can't delete a key that's already been deleted. |
| 48 | ASSERT_EQ(EINVAL, pthread_key_delete(key)); |
| 49 | } |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 50 | |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 51 | TEST(pthread, pthread_keys_max) { |
Yabin Cui | 6c238f2 | 2014-12-11 20:50:41 -0800 | [diff] [blame] | 52 | // POSIX says PTHREAD_KEYS_MAX should be at least _POSIX_THREAD_KEYS_MAX. |
| 53 | ASSERT_GE(PTHREAD_KEYS_MAX, _POSIX_THREAD_KEYS_MAX); |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 54 | } |
Elliott Hughes | 718a5b5 | 2014-01-28 17:02:03 -0800 | [diff] [blame] | 55 | |
Yabin Cui | 6c238f2 | 2014-12-11 20:50:41 -0800 | [diff] [blame] | 56 | TEST(pthread, sysconf_SC_THREAD_KEYS_MAX_eq_PTHREAD_KEYS_MAX) { |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 57 | int sysconf_max = sysconf(_SC_THREAD_KEYS_MAX); |
Yabin Cui | 6c238f2 | 2014-12-11 20:50:41 -0800 | [diff] [blame] | 58 | ASSERT_EQ(sysconf_max, PTHREAD_KEYS_MAX); |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | TEST(pthread, pthread_key_many_distinct) { |
Yabin Cui | 6c238f2 | 2014-12-11 20:50:41 -0800 | [diff] [blame] | 62 | // As gtest uses pthread keys, we can't allocate exactly PTHREAD_KEYS_MAX |
| 63 | // pthread keys, but We should be able to allocate at least this many keys. |
| 64 | int nkeys = PTHREAD_KEYS_MAX / 2; |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 65 | std::vector<pthread_key_t> keys; |
| 66 | |
| 67 | auto scope_guard = make_scope_guard([&keys]{ |
Elliott Hughes | 0b2acdf | 2015-10-02 18:25:19 -0700 | [diff] [blame] | 68 | for (const auto& key : keys) { |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 69 | EXPECT_EQ(0, pthread_key_delete(key)); |
| 70 | } |
| 71 | }); |
| 72 | |
| 73 | for (int i = 0; i < nkeys; ++i) { |
| 74 | pthread_key_t key; |
Elliott Hughes | 6170693 | 2015-03-31 10:56:58 -0700 | [diff] [blame] | 75 | // If this fails, it's likely that LIBC_PTHREAD_KEY_RESERVED_COUNT is wrong. |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 76 | ASSERT_EQ(0, pthread_key_create(&key, NULL)) << i << " of " << nkeys; |
| 77 | keys.push_back(key); |
| 78 | ASSERT_EQ(0, pthread_setspecific(key, reinterpret_cast<void*>(i))); |
| 79 | } |
| 80 | |
| 81 | for (int i = keys.size() - 1; i >= 0; --i) { |
| 82 | ASSERT_EQ(reinterpret_cast<void*>(i), pthread_getspecific(keys.back())); |
| 83 | pthread_key_t key = keys.back(); |
| 84 | keys.pop_back(); |
| 85 | ASSERT_EQ(0, pthread_key_delete(key)); |
| 86 | } |
| 87 | } |
| 88 | |
Yabin Cui | 6c238f2 | 2014-12-11 20:50:41 -0800 | [diff] [blame] | 89 | TEST(pthread, pthread_key_not_exceed_PTHREAD_KEYS_MAX) { |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 90 | std::vector<pthread_key_t> keys; |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 91 | int rv = 0; |
Yabin Cui | 6c238f2 | 2014-12-11 20:50:41 -0800 | [diff] [blame] | 92 | |
| 93 | // Pthread keys are used by gtest, so PTHREAD_KEYS_MAX should |
| 94 | // be more than we are allowed to allocate now. |
| 95 | for (int i = 0; i < PTHREAD_KEYS_MAX; i++) { |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 96 | pthread_key_t key; |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 97 | rv = pthread_key_create(&key, NULL); |
| 98 | if (rv == EAGAIN) { |
| 99 | break; |
| 100 | } |
| 101 | EXPECT_EQ(0, rv); |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 102 | keys.push_back(key); |
| 103 | } |
| 104 | |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 105 | // Don't leak keys. |
Elliott Hughes | 0b2acdf | 2015-10-02 18:25:19 -0700 | [diff] [blame] | 106 | for (const auto& key : keys) { |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 107 | EXPECT_EQ(0, pthread_key_delete(key)); |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 108 | } |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 109 | keys.clear(); |
| 110 | |
| 111 | // We should have eventually reached the maximum number of keys and received |
| 112 | // EAGAIN. |
| 113 | ASSERT_EQ(EAGAIN, rv); |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Elliott Hughes | ebb770f | 2014-06-25 13:46:46 -0700 | [diff] [blame] | 116 | TEST(pthread, pthread_key_delete) { |
| 117 | void* expected = reinterpret_cast<void*>(1234); |
| 118 | pthread_key_t key; |
| 119 | ASSERT_EQ(0, pthread_key_create(&key, NULL)); |
| 120 | ASSERT_EQ(0, pthread_setspecific(key, expected)); |
| 121 | ASSERT_EQ(expected, pthread_getspecific(key)); |
| 122 | ASSERT_EQ(0, pthread_key_delete(key)); |
| 123 | // After deletion, pthread_getspecific returns NULL. |
| 124 | ASSERT_EQ(NULL, pthread_getspecific(key)); |
| 125 | // And you can't use pthread_setspecific with the deleted key. |
| 126 | ASSERT_EQ(EINVAL, pthread_setspecific(key, expected)); |
| 127 | } |
| 128 | |
Elliott Hughes | 40a5217 | 2014-07-30 14:48:10 -0700 | [diff] [blame] | 129 | TEST(pthread, pthread_key_fork) { |
| 130 | void* expected = reinterpret_cast<void*>(1234); |
| 131 | pthread_key_t key; |
| 132 | ASSERT_EQ(0, pthread_key_create(&key, NULL)); |
| 133 | ASSERT_EQ(0, pthread_setspecific(key, expected)); |
| 134 | ASSERT_EQ(expected, pthread_getspecific(key)); |
| 135 | |
| 136 | pid_t pid = fork(); |
| 137 | ASSERT_NE(-1, pid) << strerror(errno); |
| 138 | |
| 139 | if (pid == 0) { |
| 140 | // The surviving thread inherits all the forking thread's TLS values... |
| 141 | ASSERT_EQ(expected, pthread_getspecific(key)); |
| 142 | _exit(99); |
| 143 | } |
| 144 | |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 145 | AssertChildExited(pid, 99); |
Elliott Hughes | 40a5217 | 2014-07-30 14:48:10 -0700 | [diff] [blame] | 146 | |
| 147 | ASSERT_EQ(expected, pthread_getspecific(key)); |
Dan Albert | 1d53ae2 | 2014-09-02 15:24:26 -0700 | [diff] [blame] | 148 | ASSERT_EQ(0, pthread_key_delete(key)); |
Elliott Hughes | 40a5217 | 2014-07-30 14:48:10 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | static void* DirtyKeyFn(void* key) { |
| 152 | return pthread_getspecific(*reinterpret_cast<pthread_key_t*>(key)); |
| 153 | } |
| 154 | |
| 155 | TEST(pthread, pthread_key_dirty) { |
| 156 | pthread_key_t key; |
| 157 | ASSERT_EQ(0, pthread_key_create(&key, NULL)); |
| 158 | |
Yabin Cui | a36158a | 2015-11-16 21:06:16 -0800 | [diff] [blame] | 159 | size_t stack_size = 640 * 1024; |
Elliott Hughes | 40a5217 | 2014-07-30 14:48:10 -0700 | [diff] [blame] | 160 | void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); |
| 161 | ASSERT_NE(MAP_FAILED, stack); |
| 162 | memset(stack, 0xff, stack_size); |
| 163 | |
| 164 | pthread_attr_t attr; |
| 165 | ASSERT_EQ(0, pthread_attr_init(&attr)); |
| 166 | ASSERT_EQ(0, pthread_attr_setstack(&attr, stack, stack_size)); |
| 167 | |
| 168 | pthread_t t; |
| 169 | ASSERT_EQ(0, pthread_create(&t, &attr, DirtyKeyFn, &key)); |
| 170 | |
| 171 | void* result; |
| 172 | ASSERT_EQ(0, pthread_join(t, &result)); |
| 173 | ASSERT_EQ(nullptr, result); // Not ~0! |
| 174 | |
| 175 | ASSERT_EQ(0, munmap(stack, stack_size)); |
Dan Albert | 1d53ae2 | 2014-09-02 15:24:26 -0700 | [diff] [blame] | 176 | ASSERT_EQ(0, pthread_key_delete(key)); |
Elliott Hughes | 40a5217 | 2014-07-30 14:48:10 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Yabin Cui | 5ddbb3f | 2015-03-05 20:35:32 -0800 | [diff] [blame] | 179 | TEST(pthread, static_pthread_key_used_before_creation) { |
| 180 | #if defined(__BIONIC__) |
| 181 | // See http://b/19625804. The bug is about a static/global pthread key being used before creation. |
| 182 | // So here tests if the static/global default value 0 can be detected as invalid key. |
| 183 | static pthread_key_t key; |
| 184 | ASSERT_EQ(nullptr, pthread_getspecific(key)); |
| 185 | ASSERT_EQ(EINVAL, pthread_setspecific(key, nullptr)); |
| 186 | ASSERT_EQ(EINVAL, pthread_key_delete(key)); |
| 187 | #else |
| 188 | GTEST_LOG_(INFO) << "This test tests bionic pthread key implementation detail.\n"; |
| 189 | #endif |
| 190 | } |
| 191 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 192 | static void* IdFn(void* arg) { |
| 193 | return arg; |
| 194 | } |
| 195 | |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 196 | class SpinFunctionHelper { |
| 197 | public: |
| 198 | SpinFunctionHelper() { |
| 199 | SpinFunctionHelper::spin_flag_ = true; |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 200 | } |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 201 | ~SpinFunctionHelper() { |
| 202 | UnSpin(); |
| 203 | } |
| 204 | auto GetFunction() -> void* (*)(void*) { |
| 205 | return SpinFunctionHelper::SpinFn; |
| 206 | } |
| 207 | |
| 208 | void UnSpin() { |
| 209 | SpinFunctionHelper::spin_flag_ = false; |
| 210 | } |
| 211 | |
| 212 | private: |
| 213 | static void* SpinFn(void*) { |
| 214 | while (spin_flag_) {} |
| 215 | return NULL; |
| 216 | } |
Yabin Cui | a36158a | 2015-11-16 21:06:16 -0800 | [diff] [blame] | 217 | static std::atomic<bool> spin_flag_; |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 218 | }; |
| 219 | |
| 220 | // It doesn't matter if spin_flag_ is used in several tests, |
| 221 | // because it is always set to false after each test. Each thread |
| 222 | // loops on spin_flag_ can find it becomes false at some time. |
Yabin Cui | a36158a | 2015-11-16 21:06:16 -0800 | [diff] [blame] | 223 | std::atomic<bool> SpinFunctionHelper::spin_flag_; |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 224 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 225 | static void* JoinFn(void* arg) { |
| 226 | return reinterpret_cast<void*>(pthread_join(reinterpret_cast<pthread_t>(arg), NULL)); |
| 227 | } |
| 228 | |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 229 | static void AssertDetached(pthread_t t, bool is_detached) { |
| 230 | pthread_attr_t attr; |
| 231 | ASSERT_EQ(0, pthread_getattr_np(t, &attr)); |
| 232 | int detach_state; |
| 233 | ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &detach_state)); |
| 234 | pthread_attr_destroy(&attr); |
| 235 | ASSERT_EQ(is_detached, (detach_state == PTHREAD_CREATE_DETACHED)); |
| 236 | } |
| 237 | |
Elliott Hughes | 7484c21 | 2017-02-02 02:41:38 +0000 | [diff] [blame] | 238 | static void MakeDeadThread(pthread_t& t) { |
| 239 | ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, NULL)); |
| 240 | ASSERT_EQ(0, pthread_join(t, NULL)); |
| 241 | } |
| 242 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 243 | TEST(pthread, pthread_create) { |
| 244 | void* expected_result = reinterpret_cast<void*>(123); |
| 245 | // Can we create a thread? |
| 246 | pthread_t t; |
| 247 | ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, expected_result)); |
| 248 | // If we join, do we get the expected value back? |
| 249 | void* result; |
| 250 | ASSERT_EQ(0, pthread_join(t, &result)); |
| 251 | ASSERT_EQ(expected_result, result); |
| 252 | } |
| 253 | |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 254 | TEST(pthread, pthread_create_EAGAIN) { |
| 255 | pthread_attr_t attributes; |
| 256 | ASSERT_EQ(0, pthread_attr_init(&attributes)); |
| 257 | ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, static_cast<size_t>(-1) & ~(getpagesize() - 1))); |
| 258 | |
| 259 | pthread_t t; |
| 260 | ASSERT_EQ(EAGAIN, pthread_create(&t, &attributes, IdFn, NULL)); |
| 261 | } |
| 262 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 263 | TEST(pthread, pthread_no_join_after_detach) { |
Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 264 | SpinFunctionHelper spin_helper; |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 265 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 266 | pthread_t t1; |
Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 267 | ASSERT_EQ(0, pthread_create(&t1, NULL, spin_helper.GetFunction(), NULL)); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 268 | |
| 269 | // After a pthread_detach... |
| 270 | ASSERT_EQ(0, pthread_detach(t1)); |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 271 | AssertDetached(t1, true); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 272 | |
| 273 | // ...pthread_join should fail. |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 274 | ASSERT_EQ(EINVAL, pthread_join(t1, NULL)); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | TEST(pthread, pthread_no_op_detach_after_join) { |
Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 278 | SpinFunctionHelper spin_helper; |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 279 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 280 | pthread_t t1; |
Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 281 | ASSERT_EQ(0, pthread_create(&t1, NULL, spin_helper.GetFunction(), NULL)); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 282 | |
| 283 | // If thread 2 is already waiting to join thread 1... |
| 284 | pthread_t t2; |
| 285 | ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1))); |
| 286 | |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 287 | sleep(1); // (Give t2 a chance to call pthread_join.) |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 288 | |
Yabin Cui | bbb0432 | 2015-03-19 15:19:25 -0700 | [diff] [blame] | 289 | #if defined(__BIONIC__) |
| 290 | ASSERT_EQ(EINVAL, pthread_detach(t1)); |
| 291 | #else |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 292 | ASSERT_EQ(0, pthread_detach(t1)); |
Yabin Cui | bbb0432 | 2015-03-19 15:19:25 -0700 | [diff] [blame] | 293 | #endif |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 294 | AssertDetached(t1, false); |
| 295 | |
Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 296 | spin_helper.UnSpin(); |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 297 | |
| 298 | // ...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] | 299 | void* join_result; |
| 300 | ASSERT_EQ(0, pthread_join(t2, &join_result)); |
Elliott Hughes | 5b9310e | 2013-10-02 16:59:05 -0700 | [diff] [blame] | 301 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result)); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 302 | } |
Elliott Hughes | 14f1959 | 2012-10-29 10:19:44 -0700 | [diff] [blame] | 303 | |
| 304 | TEST(pthread, pthread_join_self) { |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 305 | ASSERT_EQ(EDEADLK, pthread_join(pthread_self(), NULL)); |
Elliott Hughes | 14f1959 | 2012-10-29 10:19:44 -0700 | [diff] [blame] | 306 | } |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 307 | |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 308 | struct TestBug37410 { |
| 309 | pthread_t main_thread; |
| 310 | pthread_mutex_t mutex; |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 311 | |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 312 | static void main() { |
| 313 | TestBug37410 data; |
| 314 | data.main_thread = pthread_self(); |
| 315 | ASSERT_EQ(0, pthread_mutex_init(&data.mutex, NULL)); |
| 316 | ASSERT_EQ(0, pthread_mutex_lock(&data.mutex)); |
| 317 | |
| 318 | pthread_t t; |
| 319 | ASSERT_EQ(0, pthread_create(&t, NULL, TestBug37410::thread_fn, reinterpret_cast<void*>(&data))); |
| 320 | |
| 321 | // Wait for the thread to be running... |
| 322 | ASSERT_EQ(0, pthread_mutex_lock(&data.mutex)); |
| 323 | ASSERT_EQ(0, pthread_mutex_unlock(&data.mutex)); |
| 324 | |
| 325 | // ...and exit. |
| 326 | pthread_exit(NULL); |
| 327 | } |
| 328 | |
| 329 | private: |
| 330 | static void* thread_fn(void* arg) { |
| 331 | TestBug37410* data = reinterpret_cast<TestBug37410*>(arg); |
| 332 | |
| 333 | // Let the main thread know we're running. |
| 334 | pthread_mutex_unlock(&data->mutex); |
| 335 | |
| 336 | // And wait for the main thread to exit. |
| 337 | pthread_join(data->main_thread, NULL); |
| 338 | |
| 339 | return NULL; |
| 340 | } |
| 341 | }; |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 342 | |
Elliott Hughes | 7fd803c | 2013-02-14 16:33:52 -0800 | [diff] [blame] | 343 | // Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to |
| 344 | // run this test (which exits normally) in its own process. |
Yabin Cui | 9df7040 | 2014-11-05 18:01:01 -0800 | [diff] [blame] | 345 | |
| 346 | class pthread_DeathTest : public BionicDeathTest {}; |
| 347 | |
| 348 | TEST_F(pthread_DeathTest, pthread_bug_37410) { |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 349 | // http://code.google.com/p/android/issues/detail?id=37410 |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 350 | ASSERT_EXIT(TestBug37410::main(), ::testing::ExitedWithCode(0), ""); |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 351 | } |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 352 | |
| 353 | static void* SignalHandlerFn(void* arg) { |
| 354 | sigset_t wait_set; |
| 355 | sigfillset(&wait_set); |
| 356 | return reinterpret_cast<void*>(sigwait(&wait_set, reinterpret_cast<int*>(arg))); |
| 357 | } |
| 358 | |
| 359 | TEST(pthread, pthread_sigmask) { |
Elliott Hughes | 19e6232 | 2013-10-15 11:23:57 -0700 | [diff] [blame] | 360 | // Check that SIGUSR1 isn't blocked. |
| 361 | sigset_t original_set; |
| 362 | sigemptyset(&original_set); |
| 363 | ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &original_set)); |
| 364 | ASSERT_FALSE(sigismember(&original_set, SIGUSR1)); |
| 365 | |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 366 | // Block SIGUSR1. |
| 367 | sigset_t set; |
| 368 | sigemptyset(&set); |
| 369 | sigaddset(&set, SIGUSR1); |
| 370 | ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &set, NULL)); |
| 371 | |
Elliott Hughes | 19e6232 | 2013-10-15 11:23:57 -0700 | [diff] [blame] | 372 | // Check that SIGUSR1 is blocked. |
| 373 | sigset_t final_set; |
| 374 | sigemptyset(&final_set); |
| 375 | ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &final_set)); |
| 376 | ASSERT_TRUE(sigismember(&final_set, SIGUSR1)); |
| 377 | // ...and that sigprocmask agrees with pthread_sigmask. |
| 378 | sigemptyset(&final_set); |
| 379 | ASSERT_EQ(0, sigprocmask(SIG_BLOCK, NULL, &final_set)); |
| 380 | ASSERT_TRUE(sigismember(&final_set, SIGUSR1)); |
| 381 | |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 382 | // Spawn a thread that calls sigwait and tells us what it received. |
| 383 | pthread_t signal_thread; |
| 384 | int received_signal = -1; |
| 385 | ASSERT_EQ(0, pthread_create(&signal_thread, NULL, SignalHandlerFn, &received_signal)); |
| 386 | |
| 387 | // Send that thread SIGUSR1. |
| 388 | pthread_kill(signal_thread, SIGUSR1); |
| 389 | |
| 390 | // See what it got. |
| 391 | void* join_result; |
| 392 | ASSERT_EQ(0, pthread_join(signal_thread, &join_result)); |
| 393 | ASSERT_EQ(SIGUSR1, received_signal); |
Elliott Hughes | 5b9310e | 2013-10-02 16:59:05 -0700 | [diff] [blame] | 394 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result)); |
Elliott Hughes | 19e6232 | 2013-10-15 11:23:57 -0700 | [diff] [blame] | 395 | |
| 396 | // Restore the original signal mask. |
| 397 | ASSERT_EQ(0, pthread_sigmask(SIG_SETMASK, &original_set, NULL)); |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 398 | } |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 399 | |
Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 400 | static void test_pthread_setname_np__pthread_getname_np(pthread_t t) { |
| 401 | ASSERT_EQ(0, pthread_setname_np(t, "short")); |
| 402 | char name[32]; |
| 403 | ASSERT_EQ(0, pthread_getname_np(t, name, sizeof(name))); |
| 404 | ASSERT_STREQ("short", name); |
| 405 | |
Elliott Hughes | d1aea30 | 2015-04-25 10:05:24 -0700 | [diff] [blame] | 406 | // The limit is 15 characters --- the kernel's buffer is 16, but includes a NUL. |
Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 407 | ASSERT_EQ(0, pthread_setname_np(t, "123456789012345")); |
| 408 | ASSERT_EQ(0, pthread_getname_np(t, name, sizeof(name))); |
| 409 | ASSERT_STREQ("123456789012345", name); |
| 410 | |
| 411 | ASSERT_EQ(ERANGE, pthread_setname_np(t, "1234567890123456")); |
| 412 | |
| 413 | // The passed-in buffer should be at least 16 bytes. |
| 414 | ASSERT_EQ(0, pthread_getname_np(t, name, 16)); |
| 415 | ASSERT_EQ(ERANGE, pthread_getname_np(t, name, 15)); |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 418 | TEST(pthread, pthread_setname_np__pthread_getname_np__self) { |
| 419 | test_pthread_setname_np__pthread_getname_np(pthread_self()); |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 422 | TEST(pthread, pthread_setname_np__pthread_getname_np__other) { |
| 423 | SpinFunctionHelper spin_helper; |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 424 | |
Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 425 | pthread_t t; |
Elliott Hughes | 4d098ca | 2016-04-11 12:43:05 -0700 | [diff] [blame] | 426 | ASSERT_EQ(0, pthread_create(&t, nullptr, spin_helper.GetFunction(), nullptr)); |
| 427 | test_pthread_setname_np__pthread_getname_np(t); |
| 428 | spin_helper.UnSpin(); |
| 429 | ASSERT_EQ(0, pthread_join(t, nullptr)); |
| 430 | } |
| 431 | |
| 432 | // http://b/28051133: a kernel misfeature means that you can't change the |
| 433 | // name of another thread if you've set PR_SET_DUMPABLE to 0. |
| 434 | TEST(pthread, pthread_setname_np__pthread_getname_np__other_PR_SET_DUMPABLE) { |
| 435 | ASSERT_EQ(0, prctl(PR_SET_DUMPABLE, 0)) << strerror(errno); |
| 436 | |
| 437 | SpinFunctionHelper spin_helper; |
| 438 | |
| 439 | pthread_t t; |
| 440 | ASSERT_EQ(0, pthread_create(&t, nullptr, spin_helper.GetFunction(), nullptr)); |
Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 441 | test_pthread_setname_np__pthread_getname_np(t); |
| 442 | spin_helper.UnSpin(); |
| 443 | ASSERT_EQ(0, pthread_join(t, nullptr)); |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Elliott Hughes | 11859d4 | 2017-02-13 17:59:29 -0800 | [diff] [blame] | 446 | TEST_F(pthread_DeathTest, pthread_setname_np__no_such_thread) { |
Elliott Hughes | bcb1529 | 2017-02-07 21:05:30 +0000 | [diff] [blame] | 447 | pthread_t dead_thread; |
| 448 | MakeDeadThread(dead_thread); |
| 449 | |
Elliott Hughes | 6ce686c | 2017-02-21 13:15:20 -0800 | [diff] [blame^] | 450 | EXPECT_DEATH(pthread_setname_np(dead_thread, "short 3"), "invalid pthread_t"); |
| 451 | } |
| 452 | |
| 453 | TEST_F(pthread_DeathTest, pthread_setname_np__null_thread) { |
| 454 | pthread_t null_thread = 0; |
| 455 | EXPECT_EQ(ENOENT, pthread_setname_np(null_thread, "short 3")); |
Elliott Hughes | 11859d4 | 2017-02-13 17:59:29 -0800 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | TEST_F(pthread_DeathTest, pthread_getname_np__no_such_thread) { |
| 459 | pthread_t dead_thread; |
| 460 | MakeDeadThread(dead_thread); |
| 461 | |
Elliott Hughes | bcb1529 | 2017-02-07 21:05:30 +0000 | [diff] [blame] | 462 | char name[64]; |
Elliott Hughes | 6ce686c | 2017-02-21 13:15:20 -0800 | [diff] [blame^] | 463 | EXPECT_DEATH(pthread_getname_np(dead_thread, name, sizeof(name)), "invalid pthread_t"); |
| 464 | } |
| 465 | |
| 466 | TEST_F(pthread_DeathTest, pthread_getname_np__null_thread) { |
| 467 | pthread_t null_thread = 0; |
| 468 | |
| 469 | char name[64]; |
| 470 | EXPECT_EQ(ENOENT, pthread_getname_np(null_thread, name, sizeof(name))); |
Elliott Hughes | bcb1529 | 2017-02-07 21:05:30 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 473 | TEST(pthread, pthread_kill__0) { |
| 474 | // Signal 0 just tests that the thread exists, so it's safe to call on ourselves. |
| 475 | ASSERT_EQ(0, pthread_kill(pthread_self(), 0)); |
| 476 | } |
| 477 | |
| 478 | TEST(pthread, pthread_kill__invalid_signal) { |
| 479 | ASSERT_EQ(EINVAL, pthread_kill(pthread_self(), -1)); |
| 480 | } |
| 481 | |
Elliott Hughes | fae89fc | 2013-02-21 11:22:23 -0800 | [diff] [blame] | 482 | static void pthread_kill__in_signal_handler_helper(int signal_number) { |
| 483 | static int count = 0; |
| 484 | ASSERT_EQ(SIGALRM, signal_number); |
| 485 | if (++count == 1) { |
| 486 | // Can we call pthread_kill from a signal handler? |
| 487 | ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM)); |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | TEST(pthread, pthread_kill__in_signal_handler) { |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 492 | ScopedSignalHandler ssh(SIGALRM, pthread_kill__in_signal_handler_helper); |
Elliott Hughes | fae89fc | 2013-02-21 11:22:23 -0800 | [diff] [blame] | 493 | ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM)); |
| 494 | } |
| 495 | |
Elliott Hughes | 11859d4 | 2017-02-13 17:59:29 -0800 | [diff] [blame] | 496 | TEST_F(pthread_DeathTest, pthread_detach__no_such_thread) { |
Elliott Hughes | 7484c21 | 2017-02-02 02:41:38 +0000 | [diff] [blame] | 497 | pthread_t dead_thread; |
| 498 | MakeDeadThread(dead_thread); |
| 499 | |
Elliott Hughes | 6ce686c | 2017-02-21 13:15:20 -0800 | [diff] [blame^] | 500 | EXPECT_DEATH(pthread_detach(dead_thread), "invalid pthread_t"); |
| 501 | } |
| 502 | |
| 503 | TEST_F(pthread_DeathTest, pthread_detach__null_thread) { |
| 504 | pthread_t null_thread = 0; |
| 505 | EXPECT_EQ(ESRCH, pthread_detach(null_thread)); |
Elliott Hughes | 7484c21 | 2017-02-02 02:41:38 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Jeff Hao | 9b06cc3 | 2013-08-15 14:51:16 -0700 | [diff] [blame] | 508 | TEST(pthread, pthread_getcpuclockid__clock_gettime) { |
Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 509 | SpinFunctionHelper spin_helper; |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 510 | |
Jeff Hao | 9b06cc3 | 2013-08-15 14:51:16 -0700 | [diff] [blame] | 511 | pthread_t t; |
Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 512 | ASSERT_EQ(0, pthread_create(&t, NULL, spin_helper.GetFunction(), NULL)); |
Jeff Hao | 9b06cc3 | 2013-08-15 14:51:16 -0700 | [diff] [blame] | 513 | |
| 514 | clockid_t c; |
| 515 | ASSERT_EQ(0, pthread_getcpuclockid(t, &c)); |
| 516 | timespec ts; |
| 517 | ASSERT_EQ(0, clock_gettime(c, &ts)); |
Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 518 | spin_helper.UnSpin(); |
Yabin Cui | a36158a | 2015-11-16 21:06:16 -0800 | [diff] [blame] | 519 | ASSERT_EQ(0, pthread_join(t, nullptr)); |
Jeff Hao | 9b06cc3 | 2013-08-15 14:51:16 -0700 | [diff] [blame] | 520 | } |
| 521 | |
Elliott Hughes | 11859d4 | 2017-02-13 17:59:29 -0800 | [diff] [blame] | 522 | TEST_F(pthread_DeathTest, pthread_getcpuclockid__no_such_thread) { |
Elliott Hughes | bcb1529 | 2017-02-07 21:05:30 +0000 | [diff] [blame] | 523 | pthread_t dead_thread; |
| 524 | MakeDeadThread(dead_thread); |
| 525 | |
| 526 | clockid_t c; |
Elliott Hughes | 6ce686c | 2017-02-21 13:15:20 -0800 | [diff] [blame^] | 527 | EXPECT_DEATH(pthread_getcpuclockid(dead_thread, &c), "invalid pthread_t"); |
| 528 | } |
| 529 | |
| 530 | TEST_F(pthread_DeathTest, pthread_getcpuclockid__null_thread) { |
| 531 | pthread_t null_thread = 0; |
| 532 | clockid_t c; |
| 533 | EXPECT_EQ(ESRCH, pthread_getcpuclockid(null_thread, &c)); |
Elliott Hughes | bcb1529 | 2017-02-07 21:05:30 +0000 | [diff] [blame] | 534 | } |
| 535 | |
Elliott Hughes | 11859d4 | 2017-02-13 17:59:29 -0800 | [diff] [blame] | 536 | TEST_F(pthread_DeathTest, pthread_getschedparam__no_such_thread) { |
Elliott Hughes | bcb1529 | 2017-02-07 21:05:30 +0000 | [diff] [blame] | 537 | pthread_t dead_thread; |
| 538 | MakeDeadThread(dead_thread); |
| 539 | |
| 540 | int policy; |
| 541 | sched_param param; |
Elliott Hughes | 6ce686c | 2017-02-21 13:15:20 -0800 | [diff] [blame^] | 542 | EXPECT_DEATH(pthread_getschedparam(dead_thread, &policy, ¶m), "invalid pthread_t"); |
| 543 | } |
| 544 | |
| 545 | TEST_F(pthread_DeathTest, pthread_getschedparam__null_thread) { |
| 546 | pthread_t null_thread = 0; |
| 547 | int policy; |
| 548 | sched_param param; |
| 549 | EXPECT_EQ(ESRCH, pthread_getschedparam(null_thread, &policy, ¶m)); |
Elliott Hughes | bcb1529 | 2017-02-07 21:05:30 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Elliott Hughes | 11859d4 | 2017-02-13 17:59:29 -0800 | [diff] [blame] | 552 | TEST_F(pthread_DeathTest, pthread_setschedparam__no_such_thread) { |
Elliott Hughes | bcb1529 | 2017-02-07 21:05:30 +0000 | [diff] [blame] | 553 | pthread_t dead_thread; |
| 554 | MakeDeadThread(dead_thread); |
| 555 | |
| 556 | int policy = 0; |
| 557 | sched_param param; |
Elliott Hughes | 6ce686c | 2017-02-21 13:15:20 -0800 | [diff] [blame^] | 558 | EXPECT_DEATH(pthread_setschedparam(dead_thread, policy, ¶m), "invalid pthread_t"); |
| 559 | } |
| 560 | |
| 561 | TEST_F(pthread_DeathTest, pthread_setschedparam__null_thread) { |
| 562 | pthread_t null_thread = 0; |
| 563 | int policy = 0; |
| 564 | sched_param param; |
| 565 | EXPECT_EQ(ESRCH, pthread_setschedparam(null_thread, policy, ¶m)); |
Elliott Hughes | bcb1529 | 2017-02-07 21:05:30 +0000 | [diff] [blame] | 566 | } |
| 567 | |
Elliott Hughes | 11859d4 | 2017-02-13 17:59:29 -0800 | [diff] [blame] | 568 | TEST_F(pthread_DeathTest, pthread_join__no_such_thread) { |
Elliott Hughes | 7484c21 | 2017-02-02 02:41:38 +0000 | [diff] [blame] | 569 | pthread_t dead_thread; |
| 570 | MakeDeadThread(dead_thread); |
| 571 | |
Elliott Hughes | 6ce686c | 2017-02-21 13:15:20 -0800 | [diff] [blame^] | 572 | EXPECT_DEATH(pthread_join(dead_thread, NULL), "invalid pthread_t"); |
| 573 | } |
| 574 | |
| 575 | TEST_F(pthread_DeathTest, pthread_join__null_thread) { |
| 576 | pthread_t null_thread = 0; |
| 577 | EXPECT_EQ(ESRCH, pthread_join(null_thread, NULL)); |
Elliott Hughes | 7484c21 | 2017-02-02 02:41:38 +0000 | [diff] [blame] | 578 | } |
| 579 | |
Elliott Hughes | 11859d4 | 2017-02-13 17:59:29 -0800 | [diff] [blame] | 580 | TEST_F(pthread_DeathTest, pthread_kill__no_such_thread) { |
Elliott Hughes | 7484c21 | 2017-02-02 02:41:38 +0000 | [diff] [blame] | 581 | pthread_t dead_thread; |
| 582 | MakeDeadThread(dead_thread); |
| 583 | |
Elliott Hughes | 6ce686c | 2017-02-21 13:15:20 -0800 | [diff] [blame^] | 584 | EXPECT_DEATH(pthread_kill(dead_thread, 0), "invalid pthread_t"); |
| 585 | } |
| 586 | |
| 587 | TEST_F(pthread_DeathTest, pthread_kill__null_thread) { |
| 588 | pthread_t null_thread = 0; |
| 589 | EXPECT_EQ(ESRCH, pthread_kill(null_thread, 0)); |
Elliott Hughes | 7484c21 | 2017-02-02 02:41:38 +0000 | [diff] [blame] | 590 | } |
| 591 | |
msg555 | 0f020d1 | 2013-06-06 14:59:28 -0400 | [diff] [blame] | 592 | TEST(pthread, pthread_join__multijoin) { |
Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 593 | SpinFunctionHelper spin_helper; |
msg555 | 0f020d1 | 2013-06-06 14:59:28 -0400 | [diff] [blame] | 594 | |
| 595 | pthread_t t1; |
Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 596 | ASSERT_EQ(0, pthread_create(&t1, NULL, spin_helper.GetFunction(), NULL)); |
msg555 | 0f020d1 | 2013-06-06 14:59:28 -0400 | [diff] [blame] | 597 | |
| 598 | pthread_t t2; |
| 599 | ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1))); |
| 600 | |
| 601 | sleep(1); // (Give t2 a chance to call pthread_join.) |
| 602 | |
| 603 | // Multiple joins to the same thread should fail. |
| 604 | ASSERT_EQ(EINVAL, pthread_join(t1, NULL)); |
| 605 | |
Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 606 | spin_helper.UnSpin(); |
msg555 | 0f020d1 | 2013-06-06 14:59:28 -0400 | [diff] [blame] | 607 | |
| 608 | // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes). |
| 609 | void* join_result; |
| 610 | ASSERT_EQ(0, pthread_join(t2, &join_result)); |
Elliott Hughes | 5b9310e | 2013-10-02 16:59:05 -0700 | [diff] [blame] | 611 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result)); |
msg555 | 0f020d1 | 2013-06-06 14:59:28 -0400 | [diff] [blame] | 612 | } |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 613 | |
Elliott Hughes | 70b24b1 | 2013-11-15 11:51:07 -0800 | [diff] [blame] | 614 | TEST(pthread, pthread_join__race) { |
| 615 | // http://b/11693195 --- pthread_join could return before the thread had actually exited. |
| 616 | // If the joiner unmapped the thread's stack, that could lead to SIGSEGV in the thread. |
| 617 | for (size_t i = 0; i < 1024; ++i) { |
Yabin Cui | a36158a | 2015-11-16 21:06:16 -0800 | [diff] [blame] | 618 | size_t stack_size = 640*1024; |
Elliott Hughes | 70b24b1 | 2013-11-15 11:51:07 -0800 | [diff] [blame] | 619 | void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0); |
| 620 | |
| 621 | pthread_attr_t a; |
| 622 | pthread_attr_init(&a); |
| 623 | pthread_attr_setstack(&a, stack, stack_size); |
| 624 | |
| 625 | pthread_t t; |
| 626 | ASSERT_EQ(0, pthread_create(&t, &a, IdFn, NULL)); |
| 627 | ASSERT_EQ(0, pthread_join(t, NULL)); |
| 628 | ASSERT_EQ(0, munmap(stack, stack_size)); |
| 629 | } |
| 630 | } |
| 631 | |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 632 | static void* GetActualGuardSizeFn(void* arg) { |
| 633 | pthread_attr_t attributes; |
| 634 | pthread_getattr_np(pthread_self(), &attributes); |
| 635 | pthread_attr_getguardsize(&attributes, reinterpret_cast<size_t*>(arg)); |
| 636 | return NULL; |
| 637 | } |
| 638 | |
| 639 | static size_t GetActualGuardSize(const pthread_attr_t& attributes) { |
| 640 | size_t result; |
| 641 | pthread_t t; |
| 642 | pthread_create(&t, &attributes, GetActualGuardSizeFn, &result); |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 643 | pthread_join(t, NULL); |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 644 | return result; |
| 645 | } |
| 646 | |
| 647 | static void* GetActualStackSizeFn(void* arg) { |
| 648 | pthread_attr_t attributes; |
| 649 | pthread_getattr_np(pthread_self(), &attributes); |
| 650 | pthread_attr_getstacksize(&attributes, reinterpret_cast<size_t*>(arg)); |
| 651 | return NULL; |
| 652 | } |
| 653 | |
| 654 | static size_t GetActualStackSize(const pthread_attr_t& attributes) { |
| 655 | size_t result; |
| 656 | pthread_t t; |
| 657 | pthread_create(&t, &attributes, GetActualStackSizeFn, &result); |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 658 | pthread_join(t, NULL); |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 659 | return result; |
| 660 | } |
| 661 | |
| 662 | TEST(pthread, pthread_attr_setguardsize) { |
| 663 | pthread_attr_t attributes; |
| 664 | ASSERT_EQ(0, pthread_attr_init(&attributes)); |
| 665 | |
| 666 | // Get the default guard size. |
| 667 | size_t default_guard_size; |
| 668 | ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &default_guard_size)); |
| 669 | |
| 670 | // No such thing as too small: will be rounded up to one page by pthread_create. |
| 671 | ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 128)); |
| 672 | size_t guard_size; |
| 673 | ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size)); |
| 674 | ASSERT_EQ(128U, guard_size); |
| 675 | ASSERT_EQ(4096U, GetActualGuardSize(attributes)); |
| 676 | |
| 677 | // Large enough and a multiple of the page size. |
| 678 | ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024)); |
| 679 | ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size)); |
| 680 | ASSERT_EQ(32*1024U, guard_size); |
| 681 | |
| 682 | // Large enough but not a multiple of the page size; will be rounded up by pthread_create. |
| 683 | ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024 + 1)); |
| 684 | ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size)); |
| 685 | ASSERT_EQ(32*1024U + 1, guard_size); |
| 686 | } |
| 687 | |
| 688 | TEST(pthread, pthread_attr_setstacksize) { |
| 689 | pthread_attr_t attributes; |
| 690 | ASSERT_EQ(0, pthread_attr_init(&attributes)); |
| 691 | |
| 692 | // Get the default stack size. |
| 693 | size_t default_stack_size; |
| 694 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &default_stack_size)); |
| 695 | |
| 696 | // Too small. |
| 697 | ASSERT_EQ(EINVAL, pthread_attr_setstacksize(&attributes, 128)); |
| 698 | size_t stack_size; |
| 699 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size)); |
| 700 | ASSERT_EQ(default_stack_size, stack_size); |
| 701 | ASSERT_GE(GetActualStackSize(attributes), default_stack_size); |
| 702 | |
Yabin Cui | 917d390 | 2015-01-08 12:32:42 -0800 | [diff] [blame] | 703 | // Large enough and a multiple of the page size; may be rounded up by pthread_create. |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 704 | ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024)); |
| 705 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size)); |
| 706 | ASSERT_EQ(32*1024U, stack_size); |
Yabin Cui | 917d390 | 2015-01-08 12:32:42 -0800 | [diff] [blame] | 707 | ASSERT_GE(GetActualStackSize(attributes), 32*1024U); |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 708 | |
Yabin Cui | 917d390 | 2015-01-08 12:32:42 -0800 | [diff] [blame] | 709 | // Large enough but not aligned; will be rounded up by pthread_create. |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 710 | ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024 + 1)); |
| 711 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size)); |
| 712 | ASSERT_EQ(32*1024U + 1, stack_size); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 713 | #if defined(__BIONIC__) |
Yabin Cui | 917d390 | 2015-01-08 12:32:42 -0800 | [diff] [blame] | 714 | ASSERT_GT(GetActualStackSize(attributes), 32*1024U + 1); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 715 | #else // __BIONIC__ |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 716 | // glibc rounds down, in violation of POSIX. They document this in their BUGS section. |
| 717 | ASSERT_EQ(GetActualStackSize(attributes), 32*1024U); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 718 | #endif // __BIONIC__ |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 719 | } |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 720 | |
Yabin Cui | 76615da | 2015-03-17 14:22:09 -0700 | [diff] [blame] | 721 | TEST(pthread, pthread_rwlockattr_smoke) { |
| 722 | pthread_rwlockattr_t attr; |
| 723 | ASSERT_EQ(0, pthread_rwlockattr_init(&attr)); |
| 724 | |
| 725 | int pshared_value_array[] = {PTHREAD_PROCESS_PRIVATE, PTHREAD_PROCESS_SHARED}; |
| 726 | for (size_t i = 0; i < sizeof(pshared_value_array) / sizeof(pshared_value_array[0]); ++i) { |
| 727 | ASSERT_EQ(0, pthread_rwlockattr_setpshared(&attr, pshared_value_array[i])); |
| 728 | int pshared; |
| 729 | ASSERT_EQ(0, pthread_rwlockattr_getpshared(&attr, &pshared)); |
| 730 | ASSERT_EQ(pshared_value_array[i], pshared); |
| 731 | } |
| 732 | |
| 733 | int kind_array[] = {PTHREAD_RWLOCK_PREFER_READER_NP, |
| 734 | PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP}; |
| 735 | for (size_t i = 0; i < sizeof(kind_array) / sizeof(kind_array[0]); ++i) { |
| 736 | ASSERT_EQ(0, pthread_rwlockattr_setkind_np(&attr, kind_array[i])); |
| 737 | int kind; |
| 738 | ASSERT_EQ(0, pthread_rwlockattr_getkind_np(&attr, &kind)); |
| 739 | ASSERT_EQ(kind_array[i], kind); |
| 740 | } |
| 741 | |
| 742 | ASSERT_EQ(0, pthread_rwlockattr_destroy(&attr)); |
| 743 | } |
| 744 | |
| 745 | TEST(pthread, pthread_rwlock_init_same_as_PTHREAD_RWLOCK_INITIALIZER) { |
| 746 | pthread_rwlock_t lock1 = PTHREAD_RWLOCK_INITIALIZER; |
| 747 | pthread_rwlock_t lock2; |
| 748 | ASSERT_EQ(0, pthread_rwlock_init(&lock2, NULL)); |
| 749 | ASSERT_EQ(0, memcmp(&lock1, &lock2, sizeof(lock1))); |
| 750 | } |
| 751 | |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 752 | TEST(pthread, pthread_rwlock_smoke) { |
| 753 | pthread_rwlock_t l; |
| 754 | ASSERT_EQ(0, pthread_rwlock_init(&l, NULL)); |
| 755 | |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 756 | // Single read lock |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 757 | ASSERT_EQ(0, pthread_rwlock_rdlock(&l)); |
| 758 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 759 | |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 760 | // Multiple read lock |
| 761 | ASSERT_EQ(0, pthread_rwlock_rdlock(&l)); |
| 762 | ASSERT_EQ(0, pthread_rwlock_rdlock(&l)); |
| 763 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 764 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 765 | |
| 766 | // Write lock |
Calin Juravle | 92687e4 | 2014-05-22 19:21:22 +0100 | [diff] [blame] | 767 | ASSERT_EQ(0, pthread_rwlock_wrlock(&l)); |
| 768 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 769 | |
| 770 | // Try writer lock |
| 771 | ASSERT_EQ(0, pthread_rwlock_trywrlock(&l)); |
| 772 | ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l)); |
| 773 | ASSERT_EQ(EBUSY, pthread_rwlock_tryrdlock(&l)); |
| 774 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 775 | |
| 776 | // Try reader lock |
| 777 | ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l)); |
| 778 | ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l)); |
| 779 | ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l)); |
| 780 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 781 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 782 | |
| 783 | // Try writer lock after unlock |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 784 | ASSERT_EQ(0, pthread_rwlock_wrlock(&l)); |
| 785 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 786 | |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 787 | // EDEADLK in "read after write" |
| 788 | ASSERT_EQ(0, pthread_rwlock_wrlock(&l)); |
| 789 | ASSERT_EQ(EDEADLK, pthread_rwlock_rdlock(&l)); |
| 790 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 791 | |
| 792 | // EDEADLK in "write after write" |
| 793 | ASSERT_EQ(0, pthread_rwlock_wrlock(&l)); |
| 794 | ASSERT_EQ(EDEADLK, pthread_rwlock_wrlock(&l)); |
| 795 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 796 | |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 797 | ASSERT_EQ(0, pthread_rwlock_destroy(&l)); |
| 798 | } |
| 799 | |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 800 | struct RwlockWakeupHelperArg { |
| 801 | pthread_rwlock_t lock; |
| 802 | enum Progress { |
| 803 | LOCK_INITIALIZED, |
| 804 | LOCK_WAITING, |
| 805 | LOCK_RELEASED, |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 806 | LOCK_ACCESSED, |
| 807 | LOCK_TIMEDOUT, |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 808 | }; |
| 809 | std::atomic<Progress> progress; |
Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 810 | std::atomic<pid_t> tid; |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 811 | std::function<int (pthread_rwlock_t*)> trylock_function; |
| 812 | std::function<int (pthread_rwlock_t*)> lock_function; |
| 813 | std::function<int (pthread_rwlock_t*, const timespec*)> timed_lock_function; |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 814 | }; |
| 815 | |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 816 | static void pthread_rwlock_wakeup_helper(RwlockWakeupHelperArg* arg) { |
Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 817 | arg->tid = gettid(); |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 818 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_INITIALIZED, arg->progress); |
| 819 | arg->progress = RwlockWakeupHelperArg::LOCK_WAITING; |
| 820 | |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 821 | ASSERT_EQ(EBUSY, arg->trylock_function(&arg->lock)); |
| 822 | ASSERT_EQ(0, arg->lock_function(&arg->lock)); |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 823 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_RELEASED, arg->progress); |
| 824 | ASSERT_EQ(0, pthread_rwlock_unlock(&arg->lock)); |
| 825 | |
| 826 | arg->progress = RwlockWakeupHelperArg::LOCK_ACCESSED; |
| 827 | } |
| 828 | |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 829 | static void test_pthread_rwlock_reader_wakeup_writer(std::function<int (pthread_rwlock_t*)> lock_function) { |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 830 | RwlockWakeupHelperArg wakeup_arg; |
| 831 | ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, NULL)); |
| 832 | ASSERT_EQ(0, pthread_rwlock_rdlock(&wakeup_arg.lock)); |
| 833 | wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED; |
Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 834 | wakeup_arg.tid = 0; |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 835 | wakeup_arg.trylock_function = pthread_rwlock_trywrlock; |
| 836 | wakeup_arg.lock_function = lock_function; |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 837 | |
| 838 | pthread_t thread; |
| 839 | ASSERT_EQ(0, pthread_create(&thread, NULL, |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 840 | reinterpret_cast<void* (*)(void*)>(pthread_rwlock_wakeup_helper), &wakeup_arg)); |
Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 841 | WaitUntilThreadSleep(wakeup_arg.tid); |
| 842 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress); |
| 843 | |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 844 | wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_RELEASED; |
| 845 | ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock)); |
| 846 | |
| 847 | ASSERT_EQ(0, pthread_join(thread, NULL)); |
| 848 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_ACCESSED, wakeup_arg.progress); |
| 849 | ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock)); |
| 850 | } |
| 851 | |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 852 | TEST(pthread, pthread_rwlock_reader_wakeup_writer) { |
| 853 | test_pthread_rwlock_reader_wakeup_writer(pthread_rwlock_wrlock); |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 854 | } |
| 855 | |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 856 | TEST(pthread, pthread_rwlock_reader_wakeup_writer_timedwait) { |
| 857 | timespec ts; |
| 858 | ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts)); |
| 859 | ts.tv_sec += 1; |
| 860 | test_pthread_rwlock_reader_wakeup_writer([&](pthread_rwlock_t* lock) { |
| 861 | return pthread_rwlock_timedwrlock(lock, &ts); |
| 862 | }); |
| 863 | } |
| 864 | |
| 865 | static void test_pthread_rwlock_writer_wakeup_reader(std::function<int (pthread_rwlock_t*)> lock_function) { |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 866 | RwlockWakeupHelperArg wakeup_arg; |
| 867 | ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, NULL)); |
| 868 | ASSERT_EQ(0, pthread_rwlock_wrlock(&wakeup_arg.lock)); |
| 869 | wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED; |
Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 870 | wakeup_arg.tid = 0; |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 871 | wakeup_arg.trylock_function = pthread_rwlock_tryrdlock; |
| 872 | wakeup_arg.lock_function = lock_function; |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 873 | |
| 874 | pthread_t thread; |
| 875 | ASSERT_EQ(0, pthread_create(&thread, NULL, |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 876 | reinterpret_cast<void* (*)(void*)>(pthread_rwlock_wakeup_helper), &wakeup_arg)); |
Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 877 | WaitUntilThreadSleep(wakeup_arg.tid); |
| 878 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress); |
| 879 | |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 880 | wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_RELEASED; |
| 881 | ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock)); |
| 882 | |
| 883 | ASSERT_EQ(0, pthread_join(thread, NULL)); |
| 884 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_ACCESSED, wakeup_arg.progress); |
| 885 | ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock)); |
| 886 | } |
| 887 | |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 888 | TEST(pthread, pthread_rwlock_writer_wakeup_reader) { |
| 889 | test_pthread_rwlock_writer_wakeup_reader(pthread_rwlock_rdlock); |
| 890 | } |
| 891 | |
| 892 | TEST(pthread, pthread_rwlock_writer_wakeup_reader_timedwait) { |
| 893 | timespec ts; |
| 894 | ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts)); |
| 895 | ts.tv_sec += 1; |
| 896 | test_pthread_rwlock_writer_wakeup_reader([&](pthread_rwlock_t* lock) { |
| 897 | return pthread_rwlock_timedrdlock(lock, &ts); |
| 898 | }); |
| 899 | } |
| 900 | |
| 901 | static void pthread_rwlock_wakeup_timeout_helper(RwlockWakeupHelperArg* arg) { |
| 902 | arg->tid = gettid(); |
| 903 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_INITIALIZED, arg->progress); |
| 904 | arg->progress = RwlockWakeupHelperArg::LOCK_WAITING; |
| 905 | |
| 906 | ASSERT_EQ(EBUSY, arg->trylock_function(&arg->lock)); |
| 907 | |
| 908 | timespec ts; |
| 909 | ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts)); |
| 910 | ASSERT_EQ(ETIMEDOUT, arg->timed_lock_function(&arg->lock, &ts)); |
| 911 | ts.tv_nsec = -1; |
| 912 | ASSERT_EQ(EINVAL, arg->timed_lock_function(&arg->lock, &ts)); |
| 913 | ts.tv_nsec = NS_PER_S; |
| 914 | ASSERT_EQ(EINVAL, arg->timed_lock_function(&arg->lock, &ts)); |
| 915 | ts.tv_nsec = NS_PER_S - 1; |
| 916 | ts.tv_sec = -1; |
| 917 | ASSERT_EQ(ETIMEDOUT, arg->timed_lock_function(&arg->lock, &ts)); |
| 918 | ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts)); |
| 919 | ts.tv_sec += 1; |
| 920 | ASSERT_EQ(ETIMEDOUT, arg->timed_lock_function(&arg->lock, &ts)); |
| 921 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, arg->progress); |
| 922 | arg->progress = RwlockWakeupHelperArg::LOCK_TIMEDOUT; |
| 923 | } |
| 924 | |
| 925 | TEST(pthread, pthread_rwlock_timedrdlock_timeout) { |
| 926 | RwlockWakeupHelperArg wakeup_arg; |
| 927 | ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, nullptr)); |
| 928 | ASSERT_EQ(0, pthread_rwlock_wrlock(&wakeup_arg.lock)); |
| 929 | wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED; |
| 930 | wakeup_arg.tid = 0; |
| 931 | wakeup_arg.trylock_function = pthread_rwlock_tryrdlock; |
| 932 | wakeup_arg.timed_lock_function = pthread_rwlock_timedrdlock; |
| 933 | |
| 934 | pthread_t thread; |
| 935 | ASSERT_EQ(0, pthread_create(&thread, nullptr, |
| 936 | reinterpret_cast<void* (*)(void*)>(pthread_rwlock_wakeup_timeout_helper), &wakeup_arg)); |
| 937 | WaitUntilThreadSleep(wakeup_arg.tid); |
| 938 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress); |
| 939 | |
| 940 | ASSERT_EQ(0, pthread_join(thread, nullptr)); |
| 941 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_TIMEDOUT, wakeup_arg.progress); |
| 942 | ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock)); |
| 943 | ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock)); |
| 944 | } |
| 945 | |
| 946 | TEST(pthread, pthread_rwlock_timedwrlock_timeout) { |
| 947 | RwlockWakeupHelperArg wakeup_arg; |
| 948 | ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, nullptr)); |
| 949 | ASSERT_EQ(0, pthread_rwlock_rdlock(&wakeup_arg.lock)); |
| 950 | wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED; |
| 951 | wakeup_arg.tid = 0; |
| 952 | wakeup_arg.trylock_function = pthread_rwlock_trywrlock; |
| 953 | wakeup_arg.timed_lock_function = pthread_rwlock_timedwrlock; |
| 954 | |
| 955 | pthread_t thread; |
| 956 | ASSERT_EQ(0, pthread_create(&thread, nullptr, |
| 957 | reinterpret_cast<void* (*)(void*)>(pthread_rwlock_wakeup_timeout_helper), &wakeup_arg)); |
| 958 | WaitUntilThreadSleep(wakeup_arg.tid); |
| 959 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress); |
| 960 | |
| 961 | ASSERT_EQ(0, pthread_join(thread, nullptr)); |
| 962 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_TIMEDOUT, wakeup_arg.progress); |
| 963 | ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock)); |
| 964 | ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock)); |
| 965 | } |
| 966 | |
Yabin Cui | 76615da | 2015-03-17 14:22:09 -0700 | [diff] [blame] | 967 | class RwlockKindTestHelper { |
| 968 | private: |
| 969 | struct ThreadArg { |
| 970 | RwlockKindTestHelper* helper; |
| 971 | std::atomic<pid_t>& tid; |
| 972 | |
| 973 | ThreadArg(RwlockKindTestHelper* helper, std::atomic<pid_t>& tid) |
| 974 | : helper(helper), tid(tid) { } |
| 975 | }; |
| 976 | |
| 977 | public: |
| 978 | pthread_rwlock_t lock; |
| 979 | |
| 980 | public: |
Chih-Hung Hsieh | 62e3a07 | 2016-05-03 12:08:05 -0700 | [diff] [blame] | 981 | explicit RwlockKindTestHelper(int kind_type) { |
Yabin Cui | 76615da | 2015-03-17 14:22:09 -0700 | [diff] [blame] | 982 | InitRwlock(kind_type); |
| 983 | } |
| 984 | |
| 985 | ~RwlockKindTestHelper() { |
| 986 | DestroyRwlock(); |
| 987 | } |
| 988 | |
| 989 | void CreateWriterThread(pthread_t& thread, std::atomic<pid_t>& tid) { |
| 990 | tid = 0; |
| 991 | ThreadArg* arg = new ThreadArg(this, tid); |
| 992 | ASSERT_EQ(0, pthread_create(&thread, NULL, |
| 993 | reinterpret_cast<void* (*)(void*)>(WriterThreadFn), arg)); |
| 994 | } |
| 995 | |
| 996 | void CreateReaderThread(pthread_t& thread, std::atomic<pid_t>& tid) { |
| 997 | tid = 0; |
| 998 | ThreadArg* arg = new ThreadArg(this, tid); |
| 999 | ASSERT_EQ(0, pthread_create(&thread, NULL, |
| 1000 | reinterpret_cast<void* (*)(void*)>(ReaderThreadFn), arg)); |
| 1001 | } |
| 1002 | |
| 1003 | private: |
| 1004 | void InitRwlock(int kind_type) { |
| 1005 | pthread_rwlockattr_t attr; |
| 1006 | ASSERT_EQ(0, pthread_rwlockattr_init(&attr)); |
| 1007 | ASSERT_EQ(0, pthread_rwlockattr_setkind_np(&attr, kind_type)); |
| 1008 | ASSERT_EQ(0, pthread_rwlock_init(&lock, &attr)); |
| 1009 | ASSERT_EQ(0, pthread_rwlockattr_destroy(&attr)); |
| 1010 | } |
| 1011 | |
| 1012 | void DestroyRwlock() { |
| 1013 | ASSERT_EQ(0, pthread_rwlock_destroy(&lock)); |
| 1014 | } |
| 1015 | |
| 1016 | static void WriterThreadFn(ThreadArg* arg) { |
| 1017 | arg->tid = gettid(); |
| 1018 | |
| 1019 | RwlockKindTestHelper* helper = arg->helper; |
| 1020 | ASSERT_EQ(0, pthread_rwlock_wrlock(&helper->lock)); |
| 1021 | ASSERT_EQ(0, pthread_rwlock_unlock(&helper->lock)); |
| 1022 | delete arg; |
| 1023 | } |
| 1024 | |
| 1025 | static void ReaderThreadFn(ThreadArg* arg) { |
| 1026 | arg->tid = gettid(); |
| 1027 | |
| 1028 | RwlockKindTestHelper* helper = arg->helper; |
| 1029 | ASSERT_EQ(0, pthread_rwlock_rdlock(&helper->lock)); |
| 1030 | ASSERT_EQ(0, pthread_rwlock_unlock(&helper->lock)); |
| 1031 | delete arg; |
| 1032 | } |
| 1033 | }; |
| 1034 | |
| 1035 | TEST(pthread, pthread_rwlock_kind_PTHREAD_RWLOCK_PREFER_READER_NP) { |
| 1036 | RwlockKindTestHelper helper(PTHREAD_RWLOCK_PREFER_READER_NP); |
| 1037 | ASSERT_EQ(0, pthread_rwlock_rdlock(&helper.lock)); |
| 1038 | |
| 1039 | pthread_t writer_thread; |
| 1040 | std::atomic<pid_t> writer_tid; |
| 1041 | helper.CreateWriterThread(writer_thread, writer_tid); |
| 1042 | WaitUntilThreadSleep(writer_tid); |
| 1043 | |
| 1044 | pthread_t reader_thread; |
| 1045 | std::atomic<pid_t> reader_tid; |
| 1046 | helper.CreateReaderThread(reader_thread, reader_tid); |
| 1047 | ASSERT_EQ(0, pthread_join(reader_thread, NULL)); |
| 1048 | |
| 1049 | ASSERT_EQ(0, pthread_rwlock_unlock(&helper.lock)); |
| 1050 | ASSERT_EQ(0, pthread_join(writer_thread, NULL)); |
| 1051 | } |
| 1052 | |
| 1053 | TEST(pthread, pthread_rwlock_kind_PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP) { |
| 1054 | RwlockKindTestHelper helper(PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP); |
| 1055 | ASSERT_EQ(0, pthread_rwlock_rdlock(&helper.lock)); |
| 1056 | |
| 1057 | pthread_t writer_thread; |
| 1058 | std::atomic<pid_t> writer_tid; |
| 1059 | helper.CreateWriterThread(writer_thread, writer_tid); |
| 1060 | WaitUntilThreadSleep(writer_tid); |
| 1061 | |
| 1062 | pthread_t reader_thread; |
| 1063 | std::atomic<pid_t> reader_tid; |
| 1064 | helper.CreateReaderThread(reader_thread, reader_tid); |
| 1065 | WaitUntilThreadSleep(reader_tid); |
| 1066 | |
| 1067 | ASSERT_EQ(0, pthread_rwlock_unlock(&helper.lock)); |
| 1068 | ASSERT_EQ(0, pthread_join(writer_thread, NULL)); |
| 1069 | ASSERT_EQ(0, pthread_join(reader_thread, NULL)); |
| 1070 | } |
| 1071 | |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 1072 | static int g_once_fn_call_count = 0; |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 1073 | static void OnceFn() { |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 1074 | ++g_once_fn_call_count; |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 1075 | } |
| 1076 | |
| 1077 | TEST(pthread, pthread_once_smoke) { |
| 1078 | pthread_once_t once_control = PTHREAD_ONCE_INIT; |
| 1079 | ASSERT_EQ(0, pthread_once(&once_control, OnceFn)); |
| 1080 | ASSERT_EQ(0, pthread_once(&once_control, OnceFn)); |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 1081 | ASSERT_EQ(1, g_once_fn_call_count); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 1082 | } |
| 1083 | |
Elliott Hughes | 3694ec6 | 2014-05-14 11:46:08 -0700 | [diff] [blame] | 1084 | static std::string pthread_once_1934122_result = ""; |
| 1085 | |
| 1086 | static void Routine2() { |
| 1087 | pthread_once_1934122_result += "2"; |
| 1088 | } |
| 1089 | |
| 1090 | static void Routine1() { |
| 1091 | pthread_once_t once_control_2 = PTHREAD_ONCE_INIT; |
| 1092 | pthread_once_1934122_result += "1"; |
| 1093 | pthread_once(&once_control_2, &Routine2); |
| 1094 | } |
| 1095 | |
| 1096 | TEST(pthread, pthread_once_1934122) { |
| 1097 | // Very old versions of Android couldn't call pthread_once from a |
| 1098 | // pthread_once init routine. http://b/1934122. |
| 1099 | pthread_once_t once_control_1 = PTHREAD_ONCE_INIT; |
| 1100 | ASSERT_EQ(0, pthread_once(&once_control_1, &Routine1)); |
| 1101 | ASSERT_EQ("12", pthread_once_1934122_result); |
| 1102 | } |
| 1103 | |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 1104 | static int g_atfork_prepare_calls = 0; |
Dmitriy Ivanov | ea295f6 | 2014-11-20 20:47:02 -0800 | [diff] [blame] | 1105 | static void AtForkPrepare1() { g_atfork_prepare_calls = (g_atfork_prepare_calls * 10) + 1; } |
| 1106 | static void AtForkPrepare2() { g_atfork_prepare_calls = (g_atfork_prepare_calls * 10) + 2; } |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 1107 | static int g_atfork_parent_calls = 0; |
Dmitriy Ivanov | ea295f6 | 2014-11-20 20:47:02 -0800 | [diff] [blame] | 1108 | static void AtForkParent1() { g_atfork_parent_calls = (g_atfork_parent_calls * 10) + 1; } |
| 1109 | static void AtForkParent2() { g_atfork_parent_calls = (g_atfork_parent_calls * 10) + 2; } |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 1110 | static int g_atfork_child_calls = 0; |
Dmitriy Ivanov | ea295f6 | 2014-11-20 20:47:02 -0800 | [diff] [blame] | 1111 | static void AtForkChild1() { g_atfork_child_calls = (g_atfork_child_calls * 10) + 1; } |
| 1112 | static void AtForkChild2() { g_atfork_child_calls = (g_atfork_child_calls * 10) + 2; } |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 1113 | |
Dmitriy Ivanov | 00e3781 | 2014-11-20 16:53:47 -0800 | [diff] [blame] | 1114 | TEST(pthread, pthread_atfork_smoke) { |
Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 1115 | ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1)); |
| 1116 | ASSERT_EQ(0, pthread_atfork(AtForkPrepare2, AtForkParent2, AtForkChild2)); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 1117 | |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 1118 | pid_t pid = fork(); |
Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 1119 | ASSERT_NE(-1, pid) << strerror(errno); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 1120 | |
Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 1121 | // Child and parent calls are made in the order they were registered. |
| 1122 | if (pid == 0) { |
Dmitriy Ivanov | ea295f6 | 2014-11-20 20:47:02 -0800 | [diff] [blame] | 1123 | ASSERT_EQ(12, g_atfork_child_calls); |
Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 1124 | _exit(0); |
| 1125 | } |
Dmitriy Ivanov | ea295f6 | 2014-11-20 20:47:02 -0800 | [diff] [blame] | 1126 | ASSERT_EQ(12, g_atfork_parent_calls); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 1127 | |
Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 1128 | // Prepare calls are made in the reverse order. |
Dmitriy Ivanov | ea295f6 | 2014-11-20 20:47:02 -0800 | [diff] [blame] | 1129 | ASSERT_EQ(21, g_atfork_prepare_calls); |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 1130 | AssertChildExited(pid, 0); |
Dmitriy Ivanov | ea295f6 | 2014-11-20 20:47:02 -0800 | [diff] [blame] | 1131 | } |
| 1132 | |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 1133 | TEST(pthread, pthread_attr_getscope) { |
| 1134 | pthread_attr_t attr; |
| 1135 | ASSERT_EQ(0, pthread_attr_init(&attr)); |
| 1136 | |
| 1137 | int scope; |
| 1138 | ASSERT_EQ(0, pthread_attr_getscope(&attr, &scope)); |
| 1139 | ASSERT_EQ(PTHREAD_SCOPE_SYSTEM, scope); |
| 1140 | } |
Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 1141 | |
| 1142 | TEST(pthread, pthread_condattr_init) { |
| 1143 | pthread_condattr_t attr; |
| 1144 | pthread_condattr_init(&attr); |
| 1145 | |
| 1146 | clockid_t clock; |
| 1147 | ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock)); |
| 1148 | ASSERT_EQ(CLOCK_REALTIME, clock); |
| 1149 | |
| 1150 | int pshared; |
| 1151 | ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared)); |
| 1152 | ASSERT_EQ(PTHREAD_PROCESS_PRIVATE, pshared); |
| 1153 | } |
| 1154 | |
| 1155 | TEST(pthread, pthread_condattr_setclock) { |
| 1156 | pthread_condattr_t attr; |
| 1157 | pthread_condattr_init(&attr); |
| 1158 | |
| 1159 | ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_REALTIME)); |
| 1160 | clockid_t clock; |
| 1161 | ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock)); |
| 1162 | ASSERT_EQ(CLOCK_REALTIME, clock); |
| 1163 | |
| 1164 | ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC)); |
| 1165 | ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock)); |
| 1166 | ASSERT_EQ(CLOCK_MONOTONIC, clock); |
| 1167 | |
| 1168 | ASSERT_EQ(EINVAL, pthread_condattr_setclock(&attr, CLOCK_PROCESS_CPUTIME_ID)); |
| 1169 | } |
| 1170 | |
| 1171 | TEST(pthread, pthread_cond_broadcast__preserves_condattr_flags) { |
Yabin Cui | 32651b8 | 2015-03-13 20:30:00 -0700 | [diff] [blame] | 1172 | #if defined(__BIONIC__) |
Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 1173 | pthread_condattr_t attr; |
| 1174 | pthread_condattr_init(&attr); |
| 1175 | |
| 1176 | ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC)); |
| 1177 | ASSERT_EQ(0, pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED)); |
| 1178 | |
| 1179 | pthread_cond_t cond_var; |
| 1180 | ASSERT_EQ(0, pthread_cond_init(&cond_var, &attr)); |
| 1181 | |
| 1182 | ASSERT_EQ(0, pthread_cond_signal(&cond_var)); |
| 1183 | ASSERT_EQ(0, pthread_cond_broadcast(&cond_var)); |
| 1184 | |
Yabin Cui | 32651b8 | 2015-03-13 20:30:00 -0700 | [diff] [blame] | 1185 | attr = static_cast<pthread_condattr_t>(*reinterpret_cast<uint32_t*>(cond_var.__private)); |
Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 1186 | clockid_t clock; |
| 1187 | ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock)); |
| 1188 | ASSERT_EQ(CLOCK_MONOTONIC, clock); |
| 1189 | int pshared; |
| 1190 | ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared)); |
| 1191 | ASSERT_EQ(PTHREAD_PROCESS_SHARED, pshared); |
Yabin Cui | 32651b8 | 2015-03-13 20:30:00 -0700 | [diff] [blame] | 1192 | #else // !defined(__BIONIC__) |
| 1193 | GTEST_LOG_(INFO) << "This tests a bionic implementation detail.\n"; |
| 1194 | #endif // !defined(__BIONIC__) |
| 1195 | } |
| 1196 | |
| 1197 | class pthread_CondWakeupTest : public ::testing::Test { |
| 1198 | protected: |
| 1199 | pthread_mutex_t mutex; |
| 1200 | pthread_cond_t cond; |
| 1201 | |
| 1202 | enum Progress { |
| 1203 | INITIALIZED, |
| 1204 | WAITING, |
| 1205 | SIGNALED, |
| 1206 | FINISHED, |
| 1207 | }; |
| 1208 | std::atomic<Progress> progress; |
| 1209 | pthread_t thread; |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1210 | std::function<int (pthread_cond_t* cond, pthread_mutex_t* mutex)> wait_function; |
Yabin Cui | 32651b8 | 2015-03-13 20:30:00 -0700 | [diff] [blame] | 1211 | |
| 1212 | protected: |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1213 | void SetUp() override { |
| 1214 | ASSERT_EQ(0, pthread_mutex_init(&mutex, nullptr)); |
| 1215 | } |
| 1216 | |
| 1217 | void InitCond(clockid_t clock=CLOCK_REALTIME) { |
| 1218 | pthread_condattr_t attr; |
| 1219 | ASSERT_EQ(0, pthread_condattr_init(&attr)); |
| 1220 | ASSERT_EQ(0, pthread_condattr_setclock(&attr, clock)); |
| 1221 | ASSERT_EQ(0, pthread_cond_init(&cond, &attr)); |
| 1222 | ASSERT_EQ(0, pthread_condattr_destroy(&attr)); |
| 1223 | } |
| 1224 | |
| 1225 | void StartWaitingThread(std::function<int (pthread_cond_t* cond, pthread_mutex_t* mutex)> wait_function) { |
Yabin Cui | 32651b8 | 2015-03-13 20:30:00 -0700 | [diff] [blame] | 1226 | progress = INITIALIZED; |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1227 | this->wait_function = wait_function; |
| 1228 | ASSERT_EQ(0, pthread_create(&thread, NULL, reinterpret_cast<void* (*)(void*)>(WaitThreadFn), this)); |
| 1229 | while (progress != WAITING) { |
Yabin Cui | 32651b8 | 2015-03-13 20:30:00 -0700 | [diff] [blame] | 1230 | usleep(5000); |
| 1231 | } |
| 1232 | usleep(5000); |
| 1233 | } |
| 1234 | |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1235 | void TearDown() override { |
| 1236 | ASSERT_EQ(0, pthread_join(thread, nullptr)); |
| 1237 | ASSERT_EQ(FINISHED, progress); |
| 1238 | ASSERT_EQ(0, pthread_cond_destroy(&cond)); |
| 1239 | ASSERT_EQ(0, pthread_mutex_destroy(&mutex)); |
| 1240 | } |
| 1241 | |
Yabin Cui | 32651b8 | 2015-03-13 20:30:00 -0700 | [diff] [blame] | 1242 | private: |
| 1243 | static void WaitThreadFn(pthread_CondWakeupTest* test) { |
| 1244 | ASSERT_EQ(0, pthread_mutex_lock(&test->mutex)); |
| 1245 | test->progress = WAITING; |
| 1246 | while (test->progress == WAITING) { |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1247 | ASSERT_EQ(0, test->wait_function(&test->cond, &test->mutex)); |
Yabin Cui | 32651b8 | 2015-03-13 20:30:00 -0700 | [diff] [blame] | 1248 | } |
| 1249 | ASSERT_EQ(SIGNALED, test->progress); |
| 1250 | test->progress = FINISHED; |
| 1251 | ASSERT_EQ(0, pthread_mutex_unlock(&test->mutex)); |
| 1252 | } |
| 1253 | }; |
| 1254 | |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1255 | TEST_F(pthread_CondWakeupTest, signal_wait) { |
| 1256 | InitCond(); |
| 1257 | StartWaitingThread([](pthread_cond_t* cond, pthread_mutex_t* mutex) { |
| 1258 | return pthread_cond_wait(cond, mutex); |
| 1259 | }); |
Yabin Cui | 32651b8 | 2015-03-13 20:30:00 -0700 | [diff] [blame] | 1260 | progress = SIGNALED; |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1261 | ASSERT_EQ(0, pthread_cond_signal(&cond)); |
Yabin Cui | 32651b8 | 2015-03-13 20:30:00 -0700 | [diff] [blame] | 1262 | } |
| 1263 | |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1264 | TEST_F(pthread_CondWakeupTest, broadcast_wait) { |
| 1265 | InitCond(); |
| 1266 | StartWaitingThread([](pthread_cond_t* cond, pthread_mutex_t* mutex) { |
| 1267 | return pthread_cond_wait(cond, mutex); |
| 1268 | }); |
Yabin Cui | 32651b8 | 2015-03-13 20:30:00 -0700 | [diff] [blame] | 1269 | progress = SIGNALED; |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1270 | ASSERT_EQ(0, pthread_cond_broadcast(&cond)); |
Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 1271 | } |
Elliott Hughes | 0e714a5 | 2014-03-03 16:42:47 -0800 | [diff] [blame] | 1272 | |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1273 | TEST_F(pthread_CondWakeupTest, signal_timedwait_CLOCK_REALTIME) { |
| 1274 | InitCond(CLOCK_REALTIME); |
Elliott Hughes | 0e714a5 | 2014-03-03 16:42:47 -0800 | [diff] [blame] | 1275 | timespec ts; |
| 1276 | ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts)); |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1277 | ts.tv_sec += 1; |
| 1278 | StartWaitingThread([&](pthread_cond_t* cond, pthread_mutex_t* mutex) { |
| 1279 | return pthread_cond_timedwait(cond, mutex, &ts); |
| 1280 | }); |
| 1281 | progress = SIGNALED; |
| 1282 | ASSERT_EQ(0, pthread_cond_signal(&cond)); |
| 1283 | } |
Elliott Hughes | 0e714a5 | 2014-03-03 16:42:47 -0800 | [diff] [blame] | 1284 | |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1285 | TEST_F(pthread_CondWakeupTest, signal_timedwait_CLOCK_MONOTONIC) { |
| 1286 | InitCond(CLOCK_MONOTONIC); |
| 1287 | timespec ts; |
| 1288 | ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts)); |
| 1289 | ts.tv_sec += 1; |
| 1290 | StartWaitingThread([&](pthread_cond_t* cond, pthread_mutex_t* mutex) { |
| 1291 | return pthread_cond_timedwait(cond, mutex, &ts); |
| 1292 | }); |
| 1293 | progress = SIGNALED; |
| 1294 | ASSERT_EQ(0, pthread_cond_signal(&cond)); |
| 1295 | } |
Elliott Hughes | 0e714a5 | 2014-03-03 16:42:47 -0800 | [diff] [blame] | 1296 | |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1297 | TEST(pthread, pthread_cond_timedwait_timeout) { |
| 1298 | pthread_mutex_t mutex; |
| 1299 | ASSERT_EQ(0, pthread_mutex_init(&mutex, nullptr)); |
| 1300 | pthread_cond_t cond; |
| 1301 | ASSERT_EQ(0, pthread_cond_init(&cond, nullptr)); |
| 1302 | ASSERT_EQ(0, pthread_mutex_lock(&mutex)); |
| 1303 | timespec ts; |
Elliott Hughes | 0e714a5 | 2014-03-03 16:42:47 -0800 | [diff] [blame] | 1304 | ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts)); |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1305 | ASSERT_EQ(ETIMEDOUT, pthread_cond_timedwait(&cond, &mutex, &ts)); |
| 1306 | ts.tv_nsec = -1; |
| 1307 | ASSERT_EQ(EINVAL, pthread_cond_timedwait(&cond, &mutex, &ts)); |
| 1308 | ts.tv_nsec = NS_PER_S; |
| 1309 | ASSERT_EQ(EINVAL, pthread_cond_timedwait(&cond, &mutex, &ts)); |
| 1310 | ts.tv_nsec = NS_PER_S - 1; |
| 1311 | ts.tv_sec = -1; |
| 1312 | ASSERT_EQ(ETIMEDOUT, pthread_cond_timedwait(&cond, &mutex, &ts)); |
| 1313 | ASSERT_EQ(0, pthread_mutex_unlock(&mutex)); |
Elliott Hughes | 0e714a5 | 2014-03-03 16:42:47 -0800 | [diff] [blame] | 1314 | } |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 1315 | |
| 1316 | TEST(pthread, pthread_attr_getstack__main_thread) { |
| 1317 | // This test is only meaningful for the main thread, so make sure we're running on it! |
| 1318 | ASSERT_EQ(getpid(), syscall(__NR_gettid)); |
| 1319 | |
| 1320 | // Get the main thread's attributes. |
| 1321 | pthread_attr_t attributes; |
| 1322 | ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes)); |
| 1323 | |
| 1324 | // Check that we correctly report that the main thread has no guard page. |
| 1325 | size_t guard_size; |
| 1326 | ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size)); |
| 1327 | ASSERT_EQ(0U, guard_size); // The main thread has no guard page. |
| 1328 | |
| 1329 | // Get the stack base and the stack size (both ways). |
| 1330 | void* stack_base; |
| 1331 | size_t stack_size; |
| 1332 | ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size)); |
| 1333 | size_t stack_size2; |
| 1334 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2)); |
| 1335 | |
| 1336 | // The two methods of asking for the stack size should agree. |
| 1337 | EXPECT_EQ(stack_size, stack_size2); |
| 1338 | |
Yabin Cui | b0c6f2db | 2015-05-19 15:09:23 -0700 | [diff] [blame] | 1339 | #if defined(__BIONIC__) |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 1340 | // What does /proc/self/maps' [stack] line say? |
Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 1341 | void* maps_stack_hi = NULL; |
Elliott Hughes | 15dfd63 | 2015-09-22 16:40:14 -0700 | [diff] [blame] | 1342 | std::vector<map_record> maps; |
| 1343 | ASSERT_TRUE(Maps::parse_maps(&maps)); |
Elliott Hughes | 0b2acdf | 2015-10-02 18:25:19 -0700 | [diff] [blame] | 1344 | for (const auto& map : maps) { |
Elliott Hughes | 15dfd63 | 2015-09-22 16:40:14 -0700 | [diff] [blame] | 1345 | if (map.pathname == "[stack]") { |
| 1346 | maps_stack_hi = reinterpret_cast<void*>(map.addr_end); |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 1347 | break; |
| 1348 | } |
| 1349 | } |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 1350 | |
Yabin Cui | b0c6f2db | 2015-05-19 15:09:23 -0700 | [diff] [blame] | 1351 | // The high address of the /proc/self/maps [stack] region should equal stack_base + stack_size. |
| 1352 | // Remember that the stack grows down (and is mapped in on demand), so the low address of the |
| 1353 | // region isn't very interesting. |
| 1354 | EXPECT_EQ(maps_stack_hi, reinterpret_cast<uint8_t*>(stack_base) + stack_size); |
| 1355 | |
Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 1356 | // The stack size should correspond to RLIMIT_STACK. |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 1357 | rlimit rl; |
Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 1358 | ASSERT_EQ(0, getrlimit(RLIMIT_STACK, &rl)); |
Elliott Hughes | 27a9aed | 2014-09-04 16:09:25 -0700 | [diff] [blame] | 1359 | uint64_t original_rlim_cur = rl.rlim_cur; |
Elliott Hughes | 27a9aed | 2014-09-04 16:09:25 -0700 | [diff] [blame] | 1360 | if (rl.rlim_cur == RLIM_INFINITY) { |
| 1361 | rl.rlim_cur = 8 * 1024 * 1024; // Bionic reports unlimited stacks as 8MiB. |
| 1362 | } |
Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 1363 | EXPECT_EQ(rl.rlim_cur, stack_size); |
| 1364 | |
Dmitriy Ivanov | d9ff722 | 2014-09-08 16:22:22 -0700 | [diff] [blame] | 1365 | auto guard = make_scope_guard([&rl, original_rlim_cur]() { |
Elliott Hughes | 27a9aed | 2014-09-04 16:09:25 -0700 | [diff] [blame] | 1366 | rl.rlim_cur = original_rlim_cur; |
| 1367 | ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl)); |
| 1368 | }); |
| 1369 | |
Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 1370 | // |
| 1371 | // What if RLIMIT_STACK is smaller than the stack's current extent? |
| 1372 | // |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 1373 | rl.rlim_cur = rl.rlim_max = 1024; // 1KiB. We know the stack must be at least a page already. |
| 1374 | rl.rlim_max = RLIM_INFINITY; |
| 1375 | ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl)); |
| 1376 | |
| 1377 | ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes)); |
| 1378 | ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size)); |
| 1379 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2)); |
| 1380 | |
| 1381 | EXPECT_EQ(stack_size, stack_size2); |
| 1382 | ASSERT_EQ(1024U, stack_size); |
| 1383 | |
| 1384 | // |
Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 1385 | // What if RLIMIT_STACK isn't a whole number of pages? |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 1386 | // |
| 1387 | rl.rlim_cur = rl.rlim_max = 6666; // Not a whole number of pages. |
| 1388 | rl.rlim_max = RLIM_INFINITY; |
| 1389 | ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl)); |
| 1390 | |
| 1391 | ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes)); |
| 1392 | ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size)); |
| 1393 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2)); |
| 1394 | |
| 1395 | EXPECT_EQ(stack_size, stack_size2); |
| 1396 | ASSERT_EQ(6666U, stack_size); |
Yabin Cui | b0c6f2db | 2015-05-19 15:09:23 -0700 | [diff] [blame] | 1397 | #endif |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 1398 | } |
Elliott Hughes | 8fb639c | 2014-09-12 14:43:07 -0700 | [diff] [blame] | 1399 | |
Mor-sarid, Nitzan | 5693332 | 2015-09-11 05:31:36 +0000 | [diff] [blame] | 1400 | struct GetStackSignalHandlerArg { |
| 1401 | volatile bool done; |
Chih-Hung Hsieh | 9af13d2 | 2016-06-02 14:40:09 -0700 | [diff] [blame] | 1402 | void* signal_stack_base; |
| 1403 | size_t signal_stack_size; |
Mor-sarid, Nitzan | 5693332 | 2015-09-11 05:31:36 +0000 | [diff] [blame] | 1404 | void* main_stack_base; |
| 1405 | size_t main_stack_size; |
| 1406 | }; |
| 1407 | |
| 1408 | static GetStackSignalHandlerArg getstack_signal_handler_arg; |
| 1409 | |
| 1410 | static void getstack_signal_handler(int sig) { |
| 1411 | ASSERT_EQ(SIGUSR1, sig); |
| 1412 | // Use sleep() to make current thread be switched out by the kernel to provoke the error. |
| 1413 | sleep(1); |
| 1414 | pthread_attr_t attr; |
| 1415 | ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attr)); |
| 1416 | void* stack_base; |
| 1417 | size_t stack_size; |
| 1418 | ASSERT_EQ(0, pthread_attr_getstack(&attr, &stack_base, &stack_size)); |
Chih-Hung Hsieh | 9af13d2 | 2016-06-02 14:40:09 -0700 | [diff] [blame] | 1419 | |
| 1420 | // Verify if the stack used by the signal handler is the alternate stack just registered. |
| 1421 | ASSERT_LE(getstack_signal_handler_arg.signal_stack_base, &attr); |
| 1422 | ASSERT_LT(static_cast<void*>(&attr), |
| 1423 | static_cast<char*>(getstack_signal_handler_arg.signal_stack_base) + |
| 1424 | getstack_signal_handler_arg.signal_stack_size); |
| 1425 | |
| 1426 | // Verify if the main thread's stack got in the signal handler is correct. |
| 1427 | ASSERT_EQ(getstack_signal_handler_arg.main_stack_base, stack_base); |
| 1428 | ASSERT_LE(getstack_signal_handler_arg.main_stack_size, stack_size); |
| 1429 | |
Mor-sarid, Nitzan | 5693332 | 2015-09-11 05:31:36 +0000 | [diff] [blame] | 1430 | getstack_signal_handler_arg.done = true; |
| 1431 | } |
| 1432 | |
| 1433 | // The previous code obtained the main thread's stack by reading the entry in |
| 1434 | // /proc/self/task/<pid>/maps that was labeled [stack]. Unfortunately, on x86/x86_64, the kernel |
| 1435 | // relies on sp0 in task state segment(tss) to label the stack map with [stack]. If the kernel |
| 1436 | // switches a process while the main thread is in an alternate stack, then the kernel will label |
| 1437 | // the wrong map with [stack]. This test verifies that when the above situation happens, the main |
| 1438 | // thread's stack is found correctly. |
| 1439 | TEST(pthread, pthread_attr_getstack_in_signal_handler) { |
Yabin Cui | 61e4d46 | 2016-03-07 17:44:58 -0800 | [diff] [blame] | 1440 | // This test is only meaningful for the main thread, so make sure we're running on it! |
| 1441 | ASSERT_EQ(getpid(), syscall(__NR_gettid)); |
| 1442 | |
Mor-sarid, Nitzan | 5693332 | 2015-09-11 05:31:36 +0000 | [diff] [blame] | 1443 | const size_t sig_stack_size = 16 * 1024; |
| 1444 | void* sig_stack = mmap(NULL, sig_stack_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, |
| 1445 | -1, 0); |
| 1446 | ASSERT_NE(MAP_FAILED, sig_stack); |
| 1447 | stack_t ss; |
| 1448 | ss.ss_sp = sig_stack; |
| 1449 | ss.ss_size = sig_stack_size; |
| 1450 | ss.ss_flags = 0; |
| 1451 | stack_t oss; |
| 1452 | ASSERT_EQ(0, sigaltstack(&ss, &oss)); |
| 1453 | |
Yabin Cui | 61e4d46 | 2016-03-07 17:44:58 -0800 | [diff] [blame] | 1454 | pthread_attr_t attr; |
| 1455 | ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attr)); |
| 1456 | void* main_stack_base; |
| 1457 | size_t main_stack_size; |
| 1458 | ASSERT_EQ(0, pthread_attr_getstack(&attr, &main_stack_base, &main_stack_size)); |
| 1459 | |
Mor-sarid, Nitzan | 5693332 | 2015-09-11 05:31:36 +0000 | [diff] [blame] | 1460 | ScopedSignalHandler handler(SIGUSR1, getstack_signal_handler, SA_ONSTACK); |
| 1461 | getstack_signal_handler_arg.done = false; |
Chih-Hung Hsieh | 9af13d2 | 2016-06-02 14:40:09 -0700 | [diff] [blame] | 1462 | getstack_signal_handler_arg.signal_stack_base = sig_stack; |
| 1463 | getstack_signal_handler_arg.signal_stack_size = sig_stack_size; |
| 1464 | getstack_signal_handler_arg.main_stack_base = main_stack_base; |
| 1465 | getstack_signal_handler_arg.main_stack_size = main_stack_size; |
Mor-sarid, Nitzan | 5693332 | 2015-09-11 05:31:36 +0000 | [diff] [blame] | 1466 | kill(getpid(), SIGUSR1); |
| 1467 | ASSERT_EQ(true, getstack_signal_handler_arg.done); |
| 1468 | |
Mor-sarid, Nitzan | 5693332 | 2015-09-11 05:31:36 +0000 | [diff] [blame] | 1469 | ASSERT_EQ(0, sigaltstack(&oss, nullptr)); |
| 1470 | ASSERT_EQ(0, munmap(sig_stack, sig_stack_size)); |
| 1471 | } |
| 1472 | |
Yabin Cui | 917d390 | 2015-01-08 12:32:42 -0800 | [diff] [blame] | 1473 | static void pthread_attr_getstack_18908062_helper(void*) { |
| 1474 | char local_variable; |
| 1475 | pthread_attr_t attributes; |
| 1476 | pthread_getattr_np(pthread_self(), &attributes); |
| 1477 | void* stack_base; |
| 1478 | size_t stack_size; |
| 1479 | pthread_attr_getstack(&attributes, &stack_base, &stack_size); |
| 1480 | |
| 1481 | // Test whether &local_variable is in [stack_base, stack_base + stack_size). |
| 1482 | ASSERT_LE(reinterpret_cast<char*>(stack_base), &local_variable); |
| 1483 | ASSERT_LT(&local_variable, reinterpret_cast<char*>(stack_base) + stack_size); |
| 1484 | } |
| 1485 | |
| 1486 | // Check whether something on stack is in the range of |
| 1487 | // [stack_base, stack_base + stack_size). see b/18908062. |
| 1488 | TEST(pthread, pthread_attr_getstack_18908062) { |
| 1489 | pthread_t t; |
| 1490 | ASSERT_EQ(0, pthread_create(&t, NULL, |
| 1491 | reinterpret_cast<void* (*)(void*)>(pthread_attr_getstack_18908062_helper), |
| 1492 | NULL)); |
| 1493 | pthread_join(t, NULL); |
| 1494 | } |
| 1495 | |
Elliott Hughes | 8fb639c | 2014-09-12 14:43:07 -0700 | [diff] [blame] | 1496 | #if defined(__BIONIC__) |
Elliott Hughes | f208361 | 2015-11-11 13:32:28 -0800 | [diff] [blame] | 1497 | static pthread_mutex_t pthread_gettid_np_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 1498 | |
Elliott Hughes | 8fb639c | 2014-09-12 14:43:07 -0700 | [diff] [blame] | 1499 | static void* pthread_gettid_np_helper(void* arg) { |
| 1500 | *reinterpret_cast<pid_t*>(arg) = gettid(); |
Elliott Hughes | f208361 | 2015-11-11 13:32:28 -0800 | [diff] [blame] | 1501 | |
| 1502 | // Wait for our parent to call pthread_gettid_np on us before exiting. |
| 1503 | pthread_mutex_lock(&pthread_gettid_np_mutex); |
| 1504 | pthread_mutex_unlock(&pthread_gettid_np_mutex); |
Elliott Hughes | 8fb639c | 2014-09-12 14:43:07 -0700 | [diff] [blame] | 1505 | return NULL; |
| 1506 | } |
| 1507 | #endif |
| 1508 | |
| 1509 | TEST(pthread, pthread_gettid_np) { |
| 1510 | #if defined(__BIONIC__) |
| 1511 | ASSERT_EQ(gettid(), pthread_gettid_np(pthread_self())); |
| 1512 | |
Elliott Hughes | f208361 | 2015-11-11 13:32:28 -0800 | [diff] [blame] | 1513 | // Ensure the other thread doesn't exit until after we've called |
| 1514 | // pthread_gettid_np on it. |
| 1515 | pthread_mutex_lock(&pthread_gettid_np_mutex); |
| 1516 | |
Elliott Hughes | 8fb639c | 2014-09-12 14:43:07 -0700 | [diff] [blame] | 1517 | pid_t t_gettid_result; |
| 1518 | pthread_t t; |
| 1519 | pthread_create(&t, NULL, pthread_gettid_np_helper, &t_gettid_result); |
| 1520 | |
| 1521 | pid_t t_pthread_gettid_np_result = pthread_gettid_np(t); |
| 1522 | |
Elliott Hughes | f208361 | 2015-11-11 13:32:28 -0800 | [diff] [blame] | 1523 | // Release the other thread and wait for it to exit. |
| 1524 | pthread_mutex_unlock(&pthread_gettid_np_mutex); |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 1525 | pthread_join(t, NULL); |
Elliott Hughes | 8fb639c | 2014-09-12 14:43:07 -0700 | [diff] [blame] | 1526 | |
| 1527 | ASSERT_EQ(t_gettid_result, t_pthread_gettid_np_result); |
| 1528 | #else |
| 1529 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 1530 | #endif |
| 1531 | } |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 1532 | |
| 1533 | static size_t cleanup_counter = 0; |
| 1534 | |
Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 1535 | static void AbortCleanupRoutine(void*) { |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 1536 | abort(); |
| 1537 | } |
| 1538 | |
Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 1539 | static void CountCleanupRoutine(void*) { |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 1540 | ++cleanup_counter; |
| 1541 | } |
| 1542 | |
Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 1543 | static void PthreadCleanupTester() { |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 1544 | pthread_cleanup_push(CountCleanupRoutine, NULL); |
| 1545 | pthread_cleanup_push(CountCleanupRoutine, NULL); |
| 1546 | pthread_cleanup_push(AbortCleanupRoutine, NULL); |
| 1547 | |
| 1548 | pthread_cleanup_pop(0); // Pop the abort without executing it. |
| 1549 | pthread_cleanup_pop(1); // Pop one count while executing it. |
| 1550 | ASSERT_EQ(1U, cleanup_counter); |
| 1551 | // Exit while the other count is still on the cleanup stack. |
| 1552 | pthread_exit(NULL); |
| 1553 | |
| 1554 | // Calls to pthread_cleanup_pop/pthread_cleanup_push must always be balanced. |
| 1555 | pthread_cleanup_pop(0); |
| 1556 | } |
| 1557 | |
Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 1558 | static void* PthreadCleanupStartRoutine(void*) { |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 1559 | PthreadCleanupTester(); |
| 1560 | return NULL; |
| 1561 | } |
| 1562 | |
| 1563 | TEST(pthread, pthread_cleanup_push__pthread_cleanup_pop) { |
| 1564 | pthread_t t; |
| 1565 | ASSERT_EQ(0, pthread_create(&t, NULL, PthreadCleanupStartRoutine, NULL)); |
| 1566 | pthread_join(t, NULL); |
| 1567 | ASSERT_EQ(2U, cleanup_counter); |
| 1568 | } |
Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 1569 | |
| 1570 | TEST(pthread, PTHREAD_MUTEX_DEFAULT_is_PTHREAD_MUTEX_NORMAL) { |
| 1571 | ASSERT_EQ(PTHREAD_MUTEX_NORMAL, PTHREAD_MUTEX_DEFAULT); |
| 1572 | } |
| 1573 | |
| 1574 | TEST(pthread, pthread_mutexattr_gettype) { |
| 1575 | pthread_mutexattr_t attr; |
| 1576 | ASSERT_EQ(0, pthread_mutexattr_init(&attr)); |
| 1577 | |
| 1578 | int attr_type; |
| 1579 | |
| 1580 | ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL)); |
| 1581 | ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type)); |
| 1582 | ASSERT_EQ(PTHREAD_MUTEX_NORMAL, attr_type); |
| 1583 | |
| 1584 | ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK)); |
| 1585 | ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type)); |
| 1586 | ASSERT_EQ(PTHREAD_MUTEX_ERRORCHECK, attr_type); |
| 1587 | |
| 1588 | ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE)); |
| 1589 | ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type)); |
| 1590 | ASSERT_EQ(PTHREAD_MUTEX_RECURSIVE, attr_type); |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 1591 | |
| 1592 | ASSERT_EQ(0, pthread_mutexattr_destroy(&attr)); |
| 1593 | } |
| 1594 | |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 1595 | struct PthreadMutex { |
| 1596 | pthread_mutex_t lock; |
| 1597 | |
Chih-Hung Hsieh | 62e3a07 | 2016-05-03 12:08:05 -0700 | [diff] [blame] | 1598 | explicit PthreadMutex(int mutex_type) { |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 1599 | init(mutex_type); |
| 1600 | } |
| 1601 | |
| 1602 | ~PthreadMutex() { |
| 1603 | destroy(); |
| 1604 | } |
| 1605 | |
| 1606 | private: |
| 1607 | void init(int mutex_type) { |
| 1608 | pthread_mutexattr_t attr; |
| 1609 | ASSERT_EQ(0, pthread_mutexattr_init(&attr)); |
| 1610 | ASSERT_EQ(0, pthread_mutexattr_settype(&attr, mutex_type)); |
| 1611 | ASSERT_EQ(0, pthread_mutex_init(&lock, &attr)); |
| 1612 | ASSERT_EQ(0, pthread_mutexattr_destroy(&attr)); |
| 1613 | } |
| 1614 | |
| 1615 | void destroy() { |
| 1616 | ASSERT_EQ(0, pthread_mutex_destroy(&lock)); |
| 1617 | } |
| 1618 | |
| 1619 | DISALLOW_COPY_AND_ASSIGN(PthreadMutex); |
| 1620 | }; |
Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 1621 | |
| 1622 | TEST(pthread, pthread_mutex_lock_NORMAL) { |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 1623 | PthreadMutex m(PTHREAD_MUTEX_NORMAL); |
Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 1624 | |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 1625 | ASSERT_EQ(0, pthread_mutex_lock(&m.lock)); |
| 1626 | ASSERT_EQ(0, pthread_mutex_unlock(&m.lock)); |
Elliott Hughes | d31d4c1 | 2015-12-14 17:35:10 -0800 | [diff] [blame] | 1627 | ASSERT_EQ(0, pthread_mutex_trylock(&m.lock)); |
| 1628 | ASSERT_EQ(EBUSY, pthread_mutex_trylock(&m.lock)); |
| 1629 | ASSERT_EQ(0, pthread_mutex_unlock(&m.lock)); |
Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 1630 | } |
| 1631 | |
| 1632 | TEST(pthread, pthread_mutex_lock_ERRORCHECK) { |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 1633 | PthreadMutex m(PTHREAD_MUTEX_ERRORCHECK); |
Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 1634 | |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 1635 | ASSERT_EQ(0, pthread_mutex_lock(&m.lock)); |
| 1636 | ASSERT_EQ(EDEADLK, pthread_mutex_lock(&m.lock)); |
| 1637 | ASSERT_EQ(0, pthread_mutex_unlock(&m.lock)); |
| 1638 | ASSERT_EQ(0, pthread_mutex_trylock(&m.lock)); |
| 1639 | ASSERT_EQ(EBUSY, pthread_mutex_trylock(&m.lock)); |
| 1640 | ASSERT_EQ(0, pthread_mutex_unlock(&m.lock)); |
| 1641 | ASSERT_EQ(EPERM, pthread_mutex_unlock(&m.lock)); |
Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 1642 | } |
| 1643 | |
| 1644 | TEST(pthread, pthread_mutex_lock_RECURSIVE) { |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 1645 | PthreadMutex m(PTHREAD_MUTEX_RECURSIVE); |
Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 1646 | |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 1647 | ASSERT_EQ(0, pthread_mutex_lock(&m.lock)); |
| 1648 | ASSERT_EQ(0, pthread_mutex_lock(&m.lock)); |
| 1649 | ASSERT_EQ(0, pthread_mutex_unlock(&m.lock)); |
| 1650 | ASSERT_EQ(0, pthread_mutex_unlock(&m.lock)); |
| 1651 | ASSERT_EQ(0, pthread_mutex_trylock(&m.lock)); |
Elliott Hughes | d31d4c1 | 2015-12-14 17:35:10 -0800 | [diff] [blame] | 1652 | ASSERT_EQ(0, pthread_mutex_trylock(&m.lock)); |
| 1653 | ASSERT_EQ(0, pthread_mutex_unlock(&m.lock)); |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 1654 | ASSERT_EQ(0, pthread_mutex_unlock(&m.lock)); |
| 1655 | ASSERT_EQ(EPERM, pthread_mutex_unlock(&m.lock)); |
| 1656 | } |
| 1657 | |
| 1658 | TEST(pthread, pthread_mutex_init_same_as_static_initializers) { |
| 1659 | pthread_mutex_t lock_normal = PTHREAD_MUTEX_INITIALIZER; |
| 1660 | PthreadMutex m1(PTHREAD_MUTEX_NORMAL); |
| 1661 | ASSERT_EQ(0, memcmp(&lock_normal, &m1.lock, sizeof(pthread_mutex_t))); |
| 1662 | pthread_mutex_destroy(&lock_normal); |
| 1663 | |
| 1664 | pthread_mutex_t lock_errorcheck = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP; |
| 1665 | PthreadMutex m2(PTHREAD_MUTEX_ERRORCHECK); |
| 1666 | ASSERT_EQ(0, memcmp(&lock_errorcheck, &m2.lock, sizeof(pthread_mutex_t))); |
| 1667 | pthread_mutex_destroy(&lock_errorcheck); |
| 1668 | |
| 1669 | pthread_mutex_t lock_recursive = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; |
| 1670 | PthreadMutex m3(PTHREAD_MUTEX_RECURSIVE); |
| 1671 | ASSERT_EQ(0, memcmp(&lock_recursive, &m3.lock, sizeof(pthread_mutex_t))); |
| 1672 | ASSERT_EQ(0, pthread_mutex_destroy(&lock_recursive)); |
Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 1673 | } |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 1674 | class MutexWakeupHelper { |
| 1675 | private: |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 1676 | PthreadMutex m; |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 1677 | enum Progress { |
| 1678 | LOCK_INITIALIZED, |
| 1679 | LOCK_WAITING, |
| 1680 | LOCK_RELEASED, |
| 1681 | LOCK_ACCESSED |
| 1682 | }; |
| 1683 | std::atomic<Progress> progress; |
Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 1684 | std::atomic<pid_t> tid; |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 1685 | |
| 1686 | static void thread_fn(MutexWakeupHelper* helper) { |
Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 1687 | helper->tid = gettid(); |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 1688 | ASSERT_EQ(LOCK_INITIALIZED, helper->progress); |
| 1689 | helper->progress = LOCK_WAITING; |
| 1690 | |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 1691 | ASSERT_EQ(0, pthread_mutex_lock(&helper->m.lock)); |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 1692 | ASSERT_EQ(LOCK_RELEASED, helper->progress); |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 1693 | ASSERT_EQ(0, pthread_mutex_unlock(&helper->m.lock)); |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 1694 | |
| 1695 | helper->progress = LOCK_ACCESSED; |
| 1696 | } |
| 1697 | |
| 1698 | public: |
Chih-Hung Hsieh | 62e3a07 | 2016-05-03 12:08:05 -0700 | [diff] [blame] | 1699 | explicit MutexWakeupHelper(int mutex_type) : m(mutex_type) { |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 1700 | } |
| 1701 | |
| 1702 | void test() { |
| 1703 | ASSERT_EQ(0, pthread_mutex_lock(&m.lock)); |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 1704 | progress = LOCK_INITIALIZED; |
Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 1705 | tid = 0; |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 1706 | |
| 1707 | pthread_t thread; |
| 1708 | ASSERT_EQ(0, pthread_create(&thread, NULL, |
| 1709 | reinterpret_cast<void* (*)(void*)>(MutexWakeupHelper::thread_fn), this)); |
| 1710 | |
Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 1711 | WaitUntilThreadSleep(tid); |
| 1712 | ASSERT_EQ(LOCK_WAITING, progress); |
| 1713 | |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 1714 | progress = LOCK_RELEASED; |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 1715 | ASSERT_EQ(0, pthread_mutex_unlock(&m.lock)); |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 1716 | |
| 1717 | ASSERT_EQ(0, pthread_join(thread, NULL)); |
| 1718 | ASSERT_EQ(LOCK_ACCESSED, progress); |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 1719 | } |
| 1720 | }; |
| 1721 | |
| 1722 | TEST(pthread, pthread_mutex_NORMAL_wakeup) { |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 1723 | MutexWakeupHelper helper(PTHREAD_MUTEX_NORMAL); |
| 1724 | helper.test(); |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 1725 | } |
| 1726 | |
| 1727 | TEST(pthread, pthread_mutex_ERRORCHECK_wakeup) { |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 1728 | MutexWakeupHelper helper(PTHREAD_MUTEX_ERRORCHECK); |
| 1729 | helper.test(); |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 1730 | } |
| 1731 | |
| 1732 | TEST(pthread, pthread_mutex_RECURSIVE_wakeup) { |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 1733 | MutexWakeupHelper helper(PTHREAD_MUTEX_RECURSIVE); |
| 1734 | helper.test(); |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 1735 | } |
| 1736 | |
Yabin Cui | 140f367 | 2015-02-03 10:32:00 -0800 | [diff] [blame] | 1737 | TEST(pthread, pthread_mutex_owner_tid_limit) { |
Yabin Cui | e69c245 | 2015-02-13 16:21:25 -0800 | [diff] [blame] | 1738 | #if defined(__BIONIC__) && !defined(__LP64__) |
Yabin Cui | 140f367 | 2015-02-03 10:32:00 -0800 | [diff] [blame] | 1739 | FILE* fp = fopen("/proc/sys/kernel/pid_max", "r"); |
| 1740 | ASSERT_TRUE(fp != NULL); |
| 1741 | long pid_max; |
| 1742 | ASSERT_EQ(1, fscanf(fp, "%ld", &pid_max)); |
| 1743 | fclose(fp); |
Yabin Cui | e69c245 | 2015-02-13 16:21:25 -0800 | [diff] [blame] | 1744 | // Bionic's pthread_mutex implementation on 32-bit devices uses 16 bits to represent owner tid. |
Yabin Cui | 140f367 | 2015-02-03 10:32:00 -0800 | [diff] [blame] | 1745 | ASSERT_LE(pid_max, 65536); |
Yabin Cui | e69c245 | 2015-02-13 16:21:25 -0800 | [diff] [blame] | 1746 | #else |
| 1747 | GTEST_LOG_(INFO) << "This test does nothing as 32-bit tid is supported by pthread_mutex.\n"; |
| 1748 | #endif |
Yabin Cui | 140f367 | 2015-02-03 10:32:00 -0800 | [diff] [blame] | 1749 | } |
Yabin Cui | b584572 | 2015-03-16 22:46:42 -0700 | [diff] [blame] | 1750 | |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1751 | TEST(pthread, pthread_mutex_timedlock) { |
| 1752 | pthread_mutex_t m; |
| 1753 | ASSERT_EQ(0, pthread_mutex_init(&m, nullptr)); |
| 1754 | |
| 1755 | // If the mutex is already locked, pthread_mutex_timedlock should time out. |
| 1756 | ASSERT_EQ(0, pthread_mutex_lock(&m)); |
| 1757 | |
| 1758 | timespec ts; |
| 1759 | ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts)); |
| 1760 | ASSERT_EQ(ETIMEDOUT, pthread_mutex_timedlock(&m, &ts)); |
| 1761 | ts.tv_nsec = -1; |
| 1762 | ASSERT_EQ(EINVAL, pthread_mutex_timedlock(&m, &ts)); |
| 1763 | ts.tv_nsec = NS_PER_S; |
| 1764 | ASSERT_EQ(EINVAL, pthread_mutex_timedlock(&m, &ts)); |
| 1765 | ts.tv_nsec = NS_PER_S - 1; |
| 1766 | ts.tv_sec = -1; |
| 1767 | ASSERT_EQ(ETIMEDOUT, pthread_mutex_timedlock(&m, &ts)); |
| 1768 | |
| 1769 | // If the mutex is unlocked, pthread_mutex_timedlock should succeed. |
| 1770 | ASSERT_EQ(0, pthread_mutex_unlock(&m)); |
| 1771 | |
| 1772 | ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts)); |
| 1773 | ts.tv_sec += 1; |
| 1774 | ASSERT_EQ(0, pthread_mutex_timedlock(&m, &ts)); |
| 1775 | |
| 1776 | ASSERT_EQ(0, pthread_mutex_unlock(&m)); |
| 1777 | ASSERT_EQ(0, pthread_mutex_destroy(&m)); |
| 1778 | } |
| 1779 | |
Yabin Cui | b584572 | 2015-03-16 22:46:42 -0700 | [diff] [blame] | 1780 | class StrictAlignmentAllocator { |
| 1781 | public: |
| 1782 | void* allocate(size_t size, size_t alignment) { |
| 1783 | char* p = new char[size + alignment * 2]; |
| 1784 | allocated_array.push_back(p); |
| 1785 | while (!is_strict_aligned(p, alignment)) { |
| 1786 | ++p; |
| 1787 | } |
| 1788 | return p; |
| 1789 | } |
| 1790 | |
| 1791 | ~StrictAlignmentAllocator() { |
Elliott Hughes | 0b2acdf | 2015-10-02 18:25:19 -0700 | [diff] [blame] | 1792 | for (const auto& p : allocated_array) { |
| 1793 | delete[] p; |
Yabin Cui | b584572 | 2015-03-16 22:46:42 -0700 | [diff] [blame] | 1794 | } |
| 1795 | } |
| 1796 | |
| 1797 | private: |
| 1798 | bool is_strict_aligned(char* p, size_t alignment) { |
| 1799 | return (reinterpret_cast<uintptr_t>(p) % (alignment * 2)) == alignment; |
| 1800 | } |
| 1801 | |
| 1802 | std::vector<char*> allocated_array; |
| 1803 | }; |
| 1804 | |
| 1805 | TEST(pthread, pthread_types_allow_four_bytes_alignment) { |
| 1806 | #if defined(__BIONIC__) |
| 1807 | // For binary compatibility with old version, we need to allow 4-byte aligned data for pthread types. |
| 1808 | StrictAlignmentAllocator allocator; |
| 1809 | pthread_mutex_t* mutex = reinterpret_cast<pthread_mutex_t*>( |
| 1810 | allocator.allocate(sizeof(pthread_mutex_t), 4)); |
| 1811 | ASSERT_EQ(0, pthread_mutex_init(mutex, NULL)); |
| 1812 | ASSERT_EQ(0, pthread_mutex_lock(mutex)); |
| 1813 | ASSERT_EQ(0, pthread_mutex_unlock(mutex)); |
| 1814 | ASSERT_EQ(0, pthread_mutex_destroy(mutex)); |
| 1815 | |
| 1816 | pthread_cond_t* cond = reinterpret_cast<pthread_cond_t*>( |
| 1817 | allocator.allocate(sizeof(pthread_cond_t), 4)); |
| 1818 | ASSERT_EQ(0, pthread_cond_init(cond, NULL)); |
| 1819 | ASSERT_EQ(0, pthread_cond_signal(cond)); |
| 1820 | ASSERT_EQ(0, pthread_cond_broadcast(cond)); |
| 1821 | ASSERT_EQ(0, pthread_cond_destroy(cond)); |
| 1822 | |
| 1823 | pthread_rwlock_t* rwlock = reinterpret_cast<pthread_rwlock_t*>( |
| 1824 | allocator.allocate(sizeof(pthread_rwlock_t), 4)); |
| 1825 | ASSERT_EQ(0, pthread_rwlock_init(rwlock, NULL)); |
| 1826 | ASSERT_EQ(0, pthread_rwlock_rdlock(rwlock)); |
| 1827 | ASSERT_EQ(0, pthread_rwlock_unlock(rwlock)); |
| 1828 | ASSERT_EQ(0, pthread_rwlock_wrlock(rwlock)); |
| 1829 | ASSERT_EQ(0, pthread_rwlock_unlock(rwlock)); |
| 1830 | ASSERT_EQ(0, pthread_rwlock_destroy(rwlock)); |
| 1831 | |
| 1832 | #else |
| 1833 | GTEST_LOG_(INFO) << "This test tests bionic implementation details."; |
| 1834 | #endif |
| 1835 | } |
Christopher Ferris | 60907c7 | 2015-06-09 18:46:15 -0700 | [diff] [blame] | 1836 | |
| 1837 | TEST(pthread, pthread_mutex_lock_null_32) { |
| 1838 | #if defined(__BIONIC__) && !defined(__LP64__) |
Dan Albert | baa2a97 | 2015-08-13 16:58:50 -0700 | [diff] [blame] | 1839 | // For LP32, the pthread lock/unlock functions allow a NULL mutex and return |
| 1840 | // EINVAL in that case: http://b/19995172. |
| 1841 | // |
| 1842 | // We decorate the public defintion with _Nonnull so that people recompiling |
| 1843 | // their code with get a warning and might fix their bug, but need to pass |
| 1844 | // NULL here to test that we remain compatible. |
| 1845 | pthread_mutex_t* null_value = nullptr; |
| 1846 | ASSERT_EQ(EINVAL, pthread_mutex_lock(null_value)); |
Christopher Ferris | 60907c7 | 2015-06-09 18:46:15 -0700 | [diff] [blame] | 1847 | #else |
| 1848 | GTEST_LOG_(INFO) << "This test tests bionic implementation details on 32 bit devices."; |
| 1849 | #endif |
| 1850 | } |
| 1851 | |
| 1852 | TEST(pthread, pthread_mutex_unlock_null_32) { |
| 1853 | #if defined(__BIONIC__) && !defined(__LP64__) |
Dan Albert | baa2a97 | 2015-08-13 16:58:50 -0700 | [diff] [blame] | 1854 | // For LP32, the pthread lock/unlock functions allow a NULL mutex and return |
| 1855 | // EINVAL in that case: http://b/19995172. |
| 1856 | // |
| 1857 | // We decorate the public defintion with _Nonnull so that people recompiling |
| 1858 | // their code with get a warning and might fix their bug, but need to pass |
| 1859 | // NULL here to test that we remain compatible. |
| 1860 | pthread_mutex_t* null_value = nullptr; |
| 1861 | ASSERT_EQ(EINVAL, pthread_mutex_unlock(null_value)); |
Christopher Ferris | 60907c7 | 2015-06-09 18:46:15 -0700 | [diff] [blame] | 1862 | #else |
| 1863 | GTEST_LOG_(INFO) << "This test tests bionic implementation details on 32 bit devices."; |
| 1864 | #endif |
| 1865 | } |
| 1866 | |
| 1867 | TEST_F(pthread_DeathTest, pthread_mutex_lock_null_64) { |
| 1868 | #if defined(__BIONIC__) && defined(__LP64__) |
| 1869 | pthread_mutex_t* null_value = nullptr; |
| 1870 | ASSERT_EXIT(pthread_mutex_lock(null_value), testing::KilledBySignal(SIGSEGV), ""); |
| 1871 | #else |
| 1872 | GTEST_LOG_(INFO) << "This test tests bionic implementation details on 64 bit devices."; |
| 1873 | #endif |
| 1874 | } |
| 1875 | |
| 1876 | TEST_F(pthread_DeathTest, pthread_mutex_unlock_null_64) { |
| 1877 | #if defined(__BIONIC__) && defined(__LP64__) |
| 1878 | pthread_mutex_t* null_value = nullptr; |
| 1879 | ASSERT_EXIT(pthread_mutex_unlock(null_value), testing::KilledBySignal(SIGSEGV), ""); |
| 1880 | #else |
| 1881 | GTEST_LOG_(INFO) << "This test tests bionic implementation details on 64 bit devices."; |
| 1882 | #endif |
| 1883 | } |
Yabin Cui | 33ac04a | 2015-09-22 11:16:15 -0700 | [diff] [blame] | 1884 | |
| 1885 | extern _Unwind_Reason_Code FrameCounter(_Unwind_Context* ctx, void* arg); |
| 1886 | |
| 1887 | static volatile bool signal_handler_on_altstack_done; |
| 1888 | |
| 1889 | static void SignalHandlerOnAltStack(int signo, siginfo_t*, void*) { |
| 1890 | ASSERT_EQ(SIGUSR1, signo); |
| 1891 | // Check if we have enough stack space for unwinding. |
| 1892 | int count = 0; |
| 1893 | _Unwind_Backtrace(FrameCounter, &count); |
| 1894 | ASSERT_GT(count, 0); |
| 1895 | // Check if we have enough stack space for logging. |
| 1896 | std::string s(2048, '*'); |
| 1897 | GTEST_LOG_(INFO) << s; |
| 1898 | signal_handler_on_altstack_done = true; |
| 1899 | } |
| 1900 | |
| 1901 | TEST(pthread, big_enough_signal_stack_for_64bit_arch) { |
| 1902 | signal_handler_on_altstack_done = false; |
| 1903 | ScopedSignalHandler handler(SIGUSR1, SignalHandlerOnAltStack, SA_SIGINFO | SA_ONSTACK); |
| 1904 | kill(getpid(), SIGUSR1); |
| 1905 | ASSERT_TRUE(signal_handler_on_altstack_done); |
| 1906 | } |
Yabin Cui | e7c2fff | 2015-11-05 22:06:09 -0800 | [diff] [blame] | 1907 | |
| 1908 | TEST(pthread, pthread_barrierattr_smoke) { |
| 1909 | pthread_barrierattr_t attr; |
| 1910 | ASSERT_EQ(0, pthread_barrierattr_init(&attr)); |
| 1911 | int pshared; |
| 1912 | ASSERT_EQ(0, pthread_barrierattr_getpshared(&attr, &pshared)); |
| 1913 | ASSERT_EQ(PTHREAD_PROCESS_PRIVATE, pshared); |
| 1914 | ASSERT_EQ(0, pthread_barrierattr_setpshared(&attr, PTHREAD_PROCESS_SHARED)); |
| 1915 | ASSERT_EQ(0, pthread_barrierattr_getpshared(&attr, &pshared)); |
| 1916 | ASSERT_EQ(PTHREAD_PROCESS_SHARED, pshared); |
| 1917 | ASSERT_EQ(0, pthread_barrierattr_destroy(&attr)); |
| 1918 | } |
| 1919 | |
Yabin Cui | 81d2797 | 2016-03-22 13:45:55 -0700 | [diff] [blame] | 1920 | struct BarrierTestHelperData { |
| 1921 | size_t thread_count; |
| 1922 | pthread_barrier_t barrier; |
| 1923 | std::atomic<int> finished_mask; |
| 1924 | std::atomic<int> serial_thread_count; |
Yabin Cui | e7c2fff | 2015-11-05 22:06:09 -0800 | [diff] [blame] | 1925 | size_t iteration_count; |
Yabin Cui | 81d2797 | 2016-03-22 13:45:55 -0700 | [diff] [blame] | 1926 | std::atomic<size_t> finished_iteration_count; |
| 1927 | |
| 1928 | BarrierTestHelperData(size_t thread_count, size_t iteration_count) |
| 1929 | : thread_count(thread_count), finished_mask(0), serial_thread_count(0), |
| 1930 | iteration_count(iteration_count), finished_iteration_count(0) { |
| 1931 | } |
| 1932 | }; |
| 1933 | |
| 1934 | struct BarrierTestHelperArg { |
| 1935 | int id; |
| 1936 | BarrierTestHelperData* data; |
Yabin Cui | e7c2fff | 2015-11-05 22:06:09 -0800 | [diff] [blame] | 1937 | }; |
| 1938 | |
| 1939 | static void BarrierTestHelper(BarrierTestHelperArg* arg) { |
Yabin Cui | 81d2797 | 2016-03-22 13:45:55 -0700 | [diff] [blame] | 1940 | for (size_t i = 0; i < arg->data->iteration_count; ++i) { |
| 1941 | int result = pthread_barrier_wait(&arg->data->barrier); |
| 1942 | if (result == PTHREAD_BARRIER_SERIAL_THREAD) { |
| 1943 | arg->data->serial_thread_count++; |
| 1944 | } else { |
| 1945 | ASSERT_EQ(0, result); |
| 1946 | } |
| 1947 | arg->data->finished_mask |= (1 << arg->id); |
| 1948 | if (arg->data->finished_mask == ((1 << arg->data->thread_count) - 1)) { |
| 1949 | ASSERT_EQ(1, arg->data->serial_thread_count); |
| 1950 | arg->data->finished_iteration_count++; |
| 1951 | arg->data->finished_mask = 0; |
| 1952 | arg->data->serial_thread_count = 0; |
| 1953 | } |
Yabin Cui | e7c2fff | 2015-11-05 22:06:09 -0800 | [diff] [blame] | 1954 | } |
| 1955 | } |
| 1956 | |
| 1957 | TEST(pthread, pthread_barrier_smoke) { |
| 1958 | const size_t BARRIER_ITERATION_COUNT = 10; |
| 1959 | const size_t BARRIER_THREAD_COUNT = 10; |
Yabin Cui | 81d2797 | 2016-03-22 13:45:55 -0700 | [diff] [blame] | 1960 | BarrierTestHelperData data(BARRIER_THREAD_COUNT, BARRIER_ITERATION_COUNT); |
| 1961 | ASSERT_EQ(0, pthread_barrier_init(&data.barrier, nullptr, data.thread_count)); |
| 1962 | std::vector<pthread_t> threads(data.thread_count); |
Yabin Cui | e7c2fff | 2015-11-05 22:06:09 -0800 | [diff] [blame] | 1963 | std::vector<BarrierTestHelperArg> args(threads.size()); |
| 1964 | for (size_t i = 0; i < threads.size(); ++i) { |
Yabin Cui | 81d2797 | 2016-03-22 13:45:55 -0700 | [diff] [blame] | 1965 | args[i].id = i; |
| 1966 | args[i].data = &data; |
Yabin Cui | e7c2fff | 2015-11-05 22:06:09 -0800 | [diff] [blame] | 1967 | ASSERT_EQ(0, pthread_create(&threads[i], nullptr, |
| 1968 | reinterpret_cast<void* (*)(void*)>(BarrierTestHelper), &args[i])); |
| 1969 | } |
Yabin Cui | e7c2fff | 2015-11-05 22:06:09 -0800 | [diff] [blame] | 1970 | for (size_t i = 0; i < threads.size(); ++i) { |
| 1971 | ASSERT_EQ(0, pthread_join(threads[i], nullptr)); |
| 1972 | } |
Yabin Cui | 81d2797 | 2016-03-22 13:45:55 -0700 | [diff] [blame] | 1973 | ASSERT_EQ(data.iteration_count, data.finished_iteration_count); |
| 1974 | ASSERT_EQ(0, pthread_barrier_destroy(&data.barrier)); |
| 1975 | } |
| 1976 | |
| 1977 | struct BarrierDestroyTestArg { |
| 1978 | std::atomic<int> tid; |
| 1979 | pthread_barrier_t* barrier; |
| 1980 | }; |
| 1981 | |
| 1982 | static void BarrierDestroyTestHelper(BarrierDestroyTestArg* arg) { |
| 1983 | arg->tid = gettid(); |
| 1984 | ASSERT_EQ(0, pthread_barrier_wait(arg->barrier)); |
Yabin Cui | e7c2fff | 2015-11-05 22:06:09 -0800 | [diff] [blame] | 1985 | } |
| 1986 | |
| 1987 | TEST(pthread, pthread_barrier_destroy) { |
| 1988 | pthread_barrier_t barrier; |
| 1989 | ASSERT_EQ(0, pthread_barrier_init(&barrier, nullptr, 2)); |
| 1990 | pthread_t thread; |
Yabin Cui | 81d2797 | 2016-03-22 13:45:55 -0700 | [diff] [blame] | 1991 | BarrierDestroyTestArg arg; |
Yabin Cui | e7c2fff | 2015-11-05 22:06:09 -0800 | [diff] [blame] | 1992 | arg.tid = 0; |
| 1993 | arg.barrier = &barrier; |
Yabin Cui | e7c2fff | 2015-11-05 22:06:09 -0800 | [diff] [blame] | 1994 | ASSERT_EQ(0, pthread_create(&thread, nullptr, |
Yabin Cui | 81d2797 | 2016-03-22 13:45:55 -0700 | [diff] [blame] | 1995 | reinterpret_cast<void* (*)(void*)>(BarrierDestroyTestHelper), &arg)); |
Yabin Cui | e7c2fff | 2015-11-05 22:06:09 -0800 | [diff] [blame] | 1996 | WaitUntilThreadSleep(arg.tid); |
| 1997 | ASSERT_EQ(EBUSY, pthread_barrier_destroy(&barrier)); |
| 1998 | ASSERT_EQ(PTHREAD_BARRIER_SERIAL_THREAD, pthread_barrier_wait(&barrier)); |
| 1999 | // Verify if the barrier can be destroyed directly after pthread_barrier_wait(). |
| 2000 | ASSERT_EQ(0, pthread_barrier_destroy(&barrier)); |
| 2001 | ASSERT_EQ(0, pthread_join(thread, nullptr)); |
| 2002 | #if defined(__BIONIC__) |
| 2003 | ASSERT_EQ(EINVAL, pthread_barrier_destroy(&barrier)); |
| 2004 | #endif |
| 2005 | } |
| 2006 | |
| 2007 | struct BarrierOrderingTestHelperArg { |
| 2008 | pthread_barrier_t* barrier; |
| 2009 | size_t* array; |
| 2010 | size_t array_length; |
| 2011 | size_t id; |
| 2012 | }; |
| 2013 | |
| 2014 | void BarrierOrderingTestHelper(BarrierOrderingTestHelperArg* arg) { |
| 2015 | const size_t ITERATION_COUNT = 10000; |
| 2016 | for (size_t i = 1; i <= ITERATION_COUNT; ++i) { |
| 2017 | arg->array[arg->id] = i; |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 2018 | int result = pthread_barrier_wait(arg->barrier); |
| 2019 | ASSERT_TRUE(result == 0 || result == PTHREAD_BARRIER_SERIAL_THREAD); |
Yabin Cui | e7c2fff | 2015-11-05 22:06:09 -0800 | [diff] [blame] | 2020 | for (size_t j = 0; j < arg->array_length; ++j) { |
| 2021 | ASSERT_EQ(i, arg->array[j]); |
| 2022 | } |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 2023 | result = pthread_barrier_wait(arg->barrier); |
| 2024 | ASSERT_TRUE(result == 0 || result == PTHREAD_BARRIER_SERIAL_THREAD); |
Yabin Cui | e7c2fff | 2015-11-05 22:06:09 -0800 | [diff] [blame] | 2025 | } |
| 2026 | } |
| 2027 | |
| 2028 | TEST(pthread, pthread_barrier_check_ordering) { |
| 2029 | const size_t THREAD_COUNT = 4; |
| 2030 | pthread_barrier_t barrier; |
| 2031 | ASSERT_EQ(0, pthread_barrier_init(&barrier, nullptr, THREAD_COUNT)); |
| 2032 | size_t array[THREAD_COUNT]; |
| 2033 | std::vector<pthread_t> threads(THREAD_COUNT); |
| 2034 | std::vector<BarrierOrderingTestHelperArg> args(THREAD_COUNT); |
| 2035 | for (size_t i = 0; i < THREAD_COUNT; ++i) { |
| 2036 | args[i].barrier = &barrier; |
| 2037 | args[i].array = array; |
| 2038 | args[i].array_length = THREAD_COUNT; |
| 2039 | args[i].id = i; |
| 2040 | ASSERT_EQ(0, pthread_create(&threads[i], nullptr, |
| 2041 | reinterpret_cast<void* (*)(void*)>(BarrierOrderingTestHelper), |
| 2042 | &args[i])); |
| 2043 | } |
| 2044 | for (size_t i = 0; i < THREAD_COUNT; ++i) { |
| 2045 | ASSERT_EQ(0, pthread_join(threads[i], nullptr)); |
| 2046 | } |
| 2047 | } |
Yabin Cui | fe3a83a | 2015-11-17 16:03:18 -0800 | [diff] [blame] | 2048 | |
| 2049 | TEST(pthread, pthread_spinlock_smoke) { |
| 2050 | pthread_spinlock_t lock; |
| 2051 | ASSERT_EQ(0, pthread_spin_init(&lock, 0)); |
| 2052 | ASSERT_EQ(0, pthread_spin_trylock(&lock)); |
| 2053 | ASSERT_EQ(0, pthread_spin_unlock(&lock)); |
| 2054 | ASSERT_EQ(0, pthread_spin_lock(&lock)); |
| 2055 | ASSERT_EQ(EBUSY, pthread_spin_trylock(&lock)); |
| 2056 | ASSERT_EQ(0, pthread_spin_unlock(&lock)); |
| 2057 | ASSERT_EQ(0, pthread_spin_destroy(&lock)); |
| 2058 | } |