1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2004-2011 New Dream Network
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 *
13 */
14
15
16 #ifndef CEPH_THREAD_H
17 #define CEPH_THREAD_H
18
19 #include <system_error>
20 #include <thread>
21
22 #include <pthread.h>
23 #include <sys/types.h>
24
25 #include "include/compat.h"
26
27 extern pid_t ceph_gettid();
28
29 class Thread {
30 private:
31 pthread_t thread_id;
32 pid_t pid;
33 int cpuid;
34 const char *thread_name;
35
36 void *entry_wrapper();
37
38 public:
39 Thread(const Thread&) = delete;
40 Thread& operator=(const Thread&) = delete;
41
42 Thread();
43 virtual ~Thread();
44
45 protected:
46 virtual void *entry() = 0;
47
48 private:
49 static void *_entry_func(void *arg);
50
51 public:
52 const pthread_t &get_thread_id() const;
53 pid_t get_pid() const { return pid; }
54 bool is_started() const;
55 bool am_self() const;
56 int kill(int signal);
57 int try_create(size_t stacksize);
58 void create(const char *name, size_t stacksize = 0);
59 int join(void **prval = 0);
60 int detach();
61 int set_affinity(int cpuid);
62 };
63
64 // Functions for with std::thread
65
66 void set_thread_name(std::thread& t, const std::string& s);
67 std::string get_thread_name(const std::thread& t);
68 void kill(std::thread& t, int signal);
69
70 template<typename Fun, typename... Args>
71 std::thread make_named_thread(const std::string& s,
72 Fun&& fun,
73 Args&& ...args) {
74 auto t = std::thread(std::forward<Fun>(fun),
75 std::forward<Args>(args)...);
76 set_thread_name(t, s);
77 return t;
78 }
79
80 #endif
81