]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common: Add make_named_thread
authorAdam C. Emerson <aemerson@redhat.com>
Tue, 16 Feb 2016 19:43:51 +0000 (14:43 -0500)
committerAdam C. Emerson <aemerson@redhat.com>
Fri, 16 Feb 2018 17:41:41 +0000 (12:41 -0500)
To allow us to make a std::thread with a name.

Signed-off-by: Adam C. Emerson <aemerson@redhat.com>
src/common/Thread.cc
src/common/Thread.h

index a58c7ba252ccefb8115cc679e492fdf7a4834af2..827443a13ee16c2012e6c6b28f4080680748d2d1 100644 (file)
@@ -12,7 +12,8 @@
  *
  */
 
-#include "include/compat.h"
+#include <signal.h>
+
 #include "common/Thread.h"
 #include "common/code_environment.h"
 #include "common/debug.h"
@@ -198,3 +199,32 @@ int Thread::set_affinity(int id)
     r = _set_affinity(id);
   return r;
 }
+
+// Functions for std::thread
+// =========================
+
+void set_thread_name(std::thread& t, const std::string& s) {
+  int r = ceph_pthread_setname(t.native_handle(), s.c_str());
+  if (r != 0) {
+    throw std::system_error(r, std::generic_category());
+  }
+}
+std::string get_thread_name(const std::thread& t) {
+  std::string s(256, '\0');
+
+  int r = ceph_pthread_getname(const_cast<std::thread&>(t).native_handle(),
+                              s.data(), s.length());
+  if (r != 0) {
+    throw std::system_error(r, std::generic_category());
+  }
+  s.resize(std::strlen(s.data()));
+  return s;
+}
+
+void kill(std::thread& t, int signal)
+{
+  auto r = pthread_kill(t.native_handle(), signal);
+  if (r != 0) {
+    throw std::system_error(r, std::generic_category());
+  }
+}
index 54fd750925fe731800e9e4276ec4ffb9e02e0a8a..8e7cadd14c3bdab76e1ca9007e6acd1bb6856589 100644 (file)
@@ -1,4 +1,4 @@
- // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
 // vim: ts=8 sw=2 smarttab
 /*
  * Ceph - scalable distributed file system
 #ifndef CEPH_THREAD_H
 #define CEPH_THREAD_H
 
+#include <system_error>
+#include <thread>
+
 #include <pthread.h>
 #include <sys/types.h>
 
+#include "include/compat.h"
+
 class Thread {
  private:
   pthread_t thread_id;
@@ -56,4 +61,20 @@ class Thread {
   int set_affinity(int cpuid);
 };
 
+// Functions for with std::thread
+
+void set_thread_name(std::thread& t, const std::string& s);
+std::string get_thread_name(const std::thread& t);
+void kill(std::thread& t, int signal);
+
+template<typename Fun, typename... Args>
+std::thread make_named_thread(const std::string& s,
+                             Fun&& fun,
+                             Args&& ...args) {
+  auto t = std::thread(std::forward<Fun>(fun),
+                      std::forward<Args>(args)...);
+  set_thread_name(t, s);
+  return t;
+}
+
 #endif