--- /dev/null
+/*
+ * 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);
+}
+
+}
#include "folly/io/async/EventBase.h"
+#include "folly/ThreadName.h"
#include "folly/io/async/NotificationQueue.h"
#include <boost/static_assert.hpp>
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>(
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() {