| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include <aws/common/thread.h> |
| 6 |
|
| 7 |
#include <aws/common/clock.h> |
| 8 |
#include <aws/common/linked_list.h> |
| 9 |
#include <aws/common/logging.h> |
| 10 |
#include <aws/common/private/thread_shared.h> |
| 11 |
#include <aws/common/string.h> |
| 12 |
|
| 13 |
#include <windows.h> |
| 14 |
|
| 15 |
#include <inttypes.h> |
| 16 |
|
| 17 |
|
| 18 |
#define WIDEN2(s) L## #s |
| 19 |
#define WIDEN(s) WIDEN2(s) |
| 20 |
|
| 21 |
static struct aws_thread_options s_default_options = { |
| 22 |
|
| 23 |
.stack_size = 0, |
| 24 |
.cpu_id = -1, |
| 25 |
.join_strategy = AWS_TJS_MANUAL, |
| 26 |
}; |
| 27 |
|
| 28 |
struct thread_atexit_callback { |
| 29 |
aws_thread_atexit_fn *callback; |
| 30 |
void *user_data; |
| 31 |
struct thread_atexit_callback *next; |
| 32 |
}; |
| 33 |
|
| 34 |
struct thread_wrapper { |
| 35 |
struct aws_allocator *allocator; |
| 36 |
struct aws_linked_list_node node; |
| 37 |
void (*func)(void *arg); |
| 38 |
void *arg; |
| 39 |
struct thread_atexit_callback *atexit; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
struct aws_thread thread_copy; |
| 48 |
}; |
| 49 |
|
| 50 |
static AWS_THREAD_LOCAL struct thread_wrapper *tl_wrapper = NULL; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
void aws_thread_join_and_free_wrapper_list(struct aws_linked_list *wrapper_list) { |
| 56 |
struct aws_linked_list_node *iter = aws_linked_list_begin(wrapper_list); |
| 57 |
while (iter != aws_linked_list_end(wrapper_list)) { |
| 58 |
|
| 59 |
struct thread_wrapper *join_thread_wrapper = AWS_CONTAINER_OF(iter, struct thread_wrapper, node); |
| 60 |
iter = aws_linked_list_next(iter); |
| 61 |
join_thread_wrapper->thread_copy.detach_state = AWS_THREAD_JOINABLE; |
| 62 |
aws_thread_join(&join_thread_wrapper->thread_copy); |
| 63 |
aws_thread_clean_up(&join_thread_wrapper->thread_copy); |
| 64 |
aws_mem_release(join_thread_wrapper->allocator, join_thread_wrapper); |
| 65 |
|
| 66 |
aws_thread_decrement_unjoined_count(); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
static DWORD WINAPI thread_wrapper_fn(LPVOID arg) { |
| 71 |
struct thread_wrapper *wrapper_ptr = arg; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
DWORD current_thread_id = GetCurrentThreadId(); |
| 79 |
wrapper_ptr->thread_copy.thread_handle = OpenThread(THREAD_ALL_ACCESS, FALSE, current_thread_id); |
| 80 |
|
| 81 |
struct thread_wrapper thread_wrapper = *wrapper_ptr; |
| 82 |
struct aws_allocator *allocator = thread_wrapper.allocator; |
| 83 |
tl_wrapper = &thread_wrapper; |
| 84 |
thread_wrapper.func(thread_wrapper.arg); |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
bool is_managed_thread = wrapper_ptr->thread_copy.detach_state == AWS_THREAD_MANAGED; |
| 91 |
if (!is_managed_thread) { |
| 92 |
aws_mem_release(allocator, arg); |
| 93 |
} |
| 94 |
|
| 95 |
struct thread_atexit_callback *exit_callback_data = thread_wrapper.atexit; |
| 96 |
while (exit_callback_data) { |
| 97 |
aws_thread_atexit_fn *exit_callback = exit_callback_data->callback; |
| 98 |
void *exit_callback_user_data = exit_callback_data->user_data; |
| 99 |
struct thread_atexit_callback *next_exit_callback_data = exit_callback_data->next; |
| 100 |
|
| 101 |
aws_mem_release(allocator, exit_callback_data); |
| 102 |
|
| 103 |
exit_callback(exit_callback_user_data); |
| 104 |
exit_callback_data = next_exit_callback_data; |
| 105 |
} |
| 106 |
tl_wrapper = NULL; |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
if (is_managed_thread) { |
| 112 |
aws_thread_pending_join_add(&wrapper_ptr->node); |
| 113 |
} |
| 114 |
|
| 115 |
return 0; |
| 116 |
} |
| 117 |
|
| 118 |
const struct aws_thread_options *aws_default_thread_options(void) { |
| 119 |
return &s_default_options; |
| 120 |
} |
| 121 |
|
| 122 |
struct callback_fn_wrapper { |
| 123 |
void (*call_once)(void *); |
| 124 |
void *user_data; |
| 125 |
}; |
| 126 |
|
| 127 |
BOOL WINAPI s_init_once_wrapper(PINIT_ONCE init_once, void *param, void **context) { |
| 128 |
(void)context; |
| 129 |
(void)init_once; |
| 130 |
|
| 131 |
struct callback_fn_wrapper *callback_fn_wrapper = param; |
| 132 |
callback_fn_wrapper->call_once(callback_fn_wrapper->user_data); |
| 133 |
return TRUE; |
| 134 |
} |
| 135 |
|
| 136 |
void aws_thread_call_once(aws_thread_once *flag, void (*call_once)(void *), void *user_data) { |
| 137 |
struct callback_fn_wrapper wrapper; |
| 138 |
wrapper.call_once = call_once; |
| 139 |
wrapper.user_data = user_data; |
| 140 |
InitOnceExecuteOnce((PINIT_ONCE)flag, s_init_once_wrapper, &wrapper, NULL); |
| 141 |
} |
| 142 |
|
| 143 |
int aws_thread_init(struct aws_thread *thread, struct aws_allocator *allocator) { |
| 144 |
thread->thread_handle = 0; |
| 145 |
thread->thread_id = 0; |
| 146 |
thread->allocator = allocator; |
| 147 |
thread->detach_state = AWS_THREAD_NOT_CREATED; |
| 148 |
|
| 149 |
return AWS_OP_SUCCESS; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
static aws_thread_once s_check_functions_once = INIT_ONCE_STATIC_INIT; |
| 154 |
|
| 155 |
#if defined(AWS_OS_WINDOWS_DESKTOP) |
| 156 |
static aws_thread_once s_check_active_processor_functions_once = INIT_ONCE_STATIC_INIT; |
| 157 |
typedef DWORD WINAPI GetActiveProcessorCount_fn(WORD); |
| 158 |
static GetActiveProcessorCount_fn *s_GetActiveProcessorCount; |
| 159 |
|
| 160 |
typedef WORD WINAPI GetActiveProcessorGroupCount_fn(void); |
| 161 |
static GetActiveProcessorGroupCount_fn *s_GetActiveProcessorGroupCount; |
| 162 |
|
| 163 |
static void s_check_active_processor_functions(void *user_data) { |
| 164 |
(void)user_data; |
| 165 |
|
| 166 |
s_GetActiveProcessorGroupCount = (GetActiveProcessorGroupCount_fn *)GetProcAddress( |
| 167 |
GetModuleHandleW(WIDEN(WINDOWS_KERNEL_LIB) L".dll"), "GetActiveProcessorGroupCount"); |
| 168 |
s_GetActiveProcessorCount = (GetActiveProcessorCount_fn *)GetProcAddress( |
| 169 |
GetModuleHandleW(WIDEN(WINDOWS_KERNEL_LIB) L".dll"), "GetActiveProcessorCount"); |
| 170 |
} |
| 171 |
#endif |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
static void s_get_group_and_cpu_id(uint32_t desired_cpu, uint16_t *group, uint8_t *proc_num) { |
| 176 |
(void)desired_cpu; |
| 177 |
*group = 0; |
| 178 |
*proc_num = 0; |
| 179 |
#if defined(AWS_OS_WINDOWS_DESKTOP) |
| 180 |
|
| 181 |
aws_thread_call_once(&s_check_active_processor_functions_once, s_check_active_processor_functions, NULL); |
| 182 |
if (!s_GetActiveProcessorCount || !s_GetActiveProcessorGroupCount) { |
| 183 |
return; |
| 184 |
} |
| 185 |
|
| 186 |
unsigned group_count = s_GetActiveProcessorGroupCount(); |
| 187 |
|
| 188 |
unsigned total_processors_detected = 0; |
| 189 |
uint8_t group_with_desired_processor = 0; |
| 190 |
uint8_t group_mask_for_desired_processor = 0; |
| 191 |
|
| 192 |
|
| 193 |
for (uint8_t i = 0; i < group_count; ++group_count) { |
| 194 |
DWORD processor_count_in_group = s_GetActiveProcessorCount((WORD)i); |
| 195 |
if (total_processors_detected + processor_count_in_group > desired_cpu) { |
| 196 |
group_with_desired_processor = i; |
| 197 |
group_mask_for_desired_processor = (uint8_t)(desired_cpu - total_processors_detected); |
| 198 |
break; |
| 199 |
} |
| 200 |
total_processors_detected += processor_count_in_group; |
| 201 |
} |
| 202 |
|
| 203 |
*proc_num = group_mask_for_desired_processor; |
| 204 |
*group = group_with_desired_processor; |
| 205 |
return; |
| 206 |
#endif |
| 207 |
} |
| 208 |
|
| 209 |
typedef BOOL WINAPI SetThreadGroupAffinity_fn( |
| 210 |
HANDLE hThread, |
| 211 |
const GROUP_AFFINITY *GroupAffinity, |
| 212 |
PGROUP_AFFINITY PreviousGroupAffinity); |
| 213 |
static SetThreadGroupAffinity_fn *s_SetThreadGroupAffinity; |
| 214 |
|
| 215 |
typedef BOOL WINAPI SetThreadIdealProcessorEx_fn( |
| 216 |
HANDLE hThread, |
| 217 |
PPROCESSOR_NUMBER lpIdealProcessor, |
| 218 |
PPROCESSOR_NUMBER lpPreviousIdealProcessor); |
| 219 |
static SetThreadIdealProcessorEx_fn *s_SetThreadIdealProcessorEx; |
| 220 |
|
| 221 |
typedef HRESULT WINAPI SetThreadDescription_fn(HANDLE hThread, PCWSTR lpThreadDescription); |
| 222 |
static SetThreadDescription_fn *s_SetThreadDescription; |
| 223 |
|
| 224 |
typedef HRESULT WINAPI GetThreadDescription_fn(HANDLE hThread, PWSTR *lpThreadDescription); |
| 225 |
static GetThreadDescription_fn *s_GetThreadDescription; |
| 226 |
|
| 227 |
static void s_check_thread_functions(void *user_data) { |
| 228 |
(void)user_data; |
| 229 |
|
| 230 |
s_SetThreadGroupAffinity = (SetThreadGroupAffinity_fn *)GetProcAddress( |
| 231 |
GetModuleHandleW(WIDEN(WINDOWS_KERNEL_LIB) L".dll"), "SetThreadGroupAffinity"); |
| 232 |
s_SetThreadIdealProcessorEx = (SetThreadIdealProcessorEx_fn *)GetProcAddress( |
| 233 |
GetModuleHandleW(WIDEN(WINDOWS_KERNEL_LIB) L".dll"), "SetThreadIdealProcessorEx"); |
| 234 |
s_SetThreadDescription = (SetThreadDescription_fn *)GetProcAddress( |
| 235 |
GetModuleHandleW(WIDEN(WINDOWS_KERNEL_LIB) L".dll"), "SetThreadDescription"); |
| 236 |
s_GetThreadDescription = (GetThreadDescription_fn *)GetProcAddress( |
| 237 |
GetModuleHandleW(WIDEN(WINDOWS_KERNEL_LIB) L".dll"), "GetThreadDescription"); |
| 238 |
} |
| 239 |
|
| 240 |
int aws_thread_launch( |
| 241 |
struct aws_thread *thread, |
| 242 |
void (*func)(void *arg), |
| 243 |
void *arg, |
| 244 |
const struct aws_thread_options *options) { |
| 245 |
|
| 246 |
|
| 247 |
aws_thread_call_once(&s_check_functions_once, s_check_thread_functions, NULL); |
| 248 |
|
| 249 |
SIZE_T stack_size = 0; |
| 250 |
if (options && options->stack_size > 0) { |
| 251 |
stack_size = (SIZE_T)options->stack_size; |
| 252 |
} |
| 253 |
|
| 254 |
bool is_managed_thread = options != NULL && options->join_strategy == AWS_TJS_MANAGED; |
| 255 |
if (is_managed_thread) { |
| 256 |
thread->detach_state = AWS_THREAD_MANAGED; |
| 257 |
} |
| 258 |
|
| 259 |
struct thread_wrapper *thread_wrapper = |
| 260 |
(struct thread_wrapper *)aws_mem_calloc(thread->allocator, 1, sizeof(struct thread_wrapper)); |
| 261 |
thread_wrapper->allocator = thread->allocator; |
| 262 |
thread_wrapper->arg = arg; |
| 263 |
thread_wrapper->func = func; |
| 264 |
thread_wrapper->thread_copy = *thread; |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
if (is_managed_thread) { |
| 270 |
aws_thread_increment_unjoined_count(); |
| 271 |
} |
| 272 |
|
| 273 |
thread->thread_handle = |
| 274 |
CreateThread(0, stack_size, thread_wrapper_fn, (LPVOID)thread_wrapper, 0, &thread->thread_id); |
| 275 |
|
| 276 |
if (!thread->thread_handle) { |
| 277 |
aws_thread_decrement_unjoined_count(); |
| 278 |
return aws_raise_error(AWS_ERROR_THREAD_INSUFFICIENT_RESOURCE); |
| 279 |
} |
| 280 |
|
| 281 |
if (options && (options->name.len > 0) && s_SetThreadDescription) { |
| 282 |
|
| 283 |
struct aws_wstring *name = aws_string_convert_to_wchar_from_byte_cursor(thread->allocator, &options->name); |
| 284 |
if (name) { |
| 285 |
s_SetThreadDescription(thread->thread_handle, aws_wstring_c_str(name)); |
| 286 |
aws_wstring_destroy(name); |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
if (options && options->cpu_id >= 0) { |
| 291 |
AWS_LOGF_INFO( |
| 292 |
AWS_LS_COMMON_THREAD, |
| 293 |
"id=%p: cpu affinity of cpu_id %" PRIi32 " was specified, attempting to honor the value.", |
| 294 |
(void *)thread, |
| 295 |
options->cpu_id); |
| 296 |
|
| 297 |
uint16_t group = 0; |
| 298 |
uint8_t proc_num = 0; |
| 299 |
s_get_group_and_cpu_id(options->cpu_id, &group, &proc_num); |
| 300 |
GROUP_AFFINITY group_afinity; |
| 301 |
AWS_ZERO_STRUCT(group_afinity); |
| 302 |
group_afinity.Group = (WORD)group; |
| 303 |
group_afinity.Mask = (KAFFINITY)((uint64_t)1 << proc_num); |
| 304 |
AWS_LOGF_DEBUG( |
| 305 |
AWS_LS_COMMON_THREAD, |
| 306 |
"id=%p: computed mask %" PRIx64 " on group %" PRIu16 ".", |
| 307 |
(void *)thread, |
| 308 |
(uint64_t)group_afinity.Mask, |
| 309 |
(uint16_t)group_afinity.Group); |
| 310 |
|
| 311 |
if (!s_SetThreadGroupAffinity || !s_SetThreadIdealProcessorEx) { |
| 312 |
goto no_thread_affinity; |
| 313 |
} |
| 314 |
BOOL set_group_val = s_SetThreadGroupAffinity(thread->thread_handle, &group_afinity, NULL); |
| 315 |
AWS_LOGF_DEBUG( |
| 316 |
AWS_LS_COMMON_THREAD, |
| 317 |
"id=%p: SetThreadGroupAffinity() result %" PRIi8 ".", |
| 318 |
(void *)thread, |
| 319 |
(int8_t)set_group_val); |
| 320 |
|
| 321 |
if (set_group_val) { |
| 322 |
PROCESSOR_NUMBER processor_number; |
| 323 |
AWS_ZERO_STRUCT(processor_number); |
| 324 |
processor_number.Group = (WORD)group; |
| 325 |
processor_number.Number = proc_num; |
| 326 |
|
| 327 |
BOOL set_processor_val = s_SetThreadIdealProcessorEx(thread->thread_handle, &processor_number, NULL); |
| 328 |
AWS_LOGF_DEBUG( |
| 329 |
AWS_LS_COMMON_THREAD, |
| 330 |
"id=%p: SetThreadIdealProcessorEx() result %" PRIi8 ".", |
| 331 |
(void *)thread, |
| 332 |
(int8_t)set_processor_val); |
| 333 |
if (!set_processor_val) { |
| 334 |
AWS_LOGF_WARN( |
| 335 |
AWS_LS_COMMON_THREAD, |
| 336 |
"id=%p: SetThreadIdealProcessorEx() failed with %" PRIx32 ".", |
| 337 |
(void *)thread, |
| 338 |
(uint32_t)GetLastError()); |
| 339 |
} |
| 340 |
|
| 341 |
} else { |
| 342 |
AWS_LOGF_WARN( |
| 343 |
AWS_LS_COMMON_THREAD, |
| 344 |
"id=%p: SetThreadGroupAffinity() failed with %" PRIx32 ".", |
| 345 |
(void *)thread, |
| 346 |
(uint32_t)GetLastError()); |
| 347 |
} |
| 348 |
} |
| 349 |
no_thread_affinity: |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
if (is_managed_thread) { |
| 355 |
aws_thread_clean_up(thread); |
| 356 |
} else { |
| 357 |
thread->detach_state = AWS_THREAD_JOINABLE; |
| 358 |
} |
| 359 |
|
| 360 |
return AWS_OP_SUCCESS; |
| 361 |
} |
| 362 |
|
| 363 |
aws_thread_id_t aws_thread_get_id(struct aws_thread *thread) { |
| 364 |
return thread->thread_id; |
| 365 |
} |
| 366 |
|
| 367 |
enum aws_thread_detach_state aws_thread_get_detach_state(struct aws_thread *thread) { |
| 368 |
return thread->detach_state; |
| 369 |
} |
| 370 |
|
| 371 |
int aws_thread_join(struct aws_thread *thread) { |
| 372 |
if (thread->detach_state == AWS_THREAD_JOINABLE) { |
| 373 |
WaitForSingleObject(thread->thread_handle, INFINITE); |
| 374 |
thread->detach_state = AWS_THREAD_JOIN_COMPLETED; |
| 375 |
} |
| 376 |
|
| 377 |
return AWS_OP_SUCCESS; |
| 378 |
} |
| 379 |
|
| 380 |
void aws_thread_clean_up(struct aws_thread *thread) { |
| 381 |
CloseHandle(thread->thread_handle); |
| 382 |
thread->thread_handle = 0; |
| 383 |
} |
| 384 |
|
| 385 |
aws_thread_id_t aws_thread_current_thread_id(void) { |
| 386 |
return GetCurrentThreadId(); |
| 387 |
} |
| 388 |
|
| 389 |
bool aws_thread_thread_id_equal(aws_thread_id_t t1, aws_thread_id_t t2) { |
| 390 |
return t1 == t2; |
| 391 |
} |
| 392 |
|
| 393 |
void aws_thread_current_sleep(uint64_t nanos) { |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
Sleep((DWORD)aws_timestamp_convert(nanos, AWS_TIMESTAMP_NANOS, AWS_TIMESTAMP_MILLIS, NULL)); |
| 399 |
} |
| 400 |
|
| 401 |
int aws_thread_current_at_exit(aws_thread_atexit_fn *callback, void *user_data) { |
| 402 |
if (!tl_wrapper) { |
| 403 |
return aws_raise_error(AWS_ERROR_THREAD_NOT_JOINABLE); |
| 404 |
} |
| 405 |
|
| 406 |
struct thread_atexit_callback *cb = aws_mem_calloc(tl_wrapper->allocator, 1, sizeof(struct thread_atexit_callback)); |
| 407 |
if (!cb) { |
| 408 |
return AWS_OP_ERR; |
| 409 |
} |
| 410 |
cb->callback = callback; |
| 411 |
cb->user_data = user_data; |
| 412 |
cb->next = tl_wrapper->atexit; |
| 413 |
tl_wrapper->atexit = cb; |
| 414 |
return AWS_OP_SUCCESS; |
| 415 |
} |
| 416 |
|
| 417 |
int aws_thread_current_name(struct aws_allocator *allocator, struct aws_string **out_name) { |
| 418 |
if (s_GetThreadDescription) { |
| 419 |
|
| 420 |
PWSTR wname = NULL; |
| 421 |
if (SUCCEEDED(s_GetThreadDescription(GetCurrentThread(), &wname))) { |
| 422 |
*out_name = aws_string_convert_from_wchar_c_str(allocator, wname); |
| 423 |
LocalFree(wname); |
| 424 |
return AWS_OP_SUCCESS; |
| 425 |
} |
| 426 |
|
| 427 |
return aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE); |
| 428 |
} |
| 429 |
|
| 430 |
return aws_raise_error(AWS_ERROR_PLATFORM_NOT_SUPPORTED); |
| 431 |
} |
| 432 |
|
| 433 |
int aws_thread_name(struct aws_allocator *allocator, aws_thread_id_t thread_id, struct aws_string **out_name) { |
| 434 |
if (s_GetThreadDescription) { |
| 435 |
|
| 436 |
HANDLE thread_handle = OpenThread(THREAD_QUERY_LIMITED_INFORMATION, FALSE, thread_id); |
| 437 |
|
| 438 |
if (thread_handle == NULL) { |
| 439 |
AWS_LOGF_WARN( |
| 440 |
AWS_LS_COMMON_THREAD, |
| 441 |
"thread_id=%lu: OpenThread() failed with %" PRIx32 ".", |
| 442 |
thread_id, |
| 443 |
(uint32_t)GetLastError()); |
| 444 |
return aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE); |
| 445 |
} |
| 446 |
|
| 447 |
PWSTR wname = NULL; |
| 448 |
if (SUCCEEDED(s_GetThreadDescription(thread_handle, &wname))) { |
| 449 |
*out_name = aws_string_convert_from_wchar_c_str(allocator, wname); |
| 450 |
LocalFree(wname); |
| 451 |
CloseHandle(thread_handle); |
| 452 |
return AWS_OP_SUCCESS; |
| 453 |
} |
| 454 |
|
| 455 |
CloseHandle(thread_handle); |
| 456 |
return aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE); |
| 457 |
} |
| 458 |
|
| 459 |
return aws_raise_error(AWS_ERROR_PLATFORM_NOT_SUPPORTED); |
| 460 |
} |