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