pthread_test.cpp revision 3e898476c7230b60a0f76968e64ff25f475b48c0
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>
20bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes#include <pthread.h>
214d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes#include <unistd.h>
22bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes
23bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott HughesTEST(pthread, pthread_key_create) {
24bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes  pthread_key_t key;
25bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes  ASSERT_EQ(0, pthread_key_create(&key, NULL));
26bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes  ASSERT_EQ(0, pthread_key_delete(key));
27bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes  // Can't delete a key that's already been deleted.
28bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes  ASSERT_EQ(EINVAL, pthread_key_delete(key));
29bfeab1bbe7e8d0c08b7e3f46aedab64e3b2bf706Elliott Hughes}
304d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes
313e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes#if !defined(__GLIBC__) // glibc uses keys internally that its sysconf value doesn't account for.
3244b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott HughesTEST(pthread, pthread_key_create_lots) {
3344b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes  // We can allocate _SC_THREAD_KEYS_MAX keys.
3444b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes  std::vector<pthread_key_t> keys;
3544b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes  for (int i = 0; i < sysconf(_SC_THREAD_KEYS_MAX); ++i) {
3644b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes    pthread_key_t key;
373e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes    // If this fails, it's likely that GLOBAL_INIT_THREAD_LOCAL_BUFFER_COUNT is wrong.
383e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes    ASSERT_EQ(0, pthread_key_create(&key, NULL)) << i << " of " << sysconf(_SC_THREAD_KEYS_MAX);
3944b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes    keys.push_back(key);
4044b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes  }
4144b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes
4244b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes  // ...and that really is the maximum.
4344b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes  pthread_key_t key;
4444b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes  ASSERT_EQ(EAGAIN, pthread_key_create(&key, NULL));
4544b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes
4644b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes  // (Don't leak all those keys!)
4744b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes  for (size_t i = 0; i < keys.size(); ++i) {
4844b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes    ASSERT_EQ(0, pthread_key_delete(keys[i]));
4944b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes  }
5044b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes}
513e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes#endif
5244b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes
534d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughesstatic void* IdFn(void* arg) {
544d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  return arg;
554d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes}
564d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes
574d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughesstatic void* SleepFn(void* arg) {
584d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  sleep(reinterpret_cast<unsigned int>(arg));
594d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  return NULL;
604d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes}
614d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes
6210ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikovstatic void* SpinFn(void* arg) {
6310ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  volatile bool* b = reinterpret_cast<volatile bool*>(arg);
6410ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  while (!*b) {
6510ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  }
6610ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  return NULL;
6710ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov}
6810ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov
694d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughesstatic void* JoinFn(void* arg) {
704d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  return reinterpret_cast<void*>(pthread_join(reinterpret_cast<pthread_t>(arg), NULL));
714d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes}
724d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes
7310ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikovstatic void AssertDetached(pthread_t t, bool is_detached) {
7410ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  pthread_attr_t attr;
7510ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  ASSERT_EQ(0, pthread_getattr_np(t, &attr));
7610ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  int detach_state;
7710ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &detach_state));
7810ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  pthread_attr_destroy(&attr);
7910ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  ASSERT_EQ(is_detached, (detach_state == PTHREAD_CREATE_DETACHED));
8010ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov}
8110ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov
824d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott HughesTEST(pthread, pthread_create) {
834d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  void* expected_result = reinterpret_cast<void*>(123);
844d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  // Can we create a thread?
854d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  pthread_t t;
864d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, expected_result));
874d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  // If we join, do we get the expected value back?
884d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  void* result;
894d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  ASSERT_EQ(0, pthread_join(t, &result));
904d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  ASSERT_EQ(expected_result, result);
914d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes}
924d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes
933e898476c7230b60a0f76968e64ff25f475b48c0Elliott HughesTEST(pthread, pthread_create_EAGAIN) {
943e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes  pthread_attr_t attributes;
953e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes  ASSERT_EQ(0, pthread_attr_init(&attributes));
963e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes  ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, static_cast<size_t>(-1) & ~(getpagesize() - 1)));
973e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes
983e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes  pthread_t t;
993e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes  ASSERT_EQ(EAGAIN, pthread_create(&t, &attributes, IdFn, NULL));
1003e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes}
1013e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes
1024d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott HughesTEST(pthread, pthread_no_join_after_detach) {
1034d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  pthread_t t1;
1044d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  ASSERT_EQ(0, pthread_create(&t1, NULL, SleepFn, reinterpret_cast<void*>(5)));
1054d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes
1064d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  // After a pthread_detach...
1074d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  ASSERT_EQ(0, pthread_detach(t1));
10810ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  AssertDetached(t1, true);
1094d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes
1104d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  // ...pthread_join should fail.
1114d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  void* result;
1124d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  ASSERT_EQ(EINVAL, pthread_join(t1, &result));
1134d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes}
1144d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes
1154d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott HughesTEST(pthread, pthread_no_op_detach_after_join) {
11610ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  bool done = false;
11710ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov
1184d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  pthread_t t1;
11910ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  ASSERT_EQ(0, pthread_create(&t1, NULL, SpinFn, &done));
1204d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes
1214d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  // If thread 2 is already waiting to join thread 1...
1224d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  pthread_t t2;
1234d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
1244d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes
12510ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  sleep(1); // (Give t2 a chance to call pthread_join.)
12610ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov
12710ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  // ...a call to pthread_detach on thread 1 will "succeed" (silently fail)...
1284d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  ASSERT_EQ(0, pthread_detach(t1));
12910ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  AssertDetached(t1, false);
13010ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov
13110ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  done = true;
1324d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes
13310ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes).
1344d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  void* join_result;
1354d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes  ASSERT_EQ(0, pthread_join(t2, &join_result));
13610ce96944eaea4c459392952652fdb24742c9c29Sergey Melnikov  ASSERT_EQ(0, reinterpret_cast<int>(join_result));
1374d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8Elliott Hughes}
13814f19592ae7c819855052bcebc79de87069c2954Elliott Hughes
13914f19592ae7c819855052bcebc79de87069c2954Elliott HughesTEST(pthread, pthread_join_self) {
14014f19592ae7c819855052bcebc79de87069c2954Elliott Hughes  void* result;
14114f19592ae7c819855052bcebc79de87069c2954Elliott Hughes  ASSERT_EQ(EDEADLK, pthread_join(pthread_self(), &result));
14214f19592ae7c819855052bcebc79de87069c2954Elliott Hughes}
1434f251bee5d51228217c1bf4dfc9219f3058bd3edElliott Hughes
1444f251bee5d51228217c1bf4dfc9219f3058bd3edElliott Hughes#if __BIONIC__ // For some reason, gtest on bionic can cope with this but gtest on glibc can't.
1454f251bee5d51228217c1bf4dfc9219f3058bd3edElliott Hughes
1464f251bee5d51228217c1bf4dfc9219f3058bd3edElliott Hughesstatic void TestBug37410() {
1474f251bee5d51228217c1bf4dfc9219f3058bd3edElliott Hughes  pthread_t t1;
1484f251bee5d51228217c1bf4dfc9219f3058bd3edElliott Hughes  ASSERT_EQ(0, pthread_create(&t1, NULL, JoinFn, reinterpret_cast<void*>(pthread_self())));
1494f251bee5d51228217c1bf4dfc9219f3058bd3edElliott Hughes  pthread_exit(NULL);
1504f251bee5d51228217c1bf4dfc9219f3058bd3edElliott Hughes}
1514f251bee5d51228217c1bf4dfc9219f3058bd3edElliott Hughes
1524f251bee5d51228217c1bf4dfc9219f3058bd3edElliott Hughes// We have to say "DeathTest" here so gtest knows to run this test (which exits)
1534f251bee5d51228217c1bf4dfc9219f3058bd3edElliott Hughes// in its own process.
1544f251bee5d51228217c1bf4dfc9219f3058bd3edElliott HughesTEST(pthread_DeathTest, pthread_bug_37410) {
1554f251bee5d51228217c1bf4dfc9219f3058bd3edElliott Hughes  // http://code.google.com/p/android/issues/detail?id=37410
1564f251bee5d51228217c1bf4dfc9219f3058bd3edElliott Hughes  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
1574f251bee5d51228217c1bf4dfc9219f3058bd3edElliott Hughes  EXPECT_EXIT(TestBug37410(), ::testing::ExitedWithCode(0), "");
1584f251bee5d51228217c1bf4dfc9219f3058bd3edElliott Hughes}
1594f251bee5d51228217c1bf4dfc9219f3058bd3edElliott Hughes#endif
160c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes
161c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughesstatic void* SignalHandlerFn(void* arg) {
162c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  sigset_t wait_set;
163c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  sigfillset(&wait_set);
164c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  return reinterpret_cast<void*>(sigwait(&wait_set, reinterpret_cast<int*>(arg)));
165c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes}
166c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes
167c5d028fc913de84a781bd61084bf7ae2182fd48eElliott HughesTEST(pthread, pthread_sigmask) {
168c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  // Block SIGUSR1.
169c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  sigset_t set;
170c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  sigemptyset(&set);
171c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  sigaddset(&set, SIGUSR1);
172c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &set, NULL));
173c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes
174c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  // Spawn a thread that calls sigwait and tells us what it received.
175c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  pthread_t signal_thread;
176c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  int received_signal = -1;
177c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  ASSERT_EQ(0, pthread_create(&signal_thread, NULL, SignalHandlerFn, &received_signal));
178c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes
179c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  // Send that thread SIGUSR1.
180c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  pthread_kill(signal_thread, SIGUSR1);
181c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes
182c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  // See what it got.
183c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  void* join_result;
184c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  ASSERT_EQ(0, pthread_join(signal_thread, &join_result));
185c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  ASSERT_EQ(SIGUSR1, received_signal);
186c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes  ASSERT_EQ(0, reinterpret_cast<int>(join_result));
187c5d028fc913de84a781bd61084bf7ae2182fd48eElliott Hughes}
1885e3fc43ddeada547a155c6f561a12ff0b16e02d3Elliott Hughes
1893e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes#if __BIONIC__
1905e3fc43ddeada547a155c6f561a12ff0b16e02d3Elliott Hughesextern "C" int  __pthread_clone(int (*fn)(void*), void* child_stack, int flags, void* arg);
1915e3fc43ddeada547a155c6f561a12ff0b16e02d3Elliott HughesTEST(pthread, __pthread_clone) {
1925e3fc43ddeada547a155c6f561a12ff0b16e02d3Elliott Hughes  uintptr_t fake_child_stack[16];
1935e3fc43ddeada547a155c6f561a12ff0b16e02d3Elliott Hughes  errno = 0;
1945e3fc43ddeada547a155c6f561a12ff0b16e02d3Elliott Hughes  ASSERT_EQ(-1, __pthread_clone(NULL, &fake_child_stack[0], CLONE_THREAD, NULL));
1955e3fc43ddeada547a155c6f561a12ff0b16e02d3Elliott Hughes  ASSERT_EQ(EINVAL, errno);
1965e3fc43ddeada547a155c6f561a12ff0b16e02d3Elliott Hughes}
1975e3fc43ddeada547a155c6f561a12ff0b16e02d3Elliott Hughes#endif
1983e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes
1993e898476c7230b60a0f76968e64ff25f475b48c0Elliott HughesTEST(pthread, pthread_setname_np__too_long) {
2003e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes  ASSERT_EQ(ERANGE, pthread_setname_np(pthread_self(), "this name is far too long for linux"));
2013e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes}
2023e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes
2033e898476c7230b60a0f76968e64ff25f475b48c0Elliott HughesTEST(pthread, pthread_setname_np__self) {
2043e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes  ASSERT_EQ(0, pthread_setname_np(pthread_self(), "short 1"));
2053e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes}
2063e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes
2073e898476c7230b60a0f76968e64ff25f475b48c0Elliott HughesTEST(pthread, pthread_setname_np__other) {
2083e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes  pthread_t t1;
2093e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes  ASSERT_EQ(0, pthread_create(&t1, NULL, SleepFn, reinterpret_cast<void*>(5)));
2103e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes  ASSERT_EQ(0, pthread_setname_np(t1, "short 2"));
2113e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes}
2123e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes
2133e898476c7230b60a0f76968e64ff25f475b48c0Elliott HughesTEST(pthread, pthread_setname_np__no_such_thread) {
2143e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes  pthread_t t1;
2153e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes  ASSERT_EQ(0, pthread_create(&t1, NULL, IdFn, NULL));
2163e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes  void* result;
2173e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes  ASSERT_EQ(0, pthread_join(t1, &result));
2183e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes
2193e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes  // Call pthread_setname_np after thread has already exited.
2203e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes  ASSERT_EQ(ENOENT, pthread_setname_np(t1, "short 3"));
2213e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes}
222