pthread_test.cpp revision 6f94de3ca49e4ea147b1c59e5818fa175846518f
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>
21#include <unistd.h>
22
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}
30
31TEST(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
50static 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
59static void* SpinFn(void* arg) {
60  volatile bool* b = reinterpret_cast<volatile bool*>(arg);
61  while (!*b) {
62  }
63  return NULL;
64}
65
66static void* JoinFn(void* arg) {
67  return reinterpret_cast<void*>(pthread_join(reinterpret_cast<pthread_t>(arg), NULL));
68}
69
70static 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
79TEST(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));
96  AssertDetached(t1, true);
97
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) {
104  bool done = false;
105
106  pthread_t t1;
107  ASSERT_EQ(0, pthread_create(&t1, NULL, SpinFn, &done));
108
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
113  sleep(1); // (Give t2 a chance to call pthread_join.)
114
115  // ...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).
122  void* join_result;
123  ASSERT_EQ(0, pthread_join(t2, &join_result));
124  ASSERT_EQ(0, reinterpret_cast<int>(join_result));
125}
126
127TEST(pthread, pthread_join_self) {
128  void* result;
129  ASSERT_EQ(EDEADLK, pthread_join(pthread_self(), &result));
130}
131
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
148
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}
176