| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/io/event_loop.h> |
| 7 |
|
| 8 |
#include <aws/common/shutdown_types.h> |
| 9 |
#include <aws/io/logging.h> |
| 10 |
#include <aws/io/private/event_loop_impl.h> |
| 11 |
|
| 12 |
#include <aws/common/clock.h> |
| 13 |
#include <aws/common/device_random.h> |
| 14 |
#include <aws/common/platform.h> |
| 15 |
#include <aws/common/system_info.h> |
| 16 |
#include <aws/common/thread.h> |
| 17 |
|
| 18 |
#if defined(AWS_USE_APPLE_NETWORK_FRAMEWORK) |
| 19 |
static enum aws_event_loop_type s_default_event_loop_type_override = AWS_EVENT_LOOP_DISPATCH_QUEUE; |
| 20 |
#elif defined(AWS_USE_APPLE_DISPATCH_QUEUE) |
| 21 |
static enum aws_event_loop_type s_default_event_loop_type_override = AWS_EVENT_LOOP_DISPATCH_QUEUE; |
| 22 |
#else |
| 23 |
static enum aws_event_loop_type s_default_event_loop_type_override = AWS_EVENT_LOOP_PLATFORM_DEFAULT; |
| 24 |
#endif |
| 25 |
|
| 26 |
struct aws_event_loop *aws_event_loop_new_default(struct aws_allocator *alloc, aws_io_clock_fn *clock) { |
| 27 |
struct aws_event_loop_options options = { |
| 28 |
.thread_options = NULL, |
| 29 |
.clock = clock, |
| 30 |
.type = AWS_EVENT_LOOP_PLATFORM_DEFAULT, |
| 31 |
}; |
| 32 |
|
| 33 |
return aws_event_loop_new(alloc, &options); |
| 34 |
} |
| 35 |
|
| 36 |
#ifndef AWS_ENABLE_DISPATCH_QUEUE |
| 37 |
struct aws_event_loop *aws_event_loop_new_with_dispatch_queue( |
| 38 |
struct aws_allocator *alloc, |
| 39 |
const struct aws_event_loop_options *options) { |
| 40 |
(void)alloc; |
| 41 |
(void)options; |
| 42 |
|
| 43 |
AWS_LOGF_DEBUG(AWS_LS_IO_EVENT_LOOP, "Dispatch Queue is not supported on the platform"); |
| 44 |
aws_raise_error(AWS_ERROR_PLATFORM_NOT_SUPPORTED); |
| 45 |
return NULL; |
| 46 |
} |
| 47 |
#endif |
| 48 |
|
| 49 |
#ifndef AWS_ENABLE_IO_COMPLETION_PORTS |
| 50 |
struct aws_event_loop *aws_event_loop_new_with_iocp( |
| 51 |
struct aws_allocator *alloc, |
| 52 |
const struct aws_event_loop_options *options) { |
| 53 |
(void)alloc; |
| 54 |
(void)options; |
| 55 |
|
| 56 |
AWS_LOGF_DEBUG(AWS_LS_IO_EVENT_LOOP, "IOCP is not supported on the platform"); |
| 57 |
aws_raise_error(AWS_ERROR_PLATFORM_NOT_SUPPORTED); |
| 58 |
return NULL; |
| 59 |
} |
| 60 |
#endif |
| 61 |
|
| 62 |
#ifndef AWS_ENABLE_KQUEUE |
| 63 |
struct aws_event_loop *aws_event_loop_new_with_kqueue( |
| 64 |
struct aws_allocator *alloc, |
| 65 |
const struct aws_event_loop_options *options) { |
| 66 |
(void)alloc; |
| 67 |
(void)options; |
| 68 |
|
| 69 |
AWS_LOGF_DEBUG(AWS_LS_IO_EVENT_LOOP, "Kqueue is not supported on the platform"); |
| 70 |
aws_raise_error(AWS_ERROR_PLATFORM_NOT_SUPPORTED); |
| 71 |
return NULL; |
| 72 |
} |
| 73 |
#endif |
| 74 |
|
| 75 |
#ifndef AWS_ENABLE_EPOLL |
| 76 |
struct aws_event_loop *aws_event_loop_new_with_epoll( |
| 77 |
struct aws_allocator *alloc, |
| 78 |
const struct aws_event_loop_options *options) { |
| 79 |
(void)alloc; |
| 80 |
(void)options; |
| 81 |
|
| 82 |
AWS_LOGF_DEBUG(AWS_LS_IO_EVENT_LOOP, "Epoll is not supported on the platform"); |
| 83 |
return NULL; |
| 84 |
} |
| 85 |
#endif |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
enum aws_event_loop_type aws_event_loop_get_default_type(void) { |
| 93 |
if (s_default_event_loop_type_override != AWS_EVENT_LOOP_PLATFORM_DEFAULT) { |
| 94 |
return s_default_event_loop_type_override; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
#ifdef AWS_ENABLE_KQUEUE |
| 101 |
return AWS_EVENT_LOOP_KQUEUE; |
| 102 |
#elif defined(AWS_ENABLE_DISPATCH_QUEUE) |
| 103 |
return AWS_EVENT_LOOP_DISPATCH_QUEUE; |
| 104 |
#elif defined(AWS_ENABLE_EPOLL) |
| 105 |
return AWS_EVENT_LOOP_EPOLL; |
| 106 |
#elif defined(AWS_OS_WINDOWS) |
| 107 |
return AWS_EVENT_LOOP_IOCP; |
| 108 |
#else |
| 109 |
# error \ |
| 110 |
"Default event loop type required. Failed to get default event loop type. The library is not built correctly on the platform. " |
| 111 |
#endif |
| 112 |
} |
| 113 |
|
| 114 |
static int aws_event_loop_type_validate_platform(enum aws_event_loop_type type) { |
| 115 |
switch (type) { |
| 116 |
case AWS_EVENT_LOOP_EPOLL: |
| 117 |
#ifndef AWS_ENABLE_EPOLL |
| 118 |
AWS_LOGF_ERROR(AWS_LS_IO_EVENT_LOOP, "Event loop type EPOLL is not supported on the platform."); |
| 119 |
return aws_raise_error(AWS_ERROR_PLATFORM_NOT_SUPPORTED); |
| 120 |
#endif |
| 121 |
break; |
| 122 |
case AWS_EVENT_LOOP_IOCP: |
| 123 |
#ifndef AWS_ENABLE_IO_COMPLETION_PORTS |
| 124 |
AWS_LOGF_ERROR(AWS_LS_IO_EVENT_LOOP, "Event loop type IOCP is not supported on the platform."); |
| 125 |
return aws_raise_error(AWS_ERROR_PLATFORM_NOT_SUPPORTED); |
| 126 |
#endif |
| 127 |
break; |
| 128 |
case AWS_EVENT_LOOP_KQUEUE: |
| 129 |
#ifndef AWS_ENABLE_KQUEUE |
| 130 |
AWS_LOGF_ERROR(AWS_LS_IO_EVENT_LOOP, "Event loop type KQUEUE is not supported on the platform."); |
| 131 |
return aws_raise_error(AWS_ERROR_PLATFORM_NOT_SUPPORTED); |
| 132 |
#endif |
| 133 |
break; |
| 134 |
case AWS_EVENT_LOOP_DISPATCH_QUEUE: |
| 135 |
#ifndef AWS_ENABLE_DISPATCH_QUEUE |
| 136 |
AWS_LOGF_ERROR(AWS_LS_IO_EVENT_LOOP, "Event loop type Dispatch Queue is not supported on the platform."); |
| 137 |
return aws_raise_error(AWS_ERROR_PLATFORM_NOT_SUPPORTED); |
| 138 |
#endif |
| 139 |
break; |
| 140 |
default: |
| 141 |
AWS_LOGF_ERROR(AWS_LS_IO_EVENT_LOOP, "Invalid event loop type."); |
| 142 |
return aws_raise_error(AWS_ERROR_UNSUPPORTED_OPERATION); |
| 143 |
break; |
| 144 |
} |
| 145 |
return AWS_OP_SUCCESS; |
| 146 |
} |
| 147 |
|
| 148 |
struct aws_event_loop *aws_event_loop_new(struct aws_allocator *alloc, const struct aws_event_loop_options *options) { |
| 149 |
|
| 150 |
enum aws_event_loop_type type = options->type; |
| 151 |
if (type == AWS_EVENT_LOOP_PLATFORM_DEFAULT) { |
| 152 |
type = aws_event_loop_get_default_type(); |
| 153 |
} |
| 154 |
|
| 155 |
if (aws_event_loop_type_validate_platform(type)) { |
| 156 |
AWS_LOGF_DEBUG(AWS_LS_IO_EVENT_LOOP, "Invalid event loop type on the platform."); |
| 157 |
return NULL; |
| 158 |
} |
| 159 |
|
| 160 |
switch (type) { |
| 161 |
case AWS_EVENT_LOOP_EPOLL: |
| 162 |
return aws_event_loop_new_with_epoll(alloc, options); |
| 163 |
case AWS_EVENT_LOOP_IOCP: |
| 164 |
return aws_event_loop_new_with_iocp(alloc, options); |
| 165 |
case AWS_EVENT_LOOP_KQUEUE: |
| 166 |
return aws_event_loop_new_with_kqueue(alloc, options); |
| 167 |
case AWS_EVENT_LOOP_DISPATCH_QUEUE: |
| 168 |
return aws_event_loop_new_with_dispatch_queue(alloc, options); |
| 169 |
default: |
| 170 |
AWS_LOGF_DEBUG(AWS_LS_IO_EVENT_LOOP, "Invalid event loop type on the platform."); |
| 171 |
aws_raise_error(AWS_ERROR_PLATFORM_NOT_SUPPORTED); |
| 172 |
return NULL; |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
static void s_event_loop_group_thread_exit(void *user_data) { |
| 177 |
struct aws_event_loop_group *el_group = user_data; |
| 178 |
|
| 179 |
aws_simple_completion_callback *completion_callback = el_group->shutdown_options.shutdown_callback_fn; |
| 180 |
void *completion_user_data = el_group->shutdown_options.shutdown_callback_user_data; |
| 181 |
|
| 182 |
aws_mem_release(el_group->allocator, el_group); |
| 183 |
|
| 184 |
if (completion_callback != NULL) { |
| 185 |
completion_callback(completion_user_data); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
static void s_aws_event_loop_group_shutdown_sync(struct aws_event_loop_group *el_group) { |
| 190 |
size_t loop_count = aws_array_list_length(&el_group->event_loops); |
| 191 |
for (size_t i = 0; i < loop_count; ++i) { |
| 192 |
struct aws_event_loop *loop = NULL; |
| 193 |
aws_array_list_get_at(&el_group->event_loops, &loop, i); |
| 194 |
|
| 195 |
aws_event_loop_start_destroy(loop); |
| 196 |
} |
| 197 |
|
| 198 |
while (aws_array_list_length(&el_group->event_loops) > 0) { |
| 199 |
struct aws_event_loop *loop = NULL; |
| 200 |
|
| 201 |
if (!aws_array_list_back(&el_group->event_loops, &loop)) { |
| 202 |
aws_event_loop_complete_destroy(loop); |
| 203 |
} |
| 204 |
|
| 205 |
aws_array_list_pop_back(&el_group->event_loops); |
| 206 |
} |
| 207 |
|
| 208 |
aws_array_list_clean_up(&el_group->event_loops); |
| 209 |
} |
| 210 |
|
| 211 |
static void s_event_loop_destroy_async_thread_fn(void *thread_data) { |
| 212 |
struct aws_event_loop_group *el_group = thread_data; |
| 213 |
|
| 214 |
s_aws_event_loop_group_shutdown_sync(el_group); |
| 215 |
|
| 216 |
aws_thread_current_at_exit(s_event_loop_group_thread_exit, el_group); |
| 217 |
} |
| 218 |
|
| 219 |
static void s_aws_event_loop_group_shutdown_async(void *user_data) { |
| 220 |
struct aws_event_loop_group *el_group = user_data; |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
struct aws_thread cleanup_thread; |
| 227 |
AWS_ZERO_STRUCT(cleanup_thread); |
| 228 |
|
| 229 |
aws_thread_init(&cleanup_thread, el_group->allocator); |
| 230 |
|
| 231 |
struct aws_thread_options thread_options = *aws_default_thread_options(); |
| 232 |
thread_options.join_strategy = AWS_TJS_MANAGED; |
| 233 |
thread_options.name = aws_byte_cursor_from_c_str("EvntLoopCleanup"); |
| 234 |
|
| 235 |
aws_thread_launch(&cleanup_thread, s_event_loop_destroy_async_thread_fn, el_group, &thread_options); |
| 236 |
} |
| 237 |
|
| 238 |
struct aws_event_loop_group *aws_event_loop_group_new_internal( |
| 239 |
struct aws_allocator *allocator, |
| 240 |
const struct aws_event_loop_group_options *options, |
| 241 |
aws_new_event_loop_fn *new_loop_fn, |
| 242 |
void *new_loop_user_data) { |
| 243 |
AWS_FATAL_ASSERT(new_loop_fn); |
| 244 |
|
| 245 |
aws_io_clock_fn *clock = options->clock_override; |
| 246 |
if (!clock) { |
| 247 |
clock = aws_high_res_clock_get_ticks; |
| 248 |
} |
| 249 |
|
| 250 |
size_t group_cpu_count = 0; |
| 251 |
struct aws_cpu_info *usable_cpus = NULL; |
| 252 |
|
| 253 |
bool pin_threads = options->cpu_group != NULL; |
| 254 |
if (pin_threads) { |
| 255 |
uint16_t cpu_group = *options->cpu_group; |
| 256 |
group_cpu_count = aws_get_cpu_count_for_group(cpu_group); |
| 257 |
if (!group_cpu_count) { |
| 258 |
|
| 259 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 260 |
return NULL; |
| 261 |
} |
| 262 |
|
| 263 |
usable_cpus = aws_mem_calloc(allocator, group_cpu_count, sizeof(struct aws_cpu_info)); |
| 264 |
if (usable_cpus == NULL) { |
| 265 |
return NULL; |
| 266 |
} |
| 267 |
|
| 268 |
aws_get_cpu_ids_for_group(cpu_group, usable_cpus, group_cpu_count); |
| 269 |
} |
| 270 |
|
| 271 |
struct aws_event_loop_group *el_group = aws_mem_calloc(allocator, 1, sizeof(struct aws_event_loop_group)); |
| 272 |
|
| 273 |
el_group->allocator = allocator; |
| 274 |
aws_ref_count_init( |
| 275 |
&el_group->ref_count, el_group, (aws_simple_completion_callback *)s_aws_event_loop_group_shutdown_async); |
| 276 |
|
| 277 |
uint16_t el_count = options->loop_count; |
| 278 |
if (el_count == 0) { |
| 279 |
uint16_t processor_count = (uint16_t)aws_system_info_processor_count(); |
| 280 |
|
| 281 |
el_count = processor_count > 1 ? processor_count / 2 : processor_count; |
| 282 |
} |
| 283 |
|
| 284 |
if (aws_array_list_init_dynamic(&el_group->event_loops, allocator, el_count, sizeof(struct aws_event_loop *))) { |
| 285 |
goto on_error; |
| 286 |
} |
| 287 |
|
| 288 |
el_group->event_loop_type = options->type; |
| 289 |
if (el_group->event_loop_type == AWS_EVENT_LOOP_PLATFORM_DEFAULT) { |
| 290 |
el_group->event_loop_type = aws_event_loop_get_default_type(); |
| 291 |
} |
| 292 |
|
| 293 |
for (uint16_t i = 0; i < el_count; ++i) { |
| 294 |
|
| 295 |
if (!pin_threads || (i < group_cpu_count && !usable_cpus[i].suspected_hyper_thread)) { |
| 296 |
struct aws_thread_options thread_options = *aws_default_thread_options(); |
| 297 |
|
| 298 |
struct aws_event_loop_options el_options = { |
| 299 |
.clock = clock, |
| 300 |
.thread_options = &thread_options, |
| 301 |
.type = options->type, |
| 302 |
.parent_elg = el_group, |
| 303 |
}; |
| 304 |
|
| 305 |
if (pin_threads) { |
| 306 |
thread_options.cpu_id = usable_cpus[i].cpu_id; |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
char thread_name[32] = {0}; |
| 311 |
int thread_name_len = snprintf(thread_name, sizeof(thread_name), "AwsEventLoop%d", (int)i + 1); |
| 312 |
if (thread_name_len > AWS_THREAD_NAME_RECOMMENDED_STRLEN) { |
| 313 |
snprintf(thread_name, sizeof(thread_name), "AwsEventLoop"); |
| 314 |
} |
| 315 |
thread_options.name = aws_byte_cursor_from_c_str(thread_name); |
| 316 |
|
| 317 |
struct aws_event_loop *loop = new_loop_fn(allocator, &el_options, new_loop_user_data); |
| 318 |
if (!loop) { |
| 319 |
goto on_error; |
| 320 |
} |
| 321 |
|
| 322 |
if (aws_array_list_push_back(&el_group->event_loops, (const void *)&loop)) { |
| 323 |
aws_event_loop_destroy(loop); |
| 324 |
goto on_error; |
| 325 |
} |
| 326 |
|
| 327 |
if (aws_event_loop_run(loop)) { |
| 328 |
goto on_error; |
| 329 |
} |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
if (options->shutdown_options != NULL) { |
| 334 |
el_group->shutdown_options = *options->shutdown_options; |
| 335 |
} |
| 336 |
|
| 337 |
if (pin_threads) { |
| 338 |
aws_mem_release(allocator, usable_cpus); |
| 339 |
} |
| 340 |
|
| 341 |
return el_group; |
| 342 |
|
| 343 |
on_error:; |
| 344 |
|
| 345 |
int cached_error_code = aws_last_error(); |
| 346 |
|
| 347 |
aws_mem_release(allocator, usable_cpus); |
| 348 |
s_aws_event_loop_group_shutdown_sync(el_group); |
| 349 |
s_event_loop_group_thread_exit(el_group); |
| 350 |
|
| 351 |
|
| 352 |
aws_raise_error(cached_error_code); |
| 353 |
return NULL; |
| 354 |
} |
| 355 |
|
| 356 |
static struct aws_event_loop *s_default_new_event_loop( |
| 357 |
struct aws_allocator *allocator, |
| 358 |
const struct aws_event_loop_options *options, |
| 359 |
void *user_data) { |
| 360 |
|
| 361 |
(void)user_data; |
| 362 |
return aws_event_loop_new(allocator, options); |
| 363 |
} |
| 364 |
|
| 365 |
struct aws_event_loop_group *aws_event_loop_group_new( |
| 366 |
struct aws_allocator *allocator, |
| 367 |
const struct aws_event_loop_group_options *options) { |
| 368 |
|
| 369 |
return aws_event_loop_group_new_internal(allocator, options, s_default_new_event_loop, NULL); |
| 370 |
} |
| 371 |
|
| 372 |
struct aws_event_loop_group *aws_event_loop_group_acquire(struct aws_event_loop_group *el_group) { |
| 373 |
if (el_group != NULL) { |
| 374 |
aws_ref_count_acquire(&el_group->ref_count); |
| 375 |
} |
| 376 |
|
| 377 |
return el_group; |
| 378 |
} |
| 379 |
|
| 380 |
void aws_event_loop_group_release(struct aws_event_loop_group *el_group) { |
| 381 |
if (el_group != NULL) { |
| 382 |
aws_ref_count_release(&el_group->ref_count); |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
struct aws_event_loop_group *aws_event_loop_group_acquire_from_event_loop(struct aws_event_loop *event_loop) { |
| 387 |
if (event_loop != NULL) { |
| 388 |
return aws_event_loop_group_acquire(event_loop->base_elg); |
| 389 |
} |
| 390 |
return NULL; |
| 391 |
} |
| 392 |
|
| 393 |
void aws_event_loop_group_release_from_event_loop(struct aws_event_loop *event_loop) { |
| 394 |
if (event_loop != NULL) { |
| 395 |
aws_event_loop_group_release(event_loop->base_elg); |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
size_t aws_event_loop_group_get_loop_count(const struct aws_event_loop_group *el_group) { |
| 400 |
return aws_array_list_length(&el_group->event_loops); |
| 401 |
} |
| 402 |
|
| 403 |
enum aws_event_loop_type aws_event_loop_group_get_type(const struct aws_event_loop_group *el_group) { |
| 404 |
return el_group->event_loop_type; |
| 405 |
} |
| 406 |
|
| 407 |
struct aws_event_loop *aws_event_loop_group_get_loop_at(struct aws_event_loop_group *el_group, size_t index) { |
| 408 |
struct aws_event_loop *el = NULL; |
| 409 |
aws_array_list_get_at(&el_group->event_loops, &el, index); |
| 410 |
return el; |
| 411 |
} |
| 412 |
|
| 413 |
struct aws_event_loop *aws_event_loop_group_get_next_loop(struct aws_event_loop_group *el_group) { |
| 414 |
size_t loop_count = aws_array_list_length(&el_group->event_loops); |
| 415 |
AWS_ASSERT(loop_count > 0); |
| 416 |
if (loop_count == 0) { |
| 417 |
return NULL; |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
uint32_t random_32_bit_num = 0; |
| 422 |
aws_device_random_u32(&random_32_bit_num); |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
uint16_t random_num_a = (uint16_t)random_32_bit_num; |
| 430 |
random_num_a = random_num_a % loop_count; |
| 431 |
|
| 432 |
uint16_t random_num_b = (uint16_t)(random_32_bit_num >> 16); |
| 433 |
random_num_b = random_num_b % loop_count; |
| 434 |
|
| 435 |
struct aws_event_loop *random_loop_a = NULL; |
| 436 |
struct aws_event_loop *random_loop_b = NULL; |
| 437 |
aws_array_list_get_at(&el_group->event_loops, &random_loop_a, random_num_a); |
| 438 |
aws_array_list_get_at(&el_group->event_loops, &random_loop_b, random_num_b); |
| 439 |
|
| 440 |
|
| 441 |
AWS_FATAL_ASSERT((random_loop_a && random_loop_b) && "random_loop_a or random_loop_b is NULL."); |
| 442 |
|
| 443 |
size_t load_a = aws_event_loop_get_load_factor(random_loop_a); |
| 444 |
size_t load_b = aws_event_loop_get_load_factor(random_loop_b); |
| 445 |
|
| 446 |
return load_a < load_b ? random_loop_a : random_loop_b; |
| 447 |
} |
| 448 |
|
| 449 |
static void s_object_removed(void *value) { |
| 450 |
struct aws_event_loop_local_object *object = (struct aws_event_loop_local_object *)value; |
| 451 |
if (object->on_object_removed) { |
| 452 |
object->on_object_removed(object); |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
int aws_event_loop_init_base(struct aws_event_loop *event_loop, struct aws_allocator *alloc, aws_io_clock_fn *clock) { |
| 457 |
AWS_ZERO_STRUCT(*event_loop); |
| 458 |
|
| 459 |
event_loop->alloc = alloc; |
| 460 |
event_loop->clock = clock; |
| 461 |
aws_atomic_init_int(&event_loop->current_load_factor, 0u); |
| 462 |
aws_atomic_init_int(&event_loop->next_flush_time, 0u); |
| 463 |
|
| 464 |
if (aws_hash_table_init(&event_loop->local_data, alloc, 20, aws_hash_ptr, aws_ptr_eq, NULL, s_object_removed)) { |
| 465 |
return AWS_OP_ERR; |
| 466 |
} |
| 467 |
|
| 468 |
return AWS_OP_SUCCESS; |
| 469 |
} |
| 470 |
|
| 471 |
void aws_event_loop_clean_up_base(struct aws_event_loop *event_loop) { |
| 472 |
aws_hash_table_clean_up(&event_loop->local_data); |
| 473 |
} |
| 474 |
|
| 475 |
void aws_event_loop_register_tick_start(struct aws_event_loop *event_loop) { |
| 476 |
aws_high_res_clock_get_ticks(&event_loop->latest_tick_start); |
| 477 |
} |
| 478 |
|
| 479 |
void aws_event_loop_register_tick_end(struct aws_event_loop *event_loop) { |
| 480 |
|
| 481 |
|
| 482 |
uint64_t end_tick = 0; |
| 483 |
aws_high_res_clock_get_ticks(&end_tick); |
| 484 |
|
| 485 |
size_t elapsed = (size_t)aws_min_u64(end_tick - event_loop->latest_tick_start, SIZE_MAX); |
| 486 |
event_loop->current_tick_latency_sum = aws_add_size_saturating(event_loop->current_tick_latency_sum, elapsed); |
| 487 |
event_loop->latest_tick_start = 0; |
| 488 |
|
| 489 |
size_t next_flush_time_secs = aws_atomic_load_int(&event_loop->next_flush_time); |
| 490 |
|
| 491 |
uint64_t end_tick_secs = aws_timestamp_convert(end_tick, AWS_TIMESTAMP_NANOS, AWS_TIMESTAMP_SECS, NULL); |
| 492 |
|
| 493 |
|
| 494 |
if (end_tick_secs > next_flush_time_secs) { |
| 495 |
aws_atomic_store_int(&event_loop->current_load_factor, event_loop->current_tick_latency_sum); |
| 496 |
event_loop->current_tick_latency_sum = 0; |
| 497 |
|
| 498 |
aws_atomic_store_int(&event_loop->next_flush_time, (size_t)(end_tick_secs + 1)); |
| 499 |
} |
| 500 |
} |
| 501 |
|
| 502 |
size_t aws_event_loop_get_load_factor(struct aws_event_loop *event_loop) { |
| 503 |
uint64_t current_time = 0; |
| 504 |
aws_high_res_clock_get_ticks(¤t_time); |
| 505 |
|
| 506 |
uint64_t current_time_secs = aws_timestamp_convert(current_time, AWS_TIMESTAMP_NANOS, AWS_TIMESTAMP_SECS, NULL); |
| 507 |
size_t next_flush_time_secs = aws_atomic_load_int(&event_loop->next_flush_time); |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
if (current_time_secs > next_flush_time_secs + 10) { |
| 515 |
return 0; |
| 516 |
} |
| 517 |
|
| 518 |
return aws_atomic_load_int(&event_loop->current_load_factor); |
| 519 |
} |
| 520 |
|
| 521 |
void aws_event_loop_destroy(struct aws_event_loop *event_loop) { |
| 522 |
if (!event_loop) { |
| 523 |
return; |
| 524 |
} |
| 525 |
|
| 526 |
AWS_ASSERT(event_loop->vtable && event_loop->vtable->start_destroy); |
| 527 |
AWS_ASSERT(event_loop->vtable && event_loop->vtable->complete_destroy); |
| 528 |
AWS_ASSERT(!aws_event_loop_thread_is_callers_thread(event_loop)); |
| 529 |
|
| 530 |
event_loop->vtable->start_destroy(event_loop); |
| 531 |
event_loop->vtable->complete_destroy(event_loop); |
| 532 |
} |
| 533 |
|
| 534 |
void aws_event_loop_start_destroy(struct aws_event_loop *event_loop) { |
| 535 |
if (!event_loop) { |
| 536 |
return; |
| 537 |
} |
| 538 |
|
| 539 |
AWS_ASSERT(event_loop->vtable && event_loop->vtable->start_destroy); |
| 540 |
AWS_ASSERT(!aws_event_loop_thread_is_callers_thread(event_loop)); |
| 541 |
|
| 542 |
event_loop->vtable->start_destroy(event_loop); |
| 543 |
} |
| 544 |
|
| 545 |
void aws_event_loop_complete_destroy(struct aws_event_loop *event_loop) { |
| 546 |
if (!event_loop) { |
| 547 |
return; |
| 548 |
} |
| 549 |
|
| 550 |
AWS_ASSERT(event_loop->vtable && event_loop->vtable->complete_destroy); |
| 551 |
AWS_ASSERT(!aws_event_loop_thread_is_callers_thread(event_loop)); |
| 552 |
|
| 553 |
event_loop->vtable->complete_destroy(event_loop); |
| 554 |
} |
| 555 |
|
| 556 |
int aws_event_loop_fetch_local_object( |
| 557 |
struct aws_event_loop *event_loop, |
| 558 |
void *key, |
| 559 |
struct aws_event_loop_local_object *obj) { |
| 560 |
|
| 561 |
AWS_ASSERT(aws_event_loop_thread_is_callers_thread(event_loop)); |
| 562 |
|
| 563 |
struct aws_hash_element *object = NULL; |
| 564 |
if (!aws_hash_table_find(&event_loop->local_data, key, &object) && object) { |
| 565 |
*obj = *(struct aws_event_loop_local_object *)object->value; |
| 566 |
return AWS_OP_SUCCESS; |
| 567 |
} |
| 568 |
|
| 569 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 570 |
} |
| 571 |
|
| 572 |
int aws_event_loop_put_local_object(struct aws_event_loop *event_loop, struct aws_event_loop_local_object *obj) { |
| 573 |
AWS_ASSERT(aws_event_loop_thread_is_callers_thread(event_loop)); |
| 574 |
|
| 575 |
struct aws_hash_element *object = NULL; |
| 576 |
int was_created = 0; |
| 577 |
|
| 578 |
if (!aws_hash_table_create(&event_loop->local_data, obj->key, &object, &was_created)) { |
| 579 |
object->key = obj->key; |
| 580 |
object->value = obj; |
| 581 |
return AWS_OP_SUCCESS; |
| 582 |
} |
| 583 |
|
| 584 |
return AWS_OP_ERR; |
| 585 |
} |
| 586 |
|
| 587 |
int aws_event_loop_remove_local_object( |
| 588 |
struct aws_event_loop *event_loop, |
| 589 |
void *key, |
| 590 |
struct aws_event_loop_local_object *removed_obj) { |
| 591 |
|
| 592 |
AWS_ASSERT(aws_event_loop_thread_is_callers_thread(event_loop)); |
| 593 |
|
| 594 |
struct aws_hash_element existing_object; |
| 595 |
AWS_ZERO_STRUCT(existing_object); |
| 596 |
|
| 597 |
int was_present = 0; |
| 598 |
|
| 599 |
struct aws_hash_element *remove_candidate = removed_obj ? &existing_object : NULL; |
| 600 |
|
| 601 |
if (!aws_hash_table_remove(&event_loop->local_data, key, remove_candidate, &was_present)) { |
| 602 |
if (remove_candidate && was_present) { |
| 603 |
*removed_obj = *(struct aws_event_loop_local_object *)existing_object.value; |
| 604 |
} |
| 605 |
|
| 606 |
return AWS_OP_SUCCESS; |
| 607 |
} |
| 608 |
|
| 609 |
return AWS_OP_ERR; |
| 610 |
} |
| 611 |
|
| 612 |
int aws_event_loop_run(struct aws_event_loop *event_loop) { |
| 613 |
AWS_ASSERT(event_loop->vtable && event_loop->vtable->run); |
| 614 |
return event_loop->vtable->run(event_loop); |
| 615 |
} |
| 616 |
|
| 617 |
int aws_event_loop_stop(struct aws_event_loop *event_loop) { |
| 618 |
AWS_ASSERT(event_loop->vtable && event_loop->vtable->stop); |
| 619 |
return event_loop->vtable->stop(event_loop); |
| 620 |
} |
| 621 |
|
| 622 |
int aws_event_loop_wait_for_stop_completion(struct aws_event_loop *event_loop) { |
| 623 |
AWS_ASSERT(!aws_event_loop_thread_is_callers_thread(event_loop)); |
| 624 |
AWS_ASSERT(event_loop->vtable && event_loop->vtable->wait_for_stop_completion); |
| 625 |
return event_loop->vtable->wait_for_stop_completion(event_loop); |
| 626 |
} |
| 627 |
|
| 628 |
void aws_event_loop_schedule_task_now(struct aws_event_loop *event_loop, struct aws_task *task) { |
| 629 |
AWS_ASSERT(task); |
| 630 |
AWS_ASSERT(event_loop->vtable && event_loop->vtable->schedule_task_now); |
| 631 |
event_loop->vtable->schedule_task_now(event_loop, task); |
| 632 |
} |
| 633 |
|
| 634 |
void aws_event_loop_schedule_task_now_serialized(struct aws_event_loop *event_loop, struct aws_task *task) { |
| 635 |
AWS_ASSERT(task); |
| 636 |
AWS_ASSERT(event_loop->vtable && event_loop->vtable->schedule_task_now_serialized); |
| 637 |
event_loop->vtable->schedule_task_now_serialized(event_loop, task); |
| 638 |
} |
| 639 |
|
| 640 |
void aws_event_loop_schedule_task_future( |
| 641 |
struct aws_event_loop *event_loop, |
| 642 |
struct aws_task *task, |
| 643 |
uint64_t run_at_nanos) { |
| 644 |
AWS_ASSERT(task); |
| 645 |
AWS_ASSERT(event_loop->vtable && event_loop->vtable->schedule_task_future); |
| 646 |
event_loop->vtable->schedule_task_future(event_loop, task, run_at_nanos); |
| 647 |
} |
| 648 |
|
| 649 |
void aws_event_loop_cancel_task(struct aws_event_loop *event_loop, struct aws_task *task) { |
| 650 |
AWS_ASSERT(task); |
| 651 |
AWS_ASSERT(event_loop->vtable && event_loop->vtable->cancel_task); |
| 652 |
AWS_ASSERT(aws_event_loop_thread_is_callers_thread(event_loop)); |
| 653 |
event_loop->vtable->cancel_task(event_loop, task); |
| 654 |
} |
| 655 |
|
| 656 |
int aws_event_loop_connect_handle_to_io_completion_port( |
| 657 |
struct aws_event_loop *event_loop, |
| 658 |
struct aws_io_handle *handle) { |
| 659 |
AWS_ASSERT(event_loop->vtable && event_loop->vtable->connect_to_io_completion_port); |
| 660 |
return event_loop->vtable->connect_to_io_completion_port(event_loop, handle); |
| 661 |
} |
| 662 |
|
| 663 |
int aws_event_loop_subscribe_to_io_events( |
| 664 |
struct aws_event_loop *event_loop, |
| 665 |
struct aws_io_handle *handle, |
| 666 |
int events, |
| 667 |
aws_event_loop_on_event_fn *on_event, |
| 668 |
void *user_data) { |
| 669 |
AWS_ASSERT(event_loop->vtable && event_loop->vtable->subscribe_to_io_events); |
| 670 |
return event_loop->vtable->subscribe_to_io_events(event_loop, handle, events, on_event, user_data); |
| 671 |
} |
| 672 |
|
| 673 |
int aws_event_loop_unsubscribe_from_io_events(struct aws_event_loop *event_loop, struct aws_io_handle *handle) { |
| 674 |
AWS_ASSERT(aws_event_loop_thread_is_callers_thread(event_loop)); |
| 675 |
AWS_ASSERT(event_loop->vtable && event_loop->vtable->unsubscribe_from_io_events); |
| 676 |
return event_loop->vtable->unsubscribe_from_io_events(event_loop, handle); |
| 677 |
} |
| 678 |
|
| 679 |
void aws_event_loop_free_io_event_resources(struct aws_event_loop *event_loop, struct aws_io_handle *handle) { |
| 680 |
AWS_ASSERT(event_loop && event_loop->vtable->free_io_event_resources); |
| 681 |
event_loop->vtable->free_io_event_resources(handle->additional_data); |
| 682 |
} |
| 683 |
|
| 684 |
bool aws_event_loop_thread_is_callers_thread(struct aws_event_loop *event_loop) { |
| 685 |
AWS_ASSERT(event_loop->vtable && event_loop->vtable->is_on_callers_thread); |
| 686 |
return event_loop->vtable->is_on_callers_thread(event_loop); |
| 687 |
} |
| 688 |
|
| 689 |
int aws_event_loop_current_clock_time(const struct aws_event_loop *event_loop, uint64_t *time_nanos) { |
| 690 |
AWS_ASSERT(event_loop->clock); |
| 691 |
return event_loop->clock(time_nanos); |
| 692 |
} |
| 693 |
|
| 694 |
struct aws_event_loop_group *aws_event_loop_group_new_default( |
| 695 |
struct aws_allocator *alloc, |
| 696 |
uint16_t max_threads, |
| 697 |
const struct aws_shutdown_callback_options *shutdown_options) { |
| 698 |
|
| 699 |
struct aws_event_loop_group_options elg_options = { |
| 700 |
.loop_count = max_threads, |
| 701 |
.shutdown_options = shutdown_options, |
| 702 |
}; |
| 703 |
|
| 704 |
return aws_event_loop_group_new(alloc, &elg_options); |
| 705 |
} |
| 706 |
|
| 707 |
struct aws_event_loop_group *aws_event_loop_group_new_default_pinned_to_cpu_group( |
| 708 |
struct aws_allocator *alloc, |
| 709 |
uint16_t max_threads, |
| 710 |
uint16_t cpu_group, |
| 711 |
const struct aws_shutdown_callback_options *shutdown_options) { |
| 712 |
|
| 713 |
struct aws_event_loop_group_options elg_options = { |
| 714 |
.loop_count = max_threads, |
| 715 |
.shutdown_options = shutdown_options, |
| 716 |
.cpu_group = &cpu_group, |
| 717 |
}; |
| 718 |
|
| 719 |
return aws_event_loop_group_new(alloc, &elg_options); |
| 720 |
} |
| 721 |
|
| 722 |
void *aws_event_loop_get_impl(struct aws_event_loop *event_loop) { |
| 723 |
return event_loop->impl_data; |
| 724 |
} |
| 725 |
|
| 726 |
struct aws_event_loop *aws_event_loop_new_base( |
| 727 |
struct aws_allocator *allocator, |
| 728 |
aws_io_clock_fn *clock, |
| 729 |
struct aws_event_loop_vtable *vtable, |
| 730 |
void *impl) { |
| 731 |
struct aws_event_loop *event_loop = aws_mem_acquire(allocator, sizeof(struct aws_event_loop)); |
| 732 |
aws_event_loop_init_base(event_loop, allocator, clock); |
| 733 |
event_loop->impl_data = impl; |
| 734 |
event_loop->vtable = vtable; |
| 735 |
|
| 736 |
return event_loop; |
| 737 |
} |
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
void aws_event_loop_override_default_type(enum aws_event_loop_type default_type_override) { |
| 746 |
if (aws_event_loop_type_validate_platform(default_type_override) == AWS_OP_SUCCESS) { |
| 747 |
s_default_event_loop_type_override = default_type_override; |
| 748 |
} else { |
| 749 |
s_default_event_loop_type_override = AWS_EVENT_LOOP_PLATFORM_DEFAULT; |
| 750 |
} |
| 751 |
} |