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