Commit ed92986b authored by Louis Brandy's avatar Louis Brandy Committed by Dave Watson

Move setPosixThreadName & friends to folly.

Summary: This was previously in thrift (and copied and pasted in several other places, including folly itself). Other potential open-source projects want this basic functionality so lets centralize it in folly instead of having potentially awkward dependencies on thrift (or copy/paste everywhere).

Test Plan: Build all the things. Run the tests.

Reviewed By: delong.j@fb.com

FB internal diff: D1297972
parent 5992ca78
/*
* Copyright 2014 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <pthread.h>
#include "Range.h"
namespace folly {
inline bool setThreadName(pthread_t id, StringPiece name) {
#if (defined(__GLIBC__) && __GLIBC_PREREQ(2, 12))
return 0 == pthread_setname_np(id, name.fbstr().substr(0, 15).c_str());
#else
return false;
#endif
}
inline bool setThreadName(StringPiece name) {
return setThreadName(pthread_self(), name);
}
}
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include "folly/io/async/EventBase.h" #include "folly/io/async/EventBase.h"
#include "folly/ThreadName.h"
#include "folly/io/async/NotificationQueue.h" #include "folly/io/async/NotificationQueue.h"
#include <boost/static_assert.hpp> #include <boost/static_assert.hpp>
...@@ -242,11 +243,9 @@ bool EventBase::loopBody(bool once) { ...@@ -242,11 +243,9 @@ bool EventBase::loopBody(bool once) {
loopThread_.store(pthread_self(), std::memory_order_release); loopThread_.store(pthread_self(), std::memory_order_release);
#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 12)
if (!name_.empty()) { if (!name_.empty()) {
pthread_setname_np(pthread_self(), name_.c_str()); setThreadName(name_);
} }
#endif
auto prev = std::chrono::steady_clock::now(); auto prev = std::chrono::steady_clock::now();
int64_t idleStart = std::chrono::duration_cast<std::chrono::microseconds>( int64_t idleStart = std::chrono::duration_cast<std::chrono::microseconds>(
...@@ -672,12 +671,11 @@ void EventBase::cancelTimeout(AsyncTimeout* obj) { ...@@ -672,12 +671,11 @@ void EventBase::cancelTimeout(AsyncTimeout* obj) {
void EventBase::setName(const std::string& name) { void EventBase::setName(const std::string& name) {
assert(isInEventBaseThread()); assert(isInEventBaseThread());
name_ = name; name_ = name;
#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 12)
if (isRunning()) { if (isRunning()) {
pthread_setname_np(loopThread_.load(std::memory_order_relaxed), setThreadName(loopThread_.load(std::memory_order_relaxed),
name_.c_str()); name_);
} }
#endif
} }
const std::string& EventBase::getName() { const std::string& EventBase::getName() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment