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 | |
| 31 | static void* IdFn(void* arg) { |
| 32 | return arg; |
| 33 | } |
| 34 | |
| 35 | static void* SleepFn(void* arg) { |
| 36 | sleep(reinterpret_cast<unsigned int>(arg)); |
| 37 | return NULL; |
| 38 | } |
| 39 | |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 40 | static void* SpinFn(void* arg) { |
| 41 | volatile bool* b = reinterpret_cast<volatile bool*>(arg); |
| 42 | while (!*b) { |
| 43 | } |
| 44 | return NULL; |
| 45 | } |
| 46 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 47 | static void* JoinFn(void* arg) { |
| 48 | return reinterpret_cast<void*>(pthread_join(reinterpret_cast<pthread_t>(arg), NULL)); |
| 49 | } |
| 50 | |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 51 | static void AssertDetached(pthread_t t, bool is_detached) { |
| 52 | pthread_attr_t attr; |
| 53 | ASSERT_EQ(0, pthread_getattr_np(t, &attr)); |
| 54 | int detach_state; |
| 55 | ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &detach_state)); |
| 56 | pthread_attr_destroy(&attr); |
| 57 | ASSERT_EQ(is_detached, (detach_state == PTHREAD_CREATE_DETACHED)); |
| 58 | } |
| 59 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 60 | TEST(pthread, pthread_create) { |
| 61 | void* expected_result = reinterpret_cast<void*>(123); |
| 62 | // Can we create a thread? |
| 63 | pthread_t t; |
| 64 | ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, expected_result)); |
| 65 | // If we join, do we get the expected value back? |
| 66 | void* result; |
| 67 | ASSERT_EQ(0, pthread_join(t, &result)); |
| 68 | ASSERT_EQ(expected_result, result); |
| 69 | } |
| 70 | |
| 71 | TEST(pthread, pthread_no_join_after_detach) { |
| 72 | pthread_t t1; |
| 73 | ASSERT_EQ(0, pthread_create(&t1, NULL, SleepFn, reinterpret_cast<void*>(5))); |
| 74 | |
| 75 | // After a pthread_detach... |
| 76 | ASSERT_EQ(0, pthread_detach(t1)); |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 77 | AssertDetached(t1, true); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 78 | |
| 79 | // ...pthread_join should fail. |
| 80 | void* result; |
| 81 | ASSERT_EQ(EINVAL, pthread_join(t1, &result)); |
| 82 | } |
| 83 | |
| 84 | TEST(pthread, pthread_no_op_detach_after_join) { |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 85 | bool done = false; |
| 86 | |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 87 | pthread_t t1; |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 88 | ASSERT_EQ(0, pthread_create(&t1, NULL, SpinFn, &done)); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 89 | |
| 90 | // If thread 2 is already waiting to join thread 1... |
| 91 | pthread_t t2; |
| 92 | ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1))); |
| 93 | |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 94 | sleep(1); // (Give t2 a chance to call pthread_join.) |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 95 | |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 96 | // ...a call to pthread_detach on thread 1 will "succeed" (silently fail)... |
| 97 | ASSERT_EQ(0, pthread_detach(t1)); |
| 98 | AssertDetached(t1, false); |
| 99 | |
| 100 | done = true; |
| 101 | |
| 102 | // ...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] | 103 | void* join_result; |
| 104 | ASSERT_EQ(0, pthread_join(t2, &join_result)); |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 105 | ASSERT_EQ(0, reinterpret_cast<int>(join_result)); |
Elliott Hughes | 4d014e1 | 2012-09-07 16:47:54 -0700 | [diff] [blame] | 106 | } |
Elliott Hughes | 14f1959 | 2012-10-29 10:19:44 -0700 | [diff] [blame] | 107 | |
| 108 | TEST(pthread, pthread_join_self) { |
| 109 | void* result; |
| 110 | ASSERT_EQ(EDEADLK, pthread_join(pthread_self(), &result)); |
| 111 | } |
Elliott Hughes | 4f251be | 2012-11-01 16:33:29 -0700 | [diff] [blame] | 112 | |
| 113 | #if __BIONIC__ // For some reason, gtest on bionic can cope with this but gtest on glibc can't. |
| 114 | |
| 115 | static void TestBug37410() { |
| 116 | pthread_t t1; |
| 117 | ASSERT_EQ(0, pthread_create(&t1, NULL, JoinFn, reinterpret_cast<void*>(pthread_self()))); |
| 118 | pthread_exit(NULL); |
| 119 | } |
| 120 | |
| 121 | // We have to say "DeathTest" here so gtest knows to run this test (which exits) |
| 122 | // in its own process. |
| 123 | TEST(pthread_DeathTest, pthread_bug_37410) { |
| 124 | // http://code.google.com/p/android/issues/detail?id=37410 |
| 125 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 126 | EXPECT_EXIT(TestBug37410(), ::testing::ExitedWithCode(0), ""); |
| 127 | } |
| 128 | #endif |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 129 | |
| 130 | static void* SignalHandlerFn(void* arg) { |
| 131 | sigset_t wait_set; |
| 132 | sigfillset(&wait_set); |
| 133 | return reinterpret_cast<void*>(sigwait(&wait_set, reinterpret_cast<int*>(arg))); |
| 134 | } |
| 135 | |
| 136 | TEST(pthread, pthread_sigmask) { |
| 137 | // Block SIGUSR1. |
| 138 | sigset_t set; |
| 139 | sigemptyset(&set); |
| 140 | sigaddset(&set, SIGUSR1); |
| 141 | ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &set, NULL)); |
| 142 | |
| 143 | // Spawn a thread that calls sigwait and tells us what it received. |
| 144 | pthread_t signal_thread; |
| 145 | int received_signal = -1; |
| 146 | ASSERT_EQ(0, pthread_create(&signal_thread, NULL, SignalHandlerFn, &received_signal)); |
| 147 | |
| 148 | // Send that thread SIGUSR1. |
| 149 | pthread_kill(signal_thread, SIGUSR1); |
| 150 | |
| 151 | // See what it got. |
| 152 | void* join_result; |
| 153 | ASSERT_EQ(0, pthread_join(signal_thread, &join_result)); |
| 154 | ASSERT_EQ(SIGUSR1, received_signal); |
| 155 | ASSERT_EQ(0, reinterpret_cast<int>(join_result)); |
| 156 | } |