*
*/
-#include "include/compat.h"
+#include <signal.h>
+
#include "common/Thread.h"
#include "common/code_environment.h"
#include "common/debug.h"
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());
+ }
+}
- // -*- 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;
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