1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2004-2011 New Dream Network
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software
* Foundation. See file COPYING.
*
*/
#include <signal.h>
#include <unistd.h>
#ifdef __linux__
#include <sys/syscall.h> /* For SYS_xxx definitions */
#endif
#ifdef WITH_SEASTAR
#include "crimson/os/alienstore/alien_store.h"
#endif
#include "common/Thread.h"
#include "common/code_environment.h"
#include "common/debug.h"
#include "common/signal.h"
#ifdef HAVE_SCHED
#include <sched.h>
#endif
pid_t ceph_gettid(void)
{
#ifdef __linux__
return syscall(SYS_gettid);
#else
return -ENOSYS;
#endif
}
static int _set_affinity(int id)
{
#ifdef HAVE_SCHED
if (id >= 0 && id < CPU_SETSIZE) {
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(id, &cpuset);
if (sched_setaffinity(0, sizeof(cpuset), &cpuset) < 0)
return -errno;
/* guaranteed to take effect immediately */
sched_yield();
}
#endif
return 0;
}
Thread::Thread()
: thread_id(0),
pid(0),
cpuid(-1)
{
}
Thread::~Thread()
{
}
void *Thread::_entry_func(void *arg) {
void *r = ((Thread*)arg)->entry_wrapper();
return r;
}
void *Thread::entry_wrapper()
{
int p = ceph_gettid(); // may return -ENOSYS on other platforms
if (p > 0)
pid = p;
if (pid && cpuid >= 0)
_set_affinity(cpuid);
ceph_pthread_setname(pthread_self(), thread_name.c_str());
return entry();
}
const pthread_t &Thread::get_thread_id() const
{
return thread_id;
}
bool Thread::is_started() const
{
return thread_id != 0;
}
bool Thread::am_self() const
{
return (pthread_self() == thread_id);
}
int Thread::kill(int signal)
{
if (thread_id)
return pthread_kill(thread_id, signal);
else
return -EINVAL;
}
int Thread::try_create(size_t stacksize)
{
pthread_attr_t *thread_attr = NULL;
pthread_attr_t thread_attr_loc;
stacksize &= CEPH_PAGE_MASK; // must be multiple of page
if (stacksize) {
thread_attr = &thread_attr_loc;
pthread_attr_init(thread_attr);
pthread_attr_setstacksize(thread_attr, stacksize);
}
int r;
// The child thread will inherit our signal mask. Set our signal mask to
// the set of signals we want to block. (It's ok to block signals more
// signals than usual for a little while-- they will just be delivered to
// another thread or delieverd to this thread later.)
#ifndef _WIN32
sigset_t old_sigset;
if (g_code_env == CODE_ENVIRONMENT_LIBRARY) {
block_signals(NULL, &old_sigset);
}
else {
int to_block[] = { SIGPIPE , 0 };
block_signals(to_block, &old_sigset);
}
r = pthread_create(&thread_id, thread_attr, _entry_func, (void*)this);
restore_sigset(&old_sigset);
#else
r = pthread_create(&thread_id, thread_attr, _entry_func, (void*)this);
#endif
if (thread_attr) {
pthread_attr_destroy(thread_attr);
}
return r;
}
void Thread::create(const char *name, size_t stacksize)
{
ceph_assert(strlen(name) < 16);
thread_name = name;
int ret = try_create(stacksize);
if (ret != 0) {
char buf[256];
snprintf(buf, sizeof(buf), "Thread::try_create(): pthread_create "
"failed with error %d", ret);
dout_emergency(buf);
ceph_assert(ret == 0);
}
}
int Thread::join(void **prval)
{
if (thread_id == 0) {
ceph_abort_msg("join on thread that was never started");
return -EINVAL;
}
int status = pthread_join(thread_id, prval);
if (status != 0) {
char buf[256];
snprintf(buf, sizeof(buf), "Thread::join(): pthread_join "
"failed with error %d\n", status);
dout_emergency(buf);
ceph_assert(status == 0);
}
thread_id = 0;
return status;
}
int Thread::detach()
{
return pthread_detach(thread_id);
}
int Thread::set_affinity(int id)
{
int r = 0;
cpuid = id;
if (pid && ceph_gettid() == pid)
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());
}
}
|