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