pthread_test.cpp revision f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6d
1bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes/*
2bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes * Copyright (C) 2012 The Android Open Source Project
3bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes *
4bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
5bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes * you may not use this file except in compliance with the License.
6bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes * You may obtain a copy of the License at
7bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes *
8bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
9bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes *
10bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes * Unless required by applicable law or agreed to in writing, software
11bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
12bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes * See the License for the specific language governing permissions and
14bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes * limitations under the License.
15bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes */
16bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes
17bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes#include <gtest/gtest.h>
18bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes
19bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes#include <errno.h>
205b9310e502003e584bcb3a028ca3db7aa4d3f01bElliott Hughes#include <inttypes.h>
21b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes#include <limits.h>
22bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes#include <pthread.h>
23f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#include <signal.h>
2470b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes#include <sys/mman.h>
254d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes#include <unistd.h>
26bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes
27bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott HughesTEST(pthread, pthread_key_create) {
28bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes  pthread_key_t key;
29bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes  ASSERT_EQ(0, pthread_key_create(&key, NULL));
30bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes  ASSERT_EQ(0, pthread_key_delete(key));
31bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes  // Can't delete a key that's already been deleted.
32bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes  ASSERT_EQ(EINVAL, pthread_key_delete(key));
33bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes}
344d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes
3544b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott HughesTEST(pthread, pthread_key_create_lots) {
36f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#if defined(__BIONIC__) // glibc uses keys internally that its sysconf value doesn't account for.
371887621de8a48eece8a05f2400ddd783b9833147Elliott Hughes  // POSIX says PTHREAD_KEYS_MAX should be at least 128.
381887621de8a48eece8a05f2400ddd783b9833147Elliott Hughes  ASSERT_GE(PTHREAD_KEYS_MAX, 128);
39718a5b5495ae7726aabd2f8a748da9f391d12b98Elliott Hughes
40718a5b5495ae7726aabd2f8a748da9f391d12b98Elliott Hughes  int sysconf_max = sysconf(_SC_THREAD_KEYS_MAX);
41718a5b5495ae7726aabd2f8a748da9f391d12b98Elliott Hughes
421887621de8a48eece8a05f2400ddd783b9833147Elliott Hughes  // sysconf shouldn't return a smaller value.
43718a5b5495ae7726aabd2f8a748da9f391d12b98Elliott Hughes  ASSERT_GE(sysconf_max, PTHREAD_KEYS_MAX);
441887621de8a48eece8a05f2400ddd783b9833147Elliott Hughes
4544b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes  // We can allocate _SC_THREAD_KEYS_MAX keys.
46718a5b5495ae7726aabd2f8a748da9f391d12b98Elliott Hughes  sysconf_max -= 2; // (Except that gtest takes two for itself.)
4744b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes  std::vector<pthread_key_t> keys;
48718a5b5495ae7726aabd2f8a748da9f391d12b98Elliott Hughes  for (int i = 0; i < sysconf_max; ++i) {
4944b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes    pthread_key_t key;
503e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes    // If this fails, it's likely that GLOBAL_INIT_THREAD_LOCAL_BUFFER_COUNT is wrong.
51718a5b5495ae7726aabd2f8a748da9f391d12b98Elliott Hughes    ASSERT_EQ(0, pthread_key_create(&key, NULL)) << i << " of " << sysconf_max;
5244b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes    keys.push_back(key);
5344b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes  }
5444b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes
5544b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes  // ...and that really is the maximum.
5644b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes  pthread_key_t key;
5744b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes  ASSERT_EQ(EAGAIN, pthread_key_create(&key, NULL));
5844b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes
5944b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes  // (Don't leak all those keys!)
6044b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes  for (size_t i = 0; i < keys.size(); ++i) {
6144b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes    ASSERT_EQ(0, pthread_key_delete(keys[i]));
6244b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes  }
63f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#else // __BIONIC__
64f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris  GTEST_LOG_(INFO) << "This test does nothing.\n";
65f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#endif // __BIONIC__
6644b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes}
6744b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes
684d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughesstatic void* IdFn(void* arg) {
694d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  return arg;
704d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes}
714d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes
724d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughesstatic void* SleepFn(void* arg) {
735b9310e502003e584bcb3a028ca3db7aa4d3f01bElliott Hughes  sleep(reinterpret_cast<uintptr_t>(arg));
744d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  return NULL;
754d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes}
764d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes
7710ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikovstatic void* SpinFn(void* arg) {
7810ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  volatile bool* b = reinterpret_cast<volatile bool*>(arg);
7910ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  while (!*b) {
8010ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  }
8110ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  return NULL;
8210ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov}
8310ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov
844d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughesstatic void* JoinFn(void* arg) {
854d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  return reinterpret_cast<void*>(pthread_join(reinterpret_cast<pthread_t>(arg), NULL));
864d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes}
874d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes
8810ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikovstatic void AssertDetached(pthread_t t, bool is_detached) {
8910ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  pthread_attr_t attr;
9010ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  ASSERT_EQ(0, pthread_getattr_np(t, &attr));
9110ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  int detach_state;
9210ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &detach_state));
9310ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  pthread_attr_destroy(&attr);
9410ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  ASSERT_EQ(is_detached, (detach_state == PTHREAD_CREATE_DETACHED));
9510ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov}
9610ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov
979d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughesstatic void MakeDeadThread(pthread_t& t) {
989d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, NULL));
999d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  void* result;
1009d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  ASSERT_EQ(0, pthread_join(t, &result));
1019d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes}
1029d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes
1034d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott HughesTEST(pthread, pthread_create) {
1044d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  void* expected_result = reinterpret_cast<void*>(123);
1054d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  // Can we create a thread?
1064d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  pthread_t t;
1074d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, expected_result));
1084d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  // If we join, do we get the expected value back?
1094d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  void* result;
1104d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  ASSERT_EQ(0, pthread_join(t, &result));
1114d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  ASSERT_EQ(expected_result, result);
1124d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes}
1134d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes
1143e898476c7230b60a0f76968e64ff25f475b48c0Elliott HughesTEST(pthread, pthread_create_EAGAIN) {
1153e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes  pthread_attr_t attributes;
1163e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes  ASSERT_EQ(0, pthread_attr_init(&attributes));
1173e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes  ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, static_cast<size_t>(-1) & ~(getpagesize() - 1)));
1183e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes
1193e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes  pthread_t t;
1203e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes  ASSERT_EQ(EAGAIN, pthread_create(&t, &attributes, IdFn, NULL));
1213e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes}
1223e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes
1234d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott HughesTEST(pthread, pthread_no_join_after_detach) {
1244d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  pthread_t t1;
1254d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  ASSERT_EQ(0, pthread_create(&t1, NULL, SleepFn, reinterpret_cast<void*>(5)));
1264d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes
1274d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  // After a pthread_detach...
1284d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  ASSERT_EQ(0, pthread_detach(t1));
12910ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  AssertDetached(t1, true);
1304d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes
1314d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  // ...pthread_join should fail.
1324d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  void* result;
1334d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  ASSERT_EQ(EINVAL, pthread_join(t1, &result));
1344d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes}
1354d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes
1364d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott HughesTEST(pthread, pthread_no_op_detach_after_join) {
13710ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  bool done = false;
13810ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov
1394d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  pthread_t t1;
14010ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  ASSERT_EQ(0, pthread_create(&t1, NULL, SpinFn, &done));
1414d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes
1424d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  // If thread 2 is already waiting to join thread 1...
1434d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  pthread_t t2;
1444d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
1454d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes
14610ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  sleep(1); // (Give t2 a chance to call pthread_join.)
14710ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov
14810ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  // ...a call to pthread_detach on thread 1 will "succeed" (silently fail)...
1494d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  ASSERT_EQ(0, pthread_detach(t1));
15010ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  AssertDetached(t1, false);
15110ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov
15210ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  done = true;
1534d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes
15410ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes).
1554d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  void* join_result;
1564d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  ASSERT_EQ(0, pthread_join(t2, &join_result));
1575b9310e502003e584bcb3a028ca3db7aa4d3f01bElliott Hughes  ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
1584d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes}
15914f19592ae7c819855052bcebc79de87069c2954Elliott Hughes
16014f19592ae7c819855052bcebc79de87069c2954Elliott HughesTEST(pthread, pthread_join_self) {
16114f19592ae7c819855052bcebc79de87069c2954Elliott Hughes  void* result;
16214f19592ae7c819855052bcebc79de87069c2954Elliott Hughes  ASSERT_EQ(EDEADLK, pthread_join(pthread_self(), &result));
16314f19592ae7c819855052bcebc79de87069c2954Elliott Hughes}
1644f251bee5d51228217c1bf4dfc9219f3058bd3edElliott Hughes
165877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughesstruct TestBug37410 {
166877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes  pthread_t main_thread;
167877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes  pthread_mutex_t mutex;
1684f251bee5d51228217c1bf4dfc9219f3058bd3edElliott Hughes
169877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes  static void main() {
170877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    TestBug37410 data;
171877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    data.main_thread = pthread_self();
172877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    ASSERT_EQ(0, pthread_mutex_init(&data.mutex, NULL));
173877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
174877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes
175877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    pthread_t t;
176877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    ASSERT_EQ(0, pthread_create(&t, NULL, TestBug37410::thread_fn, reinterpret_cast<void*>(&data)));
177877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes
178877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    // Wait for the thread to be running...
179877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
180877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    ASSERT_EQ(0, pthread_mutex_unlock(&data.mutex));
181877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes
182877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    // ...and exit.
183877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    pthread_exit(NULL);
184877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes  }
185877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes
186877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes private:
187877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes  static void* thread_fn(void* arg) {
188877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    TestBug37410* data = reinterpret_cast<TestBug37410*>(arg);
189877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes
190877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    // Let the main thread know we're running.
191877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    pthread_mutex_unlock(&data->mutex);
192877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes
193877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    // And wait for the main thread to exit.
194877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    pthread_join(data->main_thread, NULL);
195877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes
196877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    return NULL;
197877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes  }
198877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes};
1994f251bee5d51228217c1bf4dfc9219f3058bd3edElliott Hughes
2007fd803cdfa873c01138dcbd614ec92418169b1c2Elliott Hughes// Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to
2017fd803cdfa873c01138dcbd614ec92418169b1c2Elliott Hughes// run this test (which exits normally) in its own process.
2024f251bee5d51228217c1bf4dfc9219f3058bd3edElliott HughesTEST(pthread_DeathTest, pthread_bug_37410) {
2034f251bee5d51228217c1bf4dfc9219f3058bd3edElliott Hughes  // http://code.google.com/p/android/issues/detail?id=37410
2044f251bee5d51228217c1bf4dfc9219f3058bd3edElliott Hughes  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
205877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes  ASSERT_EXIT(TestBug37410::main(), ::testing::ExitedWithCode(0), "");
2064f251bee5d51228217c1bf4dfc9219f3058bd3edElliott Hughes}
207c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes
208c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughesstatic void* SignalHandlerFn(void* arg) {
209c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  sigset_t wait_set;
210c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  sigfillset(&wait_set);
211c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  return reinterpret_cast<void*>(sigwait(&wait_set, reinterpret_cast<int*>(arg)));
212c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes}
213c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes
214c5d028fc913de84a781bd61084bf7ae2182fd48eElliott HughesTEST(pthread, pthread_sigmask) {
21519e62325c268a668692e2b65fde2284079f369aaElliott Hughes  // Check that SIGUSR1 isn't blocked.
21619e62325c268a668692e2b65fde2284079f369aaElliott Hughes  sigset_t original_set;
21719e62325c268a668692e2b65fde2284079f369aaElliott Hughes  sigemptyset(&original_set);
21819e62325c268a668692e2b65fde2284079f369aaElliott Hughes  ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &original_set));
21919e62325c268a668692e2b65fde2284079f369aaElliott Hughes  ASSERT_FALSE(sigismember(&original_set, SIGUSR1));
22019e62325c268a668692e2b65fde2284079f369aaElliott Hughes
221c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  // Block SIGUSR1.
222c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  sigset_t set;
223c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  sigemptyset(&set);
224c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  sigaddset(&set, SIGUSR1);
225c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &set, NULL));
226c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes
22719e62325c268a668692e2b65fde2284079f369aaElliott Hughes  // Check that SIGUSR1 is blocked.
22819e62325c268a668692e2b65fde2284079f369aaElliott Hughes  sigset_t final_set;
22919e62325c268a668692e2b65fde2284079f369aaElliott Hughes  sigemptyset(&final_set);
23019e62325c268a668692e2b65fde2284079f369aaElliott Hughes  ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &final_set));
23119e62325c268a668692e2b65fde2284079f369aaElliott Hughes  ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
23219e62325c268a668692e2b65fde2284079f369aaElliott Hughes  // ...and that sigprocmask agrees with pthread_sigmask.
23319e62325c268a668692e2b65fde2284079f369aaElliott Hughes  sigemptyset(&final_set);
23419e62325c268a668692e2b65fde2284079f369aaElliott Hughes  ASSERT_EQ(0, sigprocmask(SIG_BLOCK, NULL, &final_set));
23519e62325c268a668692e2b65fde2284079f369aaElliott Hughes  ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
23619e62325c268a668692e2b65fde2284079f369aaElliott Hughes
237c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  // Spawn a thread that calls sigwait and tells us what it received.
238c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  pthread_t signal_thread;
239c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  int received_signal = -1;
240c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  ASSERT_EQ(0, pthread_create(&signal_thread, NULL, SignalHandlerFn, &received_signal));
241c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes
242c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  // Send that thread SIGUSR1.
243c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  pthread_kill(signal_thread, SIGUSR1);
244c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes
245c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  // See what it got.
246c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  void* join_result;
247c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  ASSERT_EQ(0, pthread_join(signal_thread, &join_result));
248c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  ASSERT_EQ(SIGUSR1, received_signal);
2495b9310e502003e584bcb3a028ca3db7aa4d3f01bElliott Hughes  ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
25019e62325c268a668692e2b65fde2284079f369aaElliott Hughes
25119e62325c268a668692e2b65fde2284079f369aaElliott Hughes  // Restore the original signal mask.
25219e62325c268a668692e2b65fde2284079f369aaElliott Hughes  ASSERT_EQ(0, pthread_sigmask(SIG_SETMASK, &original_set, NULL));
253c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes}
2545e3fc43ddeada547a155c6f561a12ff0b16e02d3Elliott Hughes
255f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#if defined(__BIONIC__)
25670b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughesextern "C" pid_t __bionic_clone(int flags, void* child_stack, pid_t* parent_tid, void* tls, pid_t* child_tid, int (*fn)(void*), void* arg);
257f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#endif // __BIONIC__
258f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris
25970b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott HughesTEST(pthread, __bionic_clone) {
260f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#if defined(__BIONIC__)
26170b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes  // Check that our hand-written clone assembler sets errno correctly on failure.
2625e3fc43ddeada547a155c6f561a12ff0b16e02d3Elliott Hughes  uintptr_t fake_child_stack[16];
2635e3fc43ddeada547a155c6f561a12ff0b16e02d3Elliott Hughes  errno = 0;
264dd00364807020a244aa8be4f3481f7ec0fefcc79Chris Dearman  ASSERT_EQ(-1, __bionic_clone(CLONE_THREAD, &fake_child_stack[16], NULL, NULL, NULL, NULL, NULL));
2655e3fc43ddeada547a155c6f561a12ff0b16e02d3Elliott Hughes  ASSERT_EQ(EINVAL, errno);
266f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#else // __BIONIC__
267f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris  GTEST_LOG_(INFO) << "This test does nothing.\n";
268f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#endif // __BIONIC__
2695e3fc43ddeada547a155c6f561a12ff0b16e02d3Elliott Hughes}
2703e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes
2713e898476c7230b60a0f76968e64ff25f475b48c0Elliott HughesTEST(pthread, pthread_setname_np__too_long) {
272f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#if defined(__BIONIC__) // Not all build servers have a new enough glibc? TODO: remove when they're on gprecise.
2733e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes  ASSERT_EQ(ERANGE, pthread_setname_np(pthread_self(), "this name is far too long for linux"));
274f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#else // __BIONIC__
275f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris  GTEST_LOG_(INFO) << "This test does nothing.\n";
276f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#endif // __BIONIC__
2773e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes}
2783e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes
2793e898476c7230b60a0f76968e64ff25f475b48c0Elliott HughesTEST(pthread, pthread_setname_np__self) {
280f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#if defined(__BIONIC__) // Not all build servers have a new enough glibc? TODO: remove when they're on gprecise.
2813e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes  ASSERT_EQ(0, pthread_setname_np(pthread_self(), "short 1"));
282f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#else // __BIONIC__
283f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris  GTEST_LOG_(INFO) << "This test does nothing.\n";
284f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#endif // __BIONIC__
2853e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes}
2863e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes
2873e898476c7230b60a0f76968e64ff25f475b48c0Elliott HughesTEST(pthread, pthread_setname_np__other) {
288f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#if defined(__BIONIC__) // Not all build servers have a new enough glibc? TODO: remove when they're on gprecise.
28940eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes  // Emulator kernels don't currently support setting the name of other threads.
29040eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes  char* filename = NULL;
29140eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes  asprintf(&filename, "/proc/self/task/%d/comm", gettid());
29240eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes  struct stat sb;
29340eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes  bool has_comm = (stat(filename, &sb) != -1);
29440eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes  free(filename);
29540eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes
29640eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes  if (has_comm) {
29740eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes    pthread_t t1;
29840eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes    ASSERT_EQ(0, pthread_create(&t1, NULL, SleepFn, reinterpret_cast<void*>(5)));
29940eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes    ASSERT_EQ(0, pthread_setname_np(t1, "short 2"));
30040eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes  } else {
30140eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes    fprintf(stderr, "skipping test: this kernel doesn't have /proc/self/task/tid/comm files!\n");
30240eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes  }
303f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#else // __BIONIC__
304f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris  GTEST_LOG_(INFO) << "This test does nothing.\n";
305f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#endif // __BIONIC__
3063e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes}
3073e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes
3083e898476c7230b60a0f76968e64ff25f475b48c0Elliott HughesTEST(pthread, pthread_setname_np__no_such_thread) {
309f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#if defined(__BIONIC__) // Not all build servers have a new enough glibc? TODO: remove when they're on gprecise.
3109d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  pthread_t dead_thread;
3119d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  MakeDeadThread(dead_thread);
3123e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes
3133e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes  // Call pthread_setname_np after thread has already exited.
314a41ba2f0bfc4fce1ce8f06a9c289102c440c929dElliott Hughes  ASSERT_EQ(ESRCH, pthread_setname_np(dead_thread, "short 3"));
315f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#else // __BIONIC__
316f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris  GTEST_LOG_(INFO) << "This test does nothing.\n";
317f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#endif // __BIONIC__
3183e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes}
3199d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes
3209d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott HughesTEST(pthread, pthread_kill__0) {
3219d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  // Signal 0 just tests that the thread exists, so it's safe to call on ourselves.
3229d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  ASSERT_EQ(0, pthread_kill(pthread_self(), 0));
3239d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes}
3249d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes
3259d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott HughesTEST(pthread, pthread_kill__invalid_signal) {
3269d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  ASSERT_EQ(EINVAL, pthread_kill(pthread_self(), -1));
3279d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes}
3289d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes
329fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughesstatic void pthread_kill__in_signal_handler_helper(int signal_number) {
330fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes  static int count = 0;
331fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes  ASSERT_EQ(SIGALRM, signal_number);
332fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes  if (++count == 1) {
333fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes    // Can we call pthread_kill from a signal handler?
334fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes    ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
335fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes  }
336fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes}
337fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes
338fae89fc4042ee4c360842234dfda7831c313bd44Elliott HughesTEST(pthread, pthread_kill__in_signal_handler) {
339fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes  struct sigaction action;
340c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes  struct sigaction original_action;
341fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes  sigemptyset(&action.sa_mask);
342fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes  action.sa_flags = 0;
343fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes  action.sa_handler = pthread_kill__in_signal_handler_helper;
344c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes  ASSERT_EQ(0, sigaction(SIGALRM, &action, &original_action));
345fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes  ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
346c7e9b2331771e5e87c34a8ee3dc6cc41d35b02feElliott Hughes  ASSERT_EQ(0, sigaction(SIGALRM, &original_action, NULL));
347fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes}
348fae89fc4042ee4c360842234dfda7831c313bd44Elliott Hughes
3499d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott HughesTEST(pthread, pthread_detach__no_such_thread) {
3509d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  pthread_t dead_thread;
3519d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  MakeDeadThread(dead_thread);
3529d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes
3539d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  ASSERT_EQ(ESRCH, pthread_detach(dead_thread));
3549d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes}
3559d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes
3569b06cc3c1b2c4e2b08582f3fc9393a05aa589766Jeff HaoTEST(pthread, pthread_getcpuclockid__clock_gettime) {
3579b06cc3c1b2c4e2b08582f3fc9393a05aa589766Jeff Hao  pthread_t t;
3589b06cc3c1b2c4e2b08582f3fc9393a05aa589766Jeff Hao  ASSERT_EQ(0, pthread_create(&t, NULL, SleepFn, reinterpret_cast<void*>(5)));
3599b06cc3c1b2c4e2b08582f3fc9393a05aa589766Jeff Hao
3609b06cc3c1b2c4e2b08582f3fc9393a05aa589766Jeff Hao  clockid_t c;
3619b06cc3c1b2c4e2b08582f3fc9393a05aa589766Jeff Hao  ASSERT_EQ(0, pthread_getcpuclockid(t, &c));
3629b06cc3c1b2c4e2b08582f3fc9393a05aa589766Jeff Hao  timespec ts;
3639b06cc3c1b2c4e2b08582f3fc9393a05aa589766Jeff Hao  ASSERT_EQ(0, clock_gettime(c, &ts));
3649b06cc3c1b2c4e2b08582f3fc9393a05aa589766Jeff Hao}
3659b06cc3c1b2c4e2b08582f3fc9393a05aa589766Jeff Hao
3669d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott HughesTEST(pthread, pthread_getcpuclockid__no_such_thread) {
3679d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  pthread_t dead_thread;
3689d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  MakeDeadThread(dead_thread);
3699d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes
3709d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  clockid_t c;
3719d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  ASSERT_EQ(ESRCH, pthread_getcpuclockid(dead_thread, &c));
3729d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes}
3739d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes
3749d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott HughesTEST(pthread, pthread_getschedparam__no_such_thread) {
3759d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  pthread_t dead_thread;
3769d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  MakeDeadThread(dead_thread);
3779d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes
3789d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  int policy;
3799d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  sched_param param;
3809d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  ASSERT_EQ(ESRCH, pthread_getschedparam(dead_thread, &policy, &param));
3819d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes}
3829d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes
3839d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott HughesTEST(pthread, pthread_setschedparam__no_such_thread) {
3849d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  pthread_t dead_thread;
3859d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  MakeDeadThread(dead_thread);
3869d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes
3879d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  int policy = 0;
3889d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  sched_param param;
3899d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  ASSERT_EQ(ESRCH, pthread_setschedparam(dead_thread, policy, &param));
3909d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes}
3919d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes
3929d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott HughesTEST(pthread, pthread_join__no_such_thread) {
3939d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  pthread_t dead_thread;
3949d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  MakeDeadThread(dead_thread);
3959d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes
3969d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  void* result;
3979d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  ASSERT_EQ(ESRCH, pthread_join(dead_thread, &result));
3989d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes}
3999d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes
4009d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott HughesTEST(pthread, pthread_kill__no_such_thread) {
4019d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  pthread_t dead_thread;
4029d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  MakeDeadThread(dead_thread);
4039d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes
4049d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes  ASSERT_EQ(ESRCH, pthread_kill(dead_thread, 0));
4059d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes}
4060f020d18b138e24b1fe34074808e07ac412f35a4msg
4070f020d18b138e24b1fe34074808e07ac412f35a4msgTEST(pthread, pthread_join__multijoin) {
4080f020d18b138e24b1fe34074808e07ac412f35a4msg  bool done = false;
4090f020d18b138e24b1fe34074808e07ac412f35a4msg
4100f020d18b138e24b1fe34074808e07ac412f35a4msg  pthread_t t1;
4110f020d18b138e24b1fe34074808e07ac412f35a4msg  ASSERT_EQ(0, pthread_create(&t1, NULL, SpinFn, &done));
4120f020d18b138e24b1fe34074808e07ac412f35a4msg
4130f020d18b138e24b1fe34074808e07ac412f35a4msg  pthread_t t2;
4140f020d18b138e24b1fe34074808e07ac412f35a4msg  ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
4150f020d18b138e24b1fe34074808e07ac412f35a4msg
4160f020d18b138e24b1fe34074808e07ac412f35a4msg  sleep(1); // (Give t2 a chance to call pthread_join.)
4170f020d18b138e24b1fe34074808e07ac412f35a4msg
4180f020d18b138e24b1fe34074808e07ac412f35a4msg  // Multiple joins to the same thread should fail.
4190f020d18b138e24b1fe34074808e07ac412f35a4msg  ASSERT_EQ(EINVAL, pthread_join(t1, NULL));
4200f020d18b138e24b1fe34074808e07ac412f35a4msg
4210f020d18b138e24b1fe34074808e07ac412f35a4msg  done = true;
4220f020d18b138e24b1fe34074808e07ac412f35a4msg
4230f020d18b138e24b1fe34074808e07ac412f35a4msg  // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes).
4240f020d18b138e24b1fe34074808e07ac412f35a4msg  void* join_result;
4250f020d18b138e24b1fe34074808e07ac412f35a4msg  ASSERT_EQ(0, pthread_join(t2, &join_result));
4265b9310e502003e584bcb3a028ca3db7aa4d3f01bElliott Hughes  ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
4270f020d18b138e24b1fe34074808e07ac412f35a4msg}
428b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes
42970b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott HughesTEST(pthread, pthread_join__race) {
43070b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes  // http://b/11693195 --- pthread_join could return before the thread had actually exited.
43170b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes  // If the joiner unmapped the thread's stack, that could lead to SIGSEGV in the thread.
43270b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes  for (size_t i = 0; i < 1024; ++i) {
43370b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes    size_t stack_size = 64*1024;
43470b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes    void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
43570b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes
43670b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes    pthread_attr_t a;
43770b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes    pthread_attr_init(&a);
43870b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes    pthread_attr_setstack(&a, stack, stack_size);
43970b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes
44070b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes    pthread_t t;
44170b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes    ASSERT_EQ(0, pthread_create(&t, &a, IdFn, NULL));
44270b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes    ASSERT_EQ(0, pthread_join(t, NULL));
44370b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes    ASSERT_EQ(0, munmap(stack, stack_size));
44470b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes  }
44570b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes}
44670b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes
447b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughesstatic void* GetActualGuardSizeFn(void* arg) {
448b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  pthread_attr_t attributes;
449b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  pthread_getattr_np(pthread_self(), &attributes);
450b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  pthread_attr_getguardsize(&attributes, reinterpret_cast<size_t*>(arg));
451b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  return NULL;
452b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes}
453b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes
454b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughesstatic size_t GetActualGuardSize(const pthread_attr_t& attributes) {
455b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  size_t result;
456b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  pthread_t t;
457b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  pthread_create(&t, &attributes, GetActualGuardSizeFn, &result);
458b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  void* join_result;
459b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  pthread_join(t, &join_result);
460b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  return result;
461b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes}
462b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes
463b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughesstatic void* GetActualStackSizeFn(void* arg) {
464b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  pthread_attr_t attributes;
465b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  pthread_getattr_np(pthread_self(), &attributes);
466b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  pthread_attr_getstacksize(&attributes, reinterpret_cast<size_t*>(arg));
467b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  return NULL;
468b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes}
469b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes
470b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughesstatic size_t GetActualStackSize(const pthread_attr_t& attributes) {
471b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  size_t result;
472b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  pthread_t t;
473b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  pthread_create(&t, &attributes, GetActualStackSizeFn, &result);
474b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  void* join_result;
475b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  pthread_join(t, &join_result);
476b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  return result;
477b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes}
478b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes
479b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott HughesTEST(pthread, pthread_attr_setguardsize) {
480b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  pthread_attr_t attributes;
481b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(0, pthread_attr_init(&attributes));
482b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes
483b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  // Get the default guard size.
484b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  size_t default_guard_size;
485b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &default_guard_size));
486b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes
487b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  // No such thing as too small: will be rounded up to one page by pthread_create.
488b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 128));
489b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  size_t guard_size;
490b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
491b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(128U, guard_size);
492b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(4096U, GetActualGuardSize(attributes));
493b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes
494b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  // Large enough and a multiple of the page size.
495b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024));
496b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
497b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(32*1024U, guard_size);
498b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes
499b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  // Large enough but not a multiple of the page size; will be rounded up by pthread_create.
500b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024 + 1));
501b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
502b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(32*1024U + 1, guard_size);
503b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes}
504b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes
505b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott HughesTEST(pthread, pthread_attr_setstacksize) {
506b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  pthread_attr_t attributes;
507b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(0, pthread_attr_init(&attributes));
508b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes
509b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  // Get the default stack size.
510b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  size_t default_stack_size;
511b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &default_stack_size));
512b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes
513b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  // Too small.
514b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(EINVAL, pthread_attr_setstacksize(&attributes, 128));
515b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  size_t stack_size;
516b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
517b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(default_stack_size, stack_size);
518b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_GE(GetActualStackSize(attributes), default_stack_size);
519b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes
520b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  // Large enough and a multiple of the page size.
521b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024));
522b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
523b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(32*1024U, stack_size);
524b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(GetActualStackSize(attributes), 32*1024U);
525b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes
526b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  // Large enough but not a multiple of the page size; will be rounded up by pthread_create.
527b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024 + 1));
528b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
529b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(32*1024U + 1, stack_size);
530f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#if defined(__BIONIC__)
531b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  // Bionic rounds up, which is what POSIX allows.
532b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(GetActualStackSize(attributes), (32 + 4)*1024U);
533f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#else // __BIONIC__
534b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  // glibc rounds down, in violation of POSIX. They document this in their BUGS section.
535b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  ASSERT_EQ(GetActualStackSize(attributes), 32*1024U);
536f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#endif // __BIONIC__
537b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes}
538c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
539c3f114037dbf028896310609fd28cf2b3da99c4dElliott HughesTEST(pthread, pthread_rwlock_smoke) {
540c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  pthread_rwlock_t l;
541c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  ASSERT_EQ(0, pthread_rwlock_init(&l, NULL));
542c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
543c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
544c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  ASSERT_EQ(0, pthread_rwlock_unlock(&l));
545c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
546c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
547c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  ASSERT_EQ(0, pthread_rwlock_unlock(&l));
548c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
549c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  ASSERT_EQ(0, pthread_rwlock_destroy(&l));
550c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes}
551c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
552c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughesstatic int gOnceFnCallCount = 0;
553c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughesstatic void OnceFn() {
554c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  ++gOnceFnCallCount;
555c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes}
556c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
557c3f114037dbf028896310609fd28cf2b3da99c4dElliott HughesTEST(pthread, pthread_once_smoke) {
558c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  pthread_once_t once_control = PTHREAD_ONCE_INIT;
559c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
560c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
561c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  ASSERT_EQ(1, gOnceFnCallCount);
562c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes}
563c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
564c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughesstatic int gAtForkPrepareCalls = 0;
565c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughesstatic void AtForkPrepare1() { gAtForkPrepareCalls = (gAtForkPrepareCalls << 4) | 1; }
566c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughesstatic void AtForkPrepare2() { gAtForkPrepareCalls = (gAtForkPrepareCalls << 4) | 2; }
567c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughesstatic int gAtForkParentCalls = 0;
568c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughesstatic void AtForkParent1() { gAtForkParentCalls = (gAtForkParentCalls << 4) | 1; }
569c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughesstatic void AtForkParent2() { gAtForkParentCalls = (gAtForkParentCalls << 4) | 2; }
570c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughesstatic int gAtForkChildCalls = 0;
571c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughesstatic void AtForkChild1() { gAtForkChildCalls = (gAtForkChildCalls << 4) | 1; }
572c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughesstatic void AtForkChild2() { gAtForkChildCalls = (gAtForkChildCalls << 4) | 2; }
573c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
574c3f114037dbf028896310609fd28cf2b3da99c4dElliott HughesTEST(pthread, pthread_atfork) {
575c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1));
576c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  ASSERT_EQ(0, pthread_atfork(AtForkPrepare2, AtForkParent2, AtForkChild2));
577c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
578c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  int pid = fork();
579c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  ASSERT_NE(-1, pid) << strerror(errno);
580c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
581c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  // Child and parent calls are made in the order they were registered.
582c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  if (pid == 0) {
583c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes    ASSERT_EQ(0x12, gAtForkChildCalls);
584c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes    _exit(0);
585c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  }
586c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  ASSERT_EQ(0x12, gAtForkParentCalls);
587c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
588c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  // Prepare calls are made in the reverse order.
589c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  ASSERT_EQ(0x21, gAtForkPrepareCalls);
590c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes}
591c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
592c3f114037dbf028896310609fd28cf2b3da99c4dElliott HughesTEST(pthread, pthread_attr_getscope) {
593c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  pthread_attr_t attr;
594c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  ASSERT_EQ(0, pthread_attr_init(&attr));
595c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes
596c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  int scope;
597c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  ASSERT_EQ(0, pthread_attr_getscope(&attr, &scope));
598c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes  ASSERT_EQ(PTHREAD_SCOPE_SYSTEM, scope);
599c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes}
600