| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#if !defined(__MACH__) |
| 7 |
# define _GNU_SOURCE |
| 8 |
#endif |
| 9 |
|
| 10 |
#include <aws/common/clock.h> |
| 11 |
#include <aws/common/linked_list.h> |
| 12 |
#include <aws/common/logging.h> |
| 13 |
#include <aws/common/private/dlloads.h> |
| 14 |
#include <aws/common/private/thread_shared.h> |
| 15 |
#include <aws/common/string.h> |
| 16 |
#include <aws/common/thread.h> |
| 17 |
|
| 18 |
#include <dlfcn.h> |
| 19 |
#include <errno.h> |
| 20 |
#include <inttypes.h> |
| 21 |
#include <limits.h> |
| 22 |
#include <sched.h> |
| 23 |
#include <time.h> |
| 24 |
#include <unistd.h> |
| 25 |
|
| 26 |
#if defined(__FreeBSD__) || defined(__NetBSD__) |
| 27 |
# include <pthread_np.h> |
| 28 |
typedef cpuset_t cpu_set_t; |
| 29 |
#elif defined(__OpenBSD__) |
| 30 |
# include <pthread_np.h> |
| 31 |
#endif |
| 32 |
|
| 33 |
#if !defined(AWS_AFFINITY_METHOD) |
| 34 |
# error "Must provide a method for setting thread affinity" |
| 35 |
#endif |
| 36 |
|
| 37 |
|
| 38 |
#define AWS_AFFINITY_METHOD_NONE 0 |
| 39 |
#define AWS_AFFINITY_METHOD_PTHREAD_ATTR 1 |
| 40 |
#define AWS_AFFINITY_METHOD_PTHREAD 2 |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
#if AWS_AFFINITY_METHOD != AWS_AFFINITY_METHOD_NONE \ |
| 45 |
&& AWS_AFFINITY_METHOD != AWS_AFFINITY_METHOD_PTHREAD_ATTR \ |
| 46 |
&& AWS_AFFINITY_METHOD != AWS_AFFINITY_METHOD_PTHREAD |
| 47 |
|
| 48 |
# error "Invalid thread affinity method" |
| 49 |
#endif |
| 50 |
|
| 51 |
static struct aws_thread_options s_default_options = { |
| 52 |
|
| 53 |
.stack_size = 0, |
| 54 |
.cpu_id = -1, |
| 55 |
.join_strategy = AWS_TJS_MANUAL, |
| 56 |
}; |
| 57 |
|
| 58 |
struct thread_atexit_callback { |
| 59 |
aws_thread_atexit_fn *callback; |
| 60 |
void *user_data; |
| 61 |
struct thread_atexit_callback *next; |
| 62 |
}; |
| 63 |
|
| 64 |
struct thread_wrapper { |
| 65 |
struct aws_allocator *allocator; |
| 66 |
struct aws_linked_list_node node; |
| 67 |
void (*func)(void *arg); |
| 68 |
void *arg; |
| 69 |
struct thread_atexit_callback *atexit; |
| 70 |
void (*call_once)(void *); |
| 71 |
void *once_arg; |
| 72 |
struct aws_string *name; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
struct aws_thread thread_copy; |
| 81 |
bool membind; |
| 82 |
}; |
| 83 |
|
| 84 |
static AWS_THREAD_LOCAL struct thread_wrapper *tl_wrapper = NULL; |
| 85 |
|
| 86 |
static void s_thread_wrapper_destroy(struct thread_wrapper *wrapper) { |
| 87 |
if (!wrapper) { |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
aws_string_destroy(wrapper->name); |
| 92 |
aws_mem_release(wrapper->allocator, wrapper); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
void aws_thread_join_and_free_wrapper_list(struct aws_linked_list *wrapper_list) { |
| 99 |
struct aws_linked_list_node *iter = aws_linked_list_begin(wrapper_list); |
| 100 |
while (iter != aws_linked_list_end(wrapper_list)) { |
| 101 |
|
| 102 |
struct thread_wrapper *join_thread_wrapper = AWS_CONTAINER_OF(iter, struct thread_wrapper, node); |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
iter = aws_linked_list_next(iter); |
| 108 |
|
| 109 |
join_thread_wrapper->thread_copy.detach_state = AWS_THREAD_JOINABLE; |
| 110 |
aws_thread_join(&join_thread_wrapper->thread_copy); |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
aws_thread_clean_up(&join_thread_wrapper->thread_copy); |
| 118 |
|
| 119 |
s_thread_wrapper_destroy(join_thread_wrapper); |
| 120 |
|
| 121 |
aws_thread_decrement_unjoined_count(); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
static void s_set_thread_name(pthread_t thread_id, const char *name) { |
| 128 |
#if defined(__APPLE__) |
| 129 |
(void)thread_id; |
| 130 |
pthread_setname_np(name); |
| 131 |
#elif defined(AWS_PTHREAD_SETNAME_TAKES_2ARGS) |
| 132 |
pthread_setname_np(thread_id, name); |
| 133 |
#elif defined(AWS_PTHREAD_SET_NAME_TAKES_2ARGS) |
| 134 |
pthread_set_name_np(thread_id, name); |
| 135 |
#elif defined(AWS_PTHREAD_SETNAME_TAKES_3ARGS) |
| 136 |
pthread_setname_np(thread_id, name, NULL); |
| 137 |
#else |
| 138 |
(void)thread_id; |
| 139 |
(void)name; |
| 140 |
#endif |
| 141 |
} |
| 142 |
|
| 143 |
static void *thread_fn(void *arg) { |
| 144 |
struct thread_wrapper *wrapper_ptr = arg; |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
wrapper_ptr->thread_copy.thread_id = aws_thread_current_thread_id(); |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
if (wrapper_ptr->name) { |
| 154 |
s_set_thread_name(wrapper_ptr->thread_copy.thread_id, aws_string_c_str(wrapper_ptr->name)); |
| 155 |
aws_string_destroy(wrapper_ptr->name); |
| 156 |
wrapper_ptr->name = NULL; |
| 157 |
} |
| 158 |
|
| 159 |
struct thread_wrapper wrapper = *wrapper_ptr; |
| 160 |
struct aws_allocator *allocator = wrapper.allocator; |
| 161 |
tl_wrapper = &wrapper; |
| 162 |
|
| 163 |
if (wrapper.membind && g_set_mempolicy_ptr) { |
| 164 |
AWS_LOGF_INFO( |
| 165 |
AWS_LS_COMMON_THREAD, |
| 166 |
"a cpu affinity was specified when launching this thread and set_mempolicy() is available on this " |
| 167 |
"system. Setting the memory policy to MPOL_PREFERRED"); |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
long resp = g_set_mempolicy_ptr(AWS_MPOL_PREFERRED_ALIAS, NULL, 0); |
| 172 |
int errno_value = errno; |
| 173 |
if (resp) { |
| 174 |
AWS_LOGF_WARN(AWS_LS_COMMON_THREAD, "call to set_mempolicy() failed with errno %d", errno_value); |
| 175 |
} |
| 176 |
} |
| 177 |
wrapper.func(wrapper.arg); |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
bool is_managed_thread = wrapper.thread_copy.detach_state == AWS_THREAD_MANAGED; |
| 184 |
if (!is_managed_thread) { |
| 185 |
s_thread_wrapper_destroy(wrapper_ptr); |
| 186 |
wrapper_ptr = NULL; |
| 187 |
} |
| 188 |
|
| 189 |
struct thread_atexit_callback *exit_callback_data = wrapper.atexit; |
| 190 |
while (exit_callback_data) { |
| 191 |
aws_thread_atexit_fn *exit_callback = exit_callback_data->callback; |
| 192 |
void *exit_callback_user_data = exit_callback_data->user_data; |
| 193 |
struct thread_atexit_callback *next_exit_callback_data = exit_callback_data->next; |
| 194 |
|
| 195 |
aws_mem_release(allocator, exit_callback_data); |
| 196 |
|
| 197 |
exit_callback(exit_callback_user_data); |
| 198 |
exit_callback_data = next_exit_callback_data; |
| 199 |
} |
| 200 |
tl_wrapper = NULL; |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
if (is_managed_thread) { |
| 206 |
aws_thread_pending_join_add(&wrapper_ptr->node); |
| 207 |
} |
| 208 |
|
| 209 |
return NULL; |
| 210 |
} |
| 211 |
|
| 212 |
const struct aws_thread_options *aws_default_thread_options(void) { |
| 213 |
return &s_default_options; |
| 214 |
} |
| 215 |
|
| 216 |
void aws_thread_clean_up(struct aws_thread *thread) { |
| 217 |
if (thread->detach_state == AWS_THREAD_JOINABLE) { |
| 218 |
pthread_detach(thread->thread_id); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
static void s_call_once(void) { |
| 223 |
tl_wrapper->call_once(tl_wrapper->once_arg); |
| 224 |
} |
| 225 |
|
| 226 |
void aws_thread_call_once(aws_thread_once *flag, void (*call_once)(void *), void *user_data) { |
| 227 |
|
| 228 |
struct thread_wrapper temp_wrapper; |
| 229 |
if (!tl_wrapper) { |
| 230 |
tl_wrapper = &temp_wrapper; |
| 231 |
} |
| 232 |
|
| 233 |
tl_wrapper->call_once = call_once; |
| 234 |
tl_wrapper->once_arg = user_data; |
| 235 |
pthread_once(flag, s_call_once); |
| 236 |
|
| 237 |
if (tl_wrapper == &temp_wrapper) { |
| 238 |
tl_wrapper = NULL; |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
int aws_thread_init(struct aws_thread *thread, struct aws_allocator *allocator) { |
| 243 |
*thread = (struct aws_thread){.allocator = allocator, .detach_state = AWS_THREAD_NOT_CREATED}; |
| 244 |
|
| 245 |
return AWS_OP_SUCCESS; |
| 246 |
} |
| 247 |
|
| 248 |
int aws_thread_launch( |
| 249 |
struct aws_thread *thread, |
| 250 |
void (*func)(void *arg), |
| 251 |
void *arg, |
| 252 |
const struct aws_thread_options *options) { |
| 253 |
|
| 254 |
pthread_attr_t attributes; |
| 255 |
pthread_attr_t *attributes_ptr = NULL; |
| 256 |
int attr_return = 0; |
| 257 |
struct thread_wrapper *wrapper = NULL; |
| 258 |
bool is_managed_thread = options != NULL && options->join_strategy == AWS_TJS_MANAGED; |
| 259 |
if (is_managed_thread) { |
| 260 |
thread->detach_state = AWS_THREAD_MANAGED; |
| 261 |
} |
| 262 |
|
| 263 |
if (options) { |
| 264 |
attr_return = pthread_attr_init(&attributes); |
| 265 |
|
| 266 |
if (attr_return) { |
| 267 |
goto cleanup; |
| 268 |
} |
| 269 |
|
| 270 |
attributes_ptr = &attributes; |
| 271 |
|
| 272 |
if (options->stack_size > PTHREAD_STACK_MIN) { |
| 273 |
attr_return = pthread_attr_setstacksize(attributes_ptr, options->stack_size); |
| 274 |
|
| 275 |
if (attr_return) { |
| 276 |
goto cleanup; |
| 277 |
} |
| 278 |
} else if (!options->stack_size) { |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
size_t min_stack_size = (size_t)1 * 1024 * 1024; |
| 285 |
size_t current_stack_size; |
| 286 |
attr_return = pthread_attr_getstacksize(attributes_ptr, ¤t_stack_size); |
| 287 |
if (attr_return) { |
| 288 |
goto cleanup; |
| 289 |
} |
| 290 |
|
| 291 |
if (current_stack_size < min_stack_size) { |
| 292 |
attr_return = pthread_attr_setstacksize(attributes_ptr, min_stack_size); |
| 293 |
if (attr_return) { |
| 294 |
goto cleanup; |
| 295 |
} |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
#if AWS_AFFINITY_METHOD == AWS_AFFINITY_METHOD_PTHREAD_ATTR |
| 304 |
if (options->cpu_id >= 0) { |
| 305 |
AWS_LOGF_INFO( |
| 306 |
AWS_LS_COMMON_THREAD, |
| 307 |
"id=%p: cpu affinity of cpu_id %d was specified, attempting to honor the value.", |
| 308 |
(void *)thread, |
| 309 |
options->cpu_id); |
| 310 |
|
| 311 |
cpu_set_t cpuset; |
| 312 |
CPU_ZERO(&cpuset); |
| 313 |
CPU_SET((uint32_t)options->cpu_id, &cpuset); |
| 314 |
|
| 315 |
attr_return = pthread_attr_setaffinity_np(attributes_ptr, sizeof(cpuset), &cpuset); |
| 316 |
|
| 317 |
if (attr_return) { |
| 318 |
AWS_LOGF_WARN( |
| 319 |
AWS_LS_COMMON_THREAD, |
| 320 |
"id=%p: pthread_attr_setaffinity_np() failed with %d. Continuing without cpu affinity", |
| 321 |
(void *)thread, |
| 322 |
attr_return); |
| 323 |
goto cleanup; |
| 324 |
} |
| 325 |
} |
| 326 |
#endif |
| 327 |
} |
| 328 |
|
| 329 |
wrapper = aws_mem_calloc(thread->allocator, 1, sizeof(struct thread_wrapper)); |
| 330 |
|
| 331 |
if (options) { |
| 332 |
if (options->cpu_id >= 0) { |
| 333 |
wrapper->membind = true; |
| 334 |
} |
| 335 |
if (options->name.len > 0) { |
| 336 |
wrapper->name = aws_string_new_from_cursor(thread->allocator, &options->name); |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
wrapper->thread_copy = *thread; |
| 341 |
wrapper->allocator = thread->allocator; |
| 342 |
wrapper->func = func; |
| 343 |
wrapper->arg = arg; |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
if (is_managed_thread) { |
| 349 |
aws_thread_increment_unjoined_count(); |
| 350 |
} |
| 351 |
|
| 352 |
attr_return = pthread_create(&thread->thread_id, attributes_ptr, thread_fn, (void *)wrapper); |
| 353 |
|
| 354 |
if (attr_return) { |
| 355 |
AWS_LOGF_ERROR(AWS_LS_COMMON_THREAD, "id=%p: pthread_create() failed with %d", (void *)thread, attr_return); |
| 356 |
if (is_managed_thread) { |
| 357 |
aws_thread_decrement_unjoined_count(); |
| 358 |
} |
| 359 |
goto cleanup; |
| 360 |
} |
| 361 |
|
| 362 |
#if AWS_AFFINITY_METHOD == AWS_AFFINITY_METHOD_PTHREAD |
| 363 |
|
| 364 |
|
| 365 |
if (options && options->cpu_id >= 0) { |
| 366 |
AWS_LOGF_INFO( |
| 367 |
AWS_LS_COMMON_THREAD, |
| 368 |
"id=%p: cpu affinity of cpu_id %d was specified, attempting to honor the value.", |
| 369 |
(void *)thread, |
| 370 |
options->cpu_id); |
| 371 |
|
| 372 |
cpu_set_t cpuset; |
| 373 |
CPU_ZERO(&cpuset); |
| 374 |
CPU_SET((uint32_t)options->cpu_id, &cpuset); |
| 375 |
|
| 376 |
|
| 377 |
int setaffinity_return = pthread_setaffinity_np(thread->thread_id, sizeof(cpuset), &cpuset); |
| 378 |
if (setaffinity_return) { |
| 379 |
AWS_LOGF_WARN( |
| 380 |
AWS_LS_COMMON_THREAD, |
| 381 |
"id=%p: pthread_setaffinity_np() failed with %d. Running thread without CPU affinity.", |
| 382 |
(void *)thread, |
| 383 |
setaffinity_return); |
| 384 |
} |
| 385 |
} |
| 386 |
#endif |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
if (is_managed_thread) { |
| 392 |
aws_thread_clean_up(thread); |
| 393 |
} else { |
| 394 |
thread->detach_state = AWS_THREAD_JOINABLE; |
| 395 |
} |
| 396 |
|
| 397 |
cleanup: |
| 398 |
if (attributes_ptr) { |
| 399 |
pthread_attr_destroy(attributes_ptr); |
| 400 |
} |
| 401 |
|
| 402 |
if (attr_return) { |
| 403 |
s_thread_wrapper_destroy(wrapper); |
| 404 |
if (options && options->cpu_id >= 0) { |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
AWS_LOGF_INFO( |
| 411 |
AWS_LS_COMMON_THREAD, |
| 412 |
"id=%p: Attempting to launch the thread again without pinning to a cpu_id", |
| 413 |
(void *)thread); |
| 414 |
struct aws_thread_options new_options = *options; |
| 415 |
new_options.cpu_id = -1; |
| 416 |
return aws_thread_launch(thread, func, arg, &new_options); |
| 417 |
} |
| 418 |
switch (attr_return) { |
| 419 |
case EINVAL: |
| 420 |
return aws_raise_error(AWS_ERROR_THREAD_INVALID_SETTINGS); |
| 421 |
case EAGAIN: |
| 422 |
return aws_raise_error(AWS_ERROR_THREAD_INSUFFICIENT_RESOURCE); |
| 423 |
case EPERM: |
| 424 |
return aws_raise_error(AWS_ERROR_THREAD_NO_PERMISSIONS); |
| 425 |
case ENOMEM: |
| 426 |
return aws_raise_error(AWS_ERROR_OOM); |
| 427 |
default: |
| 428 |
return aws_raise_error(AWS_ERROR_UNKNOWN); |
| 429 |
} |
| 430 |
} |
| 431 |
return AWS_OP_SUCCESS; |
| 432 |
} |
| 433 |
|
| 434 |
aws_thread_id_t aws_thread_get_id(struct aws_thread *thread) { |
| 435 |
return thread->thread_id; |
| 436 |
} |
| 437 |
|
| 438 |
enum aws_thread_detach_state aws_thread_get_detach_state(struct aws_thread *thread) { |
| 439 |
return thread->detach_state; |
| 440 |
} |
| 441 |
|
| 442 |
int aws_thread_join(struct aws_thread *thread) { |
| 443 |
if (thread->detach_state == AWS_THREAD_JOINABLE) { |
| 444 |
int err_no = pthread_join(thread->thread_id, 0); |
| 445 |
|
| 446 |
if (err_no) { |
| 447 |
if (err_no == EINVAL) { |
| 448 |
return aws_raise_error(AWS_ERROR_THREAD_NOT_JOINABLE); |
| 449 |
} |
| 450 |
if (err_no == ESRCH) { |
| 451 |
return aws_raise_error(AWS_ERROR_THREAD_NO_SUCH_THREAD_ID); |
| 452 |
} |
| 453 |
if (err_no == EDEADLK) { |
| 454 |
return aws_raise_error(AWS_ERROR_THREAD_DEADLOCK_DETECTED); |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
thread->detach_state = AWS_THREAD_JOIN_COMPLETED; |
| 459 |
} |
| 460 |
|
| 461 |
return AWS_OP_SUCCESS; |
| 462 |
} |
| 463 |
|
| 464 |
aws_thread_id_t aws_thread_current_thread_id(void) { |
| 465 |
return pthread_self(); |
| 466 |
} |
| 467 |
|
| 468 |
bool aws_thread_thread_id_equal(aws_thread_id_t t1, aws_thread_id_t t2) { |
| 469 |
return pthread_equal(t1, t2) != 0; |
| 470 |
} |
| 471 |
|
| 472 |
void aws_thread_current_sleep(uint64_t nanos) { |
| 473 |
uint64_t nano = 0; |
| 474 |
time_t seconds = (time_t)aws_timestamp_convert(nanos, AWS_TIMESTAMP_NANOS, AWS_TIMESTAMP_SECS, &nano); |
| 475 |
|
| 476 |
struct timespec tm = { |
| 477 |
.tv_sec = seconds, |
| 478 |
.tv_nsec = (long)nano, |
| 479 |
}; |
| 480 |
struct timespec output; |
| 481 |
|
| 482 |
nanosleep(&tm, &output); |
| 483 |
} |
| 484 |
|
| 485 |
int aws_thread_current_at_exit(aws_thread_atexit_fn *callback, void *user_data) { |
| 486 |
if (!tl_wrapper) { |
| 487 |
return aws_raise_error(AWS_ERROR_THREAD_NOT_JOINABLE); |
| 488 |
} |
| 489 |
|
| 490 |
struct thread_atexit_callback *cb = aws_mem_calloc(tl_wrapper->allocator, 1, sizeof(struct thread_atexit_callback)); |
| 491 |
if (!cb) { |
| 492 |
return AWS_OP_ERR; |
| 493 |
} |
| 494 |
cb->callback = callback; |
| 495 |
cb->user_data = user_data; |
| 496 |
cb->next = tl_wrapper->atexit; |
| 497 |
tl_wrapper->atexit = cb; |
| 498 |
return AWS_OP_SUCCESS; |
| 499 |
} |
| 500 |
|
| 501 |
int aws_thread_current_name(struct aws_allocator *allocator, struct aws_string **out_name) { |
| 502 |
return aws_thread_name(allocator, aws_thread_current_thread_id(), out_name); |
| 503 |
} |
| 504 |
|
| 505 |
#define THREAD_NAME_BUFFER_SIZE 256 |
| 506 |
int aws_thread_name(struct aws_allocator *allocator, aws_thread_id_t thread_id, struct aws_string **out_name) { |
| 507 |
*out_name = NULL; |
| 508 |
#if defined(AWS_PTHREAD_GETNAME_TAKES_2ARGS) || defined(AWS_PTHREAD_GETNAME_TAKES_3ARGS) || \ |
| 509 |
defined(AWS_PTHREAD_GET_NAME_TAKES_2_ARGS) |
| 510 |
char name[THREAD_NAME_BUFFER_SIZE] = {0}; |
| 511 |
# ifdef AWS_PTHREAD_GETNAME_TAKES_3ARGS |
| 512 |
if (pthread_getname_np(thread_id, name, THREAD_NAME_BUFFER_SIZE)) { |
| 513 |
# elif AWS_PTHREAD_GETNAME_TAKES_2ARGS |
| 514 |
if (pthread_getname_np(thread_id, name)) { |
| 515 |
# elif AWS_PTHREAD_GET_NAME_TAKES_2ARGS |
| 516 |
if (pthread_get_name_np(thread_id, name)) { |
| 517 |
# endif |
| 518 |
|
| 519 |
return aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE); |
| 520 |
} |
| 521 |
|
| 522 |
*out_name = aws_string_new_from_c_str(allocator, name); |
| 523 |
return AWS_OP_SUCCESS; |
| 524 |
#else |
| 525 |
|
| 526 |
return aws_raise_error(AWS_ERROR_PLATFORM_NOT_SUPPORTED); |
| 527 |
#endif |
| 528 |
} |