]>
Commit | Line | Data |
---|---|---|
11fdf7f2 | 1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
7c673cae FG |
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 | ||
eafe8130 TL |
19 | #include <functional> |
20 | #include <string_view> | |
11fdf7f2 TL |
21 | #include <system_error> |
22 | #include <thread> | |
23 | ||
7c673cae FG |
24 | #include <pthread.h> |
25 | #include <sys/types.h> | |
26 | ||
11fdf7f2 TL |
27 | #include "include/compat.h" |
28 | ||
29 | extern pid_t ceph_gettid(); | |
30 | ||
7c673cae FG |
31 | class Thread { |
32 | private: | |
33 | pthread_t thread_id; | |
34 | pid_t pid; | |
7c673cae FG |
35 | int cpuid; |
36 | const char *thread_name; | |
37 | ||
38 | void *entry_wrapper(); | |
39 | ||
40 | public: | |
41 | Thread(const Thread&) = delete; | |
42 | Thread& operator=(const Thread&) = delete; | |
43 | ||
44 | Thread(); | |
45 | virtual ~Thread(); | |
46 | ||
47 | protected: | |
48 | virtual void *entry() = 0; | |
49 | ||
50 | private: | |
51 | static void *_entry_func(void *arg); | |
52 | ||
53 | public: | |
54 | const pthread_t &get_thread_id() const; | |
55 | pid_t get_pid() const { return pid; } | |
56 | bool is_started() const; | |
57 | bool am_self() const; | |
58 | int kill(int signal); | |
59 | int try_create(size_t stacksize); | |
60 | void create(const char *name, size_t stacksize = 0); | |
61 | int join(void **prval = 0); | |
62 | int detach(); | |
7c673cae FG |
63 | int set_affinity(int cpuid); |
64 | }; | |
65 | ||
11fdf7f2 TL |
66 | // Functions for with std::thread |
67 | ||
68 | void set_thread_name(std::thread& t, const std::string& s); | |
69 | std::string get_thread_name(const std::thread& t); | |
70 | void kill(std::thread& t, int signal); | |
71 | ||
72 | template<typename Fun, typename... Args> | |
eafe8130 | 73 | std::thread make_named_thread(std::string_view n, |
11fdf7f2 TL |
74 | Fun&& fun, |
75 | Args&& ...args) { | |
11fdf7f2 | 76 | |
eafe8130 TL |
77 | return std::thread([n = std::string(n)](auto&& fun, auto&& ...args) { |
78 | ceph_pthread_setname(pthread_self(), n.data()); | |
79 | std::invoke(std::forward<Fun>(fun), | |
80 | std::forward<Args>(args)...); | |
81 | }, std::forward<Fun>(fun), std::forward<Args>(args)...); | |
82 | } | |
7c673cae | 83 | #endif |