Move setPosixThreadName & friends to folly.
authorLouis Brandy <ldbrandy@fb.com>
Sat, 26 Apr 2014 00:05:02 +0000 (17:05 -0700)
committerDave Watson <davejwatson@fb.com>
Tue, 20 May 2014 19:53:57 +0000 (12:53 -0700)
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

folly/ThreadName.h [new file with mode: 0644]
folly/io/async/EventBase.cpp

diff --git a/folly/ThreadName.h b/folly/ThreadName.h
new file mode 100644 (file)
index 0000000..2d30efa
--- /dev/null
@@ -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 <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);
+}
+
+}
index 71b05c43048c1138570fcf14362c17e1013e5a3f..932a63ac5e75abe7bf104a43e18d0b2477db0850 100644 (file)
@@ -20,6 +20,7 @@
 
 #include "folly/io/async/EventBase.h"
 
+#include "folly/ThreadName.h"
 #include "folly/io/async/NotificationQueue.h"
 
 #include <boost/static_assert.hpp>
@@ -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<std::chrono::microseconds>(
@@ -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() {