@@ -131,39 +131,12 @@ std::vector<std::string_view> Common::SplitMsgName(const std::string& name) {
131131// 可以通过std::thread::hardware_concurrency()获得核心数
132132int 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
0 commit comments