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 | |
Yabin Cui | 9df7040 | 2014-11-05 18:01:01 -0800 | [diff] [blame] | 19 | #include "private/ScopeGuard.h" |
| 20 | #include "BionicDeathTest.h" |
| 21 | #include "ScopedSignalHandler.h" |
Dmitriy Ivanov | 00e3781 | 2014-11-20 16:53:47 -0800 | [diff] [blame] | 22 | #include "gtest_ex.h" |
Yabin Cui | 9df7040 | 2014-11-05 18:01:01 -0800 | [diff] [blame] | 23 | |
Elliott Hughes | bfeab1b | 2012-09-05 17:47:37 -0700 | [diff] [blame] | 24 | #include <errno.h> |
Elliott Hughes | 5b9310e | 2013-10-02 16:59:05 -0700 | [diff] [blame] | 25 | #include <inttypes.h> |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 26 | #include <limits.h> |
Elliott Hughes | 04620a3 | 2014-03-07 17:59:05 -0800 | [diff] [blame] | 27 | #include <malloc.h> |
Elliott Hughes | bfeab1b | 2012-09-05 17:47:37 -0700 | [diff] [blame] | 28 | #include <pthread.h> |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 29 | #include <signal.h> |
Elliott Hughes | 70b24b1 | 2013-11-15 11:51:07 -0800 | [diff] [blame] | 30 | #include <sys/mman.h> |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 31 | #include <sys/syscall.h> |
Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 32 | #include <time.h> |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 33 | #include <unistd.h> |
Elliott Hughes | bfeab1b | 2012-09-05 17:47:37 -0700 | [diff] [blame] | 34 | |
| 35 | TEST(pthread, pthread_key_create) { |
| 36 | pthread_key_t key; |
| 37 | ASSERT_EQ(0, pthread_key_create(&key, NULL)); |
| 38 | ASSERT_EQ(0, pthread_key_delete(key)); |
| 39 | // Can't delete a key that's already been deleted. |
| 40 | ASSERT_EQ(EINVAL, pthread_key_delete(key)); |
| 41 | } |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 42 | |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 43 | TEST(pthread, pthread_keys_max) { |
Yabin Cui | 6c238f2 | 2014-12-11 20:50:41 -0800 | [diff] [blame] | 44 | // POSIX says PTHREAD_KEYS_MAX should be at least _POSIX_THREAD_KEYS_MAX. |
| 45 | ASSERT_GE(PTHREAD_KEYS_MAX, _POSIX_THREAD_KEYS_MAX); |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 46 | } |
Elliott Hughes | 718a5b5 | 2014-01-28 17:02:03 -0800 | [diff] [blame] | 47 | |
Yabin Cui | 6c238f2 | 2014-12-11 20:50:41 -0800 | [diff] [blame] | 48 | TEST(pthread, sysconf_SC_THREAD_KEYS_MAX_eq_PTHREAD_KEYS_MAX) { |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 49 | int sysconf_max = sysconf(_SC_THREAD_KEYS_MAX); |
Yabin Cui | 6c238f2 | 2014-12-11 20:50:41 -0800 | [diff] [blame] | 50 | ASSERT_EQ(sysconf_max, PTHREAD_KEYS_MAX); |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | TEST(pthread, pthread_key_many_distinct) { |
Yabin Cui | 6c238f2 | 2014-12-11 20:50:41 -0800 | [diff] [blame] | 54 | // As gtest uses pthread keys, we can't allocate exactly PTHREAD_KEYS_MAX |
| 55 | // pthread keys, but We should be able to allocate at least this many keys. |
| 56 | int nkeys = PTHREAD_KEYS_MAX / 2; |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 57 | std::vector<pthread_key_t> keys; |
| 58 | |
| 59 | auto scope_guard = make_scope_guard([&keys]{ |
| 60 | for (auto key : keys) { |
| 61 | EXPECT_EQ(0, pthread_key_delete(key)); |
| 62 | } |
| 63 | }); |
| 64 | |
| 65 | for (int i = 0; i < nkeys; ++i) { |
| 66 | pthread_key_t key; |
| 67 | // If this fails, it's likely that GLOBAL_INIT_THREAD_LOCAL_BUFFER_COUNT is |
| 68 | // wrong. |
| 69 | ASSERT_EQ(0, pthread_key_create(&key, NULL)) << i << " of " << nkeys; |
| 70 | keys.push_back(key); |
| 71 | ASSERT_EQ(0, pthread_setspecific(key, reinterpret_cast<void*>(i))); |
| 72 | } |
| 73 | |
| 74 | for (int i = keys.size() - 1; i >= 0; --i) { |
| 75 | ASSERT_EQ(reinterpret_cast<void*>(i), pthread_getspecific(keys.back())); |
| 76 | pthread_key_t key = keys.back(); |
| 77 | keys.pop_back(); |
| 78 | ASSERT_EQ(0, pthread_key_delete(key)); |
| 79 | } |
| 80 | } |
| 81 | |
Yabin Cui | 6c238f2 | 2014-12-11 20:50:41 -0800 | [diff] [blame] | 82 | TEST(pthread, pthread_key_not_exceed_PTHREAD_KEYS_MAX) { |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 83 | std::vector<pthread_key_t> keys; |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 84 | int rv = 0; |
Yabin Cui | 6c238f2 | 2014-12-11 20:50:41 -0800 | [diff] [blame] | 85 | |
| 86 | // Pthread keys are used by gtest, so PTHREAD_KEYS_MAX should |
| 87 | // be more than we are allowed to allocate now. |
| 88 | for (int i = 0; i < PTHREAD_KEYS_MAX; i++) { |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 89 | pthread_key_t key; |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 90 | rv = pthread_key_create(&key, NULL); |
| 91 | if (rv == EAGAIN) { |
| 92 | break; |
| 93 | } |
| 94 | EXPECT_EQ(0, rv); |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 95 | keys.push_back(key); |
| 96 | } |
| 97 | |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 98 | // Don't leak keys. |
| 99 | for (auto key : keys) { |
| 100 | EXPECT_EQ(0, pthread_key_delete(key)); |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 101 | } |
Dan Albert | c4bcc75 | 2014-09-30 11:48:24 -0700 | [diff] [blame] | 102 | keys.clear(); |
| 103 | |
| 104 | // We should have eventually reached the maximum number of keys and received |
| 105 | // EAGAIN. |
| 106 | ASSERT_EQ(EAGAIN, rv); |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Elliott Hughes | ebb770f | 2014-06-25 13:46:46 -0700 | [diff] [blame] | 109 | TEST(pthread, pthread_key_delete) { |
| 110 | void* expected = reinterpret_cast<void*>(1234); |
| 111 | pthread_key_t key; |
| 112 | ASSERT_EQ(0, pthread_key_create(&key, NULL)); |
| 113 | ASSERT_EQ(0, pthread_setspecific(key, expected)); |
| 114 | ASSERT_EQ(expected, pthread_getspecific(key)); |
| 115 | ASSERT_EQ(0, pthread_key_delete(key)); |
| 116 | // After deletion, pthread_getspecific returns NULL. |
| 117 | ASSERT_EQ(NULL, pthread_getspecific(key)); |
| 118 | // And you can't use pthread_setspecific with the deleted key. |
| 119 | ASSERT_EQ(EINVAL, pthread_setspecific(key, expected)); |
| 120 | } |
| 121 | |
Elliott Hughes | 40a5217 | 2014-07-30 14:48:10 -0700 | [diff] [blame] | 122 | TEST(pthread, pthread_key_fork) { |
| 123 | void* expected = reinterpret_cast<void*>(1234); |
| 124 | pthread_key_t key; |
| 125 | ASSERT_EQ(0, pthread_key_create(&key, NULL)); |
| 126 | ASSERT_EQ(0, pthread_setspecific(key, expected)); |
| 127 | ASSERT_EQ(expected, pthread_getspecific(key)); |
| 128 | |
| 129 | pid_t pid = fork(); |
| 130 | ASSERT_NE(-1, pid) << strerror(errno); |
| 131 | |
| 132 | if (pid == 0) { |
| 133 | // The surviving thread inherits all the forking thread's TLS values... |
| 134 | ASSERT_EQ(expected, pthread_getspecific(key)); |
| 135 | _exit(99); |
| 136 | } |
| 137 | |
| 138 | int status; |
| 139 | ASSERT_EQ(pid, waitpid(pid, &status, 0)); |
| 140 | ASSERT_TRUE(WIFEXITED(status)); |
| 141 | ASSERT_EQ(99, WEXITSTATUS(status)); |
| 142 | |
| 143 | ASSERT_EQ(expected, pthread_getspecific(key)); |
Dan Albert | 1d53ae2 | 2014-09-02 15:24:26 -0700 | [diff] [blame] | 144 | ASSERT_EQ(0, pthread_key_delete(key)); |
Elliott Hughes | 40a5217 | 2014-07-30 14:48:10 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | static void* DirtyKeyFn(void* key) { |
| 148 | return pthread_getspecific(*reinterpret_cast<pthread_key_t*>(key)); |
| 149 | } |
| 150 | |
| 151 | TEST(pthread, pthread_key_dirty) { |
| 152 | pthread_key_t key; |
| 153 | ASSERT_EQ(0, pthread_key_create(&key, NULL)); |
| 154 | |
| 155 | size_t stack_size = 128 * 1024; |
| 156 | void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); |
| 157 | ASSERT_NE(MAP_FAILED, stack); |
| 158 | memset(stack, 0xff, stack_size); |
| 159 | |
| 160 | pthread_attr_t attr; |
| 161 | ASSERT_EQ(0, pthread_attr_init(&attr)); |
| 162 | ASSERT_EQ(0, pthread_attr_setstack(&attr, stack, stack_size)); |
| 163 | |
| 164 | pthread_t t; |
| 165 | ASSERT_EQ(0, pthread_create(&t, &attr, DirtyKeyFn, &key)); |
| 166 | |
| 167 | void* result; |
| 168 | ASSERT_EQ(0, pthread_join(t, &result)); |
| 169 | ASSERT_EQ(nullptr, result); // Not ~0! |
| 170 | |
| 171 | ASSERT_EQ(0, munmap(stack, stack_size)); |
Dan Albert | 1d53ae2 | 2014-09-02 15:24:26 -0700 | [diff] [blame] | 172 | ASSERT_EQ(0, pthread_key_delete(key)); |
Elliott Hughes | 40a5217 | 2014-07-30 14:48:10 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 175 | static void* IdFn(void* arg) { |
| 176 | return arg; |
| 177 | } |
| 178 | |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 179 | class SpinFunctionHelper { |
| 180 | public: |
| 181 | SpinFunctionHelper() { |
| 182 | SpinFunctionHelper::spin_flag_ = true; |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 183 | } |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 184 | ~SpinFunctionHelper() { |
| 185 | UnSpin(); |
| 186 | } |
| 187 | auto GetFunction() -> void* (*)(void*) { |
| 188 | return SpinFunctionHelper::SpinFn; |
| 189 | } |
| 190 | |
| 191 | void UnSpin() { |
| 192 | SpinFunctionHelper::spin_flag_ = false; |
| 193 | } |
| 194 | |
| 195 | private: |
| 196 | static void* SpinFn(void*) { |
| 197 | while (spin_flag_) {} |
| 198 | return NULL; |
| 199 | } |
| 200 | static volatile bool spin_flag_; |
| 201 | }; |
| 202 | |
| 203 | // It doesn't matter if spin_flag_ is used in several tests, |
| 204 | // because it is always set to false after each test. Each thread |
| 205 | // loops on spin_flag_ can find it becomes false at some time. |
| 206 | volatile bool SpinFunctionHelper::spin_flag_ = false; |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 207 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 208 | static void* JoinFn(void* arg) { |
| 209 | return reinterpret_cast<void*>(pthread_join(reinterpret_cast<pthread_t>(arg), NULL)); |
| 210 | } |
| 211 | |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 212 | static void AssertDetached(pthread_t t, bool is_detached) { |
| 213 | pthread_attr_t attr; |
| 214 | ASSERT_EQ(0, pthread_getattr_np(t, &attr)); |
| 215 | int detach_state; |
| 216 | ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &detach_state)); |
| 217 | pthread_attr_destroy(&attr); |
| 218 | ASSERT_EQ(is_detached, (detach_state == PTHREAD_CREATE_DETACHED)); |
| 219 | } |
| 220 | |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 221 | static void MakeDeadThread(pthread_t& t) { |
| 222 | ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, NULL)); |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 223 | ASSERT_EQ(0, pthread_join(t, NULL)); |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 224 | } |
| 225 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 226 | TEST(pthread, pthread_create) { |
| 227 | void* expected_result = reinterpret_cast<void*>(123); |
| 228 | // Can we create a thread? |
| 229 | pthread_t t; |
| 230 | ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, expected_result)); |
| 231 | // If we join, do we get the expected value back? |
| 232 | void* result; |
| 233 | ASSERT_EQ(0, pthread_join(t, &result)); |
| 234 | ASSERT_EQ(expected_result, result); |
| 235 | } |
| 236 | |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 237 | TEST(pthread, pthread_create_EAGAIN) { |
| 238 | pthread_attr_t attributes; |
| 239 | ASSERT_EQ(0, pthread_attr_init(&attributes)); |
| 240 | ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, static_cast<size_t>(-1) & ~(getpagesize() - 1))); |
| 241 | |
| 242 | pthread_t t; |
| 243 | ASSERT_EQ(EAGAIN, pthread_create(&t, &attributes, IdFn, NULL)); |
| 244 | } |
| 245 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 246 | TEST(pthread, pthread_no_join_after_detach) { |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 247 | SpinFunctionHelper spinhelper; |
| 248 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 249 | pthread_t t1; |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 250 | ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL)); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 251 | |
| 252 | // After a pthread_detach... |
| 253 | ASSERT_EQ(0, pthread_detach(t1)); |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 254 | AssertDetached(t1, true); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 255 | |
| 256 | // ...pthread_join should fail. |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 257 | ASSERT_EQ(EINVAL, pthread_join(t1, NULL)); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | TEST(pthread, pthread_no_op_detach_after_join) { |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 261 | SpinFunctionHelper spinhelper; |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 262 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 263 | pthread_t t1; |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 264 | ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL)); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 265 | |
| 266 | // If thread 2 is already waiting to join thread 1... |
| 267 | pthread_t t2; |
| 268 | ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1))); |
| 269 | |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 270 | sleep(1); // (Give t2 a chance to call pthread_join.) |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 271 | |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 272 | // ...a call to pthread_detach on thread 1 will "succeed" (silently fail)... |
| 273 | ASSERT_EQ(0, pthread_detach(t1)); |
| 274 | AssertDetached(t1, false); |
| 275 | |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 276 | spinhelper.UnSpin(); |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 277 | |
| 278 | // ...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] | 279 | void* join_result; |
| 280 | ASSERT_EQ(0, pthread_join(t2, &join_result)); |
Elliott Hughes | 5b9310e | 2013-10-02 16:59:05 -0700 | [diff] [blame] | 281 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result)); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 282 | } |
Elliott Hughes | 14f1959 | 2012-10-29 10:19:44 -0700 | [diff] [blame] | 283 | |
| 284 | TEST(pthread, pthread_join_self) { |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 285 | ASSERT_EQ(EDEADLK, pthread_join(pthread_self(), NULL)); |
Elliott Hughes | 14f1959 | 2012-10-29 10:19:44 -0700 | [diff] [blame] | 286 | } |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 287 | |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 288 | struct TestBug37410 { |
| 289 | pthread_t main_thread; |
| 290 | pthread_mutex_t mutex; |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 291 | |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 292 | static void main() { |
| 293 | TestBug37410 data; |
| 294 | data.main_thread = pthread_self(); |
| 295 | ASSERT_EQ(0, pthread_mutex_init(&data.mutex, NULL)); |
| 296 | ASSERT_EQ(0, pthread_mutex_lock(&data.mutex)); |
| 297 | |
| 298 | pthread_t t; |
| 299 | ASSERT_EQ(0, pthread_create(&t, NULL, TestBug37410::thread_fn, reinterpret_cast<void*>(&data))); |
| 300 | |
| 301 | // Wait for the thread to be running... |
| 302 | ASSERT_EQ(0, pthread_mutex_lock(&data.mutex)); |
| 303 | ASSERT_EQ(0, pthread_mutex_unlock(&data.mutex)); |
| 304 | |
| 305 | // ...and exit. |
| 306 | pthread_exit(NULL); |
| 307 | } |
| 308 | |
| 309 | private: |
| 310 | static void* thread_fn(void* arg) { |
| 311 | TestBug37410* data = reinterpret_cast<TestBug37410*>(arg); |
| 312 | |
| 313 | // Let the main thread know we're running. |
| 314 | pthread_mutex_unlock(&data->mutex); |
| 315 | |
| 316 | // And wait for the main thread to exit. |
| 317 | pthread_join(data->main_thread, NULL); |
| 318 | |
| 319 | return NULL; |
| 320 | } |
| 321 | }; |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 322 | |
Elliott Hughes | 7fd803c | 2013-02-14 16:33:52 -0800 | [diff] [blame] | 323 | // Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to |
| 324 | // run this test (which exits normally) in its own process. |
Yabin Cui | 9df7040 | 2014-11-05 18:01:01 -0800 | [diff] [blame] | 325 | |
| 326 | class pthread_DeathTest : public BionicDeathTest {}; |
| 327 | |
| 328 | TEST_F(pthread_DeathTest, pthread_bug_37410) { |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 329 | // http://code.google.com/p/android/issues/detail?id=37410 |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 330 | ASSERT_EXIT(TestBug37410::main(), ::testing::ExitedWithCode(0), ""); |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 331 | } |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 332 | |
| 333 | static void* SignalHandlerFn(void* arg) { |
| 334 | sigset_t wait_set; |
| 335 | sigfillset(&wait_set); |
| 336 | return reinterpret_cast<void*>(sigwait(&wait_set, reinterpret_cast<int*>(arg))); |
| 337 | } |
| 338 | |
| 339 | TEST(pthread, pthread_sigmask) { |
Elliott Hughes | 19e6232 | 2013-10-15 11:23:57 -0700 | [diff] [blame] | 340 | // Check that SIGUSR1 isn't blocked. |
| 341 | sigset_t original_set; |
| 342 | sigemptyset(&original_set); |
| 343 | ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &original_set)); |
| 344 | ASSERT_FALSE(sigismember(&original_set, SIGUSR1)); |
| 345 | |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 346 | // Block SIGUSR1. |
| 347 | sigset_t set; |
| 348 | sigemptyset(&set); |
| 349 | sigaddset(&set, SIGUSR1); |
| 350 | ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &set, NULL)); |
| 351 | |
Elliott Hughes | 19e6232 | 2013-10-15 11:23:57 -0700 | [diff] [blame] | 352 | // Check that SIGUSR1 is blocked. |
| 353 | sigset_t final_set; |
| 354 | sigemptyset(&final_set); |
| 355 | ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &final_set)); |
| 356 | ASSERT_TRUE(sigismember(&final_set, SIGUSR1)); |
| 357 | // ...and that sigprocmask agrees with pthread_sigmask. |
| 358 | sigemptyset(&final_set); |
| 359 | ASSERT_EQ(0, sigprocmask(SIG_BLOCK, NULL, &final_set)); |
| 360 | ASSERT_TRUE(sigismember(&final_set, SIGUSR1)); |
| 361 | |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 362 | // Spawn a thread that calls sigwait and tells us what it received. |
| 363 | pthread_t signal_thread; |
| 364 | int received_signal = -1; |
| 365 | ASSERT_EQ(0, pthread_create(&signal_thread, NULL, SignalHandlerFn, &received_signal)); |
| 366 | |
| 367 | // Send that thread SIGUSR1. |
| 368 | pthread_kill(signal_thread, SIGUSR1); |
| 369 | |
| 370 | // See what it got. |
| 371 | void* join_result; |
| 372 | ASSERT_EQ(0, pthread_join(signal_thread, &join_result)); |
| 373 | ASSERT_EQ(SIGUSR1, received_signal); |
Elliott Hughes | 5b9310e | 2013-10-02 16:59:05 -0700 | [diff] [blame] | 374 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result)); |
Elliott Hughes | 19e6232 | 2013-10-15 11:23:57 -0700 | [diff] [blame] | 375 | |
| 376 | // Restore the original signal mask. |
| 377 | ASSERT_EQ(0, pthread_sigmask(SIG_SETMASK, &original_set, NULL)); |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 378 | } |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 379 | |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 380 | TEST(pthread, pthread_setname_np__too_long) { |
| 381 | ASSERT_EQ(ERANGE, pthread_setname_np(pthread_self(), "this name is far too long for linux")); |
| 382 | } |
| 383 | |
| 384 | TEST(pthread, pthread_setname_np__self) { |
| 385 | ASSERT_EQ(0, pthread_setname_np(pthread_self(), "short 1")); |
| 386 | } |
| 387 | |
| 388 | TEST(pthread, pthread_setname_np__other) { |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 389 | SpinFunctionHelper spinhelper; |
| 390 | |
Elliott Hughes | ed29e85 | 2014-10-27 12:01:51 -0700 | [diff] [blame] | 391 | pthread_t t1; |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 392 | ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL)); |
Elliott Hughes | ed29e85 | 2014-10-27 12:01:51 -0700 | [diff] [blame] | 393 | ASSERT_EQ(0, pthread_setname_np(t1, "short 2")); |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | TEST(pthread, pthread_setname_np__no_such_thread) { |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 397 | pthread_t dead_thread; |
| 398 | MakeDeadThread(dead_thread); |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 399 | |
| 400 | // Call pthread_setname_np after thread has already exited. |
Elliott Hughes | 68d98d8 | 2014-11-12 21:03:26 -0800 | [diff] [blame] | 401 | ASSERT_EQ(ENOENT, pthread_setname_np(dead_thread, "short 3")); |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 402 | } |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 403 | |
| 404 | TEST(pthread, pthread_kill__0) { |
| 405 | // Signal 0 just tests that the thread exists, so it's safe to call on ourselves. |
| 406 | ASSERT_EQ(0, pthread_kill(pthread_self(), 0)); |
| 407 | } |
| 408 | |
| 409 | TEST(pthread, pthread_kill__invalid_signal) { |
| 410 | ASSERT_EQ(EINVAL, pthread_kill(pthread_self(), -1)); |
| 411 | } |
| 412 | |
Elliott Hughes | fae89fc | 2013-02-21 11:22:23 -0800 | [diff] [blame] | 413 | static void pthread_kill__in_signal_handler_helper(int signal_number) { |
| 414 | static int count = 0; |
| 415 | ASSERT_EQ(SIGALRM, signal_number); |
| 416 | if (++count == 1) { |
| 417 | // Can we call pthread_kill from a signal handler? |
| 418 | ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM)); |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | TEST(pthread, pthread_kill__in_signal_handler) { |
Elliott Hughes | 4b558f5 | 2014-03-04 15:58:02 -0800 | [diff] [blame] | 423 | ScopedSignalHandler ssh(SIGALRM, pthread_kill__in_signal_handler_helper); |
Elliott Hughes | fae89fc | 2013-02-21 11:22:23 -0800 | [diff] [blame] | 424 | ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM)); |
| 425 | } |
| 426 | |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 427 | TEST(pthread, pthread_detach__no_such_thread) { |
| 428 | pthread_t dead_thread; |
| 429 | MakeDeadThread(dead_thread); |
| 430 | |
| 431 | ASSERT_EQ(ESRCH, pthread_detach(dead_thread)); |
| 432 | } |
| 433 | |
Elliott Hughes | 04620a3 | 2014-03-07 17:59:05 -0800 | [diff] [blame] | 434 | TEST(pthread, pthread_detach__leak) { |
Christopher Ferris | e380960 | 2014-08-06 14:15:01 -0700 | [diff] [blame] | 435 | size_t initial_bytes = 0; |
| 436 | // Run this loop more than once since the first loop causes some memory |
| 437 | // to be allocated permenantly. Run an extra loop to help catch any subtle |
| 438 | // memory leaks. |
| 439 | for (size_t loop = 0; loop < 3; loop++) { |
| 440 | // Set the initial bytes on the second loop since the memory in use |
| 441 | // should have stabilized. |
| 442 | if (loop == 1) { |
| 443 | initial_bytes = mallinfo().uordblks; |
| 444 | } |
Elliott Hughes | 04620a3 | 2014-03-07 17:59:05 -0800 | [diff] [blame] | 445 | |
Christopher Ferris | e380960 | 2014-08-06 14:15:01 -0700 | [diff] [blame] | 446 | pthread_attr_t attr; |
| 447 | ASSERT_EQ(0, pthread_attr_init(&attr)); |
| 448 | ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE)); |
Elliott Hughes | 04620a3 | 2014-03-07 17:59:05 -0800 | [diff] [blame] | 449 | |
Christopher Ferris | e380960 | 2014-08-06 14:15:01 -0700 | [diff] [blame] | 450 | std::vector<pthread_t> threads; |
| 451 | for (size_t i = 0; i < 32; ++i) { |
| 452 | pthread_t t; |
| 453 | ASSERT_EQ(0, pthread_create(&t, &attr, IdFn, NULL)); |
| 454 | threads.push_back(t); |
| 455 | } |
Elliott Hughes | 04620a3 | 2014-03-07 17:59:05 -0800 | [diff] [blame] | 456 | |
Christopher Ferris | e380960 | 2014-08-06 14:15:01 -0700 | [diff] [blame] | 457 | sleep(1); |
Elliott Hughes | 04620a3 | 2014-03-07 17:59:05 -0800 | [diff] [blame] | 458 | |
Christopher Ferris | e380960 | 2014-08-06 14:15:01 -0700 | [diff] [blame] | 459 | for (size_t i = 0; i < 32; ++i) { |
| 460 | ASSERT_EQ(0, pthread_detach(threads[i])) << i; |
| 461 | } |
Elliott Hughes | 04620a3 | 2014-03-07 17:59:05 -0800 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | size_t final_bytes = mallinfo().uordblks; |
Elliott Hughes | 04620a3 | 2014-03-07 17:59:05 -0800 | [diff] [blame] | 465 | int leaked_bytes = (final_bytes - initial_bytes); |
| 466 | |
| 467 | // User code (like this test) doesn't know how large pthread_internal_t is. |
| 468 | // We can be pretty sure it's more than 128 bytes. |
| 469 | ASSERT_LT(leaked_bytes, 32 /*threads*/ * 128 /*bytes*/); |
| 470 | } |
| 471 | |
Jeff Hao | 9b06cc3 | 2013-08-15 14:51:16 -0700 | [diff] [blame] | 472 | TEST(pthread, pthread_getcpuclockid__clock_gettime) { |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 473 | SpinFunctionHelper spinhelper; |
| 474 | |
Jeff Hao | 9b06cc3 | 2013-08-15 14:51:16 -0700 | [diff] [blame] | 475 | pthread_t t; |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 476 | ASSERT_EQ(0, pthread_create(&t, NULL, spinhelper.GetFunction(), NULL)); |
Jeff Hao | 9b06cc3 | 2013-08-15 14:51:16 -0700 | [diff] [blame] | 477 | |
| 478 | clockid_t c; |
| 479 | ASSERT_EQ(0, pthread_getcpuclockid(t, &c)); |
| 480 | timespec ts; |
| 481 | ASSERT_EQ(0, clock_gettime(c, &ts)); |
| 482 | } |
| 483 | |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 484 | TEST(pthread, pthread_getcpuclockid__no_such_thread) { |
| 485 | pthread_t dead_thread; |
| 486 | MakeDeadThread(dead_thread); |
| 487 | |
| 488 | clockid_t c; |
| 489 | ASSERT_EQ(ESRCH, pthread_getcpuclockid(dead_thread, &c)); |
| 490 | } |
| 491 | |
| 492 | TEST(pthread, pthread_getschedparam__no_such_thread) { |
| 493 | pthread_t dead_thread; |
| 494 | MakeDeadThread(dead_thread); |
| 495 | |
| 496 | int policy; |
| 497 | sched_param param; |
| 498 | ASSERT_EQ(ESRCH, pthread_getschedparam(dead_thread, &policy, ¶m)); |
| 499 | } |
| 500 | |
| 501 | TEST(pthread, pthread_setschedparam__no_such_thread) { |
| 502 | pthread_t dead_thread; |
| 503 | MakeDeadThread(dead_thread); |
| 504 | |
| 505 | int policy = 0; |
| 506 | sched_param param; |
| 507 | ASSERT_EQ(ESRCH, pthread_setschedparam(dead_thread, policy, ¶m)); |
| 508 | } |
| 509 | |
| 510 | TEST(pthread, pthread_join__no_such_thread) { |
| 511 | pthread_t dead_thread; |
| 512 | MakeDeadThread(dead_thread); |
| 513 | |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 514 | ASSERT_EQ(ESRCH, pthread_join(dead_thread, NULL)); |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | TEST(pthread, pthread_kill__no_such_thread) { |
| 518 | pthread_t dead_thread; |
| 519 | MakeDeadThread(dead_thread); |
| 520 | |
| 521 | ASSERT_EQ(ESRCH, pthread_kill(dead_thread, 0)); |
| 522 | } |
msg555 | 0f020d1 | 2013-06-06 14:59:28 -0400 | [diff] [blame] | 523 | |
| 524 | TEST(pthread, pthread_join__multijoin) { |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 525 | SpinFunctionHelper spinhelper; |
msg555 | 0f020d1 | 2013-06-06 14:59:28 -0400 | [diff] [blame] | 526 | |
| 527 | pthread_t t1; |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 528 | ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL)); |
msg555 | 0f020d1 | 2013-06-06 14:59:28 -0400 | [diff] [blame] | 529 | |
| 530 | pthread_t t2; |
| 531 | ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1))); |
| 532 | |
| 533 | sleep(1); // (Give t2 a chance to call pthread_join.) |
| 534 | |
| 535 | // Multiple joins to the same thread should fail. |
| 536 | ASSERT_EQ(EINVAL, pthread_join(t1, NULL)); |
| 537 | |
Yabin Cui | 6348160 | 2014-12-01 17:41:04 -0800 | [diff] [blame] | 538 | spinhelper.UnSpin(); |
msg555 | 0f020d1 | 2013-06-06 14:59:28 -0400 | [diff] [blame] | 539 | |
| 540 | // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes). |
| 541 | void* join_result; |
| 542 | ASSERT_EQ(0, pthread_join(t2, &join_result)); |
Elliott Hughes | 5b9310e | 2013-10-02 16:59:05 -0700 | [diff] [blame] | 543 | ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result)); |
msg555 | 0f020d1 | 2013-06-06 14:59:28 -0400 | [diff] [blame] | 544 | } |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 545 | |
Elliott Hughes | 70b24b1 | 2013-11-15 11:51:07 -0800 | [diff] [blame] | 546 | TEST(pthread, pthread_join__race) { |
| 547 | // http://b/11693195 --- pthread_join could return before the thread had actually exited. |
| 548 | // If the joiner unmapped the thread's stack, that could lead to SIGSEGV in the thread. |
| 549 | for (size_t i = 0; i < 1024; ++i) { |
| 550 | size_t stack_size = 64*1024; |
| 551 | void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0); |
| 552 | |
| 553 | pthread_attr_t a; |
| 554 | pthread_attr_init(&a); |
| 555 | pthread_attr_setstack(&a, stack, stack_size); |
| 556 | |
| 557 | pthread_t t; |
| 558 | ASSERT_EQ(0, pthread_create(&t, &a, IdFn, NULL)); |
| 559 | ASSERT_EQ(0, pthread_join(t, NULL)); |
| 560 | ASSERT_EQ(0, munmap(stack, stack_size)); |
| 561 | } |
| 562 | } |
| 563 | |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 564 | static void* GetActualGuardSizeFn(void* arg) { |
| 565 | pthread_attr_t attributes; |
| 566 | pthread_getattr_np(pthread_self(), &attributes); |
| 567 | pthread_attr_getguardsize(&attributes, reinterpret_cast<size_t*>(arg)); |
| 568 | return NULL; |
| 569 | } |
| 570 | |
| 571 | static size_t GetActualGuardSize(const pthread_attr_t& attributes) { |
| 572 | size_t result; |
| 573 | pthread_t t; |
| 574 | pthread_create(&t, &attributes, GetActualGuardSizeFn, &result); |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 575 | pthread_join(t, NULL); |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 576 | return result; |
| 577 | } |
| 578 | |
| 579 | static void* GetActualStackSizeFn(void* arg) { |
| 580 | pthread_attr_t attributes; |
| 581 | pthread_getattr_np(pthread_self(), &attributes); |
| 582 | pthread_attr_getstacksize(&attributes, reinterpret_cast<size_t*>(arg)); |
| 583 | return NULL; |
| 584 | } |
| 585 | |
| 586 | static size_t GetActualStackSize(const pthread_attr_t& attributes) { |
| 587 | size_t result; |
| 588 | pthread_t t; |
| 589 | pthread_create(&t, &attributes, GetActualStackSizeFn, &result); |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 590 | pthread_join(t, NULL); |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 591 | return result; |
| 592 | } |
| 593 | |
| 594 | TEST(pthread, pthread_attr_setguardsize) { |
| 595 | pthread_attr_t attributes; |
| 596 | ASSERT_EQ(0, pthread_attr_init(&attributes)); |
| 597 | |
| 598 | // Get the default guard size. |
| 599 | size_t default_guard_size; |
| 600 | ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &default_guard_size)); |
| 601 | |
| 602 | // No such thing as too small: will be rounded up to one page by pthread_create. |
| 603 | ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 128)); |
| 604 | size_t guard_size; |
| 605 | ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size)); |
| 606 | ASSERT_EQ(128U, guard_size); |
| 607 | ASSERT_EQ(4096U, GetActualGuardSize(attributes)); |
| 608 | |
| 609 | // Large enough and a multiple of the page size. |
| 610 | ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024)); |
| 611 | ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size)); |
| 612 | ASSERT_EQ(32*1024U, guard_size); |
| 613 | |
| 614 | // Large enough but not a multiple of the page size; will be rounded up by pthread_create. |
| 615 | ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024 + 1)); |
| 616 | ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size)); |
| 617 | ASSERT_EQ(32*1024U + 1, guard_size); |
| 618 | } |
| 619 | |
| 620 | TEST(pthread, pthread_attr_setstacksize) { |
| 621 | pthread_attr_t attributes; |
| 622 | ASSERT_EQ(0, pthread_attr_init(&attributes)); |
| 623 | |
| 624 | // Get the default stack size. |
| 625 | size_t default_stack_size; |
| 626 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &default_stack_size)); |
| 627 | |
| 628 | // Too small. |
| 629 | ASSERT_EQ(EINVAL, pthread_attr_setstacksize(&attributes, 128)); |
| 630 | size_t stack_size; |
| 631 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size)); |
| 632 | ASSERT_EQ(default_stack_size, stack_size); |
| 633 | ASSERT_GE(GetActualStackSize(attributes), default_stack_size); |
| 634 | |
Yabin Cui | 917d390 | 2015-01-08 12:32:42 -0800 | [diff] [blame^] | 635 | // 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] | 636 | ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024)); |
| 637 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size)); |
| 638 | ASSERT_EQ(32*1024U, stack_size); |
Yabin Cui | 917d390 | 2015-01-08 12:32:42 -0800 | [diff] [blame^] | 639 | ASSERT_GE(GetActualStackSize(attributes), 32*1024U); |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 640 | |
Yabin Cui | 917d390 | 2015-01-08 12:32:42 -0800 | [diff] [blame^] | 641 | // Large enough but not aligned; will be rounded up by pthread_create. |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 642 | ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024 + 1)); |
| 643 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size)); |
| 644 | ASSERT_EQ(32*1024U + 1, stack_size); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 645 | #if defined(__BIONIC__) |
Yabin Cui | 917d390 | 2015-01-08 12:32:42 -0800 | [diff] [blame^] | 646 | ASSERT_GT(GetActualStackSize(attributes), 32*1024U + 1); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 647 | #else // __BIONIC__ |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 648 | // glibc rounds down, in violation of POSIX. They document this in their BUGS section. |
| 649 | ASSERT_EQ(GetActualStackSize(attributes), 32*1024U); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 650 | #endif // __BIONIC__ |
Elliott Hughes | b95cf0d | 2013-07-15 14:51:07 -0700 | [diff] [blame] | 651 | } |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 652 | |
| 653 | TEST(pthread, pthread_rwlock_smoke) { |
| 654 | pthread_rwlock_t l; |
| 655 | ASSERT_EQ(0, pthread_rwlock_init(&l, NULL)); |
| 656 | |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 657 | // Single read lock |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 658 | ASSERT_EQ(0, pthread_rwlock_rdlock(&l)); |
| 659 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 660 | |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 661 | // Multiple read lock |
| 662 | ASSERT_EQ(0, pthread_rwlock_rdlock(&l)); |
| 663 | ASSERT_EQ(0, pthread_rwlock_rdlock(&l)); |
| 664 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 665 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 666 | |
| 667 | // Write lock |
Calin Juravle | 92687e4 | 2014-05-22 19:21:22 +0100 | [diff] [blame] | 668 | ASSERT_EQ(0, pthread_rwlock_wrlock(&l)); |
| 669 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 670 | |
| 671 | // Try writer lock |
| 672 | ASSERT_EQ(0, pthread_rwlock_trywrlock(&l)); |
| 673 | ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l)); |
| 674 | ASSERT_EQ(EBUSY, pthread_rwlock_tryrdlock(&l)); |
| 675 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 676 | |
| 677 | // Try reader lock |
| 678 | ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l)); |
| 679 | ASSERT_EQ(0, pthread_rwlock_tryrdlock(&l)); |
| 680 | ASSERT_EQ(EBUSY, pthread_rwlock_trywrlock(&l)); |
| 681 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 682 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 683 | |
| 684 | // Try writer lock after unlock |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 685 | ASSERT_EQ(0, pthread_rwlock_wrlock(&l)); |
| 686 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 687 | |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 688 | #ifdef __BIONIC__ |
| 689 | // EDEADLK in "read after write" |
| 690 | ASSERT_EQ(0, pthread_rwlock_wrlock(&l)); |
| 691 | ASSERT_EQ(EDEADLK, pthread_rwlock_rdlock(&l)); |
| 692 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 693 | |
| 694 | // EDEADLK in "write after write" |
| 695 | ASSERT_EQ(0, pthread_rwlock_wrlock(&l)); |
| 696 | ASSERT_EQ(EDEADLK, pthread_rwlock_wrlock(&l)); |
| 697 | ASSERT_EQ(0, pthread_rwlock_unlock(&l)); |
| 698 | #endif |
| 699 | |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 700 | ASSERT_EQ(0, pthread_rwlock_destroy(&l)); |
| 701 | } |
| 702 | |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 703 | static int g_once_fn_call_count = 0; |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 704 | static void OnceFn() { |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 705 | ++g_once_fn_call_count; |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | TEST(pthread, pthread_once_smoke) { |
| 709 | pthread_once_t once_control = PTHREAD_ONCE_INIT; |
| 710 | ASSERT_EQ(0, pthread_once(&once_control, OnceFn)); |
| 711 | ASSERT_EQ(0, pthread_once(&once_control, OnceFn)); |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 712 | ASSERT_EQ(1, g_once_fn_call_count); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 713 | } |
| 714 | |
Elliott Hughes | 3694ec6 | 2014-05-14 11:46:08 -0700 | [diff] [blame] | 715 | static std::string pthread_once_1934122_result = ""; |
| 716 | |
| 717 | static void Routine2() { |
| 718 | pthread_once_1934122_result += "2"; |
| 719 | } |
| 720 | |
| 721 | static void Routine1() { |
| 722 | pthread_once_t once_control_2 = PTHREAD_ONCE_INIT; |
| 723 | pthread_once_1934122_result += "1"; |
| 724 | pthread_once(&once_control_2, &Routine2); |
| 725 | } |
| 726 | |
| 727 | TEST(pthread, pthread_once_1934122) { |
| 728 | // Very old versions of Android couldn't call pthread_once from a |
| 729 | // pthread_once init routine. http://b/1934122. |
| 730 | pthread_once_t once_control_1 = PTHREAD_ONCE_INIT; |
| 731 | ASSERT_EQ(0, pthread_once(&once_control_1, &Routine1)); |
| 732 | ASSERT_EQ("12", pthread_once_1934122_result); |
| 733 | } |
| 734 | |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 735 | static int g_atfork_prepare_calls = 0; |
| 736 | static void AtForkPrepare1() { g_atfork_prepare_calls = (g_atfork_prepare_calls << 4) | 1; } |
| 737 | static void AtForkPrepare2() { g_atfork_prepare_calls = (g_atfork_prepare_calls << 4) | 2; } |
| 738 | static int g_atfork_parent_calls = 0; |
| 739 | static void AtForkParent1() { g_atfork_parent_calls = (g_atfork_parent_calls << 4) | 1; } |
| 740 | static void AtForkParent2() { g_atfork_parent_calls = (g_atfork_parent_calls << 4) | 2; } |
| 741 | static int g_atfork_child_calls = 0; |
| 742 | static void AtForkChild1() { g_atfork_child_calls = (g_atfork_child_calls << 4) | 1; } |
| 743 | static void AtForkChild2() { g_atfork_child_calls = (g_atfork_child_calls << 4) | 2; } |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 744 | |
Dmitriy Ivanov | 00e3781 | 2014-11-20 16:53:47 -0800 | [diff] [blame] | 745 | TEST(pthread, pthread_atfork_smoke) { |
Dmitriy Ivanov | 0864d8b | 2014-11-20 21:21:26 -0800 | [diff] [blame] | 746 | test_isolated([] { |
Dmitriy Ivanov | 00e3781 | 2014-11-20 16:53:47 -0800 | [diff] [blame] | 747 | ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1)); |
| 748 | ASSERT_EQ(0, pthread_atfork(AtForkPrepare2, AtForkParent2, AtForkChild2)); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 749 | |
Dmitriy Ivanov | 00e3781 | 2014-11-20 16:53:47 -0800 | [diff] [blame] | 750 | int pid = fork(); |
| 751 | ASSERT_NE(-1, pid) << strerror(errno); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 752 | |
Dmitriy Ivanov | 00e3781 | 2014-11-20 16:53:47 -0800 | [diff] [blame] | 753 | // Child and parent calls are made in the order they were registered. |
| 754 | if (pid == 0) { |
| 755 | ASSERT_EQ(0x12, g_atfork_child_calls); |
| 756 | _exit(0); |
| 757 | } |
| 758 | ASSERT_EQ(0x12, g_atfork_parent_calls); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 759 | |
Dmitriy Ivanov | 00e3781 | 2014-11-20 16:53:47 -0800 | [diff] [blame] | 760 | // Prepare calls are made in the reverse order. |
| 761 | ASSERT_EQ(0x21, g_atfork_prepare_calls); |
| 762 | }); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | TEST(pthread, pthread_attr_getscope) { |
| 766 | pthread_attr_t attr; |
| 767 | ASSERT_EQ(0, pthread_attr_init(&attr)); |
| 768 | |
| 769 | int scope; |
| 770 | ASSERT_EQ(0, pthread_attr_getscope(&attr, &scope)); |
| 771 | ASSERT_EQ(PTHREAD_SCOPE_SYSTEM, scope); |
| 772 | } |
Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 773 | |
| 774 | TEST(pthread, pthread_condattr_init) { |
| 775 | pthread_condattr_t attr; |
| 776 | pthread_condattr_init(&attr); |
| 777 | |
| 778 | clockid_t clock; |
| 779 | ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock)); |
| 780 | ASSERT_EQ(CLOCK_REALTIME, clock); |
| 781 | |
| 782 | int pshared; |
| 783 | ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared)); |
| 784 | ASSERT_EQ(PTHREAD_PROCESS_PRIVATE, pshared); |
| 785 | } |
| 786 | |
| 787 | TEST(pthread, pthread_condattr_setclock) { |
| 788 | pthread_condattr_t attr; |
| 789 | pthread_condattr_init(&attr); |
| 790 | |
| 791 | ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_REALTIME)); |
| 792 | clockid_t clock; |
| 793 | ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock)); |
| 794 | ASSERT_EQ(CLOCK_REALTIME, clock); |
| 795 | |
| 796 | ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC)); |
| 797 | ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock)); |
| 798 | ASSERT_EQ(CLOCK_MONOTONIC, clock); |
| 799 | |
| 800 | ASSERT_EQ(EINVAL, pthread_condattr_setclock(&attr, CLOCK_PROCESS_CPUTIME_ID)); |
| 801 | } |
| 802 | |
| 803 | TEST(pthread, pthread_cond_broadcast__preserves_condattr_flags) { |
| 804 | #if defined(__BIONIC__) // This tests a bionic implementation detail. |
| 805 | pthread_condattr_t attr; |
| 806 | pthread_condattr_init(&attr); |
| 807 | |
| 808 | ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC)); |
| 809 | ASSERT_EQ(0, pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED)); |
| 810 | |
| 811 | pthread_cond_t cond_var; |
| 812 | ASSERT_EQ(0, pthread_cond_init(&cond_var, &attr)); |
| 813 | |
| 814 | ASSERT_EQ(0, pthread_cond_signal(&cond_var)); |
| 815 | ASSERT_EQ(0, pthread_cond_broadcast(&cond_var)); |
| 816 | |
| 817 | attr = static_cast<pthread_condattr_t>(cond_var.value); |
| 818 | clockid_t clock; |
| 819 | ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock)); |
| 820 | ASSERT_EQ(CLOCK_MONOTONIC, clock); |
| 821 | int pshared; |
| 822 | ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared)); |
| 823 | ASSERT_EQ(PTHREAD_PROCESS_SHARED, pshared); |
| 824 | #else // __BIONIC__ |
| 825 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 826 | #endif // __BIONIC__ |
| 827 | } |
Elliott Hughes | 0e714a5 | 2014-03-03 16:42:47 -0800 | [diff] [blame] | 828 | |
| 829 | TEST(pthread, pthread_mutex_timedlock) { |
| 830 | pthread_mutex_t m; |
| 831 | ASSERT_EQ(0, pthread_mutex_init(&m, NULL)); |
| 832 | |
| 833 | // If the mutex is already locked, pthread_mutex_timedlock should time out. |
| 834 | ASSERT_EQ(0, pthread_mutex_lock(&m)); |
| 835 | |
| 836 | timespec ts; |
| 837 | ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts)); |
| 838 | ts.tv_nsec += 1; |
| 839 | ASSERT_EQ(ETIMEDOUT, pthread_mutex_timedlock(&m, &ts)); |
| 840 | |
| 841 | // If the mutex is unlocked, pthread_mutex_timedlock should succeed. |
| 842 | ASSERT_EQ(0, pthread_mutex_unlock(&m)); |
| 843 | |
| 844 | ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts)); |
| 845 | ts.tv_nsec += 1; |
| 846 | ASSERT_EQ(0, pthread_mutex_timedlock(&m, &ts)); |
| 847 | |
| 848 | ASSERT_EQ(0, pthread_mutex_unlock(&m)); |
| 849 | ASSERT_EQ(0, pthread_mutex_destroy(&m)); |
| 850 | } |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 851 | |
| 852 | TEST(pthread, pthread_attr_getstack__main_thread) { |
| 853 | // This test is only meaningful for the main thread, so make sure we're running on it! |
| 854 | ASSERT_EQ(getpid(), syscall(__NR_gettid)); |
| 855 | |
| 856 | // Get the main thread's attributes. |
| 857 | pthread_attr_t attributes; |
| 858 | ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes)); |
| 859 | |
| 860 | // Check that we correctly report that the main thread has no guard page. |
| 861 | size_t guard_size; |
| 862 | ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size)); |
| 863 | ASSERT_EQ(0U, guard_size); // The main thread has no guard page. |
| 864 | |
| 865 | // Get the stack base and the stack size (both ways). |
| 866 | void* stack_base; |
| 867 | size_t stack_size; |
| 868 | ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size)); |
| 869 | size_t stack_size2; |
| 870 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2)); |
| 871 | |
| 872 | // The two methods of asking for the stack size should agree. |
| 873 | EXPECT_EQ(stack_size, stack_size2); |
| 874 | |
| 875 | // What does /proc/self/maps' [stack] line say? |
Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 876 | void* maps_stack_hi = NULL; |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 877 | FILE* fp = fopen("/proc/self/maps", "r"); |
| 878 | ASSERT_TRUE(fp != NULL); |
| 879 | char line[BUFSIZ]; |
| 880 | while (fgets(line, sizeof(line), fp) != NULL) { |
| 881 | uintptr_t lo, hi; |
| 882 | char name[10]; |
| 883 | sscanf(line, "%" PRIxPTR "-%" PRIxPTR " %*4s %*x %*x:%*x %*d %10s", &lo, &hi, name); |
| 884 | if (strcmp(name, "[stack]") == 0) { |
Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 885 | maps_stack_hi = reinterpret_cast<void*>(hi); |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 886 | break; |
| 887 | } |
| 888 | } |
| 889 | fclose(fp); |
| 890 | |
Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 891 | // The stack size should correspond to RLIMIT_STACK. |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 892 | rlimit rl; |
Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 893 | ASSERT_EQ(0, getrlimit(RLIMIT_STACK, &rl)); |
Elliott Hughes | 27a9aed | 2014-09-04 16:09:25 -0700 | [diff] [blame] | 894 | uint64_t original_rlim_cur = rl.rlim_cur; |
| 895 | #if defined(__BIONIC__) |
| 896 | if (rl.rlim_cur == RLIM_INFINITY) { |
| 897 | rl.rlim_cur = 8 * 1024 * 1024; // Bionic reports unlimited stacks as 8MiB. |
| 898 | } |
| 899 | #endif |
Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 900 | EXPECT_EQ(rl.rlim_cur, stack_size); |
| 901 | |
Dmitriy Ivanov | d9ff722 | 2014-09-08 16:22:22 -0700 | [diff] [blame] | 902 | auto guard = make_scope_guard([&rl, original_rlim_cur]() { |
Elliott Hughes | 27a9aed | 2014-09-04 16:09:25 -0700 | [diff] [blame] | 903 | rl.rlim_cur = original_rlim_cur; |
| 904 | ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl)); |
| 905 | }); |
| 906 | |
Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 907 | // The high address of the /proc/self/maps [stack] region should equal stack_base + stack_size. |
| 908 | // Remember that the stack grows down (and is mapped in on demand), so the low address of the |
| 909 | // region isn't very interesting. |
| 910 | EXPECT_EQ(maps_stack_hi, reinterpret_cast<uint8_t*>(stack_base) + stack_size); |
| 911 | |
| 912 | // |
| 913 | // What if RLIMIT_STACK is smaller than the stack's current extent? |
| 914 | // |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 915 | rl.rlim_cur = rl.rlim_max = 1024; // 1KiB. We know the stack must be at least a page already. |
| 916 | rl.rlim_max = RLIM_INFINITY; |
| 917 | ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl)); |
| 918 | |
| 919 | ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes)); |
| 920 | ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size)); |
| 921 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2)); |
| 922 | |
| 923 | EXPECT_EQ(stack_size, stack_size2); |
| 924 | ASSERT_EQ(1024U, stack_size); |
| 925 | |
| 926 | // |
Elliott Hughes | 9e4ffa7 | 2014-08-27 15:32:01 -0700 | [diff] [blame] | 927 | // What if RLIMIT_STACK isn't a whole number of pages? |
Elliott Hughes | 57b7a61 | 2014-08-25 17:26:50 -0700 | [diff] [blame] | 928 | // |
| 929 | rl.rlim_cur = rl.rlim_max = 6666; // Not a whole number of pages. |
| 930 | rl.rlim_max = RLIM_INFINITY; |
| 931 | ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl)); |
| 932 | |
| 933 | ASSERT_EQ(0, pthread_getattr_np(pthread_self(), &attributes)); |
| 934 | ASSERT_EQ(0, pthread_attr_getstack(&attributes, &stack_base, &stack_size)); |
| 935 | ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size2)); |
| 936 | |
| 937 | EXPECT_EQ(stack_size, stack_size2); |
| 938 | ASSERT_EQ(6666U, stack_size); |
| 939 | } |
Elliott Hughes | 8fb639c | 2014-09-12 14:43:07 -0700 | [diff] [blame] | 940 | |
Yabin Cui | 917d390 | 2015-01-08 12:32:42 -0800 | [diff] [blame^] | 941 | static void pthread_attr_getstack_18908062_helper(void*) { |
| 942 | char local_variable; |
| 943 | pthread_attr_t attributes; |
| 944 | pthread_getattr_np(pthread_self(), &attributes); |
| 945 | void* stack_base; |
| 946 | size_t stack_size; |
| 947 | pthread_attr_getstack(&attributes, &stack_base, &stack_size); |
| 948 | |
| 949 | // Test whether &local_variable is in [stack_base, stack_base + stack_size). |
| 950 | ASSERT_LE(reinterpret_cast<char*>(stack_base), &local_variable); |
| 951 | ASSERT_LT(&local_variable, reinterpret_cast<char*>(stack_base) + stack_size); |
| 952 | } |
| 953 | |
| 954 | // Check whether something on stack is in the range of |
| 955 | // [stack_base, stack_base + stack_size). see b/18908062. |
| 956 | TEST(pthread, pthread_attr_getstack_18908062) { |
| 957 | pthread_t t; |
| 958 | ASSERT_EQ(0, pthread_create(&t, NULL, |
| 959 | reinterpret_cast<void* (*)(void*)>(pthread_attr_getstack_18908062_helper), |
| 960 | NULL)); |
| 961 | pthread_join(t, NULL); |
| 962 | } |
| 963 | |
Elliott Hughes | 8fb639c | 2014-09-12 14:43:07 -0700 | [diff] [blame] | 964 | #if defined(__BIONIC__) |
| 965 | static void* pthread_gettid_np_helper(void* arg) { |
| 966 | *reinterpret_cast<pid_t*>(arg) = gettid(); |
| 967 | return NULL; |
| 968 | } |
| 969 | #endif |
| 970 | |
| 971 | TEST(pthread, pthread_gettid_np) { |
| 972 | #if defined(__BIONIC__) |
| 973 | ASSERT_EQ(gettid(), pthread_gettid_np(pthread_self())); |
| 974 | |
| 975 | pid_t t_gettid_result; |
| 976 | pthread_t t; |
| 977 | pthread_create(&t, NULL, pthread_gettid_np_helper, &t_gettid_result); |
| 978 | |
| 979 | pid_t t_pthread_gettid_np_result = pthread_gettid_np(t); |
| 980 | |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 981 | pthread_join(t, NULL); |
Elliott Hughes | 8fb639c | 2014-09-12 14:43:07 -0700 | [diff] [blame] | 982 | |
| 983 | ASSERT_EQ(t_gettid_result, t_pthread_gettid_np_result); |
| 984 | #else |
| 985 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 986 | #endif |
| 987 | } |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 988 | |
| 989 | static size_t cleanup_counter = 0; |
| 990 | |
Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 991 | static void AbortCleanupRoutine(void*) { |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 992 | abort(); |
| 993 | } |
| 994 | |
Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 995 | static void CountCleanupRoutine(void*) { |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 996 | ++cleanup_counter; |
| 997 | } |
| 998 | |
Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 999 | static void PthreadCleanupTester() { |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 1000 | pthread_cleanup_push(CountCleanupRoutine, NULL); |
| 1001 | pthread_cleanup_push(CountCleanupRoutine, NULL); |
| 1002 | pthread_cleanup_push(AbortCleanupRoutine, NULL); |
| 1003 | |
| 1004 | pthread_cleanup_pop(0); // Pop the abort without executing it. |
| 1005 | pthread_cleanup_pop(1); // Pop one count while executing it. |
| 1006 | ASSERT_EQ(1U, cleanup_counter); |
| 1007 | // Exit while the other count is still on the cleanup stack. |
| 1008 | pthread_exit(NULL); |
| 1009 | |
| 1010 | // Calls to pthread_cleanup_pop/pthread_cleanup_push must always be balanced. |
| 1011 | pthread_cleanup_pop(0); |
| 1012 | } |
| 1013 | |
Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 1014 | static void* PthreadCleanupStartRoutine(void*) { |
Elliott Hughes | 34c987a | 2014-09-22 16:01:26 -0700 | [diff] [blame] | 1015 | PthreadCleanupTester(); |
| 1016 | return NULL; |
| 1017 | } |
| 1018 | |
| 1019 | TEST(pthread, pthread_cleanup_push__pthread_cleanup_pop) { |
| 1020 | pthread_t t; |
| 1021 | ASSERT_EQ(0, pthread_create(&t, NULL, PthreadCleanupStartRoutine, NULL)); |
| 1022 | pthread_join(t, NULL); |
| 1023 | ASSERT_EQ(2U, cleanup_counter); |
| 1024 | } |
Derek Xue | 4199695 | 2014-09-25 11:05:32 +0100 | [diff] [blame] | 1025 | |
| 1026 | TEST(pthread, PTHREAD_MUTEX_DEFAULT_is_PTHREAD_MUTEX_NORMAL) { |
| 1027 | ASSERT_EQ(PTHREAD_MUTEX_NORMAL, PTHREAD_MUTEX_DEFAULT); |
| 1028 | } |
| 1029 | |
| 1030 | TEST(pthread, pthread_mutexattr_gettype) { |
| 1031 | pthread_mutexattr_t attr; |
| 1032 | ASSERT_EQ(0, pthread_mutexattr_init(&attr)); |
| 1033 | |
| 1034 | int attr_type; |
| 1035 | |
| 1036 | ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL)); |
| 1037 | ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type)); |
| 1038 | ASSERT_EQ(PTHREAD_MUTEX_NORMAL, attr_type); |
| 1039 | |
| 1040 | ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK)); |
| 1041 | ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type)); |
| 1042 | ASSERT_EQ(PTHREAD_MUTEX_ERRORCHECK, attr_type); |
| 1043 | |
| 1044 | ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE)); |
| 1045 | ASSERT_EQ(0, pthread_mutexattr_gettype(&attr, &attr_type)); |
| 1046 | ASSERT_EQ(PTHREAD_MUTEX_RECURSIVE, attr_type); |
| 1047 | } |
| 1048 | |
| 1049 | TEST(pthread, pthread_mutex_lock_NORMAL) { |
| 1050 | pthread_mutexattr_t attr; |
| 1051 | ASSERT_EQ(0, pthread_mutexattr_init(&attr)); |
| 1052 | ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL)); |
| 1053 | |
| 1054 | pthread_mutex_t lock; |
| 1055 | ASSERT_EQ(0, pthread_mutex_init(&lock, &attr)); |
| 1056 | |
| 1057 | ASSERT_EQ(0, pthread_mutex_lock(&lock)); |
| 1058 | ASSERT_EQ(0, pthread_mutex_unlock(&lock)); |
| 1059 | ASSERT_EQ(0, pthread_mutex_destroy(&lock)); |
| 1060 | } |
| 1061 | |
| 1062 | TEST(pthread, pthread_mutex_lock_ERRORCHECK) { |
| 1063 | pthread_mutexattr_t attr; |
| 1064 | ASSERT_EQ(0, pthread_mutexattr_init(&attr)); |
| 1065 | ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK)); |
| 1066 | |
| 1067 | pthread_mutex_t lock; |
| 1068 | ASSERT_EQ(0, pthread_mutex_init(&lock, &attr)); |
| 1069 | |
| 1070 | ASSERT_EQ(0, pthread_mutex_lock(&lock)); |
| 1071 | ASSERT_EQ(EDEADLK, pthread_mutex_lock(&lock)); |
| 1072 | ASSERT_EQ(0, pthread_mutex_unlock(&lock)); |
| 1073 | ASSERT_EQ(0, pthread_mutex_trylock(&lock)); |
| 1074 | ASSERT_EQ(EBUSY, pthread_mutex_trylock(&lock)); |
| 1075 | ASSERT_EQ(0, pthread_mutex_unlock(&lock)); |
| 1076 | ASSERT_EQ(EPERM, pthread_mutex_unlock(&lock)); |
| 1077 | ASSERT_EQ(0, pthread_mutex_destroy(&lock)); |
| 1078 | } |
| 1079 | |
| 1080 | TEST(pthread, pthread_mutex_lock_RECURSIVE) { |
| 1081 | pthread_mutexattr_t attr; |
| 1082 | ASSERT_EQ(0, pthread_mutexattr_init(&attr)); |
| 1083 | ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE)); |
| 1084 | |
| 1085 | pthread_mutex_t lock; |
| 1086 | ASSERT_EQ(0, pthread_mutex_init(&lock, &attr)); |
| 1087 | |
| 1088 | ASSERT_EQ(0, pthread_mutex_lock(&lock)); |
| 1089 | ASSERT_EQ(0, pthread_mutex_lock(&lock)); |
| 1090 | ASSERT_EQ(0, pthread_mutex_unlock(&lock)); |
| 1091 | ASSERT_EQ(0, pthread_mutex_unlock(&lock)); |
| 1092 | ASSERT_EQ(0, pthread_mutex_trylock(&lock)); |
| 1093 | ASSERT_EQ(0, pthread_mutex_unlock(&lock)); |
| 1094 | ASSERT_EQ(EPERM, pthread_mutex_unlock(&lock)); |
| 1095 | ASSERT_EQ(0, pthread_mutex_destroy(&lock)); |
| 1096 | } |