Skip to content

Commit b5c9f7b

Browse files
committed
remove SetSelfThreadAffinity SetSelfThreadName method
1 parent b4d92b9 commit b5c9f7b

File tree

3 files changed

+35
-63
lines changed

3 files changed

+35
-63
lines changed

myframe/common.cpp

Lines changed: 32 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -131,39 +131,12 @@ std::vector<std::string_view> Common::SplitMsgName(const std::string& name) {
131131
// 可以通过std::thread::hardware_concurrency()获得核心数
132132
int Common::SetThreadAffinity(std::thread* t, int cpu_core) {
133133
#if defined(MYFRAME_OS_WINDOWS)
134-
auto hThread = t->native_handle();
135-
DWORD_PTR mask = 1 << cpu_core;
136-
if (SetThreadAffinityMask(hThread, mask) == 0) {
137-
return -1;
138-
}
139-
return 0;
140-
#elif defined(MYFRAME_OS_MACOSX)
141-
thread_affinity_policy policy;
142-
policy.affinity_tag = cpu_core;
143-
auto handle = pthread_mach_thread_np(t->native_handle());
144-
kern_return_t kr = thread_policy_set(
145-
handle,
146-
THREAD_AFFINITY_POLICY,
147-
reinterpret_cast<thread_policy_t>(&policy),
148-
THREAD_AFFINITY_POLICY_COUNT);
149-
mach_port_deallocate(mach_task_self(), handle);
150-
if (kr != KERN_SUCCESS) {
151-
return -1;
134+
HANDLE hThread;
135+
if (t == nullptr) {
136+
hThread = GetCurrentThread();
137+
} else {
138+
hThread = t->native_handle();
152139
}
153-
return 0;
154-
#else
155-
cpu_set_t cpuset;
156-
CPU_ZERO(&cpuset);
157-
CPU_SET(cpu_core, &cpuset);
158-
auto handle = t->native_handle();
159-
int result = pthread_setaffinity_np(handle, sizeof(cpu_set_t), &cpuset);
160-
return result;
161-
#endif
162-
}
163-
164-
int Common::SetSelfThreadAffinity(int cpu_core) {
165-
#if defined(MYFRAME_OS_WINDOWS)
166-
auto hThread = GetCurrentThread();
167140
DWORD_PTR mask = 1 << cpu_core;
168141
if (SetThreadAffinityMask(hThread, mask) == 0) {
169142
return -1;
@@ -172,7 +145,12 @@ int Common::SetSelfThreadAffinity(int cpu_core) {
172145
#elif defined(MYFRAME_OS_MACOSX)
173146
thread_affinity_policy policy;
174147
policy.affinity_tag = cpu_core;
175-
auto handle = mach_thread_self();
148+
thread_t handle;
149+
if (t == nullptr) {
150+
handle = mach_thread_self();
151+
} else {
152+
handle = pthread_mach_thread_np(t->native_handle());
153+
}
176154
kern_return_t kr = thread_policy_set(
177155
handle,
178156
THREAD_AFFINITY_POLICY,
@@ -187,7 +165,12 @@ int Common::SetSelfThreadAffinity(int cpu_core) {
187165
cpu_set_t cpuset;
188166
CPU_ZERO(&cpuset);
189167
CPU_SET(cpu_core, &cpuset);
190-
auto handle = pthread_self();
168+
int handle;
169+
if (t == nullptr) {
170+
handle = pthread_self();
171+
} else {
172+
handle = t->native_handle();
173+
}
191174
int result = pthread_setaffinity_np(handle, sizeof(cpu_set_t), &cpuset);
192175
return result;
193176
#endif
@@ -201,41 +184,32 @@ int Common::SetThreadName(std::thread* t, const std::string& name) {
201184
}
202185
wchar_t* wide_name = new wchar_t[len];
203186
MultiByteToWideChar(CP_UTF8, 0, name.c_str(), -1, wide_name, len);
204-
auto handle = t->native_handle();
187+
HANDLE handle;
188+
if (t == nullptr) {
189+
handle = GetCurrentThread();
190+
} else {
191+
handle = t->native_handle();
192+
}
205193
auto res = SetThreadDescription(handle, wide_name);
206194
delete[] wide_name;
207195
if (FAILED(res)) {
208196
return -1;
209197
}
210198
return 0;
211199
#elif defined(MYFRAME_OS_MACOSX)
212-
// unsupport
213-
return -1;
214-
#else
215-
auto handle = t->native_handle();
216-
return pthread_setname_np(handle, name.c_str());
217-
#endif
218-
}
219-
220-
int Common::SetSelfThreadName(const std::string& name) {
221-
#if defined(MYFRAME_OS_WINDOWS)
222-
int len = MultiByteToWideChar(CP_UTF8, 0, name.c_str(), -1, nullptr, 0);
223-
if (len <= 0) {
200+
// macos仅支持设置自身线程名字
201+
if (t != nullptr) {
224202
return -1;
225203
}
226-
wchar_t* wide_name = new wchar_t[len];
227-
MultiByteToWideChar(CP_UTF8, 0, name.c_str(), -1, wide_name, len);
228-
auto handle = GetCurrentThread();
229-
auto res = SetThreadDescription(handle, wide_name);
230-
delete[] wide_name;
231-
if (FAILED(res)) {
232-
return -1;
233-
}
234-
return 0;
235-
#elif defined(MYFRAME_OS_MACOSX)
236204
return pthread_setname_np(name.c_str());
237205
#else
238-
return pthread_setname_np(pthread_self(), name.c_str());
206+
pthread_t handle;
207+
if (t == nullptr) {
208+
handle = pthread_self();
209+
} else {
210+
handle = t->native_handle();
211+
}
212+
return pthread_setname_np(handle, name.c_str());
239213
#endif
240214
}
241215

myframe/common.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,16 @@ namespace myframe {
2727
class MYFRAME_EXPORT Common final {
2828
public:
2929
static std::vector<stdfs::path> GetDirFiles(const std::string& conf_path);
30-
static Json::Value LoadJsonFromFile(const std::string& json_file);
31-
3230
static stdfs::path GetWorkRoot();
3331
static stdfs::path GetAbsolutePath(const std::string& flag_path);
3432
static bool IsAbsolutePath(const std::string& path);
3533

34+
static Json::Value LoadJsonFromFile(const std::string& json_file);
35+
3636
static std::vector<std::string_view> SplitMsgName(const std::string& name);
3737

3838
static int SetThreadAffinity(std::thread* t, int cpu_core);
39-
static int SetSelfThreadAffinity(int cpu_core);
4039
static int SetThreadName(std::thread* t, const std::string& name);
41-
static int SetSelfThreadName(const std::string& name);
4240
enum class ProcessPriority : int {
4341
kLowest,
4442
kNormal,

myframe/worker_context.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ void WorkerContext::Initialize() {
7777
mailbox_.SetAddr(worker_->GetWorkerName());
7878
std::string th_name = mailbox_.Addr();
7979
th_name = th_name.size() >= 16 ? th_name.substr(0, 15) : th_name;
80-
if (Common::SetSelfThreadName(th_name)) {
80+
if (Common::SetThreadName(nullptr, th_name)) {
8181
LOG(WARNING) << "set " << mailbox_.Addr()
8282
<< " thread name " << th_name << " failed";
8383
} else {

0 commit comments

Comments
 (0)