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