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