2 * Copyright 2017 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <folly/Baton.h>
20 #include <folly/ScopeGuard.h>
21 #include <folly/ThreadName.h>
22 #include <folly/portability/GTest.h>
25 using namespace folly;
27 static constexpr bool expectedSetOtherThreadNameResult =
28 #ifdef FOLLY_HAS_PTHREAD_SETNAME_NP_THREAD_NAME
31 false // This system has no known way to set the name of another thread
35 static constexpr bool expectedSetSelfThreadNameResult =
36 #if defined(FOLLY_HAS_PTHREAD_SETNAME_NP_THREAD_NAME) || \
37 defined(FOLLY_HAS_PTHREAD_SETNAME_NP_NAME)
40 false // This system has no known way to set its own thread name
44 TEST(ThreadName, setThreadName_self) {
46 EXPECT_EQ(expectedSetSelfThreadNameResult, setThreadName("rockin-thread"));
48 SCOPE_EXIT { th.join(); };
51 TEST(ThreadName, setThreadName_other_pthread) {
53 Baton<> let_thread_end;
56 handle = pthread_self();
58 let_thread_end.wait();
60 SCOPE_EXIT { th.join(); };
62 SCOPE_EXIT { let_thread_end.post(); };
64 expectedSetOtherThreadNameResult, setThreadName(handle, "rockin-thread"));
67 TEST(ThreadName, setThreadName_other_native) {
68 Baton<> let_thread_end;
70 let_thread_end.wait();
72 SCOPE_EXIT { th.join(); };
73 SCOPE_EXIT { let_thread_end.post(); };
75 expectedSetOtherThreadNameResult,
76 setThreadName(th.native_handle(), "rockin-thread"));
79 TEST(ThreadName, setThreadName_other_id) {
80 Baton<> let_thread_end;
82 let_thread_end.wait();
84 SCOPE_EXIT { th.join(); };
85 SCOPE_EXIT { let_thread_end.post(); };
87 expectedSetOtherThreadNameResult,
88 setThreadName(th.get_id(), "rockin-thread"));