From: Louis Brandy Date: Sat, 26 Apr 2014 00:05:02 +0000 (-0700) Subject: Move setPosixThreadName & friends to folly. X-Git-Tag: v0.22.0~568 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;ds=sidebyside;h=ed92986bddcc28c8c8e8de5769681c0cfdbc84e7;p=folly.git 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 --- diff --git a/folly/ThreadName.h b/folly/ThreadName.h new file mode 100644 index 00000000..2d30efa0 --- /dev/null +++ b/folly/ThreadName.h @@ -0,0 +1,36 @@ +/* + * 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 +#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); +} + +} diff --git a/folly/io/async/EventBase.cpp b/folly/io/async/EventBase.cpp index 71b05c43..932a63ac 100644 --- a/folly/io/async/EventBase.cpp +++ b/folly/io/async/EventBase.cpp @@ -20,6 +20,7 @@ #include "folly/io/async/EventBase.h" +#include "folly/ThreadName.h" #include "folly/io/async/NotificationQueue.h" #include @@ -242,11 +243,9 @@ bool EventBase::loopBody(bool once) { loopThread_.store(pthread_self(), std::memory_order_release); -#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 12) if (!name_.empty()) { - pthread_setname_np(pthread_self(), name_.c_str()); + setThreadName(name_); } -#endif auto prev = std::chrono::steady_clock::now(); int64_t idleStart = std::chrono::duration_cast( @@ -672,12 +671,11 @@ void EventBase::cancelTimeout(AsyncTimeout* obj) { void EventBase::setName(const std::string& name) { assert(isInEventBaseThread()); name_ = name; -#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 12) + if (isRunning()) { - pthread_setname_np(loopThread_.load(std::memory_order_relaxed), - name_.c_str()); + setThreadName(loopThread_.load(std::memory_order_relaxed), + name_); } -#endif } const std::string& EventBase::getName() {