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> |
| 20 | #include <pthread.h> |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 21 | #include <unistd.h> |
Elliott Hughes | bfeab1b | 2012-09-05 17:47:37 -0700 | [diff] [blame] | 22 | |
| 23 | TEST(pthread, pthread_key_create) { |
| 24 | pthread_key_t key; |
| 25 | ASSERT_EQ(0, pthread_key_create(&key, NULL)); |
| 26 | ASSERT_EQ(0, pthread_key_delete(key)); |
| 27 | // Can't delete a key that's already been deleted. |
| 28 | ASSERT_EQ(EINVAL, pthread_key_delete(key)); |
| 29 | } |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 30 | |
Elliott Hughes | 2a1bb4e | 2013-02-11 12:34:03 -0800 | [diff] [blame] | 31 | #if !defined(__GLIBC__) // glibc uses keys internally that its sysconf value doesn't account for. |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 32 | TEST(pthread, pthread_key_create_lots) { |
| 33 | // We can allocate _SC_THREAD_KEYS_MAX keys. |
| 34 | std::vector<pthread_key_t> keys; |
| 35 | for (int i = 0; i < sysconf(_SC_THREAD_KEYS_MAX); ++i) { |
| 36 | pthread_key_t key; |
Elliott Hughes | 2a1bb4e | 2013-02-11 12:34:03 -0800 | [diff] [blame] | 37 | // If this fails, it's likely that GLOBAL_INIT_THREAD_LOCAL_BUFFER_COUNT is wrong. |
| 38 | ASSERT_EQ(0, pthread_key_create(&key, NULL)) << i << " of " << sysconf(_SC_THREAD_KEYS_MAX); |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 39 | keys.push_back(key); |
| 40 | } |
| 41 | |
| 42 | // ...and that really is the maximum. |
| 43 | pthread_key_t key; |
| 44 | ASSERT_EQ(EAGAIN, pthread_key_create(&key, NULL)); |
| 45 | |
| 46 | // (Don't leak all those keys!) |
| 47 | for (size_t i = 0; i < keys.size(); ++i) { |
| 48 | ASSERT_EQ(0, pthread_key_delete(keys[i])); |
| 49 | } |
| 50 | } |
Elliott Hughes | 2a1bb4e | 2013-02-11 12:34:03 -0800 | [diff] [blame] | 51 | #endif |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 52 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 53 | static void* IdFn(void* arg) { |
| 54 | return arg; |
| 55 | } |
| 56 | |
| 57 | static void* SleepFn(void* arg) { |
| 58 | sleep(reinterpret_cast<unsigned int>(arg)); |
| 59 | return NULL; |
| 60 | } |
| 61 | |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 62 | static void* SpinFn(void* arg) { |
| 63 | volatile bool* b = reinterpret_cast<volatile bool*>(arg); |
| 64 | while (!*b) { |
| 65 | } |
| 66 | return NULL; |
| 67 | } |
| 68 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 69 | static void* JoinFn(void* arg) { |
| 70 | return reinterpret_cast<void*>(pthread_join(reinterpret_cast<pthread_t>(arg), NULL)); |
| 71 | } |
| 72 | |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 73 | static void AssertDetached(pthread_t t, bool is_detached) { |
| 74 | pthread_attr_t attr; |
| 75 | ASSERT_EQ(0, pthread_getattr_np(t, &attr)); |
| 76 | int detach_state; |
| 77 | ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &detach_state)); |
| 78 | pthread_attr_destroy(&attr); |
| 79 | ASSERT_EQ(is_detached, (detach_state == PTHREAD_CREATE_DETACHED)); |
| 80 | } |
| 81 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 82 | TEST(pthread, pthread_create) { |
| 83 | void* expected_result = reinterpret_cast<void*>(123); |
| 84 | // Can we create a thread? |
| 85 | pthread_t t; |
| 86 | ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, expected_result)); |
| 87 | // If we join, do we get the expected value back? |
| 88 | void* result; |
| 89 | ASSERT_EQ(0, pthread_join(t, &result)); |
| 90 | ASSERT_EQ(expected_result, result); |
| 91 | } |
| 92 | |
Elliott Hughes | 2a1bb4e | 2013-02-11 12:34:03 -0800 | [diff] [blame] | 93 | TEST(pthread, pthread_create_EAGAIN) { |
| 94 | pthread_attr_t attributes; |
| 95 | ASSERT_EQ(0, pthread_attr_init(&attributes)); |
| 96 | ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, static_cast<size_t>(-1) & ~(getpagesize() - 1))); |
| 97 | |
| 98 | pthread_t t; |
| 99 | ASSERT_EQ(EAGAIN, pthread_create(&t, &attributes, IdFn, NULL)); |
| 100 | } |
| 101 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 102 | TEST(pthread, pthread_no_join_after_detach) { |
| 103 | pthread_t t1; |
| 104 | ASSERT_EQ(0, pthread_create(&t1, NULL, SleepFn, reinterpret_cast<void*>(5))); |
| 105 | |
| 106 | // After a pthread_detach... |
| 107 | ASSERT_EQ(0, pthread_detach(t1)); |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 108 | AssertDetached(t1, true); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 109 | |
| 110 | // ...pthread_join should fail. |
| 111 | void* result; |
| 112 | ASSERT_EQ(EINVAL, pthread_join(t1, &result)); |
| 113 | } |
| 114 | |
| 115 | TEST(pthread, pthread_no_op_detach_after_join) { |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 116 | bool done = false; |
| 117 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 118 | pthread_t t1; |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 119 | ASSERT_EQ(0, pthread_create(&t1, NULL, SpinFn, &done)); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 120 | |
| 121 | // If thread 2 is already waiting to join thread 1... |
| 122 | pthread_t t2; |
| 123 | ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1))); |
| 124 | |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 125 | sleep(1); // (Give t2 a chance to call pthread_join.) |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 126 | |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 127 | // ...a call to pthread_detach on thread 1 will "succeed" (silently fail)... |
| 128 | ASSERT_EQ(0, pthread_detach(t1)); |
| 129 | AssertDetached(t1, false); |
| 130 | |
| 131 | done = true; |
| 132 | |
| 133 | // ...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] | 134 | void* join_result; |
| 135 | ASSERT_EQ(0, pthread_join(t2, &join_result)); |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 136 | ASSERT_EQ(0, reinterpret_cast<int>(join_result)); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 137 | } |
Elliott Hughes | 14f1959 | 2012-10-29 10:19:44 -0700 | [diff] [blame] | 138 | |
| 139 | TEST(pthread, pthread_join_self) { |
| 140 | void* result; |
| 141 | ASSERT_EQ(EDEADLK, pthread_join(pthread_self(), &result)); |
| 142 | } |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 143 | |
| 144 | #if __BIONIC__ // For some reason, gtest on bionic can cope with this but gtest on glibc can't. |
| 145 | |
| 146 | static void TestBug37410() { |
| 147 | pthread_t t1; |
| 148 | ASSERT_EQ(0, pthread_create(&t1, NULL, JoinFn, reinterpret_cast<void*>(pthread_self()))); |
| 149 | pthread_exit(NULL); |
| 150 | } |
| 151 | |
| 152 | // We have to say "DeathTest" here so gtest knows to run this test (which exits) |
| 153 | // in its own process. |
| 154 | TEST(pthread_DeathTest, pthread_bug_37410) { |
| 155 | // http://code.google.com/p/android/issues/detail?id=37410 |
| 156 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 157 | EXPECT_EXIT(TestBug37410(), ::testing::ExitedWithCode(0), ""); |
| 158 | } |
| 159 | #endif |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 160 | |
| 161 | static void* SignalHandlerFn(void* arg) { |
| 162 | sigset_t wait_set; |
| 163 | sigfillset(&wait_set); |
| 164 | return reinterpret_cast<void*>(sigwait(&wait_set, reinterpret_cast<int*>(arg))); |
| 165 | } |
| 166 | |
| 167 | TEST(pthread, pthread_sigmask) { |
| 168 | // Block SIGUSR1. |
| 169 | sigset_t set; |
| 170 | sigemptyset(&set); |
| 171 | sigaddset(&set, SIGUSR1); |
| 172 | ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &set, NULL)); |
| 173 | |
| 174 | // Spawn a thread that calls sigwait and tells us what it received. |
| 175 | pthread_t signal_thread; |
| 176 | int received_signal = -1; |
| 177 | ASSERT_EQ(0, pthread_create(&signal_thread, NULL, SignalHandlerFn, &received_signal)); |
| 178 | |
| 179 | // Send that thread SIGUSR1. |
| 180 | pthread_kill(signal_thread, SIGUSR1); |
| 181 | |
| 182 | // See what it got. |
| 183 | void* join_result; |
| 184 | ASSERT_EQ(0, pthread_join(signal_thread, &join_result)); |
| 185 | ASSERT_EQ(SIGUSR1, received_signal); |
| 186 | ASSERT_EQ(0, reinterpret_cast<int>(join_result)); |
| 187 | } |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 188 | |
| 189 | #if !defined(__GLIBC__) |
| 190 | extern "C" int __pthread_clone(int (*fn)(void*), void* child_stack, int flags, void* arg); |
| 191 | TEST(pthread, __pthread_clone) { |
| 192 | uintptr_t fake_child_stack[16]; |
| 193 | errno = 0; |
| 194 | ASSERT_EQ(-1, __pthread_clone(NULL, &fake_child_stack[0], CLONE_THREAD, NULL)); |
| 195 | ASSERT_EQ(EINVAL, errno); |
| 196 | } |
| 197 | #endif |