blob: 0ccd9484e5369428cd519cc790187429047eb8ce [file] [log] [blame]
Elliott Hughesbfeab1b2012-09-05 17:47:37 -07001/*
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 Hughes4d014e12012-09-07 16:47:54 -070021#include <unistd.h>
Elliott Hughesbfeab1b2012-09-05 17:47:37 -070022
23TEST(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 Hughes4d014e12012-09-07 16:47:54 -070030
Elliott Hughes44b53ad2013-02-11 20:18:47 +000031TEST(pthread, pthread_key_create_lots) {
32 // We can allocate _SC_THREAD_KEYS_MAX keys.
33 std::vector<pthread_key_t> keys;
34 for (int i = 0; i < sysconf(_SC_THREAD_KEYS_MAX); ++i) {
35 pthread_key_t key;
36 ASSERT_EQ(0, pthread_key_create(&key, NULL));
37 keys.push_back(key);
38 }
39
40 // ...and that really is the maximum.
41 pthread_key_t key;
42 ASSERT_EQ(EAGAIN, pthread_key_create(&key, NULL));
43
44 // (Don't leak all those keys!)
45 for (size_t i = 0; i < keys.size(); ++i) {
46 ASSERT_EQ(0, pthread_key_delete(keys[i]));
47 }
48}
49
Elliott Hughes4d014e12012-09-07 16:47:54 -070050static void* IdFn(void* arg) {
51 return arg;
52}
53
54static void* SleepFn(void* arg) {
55 sleep(reinterpret_cast<unsigned int>(arg));
56 return NULL;
57}
58
Sergey Melnikov10ce9692012-10-26 14:06:43 +040059static void* SpinFn(void* arg) {
60 volatile bool* b = reinterpret_cast<volatile bool*>(arg);
61 while (!*b) {
62 }
63 return NULL;
64}
65
Elliott Hughes4d014e12012-09-07 16:47:54 -070066static void* JoinFn(void* arg) {
67 return reinterpret_cast<void*>(pthread_join(reinterpret_cast<pthread_t>(arg), NULL));
68}
69
Sergey Melnikov10ce9692012-10-26 14:06:43 +040070static void AssertDetached(pthread_t t, bool is_detached) {
71 pthread_attr_t attr;
72 ASSERT_EQ(0, pthread_getattr_np(t, &attr));
73 int detach_state;
74 ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &detach_state));
75 pthread_attr_destroy(&attr);
76 ASSERT_EQ(is_detached, (detach_state == PTHREAD_CREATE_DETACHED));
77}
78
Elliott Hughes4d014e12012-09-07 16:47:54 -070079TEST(pthread, pthread_create) {
80 void* expected_result = reinterpret_cast<void*>(123);
81 // Can we create a thread?
82 pthread_t t;
83 ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, expected_result));
84 // If we join, do we get the expected value back?
85 void* result;
86 ASSERT_EQ(0, pthread_join(t, &result));
87 ASSERT_EQ(expected_result, result);
88}
89
90TEST(pthread, pthread_no_join_after_detach) {
91 pthread_t t1;
92 ASSERT_EQ(0, pthread_create(&t1, NULL, SleepFn, reinterpret_cast<void*>(5)));
93
94 // After a pthread_detach...
95 ASSERT_EQ(0, pthread_detach(t1));
Sergey Melnikov10ce9692012-10-26 14:06:43 +040096 AssertDetached(t1, true);
Elliott Hughes4d014e12012-09-07 16:47:54 -070097
98 // ...pthread_join should fail.
99 void* result;
100 ASSERT_EQ(EINVAL, pthread_join(t1, &result));
101}
102
103TEST(pthread, pthread_no_op_detach_after_join) {
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400104 bool done = false;
105
Elliott Hughes4d014e12012-09-07 16:47:54 -0700106 pthread_t t1;
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400107 ASSERT_EQ(0, pthread_create(&t1, NULL, SpinFn, &done));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700108
109 // If thread 2 is already waiting to join thread 1...
110 pthread_t t2;
111 ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
112
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400113 sleep(1); // (Give t2 a chance to call pthread_join.)
Elliott Hughes4d014e12012-09-07 16:47:54 -0700114
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400115 // ...a call to pthread_detach on thread 1 will "succeed" (silently fail)...
116 ASSERT_EQ(0, pthread_detach(t1));
117 AssertDetached(t1, false);
118
119 done = true;
120
121 // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes).
Elliott Hughes4d014e12012-09-07 16:47:54 -0700122 void* join_result;
123 ASSERT_EQ(0, pthread_join(t2, &join_result));
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400124 ASSERT_EQ(0, reinterpret_cast<int>(join_result));
Elliott Hughes4d014e12012-09-07 16:47:54 -0700125}
Elliott Hughes14f19592012-10-29 10:19:44 -0700126
127TEST(pthread, pthread_join_self) {
128 void* result;
129 ASSERT_EQ(EDEADLK, pthread_join(pthread_self(), &result));
130}
Elliott Hughes4f251be2012-11-01 16:33:29 -0700131
132#if __BIONIC__ // For some reason, gtest on bionic can cope with this but gtest on glibc can't.
133
134static void TestBug37410() {
135 pthread_t t1;
136 ASSERT_EQ(0, pthread_create(&t1, NULL, JoinFn, reinterpret_cast<void*>(pthread_self())));
137 pthread_exit(NULL);
138}
139
140// We have to say "DeathTest" here so gtest knows to run this test (which exits)
141// in its own process.
142TEST(pthread_DeathTest, pthread_bug_37410) {
143 // http://code.google.com/p/android/issues/detail?id=37410
144 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
145 EXPECT_EXIT(TestBug37410(), ::testing::ExitedWithCode(0), "");
146}
147#endif
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800148
149static void* SignalHandlerFn(void* arg) {
150 sigset_t wait_set;
151 sigfillset(&wait_set);
152 return reinterpret_cast<void*>(sigwait(&wait_set, reinterpret_cast<int*>(arg)));
153}
154
155TEST(pthread, pthread_sigmask) {
156 // Block SIGUSR1.
157 sigset_t set;
158 sigemptyset(&set);
159 sigaddset(&set, SIGUSR1);
160 ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &set, NULL));
161
162 // Spawn a thread that calls sigwait and tells us what it received.
163 pthread_t signal_thread;
164 int received_signal = -1;
165 ASSERT_EQ(0, pthread_create(&signal_thread, NULL, SignalHandlerFn, &received_signal));
166
167 // Send that thread SIGUSR1.
168 pthread_kill(signal_thread, SIGUSR1);
169
170 // See what it got.
171 void* join_result;
172 ASSERT_EQ(0, pthread_join(signal_thread, &join_result));
173 ASSERT_EQ(SIGUSR1, received_signal);
174 ASSERT_EQ(0, reinterpret_cast<int>(join_result));
175}
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800176
177#if !defined(__GLIBC__)
178extern "C" int __pthread_clone(int (*fn)(void*), void* child_stack, int flags, void* arg);
179TEST(pthread, __pthread_clone) {
180 uintptr_t fake_child_stack[16];
181 errno = 0;
182 ASSERT_EQ(-1, __pthread_clone(NULL, &fake_child_stack[0], CLONE_THREAD, NULL));
183 ASSERT_EQ(EINVAL, errno);
184}
185#endif