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> |
Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame] | 26 | #include <sys/cdefs.h> |
Elliott Hughes | 70b24b1 | 2013-11-15 11:51:07 -0800 | [diff] [blame] | 27 | #include <sys/mman.h> |
Juan Yescas | 65af9a8 | 2023-12-07 11:35:21 -0800 | [diff] [blame] | 28 | #include <sys/param.h> |
Elliott Hughes | 4d098ca | 2016-04-11 12:43:05 -0700 | [diff] [blame] | 29 | #include <sys/prctl.h> |
George Burgess IV | 08fd072 | 2019-01-15 19:00:11 -0800 | [diff] [blame] | 30 | #include <sys/resource.h> |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 31 | #include <sys/syscall.h> |
Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 32 | #include <time.h> |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 33 | #include <unistd.h> |
Yabin Cui | 33ac04a | 2015-09-22 11:16:15 -0700 | [diff] [blame] | 34 | #include <unwind.h> |
Elliott Hughes | bfeab1b | 2012-09-05 17:47:37 -0700 | [diff] [blame] | 35 | |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 36 | #include <atomic> |
Josh Gao | ddf757e | 2018-10-17 15:23:03 -0700 | [diff] [blame] | 37 | #include <future> |
Yabin Cui | b584572 | 2015-03-16 22:46:42 -0700 | [diff] [blame] | 38 | #include <vector> |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 39 | |
Elliott Hughes | 5e62b34 | 2018-10-25 11:00:00 -0700 | [diff] [blame] | 40 | #include <android-base/macros.h> |
Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 41 | #include <android-base/parseint.h> |
Tom Cherry | b8ab618 | 2017-04-05 16:20:29 -0700 | [diff] [blame] | 42 | #include <android-base/scopeguard.h> |
Elliott Hughes | 141b917 | 2021-04-09 17:13:09 -0700 | [diff] [blame] | 43 | #include <android-base/silent_death_test.h> |
Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 44 | #include <android-base/strings.h> |
Florian Mayer | f966620 | 2023-02-28 14:26:06 -0800 | [diff] [blame] | 45 | #include <android-base/test_utils.h> |
Tom Cherry | b8ab618 | 2017-04-05 16:20:29 -0700 | [diff] [blame] | 46 | |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 47 | #include "private/bionic_constants.h" |
Elliott Hughes | eabc47b | 2024-11-08 22:05:00 +0000 | [diff] [blame] | 48 | #include "private/bionic_time_conversions.h" |
Elliott Hughes | 71ba589 | 2018-02-07 12:44:45 -0800 | [diff] [blame] | 49 | #include "SignalUtils.h" |
Elliott Hughes | 15dfd63 | 2015-09-22 16:40:14 -0700 | [diff] [blame] | 50 | #include "utils.h" |
| 51 | |
Elliott Hughes | 141b917 | 2021-04-09 17:13:09 -0700 | [diff] [blame] | 52 | using pthread_DeathTest = SilentDeathTest; |
Elliott Hughes | e657eb4 | 2021-02-18 17:11:56 -0800 | [diff] [blame] | 53 | |
Elliott Hughes | bfeab1b | 2012-09-05 17:47:37 -0700 | [diff] [blame] | 54 | TEST(pthread, pthread_key_create) { |
| 55 | pthread_key_t key; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 56 | ASSERT_EQ(0, pthread_key_create(&key, nullptr)); |
Elliott Hughes | bfeab1b | 2012-09-05 17:47:37 -0700 | [diff] [blame] | 57 | ASSERT_EQ(0, pthread_key_delete(key)); |
| 58 | // Can't delete a key that's already been deleted. |
| 59 | ASSERT_EQ(EINVAL, pthread_key_delete(key)); |
| 60 | } |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 61 | |
Elliott Hughes | 1960f1c | 2025-02-26 14:46:52 -0800 | [diff] [blame] | 62 | static std::vector<void*> example_key_destructor_data; |
| 63 | static pthread_key_t example_key; |
| 64 | static void example_key_destructor(void *data) { |
| 65 | // By the time the destructor function is running, |
| 66 | // this thread's value for the key should have been zeroed. |
| 67 | ASSERT_EQ(NULL, pthread_getspecific(example_key)); |
| 68 | |
| 69 | // Store the value so we can check we got the expected result. |
| 70 | example_key_destructor_data.push_back(data); |
| 71 | } |
| 72 | |
| 73 | TEST(pthread, pthread_key_destructors) { |
| 74 | ASSERT_EQ(0, pthread_key_create(&example_key, example_key_destructor)); |
| 75 | |
| 76 | // Check that the destructor isn't called for a default null value. |
| 77 | std::thread([]() {}).join(); |
| 78 | ASSERT_TRUE(example_key_destructor_data.empty()); |
| 79 | |
| 80 | // Check that the destructor isn't called for an explicit null value. |
| 81 | std::thread([]() { |
| 82 | ASSERT_EQ(0, pthread_setspecific(example_key, (void*) 1234)); |
| 83 | ASSERT_EQ(0, pthread_setspecific(example_key, nullptr)); |
| 84 | }).join(); |
| 85 | ASSERT_TRUE(example_key_destructor_data.empty()); |
| 86 | |
| 87 | // Check that the destructor is called for a non-null value. |
| 88 | std::thread([]() { ASSERT_EQ(0, pthread_setspecific(example_key, (void*) 1234)); }).join(); |
| 89 | ASSERT_EQ(1u, example_key_destructor_data.size()); |
| 90 | ASSERT_EQ((void*) 1234, example_key_destructor_data[0]); |
| 91 | |
| 92 | ASSERT_EQ(0, pthread_key_delete(example_key)); |
| 93 | } |
| 94 | |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 95 | TEST(pthread, pthread_keys_max) { |
Yabin Cui | 6c238f2 | 2014-12-11 20:50:41 -0800 | [diff] [blame] | 96 | // POSIX says PTHREAD_KEYS_MAX should be at least _POSIX_THREAD_KEYS_MAX. |
| 97 | ASSERT_GE(PTHREAD_KEYS_MAX, _POSIX_THREAD_KEYS_MAX); |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 98 | } |
Elliott Hughes | 718a5b5 | 2014-01-28 17:02:03 -0800 | [diff] [blame] | 99 | |
Yabin Cui | 6c238f2 | 2014-12-11 20:50:41 -0800 | [diff] [blame] | 100 | TEST(pthread, sysconf_SC_THREAD_KEYS_MAX_eq_PTHREAD_KEYS_MAX) { |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 101 | int sysconf_max = sysconf(_SC_THREAD_KEYS_MAX); |
Yabin Cui | 6c238f2 | 2014-12-11 20:50:41 -0800 | [diff] [blame] | 102 | ASSERT_EQ(sysconf_max, PTHREAD_KEYS_MAX); |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | TEST(pthread, pthread_key_many_distinct) { |
Yabin Cui | 6c238f2 | 2014-12-11 20:50:41 -0800 | [diff] [blame] | 106 | // As gtest uses pthread keys, we can't allocate exactly PTHREAD_KEYS_MAX |
| 107 | // pthread keys, but We should be able to allocate at least this many keys. |
| 108 | int nkeys = PTHREAD_KEYS_MAX / 2; |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 109 | std::vector<pthread_key_t> keys; |
| 110 | |
Tom Cherry | b8ab618 | 2017-04-05 16:20:29 -0700 | [diff] [blame] | 111 | auto scope_guard = android::base::make_scope_guard([&keys] { |
Elliott Hughes | 0b2acdf | 2015-10-02 18:25:19 -0700 | [diff] [blame] | 112 | for (const auto& key : keys) { |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 113 | EXPECT_EQ(0, pthread_key_delete(key)); |
| 114 | } |
| 115 | }); |
| 116 | |
| 117 | for (int i = 0; i < nkeys; ++i) { |
| 118 | pthread_key_t key; |
Elliott Hughes | 6170693 | 2015-03-31 10:56:58 -0700 | [diff] [blame] | 119 | // If this fails, it's likely that LIBC_PTHREAD_KEY_RESERVED_COUNT is wrong. |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 120 | ASSERT_EQ(0, pthread_key_create(&key, nullptr)) << i << " of " << nkeys; |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 121 | keys.push_back(key); |
| 122 | ASSERT_EQ(0, pthread_setspecific(key, reinterpret_cast<void*>(i))); |
| 123 | } |
| 124 | |
| 125 | for (int i = keys.size() - 1; i >= 0; --i) { |
| 126 | ASSERT_EQ(reinterpret_cast<void*>(i), pthread_getspecific(keys.back())); |
| 127 | pthread_key_t key = keys.back(); |
| 128 | keys.pop_back(); |
| 129 | ASSERT_EQ(0, pthread_key_delete(key)); |
| 130 | } |
| 131 | } |
| 132 | |
Yabin Cui | 6c238f2 | 2014-12-11 20:50:41 -0800 | [diff] [blame] | 133 | TEST(pthread, pthread_key_not_exceed_PTHREAD_KEYS_MAX) { |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 134 | std::vector<pthread_key_t> keys; |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 135 | int rv = 0; |
Yabin Cui | 6c238f2 | 2014-12-11 20:50:41 -0800 | [diff] [blame] | 136 | |
| 137 | // Pthread keys are used by gtest, so PTHREAD_KEYS_MAX should |
| 138 | // be more than we are allowed to allocate now. |
| 139 | for (int i = 0; i < PTHREAD_KEYS_MAX; i++) { |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 140 | pthread_key_t key; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 141 | rv = pthread_key_create(&key, nullptr); |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 142 | if (rv == EAGAIN) { |
| 143 | break; |
| 144 | } |
| 145 | EXPECT_EQ(0, rv); |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 146 | keys.push_back(key); |
| 147 | } |
| 148 | |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 149 | // Don't leak keys. |
Elliott Hughes | 0b2acdf | 2015-10-02 18:25:19 -0700 | [diff] [blame] | 150 | for (const auto& key : keys) { |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 151 | EXPECT_EQ(0, pthread_key_delete(key)); |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 152 | } |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 153 | keys.clear(); |
| 154 | |
| 155 | // We should have eventually reached the maximum number of keys and received |
| 156 | // EAGAIN. |
| 157 | ASSERT_EQ(EAGAIN, rv); |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Elliott Hughes | ebb770f | 2014-06-25 13:46:46 -0700 | [diff] [blame] | 160 | TEST(pthread, pthread_key_delete) { |
| 161 | void* expected = reinterpret_cast<void*>(1234); |
| 162 | pthread_key_t key; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 163 | ASSERT_EQ(0, pthread_key_create(&key, nullptr)); |
Elliott Hughes | ebb770f | 2014-06-25 13:46:46 -0700 | [diff] [blame] | 164 | ASSERT_EQ(0, pthread_setspecific(key, expected)); |
| 165 | ASSERT_EQ(expected, pthread_getspecific(key)); |
| 166 | ASSERT_EQ(0, pthread_key_delete(key)); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 167 | // After deletion, pthread_getspecific returns nullptr. |
| 168 | ASSERT_EQ(nullptr, pthread_getspecific(key)); |
Elliott Hughes | ebb770f | 2014-06-25 13:46:46 -0700 | [diff] [blame] | 169 | // And you can't use pthread_setspecific with the deleted key. |
| 170 | ASSERT_EQ(EINVAL, pthread_setspecific(key, expected)); |
| 171 | } |
| 172 | |
Elliott Hughes | 40a5217 | 2014-07-30 14:48:10 -0700 | [diff] [blame] | 173 | TEST(pthread, pthread_key_fork) { |
| 174 | void* expected = reinterpret_cast<void*>(1234); |
| 175 | pthread_key_t key; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 176 | ASSERT_EQ(0, pthread_key_create(&key, nullptr)); |
Elliott Hughes | 40a5217 | 2014-07-30 14:48:10 -0700 | [diff] [blame] | 177 | ASSERT_EQ(0, pthread_setspecific(key, expected)); |
| 178 | ASSERT_EQ(expected, pthread_getspecific(key)); |
| 179 | |
| 180 | pid_t pid = fork(); |
| 181 | ASSERT_NE(-1, pid) << strerror(errno); |
| 182 | |
| 183 | if (pid == 0) { |
| 184 | // The surviving thread inherits all the forking thread's TLS values... |
| 185 | ASSERT_EQ(expected, pthread_getspecific(key)); |
| 186 | _exit(99); |
| 187 | } |
| 188 | |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 189 | AssertChildExited(pid, 99); |
Elliott Hughes | 40a5217 | 2014-07-30 14:48:10 -0700 | [diff] [blame] | 190 | |
| 191 | ASSERT_EQ(expected, pthread_getspecific(key)); |
Dan Albert | 1d53ae2 | 2014-09-02 15:24:26 -0700 | [diff] [blame] | 192 | ASSERT_EQ(0, pthread_key_delete(key)); |
Elliott Hughes | 40a5217 | 2014-07-30 14:48:10 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | static void* DirtyKeyFn(void* key) { |
| 196 | return pthread_getspecific(*reinterpret_cast<pthread_key_t*>(key)); |
| 197 | } |
| 198 | |
| 199 | TEST(pthread, pthread_key_dirty) { |
| 200 | pthread_key_t key; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 201 | ASSERT_EQ(0, pthread_key_create(&key, nullptr)); |
Elliott Hughes | 40a5217 | 2014-07-30 14:48:10 -0700 | [diff] [blame] | 202 | |
Yabin Cui | a36158a | 2015-11-16 21:06:16 -0800 | [diff] [blame] | 203 | size_t stack_size = 640 * 1024; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 204 | void* stack = mmap(nullptr, stack_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); |
Elliott Hughes | 40a5217 | 2014-07-30 14:48:10 -0700 | [diff] [blame] | 205 | ASSERT_NE(MAP_FAILED, stack); |
| 206 | memset(stack, 0xff, stack_size); |
| 207 | |
| 208 | pthread_attr_t attr; |
| 209 | ASSERT_EQ(0, pthread_attr_init(&attr)); |
| 210 | ASSERT_EQ(0, pthread_attr_setstack(&attr, stack, stack_size)); |
| 211 | |
| 212 | pthread_t t; |
| 213 | ASSERT_EQ(0, pthread_create(&t, &attr, DirtyKeyFn, &key)); |
| 214 | |
| 215 | void* result; |
| 216 | ASSERT_EQ(0, pthread_join(t, &result)); |
| 217 | ASSERT_EQ(nullptr, result); // Not ~0! |
| 218 | |
| 219 | ASSERT_EQ(0, munmap(stack, stack_size)); |
Dan Albert | 1d53ae2 | 2014-09-02 15:24:26 -0700 | [diff] [blame] | 220 | ASSERT_EQ(0, pthread_key_delete(key)); |
Elliott Hughes | 40a5217 | 2014-07-30 14:48:10 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Florian Mayer | f966620 | 2023-02-28 14:26:06 -0800 | [diff] [blame] | 223 | static void* FnWithStackFrame(void*) { |
| 224 | int x; |
| 225 | *const_cast<volatile int*>(&x) = 1; |
| 226 | return nullptr; |
| 227 | } |
| 228 | |
| 229 | TEST(pthread, pthread_heap_allocated_stack) { |
| 230 | SKIP_WITH_HWASAN; // TODO(b/148982147): Re-enable when fixed. |
| 231 | |
| 232 | size_t stack_size = 640 * 1024; |
Elliott Hughes | 18e335b | 2023-04-21 11:18:40 -0700 | [diff] [blame] | 233 | std::unique_ptr<char[]> stack(new (std::align_val_t(getpagesize())) char[stack_size]); |
| 234 | memset(stack.get(), '\xff', stack_size); |
Florian Mayer | f966620 | 2023-02-28 14:26:06 -0800 | [diff] [blame] | 235 | |
| 236 | pthread_attr_t attr; |
| 237 | ASSERT_EQ(0, pthread_attr_init(&attr)); |
Elliott Hughes | 18e335b | 2023-04-21 11:18:40 -0700 | [diff] [blame] | 238 | ASSERT_EQ(0, pthread_attr_setstack(&attr, stack.get(), stack_size)); |
Florian Mayer | f966620 | 2023-02-28 14:26:06 -0800 | [diff] [blame] | 239 | |
| 240 | pthread_t t; |
| 241 | ASSERT_EQ(0, pthread_create(&t, &attr, FnWithStackFrame, nullptr)); |
| 242 | |
| 243 | void* result; |
| 244 | ASSERT_EQ(0, pthread_join(t, &result)); |
| 245 | } |
| 246 | |
Yabin Cui | 5ddbb3f | 2015-03-05 20:35:32 -0800 | [diff] [blame] | 247 | TEST(pthread, static_pthread_key_used_before_creation) { |
| 248 | #if defined(__BIONIC__) |
| 249 | // See http://b/19625804. The bug is about a static/global pthread key being used before creation. |
| 250 | // So here tests if the static/global default value 0 can be detected as invalid key. |
| 251 | static pthread_key_t key; |
| 252 | ASSERT_EQ(nullptr, pthread_getspecific(key)); |
| 253 | ASSERT_EQ(EINVAL, pthread_setspecific(key, nullptr)); |
| 254 | ASSERT_EQ(EINVAL, pthread_key_delete(key)); |
| 255 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 256 | GTEST_SKIP() << "bionic-only test"; |
Yabin Cui | 5ddbb3f | 2015-03-05 20:35:32 -0800 | [diff] [blame] | 257 | #endif |
| 258 | } |
| 259 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 260 | static void* IdFn(void* arg) { |
| 261 | return arg; |
| 262 | } |
| 263 | |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 264 | class SpinFunctionHelper { |
| 265 | public: |
| 266 | SpinFunctionHelper() { |
| 267 | SpinFunctionHelper::spin_flag_ = true; |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 268 | } |
Elliott Hughes | 0bd9d13 | 2017-11-02 13:11:13 -0700 | [diff] [blame] | 269 | |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 270 | ~SpinFunctionHelper() { |
| 271 | UnSpin(); |
| 272 | } |
Elliott Hughes | 0bd9d13 | 2017-11-02 13:11:13 -0700 | [diff] [blame] | 273 | |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 274 | auto GetFunction() -> void* (*)(void*) { |
| 275 | return SpinFunctionHelper::SpinFn; |
| 276 | } |
| 277 | |
| 278 | void UnSpin() { |
| 279 | SpinFunctionHelper::spin_flag_ = false; |
| 280 | } |
| 281 | |
| 282 | private: |
| 283 | static void* SpinFn(void*) { |
| 284 | while (spin_flag_) {} |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 285 | return nullptr; |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 286 | } |
Yabin Cui | a36158a | 2015-11-16 21:06:16 -0800 | [diff] [blame] | 287 | static std::atomic<bool> spin_flag_; |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 288 | }; |
| 289 | |
| 290 | // It doesn't matter if spin_flag_ is used in several tests, |
| 291 | // because it is always set to false after each test. Each thread |
| 292 | // loops on spin_flag_ can find it becomes false at some time. |
Yabin Cui | a36158a | 2015-11-16 21:06:16 -0800 | [diff] [blame] | 293 | std::atomic<bool> SpinFunctionHelper::spin_flag_; |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 294 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 295 | static void* JoinFn(void* arg) { |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 296 | return reinterpret_cast<void*>(pthread_join(reinterpret_cast<pthread_t>(arg), nullptr)); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 299 | static void AssertDetached(pthread_t t, bool is_detached) { |
| 300 | pthread_attr_t attr; |
| 301 | ASSERT_EQ(0, pthread_getattr_np(t, &attr)); |
| 302 | int detach_state; |
| 303 | ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &detach_state)); |
| 304 | pthread_attr_destroy(&attr); |
| 305 | ASSERT_EQ(is_detached, (detach_state == PTHREAD_CREATE_DETACHED)); |
| 306 | } |
| 307 | |
Elliott Hughes | 7484c21 | 2017-02-02 02:41:38 +0000 | [diff] [blame] | 308 | static void MakeDeadThread(pthread_t& t) { |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 309 | ASSERT_EQ(0, pthread_create(&t, nullptr, IdFn, nullptr)); |
| 310 | ASSERT_EQ(0, pthread_join(t, nullptr)); |
Elliott Hughes | 7484c21 | 2017-02-02 02:41:38 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 313 | TEST(pthread, pthread_create) { |
| 314 | void* expected_result = reinterpret_cast<void*>(123); |
| 315 | // Can we create a thread? |
| 316 | pthread_t t; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 317 | ASSERT_EQ(0, pthread_create(&t, nullptr, IdFn, expected_result)); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 318 | // If we join, do we get the expected value back? |
| 319 | void* result; |
| 320 | ASSERT_EQ(0, pthread_join(t, &result)); |
| 321 | ASSERT_EQ(expected_result, result); |
| 322 | } |
| 323 | |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 324 | TEST(pthread, pthread_create_EAGAIN) { |
| 325 | pthread_attr_t attributes; |
| 326 | ASSERT_EQ(0, pthread_attr_init(&attributes)); |
| 327 | ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, static_cast<size_t>(-1) & ~(getpagesize() - 1))); |
| 328 | |
| 329 | pthread_t t; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 330 | ASSERT_EQ(EAGAIN, pthread_create(&t, &attributes, IdFn, nullptr)); |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 333 | TEST(pthread, pthread_no_join_after_detach) { |
Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 334 | SpinFunctionHelper spin_helper; |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 335 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 336 | pthread_t t1; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 337 | ASSERT_EQ(0, pthread_create(&t1, nullptr, spin_helper.GetFunction(), nullptr)); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 338 | |
| 339 | // After a pthread_detach... |
| 340 | ASSERT_EQ(0, pthread_detach(t1)); |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 341 | AssertDetached(t1, true); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 342 | |
| 343 | // ...pthread_join should fail. |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 344 | ASSERT_EQ(EINVAL, pthread_join(t1, nullptr)); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | TEST(pthread, pthread_no_op_detach_after_join) { |
Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 348 | SpinFunctionHelper spin_helper; |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 349 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 350 | pthread_t t1; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 351 | ASSERT_EQ(0, pthread_create(&t1, nullptr, spin_helper.GetFunction(), nullptr)); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 352 | |
| 353 | // If thread 2 is already waiting to join thread 1... |
| 354 | pthread_t t2; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 355 | ASSERT_EQ(0, pthread_create(&t2, nullptr, JoinFn, reinterpret_cast<void*>(t1))); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 356 | |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 357 | sleep(1); // (Give t2 a chance to call pthread_join.) |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 358 | |
Yabin Cui | bbb0432 | 2015-03-19 15:19:25 -0700 | [diff] [blame] | 359 | #if defined(__BIONIC__) |
| 360 | ASSERT_EQ(EINVAL, pthread_detach(t1)); |
| 361 | #else |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 362 | ASSERT_EQ(0, pthread_detach(t1)); |
Yabin Cui | bbb0432 | 2015-03-19 15:19:25 -0700 | [diff] [blame] | 363 | #endif |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 364 | AssertDetached(t1, false); |
| 365 | |
Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 366 | spin_helper.UnSpin(); |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 367 | |
| 368 | // ...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] | 369 | void* join_result; |
| 370 | ASSERT_EQ(0, pthread_join(t2, &join_result)); |
Elliott Hughes | 5b9310e | 2013-10-02 16:59:05 -0700 | [diff] [blame] | 371 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result)); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 372 | } |
Elliott Hughes | 14f1959 | 2012-10-29 10:19:44 -0700 | [diff] [blame] | 373 | |
| 374 | TEST(pthread, pthread_join_self) { |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 375 | ASSERT_EQ(EDEADLK, pthread_join(pthread_self(), nullptr)); |
Elliott Hughes | 14f1959 | 2012-10-29 10:19:44 -0700 | [diff] [blame] | 376 | } |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 377 | |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 378 | struct TestBug37410 { |
| 379 | pthread_t main_thread; |
| 380 | pthread_mutex_t mutex; |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 381 | |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 382 | static void main() { |
| 383 | TestBug37410 data; |
| 384 | data.main_thread = pthread_self(); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 385 | ASSERT_EQ(0, pthread_mutex_init(&data.mutex, nullptr)); |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 386 | ASSERT_EQ(0, pthread_mutex_lock(&data.mutex)); |
| 387 | |
| 388 | pthread_t t; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 389 | ASSERT_EQ(0, pthread_create(&t, nullptr, TestBug37410::thread_fn, reinterpret_cast<void*>(&data))); |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 390 | |
| 391 | // Wait for the thread to be running... |
| 392 | ASSERT_EQ(0, pthread_mutex_lock(&data.mutex)); |
| 393 | ASSERT_EQ(0, pthread_mutex_unlock(&data.mutex)); |
| 394 | |
| 395 | // ...and exit. |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 396 | pthread_exit(nullptr); |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | private: |
| 400 | static void* thread_fn(void* arg) { |
| 401 | TestBug37410* data = reinterpret_cast<TestBug37410*>(arg); |
| 402 | |
Evgenii Stepanov | 352853a | 2019-02-05 17:37:37 -0800 | [diff] [blame] | 403 | // Unlocking data->mutex will cause the main thread to exit, invalidating *data. Save the handle. |
| 404 | pthread_t main_thread = data->main_thread; |
| 405 | |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 406 | // Let the main thread know we're running. |
| 407 | pthread_mutex_unlock(&data->mutex); |
| 408 | |
| 409 | // And wait for the main thread to exit. |
Evgenii Stepanov | 352853a | 2019-02-05 17:37:37 -0800 | [diff] [blame] | 410 | pthread_join(main_thread, nullptr); |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 411 | |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 412 | return nullptr; |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 413 | } |
| 414 | }; |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 415 | |
Elliott Hughes | 7fd803c | 2013-02-14 16:33:52 -0800 | [diff] [blame] | 416 | // Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to |
| 417 | // run this test (which exits normally) in its own process. |
Yabin Cui | 9df7040 | 2014-11-05 18:01:01 -0800 | [diff] [blame] | 418 | TEST_F(pthread_DeathTest, pthread_bug_37410) { |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 419 | // http://code.google.com/p/android/issues/detail?id=37410 |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 420 | ASSERT_EXIT(TestBug37410::main(), ::testing::ExitedWithCode(0), ""); |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 421 | } |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 422 | |
| 423 | static void* SignalHandlerFn(void* arg) { |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 424 | sigset64_t wait_set; |
| 425 | sigfillset64(&wait_set); |
| 426 | return reinterpret_cast<void*>(sigwait64(&wait_set, reinterpret_cast<int*>(arg))); |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | TEST(pthread, pthread_sigmask) { |
Elliott Hughes | 19e6232 | 2013-10-15 11:23:57 -0700 | [diff] [blame] | 430 | // Check that SIGUSR1 isn't blocked. |
| 431 | sigset_t original_set; |
| 432 | sigemptyset(&original_set); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 433 | ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, nullptr, &original_set)); |
Elliott Hughes | 19e6232 | 2013-10-15 11:23:57 -0700 | [diff] [blame] | 434 | ASSERT_FALSE(sigismember(&original_set, SIGUSR1)); |
| 435 | |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 436 | // Block SIGUSR1. |
| 437 | sigset_t set; |
| 438 | sigemptyset(&set); |
| 439 | sigaddset(&set, SIGUSR1); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 440 | ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &set, nullptr)); |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 441 | |
Elliott Hughes | 19e6232 | 2013-10-15 11:23:57 -0700 | [diff] [blame] | 442 | // Check that SIGUSR1 is blocked. |
| 443 | sigset_t final_set; |
| 444 | sigemptyset(&final_set); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 445 | ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, nullptr, &final_set)); |
Elliott Hughes | 19e6232 | 2013-10-15 11:23:57 -0700 | [diff] [blame] | 446 | ASSERT_TRUE(sigismember(&final_set, SIGUSR1)); |
| 447 | // ...and that sigprocmask agrees with pthread_sigmask. |
| 448 | sigemptyset(&final_set); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 449 | ASSERT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &final_set)); |
Elliott Hughes | 19e6232 | 2013-10-15 11:23:57 -0700 | [diff] [blame] | 450 | ASSERT_TRUE(sigismember(&final_set, SIGUSR1)); |
| 451 | |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 452 | // Spawn a thread that calls sigwait and tells us what it received. |
| 453 | pthread_t signal_thread; |
| 454 | int received_signal = -1; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 455 | ASSERT_EQ(0, pthread_create(&signal_thread, nullptr, SignalHandlerFn, &received_signal)); |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 456 | |
| 457 | // Send that thread SIGUSR1. |
| 458 | pthread_kill(signal_thread, SIGUSR1); |
| 459 | |
| 460 | // See what it got. |
| 461 | void* join_result; |
| 462 | ASSERT_EQ(0, pthread_join(signal_thread, &join_result)); |
| 463 | ASSERT_EQ(SIGUSR1, received_signal); |
Elliott Hughes | 5b9310e | 2013-10-02 16:59:05 -0700 | [diff] [blame] | 464 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result)); |
Elliott Hughes | 19e6232 | 2013-10-15 11:23:57 -0700 | [diff] [blame] | 465 | |
| 466 | // Restore the original signal mask. |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 467 | ASSERT_EQ(0, pthread_sigmask(SIG_SETMASK, &original_set, nullptr)); |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 468 | } |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 469 | |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 470 | TEST(pthread, pthread_sigmask64_SIGTRMIN) { |
| 471 | // Check that SIGRTMIN isn't blocked. |
| 472 | sigset64_t original_set; |
| 473 | sigemptyset64(&original_set); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 474 | ASSERT_EQ(0, pthread_sigmask64(SIG_BLOCK, nullptr, &original_set)); |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 475 | ASSERT_FALSE(sigismember64(&original_set, SIGRTMIN)); |
| 476 | |
| 477 | // Block SIGRTMIN. |
| 478 | sigset64_t set; |
| 479 | sigemptyset64(&set); |
| 480 | sigaddset64(&set, SIGRTMIN); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 481 | ASSERT_EQ(0, pthread_sigmask64(SIG_BLOCK, &set, nullptr)); |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 482 | |
| 483 | // Check that SIGRTMIN is blocked. |
| 484 | sigset64_t final_set; |
| 485 | sigemptyset64(&final_set); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 486 | ASSERT_EQ(0, pthread_sigmask64(SIG_BLOCK, nullptr, &final_set)); |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 487 | ASSERT_TRUE(sigismember64(&final_set, SIGRTMIN)); |
| 488 | // ...and that sigprocmask64 agrees with pthread_sigmask64. |
| 489 | sigemptyset64(&final_set); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 490 | ASSERT_EQ(0, sigprocmask64(SIG_BLOCK, nullptr, &final_set)); |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 491 | ASSERT_TRUE(sigismember64(&final_set, SIGRTMIN)); |
| 492 | |
| 493 | // Spawn a thread that calls sigwait64 and tells us what it received. |
| 494 | pthread_t signal_thread; |
| 495 | int received_signal = -1; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 496 | ASSERT_EQ(0, pthread_create(&signal_thread, nullptr, SignalHandlerFn, &received_signal)); |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 497 | |
| 498 | // Send that thread SIGRTMIN. |
| 499 | pthread_kill(signal_thread, SIGRTMIN); |
| 500 | |
| 501 | // See what it got. |
| 502 | void* join_result; |
| 503 | ASSERT_EQ(0, pthread_join(signal_thread, &join_result)); |
| 504 | ASSERT_EQ(SIGRTMIN, received_signal); |
| 505 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result)); |
| 506 | |
| 507 | // Restore the original signal mask. |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 508 | ASSERT_EQ(0, pthread_sigmask64(SIG_SETMASK, &original_set, nullptr)); |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 509 | } |
| 510 | |
Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 511 | static void test_pthread_setname_np__pthread_getname_np(pthread_t t) { |
| 512 | ASSERT_EQ(0, pthread_setname_np(t, "short")); |
| 513 | char name[32]; |
| 514 | ASSERT_EQ(0, pthread_getname_np(t, name, sizeof(name))); |
| 515 | ASSERT_STREQ("short", name); |
| 516 | |
Elliott Hughes | d1aea30 | 2015-04-25 10:05:24 -0700 | [diff] [blame] | 517 | // 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] | 518 | ASSERT_EQ(0, pthread_setname_np(t, "123456789012345")); |
| 519 | ASSERT_EQ(0, pthread_getname_np(t, name, sizeof(name))); |
| 520 | ASSERT_STREQ("123456789012345", name); |
| 521 | |
| 522 | ASSERT_EQ(ERANGE, pthread_setname_np(t, "1234567890123456")); |
| 523 | |
| 524 | // The passed-in buffer should be at least 16 bytes. |
| 525 | ASSERT_EQ(0, pthread_getname_np(t, name, 16)); |
| 526 | ASSERT_EQ(ERANGE, pthread_getname_np(t, name, 15)); |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 529 | TEST(pthread, pthread_setname_np__pthread_getname_np__self) { |
| 530 | test_pthread_setname_np__pthread_getname_np(pthread_self()); |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 531 | } |
| 532 | |
Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 533 | TEST(pthread, pthread_setname_np__pthread_getname_np__other) { |
| 534 | SpinFunctionHelper spin_helper; |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 535 | |
Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 536 | pthread_t t; |
Elliott Hughes | 4d098ca | 2016-04-11 12:43:05 -0700 | [diff] [blame] | 537 | ASSERT_EQ(0, pthread_create(&t, nullptr, spin_helper.GetFunction(), nullptr)); |
| 538 | test_pthread_setname_np__pthread_getname_np(t); |
| 539 | spin_helper.UnSpin(); |
| 540 | ASSERT_EQ(0, pthread_join(t, nullptr)); |
| 541 | } |
| 542 | |
| 543 | // http://b/28051133: a kernel misfeature means that you can't change the |
| 544 | // name of another thread if you've set PR_SET_DUMPABLE to 0. |
| 545 | TEST(pthread, pthread_setname_np__pthread_getname_np__other_PR_SET_DUMPABLE) { |
| 546 | ASSERT_EQ(0, prctl(PR_SET_DUMPABLE, 0)) << strerror(errno); |
| 547 | |
| 548 | SpinFunctionHelper spin_helper; |
| 549 | |
| 550 | pthread_t t; |
| 551 | ASSERT_EQ(0, pthread_create(&t, nullptr, spin_helper.GetFunction(), nullptr)); |
Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 552 | test_pthread_setname_np__pthread_getname_np(t); |
| 553 | spin_helper.UnSpin(); |
| 554 | ASSERT_EQ(0, pthread_join(t, nullptr)); |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Elliott Hughes | 11859d4 | 2017-02-13 17:59:29 -0800 | [diff] [blame] | 557 | TEST_F(pthread_DeathTest, pthread_setname_np__no_such_thread) { |
Elliott Hughes | bcb1529 | 2017-02-07 21:05:30 +0000 | [diff] [blame] | 558 | pthread_t dead_thread; |
| 559 | MakeDeadThread(dead_thread); |
| 560 | |
Elliott Hughes | 5bb113c | 2019-02-01 16:31:10 -0800 | [diff] [blame] | 561 | EXPECT_DEATH(pthread_setname_np(dead_thread, "short 3"), |
| 562 | "invalid pthread_t (.*) passed to pthread_setname_np"); |
Elliott Hughes | 6ce686c | 2017-02-21 13:15:20 -0800 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | TEST_F(pthread_DeathTest, pthread_setname_np__null_thread) { |
| 566 | pthread_t null_thread = 0; |
| 567 | EXPECT_EQ(ENOENT, pthread_setname_np(null_thread, "short 3")); |
Elliott Hughes | 11859d4 | 2017-02-13 17:59:29 -0800 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | TEST_F(pthread_DeathTest, pthread_getname_np__no_such_thread) { |
| 571 | pthread_t dead_thread; |
| 572 | MakeDeadThread(dead_thread); |
| 573 | |
Elliott Hughes | bcb1529 | 2017-02-07 21:05:30 +0000 | [diff] [blame] | 574 | char name[64]; |
Elliott Hughes | 5bb113c | 2019-02-01 16:31:10 -0800 | [diff] [blame] | 575 | EXPECT_DEATH(pthread_getname_np(dead_thread, name, sizeof(name)), |
| 576 | "invalid pthread_t (.*) passed to pthread_getname_np"); |
Elliott Hughes | 6ce686c | 2017-02-21 13:15:20 -0800 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | TEST_F(pthread_DeathTest, pthread_getname_np__null_thread) { |
| 580 | pthread_t null_thread = 0; |
| 581 | |
| 582 | char name[64]; |
| 583 | EXPECT_EQ(ENOENT, pthread_getname_np(null_thread, name, sizeof(name))); |
Elliott Hughes | bcb1529 | 2017-02-07 21:05:30 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 586 | TEST(pthread, pthread_kill__0) { |
| 587 | // Signal 0 just tests that the thread exists, so it's safe to call on ourselves. |
| 588 | ASSERT_EQ(0, pthread_kill(pthread_self(), 0)); |
| 589 | } |
| 590 | |
| 591 | TEST(pthread, pthread_kill__invalid_signal) { |
| 592 | ASSERT_EQ(EINVAL, pthread_kill(pthread_self(), -1)); |
| 593 | } |
| 594 | |
Elliott Hughes | fae89fc | 2013-02-21 11:22:23 -0800 | [diff] [blame] | 595 | static void pthread_kill__in_signal_handler_helper(int signal_number) { |
| 596 | static int count = 0; |
| 597 | ASSERT_EQ(SIGALRM, signal_number); |
| 598 | if (++count == 1) { |
| 599 | // Can we call pthread_kill from a signal handler? |
| 600 | ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM)); |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | TEST(pthread, pthread_kill__in_signal_handler) { |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 605 | ScopedSignalHandler ssh(SIGALRM, pthread_kill__in_signal_handler_helper); |
Elliott Hughes | fae89fc | 2013-02-21 11:22:23 -0800 | [diff] [blame] | 606 | ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM)); |
| 607 | } |
| 608 | |
Josh Gao | ddf757e | 2018-10-17 15:23:03 -0700 | [diff] [blame] | 609 | TEST(pthread, pthread_kill__exited_thread) { |
| 610 | static std::promise<pid_t> tid_promise; |
| 611 | pthread_t thread; |
| 612 | ASSERT_EQ(0, pthread_create(&thread, nullptr, |
| 613 | [](void*) -> void* { |
| 614 | tid_promise.set_value(gettid()); |
| 615 | return nullptr; |
| 616 | }, |
| 617 | nullptr)); |
| 618 | |
| 619 | pid_t tid = tid_promise.get_future().get(); |
| 620 | while (TEMP_FAILURE_RETRY(syscall(__NR_tgkill, getpid(), tid, 0)) != -1) { |
| 621 | continue; |
| 622 | } |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 623 | ASSERT_ERRNO(ESRCH); |
Josh Gao | ddf757e | 2018-10-17 15:23:03 -0700 | [diff] [blame] | 624 | |
| 625 | ASSERT_EQ(ESRCH, pthread_kill(thread, 0)); |
| 626 | } |
| 627 | |
Elliott Hughes | 11859d4 | 2017-02-13 17:59:29 -0800 | [diff] [blame] | 628 | TEST_F(pthread_DeathTest, pthread_detach__no_such_thread) { |
Elliott Hughes | 7484c21 | 2017-02-02 02:41:38 +0000 | [diff] [blame] | 629 | pthread_t dead_thread; |
| 630 | MakeDeadThread(dead_thread); |
| 631 | |
Elliott Hughes | 5bb113c | 2019-02-01 16:31:10 -0800 | [diff] [blame] | 632 | EXPECT_DEATH(pthread_detach(dead_thread), |
| 633 | "invalid pthread_t (.*) passed to pthread_detach"); |
Elliott Hughes | 6ce686c | 2017-02-21 13:15:20 -0800 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | TEST_F(pthread_DeathTest, pthread_detach__null_thread) { |
| 637 | pthread_t null_thread = 0; |
| 638 | EXPECT_EQ(ESRCH, pthread_detach(null_thread)); |
Elliott Hughes | 7484c21 | 2017-02-02 02:41:38 +0000 | [diff] [blame] | 639 | } |
| 640 | |
Jeff Hao | 9b06cc3 | 2013-08-15 14:51:16 -0700 | [diff] [blame] | 641 | TEST(pthread, pthread_getcpuclockid__clock_gettime) { |
Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 642 | SpinFunctionHelper spin_helper; |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 643 | |
Jeff Hao | 9b06cc3 | 2013-08-15 14:51:16 -0700 | [diff] [blame] | 644 | pthread_t t; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 645 | ASSERT_EQ(0, pthread_create(&t, nullptr, spin_helper.GetFunction(), nullptr)); |
Jeff Hao | 9b06cc3 | 2013-08-15 14:51:16 -0700 | [diff] [blame] | 646 | |
| 647 | clockid_t c; |
| 648 | ASSERT_EQ(0, pthread_getcpuclockid(t, &c)); |
| 649 | timespec ts; |
| 650 | ASSERT_EQ(0, clock_gettime(c, &ts)); |
Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 651 | spin_helper.UnSpin(); |
Yabin Cui | a36158a | 2015-11-16 21:06:16 -0800 | [diff] [blame] | 652 | ASSERT_EQ(0, pthread_join(t, nullptr)); |
Jeff Hao | 9b06cc3 | 2013-08-15 14:51:16 -0700 | [diff] [blame] | 653 | } |
| 654 | |
Elliott Hughes | 11859d4 | 2017-02-13 17:59:29 -0800 | [diff] [blame] | 655 | TEST_F(pthread_DeathTest, pthread_getcpuclockid__no_such_thread) { |
Elliott Hughes | bcb1529 | 2017-02-07 21:05:30 +0000 | [diff] [blame] | 656 | pthread_t dead_thread; |
| 657 | MakeDeadThread(dead_thread); |
| 658 | |
| 659 | clockid_t c; |
Elliott Hughes | 5bb113c | 2019-02-01 16:31:10 -0800 | [diff] [blame] | 660 | EXPECT_DEATH(pthread_getcpuclockid(dead_thread, &c), |
| 661 | "invalid pthread_t (.*) passed to pthread_getcpuclockid"); |
Elliott Hughes | 6ce686c | 2017-02-21 13:15:20 -0800 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | TEST_F(pthread_DeathTest, pthread_getcpuclockid__null_thread) { |
| 665 | pthread_t null_thread = 0; |
| 666 | clockid_t c; |
| 667 | EXPECT_EQ(ESRCH, pthread_getcpuclockid(null_thread, &c)); |
Elliott Hughes | bcb1529 | 2017-02-07 21:05:30 +0000 | [diff] [blame] | 668 | } |
| 669 | |
Elliott Hughes | 11859d4 | 2017-02-13 17:59:29 -0800 | [diff] [blame] | 670 | TEST_F(pthread_DeathTest, pthread_getschedparam__no_such_thread) { |
Elliott Hughes | bcb1529 | 2017-02-07 21:05:30 +0000 | [diff] [blame] | 671 | pthread_t dead_thread; |
| 672 | MakeDeadThread(dead_thread); |
| 673 | |
| 674 | int policy; |
| 675 | sched_param param; |
Elliott Hughes | 5bb113c | 2019-02-01 16:31:10 -0800 | [diff] [blame] | 676 | EXPECT_DEATH(pthread_getschedparam(dead_thread, &policy, ¶m), |
| 677 | "invalid pthread_t (.*) passed to pthread_getschedparam"); |
Elliott Hughes | 6ce686c | 2017-02-21 13:15:20 -0800 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | TEST_F(pthread_DeathTest, pthread_getschedparam__null_thread) { |
| 681 | pthread_t null_thread = 0; |
| 682 | int policy; |
| 683 | sched_param param; |
| 684 | EXPECT_EQ(ESRCH, pthread_getschedparam(null_thread, &policy, ¶m)); |
Elliott Hughes | bcb1529 | 2017-02-07 21:05:30 +0000 | [diff] [blame] | 685 | } |
| 686 | |
Elliott Hughes | 11859d4 | 2017-02-13 17:59:29 -0800 | [diff] [blame] | 687 | TEST_F(pthread_DeathTest, pthread_setschedparam__no_such_thread) { |
Elliott Hughes | bcb1529 | 2017-02-07 21:05:30 +0000 | [diff] [blame] | 688 | pthread_t dead_thread; |
| 689 | MakeDeadThread(dead_thread); |
| 690 | |
| 691 | int policy = 0; |
| 692 | sched_param param; |
Elliott Hughes | 5bb113c | 2019-02-01 16:31:10 -0800 | [diff] [blame] | 693 | EXPECT_DEATH(pthread_setschedparam(dead_thread, policy, ¶m), |
| 694 | "invalid pthread_t (.*) passed to pthread_setschedparam"); |
Elliott Hughes | 6ce686c | 2017-02-21 13:15:20 -0800 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | TEST_F(pthread_DeathTest, pthread_setschedparam__null_thread) { |
| 698 | pthread_t null_thread = 0; |
| 699 | int policy = 0; |
| 700 | sched_param param; |
| 701 | EXPECT_EQ(ESRCH, pthread_setschedparam(null_thread, policy, ¶m)); |
Elliott Hughes | bcb1529 | 2017-02-07 21:05:30 +0000 | [diff] [blame] | 702 | } |
| 703 | |
Elliott Hughes | dff08ce | 2017-10-16 09:58:45 -0700 | [diff] [blame] | 704 | TEST_F(pthread_DeathTest, pthread_setschedprio__no_such_thread) { |
| 705 | pthread_t dead_thread; |
| 706 | MakeDeadThread(dead_thread); |
| 707 | |
Elliott Hughes | 5bb113c | 2019-02-01 16:31:10 -0800 | [diff] [blame] | 708 | EXPECT_DEATH(pthread_setschedprio(dead_thread, 123), |
| 709 | "invalid pthread_t (.*) passed to pthread_setschedprio"); |
Elliott Hughes | dff08ce | 2017-10-16 09:58:45 -0700 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | TEST_F(pthread_DeathTest, pthread_setschedprio__null_thread) { |
| 713 | pthread_t null_thread = 0; |
| 714 | EXPECT_EQ(ESRCH, pthread_setschedprio(null_thread, 123)); |
| 715 | } |
| 716 | |
Elliott Hughes | 11859d4 | 2017-02-13 17:59:29 -0800 | [diff] [blame] | 717 | TEST_F(pthread_DeathTest, pthread_join__no_such_thread) { |
Elliott Hughes | 7484c21 | 2017-02-02 02:41:38 +0000 | [diff] [blame] | 718 | pthread_t dead_thread; |
| 719 | MakeDeadThread(dead_thread); |
| 720 | |
Elliott Hughes | 5bb113c | 2019-02-01 16:31:10 -0800 | [diff] [blame] | 721 | EXPECT_DEATH(pthread_join(dead_thread, nullptr), |
| 722 | "invalid pthread_t (.*) passed to pthread_join"); |
Elliott Hughes | 6ce686c | 2017-02-21 13:15:20 -0800 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | TEST_F(pthread_DeathTest, pthread_join__null_thread) { |
| 726 | pthread_t null_thread = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 727 | EXPECT_EQ(ESRCH, pthread_join(null_thread, nullptr)); |
Elliott Hughes | 7484c21 | 2017-02-02 02:41:38 +0000 | [diff] [blame] | 728 | } |
| 729 | |
Elliott Hughes | 11859d4 | 2017-02-13 17:59:29 -0800 | [diff] [blame] | 730 | TEST_F(pthread_DeathTest, pthread_kill__no_such_thread) { |
Elliott Hughes | 7484c21 | 2017-02-02 02:41:38 +0000 | [diff] [blame] | 731 | pthread_t dead_thread; |
| 732 | MakeDeadThread(dead_thread); |
| 733 | |
Elliott Hughes | 5bb113c | 2019-02-01 16:31:10 -0800 | [diff] [blame] | 734 | EXPECT_DEATH(pthread_kill(dead_thread, 0), |
| 735 | "invalid pthread_t (.*) passed to pthread_kill"); |
Elliott Hughes | 6ce686c | 2017-02-21 13:15:20 -0800 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | TEST_F(pthread_DeathTest, pthread_kill__null_thread) { |
| 739 | pthread_t null_thread = 0; |
| 740 | EXPECT_EQ(ESRCH, pthread_kill(null_thread, 0)); |
Elliott Hughes | 7484c21 | 2017-02-02 02:41:38 +0000 | [diff] [blame] | 741 | } |
| 742 | |
msg555 | 0f020d1 | 2013-06-06 14:59:28 -0400 | [diff] [blame] | 743 | TEST(pthread, pthread_join__multijoin) { |
Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 744 | SpinFunctionHelper spin_helper; |
msg555 | 0f020d1 | 2013-06-06 14:59:28 -0400 | [diff] [blame] | 745 | |
| 746 | pthread_t t1; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 747 | ASSERT_EQ(0, pthread_create(&t1, nullptr, spin_helper.GetFunction(), nullptr)); |
msg555 | 0f020d1 | 2013-06-06 14:59:28 -0400 | [diff] [blame] | 748 | |
| 749 | pthread_t t2; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 750 | ASSERT_EQ(0, pthread_create(&t2, nullptr, JoinFn, reinterpret_cast<void*>(t1))); |
msg555 | 0f020d1 | 2013-06-06 14:59:28 -0400 | [diff] [blame] | 751 | |
| 752 | sleep(1); // (Give t2 a chance to call pthread_join.) |
| 753 | |
| 754 | // Multiple joins to the same thread should fail. |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 755 | ASSERT_EQ(EINVAL, pthread_join(t1, nullptr)); |
msg555 | 0f020d1 | 2013-06-06 14:59:28 -0400 | [diff] [blame] | 756 | |
Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 757 | spin_helper.UnSpin(); |
msg555 | 0f020d1 | 2013-06-06 14:59:28 -0400 | [diff] [blame] | 758 | |
| 759 | // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes). |
| 760 | void* join_result; |
| 761 | ASSERT_EQ(0, pthread_join(t2, &join_result)); |
Elliott Hughes | 5b9310e | 2013-10-02 16:59:05 -0700 | [diff] [blame] | 762 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result)); |
msg555 | 0f020d1 | 2013-06-06 14:59:28 -0400 | [diff] [blame] | 763 | } |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 764 | |
Elliott Hughes | 70b24b1 | 2013-11-15 11:51:07 -0800 | [diff] [blame] | 765 | TEST(pthread, pthread_join__race) { |
| 766 | // http://b/11693195 --- pthread_join could return before the thread had actually exited. |
| 767 | // If the joiner unmapped the thread's stack, that could lead to SIGSEGV in the thread. |
| 768 | for (size_t i = 0; i < 1024; ++i) { |
Yabin Cui | a36158a | 2015-11-16 21:06:16 -0800 | [diff] [blame] | 769 | size_t stack_size = 640*1024; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 770 | void* stack = mmap(nullptr, stack_size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0); |
Elliott Hughes | 70b24b1 | 2013-11-15 11:51:07 -0800 | [diff] [blame] | 771 | |
| 772 | pthread_attr_t a; |
| 773 | pthread_attr_init(&a); |
| 774 | pthread_attr_setstack(&a, stack, stack_size); |
| 775 | |
| 776 | pthread_t t; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 777 | ASSERT_EQ(0, pthread_create(&t, &a, IdFn, nullptr)); |
| 778 | ASSERT_EQ(0, pthread_join(t, nullptr)); |
Elliott Hughes | 70b24b1 | 2013-11-15 11:51:07 -0800 | [diff] [blame] | 779 | ASSERT_EQ(0, munmap(stack, stack_size)); |
| 780 | } |
| 781 | } |
| 782 | |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 783 | static void* GetActualGuardSizeFn(void* arg) { |
| 784 | pthread_attr_t attributes; |
| 785 | pthread_getattr_np(pthread_self(), &attributes); |
| 786 | pthread_attr_getguardsize(&attributes, reinterpret_cast<size_t*>(arg)); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 787 | return nullptr; |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | static size_t GetActualGuardSize(const pthread_attr_t& attributes) { |
| 791 | size_t result; |
| 792 | pthread_t t; |
| 793 | pthread_create(&t, &attributes, GetActualGuardSizeFn, &result); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 794 | pthread_join(t, nullptr); |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 795 | return result; |
| 796 | } |
| 797 | |
| 798 | static void* GetActualStackSizeFn(void* arg) { |
| 799 | pthread_attr_t attributes; |
| 800 | pthread_getattr_np(pthread_self(), &attributes); |
| 801 | pthread_attr_getstacksize(&attributes, reinterpret_cast<size_t*>(arg)); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 802 | return nullptr; |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | static size_t GetActualStackSize(const pthread_attr_t& attributes) { |
| 806 | size_t result; |
| 807 | pthread_t t; |
| 808 | pthread_create(&t, &attributes, GetActualStackSizeFn, &result); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 809 | pthread_join(t, nullptr); |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 810 | return result; |
| 811 | } |
| 812 | |
Elliott Hughes | d6c678c | 2017-06-27 17:01:57 -0700 | [diff] [blame] | 813 | TEST(pthread, pthread_attr_setguardsize_tiny) { |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 814 | pthread_attr_t attributes; |
| 815 | ASSERT_EQ(0, pthread_attr_init(&attributes)); |
| 816 | |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 817 | // No such thing as too small: will be rounded up to one page by pthread_create. |
| 818 | ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 128)); |
| 819 | size_t guard_size; |
| 820 | ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size)); |
| 821 | ASSERT_EQ(128U, guard_size); |
Juan Yescas | 65af9a8 | 2023-12-07 11:35:21 -0800 | [diff] [blame] | 822 | ASSERT_EQ(static_cast<unsigned long>(getpagesize()), GetActualGuardSize(attributes)); |
Elliott Hughes | d6c678c | 2017-06-27 17:01:57 -0700 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | TEST(pthread, pthread_attr_setguardsize_reasonable) { |
| 826 | pthread_attr_t attributes; |
| 827 | ASSERT_EQ(0, pthread_attr_init(&attributes)); |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 828 | |
| 829 | // Large enough and a multiple of the page size. |
| 830 | ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024)); |
Elliott Hughes | d6c678c | 2017-06-27 17:01:57 -0700 | [diff] [blame] | 831 | size_t guard_size; |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 832 | ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size)); |
| 833 | ASSERT_EQ(32*1024U, guard_size); |
Elliott Hughes | d6c678c | 2017-06-27 17:01:57 -0700 | [diff] [blame] | 834 | ASSERT_EQ(32*1024U, GetActualGuardSize(attributes)); |
| 835 | } |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 836 | |
Elliott Hughes | d6c678c | 2017-06-27 17:01:57 -0700 | [diff] [blame] | 837 | TEST(pthread, pthread_attr_setguardsize_needs_rounding) { |
| 838 | pthread_attr_t attributes; |
| 839 | ASSERT_EQ(0, pthread_attr_init(&attributes)); |
| 840 | |
| 841 | // Large enough but not a multiple of the page size. |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 842 | ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024 + 1)); |
Elliott Hughes | d6c678c | 2017-06-27 17:01:57 -0700 | [diff] [blame] | 843 | size_t guard_size; |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 844 | ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size)); |
| 845 | ASSERT_EQ(32*1024U + 1, guard_size); |
Juan Yescas | 65af9a8 | 2023-12-07 11:35:21 -0800 | [diff] [blame] | 846 | ASSERT_EQ(roundup(32 * 1024U + 1, getpagesize()), GetActualGuardSize(attributes)); |
Elliott Hughes | d6c678c | 2017-06-27 17:01:57 -0700 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | TEST(pthread, pthread_attr_setguardsize_enormous) { |
| 850 | pthread_attr_t attributes; |
| 851 | ASSERT_EQ(0, pthread_attr_init(&attributes)); |
| 852 | |
| 853 | // Larger than the stack itself. (Historically we mistakenly carved |
| 854 | // the guard out of the stack itself, rather than adding it after the |
| 855 | // end.) |
| 856 | ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024*1024)); |
| 857 | size_t guard_size; |
| 858 | ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size)); |
| 859 | ASSERT_EQ(32*1024*1024U, guard_size); |
| 860 | ASSERT_EQ(32*1024*1024U, GetActualGuardSize(attributes)); |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | TEST(pthread, pthread_attr_setstacksize) { |
| 864 | pthread_attr_t attributes; |
| 865 | ASSERT_EQ(0, pthread_attr_init(&attributes)); |
| 866 | |
| 867 | // Get the default stack size. |
| 868 | size_t default_stack_size; |
| 869 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &default_stack_size)); |
| 870 | |
| 871 | // Too small. |
| 872 | ASSERT_EQ(EINVAL, pthread_attr_setstacksize(&attributes, 128)); |
| 873 | size_t stack_size; |
| 874 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size)); |
| 875 | ASSERT_EQ(default_stack_size, stack_size); |
| 876 | ASSERT_GE(GetActualStackSize(attributes), default_stack_size); |
| 877 | |
Yabin Cui | 917d390 | 2015-01-08 12:32:42 -0800 | [diff] [blame] | 878 | // 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] | 879 | ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024)); |
| 880 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size)); |
| 881 | ASSERT_EQ(32*1024U, stack_size); |
Yabin Cui | 917d390 | 2015-01-08 12:32:42 -0800 | [diff] [blame] | 882 | ASSERT_GE(GetActualStackSize(attributes), 32*1024U); |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 883 | |
Yabin Cui | 917d390 | 2015-01-08 12:32:42 -0800 | [diff] [blame] | 884 | // Large enough but not aligned; will be rounded up by pthread_create. |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 885 | ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024 + 1)); |
| 886 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size)); |
| 887 | ASSERT_EQ(32*1024U + 1, stack_size); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 888 | #if defined(__BIONIC__) |
Yabin Cui | 917d390 | 2015-01-08 12:32:42 -0800 | [diff] [blame] | 889 | ASSERT_GT(GetActualStackSize(attributes), 32*1024U + 1); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 890 | #else // __BIONIC__ |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 891 | // glibc rounds down, in violation of POSIX. They document this in their BUGS section. |
| 892 | ASSERT_EQ(GetActualStackSize(attributes), 32*1024U); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 893 | #endif // __BIONIC__ |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 894 | } |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 895 | |
Yabin Cui | 76615da | 2015-03-17 14:22:09 -0700 | [diff] [blame] | 896 | TEST(pthread, pthread_rwlockattr_smoke) { |
| 897 | pthread_rwlockattr_t attr; |
| 898 | ASSERT_EQ(0, pthread_rwlockattr_init(&attr)); |
| 899 | |
| 900 | int pshared_value_array[] = {PTHREAD_PROCESS_PRIVATE, PTHREAD_PROCESS_SHARED}; |
| 901 | for (size_t i = 0; i < sizeof(pshared_value_array) / sizeof(pshared_value_array[0]); ++i) { |
| 902 | ASSERT_EQ(0, pthread_rwlockattr_setpshared(&attr, pshared_value_array[i])); |
| 903 | int pshared; |
| 904 | ASSERT_EQ(0, pthread_rwlockattr_getpshared(&attr, &pshared)); |
| 905 | ASSERT_EQ(pshared_value_array[i], pshared); |
| 906 | } |
| 907 | |
Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame] | 908 | #if !defined(ANDROID_HOST_MUSL) |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 909 | // musl doesn't have pthread_rwlockattr_setkind_np |
Yabin Cui | 76615da | 2015-03-17 14:22:09 -0700 | [diff] [blame] | 910 | int kind_array[] = {PTHREAD_RWLOCK_PREFER_READER_NP, |
| 911 | PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP}; |
| 912 | for (size_t i = 0; i < sizeof(kind_array) / sizeof(kind_array[0]); ++i) { |
| 913 | ASSERT_EQ(0, pthread_rwlockattr_setkind_np(&attr, kind_array[i])); |
| 914 | int kind; |
| 915 | ASSERT_EQ(0, pthread_rwlockattr_getkind_np(&attr, &kind)); |
| 916 | ASSERT_EQ(kind_array[i], kind); |
| 917 | } |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 918 | #endif |
Yabin Cui | 76615da | 2015-03-17 14:22:09 -0700 | [diff] [blame] | 919 | |
| 920 | ASSERT_EQ(0, pthread_rwlockattr_destroy(&attr)); |
| 921 | } |
| 922 | |
| 923 | TEST(pthread, pthread_rwlock_init_same_as_PTHREAD_RWLOCK_INITIALIZER) { |
| 924 | pthread_rwlock_t lock1 = PTHREAD_RWLOCK_INITIALIZER; |
| 925 | pthread_rwlock_t lock2; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 926 | ASSERT_EQ(0, pthread_rwlock_init(&lock2, nullptr)); |
Yabin Cui | 76615da | 2015-03-17 14:22:09 -0700 | [diff] [blame] | 927 | ASSERT_EQ(0, memcmp(&lock1, &lock2, sizeof(lock1))); |
| 928 | } |
| 929 | |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 930 | TEST(pthread, pthread_rwlock_smoke) { |
| 931 | pthread_rwlock_t l; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 932 | ASSERT_EQ(0, pthread_rwlock_init(&l, nullptr)); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 933 | |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 934 | // Single read lock |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 935 | ASSERT_EQ(0, pthread_rwlock_rdlock(&l)); |
| 936 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 937 | |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 938 | // Multiple read lock |
| 939 | ASSERT_EQ(0, pthread_rwlock_rdlock(&l)); |
| 940 | ASSERT_EQ(0, pthread_rwlock_rdlock(&l)); |
| 941 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 942 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 943 | |
| 944 | // Write lock |
Calin Juravle | 92687e4 | 2014-05-22 19:21:22 +0100 | [diff] [blame] | 945 | ASSERT_EQ(0, pthread_rwlock_wrlock(&l)); |
| 946 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 947 | |
| 948 | // Try writer lock |
| 949 | ASSERT_EQ(0, pthread_rwlock_trywrlock(&l)); |
| 950 | ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l)); |
| 951 | ASSERT_EQ(EBUSY, pthread_rwlock_tryrdlock(&l)); |
| 952 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 953 | |
| 954 | // Try reader lock |
| 955 | ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l)); |
| 956 | ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l)); |
| 957 | ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l)); |
| 958 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 959 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 960 | |
| 961 | // Try writer lock after unlock |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 962 | ASSERT_EQ(0, pthread_rwlock_wrlock(&l)); |
| 963 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 964 | |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 965 | // EDEADLK in "read after write" |
| 966 | ASSERT_EQ(0, pthread_rwlock_wrlock(&l)); |
| 967 | ASSERT_EQ(EDEADLK, pthread_rwlock_rdlock(&l)); |
| 968 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 969 | |
| 970 | // EDEADLK in "write after write" |
| 971 | ASSERT_EQ(0, pthread_rwlock_wrlock(&l)); |
| 972 | ASSERT_EQ(EDEADLK, pthread_rwlock_wrlock(&l)); |
| 973 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 974 | |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 975 | ASSERT_EQ(0, pthread_rwlock_destroy(&l)); |
| 976 | } |
| 977 | |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 978 | struct RwlockWakeupHelperArg { |
| 979 | pthread_rwlock_t lock; |
| 980 | enum Progress { |
| 981 | LOCK_INITIALIZED, |
| 982 | LOCK_WAITING, |
| 983 | LOCK_RELEASED, |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 984 | LOCK_ACCESSED, |
| 985 | LOCK_TIMEDOUT, |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 986 | }; |
| 987 | std::atomic<Progress> progress; |
Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 988 | std::atomic<pid_t> tid; |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 989 | std::function<int (pthread_rwlock_t*)> trylock_function; |
| 990 | std::function<int (pthread_rwlock_t*)> lock_function; |
| 991 | std::function<int (pthread_rwlock_t*, const timespec*)> timed_lock_function; |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 992 | clockid_t clock; |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 993 | }; |
| 994 | |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 995 | static void pthread_rwlock_wakeup_helper(RwlockWakeupHelperArg* arg) { |
Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 996 | arg->tid = gettid(); |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 997 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_INITIALIZED, arg->progress); |
| 998 | arg->progress = RwlockWakeupHelperArg::LOCK_WAITING; |
| 999 | |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1000 | ASSERT_EQ(EBUSY, arg->trylock_function(&arg->lock)); |
| 1001 | ASSERT_EQ(0, arg->lock_function(&arg->lock)); |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 1002 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_RELEASED, arg->progress); |
| 1003 | ASSERT_EQ(0, pthread_rwlock_unlock(&arg->lock)); |
| 1004 | |
| 1005 | arg->progress = RwlockWakeupHelperArg::LOCK_ACCESSED; |
| 1006 | } |
| 1007 | |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1008 | 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] | 1009 | RwlockWakeupHelperArg wakeup_arg; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 1010 | ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, nullptr)); |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 1011 | ASSERT_EQ(0, pthread_rwlock_rdlock(&wakeup_arg.lock)); |
| 1012 | wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED; |
Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 1013 | wakeup_arg.tid = 0; |
Tom Cherry | 60ddedf | 2018-02-20 15:40:02 -0800 | [diff] [blame] | 1014 | wakeup_arg.trylock_function = &pthread_rwlock_trywrlock; |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1015 | wakeup_arg.lock_function = lock_function; |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 1016 | |
| 1017 | pthread_t thread; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 1018 | ASSERT_EQ(0, pthread_create(&thread, nullptr, |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1019 | reinterpret_cast<void* (*)(void*)>(pthread_rwlock_wakeup_helper), &wakeup_arg)); |
Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 1020 | WaitUntilThreadSleep(wakeup_arg.tid); |
| 1021 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress); |
| 1022 | |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 1023 | wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_RELEASED; |
| 1024 | ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock)); |
| 1025 | |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 1026 | ASSERT_EQ(0, pthread_join(thread, nullptr)); |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 1027 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_ACCESSED, wakeup_arg.progress); |
| 1028 | ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock)); |
| 1029 | } |
| 1030 | |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1031 | TEST(pthread, pthread_rwlock_reader_wakeup_writer) { |
| 1032 | test_pthread_rwlock_reader_wakeup_writer(pthread_rwlock_wrlock); |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 1033 | } |
| 1034 | |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1035 | TEST(pthread, pthread_rwlock_reader_wakeup_writer_timedwait) { |
| 1036 | timespec ts; |
| 1037 | ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts)); |
| 1038 | ts.tv_sec += 1; |
| 1039 | test_pthread_rwlock_reader_wakeup_writer([&](pthread_rwlock_t* lock) { |
| 1040 | return pthread_rwlock_timedwrlock(lock, &ts); |
| 1041 | }); |
| 1042 | } |
| 1043 | |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1044 | TEST(pthread, pthread_rwlock_reader_wakeup_writer_timedwait_monotonic_np) { |
| 1045 | #if defined(__BIONIC__) |
| 1046 | timespec ts; |
| 1047 | ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts)); |
| 1048 | ts.tv_sec += 1; |
| 1049 | test_pthread_rwlock_reader_wakeup_writer( |
| 1050 | [&](pthread_rwlock_t* lock) { return pthread_rwlock_timedwrlock_monotonic_np(lock, &ts); }); |
| 1051 | #else // __BIONIC__ |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 1052 | GTEST_SKIP() << "pthread_rwlock_timedwrlock_monotonic_np not available"; |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1053 | #endif // __BIONIC__ |
| 1054 | } |
| 1055 | |
Tom Cherry | 6901080 | 2019-05-07 20:33:05 -0700 | [diff] [blame] | 1056 | TEST(pthread, pthread_rwlock_reader_wakeup_writer_clockwait) { |
| 1057 | #if defined(__BIONIC__) |
| 1058 | timespec ts; |
| 1059 | ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts)); |
| 1060 | ts.tv_sec += 1; |
| 1061 | test_pthread_rwlock_reader_wakeup_writer([&](pthread_rwlock_t* lock) { |
| 1062 | return pthread_rwlock_clockwrlock(lock, CLOCK_MONOTONIC, &ts); |
| 1063 | }); |
| 1064 | |
| 1065 | ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts)); |
| 1066 | ts.tv_sec += 1; |
| 1067 | test_pthread_rwlock_reader_wakeup_writer([&](pthread_rwlock_t* lock) { |
| 1068 | return pthread_rwlock_clockwrlock(lock, CLOCK_REALTIME, &ts); |
| 1069 | }); |
| 1070 | #else // __BIONIC__ |
| 1071 | GTEST_SKIP() << "pthread_rwlock_clockwrlock not available"; |
| 1072 | #endif // __BIONIC__ |
| 1073 | } |
| 1074 | |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1075 | 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] | 1076 | RwlockWakeupHelperArg wakeup_arg; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 1077 | ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, nullptr)); |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 1078 | ASSERT_EQ(0, pthread_rwlock_wrlock(&wakeup_arg.lock)); |
| 1079 | wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED; |
Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 1080 | wakeup_arg.tid = 0; |
Tom Cherry | 60ddedf | 2018-02-20 15:40:02 -0800 | [diff] [blame] | 1081 | wakeup_arg.trylock_function = &pthread_rwlock_tryrdlock; |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1082 | wakeup_arg.lock_function = lock_function; |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 1083 | |
| 1084 | pthread_t thread; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 1085 | ASSERT_EQ(0, pthread_create(&thread, nullptr, |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1086 | reinterpret_cast<void* (*)(void*)>(pthread_rwlock_wakeup_helper), &wakeup_arg)); |
Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 1087 | WaitUntilThreadSleep(wakeup_arg.tid); |
| 1088 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress); |
| 1089 | |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 1090 | wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_RELEASED; |
| 1091 | ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock)); |
| 1092 | |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 1093 | ASSERT_EQ(0, pthread_join(thread, nullptr)); |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 1094 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_ACCESSED, wakeup_arg.progress); |
| 1095 | ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock)); |
| 1096 | } |
| 1097 | |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1098 | TEST(pthread, pthread_rwlock_writer_wakeup_reader) { |
| 1099 | test_pthread_rwlock_writer_wakeup_reader(pthread_rwlock_rdlock); |
| 1100 | } |
| 1101 | |
| 1102 | TEST(pthread, pthread_rwlock_writer_wakeup_reader_timedwait) { |
| 1103 | timespec ts; |
| 1104 | ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts)); |
| 1105 | ts.tv_sec += 1; |
| 1106 | test_pthread_rwlock_writer_wakeup_reader([&](pthread_rwlock_t* lock) { |
| 1107 | return pthread_rwlock_timedrdlock(lock, &ts); |
| 1108 | }); |
| 1109 | } |
| 1110 | |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1111 | TEST(pthread, pthread_rwlock_writer_wakeup_reader_timedwait_monotonic_np) { |
| 1112 | #if defined(__BIONIC__) |
| 1113 | timespec ts; |
| 1114 | ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts)); |
| 1115 | ts.tv_sec += 1; |
| 1116 | test_pthread_rwlock_writer_wakeup_reader( |
| 1117 | [&](pthread_rwlock_t* lock) { return pthread_rwlock_timedrdlock_monotonic_np(lock, &ts); }); |
| 1118 | #else // __BIONIC__ |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 1119 | GTEST_SKIP() << "pthread_rwlock_timedrdlock_monotonic_np not available"; |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1120 | #endif // __BIONIC__ |
| 1121 | } |
| 1122 | |
Tom Cherry | 6901080 | 2019-05-07 20:33:05 -0700 | [diff] [blame] | 1123 | TEST(pthread, pthread_rwlock_writer_wakeup_reader_clockwait) { |
| 1124 | #if defined(__BIONIC__) |
| 1125 | timespec ts; |
| 1126 | ASSERT_EQ(0, clock_gettime(CLOCK_MONOTONIC, &ts)); |
| 1127 | ts.tv_sec += 1; |
| 1128 | test_pthread_rwlock_writer_wakeup_reader([&](pthread_rwlock_t* lock) { |
| 1129 | return pthread_rwlock_clockrdlock(lock, CLOCK_MONOTONIC, &ts); |
| 1130 | }); |
| 1131 | |
| 1132 | ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts)); |
| 1133 | ts.tv_sec += 1; |
| 1134 | test_pthread_rwlock_writer_wakeup_reader([&](pthread_rwlock_t* lock) { |
| 1135 | return pthread_rwlock_clockrdlock(lock, CLOCK_REALTIME, &ts); |
| 1136 | }); |
| 1137 | #else // __BIONIC__ |
| 1138 | GTEST_SKIP() << "pthread_rwlock_clockrdlock not available"; |
| 1139 | #endif // __BIONIC__ |
| 1140 | } |
| 1141 | |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1142 | static void pthread_rwlock_wakeup_timeout_helper(RwlockWakeupHelperArg* arg) { |
| 1143 | arg->tid = gettid(); |
| 1144 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_INITIALIZED, arg->progress); |
| 1145 | arg->progress = RwlockWakeupHelperArg::LOCK_WAITING; |
| 1146 | |
| 1147 | ASSERT_EQ(EBUSY, arg->trylock_function(&arg->lock)); |
| 1148 | |
| 1149 | timespec ts; |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1150 | ASSERT_EQ(0, clock_gettime(arg->clock, &ts)); |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1151 | ASSERT_EQ(ETIMEDOUT, arg->timed_lock_function(&arg->lock, &ts)); |
| 1152 | ts.tv_nsec = -1; |
| 1153 | ASSERT_EQ(EINVAL, arg->timed_lock_function(&arg->lock, &ts)); |
| 1154 | ts.tv_nsec = NS_PER_S; |
| 1155 | ASSERT_EQ(EINVAL, arg->timed_lock_function(&arg->lock, &ts)); |
| 1156 | ts.tv_nsec = NS_PER_S - 1; |
| 1157 | ts.tv_sec = -1; |
| 1158 | ASSERT_EQ(ETIMEDOUT, arg->timed_lock_function(&arg->lock, &ts)); |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1159 | ASSERT_EQ(0, clock_gettime(arg->clock, &ts)); |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1160 | ts.tv_sec += 1; |
| 1161 | ASSERT_EQ(ETIMEDOUT, arg->timed_lock_function(&arg->lock, &ts)); |
| 1162 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, arg->progress); |
| 1163 | arg->progress = RwlockWakeupHelperArg::LOCK_TIMEDOUT; |
| 1164 | } |
| 1165 | |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1166 | static void pthread_rwlock_timedrdlock_timeout_helper( |
| 1167 | clockid_t clock, int (*lock_function)(pthread_rwlock_t* __rwlock, const timespec* __timeout)) { |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1168 | RwlockWakeupHelperArg wakeup_arg; |
| 1169 | ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, nullptr)); |
| 1170 | ASSERT_EQ(0, pthread_rwlock_wrlock(&wakeup_arg.lock)); |
| 1171 | wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED; |
| 1172 | wakeup_arg.tid = 0; |
Tom Cherry | 60ddedf | 2018-02-20 15:40:02 -0800 | [diff] [blame] | 1173 | wakeup_arg.trylock_function = &pthread_rwlock_tryrdlock; |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1174 | wakeup_arg.timed_lock_function = lock_function; |
| 1175 | wakeup_arg.clock = clock; |
| 1176 | |
| 1177 | pthread_t thread; |
| 1178 | ASSERT_EQ(0, pthread_create(&thread, nullptr, |
| 1179 | reinterpret_cast<void* (*)(void*)>(pthread_rwlock_wakeup_timeout_helper), &wakeup_arg)); |
| 1180 | WaitUntilThreadSleep(wakeup_arg.tid); |
| 1181 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress); |
| 1182 | |
| 1183 | ASSERT_EQ(0, pthread_join(thread, nullptr)); |
| 1184 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_TIMEDOUT, wakeup_arg.progress); |
| 1185 | ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock)); |
| 1186 | ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock)); |
| 1187 | } |
| 1188 | |
| 1189 | TEST(pthread, pthread_rwlock_timedrdlock_timeout) { |
| 1190 | pthread_rwlock_timedrdlock_timeout_helper(CLOCK_REALTIME, pthread_rwlock_timedrdlock); |
| 1191 | } |
| 1192 | |
| 1193 | TEST(pthread, pthread_rwlock_timedrdlock_monotonic_np_timeout) { |
| 1194 | #if defined(__BIONIC__) |
| 1195 | pthread_rwlock_timedrdlock_timeout_helper(CLOCK_MONOTONIC, |
| 1196 | pthread_rwlock_timedrdlock_monotonic_np); |
| 1197 | #else // __BIONIC__ |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 1198 | GTEST_SKIP() << "pthread_rwlock_timedrdlock_monotonic_np not available"; |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1199 | #endif // __BIONIC__ |
| 1200 | } |
| 1201 | |
Tom Cherry | 6901080 | 2019-05-07 20:33:05 -0700 | [diff] [blame] | 1202 | TEST(pthread, pthread_rwlock_clockrdlock_monotonic_timeout) { |
| 1203 | #if defined(__BIONIC__) |
| 1204 | pthread_rwlock_timedrdlock_timeout_helper( |
| 1205 | CLOCK_MONOTONIC, [](pthread_rwlock_t* __rwlock, const timespec* __timeout) { |
| 1206 | return pthread_rwlock_clockrdlock(__rwlock, CLOCK_MONOTONIC, __timeout); |
| 1207 | }); |
| 1208 | #else // __BIONIC__ |
| 1209 | GTEST_SKIP() << "pthread_rwlock_clockrdlock not available"; |
| 1210 | #endif // __BIONIC__ |
| 1211 | } |
| 1212 | |
| 1213 | TEST(pthread, pthread_rwlock_clockrdlock_realtime_timeout) { |
| 1214 | #if defined(__BIONIC__) |
| 1215 | pthread_rwlock_timedrdlock_timeout_helper( |
| 1216 | CLOCK_REALTIME, [](pthread_rwlock_t* __rwlock, const timespec* __timeout) { |
| 1217 | return pthread_rwlock_clockrdlock(__rwlock, CLOCK_REALTIME, __timeout); |
| 1218 | }); |
| 1219 | #else // __BIONIC__ |
| 1220 | GTEST_SKIP() << "pthread_rwlock_clockrdlock not available"; |
| 1221 | #endif // __BIONIC__ |
| 1222 | } |
| 1223 | |
| 1224 | TEST(pthread, pthread_rwlock_clockrdlock_invalid) { |
| 1225 | #if defined(__BIONIC__) |
| 1226 | pthread_rwlock_t lock = PTHREAD_RWLOCK_INITIALIZER; |
| 1227 | timespec ts; |
| 1228 | EXPECT_EQ(EINVAL, pthread_rwlock_clockrdlock(&lock, CLOCK_PROCESS_CPUTIME_ID, &ts)); |
| 1229 | #else // __BIONIC__ |
| 1230 | GTEST_SKIP() << "pthread_rwlock_clockrdlock not available"; |
| 1231 | #endif // __BIONIC__ |
| 1232 | } |
| 1233 | |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1234 | static void pthread_rwlock_timedwrlock_timeout_helper( |
| 1235 | clockid_t clock, int (*lock_function)(pthread_rwlock_t* __rwlock, const timespec* __timeout)) { |
| 1236 | RwlockWakeupHelperArg wakeup_arg; |
| 1237 | ASSERT_EQ(0, pthread_rwlock_init(&wakeup_arg.lock, nullptr)); |
| 1238 | ASSERT_EQ(0, pthread_rwlock_rdlock(&wakeup_arg.lock)); |
| 1239 | wakeup_arg.progress = RwlockWakeupHelperArg::LOCK_INITIALIZED; |
| 1240 | wakeup_arg.tid = 0; |
| 1241 | wakeup_arg.trylock_function = &pthread_rwlock_trywrlock; |
| 1242 | wakeup_arg.timed_lock_function = lock_function; |
| 1243 | wakeup_arg.clock = clock; |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1244 | |
| 1245 | pthread_t thread; |
| 1246 | ASSERT_EQ(0, pthread_create(&thread, nullptr, |
| 1247 | reinterpret_cast<void* (*)(void*)>(pthread_rwlock_wakeup_timeout_helper), &wakeup_arg)); |
| 1248 | WaitUntilThreadSleep(wakeup_arg.tid); |
| 1249 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_WAITING, wakeup_arg.progress); |
| 1250 | |
| 1251 | ASSERT_EQ(0, pthread_join(thread, nullptr)); |
| 1252 | ASSERT_EQ(RwlockWakeupHelperArg::LOCK_TIMEDOUT, wakeup_arg.progress); |
| 1253 | ASSERT_EQ(0, pthread_rwlock_unlock(&wakeup_arg.lock)); |
| 1254 | ASSERT_EQ(0, pthread_rwlock_destroy(&wakeup_arg.lock)); |
| 1255 | } |
| 1256 | |
| 1257 | TEST(pthread, pthread_rwlock_timedwrlock_timeout) { |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1258 | pthread_rwlock_timedwrlock_timeout_helper(CLOCK_REALTIME, pthread_rwlock_timedwrlock); |
| 1259 | } |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1260 | |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1261 | TEST(pthread, pthread_rwlock_timedwrlock_monotonic_np_timeout) { |
| 1262 | #if defined(__BIONIC__) |
| 1263 | pthread_rwlock_timedwrlock_timeout_helper(CLOCK_MONOTONIC, |
| 1264 | pthread_rwlock_timedwrlock_monotonic_np); |
| 1265 | #else // __BIONIC__ |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 1266 | GTEST_SKIP() << "pthread_rwlock_timedwrlock_monotonic_np not available"; |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1267 | #endif // __BIONIC__ |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1268 | } |
| 1269 | |
Tom Cherry | 6901080 | 2019-05-07 20:33:05 -0700 | [diff] [blame] | 1270 | TEST(pthread, pthread_rwlock_clockwrlock_monotonic_timeout) { |
| 1271 | #if defined(__BIONIC__) |
| 1272 | pthread_rwlock_timedwrlock_timeout_helper( |
| 1273 | CLOCK_MONOTONIC, [](pthread_rwlock_t* __rwlock, const timespec* __timeout) { |
| 1274 | return pthread_rwlock_clockwrlock(__rwlock, CLOCK_MONOTONIC, __timeout); |
| 1275 | }); |
| 1276 | #else // __BIONIC__ |
| 1277 | GTEST_SKIP() << "pthread_rwlock_clockwrlock not available"; |
| 1278 | #endif // __BIONIC__ |
| 1279 | } |
| 1280 | |
| 1281 | TEST(pthread, pthread_rwlock_clockwrlock_realtime_timeout) { |
| 1282 | #if defined(__BIONIC__) |
| 1283 | pthread_rwlock_timedwrlock_timeout_helper( |
| 1284 | CLOCK_REALTIME, [](pthread_rwlock_t* __rwlock, const timespec* __timeout) { |
| 1285 | return pthread_rwlock_clockwrlock(__rwlock, CLOCK_REALTIME, __timeout); |
| 1286 | }); |
| 1287 | #else // __BIONIC__ |
| 1288 | GTEST_SKIP() << "pthread_rwlock_clockwrlock not available"; |
| 1289 | #endif // __BIONIC__ |
| 1290 | } |
| 1291 | |
| 1292 | TEST(pthread, pthread_rwlock_clockwrlock_invalid) { |
| 1293 | #if defined(__BIONIC__) |
| 1294 | pthread_rwlock_t lock = PTHREAD_RWLOCK_INITIALIZER; |
| 1295 | timespec ts; |
| 1296 | EXPECT_EQ(EINVAL, pthread_rwlock_clockwrlock(&lock, CLOCK_PROCESS_CPUTIME_ID, &ts)); |
| 1297 | #else // __BIONIC__ |
| 1298 | GTEST_SKIP() << "pthread_rwlock_clockrwlock not available"; |
| 1299 | #endif // __BIONIC__ |
| 1300 | } |
| 1301 | |
Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame] | 1302 | #if !defined(ANDROID_HOST_MUSL) |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 1303 | // musl doesn't have pthread_rwlockattr_setkind_np |
Yabin Cui | 76615da | 2015-03-17 14:22:09 -0700 | [diff] [blame] | 1304 | class RwlockKindTestHelper { |
| 1305 | private: |
| 1306 | struct ThreadArg { |
| 1307 | RwlockKindTestHelper* helper; |
| 1308 | std::atomic<pid_t>& tid; |
| 1309 | |
| 1310 | ThreadArg(RwlockKindTestHelper* helper, std::atomic<pid_t>& tid) |
| 1311 | : helper(helper), tid(tid) { } |
| 1312 | }; |
| 1313 | |
| 1314 | public: |
| 1315 | pthread_rwlock_t lock; |
| 1316 | |
| 1317 | public: |
Chih-Hung Hsieh | 62e3a07 | 2016-05-03 12:08:05 -0700 | [diff] [blame] | 1318 | explicit RwlockKindTestHelper(int kind_type) { |
Yabin Cui | 76615da | 2015-03-17 14:22:09 -0700 | [diff] [blame] | 1319 | InitRwlock(kind_type); |
| 1320 | } |
| 1321 | |
| 1322 | ~RwlockKindTestHelper() { |
| 1323 | DestroyRwlock(); |
| 1324 | } |
| 1325 | |
| 1326 | void CreateWriterThread(pthread_t& thread, std::atomic<pid_t>& tid) { |
| 1327 | tid = 0; |
| 1328 | ThreadArg* arg = new ThreadArg(this, tid); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 1329 | ASSERT_EQ(0, pthread_create(&thread, nullptr, |
Yabin Cui | 76615da | 2015-03-17 14:22:09 -0700 | [diff] [blame] | 1330 | reinterpret_cast<void* (*)(void*)>(WriterThreadFn), arg)); |
| 1331 | } |
| 1332 | |
| 1333 | void CreateReaderThread(pthread_t& thread, std::atomic<pid_t>& tid) { |
| 1334 | tid = 0; |
| 1335 | ThreadArg* arg = new ThreadArg(this, tid); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 1336 | ASSERT_EQ(0, pthread_create(&thread, nullptr, |
Yabin Cui | 76615da | 2015-03-17 14:22:09 -0700 | [diff] [blame] | 1337 | reinterpret_cast<void* (*)(void*)>(ReaderThreadFn), arg)); |
| 1338 | } |
| 1339 | |
| 1340 | private: |
| 1341 | void InitRwlock(int kind_type) { |
| 1342 | pthread_rwlockattr_t attr; |
| 1343 | ASSERT_EQ(0, pthread_rwlockattr_init(&attr)); |
| 1344 | ASSERT_EQ(0, pthread_rwlockattr_setkind_np(&attr, kind_type)); |
| 1345 | ASSERT_EQ(0, pthread_rwlock_init(&lock, &attr)); |
| 1346 | ASSERT_EQ(0, pthread_rwlockattr_destroy(&attr)); |
| 1347 | } |
| 1348 | |
| 1349 | void DestroyRwlock() { |
| 1350 | ASSERT_EQ(0, pthread_rwlock_destroy(&lock)); |
| 1351 | } |
| 1352 | |
| 1353 | static void WriterThreadFn(ThreadArg* arg) { |
| 1354 | arg->tid = gettid(); |
| 1355 | |
| 1356 | RwlockKindTestHelper* helper = arg->helper; |
| 1357 | ASSERT_EQ(0, pthread_rwlock_wrlock(&helper->lock)); |
| 1358 | ASSERT_EQ(0, pthread_rwlock_unlock(&helper->lock)); |
| 1359 | delete arg; |
| 1360 | } |
| 1361 | |
| 1362 | static void ReaderThreadFn(ThreadArg* arg) { |
| 1363 | arg->tid = gettid(); |
| 1364 | |
| 1365 | RwlockKindTestHelper* helper = arg->helper; |
| 1366 | ASSERT_EQ(0, pthread_rwlock_rdlock(&helper->lock)); |
| 1367 | ASSERT_EQ(0, pthread_rwlock_unlock(&helper->lock)); |
| 1368 | delete arg; |
| 1369 | } |
| 1370 | }; |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 1371 | #endif |
Yabin Cui | 76615da | 2015-03-17 14:22:09 -0700 | [diff] [blame] | 1372 | |
| 1373 | TEST(pthread, pthread_rwlock_kind_PTHREAD_RWLOCK_PREFER_READER_NP) { |
Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame] | 1374 | #if !defined(ANDROID_HOST_MUSL) |
Yabin Cui | 76615da | 2015-03-17 14:22:09 -0700 | [diff] [blame] | 1375 | RwlockKindTestHelper helper(PTHREAD_RWLOCK_PREFER_READER_NP); |
| 1376 | ASSERT_EQ(0, pthread_rwlock_rdlock(&helper.lock)); |
| 1377 | |
| 1378 | pthread_t writer_thread; |
| 1379 | std::atomic<pid_t> writer_tid; |
| 1380 | helper.CreateWriterThread(writer_thread, writer_tid); |
| 1381 | WaitUntilThreadSleep(writer_tid); |
| 1382 | |
| 1383 | pthread_t reader_thread; |
| 1384 | std::atomic<pid_t> reader_tid; |
| 1385 | helper.CreateReaderThread(reader_thread, reader_tid); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 1386 | ASSERT_EQ(0, pthread_join(reader_thread, nullptr)); |
Yabin Cui | 76615da | 2015-03-17 14:22:09 -0700 | [diff] [blame] | 1387 | |
| 1388 | ASSERT_EQ(0, pthread_rwlock_unlock(&helper.lock)); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 1389 | ASSERT_EQ(0, pthread_join(writer_thread, nullptr)); |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 1390 | #else |
| 1391 | GTEST_SKIP() << "musl doesn't have pthread_rwlockattr_setkind_np"; |
| 1392 | #endif |
Yabin Cui | 76615da | 2015-03-17 14:22:09 -0700 | [diff] [blame] | 1393 | } |
| 1394 | |
| 1395 | TEST(pthread, pthread_rwlock_kind_PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP) { |
Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame] | 1396 | #if !defined(ANDROID_HOST_MUSL) |
Yabin Cui | 76615da | 2015-03-17 14:22:09 -0700 | [diff] [blame] | 1397 | RwlockKindTestHelper helper(PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP); |
| 1398 | ASSERT_EQ(0, pthread_rwlock_rdlock(&helper.lock)); |
| 1399 | |
| 1400 | pthread_t writer_thread; |
| 1401 | std::atomic<pid_t> writer_tid; |
| 1402 | helper.CreateWriterThread(writer_thread, writer_tid); |
| 1403 | WaitUntilThreadSleep(writer_tid); |
| 1404 | |
| 1405 | pthread_t reader_thread; |
| 1406 | std::atomic<pid_t> reader_tid; |
| 1407 | helper.CreateReaderThread(reader_thread, reader_tid); |
| 1408 | WaitUntilThreadSleep(reader_tid); |
| 1409 | |
| 1410 | ASSERT_EQ(0, pthread_rwlock_unlock(&helper.lock)); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 1411 | ASSERT_EQ(0, pthread_join(writer_thread, nullptr)); |
| 1412 | ASSERT_EQ(0, pthread_join(reader_thread, nullptr)); |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 1413 | #else |
| 1414 | GTEST_SKIP() << "musl doesn't have pthread_rwlockattr_setkind_np"; |
| 1415 | #endif |
Yabin Cui | 76615da | 2015-03-17 14:22:09 -0700 | [diff] [blame] | 1416 | } |
| 1417 | |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 1418 | static int g_once_fn_call_count = 0; |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 1419 | static void OnceFn() { |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 1420 | ++g_once_fn_call_count; |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 1421 | } |
| 1422 | |
| 1423 | TEST(pthread, pthread_once_smoke) { |
| 1424 | pthread_once_t once_control = PTHREAD_ONCE_INIT; |
| 1425 | ASSERT_EQ(0, pthread_once(&once_control, OnceFn)); |
| 1426 | ASSERT_EQ(0, pthread_once(&once_control, OnceFn)); |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 1427 | ASSERT_EQ(1, g_once_fn_call_count); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 1428 | } |
| 1429 | |
Elliott Hughes | 3694ec6 | 2014-05-14 11:46:08 -0700 | [diff] [blame] | 1430 | static std::string pthread_once_1934122_result = ""; |
| 1431 | |
| 1432 | static void Routine2() { |
| 1433 | pthread_once_1934122_result += "2"; |
| 1434 | } |
| 1435 | |
| 1436 | static void Routine1() { |
| 1437 | pthread_once_t once_control_2 = PTHREAD_ONCE_INIT; |
| 1438 | pthread_once_1934122_result += "1"; |
| 1439 | pthread_once(&once_control_2, &Routine2); |
| 1440 | } |
| 1441 | |
| 1442 | TEST(pthread, pthread_once_1934122) { |
| 1443 | // Very old versions of Android couldn't call pthread_once from a |
| 1444 | // pthread_once init routine. http://b/1934122. |
| 1445 | pthread_once_t once_control_1 = PTHREAD_ONCE_INIT; |
| 1446 | ASSERT_EQ(0, pthread_once(&once_control_1, &Routine1)); |
| 1447 | ASSERT_EQ("12", pthread_once_1934122_result); |
| 1448 | } |
| 1449 | |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 1450 | static int g_atfork_prepare_calls = 0; |
Dmitriy Ivanov | ea295f6 | 2014-11-20 20:47:02 -0800 | [diff] [blame] | 1451 | static void AtForkPrepare1() { g_atfork_prepare_calls = (g_atfork_prepare_calls * 10) + 1; } |
| 1452 | 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] | 1453 | static int g_atfork_parent_calls = 0; |
Dmitriy Ivanov | ea295f6 | 2014-11-20 20:47:02 -0800 | [diff] [blame] | 1454 | static void AtForkParent1() { g_atfork_parent_calls = (g_atfork_parent_calls * 10) + 1; } |
| 1455 | 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] | 1456 | static int g_atfork_child_calls = 0; |
Dmitriy Ivanov | ea295f6 | 2014-11-20 20:47:02 -0800 | [diff] [blame] | 1457 | static void AtForkChild1() { g_atfork_child_calls = (g_atfork_child_calls * 10) + 1; } |
| 1458 | 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] | 1459 | |
Elliott Hughes | 2411fff | 2024-02-24 23:55:58 +0000 | [diff] [blame] | 1460 | TEST(pthread, pthread_atfork_smoke_fork) { |
Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 1461 | ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1)); |
| 1462 | ASSERT_EQ(0, pthread_atfork(AtForkPrepare2, AtForkParent2, AtForkChild2)); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 1463 | |
Elliott Hughes | 2411fff | 2024-02-24 23:55:58 +0000 | [diff] [blame] | 1464 | g_atfork_prepare_calls = g_atfork_parent_calls = g_atfork_child_calls = 0; |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 1465 | pid_t pid = fork(); |
Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 1466 | ASSERT_NE(-1, pid) << strerror(errno); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 1467 | |
Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 1468 | // Child and parent calls are made in the order they were registered. |
| 1469 | if (pid == 0) { |
Dmitriy Ivanov | ea295f6 | 2014-11-20 20:47:02 -0800 | [diff] [blame] | 1470 | ASSERT_EQ(12, g_atfork_child_calls); |
Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 1471 | _exit(0); |
| 1472 | } |
Dmitriy Ivanov | ea295f6 | 2014-11-20 20:47:02 -0800 | [diff] [blame] | 1473 | ASSERT_EQ(12, g_atfork_parent_calls); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 1474 | |
Dmitriy Ivanov | cb0443c | 2015-03-16 14:15:46 -0700 | [diff] [blame] | 1475 | // Prepare calls are made in the reverse order. |
Dmitriy Ivanov | ea295f6 | 2014-11-20 20:47:02 -0800 | [diff] [blame] | 1476 | ASSERT_EQ(21, g_atfork_prepare_calls); |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 1477 | AssertChildExited(pid, 0); |
Dmitriy Ivanov | ea295f6 | 2014-11-20 20:47:02 -0800 | [diff] [blame] | 1478 | } |
| 1479 | |
Elliott Hughes | 2411fff | 2024-02-24 23:55:58 +0000 | [diff] [blame] | 1480 | TEST(pthread, pthread_atfork_smoke_vfork) { |
| 1481 | ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1)); |
| 1482 | ASSERT_EQ(0, pthread_atfork(AtForkPrepare2, AtForkParent2, AtForkChild2)); |
| 1483 | |
| 1484 | g_atfork_prepare_calls = g_atfork_parent_calls = g_atfork_child_calls = 0; |
| 1485 | pid_t pid = vfork(); |
| 1486 | ASSERT_NE(-1, pid) << strerror(errno); |
| 1487 | |
| 1488 | // atfork handlers are not called. |
| 1489 | if (pid == 0) { |
| 1490 | ASSERT_EQ(0, g_atfork_child_calls); |
| 1491 | _exit(0); |
| 1492 | } |
| 1493 | ASSERT_EQ(0, g_atfork_parent_calls); |
| 1494 | ASSERT_EQ(0, g_atfork_prepare_calls); |
| 1495 | AssertChildExited(pid, 0); |
| 1496 | } |
| 1497 | |
| 1498 | TEST(pthread, pthread_atfork_smoke__Fork) { |
| 1499 | #if defined(__BIONIC__) |
| 1500 | ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1)); |
| 1501 | ASSERT_EQ(0, pthread_atfork(AtForkPrepare2, AtForkParent2, AtForkChild2)); |
| 1502 | |
| 1503 | g_atfork_prepare_calls = g_atfork_parent_calls = g_atfork_child_calls = 0; |
| 1504 | pid_t pid = _Fork(); |
| 1505 | ASSERT_NE(-1, pid) << strerror(errno); |
| 1506 | |
| 1507 | // atfork handlers are not called. |
| 1508 | if (pid == 0) { |
| 1509 | ASSERT_EQ(0, g_atfork_child_calls); |
| 1510 | _exit(0); |
| 1511 | } |
| 1512 | ASSERT_EQ(0, g_atfork_parent_calls); |
| 1513 | ASSERT_EQ(0, g_atfork_prepare_calls); |
| 1514 | AssertChildExited(pid, 0); |
| 1515 | #endif |
| 1516 | } |
| 1517 | |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 1518 | TEST(pthread, pthread_attr_getscope) { |
| 1519 | pthread_attr_t attr; |
| 1520 | ASSERT_EQ(0, pthread_attr_init(&attr)); |
| 1521 | |
| 1522 | int scope; |
| 1523 | ASSERT_EQ(0, pthread_attr_getscope(&attr, &scope)); |
| 1524 | ASSERT_EQ(PTHREAD_SCOPE_SYSTEM, scope); |
| 1525 | } |
Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 1526 | |
| 1527 | TEST(pthread, pthread_condattr_init) { |
| 1528 | pthread_condattr_t attr; |
| 1529 | pthread_condattr_init(&attr); |
| 1530 | |
| 1531 | clockid_t clock; |
| 1532 | ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock)); |
| 1533 | ASSERT_EQ(CLOCK_REALTIME, clock); |
| 1534 | |
| 1535 | int pshared; |
| 1536 | ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared)); |
| 1537 | ASSERT_EQ(PTHREAD_PROCESS_PRIVATE, pshared); |
| 1538 | } |
| 1539 | |
| 1540 | TEST(pthread, pthread_condattr_setclock) { |
| 1541 | pthread_condattr_t attr; |
| 1542 | pthread_condattr_init(&attr); |
| 1543 | |
| 1544 | ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_REALTIME)); |
| 1545 | clockid_t clock; |
| 1546 | ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock)); |
| 1547 | ASSERT_EQ(CLOCK_REALTIME, clock); |
| 1548 | |
| 1549 | ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC)); |
| 1550 | ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock)); |
| 1551 | ASSERT_EQ(CLOCK_MONOTONIC, clock); |
| 1552 | |
| 1553 | ASSERT_EQ(EINVAL, pthread_condattr_setclock(&attr, CLOCK_PROCESS_CPUTIME_ID)); |
| 1554 | } |
| 1555 | |
| 1556 | TEST(pthread, pthread_cond_broadcast__preserves_condattr_flags) { |
Yabin Cui | 32651b8 | 2015-03-13 20:30:00 -0700 | [diff] [blame] | 1557 | #if defined(__BIONIC__) |
Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 1558 | pthread_condattr_t attr; |
| 1559 | pthread_condattr_init(&attr); |
| 1560 | |
| 1561 | ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC)); |
| 1562 | ASSERT_EQ(0, pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED)); |
| 1563 | |
| 1564 | pthread_cond_t cond_var; |
| 1565 | ASSERT_EQ(0, pthread_cond_init(&cond_var, &attr)); |
| 1566 | |
| 1567 | ASSERT_EQ(0, pthread_cond_signal(&cond_var)); |
| 1568 | ASSERT_EQ(0, pthread_cond_broadcast(&cond_var)); |
| 1569 | |
Yabin Cui | 32651b8 | 2015-03-13 20:30:00 -0700 | [diff] [blame] | 1570 | 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] | 1571 | clockid_t clock; |
| 1572 | ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock)); |
| 1573 | ASSERT_EQ(CLOCK_MONOTONIC, clock); |
| 1574 | int pshared; |
| 1575 | ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared)); |
| 1576 | ASSERT_EQ(PTHREAD_PROCESS_SHARED, pshared); |
Yabin Cui | 32651b8 | 2015-03-13 20:30:00 -0700 | [diff] [blame] | 1577 | #else // !defined(__BIONIC__) |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 1578 | GTEST_SKIP() << "bionic-only test"; |
Yabin Cui | 32651b8 | 2015-03-13 20:30:00 -0700 | [diff] [blame] | 1579 | #endif // !defined(__BIONIC__) |
| 1580 | } |
| 1581 | |
| 1582 | class pthread_CondWakeupTest : public ::testing::Test { |
| 1583 | protected: |
| 1584 | pthread_mutex_t mutex; |
| 1585 | pthread_cond_t cond; |
| 1586 | |
| 1587 | enum Progress { |
| 1588 | INITIALIZED, |
| 1589 | WAITING, |
| 1590 | SIGNALED, |
| 1591 | FINISHED, |
| 1592 | }; |
| 1593 | std::atomic<Progress> progress; |
| 1594 | pthread_t thread; |
Peter Collingbourne | c5b8184 | 2021-12-02 12:38:46 -0800 | [diff] [blame] | 1595 | timespec ts; |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1596 | 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] | 1597 | |
| 1598 | protected: |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1599 | void SetUp() override { |
| 1600 | ASSERT_EQ(0, pthread_mutex_init(&mutex, nullptr)); |
| 1601 | } |
| 1602 | |
| 1603 | void InitCond(clockid_t clock=CLOCK_REALTIME) { |
| 1604 | pthread_condattr_t attr; |
| 1605 | ASSERT_EQ(0, pthread_condattr_init(&attr)); |
| 1606 | ASSERT_EQ(0, pthread_condattr_setclock(&attr, clock)); |
| 1607 | ASSERT_EQ(0, pthread_cond_init(&cond, &attr)); |
| 1608 | ASSERT_EQ(0, pthread_condattr_destroy(&attr)); |
| 1609 | } |
| 1610 | |
Tom Cherry | 6901080 | 2019-05-07 20:33:05 -0700 | [diff] [blame] | 1611 | void StartWaitingThread( |
| 1612 | 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] | 1613 | progress = INITIALIZED; |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1614 | this->wait_function = wait_function; |
Tom Cherry | 6901080 | 2019-05-07 20:33:05 -0700 | [diff] [blame] | 1615 | ASSERT_EQ(0, pthread_create(&thread, nullptr, reinterpret_cast<void* (*)(void*)>(WaitThreadFn), |
| 1616 | this)); |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1617 | while (progress != WAITING) { |
Yabin Cui | 32651b8 | 2015-03-13 20:30:00 -0700 | [diff] [blame] | 1618 | usleep(5000); |
| 1619 | } |
| 1620 | usleep(5000); |
| 1621 | } |
| 1622 | |
Tom Cherry | 6901080 | 2019-05-07 20:33:05 -0700 | [diff] [blame] | 1623 | void RunTimedTest( |
| 1624 | clockid_t clock, |
| 1625 | std::function<int(pthread_cond_t* cond, pthread_mutex_t* mutex, const timespec* timeout)> |
| 1626 | wait_function) { |
Tom Cherry | 6901080 | 2019-05-07 20:33:05 -0700 | [diff] [blame] | 1627 | ASSERT_EQ(0, clock_gettime(clock, &ts)); |
| 1628 | ts.tv_sec += 1; |
| 1629 | |
Peter Collingbourne | c5b8184 | 2021-12-02 12:38:46 -0800 | [diff] [blame] | 1630 | StartWaitingThread([&wait_function, this](pthread_cond_t* cond, pthread_mutex_t* mutex) { |
Tom Cherry | 6901080 | 2019-05-07 20:33:05 -0700 | [diff] [blame] | 1631 | return wait_function(cond, mutex, &ts); |
| 1632 | }); |
| 1633 | |
| 1634 | progress = SIGNALED; |
| 1635 | ASSERT_EQ(0, pthread_cond_signal(&cond)); |
| 1636 | } |
| 1637 | |
| 1638 | void RunTimedTest(clockid_t clock, std::function<int(pthread_cond_t* cond, pthread_mutex_t* mutex, |
| 1639 | clockid_t clock, const timespec* timeout)> |
| 1640 | wait_function) { |
| 1641 | RunTimedTest(clock, [clock, &wait_function](pthread_cond_t* cond, pthread_mutex_t* mutex, |
| 1642 | const timespec* timeout) { |
| 1643 | return wait_function(cond, mutex, clock, timeout); |
| 1644 | }); |
| 1645 | } |
| 1646 | |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1647 | void TearDown() override { |
| 1648 | ASSERT_EQ(0, pthread_join(thread, nullptr)); |
| 1649 | ASSERT_EQ(FINISHED, progress); |
| 1650 | ASSERT_EQ(0, pthread_cond_destroy(&cond)); |
| 1651 | ASSERT_EQ(0, pthread_mutex_destroy(&mutex)); |
| 1652 | } |
| 1653 | |
Yabin Cui | 32651b8 | 2015-03-13 20:30:00 -0700 | [diff] [blame] | 1654 | private: |
| 1655 | static void WaitThreadFn(pthread_CondWakeupTest* test) { |
| 1656 | ASSERT_EQ(0, pthread_mutex_lock(&test->mutex)); |
| 1657 | test->progress = WAITING; |
| 1658 | while (test->progress == WAITING) { |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1659 | ASSERT_EQ(0, test->wait_function(&test->cond, &test->mutex)); |
Yabin Cui | 32651b8 | 2015-03-13 20:30:00 -0700 | [diff] [blame] | 1660 | } |
| 1661 | ASSERT_EQ(SIGNALED, test->progress); |
| 1662 | test->progress = FINISHED; |
| 1663 | ASSERT_EQ(0, pthread_mutex_unlock(&test->mutex)); |
| 1664 | } |
| 1665 | }; |
| 1666 | |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1667 | TEST_F(pthread_CondWakeupTest, signal_wait) { |
| 1668 | InitCond(); |
| 1669 | StartWaitingThread([](pthread_cond_t* cond, pthread_mutex_t* mutex) { |
| 1670 | return pthread_cond_wait(cond, mutex); |
| 1671 | }); |
Yabin Cui | 32651b8 | 2015-03-13 20:30:00 -0700 | [diff] [blame] | 1672 | progress = SIGNALED; |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1673 | ASSERT_EQ(0, pthread_cond_signal(&cond)); |
Yabin Cui | 32651b8 | 2015-03-13 20:30:00 -0700 | [diff] [blame] | 1674 | } |
| 1675 | |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1676 | TEST_F(pthread_CondWakeupTest, broadcast_wait) { |
| 1677 | InitCond(); |
| 1678 | StartWaitingThread([](pthread_cond_t* cond, pthread_mutex_t* mutex) { |
| 1679 | return pthread_cond_wait(cond, mutex); |
| 1680 | }); |
Yabin Cui | 32651b8 | 2015-03-13 20:30:00 -0700 | [diff] [blame] | 1681 | progress = SIGNALED; |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1682 | ASSERT_EQ(0, pthread_cond_broadcast(&cond)); |
Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 1683 | } |
Elliott Hughes | 0e714a5 | 2014-03-03 16:42:47 -0800 | [diff] [blame] | 1684 | |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1685 | TEST_F(pthread_CondWakeupTest, signal_timedwait_CLOCK_REALTIME) { |
| 1686 | InitCond(CLOCK_REALTIME); |
Tom Cherry | 6901080 | 2019-05-07 20:33:05 -0700 | [diff] [blame] | 1687 | RunTimedTest(CLOCK_REALTIME, pthread_cond_timedwait); |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1688 | } |
Elliott Hughes | 0e714a5 | 2014-03-03 16:42:47 -0800 | [diff] [blame] | 1689 | |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1690 | TEST_F(pthread_CondWakeupTest, signal_timedwait_CLOCK_MONOTONIC) { |
| 1691 | InitCond(CLOCK_MONOTONIC); |
Tom Cherry | 6901080 | 2019-05-07 20:33:05 -0700 | [diff] [blame] | 1692 | RunTimedTest(CLOCK_MONOTONIC, pthread_cond_timedwait); |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1693 | } |
Elliott Hughes | 0e714a5 | 2014-03-03 16:42:47 -0800 | [diff] [blame] | 1694 | |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1695 | TEST_F(pthread_CondWakeupTest, signal_timedwait_CLOCK_MONOTONIC_np) { |
| 1696 | #if defined(__BIONIC__) |
| 1697 | InitCond(CLOCK_REALTIME); |
Tom Cherry | 6901080 | 2019-05-07 20:33:05 -0700 | [diff] [blame] | 1698 | RunTimedTest(CLOCK_MONOTONIC, pthread_cond_timedwait_monotonic_np); |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1699 | #else // __BIONIC__ |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 1700 | GTEST_SKIP() << "pthread_cond_timedwait_monotonic_np not available"; |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1701 | #endif // __BIONIC__ |
| 1702 | } |
| 1703 | |
Tom Cherry | 6901080 | 2019-05-07 20:33:05 -0700 | [diff] [blame] | 1704 | TEST_F(pthread_CondWakeupTest, signal_clockwait_monotonic_monotonic) { |
| 1705 | #if defined(__BIONIC__) |
| 1706 | InitCond(CLOCK_MONOTONIC); |
| 1707 | RunTimedTest(CLOCK_MONOTONIC, pthread_cond_clockwait); |
| 1708 | #else // __BIONIC__ |
| 1709 | GTEST_SKIP() << "pthread_cond_clockwait not available"; |
| 1710 | #endif // __BIONIC__ |
| 1711 | } |
| 1712 | |
| 1713 | TEST_F(pthread_CondWakeupTest, signal_clockwait_monotonic_realtime) { |
| 1714 | #if defined(__BIONIC__) |
| 1715 | InitCond(CLOCK_MONOTONIC); |
| 1716 | RunTimedTest(CLOCK_REALTIME, pthread_cond_clockwait); |
| 1717 | #else // __BIONIC__ |
| 1718 | GTEST_SKIP() << "pthread_cond_clockwait not available"; |
| 1719 | #endif // __BIONIC__ |
| 1720 | } |
| 1721 | |
| 1722 | TEST_F(pthread_CondWakeupTest, signal_clockwait_realtime_monotonic) { |
| 1723 | #if defined(__BIONIC__) |
| 1724 | InitCond(CLOCK_REALTIME); |
| 1725 | RunTimedTest(CLOCK_MONOTONIC, pthread_cond_clockwait); |
| 1726 | #else // __BIONIC__ |
| 1727 | GTEST_SKIP() << "pthread_cond_clockwait not available"; |
| 1728 | #endif // __BIONIC__ |
| 1729 | } |
| 1730 | |
| 1731 | TEST_F(pthread_CondWakeupTest, signal_clockwait_realtime_realtime) { |
| 1732 | #if defined(__BIONIC__) |
| 1733 | InitCond(CLOCK_REALTIME); |
| 1734 | RunTimedTest(CLOCK_REALTIME, pthread_cond_clockwait); |
| 1735 | #else // __BIONIC__ |
| 1736 | GTEST_SKIP() << "pthread_cond_clockwait not available"; |
| 1737 | #endif // __BIONIC__ |
| 1738 | } |
| 1739 | |
Tom Cherry | 800c1a9 | 2019-07-17 10:45:18 -0700 | [diff] [blame] | 1740 | static void pthread_cond_timedwait_timeout_helper(bool init_monotonic, clockid_t clock, |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1741 | int (*wait_function)(pthread_cond_t* __cond, |
| 1742 | pthread_mutex_t* __mutex, |
| 1743 | const timespec* __timeout)) { |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1744 | pthread_mutex_t mutex; |
| 1745 | ASSERT_EQ(0, pthread_mutex_init(&mutex, nullptr)); |
| 1746 | pthread_cond_t cond; |
Tom Cherry | 800c1a9 | 2019-07-17 10:45:18 -0700 | [diff] [blame] | 1747 | |
| 1748 | if (init_monotonic) { |
| 1749 | pthread_condattr_t attr; |
| 1750 | pthread_condattr_init(&attr); |
| 1751 | |
| 1752 | ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC)); |
| 1753 | clockid_t clock; |
| 1754 | ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock)); |
| 1755 | ASSERT_EQ(CLOCK_MONOTONIC, clock); |
| 1756 | |
| 1757 | ASSERT_EQ(0, pthread_cond_init(&cond, &attr)); |
| 1758 | } else { |
| 1759 | ASSERT_EQ(0, pthread_cond_init(&cond, nullptr)); |
| 1760 | } |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1761 | ASSERT_EQ(0, pthread_mutex_lock(&mutex)); |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1762 | |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1763 | timespec ts; |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1764 | ASSERT_EQ(0, clock_gettime(clock, &ts)); |
| 1765 | ASSERT_EQ(ETIMEDOUT, wait_function(&cond, &mutex, &ts)); |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1766 | ts.tv_nsec = -1; |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1767 | ASSERT_EQ(EINVAL, wait_function(&cond, &mutex, &ts)); |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1768 | ts.tv_nsec = NS_PER_S; |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1769 | ASSERT_EQ(EINVAL, wait_function(&cond, &mutex, &ts)); |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1770 | ts.tv_nsec = NS_PER_S - 1; |
| 1771 | ts.tv_sec = -1; |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1772 | ASSERT_EQ(ETIMEDOUT, wait_function(&cond, &mutex, &ts)); |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 1773 | ASSERT_EQ(0, pthread_mutex_unlock(&mutex)); |
Elliott Hughes | 0e714a5 | 2014-03-03 16:42:47 -0800 | [diff] [blame] | 1774 | } |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 1775 | |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1776 | TEST(pthread, pthread_cond_timedwait_timeout) { |
Tom Cherry | 800c1a9 | 2019-07-17 10:45:18 -0700 | [diff] [blame] | 1777 | pthread_cond_timedwait_timeout_helper(false, CLOCK_REALTIME, pthread_cond_timedwait); |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1778 | } |
| 1779 | |
| 1780 | TEST(pthread, pthread_cond_timedwait_monotonic_np_timeout) { |
| 1781 | #if defined(__BIONIC__) |
Tom Cherry | 800c1a9 | 2019-07-17 10:45:18 -0700 | [diff] [blame] | 1782 | pthread_cond_timedwait_timeout_helper(false, CLOCK_MONOTONIC, pthread_cond_timedwait_monotonic_np); |
| 1783 | pthread_cond_timedwait_timeout_helper(true, CLOCK_MONOTONIC, pthread_cond_timedwait_monotonic_np); |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1784 | #else // __BIONIC__ |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 1785 | GTEST_SKIP() << "pthread_cond_timedwait_monotonic_np not available"; |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 1786 | #endif // __BIONIC__ |
| 1787 | } |
| 1788 | |
Tom Cherry | 6901080 | 2019-05-07 20:33:05 -0700 | [diff] [blame] | 1789 | TEST(pthread, pthread_cond_clockwait_timeout) { |
| 1790 | #if defined(__BIONIC__) |
| 1791 | pthread_cond_timedwait_timeout_helper( |
Tom Cherry | 800c1a9 | 2019-07-17 10:45:18 -0700 | [diff] [blame] | 1792 | false, CLOCK_MONOTONIC, |
Tom Cherry | 6901080 | 2019-05-07 20:33:05 -0700 | [diff] [blame] | 1793 | [](pthread_cond_t* __cond, pthread_mutex_t* __mutex, const timespec* __timeout) { |
| 1794 | return pthread_cond_clockwait(__cond, __mutex, CLOCK_MONOTONIC, __timeout); |
| 1795 | }); |
| 1796 | pthread_cond_timedwait_timeout_helper( |
Tom Cherry | 800c1a9 | 2019-07-17 10:45:18 -0700 | [diff] [blame] | 1797 | true, CLOCK_MONOTONIC, |
| 1798 | [](pthread_cond_t* __cond, pthread_mutex_t* __mutex, const timespec* __timeout) { |
| 1799 | return pthread_cond_clockwait(__cond, __mutex, CLOCK_MONOTONIC, __timeout); |
| 1800 | }); |
| 1801 | pthread_cond_timedwait_timeout_helper( |
| 1802 | false, CLOCK_REALTIME, |
| 1803 | [](pthread_cond_t* __cond, pthread_mutex_t* __mutex, const timespec* __timeout) { |
| 1804 | return pthread_cond_clockwait(__cond, __mutex, CLOCK_REALTIME, __timeout); |
| 1805 | }); |
| 1806 | pthread_cond_timedwait_timeout_helper( |
| 1807 | true, CLOCK_REALTIME, |
Tom Cherry | 6901080 | 2019-05-07 20:33:05 -0700 | [diff] [blame] | 1808 | [](pthread_cond_t* __cond, pthread_mutex_t* __mutex, const timespec* __timeout) { |
| 1809 | return pthread_cond_clockwait(__cond, __mutex, CLOCK_REALTIME, __timeout); |
| 1810 | }); |
| 1811 | #else // __BIONIC__ |
| 1812 | GTEST_SKIP() << "pthread_cond_clockwait not available"; |
| 1813 | #endif // __BIONIC__ |
| 1814 | } |
| 1815 | |
| 1816 | TEST(pthread, pthread_cond_clockwait_invalid) { |
| 1817 | #if defined(__BIONIC__) |
| 1818 | pthread_cond_t cond = PTHREAD_COND_INITIALIZER; |
| 1819 | pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; |
| 1820 | timespec ts; |
| 1821 | EXPECT_EQ(EINVAL, pthread_cond_clockwait(&cond, &mutex, CLOCK_PROCESS_CPUTIME_ID, &ts)); |
| 1822 | |
| 1823 | #else // __BIONIC__ |
| 1824 | GTEST_SKIP() << "pthread_cond_clockwait not available"; |
| 1825 | #endif // __BIONIC__ |
| 1826 | } |
| 1827 | |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 1828 | TEST(pthread, pthread_attr_getstack__main_thread) { |
| 1829 | // This test is only meaningful for the main thread, so make sure we're running on it! |
| 1830 | ASSERT_EQ(getpid(), syscall(__NR_gettid)); |
| 1831 | |
| 1832 | // Get the main thread's attributes. |
| 1833 | pthread_attr_t attributes; |
| 1834 | ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes)); |
| 1835 | |
| 1836 | // Check that we correctly report that the main thread has no guard page. |
| 1837 | size_t guard_size; |
| 1838 | ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size)); |
| 1839 | ASSERT_EQ(0U, guard_size); // The main thread has no guard page. |
| 1840 | |
| 1841 | // Get the stack base and the stack size (both ways). |
| 1842 | void* stack_base; |
| 1843 | size_t stack_size; |
| 1844 | ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size)); |
| 1845 | size_t stack_size2; |
| 1846 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2)); |
| 1847 | |
| 1848 | // The two methods of asking for the stack size should agree. |
| 1849 | EXPECT_EQ(stack_size, stack_size2); |
| 1850 | |
Yabin Cui | b0c6f2db | 2015-05-19 15:09:23 -0700 | [diff] [blame] | 1851 | #if defined(__BIONIC__) |
dimitry | 6dfa5b5 | 2018-01-30 13:24:28 +0100 | [diff] [blame] | 1852 | // Find stack in /proc/self/maps using a pointer to the stack. |
| 1853 | // |
| 1854 | // We do not use "[stack]" label because in native-bridge environment it is not |
| 1855 | // guaranteed to point to the right stack. A native bridge implementation may |
| 1856 | // keep separate stack for the guest code. |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 1857 | void* maps_stack_hi = nullptr; |
Elliott Hughes | 15dfd63 | 2015-09-22 16:40:14 -0700 | [diff] [blame] | 1858 | std::vector<map_record> maps; |
| 1859 | ASSERT_TRUE(Maps::parse_maps(&maps)); |
Evgenii Stepanov | 7cc6706 | 2019-02-05 18:43:34 -0800 | [diff] [blame] | 1860 | uintptr_t stack_address = reinterpret_cast<uintptr_t>(untag_address(&maps_stack_hi)); |
Elliott Hughes | 0b2acdf | 2015-10-02 18:25:19 -0700 | [diff] [blame] | 1861 | for (const auto& map : maps) { |
dimitry | 6dfa5b5 | 2018-01-30 13:24:28 +0100 | [diff] [blame] | 1862 | if (map.addr_start <= stack_address && map.addr_end > stack_address){ |
Elliott Hughes | 15dfd63 | 2015-09-22 16:40:14 -0700 | [diff] [blame] | 1863 | maps_stack_hi = reinterpret_cast<void*>(map.addr_end); |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 1864 | break; |
| 1865 | } |
| 1866 | } |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 1867 | |
dimitry | 6dfa5b5 | 2018-01-30 13:24:28 +0100 | [diff] [blame] | 1868 | // The high address of the /proc/self/maps stack region should equal stack_base + stack_size. |
Yabin Cui | b0c6f2db | 2015-05-19 15:09:23 -0700 | [diff] [blame] | 1869 | // Remember that the stack grows down (and is mapped in on demand), so the low address of the |
| 1870 | // region isn't very interesting. |
| 1871 | EXPECT_EQ(maps_stack_hi, reinterpret_cast<uint8_t*>(stack_base) + stack_size); |
| 1872 | |
Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 1873 | // The stack size should correspond to RLIMIT_STACK. |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 1874 | rlimit rl; |
Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 1875 | ASSERT_EQ(0, getrlimit(RLIMIT_STACK, &rl)); |
Elliott Hughes | 27a9aed | 2014-09-04 16:09:25 -0700 | [diff] [blame] | 1876 | uint64_t original_rlim_cur = rl.rlim_cur; |
Elliott Hughes | 27a9aed | 2014-09-04 16:09:25 -0700 | [diff] [blame] | 1877 | if (rl.rlim_cur == RLIM_INFINITY) { |
| 1878 | rl.rlim_cur = 8 * 1024 * 1024; // Bionic reports unlimited stacks as 8MiB. |
| 1879 | } |
Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 1880 | EXPECT_EQ(rl.rlim_cur, stack_size); |
| 1881 | |
Tom Cherry | b8ab618 | 2017-04-05 16:20:29 -0700 | [diff] [blame] | 1882 | auto guard = android::base::make_scope_guard([&rl, original_rlim_cur]() { |
Elliott Hughes | 27a9aed | 2014-09-04 16:09:25 -0700 | [diff] [blame] | 1883 | rl.rlim_cur = original_rlim_cur; |
| 1884 | ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl)); |
| 1885 | }); |
| 1886 | |
Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 1887 | // |
| 1888 | // What if RLIMIT_STACK is smaller than the stack's current extent? |
| 1889 | // |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 1890 | rl.rlim_cur = rl.rlim_max = 1024; // 1KiB. We know the stack must be at least a page already. |
| 1891 | rl.rlim_max = RLIM_INFINITY; |
| 1892 | ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl)); |
| 1893 | |
| 1894 | ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes)); |
| 1895 | ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size)); |
| 1896 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2)); |
| 1897 | |
| 1898 | EXPECT_EQ(stack_size, stack_size2); |
| 1899 | ASSERT_EQ(1024U, stack_size); |
| 1900 | |
| 1901 | // |
Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 1902 | // What if RLIMIT_STACK isn't a whole number of pages? |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 1903 | // |
| 1904 | rl.rlim_cur = rl.rlim_max = 6666; // Not a whole number of pages. |
| 1905 | rl.rlim_max = RLIM_INFINITY; |
| 1906 | ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl)); |
| 1907 | |
| 1908 | ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes)); |
| 1909 | ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size)); |
| 1910 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2)); |
| 1911 | |
| 1912 | EXPECT_EQ(stack_size, stack_size2); |
| 1913 | ASSERT_EQ(6666U, stack_size); |
Yabin Cui | b0c6f2db | 2015-05-19 15:09:23 -0700 | [diff] [blame] | 1914 | #endif |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 1915 | } |
Elliott Hughes | 8fb639c | 2014-09-12 14:43:07 -0700 | [diff] [blame] | 1916 | |
Mor-sarid, Nitzan | 5693332 | 2015-09-11 05:31:36 +0000 | [diff] [blame] | 1917 | struct GetStackSignalHandlerArg { |
| 1918 | volatile bool done; |
Chih-Hung Hsieh | 9af13d2 | 2016-06-02 14:40:09 -0700 | [diff] [blame] | 1919 | void* signal_stack_base; |
| 1920 | size_t signal_stack_size; |
Mor-sarid, Nitzan | 5693332 | 2015-09-11 05:31:36 +0000 | [diff] [blame] | 1921 | void* main_stack_base; |
| 1922 | size_t main_stack_size; |
| 1923 | }; |
| 1924 | |
| 1925 | static GetStackSignalHandlerArg getstack_signal_handler_arg; |
| 1926 | |
| 1927 | static void getstack_signal_handler(int sig) { |
| 1928 | ASSERT_EQ(SIGUSR1, sig); |
| 1929 | // Use sleep() to make current thread be switched out by the kernel to provoke the error. |
| 1930 | sleep(1); |
| 1931 | pthread_attr_t attr; |
| 1932 | ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attr)); |
| 1933 | void* stack_base; |
| 1934 | size_t stack_size; |
| 1935 | ASSERT_EQ(0, pthread_attr_getstack(&attr, &stack_base, &stack_size)); |
Chih-Hung Hsieh | 9af13d2 | 2016-06-02 14:40:09 -0700 | [diff] [blame] | 1936 | |
| 1937 | // Verify if the stack used by the signal handler is the alternate stack just registered. |
| 1938 | ASSERT_LE(getstack_signal_handler_arg.signal_stack_base, &attr); |
Evgenii Stepanov | 7cc6706 | 2019-02-05 18:43:34 -0800 | [diff] [blame] | 1939 | ASSERT_LT(static_cast<void*>(untag_address(&attr)), |
Chih-Hung Hsieh | 9af13d2 | 2016-06-02 14:40:09 -0700 | [diff] [blame] | 1940 | static_cast<char*>(getstack_signal_handler_arg.signal_stack_base) + |
Evgenii Stepanov | 7cc6706 | 2019-02-05 18:43:34 -0800 | [diff] [blame] | 1941 | getstack_signal_handler_arg.signal_stack_size); |
Chih-Hung Hsieh | 9af13d2 | 2016-06-02 14:40:09 -0700 | [diff] [blame] | 1942 | |
| 1943 | // Verify if the main thread's stack got in the signal handler is correct. |
| 1944 | ASSERT_EQ(getstack_signal_handler_arg.main_stack_base, stack_base); |
| 1945 | ASSERT_LE(getstack_signal_handler_arg.main_stack_size, stack_size); |
| 1946 | |
Mor-sarid, Nitzan | 5693332 | 2015-09-11 05:31:36 +0000 | [diff] [blame] | 1947 | getstack_signal_handler_arg.done = true; |
| 1948 | } |
| 1949 | |
| 1950 | // The previous code obtained the main thread's stack by reading the entry in |
| 1951 | // /proc/self/task/<pid>/maps that was labeled [stack]. Unfortunately, on x86/x86_64, the kernel |
| 1952 | // relies on sp0 in task state segment(tss) to label the stack map with [stack]. If the kernel |
| 1953 | // switches a process while the main thread is in an alternate stack, then the kernel will label |
| 1954 | // the wrong map with [stack]. This test verifies that when the above situation happens, the main |
| 1955 | // thread's stack is found correctly. |
| 1956 | TEST(pthread, pthread_attr_getstack_in_signal_handler) { |
Yabin Cui | 61e4d46 | 2016-03-07 17:44:58 -0800 | [diff] [blame] | 1957 | // This test is only meaningful for the main thread, so make sure we're running on it! |
| 1958 | ASSERT_EQ(getpid(), syscall(__NR_gettid)); |
| 1959 | |
Mor-sarid, Nitzan | 5693332 | 2015-09-11 05:31:36 +0000 | [diff] [blame] | 1960 | const size_t sig_stack_size = 16 * 1024; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 1961 | void* sig_stack = mmap(nullptr, sig_stack_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, |
Mor-sarid, Nitzan | 5693332 | 2015-09-11 05:31:36 +0000 | [diff] [blame] | 1962 | -1, 0); |
| 1963 | ASSERT_NE(MAP_FAILED, sig_stack); |
| 1964 | stack_t ss; |
| 1965 | ss.ss_sp = sig_stack; |
| 1966 | ss.ss_size = sig_stack_size; |
| 1967 | ss.ss_flags = 0; |
| 1968 | stack_t oss; |
| 1969 | ASSERT_EQ(0, sigaltstack(&ss, &oss)); |
| 1970 | |
Yabin Cui | 61e4d46 | 2016-03-07 17:44:58 -0800 | [diff] [blame] | 1971 | pthread_attr_t attr; |
| 1972 | ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attr)); |
| 1973 | void* main_stack_base; |
| 1974 | size_t main_stack_size; |
| 1975 | ASSERT_EQ(0, pthread_attr_getstack(&attr, &main_stack_base, &main_stack_size)); |
| 1976 | |
Mor-sarid, Nitzan | 5693332 | 2015-09-11 05:31:36 +0000 | [diff] [blame] | 1977 | ScopedSignalHandler handler(SIGUSR1, getstack_signal_handler, SA_ONSTACK); |
| 1978 | getstack_signal_handler_arg.done = false; |
Chih-Hung Hsieh | 9af13d2 | 2016-06-02 14:40:09 -0700 | [diff] [blame] | 1979 | getstack_signal_handler_arg.signal_stack_base = sig_stack; |
| 1980 | getstack_signal_handler_arg.signal_stack_size = sig_stack_size; |
| 1981 | getstack_signal_handler_arg.main_stack_base = main_stack_base; |
| 1982 | getstack_signal_handler_arg.main_stack_size = main_stack_size; |
Mor-sarid, Nitzan | 5693332 | 2015-09-11 05:31:36 +0000 | [diff] [blame] | 1983 | kill(getpid(), SIGUSR1); |
| 1984 | ASSERT_EQ(true, getstack_signal_handler_arg.done); |
| 1985 | |
Mor-sarid, Nitzan | 5693332 | 2015-09-11 05:31:36 +0000 | [diff] [blame] | 1986 | ASSERT_EQ(0, sigaltstack(&oss, nullptr)); |
| 1987 | ASSERT_EQ(0, munmap(sig_stack, sig_stack_size)); |
| 1988 | } |
| 1989 | |
Yabin Cui | 917d390 | 2015-01-08 12:32:42 -0800 | [diff] [blame] | 1990 | static void pthread_attr_getstack_18908062_helper(void*) { |
| 1991 | char local_variable; |
| 1992 | pthread_attr_t attributes; |
| 1993 | pthread_getattr_np(pthread_self(), &attributes); |
| 1994 | void* stack_base; |
| 1995 | size_t stack_size; |
| 1996 | pthread_attr_getstack(&attributes, &stack_base, &stack_size); |
| 1997 | |
| 1998 | // Test whether &local_variable is in [stack_base, stack_base + stack_size). |
| 1999 | ASSERT_LE(reinterpret_cast<char*>(stack_base), &local_variable); |
Evgenii Stepanov | 7cc6706 | 2019-02-05 18:43:34 -0800 | [diff] [blame] | 2000 | ASSERT_LT(untag_address(&local_variable), reinterpret_cast<char*>(stack_base) + stack_size); |
Yabin Cui | 917d390 | 2015-01-08 12:32:42 -0800 | [diff] [blame] | 2001 | } |
| 2002 | |
| 2003 | // Check whether something on stack is in the range of |
| 2004 | // [stack_base, stack_base + stack_size). see b/18908062. |
| 2005 | TEST(pthread, pthread_attr_getstack_18908062) { |
| 2006 | pthread_t t; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 2007 | ASSERT_EQ(0, pthread_create(&t, nullptr, |
Yabin Cui | 917d390 | 2015-01-08 12:32:42 -0800 | [diff] [blame] | 2008 | reinterpret_cast<void* (*)(void*)>(pthread_attr_getstack_18908062_helper), |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 2009 | nullptr)); |
| 2010 | ASSERT_EQ(0, pthread_join(t, nullptr)); |
Yabin Cui | 917d390 | 2015-01-08 12:32:42 -0800 | [diff] [blame] | 2011 | } |
| 2012 | |
Elliott Hughes | 8fb639c | 2014-09-12 14:43:07 -0700 | [diff] [blame] | 2013 | #if defined(__BIONIC__) |
Elliott Hughes | f208361 | 2015-11-11 13:32:28 -0800 | [diff] [blame] | 2014 | static pthread_mutex_t pthread_gettid_np_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 2015 | |
Elliott Hughes | 8fb639c | 2014-09-12 14:43:07 -0700 | [diff] [blame] | 2016 | static void* pthread_gettid_np_helper(void* arg) { |
| 2017 | *reinterpret_cast<pid_t*>(arg) = gettid(); |
Elliott Hughes | f208361 | 2015-11-11 13:32:28 -0800 | [diff] [blame] | 2018 | |
| 2019 | // Wait for our parent to call pthread_gettid_np on us before exiting. |
| 2020 | pthread_mutex_lock(&pthread_gettid_np_mutex); |
| 2021 | pthread_mutex_unlock(&pthread_gettid_np_mutex); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 2022 | return nullptr; |
Elliott Hughes | 8fb639c | 2014-09-12 14:43:07 -0700 | [diff] [blame] | 2023 | } |
| 2024 | #endif |
| 2025 | |
| 2026 | TEST(pthread, pthread_gettid_np) { |
| 2027 | #if defined(__BIONIC__) |
| 2028 | ASSERT_EQ(gettid(), pthread_gettid_np(pthread_self())); |
| 2029 | |
Elliott Hughes | f208361 | 2015-11-11 13:32:28 -0800 | [diff] [blame] | 2030 | // Ensure the other thread doesn't exit until after we've called |
| 2031 | // pthread_gettid_np on it. |
| 2032 | pthread_mutex_lock(&pthread_gettid_np_mutex); |
| 2033 | |
Elliott Hughes | 8fb639c | 2014-09-12 14:43:07 -0700 | [diff] [blame] | 2034 | pid_t t_gettid_result; |
| 2035 | pthread_t t; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 2036 | pthread_create(&t, nullptr, pthread_gettid_np_helper, &t_gettid_result); |
Elliott Hughes | 8fb639c | 2014-09-12 14:43:07 -0700 | [diff] [blame] | 2037 | |
| 2038 | pid_t t_pthread_gettid_np_result = pthread_gettid_np(t); |
| 2039 | |
Elliott Hughes | f208361 | 2015-11-11 13:32:28 -0800 | [diff] [blame] | 2040 | // Release the other thread and wait for it to exit. |
| 2041 | pthread_mutex_unlock(&pthread_gettid_np_mutex); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 2042 | ASSERT_EQ(0, pthread_join(t, nullptr)); |
Elliott Hughes | 8fb639c | 2014-09-12 14:43:07 -0700 | [diff] [blame] | 2043 | |
| 2044 | ASSERT_EQ(t_gettid_result, t_pthread_gettid_np_result); |
| 2045 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 2046 | GTEST_SKIP() << "pthread_gettid_np not available"; |
Elliott Hughes | 8fb639c | 2014-09-12 14:43:07 -0700 | [diff] [blame] | 2047 | #endif |
| 2048 | } |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 2049 | |
| 2050 | static size_t cleanup_counter = 0; |
| 2051 | |
Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 2052 | static void AbortCleanupRoutine(void*) { |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 2053 | abort(); |
| 2054 | } |
| 2055 | |
Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 2056 | static void CountCleanupRoutine(void*) { |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 2057 | ++cleanup_counter; |
| 2058 | } |
| 2059 | |
Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 2060 | static void PthreadCleanupTester() { |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 2061 | pthread_cleanup_push(CountCleanupRoutine, nullptr); |
| 2062 | pthread_cleanup_push(CountCleanupRoutine, nullptr); |
| 2063 | pthread_cleanup_push(AbortCleanupRoutine, nullptr); |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 2064 | |
| 2065 | pthread_cleanup_pop(0); // Pop the abort without executing it. |
| 2066 | pthread_cleanup_pop(1); // Pop one count while executing it. |
| 2067 | ASSERT_EQ(1U, cleanup_counter); |
| 2068 | // Exit while the other count is still on the cleanup stack. |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 2069 | pthread_exit(nullptr); |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 2070 | |
| 2071 | // Calls to pthread_cleanup_pop/pthread_cleanup_push must always be balanced. |
| 2072 | pthread_cleanup_pop(0); |
| 2073 | } |
| 2074 | |
Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 2075 | static void* PthreadCleanupStartRoutine(void*) { |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 2076 | PthreadCleanupTester(); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 2077 | return nullptr; |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 2078 | } |
| 2079 | |
| 2080 | TEST(pthread, pthread_cleanup_push__pthread_cleanup_pop) { |
| 2081 | pthread_t t; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 2082 | ASSERT_EQ(0, pthread_create(&t, nullptr, PthreadCleanupStartRoutine, nullptr)); |
| 2083 | ASSERT_EQ(0, pthread_join(t, nullptr)); |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 2084 | ASSERT_EQ(2U, cleanup_counter); |
| 2085 | } |
Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 2086 | |
| 2087 | TEST(pthread, PTHREAD_MUTEX_DEFAULT_is_PTHREAD_MUTEX_NORMAL) { |
| 2088 | ASSERT_EQ(PTHREAD_MUTEX_NORMAL, PTHREAD_MUTEX_DEFAULT); |
| 2089 | } |
| 2090 | |
| 2091 | TEST(pthread, pthread_mutexattr_gettype) { |
| 2092 | pthread_mutexattr_t attr; |
| 2093 | ASSERT_EQ(0, pthread_mutexattr_init(&attr)); |
| 2094 | |
| 2095 | int attr_type; |
| 2096 | |
| 2097 | ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL)); |
| 2098 | ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type)); |
| 2099 | ASSERT_EQ(PTHREAD_MUTEX_NORMAL, attr_type); |
| 2100 | |
| 2101 | ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK)); |
| 2102 | ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type)); |
| 2103 | ASSERT_EQ(PTHREAD_MUTEX_ERRORCHECK, attr_type); |
| 2104 | |
| 2105 | ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE)); |
| 2106 | ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type)); |
| 2107 | ASSERT_EQ(PTHREAD_MUTEX_RECURSIVE, attr_type); |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 2108 | |
| 2109 | ASSERT_EQ(0, pthread_mutexattr_destroy(&attr)); |
| 2110 | } |
| 2111 | |
Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 2112 | TEST(pthread, pthread_mutexattr_protocol) { |
| 2113 | pthread_mutexattr_t attr; |
| 2114 | ASSERT_EQ(0, pthread_mutexattr_init(&attr)); |
| 2115 | |
| 2116 | int protocol; |
| 2117 | ASSERT_EQ(0, pthread_mutexattr_getprotocol(&attr, &protocol)); |
| 2118 | ASSERT_EQ(PTHREAD_PRIO_NONE, protocol); |
| 2119 | for (size_t repeat = 0; repeat < 2; ++repeat) { |
| 2120 | for (int set_protocol : {PTHREAD_PRIO_NONE, PTHREAD_PRIO_INHERIT}) { |
| 2121 | ASSERT_EQ(0, pthread_mutexattr_setprotocol(&attr, set_protocol)); |
| 2122 | ASSERT_EQ(0, pthread_mutexattr_getprotocol(&attr, &protocol)); |
| 2123 | ASSERT_EQ(protocol, set_protocol); |
| 2124 | } |
| 2125 | } |
| 2126 | } |
| 2127 | |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 2128 | struct PthreadMutex { |
| 2129 | pthread_mutex_t lock; |
| 2130 | |
Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 2131 | explicit PthreadMutex(int mutex_type, int protocol = PTHREAD_PRIO_NONE) { |
| 2132 | init(mutex_type, protocol); |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 2133 | } |
| 2134 | |
| 2135 | ~PthreadMutex() { |
| 2136 | destroy(); |
| 2137 | } |
| 2138 | |
| 2139 | private: |
Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 2140 | void init(int mutex_type, int protocol) { |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 2141 | pthread_mutexattr_t attr; |
| 2142 | ASSERT_EQ(0, pthread_mutexattr_init(&attr)); |
| 2143 | ASSERT_EQ(0, pthread_mutexattr_settype(&attr, mutex_type)); |
Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 2144 | ASSERT_EQ(0, pthread_mutexattr_setprotocol(&attr, protocol)); |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 2145 | ASSERT_EQ(0, pthread_mutex_init(&lock, &attr)); |
| 2146 | ASSERT_EQ(0, pthread_mutexattr_destroy(&attr)); |
| 2147 | } |
| 2148 | |
| 2149 | void destroy() { |
| 2150 | ASSERT_EQ(0, pthread_mutex_destroy(&lock)); |
| 2151 | } |
| 2152 | |
| 2153 | DISALLOW_COPY_AND_ASSIGN(PthreadMutex); |
| 2154 | }; |
Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 2155 | |
Ryan Prichard | 4b6c0f5 | 2019-04-18 22:47:04 -0700 | [diff] [blame] | 2156 | static int UnlockFromAnotherThread(pthread_mutex_t* mutex) { |
| 2157 | pthread_t thread; |
| 2158 | pthread_create(&thread, nullptr, [](void* mutex_voidp) -> void* { |
| 2159 | pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(mutex_voidp); |
| 2160 | intptr_t result = pthread_mutex_unlock(mutex); |
| 2161 | return reinterpret_cast<void*>(result); |
| 2162 | }, mutex); |
| 2163 | void* result; |
| 2164 | EXPECT_EQ(0, pthread_join(thread, &result)); |
| 2165 | return reinterpret_cast<intptr_t>(result); |
| 2166 | }; |
| 2167 | |
Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 2168 | static void TestPthreadMutexLockNormal(int protocol) { |
| 2169 | PthreadMutex m(PTHREAD_MUTEX_NORMAL, protocol); |
Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 2170 | |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 2171 | ASSERT_EQ(0, pthread_mutex_lock(&m.lock)); |
Ryan Prichard | 4b6c0f5 | 2019-04-18 22:47:04 -0700 | [diff] [blame] | 2172 | if (protocol == PTHREAD_PRIO_INHERIT) { |
| 2173 | ASSERT_EQ(EPERM, UnlockFromAnotherThread(&m.lock)); |
| 2174 | } |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 2175 | ASSERT_EQ(0, pthread_mutex_unlock(&m.lock)); |
Elliott Hughes | d31d4c1 | 2015-12-14 17:35:10 -0800 | [diff] [blame] | 2176 | ASSERT_EQ(0, pthread_mutex_trylock(&m.lock)); |
| 2177 | ASSERT_EQ(EBUSY, pthread_mutex_trylock(&m.lock)); |
| 2178 | ASSERT_EQ(0, pthread_mutex_unlock(&m.lock)); |
Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 2179 | } |
| 2180 | |
Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 2181 | static void TestPthreadMutexLockErrorCheck(int protocol) { |
| 2182 | PthreadMutex m(PTHREAD_MUTEX_ERRORCHECK, protocol); |
Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 2183 | |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 2184 | ASSERT_EQ(0, pthread_mutex_lock(&m.lock)); |
Ryan Prichard | 4b6c0f5 | 2019-04-18 22:47:04 -0700 | [diff] [blame] | 2185 | ASSERT_EQ(EPERM, UnlockFromAnotherThread(&m.lock)); |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 2186 | ASSERT_EQ(EDEADLK, pthread_mutex_lock(&m.lock)); |
| 2187 | ASSERT_EQ(0, pthread_mutex_unlock(&m.lock)); |
| 2188 | ASSERT_EQ(0, pthread_mutex_trylock(&m.lock)); |
Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 2189 | if (protocol == PTHREAD_PRIO_NONE) { |
| 2190 | ASSERT_EQ(EBUSY, pthread_mutex_trylock(&m.lock)); |
| 2191 | } else { |
| 2192 | ASSERT_EQ(EDEADLK, pthread_mutex_trylock(&m.lock)); |
| 2193 | } |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 2194 | ASSERT_EQ(0, pthread_mutex_unlock(&m.lock)); |
| 2195 | ASSERT_EQ(EPERM, pthread_mutex_unlock(&m.lock)); |
Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 2196 | } |
| 2197 | |
Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 2198 | static void TestPthreadMutexLockRecursive(int protocol) { |
| 2199 | PthreadMutex m(PTHREAD_MUTEX_RECURSIVE, protocol); |
Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 2200 | |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 2201 | ASSERT_EQ(0, pthread_mutex_lock(&m.lock)); |
Ryan Prichard | 4b6c0f5 | 2019-04-18 22:47:04 -0700 | [diff] [blame] | 2202 | ASSERT_EQ(EPERM, UnlockFromAnotherThread(&m.lock)); |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 2203 | ASSERT_EQ(0, pthread_mutex_lock(&m.lock)); |
Ryan Prichard | 4b6c0f5 | 2019-04-18 22:47:04 -0700 | [diff] [blame] | 2204 | ASSERT_EQ(EPERM, UnlockFromAnotherThread(&m.lock)); |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 2205 | ASSERT_EQ(0, pthread_mutex_unlock(&m.lock)); |
| 2206 | ASSERT_EQ(0, pthread_mutex_unlock(&m.lock)); |
| 2207 | ASSERT_EQ(0, pthread_mutex_trylock(&m.lock)); |
Elliott Hughes | d31d4c1 | 2015-12-14 17:35:10 -0800 | [diff] [blame] | 2208 | ASSERT_EQ(0, pthread_mutex_trylock(&m.lock)); |
| 2209 | ASSERT_EQ(0, pthread_mutex_unlock(&m.lock)); |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 2210 | ASSERT_EQ(0, pthread_mutex_unlock(&m.lock)); |
| 2211 | ASSERT_EQ(EPERM, pthread_mutex_unlock(&m.lock)); |
| 2212 | } |
| 2213 | |
Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 2214 | TEST(pthread, pthread_mutex_lock_NORMAL) { |
| 2215 | TestPthreadMutexLockNormal(PTHREAD_PRIO_NONE); |
| 2216 | } |
| 2217 | |
| 2218 | TEST(pthread, pthread_mutex_lock_ERRORCHECK) { |
| 2219 | TestPthreadMutexLockErrorCheck(PTHREAD_PRIO_NONE); |
| 2220 | } |
| 2221 | |
| 2222 | TEST(pthread, pthread_mutex_lock_RECURSIVE) { |
| 2223 | TestPthreadMutexLockRecursive(PTHREAD_PRIO_NONE); |
| 2224 | } |
| 2225 | |
| 2226 | TEST(pthread, pthread_mutex_lock_pi) { |
Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 2227 | TestPthreadMutexLockNormal(PTHREAD_PRIO_INHERIT); |
| 2228 | TestPthreadMutexLockErrorCheck(PTHREAD_PRIO_INHERIT); |
| 2229 | TestPthreadMutexLockRecursive(PTHREAD_PRIO_INHERIT); |
| 2230 | } |
| 2231 | |
Yabin Cui | 5a00ba7 | 2018-01-26 17:32:31 -0800 | [diff] [blame] | 2232 | TEST(pthread, pthread_mutex_pi_count_limit) { |
| 2233 | #if defined(__BIONIC__) && !defined(__LP64__) |
| 2234 | // Bionic only supports 65536 pi mutexes in 32-bit programs. |
| 2235 | pthread_mutexattr_t attr; |
| 2236 | ASSERT_EQ(0, pthread_mutexattr_init(&attr)); |
| 2237 | ASSERT_EQ(0, pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT)); |
| 2238 | std::vector<pthread_mutex_t> mutexes(65536); |
| 2239 | // Test if we can use 65536 pi mutexes at the same time. |
| 2240 | // Run 2 times to check if freed pi mutexes can be recycled. |
| 2241 | for (int repeat = 0; repeat < 2; ++repeat) { |
| 2242 | for (auto& m : mutexes) { |
| 2243 | ASSERT_EQ(0, pthread_mutex_init(&m, &attr)); |
| 2244 | } |
| 2245 | pthread_mutex_t m; |
| 2246 | ASSERT_EQ(ENOMEM, pthread_mutex_init(&m, &attr)); |
| 2247 | for (auto& m : mutexes) { |
| 2248 | ASSERT_EQ(0, pthread_mutex_lock(&m)); |
| 2249 | } |
| 2250 | for (auto& m : mutexes) { |
| 2251 | ASSERT_EQ(0, pthread_mutex_unlock(&m)); |
| 2252 | } |
| 2253 | for (auto& m : mutexes) { |
| 2254 | ASSERT_EQ(0, pthread_mutex_destroy(&m)); |
| 2255 | } |
| 2256 | } |
| 2257 | ASSERT_EQ(0, pthread_mutexattr_destroy(&attr)); |
| 2258 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 2259 | GTEST_SKIP() << "pi mutex count not limited to 64Ki"; |
Yabin Cui | 5a00ba7 | 2018-01-26 17:32:31 -0800 | [diff] [blame] | 2260 | #endif |
| 2261 | } |
| 2262 | |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 2263 | TEST(pthread, pthread_mutex_init_same_as_static_initializers) { |
| 2264 | pthread_mutex_t lock_normal = PTHREAD_MUTEX_INITIALIZER; |
| 2265 | PthreadMutex m1(PTHREAD_MUTEX_NORMAL); |
| 2266 | ASSERT_EQ(0, memcmp(&lock_normal, &m1.lock, sizeof(pthread_mutex_t))); |
| 2267 | pthread_mutex_destroy(&lock_normal); |
| 2268 | |
Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame] | 2269 | #if !defined(ANDROID_HOST_MUSL) |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 2270 | // musl doesn't support PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP or |
| 2271 | // PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP. |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 2272 | pthread_mutex_t lock_errorcheck = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP; |
| 2273 | PthreadMutex m2(PTHREAD_MUTEX_ERRORCHECK); |
| 2274 | ASSERT_EQ(0, memcmp(&lock_errorcheck, &m2.lock, sizeof(pthread_mutex_t))); |
| 2275 | pthread_mutex_destroy(&lock_errorcheck); |
| 2276 | |
| 2277 | pthread_mutex_t lock_recursive = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; |
| 2278 | PthreadMutex m3(PTHREAD_MUTEX_RECURSIVE); |
| 2279 | ASSERT_EQ(0, memcmp(&lock_recursive, &m3.lock, sizeof(pthread_mutex_t))); |
| 2280 | ASSERT_EQ(0, pthread_mutex_destroy(&lock_recursive)); |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 2281 | #endif |
Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 2282 | } |
Yabin Cui | 5a00ba7 | 2018-01-26 17:32:31 -0800 | [diff] [blame] | 2283 | |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 2284 | class MutexWakeupHelper { |
| 2285 | private: |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 2286 | PthreadMutex m; |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 2287 | enum Progress { |
| 2288 | LOCK_INITIALIZED, |
| 2289 | LOCK_WAITING, |
| 2290 | LOCK_RELEASED, |
| 2291 | LOCK_ACCESSED |
| 2292 | }; |
| 2293 | std::atomic<Progress> progress; |
Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 2294 | std::atomic<pid_t> tid; |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 2295 | |
| 2296 | static void thread_fn(MutexWakeupHelper* helper) { |
Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 2297 | helper->tid = gettid(); |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 2298 | ASSERT_EQ(LOCK_INITIALIZED, helper->progress); |
| 2299 | helper->progress = LOCK_WAITING; |
| 2300 | |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 2301 | ASSERT_EQ(0, pthread_mutex_lock(&helper->m.lock)); |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 2302 | ASSERT_EQ(LOCK_RELEASED, helper->progress); |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 2303 | ASSERT_EQ(0, pthread_mutex_unlock(&helper->m.lock)); |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 2304 | |
| 2305 | helper->progress = LOCK_ACCESSED; |
| 2306 | } |
| 2307 | |
| 2308 | public: |
Chih-Hung Hsieh | 62e3a07 | 2016-05-03 12:08:05 -0700 | [diff] [blame] | 2309 | explicit MutexWakeupHelper(int mutex_type) : m(mutex_type) { |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 2310 | } |
| 2311 | |
| 2312 | void test() { |
| 2313 | ASSERT_EQ(0, pthread_mutex_lock(&m.lock)); |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 2314 | progress = LOCK_INITIALIZED; |
Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 2315 | tid = 0; |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 2316 | |
| 2317 | pthread_t thread; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 2318 | ASSERT_EQ(0, pthread_create(&thread, nullptr, |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 2319 | reinterpret_cast<void* (*)(void*)>(MutexWakeupHelper::thread_fn), this)); |
| 2320 | |
Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 2321 | WaitUntilThreadSleep(tid); |
| 2322 | ASSERT_EQ(LOCK_WAITING, progress); |
| 2323 | |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 2324 | progress = LOCK_RELEASED; |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 2325 | ASSERT_EQ(0, pthread_mutex_unlock(&m.lock)); |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 2326 | |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 2327 | ASSERT_EQ(0, pthread_join(thread, nullptr)); |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 2328 | ASSERT_EQ(LOCK_ACCESSED, progress); |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 2329 | } |
| 2330 | }; |
| 2331 | |
| 2332 | TEST(pthread, pthread_mutex_NORMAL_wakeup) { |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 2333 | MutexWakeupHelper helper(PTHREAD_MUTEX_NORMAL); |
| 2334 | helper.test(); |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 2335 | } |
| 2336 | |
| 2337 | TEST(pthread, pthread_mutex_ERRORCHECK_wakeup) { |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 2338 | MutexWakeupHelper helper(PTHREAD_MUTEX_ERRORCHECK); |
| 2339 | helper.test(); |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 2340 | } |
| 2341 | |
| 2342 | TEST(pthread, pthread_mutex_RECURSIVE_wakeup) { |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 2343 | MutexWakeupHelper helper(PTHREAD_MUTEX_RECURSIVE); |
| 2344 | helper.test(); |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 2345 | } |
| 2346 | |
Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 2347 | static int GetThreadPriority(pid_t tid) { |
| 2348 | // sched_getparam() returns the static priority of a thread, which can't reflect a thread's |
| 2349 | // priority after priority inheritance. So read /proc/<pid>/stat to get the dynamic priority. |
| 2350 | std::string filename = android::base::StringPrintf("/proc/%d/stat", tid); |
| 2351 | std::string content; |
| 2352 | int result = INT_MAX; |
| 2353 | if (!android::base::ReadFileToString(filename, &content)) { |
| 2354 | return result; |
| 2355 | } |
| 2356 | std::vector<std::string> strs = android::base::Split(content, " "); |
| 2357 | if (strs.size() < 18) { |
| 2358 | return result; |
| 2359 | } |
| 2360 | if (!android::base::ParseInt(strs[17], &result)) { |
| 2361 | return INT_MAX; |
| 2362 | } |
| 2363 | return result; |
| 2364 | } |
| 2365 | |
| 2366 | class PIMutexWakeupHelper { |
| 2367 | private: |
| 2368 | PthreadMutex m; |
| 2369 | int protocol; |
| 2370 | enum Progress { |
| 2371 | LOCK_INITIALIZED, |
| 2372 | LOCK_CHILD_READY, |
| 2373 | LOCK_WAITING, |
| 2374 | LOCK_RELEASED, |
| 2375 | }; |
| 2376 | std::atomic<Progress> progress; |
| 2377 | std::atomic<pid_t> main_tid; |
| 2378 | std::atomic<pid_t> child_tid; |
| 2379 | PthreadMutex start_thread_m; |
| 2380 | |
| 2381 | static void thread_fn(PIMutexWakeupHelper* helper) { |
| 2382 | helper->child_tid = gettid(); |
| 2383 | ASSERT_EQ(LOCK_INITIALIZED, helper->progress); |
| 2384 | ASSERT_EQ(0, setpriority(PRIO_PROCESS, gettid(), 1)); |
| 2385 | ASSERT_EQ(21, GetThreadPriority(gettid())); |
| 2386 | ASSERT_EQ(0, pthread_mutex_lock(&helper->m.lock)); |
| 2387 | helper->progress = LOCK_CHILD_READY; |
| 2388 | ASSERT_EQ(0, pthread_mutex_lock(&helper->start_thread_m.lock)); |
| 2389 | |
| 2390 | ASSERT_EQ(0, pthread_mutex_unlock(&helper->start_thread_m.lock)); |
| 2391 | WaitUntilThreadSleep(helper->main_tid); |
| 2392 | ASSERT_EQ(LOCK_WAITING, helper->progress); |
| 2393 | |
| 2394 | if (helper->protocol == PTHREAD_PRIO_INHERIT) { |
| 2395 | ASSERT_EQ(20, GetThreadPriority(gettid())); |
| 2396 | } else { |
| 2397 | ASSERT_EQ(21, GetThreadPriority(gettid())); |
| 2398 | } |
| 2399 | helper->progress = LOCK_RELEASED; |
| 2400 | ASSERT_EQ(0, pthread_mutex_unlock(&helper->m.lock)); |
| 2401 | } |
| 2402 | |
| 2403 | public: |
| 2404 | explicit PIMutexWakeupHelper(int mutex_type, int protocol) |
| 2405 | : m(mutex_type, protocol), protocol(protocol), start_thread_m(PTHREAD_MUTEX_NORMAL) { |
| 2406 | } |
| 2407 | |
| 2408 | void test() { |
| 2409 | ASSERT_EQ(0, pthread_mutex_lock(&start_thread_m.lock)); |
| 2410 | main_tid = gettid(); |
| 2411 | ASSERT_EQ(20, GetThreadPriority(main_tid)); |
| 2412 | progress = LOCK_INITIALIZED; |
| 2413 | child_tid = 0; |
| 2414 | |
| 2415 | pthread_t thread; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 2416 | ASSERT_EQ(0, pthread_create(&thread, nullptr, |
Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 2417 | reinterpret_cast<void* (*)(void*)>(PIMutexWakeupHelper::thread_fn), this)); |
| 2418 | |
| 2419 | WaitUntilThreadSleep(child_tid); |
| 2420 | ASSERT_EQ(LOCK_CHILD_READY, progress); |
| 2421 | ASSERT_EQ(0, pthread_mutex_unlock(&start_thread_m.lock)); |
| 2422 | progress = LOCK_WAITING; |
| 2423 | ASSERT_EQ(0, pthread_mutex_lock(&m.lock)); |
| 2424 | |
| 2425 | ASSERT_EQ(LOCK_RELEASED, progress); |
| 2426 | ASSERT_EQ(0, pthread_mutex_unlock(&m.lock)); |
| 2427 | ASSERT_EQ(0, pthread_join(thread, nullptr)); |
| 2428 | } |
| 2429 | }; |
| 2430 | |
| 2431 | TEST(pthread, pthread_mutex_pi_wakeup) { |
Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 2432 | for (int type : {PTHREAD_MUTEX_NORMAL, PTHREAD_MUTEX_RECURSIVE, PTHREAD_MUTEX_ERRORCHECK}) { |
| 2433 | for (int protocol : {PTHREAD_PRIO_INHERIT}) { |
| 2434 | PIMutexWakeupHelper helper(type, protocol); |
| 2435 | helper.test(); |
| 2436 | } |
| 2437 | } |
| 2438 | } |
| 2439 | |
Yabin Cui | 140f367 | 2015-02-03 10:32:00 -0800 | [diff] [blame] | 2440 | TEST(pthread, pthread_mutex_owner_tid_limit) { |
Yabin Cui | e69c245 | 2015-02-13 16:21:25 -0800 | [diff] [blame] | 2441 | #if defined(__BIONIC__) && !defined(__LP64__) |
Yabin Cui | 140f367 | 2015-02-03 10:32:00 -0800 | [diff] [blame] | 2442 | FILE* fp = fopen("/proc/sys/kernel/pid_max", "r"); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 2443 | ASSERT_TRUE(fp != nullptr); |
Yabin Cui | 140f367 | 2015-02-03 10:32:00 -0800 | [diff] [blame] | 2444 | long pid_max; |
| 2445 | ASSERT_EQ(1, fscanf(fp, "%ld", &pid_max)); |
| 2446 | fclose(fp); |
Yabin Cui | e69c245 | 2015-02-13 16:21:25 -0800 | [diff] [blame] | 2447 | // 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] | 2448 | ASSERT_LE(pid_max, 65536); |
Yabin Cui | e69c245 | 2015-02-13 16:21:25 -0800 | [diff] [blame] | 2449 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 2450 | GTEST_SKIP() << "pthread_mutex supports 32-bit tid"; |
Yabin Cui | e69c245 | 2015-02-13 16:21:25 -0800 | [diff] [blame] | 2451 | #endif |
Yabin Cui | 140f367 | 2015-02-03 10:32:00 -0800 | [diff] [blame] | 2452 | } |
Yabin Cui | b584572 | 2015-03-16 22:46:42 -0700 | [diff] [blame] | 2453 | |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 2454 | static void pthread_mutex_timedlock_helper(clockid_t clock, |
| 2455 | int (*lock_function)(pthread_mutex_t* __mutex, |
| 2456 | const timespec* __timeout)) { |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 2457 | pthread_mutex_t m; |
| 2458 | ASSERT_EQ(0, pthread_mutex_init(&m, nullptr)); |
| 2459 | |
| 2460 | // If the mutex is already locked, pthread_mutex_timedlock should time out. |
| 2461 | ASSERT_EQ(0, pthread_mutex_lock(&m)); |
| 2462 | |
| 2463 | timespec ts; |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 2464 | ASSERT_EQ(0, clock_gettime(clock, &ts)); |
| 2465 | ASSERT_EQ(ETIMEDOUT, lock_function(&m, &ts)); |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 2466 | ts.tv_nsec = -1; |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 2467 | ASSERT_EQ(EINVAL, lock_function(&m, &ts)); |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 2468 | ts.tv_nsec = NS_PER_S; |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 2469 | ASSERT_EQ(EINVAL, lock_function(&m, &ts)); |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 2470 | ts.tv_nsec = NS_PER_S - 1; |
| 2471 | ts.tv_sec = -1; |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 2472 | ASSERT_EQ(ETIMEDOUT, lock_function(&m, &ts)); |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 2473 | |
Elliott Hughes | eabc47b | 2024-11-08 22:05:00 +0000 | [diff] [blame] | 2474 | // Check we wait long enough for the lock before timing out... |
| 2475 | |
| 2476 | // What time is it before we start? |
Andy Hung | 5e19b18 | 2023-12-11 19:28:02 -0800 | [diff] [blame] | 2477 | ASSERT_EQ(0, clock_gettime(clock, &ts)); |
Elliott Hughes | eabc47b | 2024-11-08 22:05:00 +0000 | [diff] [blame] | 2478 | const int64_t start_ns = to_ns(ts); |
| 2479 | // Add a second to get deadline, and wait until we time out. |
Andy Hung | 5e19b18 | 2023-12-11 19:28:02 -0800 | [diff] [blame] | 2480 | ts.tv_sec += 1; |
Andy Hung | 5e19b18 | 2023-12-11 19:28:02 -0800 | [diff] [blame] | 2481 | ASSERT_EQ(ETIMEDOUT, lock_function(&m, &ts)); |
| 2482 | |
Elliott Hughes | eabc47b | 2024-11-08 22:05:00 +0000 | [diff] [blame] | 2483 | // What time is it now we've timed out? |
| 2484 | timespec ts2; |
| 2485 | clock_gettime(clock, &ts2); |
| 2486 | const int64_t end_ns = to_ns(ts2); |
| 2487 | |
Andy Hung | 5e19b18 | 2023-12-11 19:28:02 -0800 | [diff] [blame] | 2488 | // The timedlock must have waited at least 1 second before returning. |
Elliott Hughes | eabc47b | 2024-11-08 22:05:00 +0000 | [diff] [blame] | 2489 | ASSERT_GE(end_ns - start_ns, NS_PER_S); |
Andy Hung | 5e19b18 | 2023-12-11 19:28:02 -0800 | [diff] [blame] | 2490 | |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 2491 | // If the mutex is unlocked, pthread_mutex_timedlock should succeed. |
| 2492 | ASSERT_EQ(0, pthread_mutex_unlock(&m)); |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 2493 | ASSERT_EQ(0, clock_gettime(clock, &ts)); |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 2494 | ts.tv_sec += 1; |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 2495 | ASSERT_EQ(0, lock_function(&m, &ts)); |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 2496 | |
| 2497 | ASSERT_EQ(0, pthread_mutex_unlock(&m)); |
| 2498 | ASSERT_EQ(0, pthread_mutex_destroy(&m)); |
| 2499 | } |
| 2500 | |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 2501 | TEST(pthread, pthread_mutex_timedlock) { |
| 2502 | pthread_mutex_timedlock_helper(CLOCK_REALTIME, pthread_mutex_timedlock); |
| 2503 | } |
| 2504 | |
| 2505 | TEST(pthread, pthread_mutex_timedlock_monotonic_np) { |
| 2506 | #if defined(__BIONIC__) |
| 2507 | pthread_mutex_timedlock_helper(CLOCK_MONOTONIC, pthread_mutex_timedlock_monotonic_np); |
| 2508 | #else // __BIONIC__ |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 2509 | GTEST_SKIP() << "pthread_mutex_timedlock_monotonic_np not available"; |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 2510 | #endif // __BIONIC__ |
| 2511 | } |
| 2512 | |
Elliott Hughes | eabc47b | 2024-11-08 22:05:00 +0000 | [diff] [blame] | 2513 | TEST(pthread, pthread_mutex_clocklock_MONOTONIC) { |
Tom Cherry | 6901080 | 2019-05-07 20:33:05 -0700 | [diff] [blame] | 2514 | #if defined(__BIONIC__) |
| 2515 | pthread_mutex_timedlock_helper( |
| 2516 | CLOCK_MONOTONIC, [](pthread_mutex_t* __mutex, const timespec* __timeout) { |
| 2517 | return pthread_mutex_clocklock(__mutex, CLOCK_MONOTONIC, __timeout); |
| 2518 | }); |
Elliott Hughes | eabc47b | 2024-11-08 22:05:00 +0000 | [diff] [blame] | 2519 | #else // __BIONIC__ |
| 2520 | GTEST_SKIP() << "pthread_mutex_clocklock not available"; |
| 2521 | #endif // __BIONIC__ |
| 2522 | } |
| 2523 | |
| 2524 | TEST(pthread, pthread_mutex_clocklock_REALTIME) { |
| 2525 | #if defined(__BIONIC__) |
Tom Cherry | 6901080 | 2019-05-07 20:33:05 -0700 | [diff] [blame] | 2526 | pthread_mutex_timedlock_helper( |
| 2527 | CLOCK_REALTIME, [](pthread_mutex_t* __mutex, const timespec* __timeout) { |
| 2528 | return pthread_mutex_clocklock(__mutex, CLOCK_REALTIME, __timeout); |
| 2529 | }); |
| 2530 | #else // __BIONIC__ |
| 2531 | GTEST_SKIP() << "pthread_mutex_clocklock not available"; |
| 2532 | #endif // __BIONIC__ |
| 2533 | } |
| 2534 | |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 2535 | static void pthread_mutex_timedlock_pi_helper(clockid_t clock, |
| 2536 | int (*lock_function)(pthread_mutex_t* __mutex, |
| 2537 | const timespec* __timeout)) { |
Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 2538 | PthreadMutex m(PTHREAD_MUTEX_NORMAL, PTHREAD_PRIO_INHERIT); |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 2539 | |
Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 2540 | timespec ts; |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 2541 | clock_gettime(clock, &ts); |
Andy Hung | 5e19b18 | 2023-12-11 19:28:02 -0800 | [diff] [blame] | 2542 | const int64_t start_ns = ts.tv_sec * NS_PER_S + ts.tv_nsec; |
| 2543 | |
| 2544 | // add a second to get deadline. |
Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 2545 | ts.tv_sec += 1; |
Andy Hung | 5e19b18 | 2023-12-11 19:28:02 -0800 | [diff] [blame] | 2546 | |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 2547 | ASSERT_EQ(0, lock_function(&m.lock, &ts)); |
| 2548 | |
| 2549 | struct ThreadArgs { |
| 2550 | clockid_t clock; |
| 2551 | int (*lock_function)(pthread_mutex_t* __mutex, const timespec* __timeout); |
| 2552 | PthreadMutex& m; |
| 2553 | }; |
| 2554 | |
| 2555 | ThreadArgs thread_args = { |
| 2556 | .clock = clock, |
| 2557 | .lock_function = lock_function, |
| 2558 | .m = m, |
| 2559 | }; |
Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 2560 | |
| 2561 | auto ThreadFn = [](void* arg) -> void* { |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 2562 | auto args = static_cast<ThreadArgs*>(arg); |
Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 2563 | timespec ts; |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 2564 | clock_gettime(args->clock, &ts); |
Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 2565 | ts.tv_sec += 1; |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 2566 | intptr_t result = args->lock_function(&args->m.lock, &ts); |
Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 2567 | return reinterpret_cast<void*>(result); |
| 2568 | }; |
| 2569 | |
| 2570 | pthread_t thread; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 2571 | ASSERT_EQ(0, pthread_create(&thread, nullptr, ThreadFn, &thread_args)); |
Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 2572 | void* result; |
| 2573 | ASSERT_EQ(0, pthread_join(thread, &result)); |
| 2574 | ASSERT_EQ(ETIMEDOUT, reinterpret_cast<intptr_t>(result)); |
Andy Hung | 5e19b18 | 2023-12-11 19:28:02 -0800 | [diff] [blame] | 2575 | |
| 2576 | // The timedlock must have waited at least 1 second before returning. |
| 2577 | clock_gettime(clock, &ts); |
| 2578 | const int64_t end_ns = ts.tv_sec * NS_PER_S + ts.tv_nsec; |
| 2579 | ASSERT_GT(end_ns - start_ns, NS_PER_S); |
| 2580 | |
Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 2581 | ASSERT_EQ(0, pthread_mutex_unlock(&m.lock)); |
| 2582 | } |
| 2583 | |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 2584 | TEST(pthread, pthread_mutex_timedlock_pi) { |
| 2585 | pthread_mutex_timedlock_pi_helper(CLOCK_REALTIME, pthread_mutex_timedlock); |
| 2586 | } |
| 2587 | |
| 2588 | TEST(pthread, pthread_mutex_timedlock_monotonic_np_pi) { |
| 2589 | #if defined(__BIONIC__) |
| 2590 | pthread_mutex_timedlock_pi_helper(CLOCK_MONOTONIC, pthread_mutex_timedlock_monotonic_np); |
| 2591 | #else // __BIONIC__ |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 2592 | GTEST_SKIP() << "pthread_mutex_timedlock_monotonic_np not available"; |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 2593 | #endif // __BIONIC__ |
| 2594 | } |
| 2595 | |
Tom Cherry | 6901080 | 2019-05-07 20:33:05 -0700 | [diff] [blame] | 2596 | TEST(pthread, pthread_mutex_clocklock_pi) { |
| 2597 | #if defined(__BIONIC__) |
| 2598 | pthread_mutex_timedlock_pi_helper( |
| 2599 | CLOCK_MONOTONIC, [](pthread_mutex_t* __mutex, const timespec* __timeout) { |
| 2600 | return pthread_mutex_clocklock(__mutex, CLOCK_MONOTONIC, __timeout); |
| 2601 | }); |
| 2602 | pthread_mutex_timedlock_pi_helper( |
| 2603 | CLOCK_REALTIME, [](pthread_mutex_t* __mutex, const timespec* __timeout) { |
| 2604 | return pthread_mutex_clocklock(__mutex, CLOCK_REALTIME, __timeout); |
| 2605 | }); |
| 2606 | #else // __BIONIC__ |
| 2607 | GTEST_SKIP() << "pthread_mutex_clocklock not available"; |
| 2608 | #endif // __BIONIC__ |
| 2609 | } |
| 2610 | |
| 2611 | TEST(pthread, pthread_mutex_clocklock_invalid) { |
| 2612 | #if defined(__BIONIC__) |
| 2613 | pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; |
| 2614 | timespec ts; |
| 2615 | EXPECT_EQ(EINVAL, pthread_mutex_clocklock(&mutex, CLOCK_PROCESS_CPUTIME_ID, &ts)); |
| 2616 | #else // __BIONIC__ |
| 2617 | GTEST_SKIP() << "pthread_mutex_clocklock not available"; |
| 2618 | #endif // __BIONIC__ |
| 2619 | } |
| 2620 | |
Elliott Hughes | e657eb4 | 2021-02-18 17:11:56 -0800 | [diff] [blame] | 2621 | TEST_F(pthread_DeathTest, pthread_mutex_using_destroyed_mutex) { |
Yabin Cui | 9651fdf | 2018-03-14 12:02:21 -0700 | [diff] [blame] | 2622 | #if defined(__BIONIC__) |
| 2623 | pthread_mutex_t m; |
| 2624 | ASSERT_EQ(0, pthread_mutex_init(&m, nullptr)); |
| 2625 | ASSERT_EQ(0, pthread_mutex_destroy(&m)); |
| 2626 | ASSERT_EXIT(pthread_mutex_lock(&m), ::testing::KilledBySignal(SIGABRT), |
| 2627 | "pthread_mutex_lock called on a destroyed mutex"); |
| 2628 | ASSERT_EXIT(pthread_mutex_unlock(&m), ::testing::KilledBySignal(SIGABRT), |
| 2629 | "pthread_mutex_unlock called on a destroyed mutex"); |
| 2630 | ASSERT_EXIT(pthread_mutex_trylock(&m), ::testing::KilledBySignal(SIGABRT), |
| 2631 | "pthread_mutex_trylock called on a destroyed mutex"); |
| 2632 | timespec ts; |
| 2633 | ASSERT_EXIT(pthread_mutex_timedlock(&m, &ts), ::testing::KilledBySignal(SIGABRT), |
| 2634 | "pthread_mutex_timedlock called on a destroyed mutex"); |
Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 2635 | ASSERT_EXIT(pthread_mutex_timedlock_monotonic_np(&m, &ts), ::testing::KilledBySignal(SIGABRT), |
| 2636 | "pthread_mutex_timedlock_monotonic_np called on a destroyed mutex"); |
Tom Cherry | 6901080 | 2019-05-07 20:33:05 -0700 | [diff] [blame] | 2637 | ASSERT_EXIT(pthread_mutex_clocklock(&m, CLOCK_MONOTONIC, &ts), ::testing::KilledBySignal(SIGABRT), |
| 2638 | "pthread_mutex_clocklock called on a destroyed mutex"); |
| 2639 | ASSERT_EXIT(pthread_mutex_clocklock(&m, CLOCK_REALTIME, &ts), ::testing::KilledBySignal(SIGABRT), |
| 2640 | "pthread_mutex_clocklock called on a destroyed mutex"); |
| 2641 | ASSERT_EXIT(pthread_mutex_clocklock(&m, CLOCK_PROCESS_CPUTIME_ID, &ts), |
| 2642 | ::testing::KilledBySignal(SIGABRT), |
| 2643 | "pthread_mutex_clocklock called on a destroyed mutex"); |
Yabin Cui | 9651fdf | 2018-03-14 12:02:21 -0700 | [diff] [blame] | 2644 | ASSERT_EXIT(pthread_mutex_destroy(&m), ::testing::KilledBySignal(SIGABRT), |
| 2645 | "pthread_mutex_destroy called on a destroyed mutex"); |
| 2646 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 2647 | GTEST_SKIP() << "bionic-only test"; |
Yabin Cui | 9651fdf | 2018-03-14 12:02:21 -0700 | [diff] [blame] | 2648 | #endif |
| 2649 | } |
| 2650 | |
Yabin Cui | b584572 | 2015-03-16 22:46:42 -0700 | [diff] [blame] | 2651 | class StrictAlignmentAllocator { |
| 2652 | public: |
| 2653 | void* allocate(size_t size, size_t alignment) { |
| 2654 | char* p = new char[size + alignment * 2]; |
| 2655 | allocated_array.push_back(p); |
| 2656 | while (!is_strict_aligned(p, alignment)) { |
| 2657 | ++p; |
| 2658 | } |
| 2659 | return p; |
| 2660 | } |
| 2661 | |
| 2662 | ~StrictAlignmentAllocator() { |
Elliott Hughes | 0b2acdf | 2015-10-02 18:25:19 -0700 | [diff] [blame] | 2663 | for (const auto& p : allocated_array) { |
| 2664 | delete[] p; |
Yabin Cui | b584572 | 2015-03-16 22:46:42 -0700 | [diff] [blame] | 2665 | } |
| 2666 | } |
| 2667 | |
| 2668 | private: |
| 2669 | bool is_strict_aligned(char* p, size_t alignment) { |
| 2670 | return (reinterpret_cast<uintptr_t>(p) % (alignment * 2)) == alignment; |
| 2671 | } |
| 2672 | |
| 2673 | std::vector<char*> allocated_array; |
| 2674 | }; |
| 2675 | |
| 2676 | TEST(pthread, pthread_types_allow_four_bytes_alignment) { |
| 2677 | #if defined(__BIONIC__) |
| 2678 | // For binary compatibility with old version, we need to allow 4-byte aligned data for pthread types. |
| 2679 | StrictAlignmentAllocator allocator; |
| 2680 | pthread_mutex_t* mutex = reinterpret_cast<pthread_mutex_t*>( |
| 2681 | allocator.allocate(sizeof(pthread_mutex_t), 4)); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 2682 | ASSERT_EQ(0, pthread_mutex_init(mutex, nullptr)); |
Yabin Cui | b584572 | 2015-03-16 22:46:42 -0700 | [diff] [blame] | 2683 | ASSERT_EQ(0, pthread_mutex_lock(mutex)); |
| 2684 | ASSERT_EQ(0, pthread_mutex_unlock(mutex)); |
| 2685 | ASSERT_EQ(0, pthread_mutex_destroy(mutex)); |
| 2686 | |
| 2687 | pthread_cond_t* cond = reinterpret_cast<pthread_cond_t*>( |
| 2688 | allocator.allocate(sizeof(pthread_cond_t), 4)); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 2689 | ASSERT_EQ(0, pthread_cond_init(cond, nullptr)); |
Yabin Cui | b584572 | 2015-03-16 22:46:42 -0700 | [diff] [blame] | 2690 | ASSERT_EQ(0, pthread_cond_signal(cond)); |
| 2691 | ASSERT_EQ(0, pthread_cond_broadcast(cond)); |
| 2692 | ASSERT_EQ(0, pthread_cond_destroy(cond)); |
| 2693 | |
| 2694 | pthread_rwlock_t* rwlock = reinterpret_cast<pthread_rwlock_t*>( |
| 2695 | allocator.allocate(sizeof(pthread_rwlock_t), 4)); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 2696 | ASSERT_EQ(0, pthread_rwlock_init(rwlock, nullptr)); |
Yabin Cui | b584572 | 2015-03-16 22:46:42 -0700 | [diff] [blame] | 2697 | ASSERT_EQ(0, pthread_rwlock_rdlock(rwlock)); |
| 2698 | ASSERT_EQ(0, pthread_rwlock_unlock(rwlock)); |
| 2699 | ASSERT_EQ(0, pthread_rwlock_wrlock(rwlock)); |
| 2700 | ASSERT_EQ(0, pthread_rwlock_unlock(rwlock)); |
| 2701 | ASSERT_EQ(0, pthread_rwlock_destroy(rwlock)); |
| 2702 | |
| 2703 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 2704 | GTEST_SKIP() << "bionic-only test"; |
Yabin Cui | b584572 | 2015-03-16 22:46:42 -0700 | [diff] [blame] | 2705 | #endif |
| 2706 | } |
Christopher Ferris | 60907c7 | 2015-06-09 18:46:15 -0700 | [diff] [blame] | 2707 | |
| 2708 | TEST(pthread, pthread_mutex_lock_null_32) { |
| 2709 | #if defined(__BIONIC__) && !defined(__LP64__) |
Dan Albert | baa2a97 | 2015-08-13 16:58:50 -0700 | [diff] [blame] | 2710 | // For LP32, the pthread lock/unlock functions allow a NULL mutex and return |
| 2711 | // EINVAL in that case: http://b/19995172. |
| 2712 | // |
| 2713 | // We decorate the public defintion with _Nonnull so that people recompiling |
| 2714 | // their code with get a warning and might fix their bug, but need to pass |
| 2715 | // NULL here to test that we remain compatible. |
| 2716 | pthread_mutex_t* null_value = nullptr; |
| 2717 | ASSERT_EQ(EINVAL, pthread_mutex_lock(null_value)); |
Christopher Ferris | 60907c7 | 2015-06-09 18:46:15 -0700 | [diff] [blame] | 2718 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 2719 | GTEST_SKIP() << "32-bit bionic-only test"; |
Christopher Ferris | 60907c7 | 2015-06-09 18:46:15 -0700 | [diff] [blame] | 2720 | #endif |
| 2721 | } |
| 2722 | |
| 2723 | TEST(pthread, pthread_mutex_unlock_null_32) { |
| 2724 | #if defined(__BIONIC__) && !defined(__LP64__) |
Dan Albert | baa2a97 | 2015-08-13 16:58:50 -0700 | [diff] [blame] | 2725 | // For LP32, the pthread lock/unlock functions allow a NULL mutex and return |
| 2726 | // EINVAL in that case: http://b/19995172. |
| 2727 | // |
| 2728 | // We decorate the public defintion with _Nonnull so that people recompiling |
| 2729 | // their code with get a warning and might fix their bug, but need to pass |
| 2730 | // NULL here to test that we remain compatible. |
| 2731 | pthread_mutex_t* null_value = nullptr; |
| 2732 | ASSERT_EQ(EINVAL, pthread_mutex_unlock(null_value)); |
Christopher Ferris | 60907c7 | 2015-06-09 18:46:15 -0700 | [diff] [blame] | 2733 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 2734 | GTEST_SKIP() << "32-bit bionic-only test"; |
Christopher Ferris | 60907c7 | 2015-06-09 18:46:15 -0700 | [diff] [blame] | 2735 | #endif |
| 2736 | } |
| 2737 | |
| 2738 | TEST_F(pthread_DeathTest, pthread_mutex_lock_null_64) { |
| 2739 | #if defined(__BIONIC__) && defined(__LP64__) |
| 2740 | pthread_mutex_t* null_value = nullptr; |
| 2741 | ASSERT_EXIT(pthread_mutex_lock(null_value), testing::KilledBySignal(SIGSEGV), ""); |
| 2742 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 2743 | GTEST_SKIP() << "64-bit bionic-only test"; |
Christopher Ferris | 60907c7 | 2015-06-09 18:46:15 -0700 | [diff] [blame] | 2744 | #endif |
| 2745 | } |
| 2746 | |
| 2747 | TEST_F(pthread_DeathTest, pthread_mutex_unlock_null_64) { |
| 2748 | #if defined(__BIONIC__) && defined(__LP64__) |
| 2749 | pthread_mutex_t* null_value = nullptr; |
| 2750 | ASSERT_EXIT(pthread_mutex_unlock(null_value), testing::KilledBySignal(SIGSEGV), ""); |
| 2751 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 2752 | GTEST_SKIP() << "64-bit bionic-only test"; |
Christopher Ferris | 60907c7 | 2015-06-09 18:46:15 -0700 | [diff] [blame] | 2753 | #endif |
| 2754 | } |
Yabin Cui | 33ac04a | 2015-09-22 11:16:15 -0700 | [diff] [blame] | 2755 | |
| 2756 | extern _Unwind_Reason_Code FrameCounter(_Unwind_Context* ctx, void* arg); |
| 2757 | |
| 2758 | static volatile bool signal_handler_on_altstack_done; |
| 2759 | |
Josh Gao | 61db9ac | 2017-03-15 19:42:05 -0700 | [diff] [blame] | 2760 | __attribute__((__noinline__)) |
| 2761 | static void signal_handler_backtrace() { |
| 2762 | // Check if we have enough stack space for unwinding. |
| 2763 | int count = 0; |
| 2764 | _Unwind_Backtrace(FrameCounter, &count); |
| 2765 | ASSERT_GT(count, 0); |
| 2766 | } |
| 2767 | |
| 2768 | __attribute__((__noinline__)) |
| 2769 | static void signal_handler_logging() { |
| 2770 | // Check if we have enough stack space for logging. |
| 2771 | std::string s(2048, '*'); |
| 2772 | GTEST_LOG_(INFO) << s; |
| 2773 | signal_handler_on_altstack_done = true; |
| 2774 | } |
| 2775 | |
| 2776 | __attribute__((__noinline__)) |
| 2777 | static void signal_handler_snprintf() { |
| 2778 | // Check if we have enough stack space for snprintf to a PATH_MAX buffer, plus some extra. |
| 2779 | char buf[PATH_MAX + 2048]; |
| 2780 | ASSERT_GT(snprintf(buf, sizeof(buf), "/proc/%d/status", getpid()), 0); |
| 2781 | } |
| 2782 | |
Yabin Cui | 33ac04a | 2015-09-22 11:16:15 -0700 | [diff] [blame] | 2783 | static void SignalHandlerOnAltStack(int signo, siginfo_t*, void*) { |
| 2784 | ASSERT_EQ(SIGUSR1, signo); |
Josh Gao | 61db9ac | 2017-03-15 19:42:05 -0700 | [diff] [blame] | 2785 | signal_handler_backtrace(); |
| 2786 | signal_handler_logging(); |
| 2787 | signal_handler_snprintf(); |
Yabin Cui | 33ac04a | 2015-09-22 11:16:15 -0700 | [diff] [blame] | 2788 | } |
| 2789 | |
Josh Gao | 415daa8 | 2017-03-06 17:45:33 -0800 | [diff] [blame] | 2790 | TEST(pthread, big_enough_signal_stack) { |
Yabin Cui | 33ac04a | 2015-09-22 11:16:15 -0700 | [diff] [blame] | 2791 | signal_handler_on_altstack_done = false; |
| 2792 | ScopedSignalHandler handler(SIGUSR1, SignalHandlerOnAltStack, SA_SIGINFO | SA_ONSTACK); |
| 2793 | kill(getpid(), SIGUSR1); |
| 2794 | ASSERT_TRUE(signal_handler_on_altstack_done); |
| 2795 | } |
Yabin Cui | e7c2fff | 2015-11-05 22:06:09 -0800 | [diff] [blame] | 2796 | |
| 2797 | TEST(pthread, pthread_barrierattr_smoke) { |
| 2798 | pthread_barrierattr_t attr; |
| 2799 | ASSERT_EQ(0, pthread_barrierattr_init(&attr)); |
| 2800 | int pshared; |
| 2801 | ASSERT_EQ(0, pthread_barrierattr_getpshared(&attr, &pshared)); |
| 2802 | ASSERT_EQ(PTHREAD_PROCESS_PRIVATE, pshared); |
| 2803 | ASSERT_EQ(0, pthread_barrierattr_setpshared(&attr, PTHREAD_PROCESS_SHARED)); |
| 2804 | ASSERT_EQ(0, pthread_barrierattr_getpshared(&attr, &pshared)); |
| 2805 | ASSERT_EQ(PTHREAD_PROCESS_SHARED, pshared); |
| 2806 | ASSERT_EQ(0, pthread_barrierattr_destroy(&attr)); |
| 2807 | } |
| 2808 | |
Yabin Cui | 81d2797 | 2016-03-22 13:45:55 -0700 | [diff] [blame] | 2809 | struct BarrierTestHelperData { |
| 2810 | size_t thread_count; |
| 2811 | pthread_barrier_t barrier; |
| 2812 | std::atomic<int> finished_mask; |
| 2813 | std::atomic<int> serial_thread_count; |
Yabin Cui | e7c2fff | 2015-11-05 22:06:09 -0800 | [diff] [blame] | 2814 | size_t iteration_count; |
Yabin Cui | 81d2797 | 2016-03-22 13:45:55 -0700 | [diff] [blame] | 2815 | std::atomic<size_t> finished_iteration_count; |
| 2816 | |
| 2817 | BarrierTestHelperData(size_t thread_count, size_t iteration_count) |
| 2818 | : thread_count(thread_count), finished_mask(0), serial_thread_count(0), |
| 2819 | iteration_count(iteration_count), finished_iteration_count(0) { |
| 2820 | } |
| 2821 | }; |
| 2822 | |
| 2823 | struct BarrierTestHelperArg { |
| 2824 | int id; |
| 2825 | BarrierTestHelperData* data; |
Yabin Cui | e7c2fff | 2015-11-05 22:06:09 -0800 | [diff] [blame] | 2826 | }; |
| 2827 | |
| 2828 | static void BarrierTestHelper(BarrierTestHelperArg* arg) { |
Yabin Cui | 81d2797 | 2016-03-22 13:45:55 -0700 | [diff] [blame] | 2829 | for (size_t i = 0; i < arg->data->iteration_count; ++i) { |
| 2830 | int result = pthread_barrier_wait(&arg->data->barrier); |
| 2831 | if (result == PTHREAD_BARRIER_SERIAL_THREAD) { |
| 2832 | arg->data->serial_thread_count++; |
| 2833 | } else { |
| 2834 | ASSERT_EQ(0, result); |
| 2835 | } |
Yabin Cui | d5c04c5 | 2017-05-02 12:57:39 -0700 | [diff] [blame] | 2836 | int mask = arg->data->finished_mask.fetch_or(1 << arg->id); |
Yabin Cui | ab4cddc | 2017-05-02 16:18:13 -0700 | [diff] [blame] | 2837 | mask |= 1 << arg->id; |
Yabin Cui | d5c04c5 | 2017-05-02 12:57:39 -0700 | [diff] [blame] | 2838 | if (mask == ((1 << arg->data->thread_count) - 1)) { |
Yabin Cui | 81d2797 | 2016-03-22 13:45:55 -0700 | [diff] [blame] | 2839 | ASSERT_EQ(1, arg->data->serial_thread_count); |
| 2840 | arg->data->finished_iteration_count++; |
| 2841 | arg->data->finished_mask = 0; |
| 2842 | arg->data->serial_thread_count = 0; |
| 2843 | } |
Yabin Cui | e7c2fff | 2015-11-05 22:06:09 -0800 | [diff] [blame] | 2844 | } |
| 2845 | } |
| 2846 | |
| 2847 | TEST(pthread, pthread_barrier_smoke) { |
| 2848 | const size_t BARRIER_ITERATION_COUNT = 10; |
| 2849 | const size_t BARRIER_THREAD_COUNT = 10; |
Yabin Cui | 81d2797 | 2016-03-22 13:45:55 -0700 | [diff] [blame] | 2850 | BarrierTestHelperData data(BARRIER_THREAD_COUNT, BARRIER_ITERATION_COUNT); |
| 2851 | ASSERT_EQ(0, pthread_barrier_init(&data.barrier, nullptr, data.thread_count)); |
| 2852 | std::vector<pthread_t> threads(data.thread_count); |
Yabin Cui | e7c2fff | 2015-11-05 22:06:09 -0800 | [diff] [blame] | 2853 | std::vector<BarrierTestHelperArg> args(threads.size()); |
| 2854 | for (size_t i = 0; i < threads.size(); ++i) { |
Yabin Cui | 81d2797 | 2016-03-22 13:45:55 -0700 | [diff] [blame] | 2855 | args[i].id = i; |
| 2856 | args[i].data = &data; |
Yabin Cui | e7c2fff | 2015-11-05 22:06:09 -0800 | [diff] [blame] | 2857 | ASSERT_EQ(0, pthread_create(&threads[i], nullptr, |
| 2858 | reinterpret_cast<void* (*)(void*)>(BarrierTestHelper), &args[i])); |
| 2859 | } |
Yabin Cui | e7c2fff | 2015-11-05 22:06:09 -0800 | [diff] [blame] | 2860 | for (size_t i = 0; i < threads.size(); ++i) { |
| 2861 | ASSERT_EQ(0, pthread_join(threads[i], nullptr)); |
| 2862 | } |
Yabin Cui | 81d2797 | 2016-03-22 13:45:55 -0700 | [diff] [blame] | 2863 | ASSERT_EQ(data.iteration_count, data.finished_iteration_count); |
| 2864 | ASSERT_EQ(0, pthread_barrier_destroy(&data.barrier)); |
| 2865 | } |
| 2866 | |
| 2867 | struct BarrierDestroyTestArg { |
| 2868 | std::atomic<int> tid; |
| 2869 | pthread_barrier_t* barrier; |
| 2870 | }; |
| 2871 | |
| 2872 | static void BarrierDestroyTestHelper(BarrierDestroyTestArg* arg) { |
| 2873 | arg->tid = gettid(); |
| 2874 | ASSERT_EQ(0, pthread_barrier_wait(arg->barrier)); |
Yabin Cui | e7c2fff | 2015-11-05 22:06:09 -0800 | [diff] [blame] | 2875 | } |
| 2876 | |
| 2877 | TEST(pthread, pthread_barrier_destroy) { |
| 2878 | pthread_barrier_t barrier; |
| 2879 | ASSERT_EQ(0, pthread_barrier_init(&barrier, nullptr, 2)); |
| 2880 | pthread_t thread; |
Yabin Cui | 81d2797 | 2016-03-22 13:45:55 -0700 | [diff] [blame] | 2881 | BarrierDestroyTestArg arg; |
Yabin Cui | e7c2fff | 2015-11-05 22:06:09 -0800 | [diff] [blame] | 2882 | arg.tid = 0; |
| 2883 | arg.barrier = &barrier; |
Yabin Cui | e7c2fff | 2015-11-05 22:06:09 -0800 | [diff] [blame] | 2884 | ASSERT_EQ(0, pthread_create(&thread, nullptr, |
Yabin Cui | 81d2797 | 2016-03-22 13:45:55 -0700 | [diff] [blame] | 2885 | reinterpret_cast<void* (*)(void*)>(BarrierDestroyTestHelper), &arg)); |
Yabin Cui | e7c2fff | 2015-11-05 22:06:09 -0800 | [diff] [blame] | 2886 | WaitUntilThreadSleep(arg.tid); |
| 2887 | ASSERT_EQ(EBUSY, pthread_barrier_destroy(&barrier)); |
| 2888 | ASSERT_EQ(PTHREAD_BARRIER_SERIAL_THREAD, pthread_barrier_wait(&barrier)); |
| 2889 | // Verify if the barrier can be destroyed directly after pthread_barrier_wait(). |
| 2890 | ASSERT_EQ(0, pthread_barrier_destroy(&barrier)); |
| 2891 | ASSERT_EQ(0, pthread_join(thread, nullptr)); |
| 2892 | #if defined(__BIONIC__) |
| 2893 | ASSERT_EQ(EINVAL, pthread_barrier_destroy(&barrier)); |
| 2894 | #endif |
| 2895 | } |
| 2896 | |
| 2897 | struct BarrierOrderingTestHelperArg { |
| 2898 | pthread_barrier_t* barrier; |
| 2899 | size_t* array; |
| 2900 | size_t array_length; |
| 2901 | size_t id; |
| 2902 | }; |
| 2903 | |
| 2904 | void BarrierOrderingTestHelper(BarrierOrderingTestHelperArg* arg) { |
| 2905 | const size_t ITERATION_COUNT = 10000; |
| 2906 | for (size_t i = 1; i <= ITERATION_COUNT; ++i) { |
| 2907 | arg->array[arg->id] = i; |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 2908 | int result = pthread_barrier_wait(arg->barrier); |
| 2909 | ASSERT_TRUE(result == 0 || result == PTHREAD_BARRIER_SERIAL_THREAD); |
Yabin Cui | e7c2fff | 2015-11-05 22:06:09 -0800 | [diff] [blame] | 2910 | for (size_t j = 0; j < arg->array_length; ++j) { |
| 2911 | ASSERT_EQ(i, arg->array[j]); |
| 2912 | } |
Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 2913 | result = pthread_barrier_wait(arg->barrier); |
| 2914 | ASSERT_TRUE(result == 0 || result == PTHREAD_BARRIER_SERIAL_THREAD); |
Yabin Cui | e7c2fff | 2015-11-05 22:06:09 -0800 | [diff] [blame] | 2915 | } |
| 2916 | } |
| 2917 | |
| 2918 | TEST(pthread, pthread_barrier_check_ordering) { |
| 2919 | const size_t THREAD_COUNT = 4; |
| 2920 | pthread_barrier_t barrier; |
| 2921 | ASSERT_EQ(0, pthread_barrier_init(&barrier, nullptr, THREAD_COUNT)); |
| 2922 | size_t array[THREAD_COUNT]; |
| 2923 | std::vector<pthread_t> threads(THREAD_COUNT); |
| 2924 | std::vector<BarrierOrderingTestHelperArg> args(THREAD_COUNT); |
| 2925 | for (size_t i = 0; i < THREAD_COUNT; ++i) { |
| 2926 | args[i].barrier = &barrier; |
| 2927 | args[i].array = array; |
| 2928 | args[i].array_length = THREAD_COUNT; |
| 2929 | args[i].id = i; |
| 2930 | ASSERT_EQ(0, pthread_create(&threads[i], nullptr, |
| 2931 | reinterpret_cast<void* (*)(void*)>(BarrierOrderingTestHelper), |
| 2932 | &args[i])); |
| 2933 | } |
| 2934 | for (size_t i = 0; i < THREAD_COUNT; ++i) { |
| 2935 | ASSERT_EQ(0, pthread_join(threads[i], nullptr)); |
| 2936 | } |
| 2937 | } |
Yabin Cui | fe3a83a | 2015-11-17 16:03:18 -0800 | [diff] [blame] | 2938 | |
Elliott Hughes | 463faad | 2018-07-06 14:34:49 -0700 | [diff] [blame] | 2939 | TEST(pthread, pthread_barrier_init_zero_count) { |
| 2940 | pthread_barrier_t barrier; |
| 2941 | ASSERT_EQ(EINVAL, pthread_barrier_init(&barrier, nullptr, 0)); |
| 2942 | } |
| 2943 | |
Yabin Cui | fe3a83a | 2015-11-17 16:03:18 -0800 | [diff] [blame] | 2944 | TEST(pthread, pthread_spinlock_smoke) { |
| 2945 | pthread_spinlock_t lock; |
| 2946 | ASSERT_EQ(0, pthread_spin_init(&lock, 0)); |
| 2947 | ASSERT_EQ(0, pthread_spin_trylock(&lock)); |
| 2948 | ASSERT_EQ(0, pthread_spin_unlock(&lock)); |
| 2949 | ASSERT_EQ(0, pthread_spin_lock(&lock)); |
| 2950 | ASSERT_EQ(EBUSY, pthread_spin_trylock(&lock)); |
| 2951 | ASSERT_EQ(0, pthread_spin_unlock(&lock)); |
| 2952 | ASSERT_EQ(0, pthread_spin_destroy(&lock)); |
| 2953 | } |
Elliott Hughes | 53dc9dd | 2017-09-19 14:02:50 -0700 | [diff] [blame] | 2954 | |
Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 2955 | TEST(pthread, pthread_attr_getdetachstate__pthread_attr_setdetachstate) { |
Elliott Hughes | 53dc9dd | 2017-09-19 14:02:50 -0700 | [diff] [blame] | 2956 | pthread_attr_t attr; |
| 2957 | ASSERT_EQ(0, pthread_attr_init(&attr)); |
| 2958 | |
Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 2959 | int state; |
Elliott Hughes | 53dc9dd | 2017-09-19 14:02:50 -0700 | [diff] [blame] | 2960 | ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)); |
Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 2961 | ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &state)); |
| 2962 | ASSERT_EQ(PTHREAD_CREATE_DETACHED, state); |
| 2963 | |
Elliott Hughes | 53dc9dd | 2017-09-19 14:02:50 -0700 | [diff] [blame] | 2964 | ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE)); |
Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 2965 | ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &state)); |
| 2966 | ASSERT_EQ(PTHREAD_CREATE_JOINABLE, state); |
| 2967 | |
Elliott Hughes | 53dc9dd | 2017-09-19 14:02:50 -0700 | [diff] [blame] | 2968 | ASSERT_EQ(EINVAL, pthread_attr_setdetachstate(&attr, 123)); |
Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 2969 | ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &state)); |
| 2970 | ASSERT_EQ(PTHREAD_CREATE_JOINABLE, state); |
Elliott Hughes | 53dc9dd | 2017-09-19 14:02:50 -0700 | [diff] [blame] | 2971 | } |
| 2972 | |
| 2973 | TEST(pthread, pthread_create__mmap_failures) { |
Evgeny Eltsin | b4f7aaa | 2020-06-09 15:49:20 +0200 | [diff] [blame] | 2974 | // After thread is successfully created, native_bridge might need more memory to run it. |
| 2975 | SKIP_WITH_NATIVE_BRIDGE; |
| 2976 | |
Elliott Hughes | 53dc9dd | 2017-09-19 14:02:50 -0700 | [diff] [blame] | 2977 | pthread_attr_t attr; |
| 2978 | ASSERT_EQ(0, pthread_attr_init(&attr)); |
| 2979 | ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)); |
| 2980 | |
| 2981 | const auto kPageSize = sysconf(_SC_PAGE_SIZE); |
| 2982 | |
Elliott Hughes | 5751298 | 2017-10-02 22:49:18 -0700 | [diff] [blame] | 2983 | // Use up all the VMAs. By default this is 64Ki (though some will already be in use). |
Elliott Hughes | 53dc9dd | 2017-09-19 14:02:50 -0700 | [diff] [blame] | 2984 | std::vector<void*> pages; |
Elliott Hughes | 5751298 | 2017-10-02 22:49:18 -0700 | [diff] [blame] | 2985 | pages.reserve(64 * 1024); |
Elliott Hughes | 53dc9dd | 2017-09-19 14:02:50 -0700 | [diff] [blame] | 2986 | int prot = PROT_NONE; |
| 2987 | while (true) { |
| 2988 | void* page = mmap(nullptr, kPageSize, prot, MAP_ANON|MAP_PRIVATE, -1, 0); |
| 2989 | if (page == MAP_FAILED) break; |
| 2990 | pages.push_back(page); |
| 2991 | prot = (prot == PROT_NONE) ? PROT_READ : PROT_NONE; |
| 2992 | } |
| 2993 | |
| 2994 | // Try creating threads, freeing up a page each time we fail. |
| 2995 | size_t EAGAIN_count = 0; |
| 2996 | size_t i = 0; |
| 2997 | for (; i < pages.size(); ++i) { |
| 2998 | pthread_t t; |
| 2999 | int status = pthread_create(&t, &attr, IdFn, nullptr); |
| 3000 | if (status != EAGAIN) break; |
| 3001 | ++EAGAIN_count; |
| 3002 | ASSERT_EQ(0, munmap(pages[i], kPageSize)); |
| 3003 | } |
| 3004 | |
Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame] | 3005 | // Creating a thread uses at least three VMAs: the combined stack and TLS, and a guard on each |
| 3006 | // side. So we should have seen at least three failures. |
| 3007 | ASSERT_GE(EAGAIN_count, 3U); |
Elliott Hughes | 53dc9dd | 2017-09-19 14:02:50 -0700 | [diff] [blame] | 3008 | |
| 3009 | for (; i < pages.size(); ++i) { |
| 3010 | ASSERT_EQ(0, munmap(pages[i], kPageSize)); |
| 3011 | } |
| 3012 | } |
Elliott Hughes | dff08ce | 2017-10-16 09:58:45 -0700 | [diff] [blame] | 3013 | |
| 3014 | TEST(pthread, pthread_setschedparam) { |
| 3015 | sched_param p = { .sched_priority = INT_MIN }; |
| 3016 | ASSERT_EQ(EINVAL, pthread_setschedparam(pthread_self(), INT_MIN, &p)); |
| 3017 | } |
| 3018 | |
| 3019 | TEST(pthread, pthread_setschedprio) { |
| 3020 | ASSERT_EQ(EINVAL, pthread_setschedprio(pthread_self(), INT_MIN)); |
| 3021 | } |
Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 3022 | |
| 3023 | TEST(pthread, pthread_attr_getinheritsched__pthread_attr_setinheritsched) { |
| 3024 | pthread_attr_t attr; |
| 3025 | ASSERT_EQ(0, pthread_attr_init(&attr)); |
| 3026 | |
| 3027 | int state; |
| 3028 | ASSERT_EQ(0, pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED)); |
| 3029 | ASSERT_EQ(0, pthread_attr_getinheritsched(&attr, &state)); |
| 3030 | ASSERT_EQ(PTHREAD_INHERIT_SCHED, state); |
| 3031 | |
| 3032 | ASSERT_EQ(0, pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED)); |
| 3033 | ASSERT_EQ(0, pthread_attr_getinheritsched(&attr, &state)); |
| 3034 | ASSERT_EQ(PTHREAD_EXPLICIT_SCHED, state); |
| 3035 | |
| 3036 | ASSERT_EQ(EINVAL, pthread_attr_setinheritsched(&attr, 123)); |
| 3037 | ASSERT_EQ(0, pthread_attr_getinheritsched(&attr, &state)); |
| 3038 | ASSERT_EQ(PTHREAD_EXPLICIT_SCHED, state); |
| 3039 | } |
| 3040 | |
| 3041 | TEST(pthread, pthread_attr_setinheritsched__PTHREAD_INHERIT_SCHED__PTHREAD_EXPLICIT_SCHED) { |
| 3042 | pthread_attr_t attr; |
| 3043 | ASSERT_EQ(0, pthread_attr_init(&attr)); |
| 3044 | |
| 3045 | // If we set invalid scheduling attributes but choose to inherit, everything's fine... |
| 3046 | sched_param param = { .sched_priority = sched_get_priority_max(SCHED_FIFO) + 1 }; |
| 3047 | ASSERT_EQ(0, pthread_attr_setschedparam(&attr, ¶m)); |
| 3048 | ASSERT_EQ(0, pthread_attr_setschedpolicy(&attr, SCHED_FIFO)); |
| 3049 | ASSERT_EQ(0, pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED)); |
| 3050 | |
| 3051 | pthread_t t; |
| 3052 | ASSERT_EQ(0, pthread_create(&t, &attr, IdFn, nullptr)); |
| 3053 | ASSERT_EQ(0, pthread_join(t, nullptr)); |
| 3054 | |
Elliott Hughes | 7a66066 | 2017-10-30 09:26:06 -0700 | [diff] [blame] | 3055 | #if defined(__LP64__) |
| 3056 | // If we ask to use them, though, we'll see a failure... |
Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 3057 | ASSERT_EQ(0, pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED)); |
| 3058 | ASSERT_EQ(EINVAL, pthread_create(&t, &attr, IdFn, nullptr)); |
Elliott Hughes | 7a66066 | 2017-10-30 09:26:06 -0700 | [diff] [blame] | 3059 | #else |
| 3060 | // For backwards compatibility with broken apps, we just ignore failures |
| 3061 | // to set scheduler attributes on LP32. |
| 3062 | #endif |
Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 3063 | } |
| 3064 | |
| 3065 | TEST(pthread, pthread_attr_setinheritsched_PTHREAD_INHERIT_SCHED_takes_effect) { |
| 3066 | sched_param param = { .sched_priority = sched_get_priority_min(SCHED_FIFO) }; |
| 3067 | int rc = pthread_setschedparam(pthread_self(), SCHED_FIFO, ¶m); |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 3068 | if (rc == EPERM) GTEST_SKIP() << "pthread_setschedparam failed with EPERM"; |
Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 3069 | ASSERT_EQ(0, rc); |
| 3070 | |
| 3071 | pthread_attr_t attr; |
| 3072 | ASSERT_EQ(0, pthread_attr_init(&attr)); |
| 3073 | ASSERT_EQ(0, pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED)); |
| 3074 | |
Elliott Hughes | 0bd9d13 | 2017-11-02 13:11:13 -0700 | [diff] [blame] | 3075 | SpinFunctionHelper spin_helper; |
Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 3076 | pthread_t t; |
Elliott Hughes | 0bd9d13 | 2017-11-02 13:11:13 -0700 | [diff] [blame] | 3077 | ASSERT_EQ(0, pthread_create(&t, &attr, spin_helper.GetFunction(), nullptr)); |
Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 3078 | int actual_policy; |
| 3079 | sched_param actual_param; |
| 3080 | ASSERT_EQ(0, pthread_getschedparam(t, &actual_policy, &actual_param)); |
| 3081 | ASSERT_EQ(SCHED_FIFO, actual_policy); |
Elliott Hughes | 0bd9d13 | 2017-11-02 13:11:13 -0700 | [diff] [blame] | 3082 | spin_helper.UnSpin(); |
Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 3083 | ASSERT_EQ(0, pthread_join(t, nullptr)); |
| 3084 | } |
| 3085 | |
| 3086 | TEST(pthread, pthread_attr_setinheritsched_PTHREAD_EXPLICIT_SCHED_takes_effect) { |
| 3087 | sched_param param = { .sched_priority = sched_get_priority_min(SCHED_FIFO) }; |
| 3088 | int rc = pthread_setschedparam(pthread_self(), SCHED_FIFO, ¶m); |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 3089 | if (rc == EPERM) GTEST_SKIP() << "pthread_setschedparam failed with EPERM"; |
Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 3090 | ASSERT_EQ(0, rc); |
| 3091 | |
| 3092 | pthread_attr_t attr; |
| 3093 | ASSERT_EQ(0, pthread_attr_init(&attr)); |
| 3094 | ASSERT_EQ(0, pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED)); |
| 3095 | ASSERT_EQ(0, pthread_attr_setschedpolicy(&attr, SCHED_OTHER)); |
| 3096 | |
Elliott Hughes | 0bd9d13 | 2017-11-02 13:11:13 -0700 | [diff] [blame] | 3097 | SpinFunctionHelper spin_helper; |
Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 3098 | pthread_t t; |
Elliott Hughes | 0bd9d13 | 2017-11-02 13:11:13 -0700 | [diff] [blame] | 3099 | ASSERT_EQ(0, pthread_create(&t, &attr, spin_helper.GetFunction(), nullptr)); |
Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 3100 | int actual_policy; |
| 3101 | sched_param actual_param; |
| 3102 | ASSERT_EQ(0, pthread_getschedparam(t, &actual_policy, &actual_param)); |
| 3103 | ASSERT_EQ(SCHED_OTHER, actual_policy); |
Elliott Hughes | 0bd9d13 | 2017-11-02 13:11:13 -0700 | [diff] [blame] | 3104 | spin_helper.UnSpin(); |
Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 3105 | ASSERT_EQ(0, pthread_join(t, nullptr)); |
| 3106 | } |
| 3107 | |
| 3108 | TEST(pthread, pthread_attr_setinheritsched__takes_effect_despite_SCHED_RESET_ON_FORK) { |
| 3109 | sched_param param = { .sched_priority = sched_get_priority_min(SCHED_FIFO) }; |
| 3110 | int rc = pthread_setschedparam(pthread_self(), SCHED_FIFO | SCHED_RESET_ON_FORK, ¶m); |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 3111 | if (rc == EPERM) GTEST_SKIP() << "pthread_setschedparam failed with EPERM"; |
Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 3112 | ASSERT_EQ(0, rc); |
| 3113 | |
| 3114 | pthread_attr_t attr; |
| 3115 | ASSERT_EQ(0, pthread_attr_init(&attr)); |
| 3116 | ASSERT_EQ(0, pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED)); |
| 3117 | |
Elliott Hughes | 0bd9d13 | 2017-11-02 13:11:13 -0700 | [diff] [blame] | 3118 | SpinFunctionHelper spin_helper; |
Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 3119 | pthread_t t; |
Elliott Hughes | 0bd9d13 | 2017-11-02 13:11:13 -0700 | [diff] [blame] | 3120 | ASSERT_EQ(0, pthread_create(&t, &attr, spin_helper.GetFunction(), nullptr)); |
Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 3121 | int actual_policy; |
| 3122 | sched_param actual_param; |
| 3123 | ASSERT_EQ(0, pthread_getschedparam(t, &actual_policy, &actual_param)); |
| 3124 | ASSERT_EQ(SCHED_FIFO | SCHED_RESET_ON_FORK, actual_policy); |
Elliott Hughes | 0bd9d13 | 2017-11-02 13:11:13 -0700 | [diff] [blame] | 3125 | spin_helper.UnSpin(); |
Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 3126 | ASSERT_EQ(0, pthread_join(t, nullptr)); |
| 3127 | } |
Peter Collingbourne | 5d3aa86 | 2020-09-11 15:05:17 -0700 | [diff] [blame] | 3128 | |
| 3129 | extern "C" bool android_run_on_all_threads(bool (*func)(void*), void* arg); |
| 3130 | |
| 3131 | TEST(pthread, run_on_all_threads) { |
| 3132 | #if defined(__BIONIC__) |
| 3133 | pthread_t t; |
| 3134 | ASSERT_EQ( |
| 3135 | 0, pthread_create( |
| 3136 | &t, nullptr, |
| 3137 | [](void*) -> void* { |
| 3138 | pthread_attr_t detached; |
| 3139 | if (pthread_attr_init(&detached) != 0 || |
| 3140 | pthread_attr_setdetachstate(&detached, PTHREAD_CREATE_DETACHED) != 0) { |
| 3141 | return reinterpret_cast<void*>(errno); |
| 3142 | } |
| 3143 | |
| 3144 | for (int i = 0; i != 1000; ++i) { |
| 3145 | pthread_t t1, t2; |
| 3146 | if (pthread_create( |
| 3147 | &t1, &detached, [](void*) -> void* { return nullptr; }, nullptr) != 0 || |
| 3148 | pthread_create( |
| 3149 | &t2, nullptr, [](void*) -> void* { return nullptr; }, nullptr) != 0 || |
| 3150 | pthread_join(t2, nullptr) != 0) { |
| 3151 | return reinterpret_cast<void*>(errno); |
| 3152 | } |
| 3153 | } |
| 3154 | |
| 3155 | if (pthread_attr_destroy(&detached) != 0) { |
| 3156 | return reinterpret_cast<void*>(errno); |
| 3157 | } |
| 3158 | return nullptr; |
| 3159 | }, |
| 3160 | nullptr)); |
| 3161 | |
| 3162 | for (int i = 0; i != 1000; ++i) { |
| 3163 | ASSERT_TRUE(android_run_on_all_threads([](void* arg) { return arg == nullptr; }, nullptr)); |
| 3164 | } |
| 3165 | |
| 3166 | void *retval; |
| 3167 | ASSERT_EQ(0, pthread_join(t, &retval)); |
| 3168 | ASSERT_EQ(nullptr, retval); |
| 3169 | #else |
| 3170 | GTEST_SKIP() << "bionic-only test"; |
| 3171 | #endif |
| 3172 | } |
Elliott Hughes | e117d6e | 2024-11-14 17:31:13 +0000 | [diff] [blame] | 3173 | |
| 3174 | TEST(pthread, pthread_getaffinity_np_failure) { |
| 3175 | // Trivial test of the errno-preserving/returning behavior. |
| 3176 | #pragma clang diagnostic push |
| 3177 | #pragma clang diagnostic ignored "-Wnonnull" |
| 3178 | errno = 0; |
| 3179 | ASSERT_EQ(EINVAL, pthread_getaffinity_np(pthread_self(), 0, nullptr)); |
| 3180 | ASSERT_ERRNO(0); |
| 3181 | #pragma clang diagnostic pop |
| 3182 | } |
| 3183 | |
Elliott Hughes | d354c42 | 2024-11-18 14:49:45 +0000 | [diff] [blame] | 3184 | TEST(pthread, pthread_getaffinity) { |
| 3185 | cpu_set_t set; |
| 3186 | CPU_ZERO(&set); |
| 3187 | ASSERT_EQ(0, pthread_getaffinity_np(pthread_self(), sizeof(set), &set)); |
| 3188 | ASSERT_GT(CPU_COUNT(&set), 0); |
| 3189 | } |
| 3190 | |
Elliott Hughes | e117d6e | 2024-11-14 17:31:13 +0000 | [diff] [blame] | 3191 | TEST(pthread, pthread_setaffinity_np_failure) { |
| 3192 | // Trivial test of the errno-preserving/returning behavior. |
| 3193 | #pragma clang diagnostic push |
| 3194 | #pragma clang diagnostic ignored "-Wnonnull" |
| 3195 | errno = 0; |
| 3196 | ASSERT_EQ(EINVAL, pthread_setaffinity_np(pthread_self(), 0, nullptr)); |
| 3197 | ASSERT_ERRNO(0); |
| 3198 | #pragma clang diagnostic pop |
| 3199 | } |
Elliott Hughes | fab5e6f | 2024-11-18 21:37:20 +0000 | [diff] [blame] | 3200 | |
| 3201 | TEST(pthread, pthread_setaffinity) { |
| 3202 | cpu_set_t set; |
| 3203 | CPU_ZERO(&set); |
| 3204 | ASSERT_EQ(0, pthread_getaffinity_np(pthread_self(), sizeof(set), &set)); |
| 3205 | // It's hard to make any more general claim than this, |
| 3206 | // but it ought to be safe to ask for the same affinity you already have. |
| 3207 | ASSERT_EQ(0, pthread_setaffinity_np(pthread_self(), sizeof(set), &set)); |
| 3208 | } |