| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/io/channel.h> |
| 7 |
|
| 8 |
#include <aws/common/atomics.h> |
| 9 |
#include <aws/common/clock.h> |
| 10 |
#include <aws/common/mutex.h> |
| 11 |
|
| 12 |
#include <aws/io/event_loop.h> |
| 13 |
#include <aws/io/logging.h> |
| 14 |
#include <aws/io/message_pool.h> |
| 15 |
#include <aws/io/private/event_loop_impl.h> |
| 16 |
#include <aws/io/statistics.h> |
| 17 |
|
| 18 |
#ifdef _MSC_VER |
| 19 |
# pragma warning(disable : 4204) |
| 20 |
#endif |
| 21 |
|
| 22 |
static size_t s_message_pool_key = 0; |
| 23 |
|
| 24 |
enum { |
| 25 |
KB_16 = 16 * 1024, |
| 26 |
}; |
| 27 |
|
| 28 |
size_t g_aws_channel_max_fragment_size = KB_16; |
| 29 |
|
| 30 |
#define INITIAL_STATISTIC_LIST_SIZE 5 |
| 31 |
|
| 32 |
enum aws_channel_state { |
| 33 |
AWS_CHANNEL_SETTING_UP, |
| 34 |
AWS_CHANNEL_ACTIVE, |
| 35 |
AWS_CHANNEL_SHUTTING_DOWN, |
| 36 |
AWS_CHANNEL_SHUT_DOWN, |
| 37 |
}; |
| 38 |
|
| 39 |
struct aws_shutdown_notification_task { |
| 40 |
struct aws_task task; |
| 41 |
int error_code; |
| 42 |
struct aws_channel_slot *slot; |
| 43 |
bool shutdown_immediately; |
| 44 |
}; |
| 45 |
|
| 46 |
struct shutdown_task { |
| 47 |
struct aws_channel_task task; |
| 48 |
struct aws_channel *channel; |
| 49 |
int error_code; |
| 50 |
bool shutdown_immediately; |
| 51 |
}; |
| 52 |
|
| 53 |
struct aws_channel { |
| 54 |
struct aws_allocator *alloc; |
| 55 |
struct aws_event_loop *loop; |
| 56 |
struct aws_channel_slot *first; |
| 57 |
struct aws_message_pool *msg_pool; |
| 58 |
enum aws_channel_state channel_state; |
| 59 |
struct aws_shutdown_notification_task shutdown_notify_task; |
| 60 |
aws_channel_on_shutdown_completed_fn *on_shutdown_completed; |
| 61 |
void *shutdown_user_data; |
| 62 |
struct aws_atomic_var refcount; |
| 63 |
struct aws_task deletion_task; |
| 64 |
|
| 65 |
struct aws_task statistics_task; |
| 66 |
struct aws_crt_statistics_handler *statistics_handler; |
| 67 |
uint64_t statistics_interval_start_time_ms; |
| 68 |
struct aws_array_list statistic_list; |
| 69 |
|
| 70 |
struct { |
| 71 |
struct aws_linked_list list; |
| 72 |
} channel_thread_tasks; |
| 73 |
struct { |
| 74 |
struct aws_mutex lock; |
| 75 |
struct aws_linked_list list; |
| 76 |
struct aws_task scheduling_task; |
| 77 |
struct shutdown_task shutdown_task; |
| 78 |
bool is_channel_shut_down; |
| 79 |
} cross_thread_tasks; |
| 80 |
|
| 81 |
size_t window_update_batch_emit_threshold; |
| 82 |
struct aws_channel_task window_update_task; |
| 83 |
bool read_back_pressure_enabled; |
| 84 |
bool window_update_scheduled; |
| 85 |
}; |
| 86 |
|
| 87 |
struct channel_setup_args { |
| 88 |
struct aws_allocator *alloc; |
| 89 |
struct aws_channel *channel; |
| 90 |
aws_channel_on_setup_completed_fn *on_setup_completed; |
| 91 |
void *user_data; |
| 92 |
struct aws_task task; |
| 93 |
}; |
| 94 |
|
| 95 |
static void s_on_msg_pool_removed(struct aws_event_loop_local_object *object) { |
| 96 |
struct aws_message_pool *msg_pool = object->object; |
| 97 |
AWS_LOGF_TRACE( |
| 98 |
AWS_LS_IO_CHANNEL, |
| 99 |
"static: message pool %p has been purged " |
| 100 |
"from the event-loop: likely because of shutdown", |
| 101 |
(void *)msg_pool); |
| 102 |
struct aws_allocator *alloc = msg_pool->alloc; |
| 103 |
aws_message_pool_clean_up(msg_pool); |
| 104 |
aws_mem_release(alloc, msg_pool); |
| 105 |
aws_mem_release(alloc, object); |
| 106 |
} |
| 107 |
|
| 108 |
static void s_on_channel_setup_complete(struct aws_task *task, void *arg, enum aws_task_status task_status) { |
| 109 |
|
| 110 |
(void)task; |
| 111 |
struct channel_setup_args *setup_args = arg; |
| 112 |
struct aws_message_pool *message_pool = NULL; |
| 113 |
struct aws_event_loop_local_object *local_object = NULL; |
| 114 |
|
| 115 |
AWS_LOGF_DEBUG(AWS_LS_IO_CHANNEL, "id=%p: setup complete, notifying caller.", (void *)setup_args->channel); |
| 116 |
if (task_status == AWS_TASK_STATUS_RUN_READY) { |
| 117 |
struct aws_event_loop_local_object stack_obj; |
| 118 |
AWS_ZERO_STRUCT(stack_obj); |
| 119 |
local_object = &stack_obj; |
| 120 |
|
| 121 |
if (aws_event_loop_fetch_local_object(setup_args->channel->loop, &s_message_pool_key, local_object)) { |
| 122 |
|
| 123 |
local_object = aws_mem_calloc(setup_args->alloc, 1, sizeof(struct aws_event_loop_local_object)); |
| 124 |
if (!local_object) { |
| 125 |
goto cleanup_setup_args; |
| 126 |
} |
| 127 |
|
| 128 |
message_pool = aws_mem_acquire(setup_args->alloc, sizeof(struct aws_message_pool)); |
| 129 |
if (!message_pool) { |
| 130 |
goto cleanup_local_obj; |
| 131 |
} |
| 132 |
|
| 133 |
AWS_LOGF_DEBUG( |
| 134 |
AWS_LS_IO_CHANNEL, |
| 135 |
"id=%p: no message pool is currently stored in the event-loop " |
| 136 |
"local storage, adding %p with max message size %zu, " |
| 137 |
"message count 4, with 4 small blocks of 128 bytes.", |
| 138 |
(void *)setup_args->channel, |
| 139 |
(void *)message_pool, |
| 140 |
g_aws_channel_max_fragment_size); |
| 141 |
|
| 142 |
struct aws_message_pool_creation_args creation_args = { |
| 143 |
.application_data_msg_data_size = g_aws_channel_max_fragment_size, |
| 144 |
.application_data_msg_count = 4, |
| 145 |
.small_block_msg_count = 4, |
| 146 |
.small_block_msg_data_size = 128, |
| 147 |
}; |
| 148 |
|
| 149 |
if (aws_message_pool_init(message_pool, setup_args->alloc, &creation_args)) { |
| 150 |
goto cleanup_msg_pool_mem; |
| 151 |
} |
| 152 |
|
| 153 |
local_object->key = &s_message_pool_key; |
| 154 |
local_object->object = message_pool; |
| 155 |
local_object->on_object_removed = s_on_msg_pool_removed; |
| 156 |
|
| 157 |
if (aws_event_loop_put_local_object(setup_args->channel->loop, local_object)) { |
| 158 |
goto cleanup_msg_pool; |
| 159 |
} |
| 160 |
} else { |
| 161 |
message_pool = local_object->object; |
| 162 |
AWS_LOGF_DEBUG( |
| 163 |
AWS_LS_IO_CHANNEL, |
| 164 |
"id=%p: message pool %p found in event-loop local storage: using it.", |
| 165 |
(void *)setup_args->channel, |
| 166 |
(void *)message_pool); |
| 167 |
} |
| 168 |
|
| 169 |
setup_args->channel->msg_pool = message_pool; |
| 170 |
setup_args->channel->channel_state = AWS_CHANNEL_ACTIVE; |
| 171 |
setup_args->on_setup_completed(setup_args->channel, AWS_OP_SUCCESS, setup_args->user_data); |
| 172 |
aws_channel_release_hold(setup_args->channel); |
| 173 |
aws_mem_release(setup_args->alloc, setup_args); |
| 174 |
return; |
| 175 |
} |
| 176 |
|
| 177 |
goto cleanup_setup_args; |
| 178 |
|
| 179 |
cleanup_msg_pool: |
| 180 |
aws_message_pool_clean_up(message_pool); |
| 181 |
|
| 182 |
cleanup_msg_pool_mem: |
| 183 |
aws_mem_release(setup_args->alloc, message_pool); |
| 184 |
|
| 185 |
cleanup_local_obj: |
| 186 |
aws_mem_release(setup_args->alloc, local_object); |
| 187 |
|
| 188 |
cleanup_setup_args: |
| 189 |
setup_args->on_setup_completed(setup_args->channel, AWS_OP_ERR, setup_args->user_data); |
| 190 |
aws_channel_release_hold(setup_args->channel); |
| 191 |
aws_mem_release(setup_args->alloc, setup_args); |
| 192 |
} |
| 193 |
|
| 194 |
static void s_schedule_cross_thread_tasks(struct aws_task *task, void *arg, enum aws_task_status status); |
| 195 |
|
| 196 |
static void s_destroy_partially_constructed_channel(struct aws_channel *channel) { |
| 197 |
if (channel == NULL) { |
| 198 |
return; |
| 199 |
} |
| 200 |
|
| 201 |
aws_array_list_clean_up(&channel->statistic_list); |
| 202 |
|
| 203 |
aws_mem_release(channel->alloc, channel); |
| 204 |
} |
| 205 |
|
| 206 |
struct aws_channel *aws_channel_new(struct aws_allocator *alloc, const struct aws_channel_options *creation_args) { |
| 207 |
AWS_PRECONDITION(creation_args); |
| 208 |
AWS_PRECONDITION(creation_args->event_loop); |
| 209 |
AWS_PRECONDITION(creation_args->on_setup_completed); |
| 210 |
|
| 211 |
struct aws_channel *channel = aws_mem_calloc(alloc, 1, sizeof(struct aws_channel)); |
| 212 |
if (!channel) { |
| 213 |
return NULL; |
| 214 |
} |
| 215 |
|
| 216 |
AWS_LOGF_DEBUG(AWS_LS_IO_CHANNEL, "id=%p: Beginning creation and setup of new channel.", (void *)channel); |
| 217 |
channel->alloc = alloc; |
| 218 |
channel->loop = creation_args->event_loop; |
| 219 |
channel->on_shutdown_completed = creation_args->on_shutdown_completed; |
| 220 |
channel->shutdown_user_data = creation_args->shutdown_user_data; |
| 221 |
|
| 222 |
if (aws_array_list_init_dynamic( |
| 223 |
&channel->statistic_list, alloc, INITIAL_STATISTIC_LIST_SIZE, sizeof(struct aws_crt_statistics_base *))) { |
| 224 |
goto on_error; |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
aws_atomic_init_int(&channel->refcount, 2); |
| 231 |
|
| 232 |
struct channel_setup_args *setup_args = aws_mem_calloc(alloc, 1, sizeof(struct channel_setup_args)); |
| 233 |
if (!setup_args) { |
| 234 |
goto on_error; |
| 235 |
} |
| 236 |
|
| 237 |
channel->channel_state = AWS_CHANNEL_SETTING_UP; |
| 238 |
aws_linked_list_init(&channel->channel_thread_tasks.list); |
| 239 |
aws_linked_list_init(&channel->cross_thread_tasks.list); |
| 240 |
channel->cross_thread_tasks.lock = (struct aws_mutex)AWS_MUTEX_INIT; |
| 241 |
|
| 242 |
if (creation_args->enable_read_back_pressure) { |
| 243 |
channel->read_back_pressure_enabled = true; |
| 244 |
|
| 245 |
|
| 246 |
channel->window_update_batch_emit_threshold = g_aws_channel_max_fragment_size * 2; |
| 247 |
} |
| 248 |
|
| 249 |
aws_task_init( |
| 250 |
&channel->cross_thread_tasks.scheduling_task, |
| 251 |
s_schedule_cross_thread_tasks, |
| 252 |
channel, |
| 253 |
"schedule_cross_thread_tasks"); |
| 254 |
|
| 255 |
setup_args->alloc = alloc; |
| 256 |
setup_args->channel = channel; |
| 257 |
setup_args->on_setup_completed = creation_args->on_setup_completed; |
| 258 |
setup_args->user_data = creation_args->setup_user_data; |
| 259 |
|
| 260 |
|
| 261 |
channel->loop = creation_args->event_loop; |
| 262 |
aws_event_loop_group_acquire_from_event_loop(channel->loop); |
| 263 |
|
| 264 |
aws_task_init(&setup_args->task, s_on_channel_setup_complete, setup_args, "on_channel_setup_complete"); |
| 265 |
aws_event_loop_schedule_task_now(creation_args->event_loop, &setup_args->task); |
| 266 |
|
| 267 |
return channel; |
| 268 |
|
| 269 |
on_error: |
| 270 |
|
| 271 |
s_destroy_partially_constructed_channel(channel); |
| 272 |
|
| 273 |
return NULL; |
| 274 |
} |
| 275 |
|
| 276 |
static void s_cleanup_slot(struct aws_channel_slot *slot) { |
| 277 |
if (slot) { |
| 278 |
if (slot->handler) { |
| 279 |
aws_channel_handler_destroy(slot->handler); |
| 280 |
} |
| 281 |
aws_mem_release(slot->alloc, slot); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
void aws_channel_destroy(struct aws_channel *channel) { |
| 286 |
AWS_LOGF_DEBUG(AWS_LS_IO_CHANNEL, "id=%p: destroying channel.", (void *)channel); |
| 287 |
|
| 288 |
aws_channel_release_hold(channel); |
| 289 |
} |
| 290 |
|
| 291 |
static void s_final_channel_deletion_task(struct aws_task *task, void *arg, enum aws_task_status status) { |
| 292 |
(void)task; |
| 293 |
(void)status; |
| 294 |
struct aws_channel *channel = arg; |
| 295 |
|
| 296 |
struct aws_channel_slot *current = channel->first; |
| 297 |
|
| 298 |
if (!current || !current->handler) { |
| 299 |
|
| 300 |
channel->channel_state = AWS_CHANNEL_SHUT_DOWN; |
| 301 |
} |
| 302 |
|
| 303 |
AWS_ASSERT(channel->channel_state == AWS_CHANNEL_SHUT_DOWN); |
| 304 |
|
| 305 |
while (current) { |
| 306 |
struct aws_channel_slot *tmp = current->adj_right; |
| 307 |
s_cleanup_slot(current); |
| 308 |
current = tmp; |
| 309 |
} |
| 310 |
|
| 311 |
aws_array_list_clean_up(&channel->statistic_list); |
| 312 |
|
| 313 |
aws_channel_set_statistics_handler(channel, NULL); |
| 314 |
|
| 315 |
aws_event_loop_group_release_from_event_loop(channel->loop); |
| 316 |
|
| 317 |
aws_mem_release(channel->alloc, channel); |
| 318 |
} |
| 319 |
|
| 320 |
void aws_channel_acquire_hold(struct aws_channel *channel) { |
| 321 |
size_t prev_refcount = aws_atomic_fetch_add(&channel->refcount, 1); |
| 322 |
AWS_ASSERT(prev_refcount != 0); |
| 323 |
(void)prev_refcount; |
| 324 |
} |
| 325 |
|
| 326 |
void aws_channel_release_hold(struct aws_channel *channel) { |
| 327 |
size_t prev_refcount = aws_atomic_fetch_sub(&channel->refcount, 1); |
| 328 |
AWS_ASSERT(prev_refcount != 0); |
| 329 |
|
| 330 |
if (prev_refcount == 1) { |
| 331 |
|
| 332 |
if (aws_channel_thread_is_callers_thread(channel)) { |
| 333 |
s_final_channel_deletion_task(NULL, channel, AWS_TASK_STATUS_RUN_READY); |
| 334 |
} else { |
| 335 |
aws_task_init(&channel->deletion_task, s_final_channel_deletion_task, channel, "final_channel_deletion"); |
| 336 |
aws_event_loop_schedule_task_now(channel->loop, &channel->deletion_task); |
| 337 |
} |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
struct channel_shutdown_task_args { |
| 342 |
struct aws_channel *channel; |
| 343 |
struct aws_allocator *alloc; |
| 344 |
int error_code; |
| 345 |
struct aws_task task; |
| 346 |
}; |
| 347 |
|
| 348 |
static int s_channel_shutdown(struct aws_channel *channel, int error_code, bool shutdown_immediately); |
| 349 |
|
| 350 |
static void s_on_shutdown_completion_task(struct aws_task *task, void *arg, enum aws_task_status status); |
| 351 |
|
| 352 |
static void s_shutdown_task(struct aws_channel_task *task, void *arg, enum aws_task_status status) { |
| 353 |
|
| 354 |
(void)task; |
| 355 |
(void)status; |
| 356 |
struct shutdown_task *shutdown_task = arg; |
| 357 |
struct aws_channel *channel = shutdown_task->channel; |
| 358 |
int error_code = shutdown_task->error_code; |
| 359 |
bool shutdown_immediately = shutdown_task->shutdown_immediately; |
| 360 |
if (channel->channel_state < AWS_CHANNEL_SHUTTING_DOWN) { |
| 361 |
AWS_LOGF_DEBUG(AWS_LS_IO_CHANNEL, "id=%p: beginning shutdown process", (void *)channel); |
| 362 |
|
| 363 |
struct aws_channel_slot *slot = channel->first; |
| 364 |
channel->channel_state = AWS_CHANNEL_SHUTTING_DOWN; |
| 365 |
|
| 366 |
if (slot) { |
| 367 |
AWS_LOGF_TRACE( |
| 368 |
AWS_LS_IO_CHANNEL, |
| 369 |
"id=%p: shutting down slot %p (the first one) in the read direction", |
| 370 |
(void *)channel, |
| 371 |
(void *)slot); |
| 372 |
|
| 373 |
aws_channel_slot_shutdown(slot, AWS_CHANNEL_DIR_READ, error_code, shutdown_immediately); |
| 374 |
return; |
| 375 |
} |
| 376 |
|
| 377 |
channel->channel_state = AWS_CHANNEL_SHUT_DOWN; |
| 378 |
AWS_LOGF_TRACE(AWS_LS_IO_CHANNEL, "id=%p: shutdown completed", (void *)channel); |
| 379 |
|
| 380 |
aws_mutex_lock(&channel->cross_thread_tasks.lock); |
| 381 |
channel->cross_thread_tasks.is_channel_shut_down = true; |
| 382 |
aws_mutex_unlock(&channel->cross_thread_tasks.lock); |
| 383 |
|
| 384 |
if (channel->on_shutdown_completed) { |
| 385 |
channel->shutdown_notify_task.task.fn = s_on_shutdown_completion_task; |
| 386 |
channel->shutdown_notify_task.task.arg = channel; |
| 387 |
channel->shutdown_notify_task.error_code = error_code; |
| 388 |
aws_event_loop_schedule_task_now(channel->loop, &channel->shutdown_notify_task.task); |
| 389 |
} |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
static int s_channel_shutdown(struct aws_channel *channel, int error_code, bool shutdown_immediately) { |
| 394 |
bool need_to_schedule = true; |
| 395 |
aws_mutex_lock(&channel->cross_thread_tasks.lock); |
| 396 |
if (channel->cross_thread_tasks.shutdown_task.task.task_fn) { |
| 397 |
need_to_schedule = false; |
| 398 |
AWS_LOGF_DEBUG( |
| 399 |
AWS_LS_IO_CHANNEL, "id=%p: Channel shutdown is already pending, not scheduling another.", (void *)channel); |
| 400 |
|
| 401 |
} else { |
| 402 |
aws_channel_task_init( |
| 403 |
&channel->cross_thread_tasks.shutdown_task.task, |
| 404 |
s_shutdown_task, |
| 405 |
&channel->cross_thread_tasks.shutdown_task, |
| 406 |
"channel_shutdown"); |
| 407 |
channel->cross_thread_tasks.shutdown_task.shutdown_immediately = shutdown_immediately; |
| 408 |
channel->cross_thread_tasks.shutdown_task.channel = channel; |
| 409 |
channel->cross_thread_tasks.shutdown_task.error_code = error_code; |
| 410 |
} |
| 411 |
|
| 412 |
aws_mutex_unlock(&channel->cross_thread_tasks.lock); |
| 413 |
|
| 414 |
if (need_to_schedule) { |
| 415 |
AWS_LOGF_TRACE(AWS_LS_IO_CHANNEL, "id=%p: channel shutdown task is scheduled", (void *)channel); |
| 416 |
aws_channel_schedule_task_now(channel, &channel->cross_thread_tasks.shutdown_task.task); |
| 417 |
} |
| 418 |
|
| 419 |
return AWS_OP_SUCCESS; |
| 420 |
} |
| 421 |
|
| 422 |
int aws_channel_shutdown(struct aws_channel *channel, int error_code) { |
| 423 |
return s_channel_shutdown(channel, error_code, false); |
| 424 |
} |
| 425 |
|
| 426 |
struct aws_io_message *aws_channel_acquire_message_from_pool( |
| 427 |
struct aws_channel *channel, |
| 428 |
enum aws_io_message_type message_type, |
| 429 |
size_t size_hint) { |
| 430 |
|
| 431 |
struct aws_io_message *message = aws_message_pool_acquire(channel->msg_pool, message_type, size_hint); |
| 432 |
message->owning_channel = channel; |
| 433 |
AWS_LOGF_TRACE( |
| 434 |
AWS_LS_IO_CHANNEL, |
| 435 |
"id=%p: acquired message %p of capacity %zu from pool %p. Requested size was %zu", |
| 436 |
(void *)channel, |
| 437 |
(void *)message, |
| 438 |
message->message_data.capacity, |
| 439 |
(void *)channel->msg_pool, |
| 440 |
size_hint); |
| 441 |
|
| 442 |
return message; |
| 443 |
} |
| 444 |
|
| 445 |
struct aws_channel_slot *aws_channel_slot_new(struct aws_channel *channel) { |
| 446 |
struct aws_channel_slot *new_slot = aws_mem_calloc(channel->alloc, 1, sizeof(struct aws_channel_slot)); |
| 447 |
if (!new_slot) { |
| 448 |
return NULL; |
| 449 |
} |
| 450 |
|
| 451 |
AWS_LOGF_TRACE(AWS_LS_IO_CHANNEL, "id=%p: creating new slot %p.", (void *)channel, (void *)new_slot); |
| 452 |
new_slot->alloc = channel->alloc; |
| 453 |
new_slot->channel = channel; |
| 454 |
|
| 455 |
if (!channel->first) { |
| 456 |
channel->first = new_slot; |
| 457 |
} |
| 458 |
|
| 459 |
return new_slot; |
| 460 |
} |
| 461 |
|
| 462 |
int aws_channel_current_clock_time(struct aws_channel *channel, uint64_t *time_nanos) { |
| 463 |
return aws_event_loop_current_clock_time(channel->loop, time_nanos); |
| 464 |
} |
| 465 |
|
| 466 |
int aws_channel_fetch_local_object( |
| 467 |
struct aws_channel *channel, |
| 468 |
const void *key, |
| 469 |
struct aws_event_loop_local_object *obj) { |
| 470 |
|
| 471 |
return aws_event_loop_fetch_local_object(channel->loop, (void *)key, obj); |
| 472 |
} |
| 473 |
int aws_channel_put_local_object( |
| 474 |
struct aws_channel *channel, |
| 475 |
const void *key, |
| 476 |
const struct aws_event_loop_local_object *obj) { |
| 477 |
|
| 478 |
(void)key; |
| 479 |
return aws_event_loop_put_local_object(channel->loop, (struct aws_event_loop_local_object *)obj); |
| 480 |
} |
| 481 |
|
| 482 |
int aws_channel_remove_local_object( |
| 483 |
struct aws_channel *channel, |
| 484 |
const void *key, |
| 485 |
struct aws_event_loop_local_object *removed_obj) { |
| 486 |
|
| 487 |
return aws_event_loop_remove_local_object(channel->loop, (void *)key, removed_obj); |
| 488 |
} |
| 489 |
|
| 490 |
static void s_channel_task_run(struct aws_task *task, void *arg, enum aws_task_status status) { |
| 491 |
struct aws_channel_task *channel_task = AWS_CONTAINER_OF(task, struct aws_channel_task, wrapper_task); |
| 492 |
struct aws_channel *channel = arg; |
| 493 |
|
| 494 |
|
| 495 |
if (channel->channel_state == AWS_CHANNEL_SHUT_DOWN) { |
| 496 |
status = AWS_TASK_STATUS_CANCELED; |
| 497 |
} |
| 498 |
|
| 499 |
aws_linked_list_remove(&channel_task->node); |
| 500 |
channel_task->task_fn(channel_task, channel_task->arg, status); |
| 501 |
} |
| 502 |
|
| 503 |
static void s_schedule_cross_thread_tasks(struct aws_task *task, void *arg, enum aws_task_status status) { |
| 504 |
(void)task; |
| 505 |
struct aws_channel *channel = arg; |
| 506 |
|
| 507 |
struct aws_linked_list cross_thread_task_list; |
| 508 |
aws_linked_list_init(&cross_thread_task_list); |
| 509 |
|
| 510 |
|
| 511 |
aws_mutex_lock(&channel->cross_thread_tasks.lock); |
| 512 |
aws_linked_list_swap_contents(&channel->cross_thread_tasks.list, &cross_thread_task_list); |
| 513 |
aws_mutex_unlock(&channel->cross_thread_tasks.lock); |
| 514 |
|
| 515 |
|
| 516 |
if (channel->channel_state == AWS_CHANNEL_SHUT_DOWN) { |
| 517 |
status = AWS_TASK_STATUS_CANCELED; |
| 518 |
} |
| 519 |
|
| 520 |
while (!aws_linked_list_empty(&cross_thread_task_list)) { |
| 521 |
struct aws_linked_list_node *node = aws_linked_list_pop_front(&cross_thread_task_list); |
| 522 |
struct aws_channel_task *channel_task = AWS_CONTAINER_OF(node, struct aws_channel_task, node); |
| 523 |
|
| 524 |
if ((channel_task->wrapper_task.timestamp == 0) || (status == AWS_TASK_STATUS_CANCELED)) { |
| 525 |
|
| 526 |
channel_task->task_fn(channel_task, channel_task->arg, status); |
| 527 |
} else { |
| 528 |
|
| 529 |
aws_linked_list_push_back(&channel->channel_thread_tasks.list, &channel_task->node); |
| 530 |
aws_event_loop_schedule_task_future( |
| 531 |
channel->loop, &channel_task->wrapper_task, channel_task->wrapper_task.timestamp); |
| 532 |
} |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
void aws_channel_task_init( |
| 537 |
struct aws_channel_task *channel_task, |
| 538 |
aws_channel_task_fn *task_fn, |
| 539 |
void *arg, |
| 540 |
const char *type_tag) { |
| 541 |
AWS_ZERO_STRUCT(*channel_task); |
| 542 |
channel_task->task_fn = task_fn; |
| 543 |
channel_task->arg = arg; |
| 544 |
channel_task->type_tag = type_tag; |
| 545 |
} |
| 546 |
|
| 547 |
static void s_register_pending_task_in_event_loop( |
| 548 |
struct aws_channel *channel, |
| 549 |
struct aws_channel_task *channel_task, |
| 550 |
uint64_t run_at_nanos) { |
| 551 |
|
| 552 |
AWS_LOGF_TRACE( |
| 553 |
AWS_LS_IO_CHANNEL, |
| 554 |
"id=%p: scheduling task with wrapper task id %p.", |
| 555 |
(void *)channel, |
| 556 |
(void *)&channel_task->wrapper_task); |
| 557 |
|
| 558 |
|
| 559 |
if (channel->channel_state == AWS_CHANNEL_SHUT_DOWN) { |
| 560 |
AWS_LOGF_DEBUG( |
| 561 |
AWS_LS_IO_CHANNEL, |
| 562 |
"id=%p: Running %s channel task immediately as canceled due to shut down channel", |
| 563 |
(void *)channel, |
| 564 |
channel_task->type_tag); |
| 565 |
channel_task->task_fn(channel_task, channel_task->arg, AWS_TASK_STATUS_CANCELED); |
| 566 |
return; |
| 567 |
} |
| 568 |
|
| 569 |
aws_linked_list_push_back(&channel->channel_thread_tasks.list, &channel_task->node); |
| 570 |
if (run_at_nanos == 0) { |
| 571 |
aws_event_loop_schedule_task_now(channel->loop, &channel_task->wrapper_task); |
| 572 |
} else { |
| 573 |
aws_event_loop_schedule_task_future( |
| 574 |
channel->loop, &channel_task->wrapper_task, channel_task->wrapper_task.timestamp); |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
static void s_register_pending_task_cross_thread(struct aws_channel *channel, struct aws_channel_task *channel_task) { |
| 579 |
|
| 580 |
AWS_LOGF_TRACE( |
| 581 |
AWS_LS_IO_CHANNEL, |
| 582 |
"id=%p: scheduling task with wrapper task id %p from " |
| 583 |
"outside the event-loop thread.", |
| 584 |
(void *)channel, |
| 585 |
(void *)&channel_task->wrapper_task); |
| 586 |
|
| 587 |
bool should_cancel_task = false; |
| 588 |
|
| 589 |
|
| 590 |
aws_mutex_lock(&channel->cross_thread_tasks.lock); |
| 591 |
if (channel->cross_thread_tasks.is_channel_shut_down) { |
| 592 |
should_cancel_task = true; |
| 593 |
} else { |
| 594 |
bool list_was_empty = aws_linked_list_empty(&channel->cross_thread_tasks.list); |
| 595 |
aws_linked_list_push_back(&channel->cross_thread_tasks.list, &channel_task->node); |
| 596 |
|
| 597 |
if (list_was_empty) { |
| 598 |
aws_event_loop_schedule_task_now(channel->loop, &channel->cross_thread_tasks.scheduling_task); |
| 599 |
} |
| 600 |
} |
| 601 |
aws_mutex_unlock(&channel->cross_thread_tasks.lock); |
| 602 |
|
| 603 |
|
| 604 |
if (should_cancel_task) { |
| 605 |
channel_task->task_fn(channel_task, channel_task->arg, AWS_TASK_STATUS_CANCELED); |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
static void s_reset_pending_channel_task( |
| 610 |
struct aws_channel *channel, |
| 611 |
struct aws_channel_task *channel_task, |
| 612 |
uint64_t run_at_nanos) { |
| 613 |
|
| 614 |
|
| 615 |
aws_task_init(&channel_task->wrapper_task, s_channel_task_run, channel, channel_task->type_tag); |
| 616 |
channel_task->wrapper_task.timestamp = run_at_nanos; |
| 617 |
aws_linked_list_node_reset(&channel_task->node); |
| 618 |
} |
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
static void s_register_pending_task( |
| 623 |
struct aws_channel *channel, |
| 624 |
struct aws_channel_task *channel_task, |
| 625 |
uint64_t run_at_nanos) { |
| 626 |
|
| 627 |
s_reset_pending_channel_task(channel, channel_task, run_at_nanos); |
| 628 |
|
| 629 |
if (aws_channel_thread_is_callers_thread(channel)) { |
| 630 |
s_register_pending_task_in_event_loop(channel, channel_task, run_at_nanos); |
| 631 |
} else { |
| 632 |
s_register_pending_task_cross_thread(channel, channel_task); |
| 633 |
} |
| 634 |
} |
| 635 |
|
| 636 |
void aws_channel_schedule_task_now(struct aws_channel *channel, struct aws_channel_task *task) { |
| 637 |
s_register_pending_task(channel, task, 0); |
| 638 |
} |
| 639 |
|
| 640 |
void aws_channel_schedule_task_now_serialized(struct aws_channel *channel, struct aws_channel_task *task) { |
| 641 |
|
| 642 |
s_reset_pending_channel_task(channel, task, 0); |
| 643 |
s_register_pending_task_cross_thread(channel, task); |
| 644 |
} |
| 645 |
|
| 646 |
void aws_channel_schedule_task_future( |
| 647 |
struct aws_channel *channel, |
| 648 |
struct aws_channel_task *task, |
| 649 |
uint64_t run_at_nanos) { |
| 650 |
|
| 651 |
s_register_pending_task(channel, task, run_at_nanos); |
| 652 |
} |
| 653 |
|
| 654 |
bool aws_channel_thread_is_callers_thread(struct aws_channel *channel) { |
| 655 |
return aws_event_loop_thread_is_callers_thread(channel->loop); |
| 656 |
} |
| 657 |
|
| 658 |
static void s_update_channel_slot_message_overheads(struct aws_channel *channel) { |
| 659 |
size_t overhead = 0; |
| 660 |
struct aws_channel_slot *slot_iter = channel->first; |
| 661 |
while (slot_iter) { |
| 662 |
slot_iter->upstream_message_overhead = overhead; |
| 663 |
|
| 664 |
if (slot_iter->handler) { |
| 665 |
overhead += slot_iter->handler->vtable->message_overhead(slot_iter->handler); |
| 666 |
} |
| 667 |
slot_iter = slot_iter->adj_right; |
| 668 |
} |
| 669 |
} |
| 670 |
|
| 671 |
int aws_channel_slot_set_handler(struct aws_channel_slot *slot, struct aws_channel_handler *handler) { |
| 672 |
slot->handler = handler; |
| 673 |
slot->handler->slot = slot; |
| 674 |
s_update_channel_slot_message_overheads(slot->channel); |
| 675 |
|
| 676 |
return aws_channel_slot_increment_read_window(slot, slot->handler->vtable->initial_window_size(handler)); |
| 677 |
} |
| 678 |
|
| 679 |
int aws_channel_slot_remove(struct aws_channel_slot *slot) { |
| 680 |
if (slot->adj_right) { |
| 681 |
slot->adj_right->adj_left = slot->adj_left; |
| 682 |
|
| 683 |
if (slot == slot->channel->first) { |
| 684 |
slot->channel->first = slot->adj_right; |
| 685 |
} |
| 686 |
} |
| 687 |
|
| 688 |
if (slot->adj_left) { |
| 689 |
slot->adj_left->adj_right = slot->adj_right; |
| 690 |
} |
| 691 |
|
| 692 |
if (slot == slot->channel->first) { |
| 693 |
slot->channel->first = NULL; |
| 694 |
} |
| 695 |
|
| 696 |
s_update_channel_slot_message_overheads(slot->channel); |
| 697 |
s_cleanup_slot(slot); |
| 698 |
return AWS_OP_SUCCESS; |
| 699 |
} |
| 700 |
|
| 701 |
int aws_channel_slot_replace(struct aws_channel_slot *remove, struct aws_channel_slot *new_slot) { |
| 702 |
new_slot->adj_left = remove->adj_left; |
| 703 |
|
| 704 |
if (remove->adj_left) { |
| 705 |
remove->adj_left->adj_right = new_slot; |
| 706 |
} |
| 707 |
|
| 708 |
new_slot->adj_right = remove->adj_right; |
| 709 |
|
| 710 |
if (remove->adj_right) { |
| 711 |
remove->adj_right->adj_left = new_slot; |
| 712 |
} |
| 713 |
|
| 714 |
if (remove == remove->channel->first) { |
| 715 |
remove->channel->first = new_slot; |
| 716 |
} |
| 717 |
|
| 718 |
s_update_channel_slot_message_overheads(remove->channel); |
| 719 |
s_cleanup_slot(remove); |
| 720 |
return AWS_OP_SUCCESS; |
| 721 |
} |
| 722 |
|
| 723 |
int aws_channel_slot_insert_right(struct aws_channel_slot *slot, struct aws_channel_slot *to_add) { |
| 724 |
to_add->adj_right = slot->adj_right; |
| 725 |
|
| 726 |
if (slot->adj_right) { |
| 727 |
slot->adj_right->adj_left = to_add; |
| 728 |
} |
| 729 |
|
| 730 |
slot->adj_right = to_add; |
| 731 |
to_add->adj_left = slot; |
| 732 |
|
| 733 |
return AWS_OP_SUCCESS; |
| 734 |
} |
| 735 |
|
| 736 |
int aws_channel_slot_insert_end(struct aws_channel *channel, struct aws_channel_slot *to_add) { |
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
if (AWS_LIKELY(channel->first && channel->first != to_add)) { |
| 741 |
struct aws_channel_slot *cur = channel->first; |
| 742 |
while (cur->adj_right) { |
| 743 |
cur = cur->adj_right; |
| 744 |
} |
| 745 |
|
| 746 |
return aws_channel_slot_insert_right(cur, to_add); |
| 747 |
} |
| 748 |
|
| 749 |
AWS_ASSERT(0); |
| 750 |
return aws_raise_error(AWS_ERROR_INVALID_STATE); |
| 751 |
} |
| 752 |
|
| 753 |
int aws_channel_slot_insert_left(struct aws_channel_slot *slot, struct aws_channel_slot *to_add) { |
| 754 |
to_add->adj_left = slot->adj_left; |
| 755 |
|
| 756 |
if (slot->adj_left) { |
| 757 |
slot->adj_left->adj_right = to_add; |
| 758 |
} |
| 759 |
|
| 760 |
slot->adj_left = to_add; |
| 761 |
to_add->adj_right = slot; |
| 762 |
|
| 763 |
if (slot == slot->channel->first) { |
| 764 |
slot->channel->first = to_add; |
| 765 |
} |
| 766 |
|
| 767 |
return AWS_OP_SUCCESS; |
| 768 |
} |
| 769 |
|
| 770 |
int aws_channel_slot_send_message( |
| 771 |
struct aws_channel_slot *slot, |
| 772 |
struct aws_io_message *message, |
| 773 |
enum aws_channel_direction dir) { |
| 774 |
|
| 775 |
if (dir == AWS_CHANNEL_DIR_READ) { |
| 776 |
AWS_ASSERT(slot->adj_right); |
| 777 |
AWS_ASSERT(slot->adj_right->handler); |
| 778 |
|
| 779 |
if (!slot->channel->read_back_pressure_enabled || slot->adj_right->window_size >= message->message_data.len) { |
| 780 |
AWS_LOGF_TRACE( |
| 781 |
AWS_LS_IO_CHANNEL, |
| 782 |
"id=%p: sending read message of size %zu, " |
| 783 |
"from slot %p to slot %p with handler %p.", |
| 784 |
(void *)slot->channel, |
| 785 |
message->message_data.len, |
| 786 |
(void *)slot, |
| 787 |
(void *)slot->adj_right, |
| 788 |
(void *)slot->adj_right->handler); |
| 789 |
slot->adj_right->window_size -= message->message_data.len; |
| 790 |
return aws_channel_handler_process_read_message(slot->adj_right->handler, slot->adj_right, message); |
| 791 |
} |
| 792 |
AWS_LOGF_ERROR( |
| 793 |
AWS_LS_IO_CHANNEL, |
| 794 |
"id=%p: sending message of size %zu, " |
| 795 |
"from slot %p to slot %p with handler %p, but this would exceed the channel's " |
| 796 |
"read window, this is always a programming error.", |
| 797 |
(void *)slot->channel, |
| 798 |
message->message_data.len, |
| 799 |
(void *)slot, |
| 800 |
(void *)slot->adj_right, |
| 801 |
(void *)slot->adj_right->handler); |
| 802 |
return aws_raise_error(AWS_IO_CHANNEL_READ_WOULD_EXCEED_WINDOW); |
| 803 |
} |
| 804 |
|
| 805 |
AWS_ASSERT(slot->adj_left); |
| 806 |
AWS_ASSERT(slot->adj_left->handler); |
| 807 |
AWS_LOGF_TRACE( |
| 808 |
AWS_LS_IO_CHANNEL, |
| 809 |
"id=%p: sending write message of size %zu, " |
| 810 |
"from slot %p to slot %p with handler %p.", |
| 811 |
(void *)slot->channel, |
| 812 |
message->message_data.len, |
| 813 |
(void *)slot, |
| 814 |
(void *)slot->adj_left, |
| 815 |
(void *)slot->adj_left->handler); |
| 816 |
return aws_channel_handler_process_write_message(slot->adj_left->handler, slot->adj_left, message); |
| 817 |
} |
| 818 |
|
| 819 |
struct aws_io_message *aws_channel_slot_acquire_max_message_for_write(struct aws_channel_slot *slot) { |
| 820 |
AWS_PRECONDITION(slot); |
| 821 |
AWS_PRECONDITION(slot->channel); |
| 822 |
AWS_PRECONDITION(aws_channel_thread_is_callers_thread(slot->channel)); |
| 823 |
|
| 824 |
const size_t overhead = aws_channel_slot_upstream_message_overhead(slot); |
| 825 |
AWS_FATAL_ASSERT( |
| 826 |
overhead < g_aws_channel_max_fragment_size && "Upstream overhead cannot exceed channel's max message size"); |
| 827 |
|
| 828 |
const size_t size_hint = g_aws_channel_max_fragment_size - overhead; |
| 829 |
return aws_channel_acquire_message_from_pool(slot->channel, AWS_IO_MESSAGE_APPLICATION_DATA, size_hint); |
| 830 |
} |
| 831 |
|
| 832 |
static void s_window_update_task(struct aws_channel_task *channel_task, void *arg, enum aws_task_status status) { |
| 833 |
(void)channel_task; |
| 834 |
struct aws_channel *channel = arg; |
| 835 |
|
| 836 |
channel->window_update_scheduled = false; |
| 837 |
|
| 838 |
if (status == AWS_TASK_STATUS_RUN_READY && channel->channel_state < AWS_CHANNEL_SHUT_DOWN) { |
| 839 |
|
| 840 |
struct aws_channel_slot *slot = channel->first; |
| 841 |
while (slot->adj_right) { |
| 842 |
slot = slot->adj_right; |
| 843 |
} |
| 844 |
|
| 845 |
while (slot->adj_left) { |
| 846 |
struct aws_channel_slot *upstream_slot = slot->adj_left; |
| 847 |
if (upstream_slot->handler) { |
| 848 |
slot->window_size = aws_add_size_saturating(slot->window_size, slot->current_window_update_batch_size); |
| 849 |
size_t update_size = slot->current_window_update_batch_size; |
| 850 |
slot->current_window_update_batch_size = 0; |
| 851 |
if (aws_channel_handler_increment_read_window(upstream_slot->handler, upstream_slot, update_size)) { |
| 852 |
AWS_LOGF_ERROR( |
| 853 |
AWS_LS_IO_CHANNEL, |
| 854 |
"channel %p: channel update task failed with status %d", |
| 855 |
(void *)slot->channel, |
| 856 |
aws_last_error()); |
| 857 |
aws_channel_shutdown(channel, aws_last_error()); |
| 858 |
return; |
| 859 |
} |
| 860 |
} |
| 861 |
slot = slot->adj_left; |
| 862 |
} |
| 863 |
} |
| 864 |
} |
| 865 |
|
| 866 |
int aws_channel_slot_increment_read_window(struct aws_channel_slot *slot, size_t window) { |
| 867 |
|
| 868 |
if (slot->channel->read_back_pressure_enabled && slot->channel->channel_state < AWS_CHANNEL_SHUT_DOWN) { |
| 869 |
slot->current_window_update_batch_size = |
| 870 |
aws_add_size_saturating(slot->current_window_update_batch_size, window); |
| 871 |
|
| 872 |
if (!slot->channel->window_update_scheduled && |
| 873 |
slot->window_size <= slot->channel->window_update_batch_emit_threshold) { |
| 874 |
slot->channel->window_update_scheduled = true; |
| 875 |
aws_channel_task_init( |
| 876 |
&slot->channel->window_update_task, s_window_update_task, slot->channel, "window update task"); |
| 877 |
aws_channel_schedule_task_now(slot->channel, &slot->channel->window_update_task); |
| 878 |
} |
| 879 |
} |
| 880 |
|
| 881 |
return AWS_OP_SUCCESS; |
| 882 |
} |
| 883 |
|
| 884 |
int aws_channel_slot_shutdown( |
| 885 |
struct aws_channel_slot *slot, |
| 886 |
enum aws_channel_direction dir, |
| 887 |
int err_code, |
| 888 |
bool free_scarce_resources_immediately) { |
| 889 |
AWS_ASSERT(slot->handler); |
| 890 |
AWS_LOGF_TRACE( |
| 891 |
AWS_LS_IO_CHANNEL, |
| 892 |
"id=%p: shutting down slot %p, with handler %p " |
| 893 |
"in %s direction with error code %d : %s", |
| 894 |
(void *)slot->channel, |
| 895 |
(void *)slot, |
| 896 |
(void *)slot->handler, |
| 897 |
(dir == AWS_CHANNEL_DIR_READ) ? "read" : "write", |
| 898 |
err_code, |
| 899 |
aws_error_name(err_code)); |
| 900 |
return aws_channel_handler_shutdown(slot->handler, slot, dir, err_code, free_scarce_resources_immediately); |
| 901 |
} |
| 902 |
|
| 903 |
static void s_on_shutdown_completion_task(struct aws_task *task, void *arg, enum aws_task_status status) { |
| 904 |
(void)status; |
| 905 |
|
| 906 |
struct aws_shutdown_notification_task *shutdown_notify = (struct aws_shutdown_notification_task *)task; |
| 907 |
struct aws_channel *channel = arg; |
| 908 |
AWS_ASSERT(channel->channel_state == AWS_CHANNEL_SHUT_DOWN); |
| 909 |
|
| 910 |
|
| 911 |
while (!aws_linked_list_empty(&channel->channel_thread_tasks.list)) { |
| 912 |
struct aws_linked_list_node *node = aws_linked_list_front(&channel->channel_thread_tasks.list); |
| 913 |
struct aws_channel_task *channel_task = AWS_CONTAINER_OF(node, struct aws_channel_task, node); |
| 914 |
AWS_LOGF_DEBUG( |
| 915 |
AWS_LS_IO_CHANNEL, |
| 916 |
"id=%p: during shutdown, canceling task %p", |
| 917 |
(void *)channel, |
| 918 |
(void *)&channel_task->wrapper_task); |
| 919 |
|
| 920 |
aws_event_loop_cancel_task(channel->loop, &channel_task->wrapper_task); |
| 921 |
} |
| 922 |
|
| 923 |
|
| 924 |
aws_mutex_lock(&channel->cross_thread_tasks.lock); |
| 925 |
bool cancel_cross_thread_tasks = !aws_linked_list_empty(&channel->cross_thread_tasks.list); |
| 926 |
aws_mutex_unlock(&channel->cross_thread_tasks.lock); |
| 927 |
|
| 928 |
if (cancel_cross_thread_tasks) { |
| 929 |
aws_event_loop_cancel_task(channel->loop, &channel->cross_thread_tasks.scheduling_task); |
| 930 |
} |
| 931 |
|
| 932 |
AWS_ASSERT(aws_linked_list_empty(&channel->channel_thread_tasks.list)); |
| 933 |
AWS_ASSERT(aws_linked_list_empty(&channel->cross_thread_tasks.list)); |
| 934 |
|
| 935 |
channel->on_shutdown_completed(channel, shutdown_notify->error_code, channel->shutdown_user_data); |
| 936 |
} |
| 937 |
|
| 938 |
static void s_run_shutdown_write_direction(struct aws_task *task, void *arg, enum aws_task_status status) { |
| 939 |
(void)arg; |
| 940 |
(void)status; |
| 941 |
|
| 942 |
struct aws_shutdown_notification_task *shutdown_notify = (struct aws_shutdown_notification_task *)task; |
| 943 |
task->fn = NULL; |
| 944 |
task->arg = NULL; |
| 945 |
struct aws_channel_slot *slot = shutdown_notify->slot; |
| 946 |
aws_channel_handler_shutdown( |
| 947 |
slot->handler, slot, AWS_CHANNEL_DIR_WRITE, shutdown_notify->error_code, shutdown_notify->shutdown_immediately); |
| 948 |
} |
| 949 |
|
| 950 |
int aws_channel_slot_on_handler_shutdown_complete( |
| 951 |
struct aws_channel_slot *slot, |
| 952 |
enum aws_channel_direction dir, |
| 953 |
int err_code, |
| 954 |
bool free_scarce_resources_immediately) { |
| 955 |
|
| 956 |
AWS_LOGF_DEBUG( |
| 957 |
AWS_LS_IO_CHANNEL, |
| 958 |
"id=%p: handler %p shutdown in %s dir completed.", |
| 959 |
(void *)slot->channel, |
| 960 |
(void *)slot->handler, |
| 961 |
(dir == AWS_CHANNEL_DIR_READ) ? "read" : "write"); |
| 962 |
|
| 963 |
if (slot->channel->channel_state == AWS_CHANNEL_SHUT_DOWN) { |
| 964 |
return AWS_OP_SUCCESS; |
| 965 |
} |
| 966 |
|
| 967 |
if (dir == AWS_CHANNEL_DIR_READ) { |
| 968 |
if (slot->adj_right && slot->adj_right->handler) { |
| 969 |
return aws_channel_handler_shutdown( |
| 970 |
slot->adj_right->handler, slot->adj_right, dir, err_code, free_scarce_resources_immediately); |
| 971 |
} |
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
slot->channel->shutdown_notify_task.slot = slot; |
| 976 |
slot->channel->shutdown_notify_task.shutdown_immediately = free_scarce_resources_immediately; |
| 977 |
slot->channel->shutdown_notify_task.error_code = err_code; |
| 978 |
slot->channel->shutdown_notify_task.task.fn = s_run_shutdown_write_direction; |
| 979 |
slot->channel->shutdown_notify_task.task.arg = NULL; |
| 980 |
|
| 981 |
aws_event_loop_schedule_task_now(slot->channel->loop, &slot->channel->shutdown_notify_task.task); |
| 982 |
return AWS_OP_SUCCESS; |
| 983 |
} |
| 984 |
|
| 985 |
if (slot->adj_left && slot->adj_left->handler) { |
| 986 |
return aws_channel_handler_shutdown( |
| 987 |
slot->adj_left->handler, slot->adj_left, dir, err_code, free_scarce_resources_immediately); |
| 988 |
} |
| 989 |
|
| 990 |
if (slot->channel->first == slot) { |
| 991 |
slot->channel->channel_state = AWS_CHANNEL_SHUT_DOWN; |
| 992 |
aws_mutex_lock(&slot->channel->cross_thread_tasks.lock); |
| 993 |
slot->channel->cross_thread_tasks.is_channel_shut_down = true; |
| 994 |
aws_mutex_unlock(&slot->channel->cross_thread_tasks.lock); |
| 995 |
|
| 996 |
if (slot->channel->on_shutdown_completed) { |
| 997 |
slot->channel->shutdown_notify_task.task.fn = s_on_shutdown_completion_task; |
| 998 |
slot->channel->shutdown_notify_task.task.arg = slot->channel; |
| 999 |
slot->channel->shutdown_notify_task.error_code = err_code; |
| 1000 |
aws_event_loop_schedule_task_now(slot->channel->loop, &slot->channel->shutdown_notify_task.task); |
| 1001 |
} |
| 1002 |
} |
| 1003 |
|
| 1004 |
return AWS_OP_SUCCESS; |
| 1005 |
} |
| 1006 |
|
| 1007 |
size_t aws_channel_slot_downstream_read_window(struct aws_channel_slot *slot) { |
| 1008 |
AWS_ASSERT(slot->adj_right); |
| 1009 |
return slot->channel->read_back_pressure_enabled ? slot->adj_right->window_size : SIZE_MAX; |
| 1010 |
} |
| 1011 |
|
| 1012 |
size_t aws_channel_slot_upstream_message_overhead(struct aws_channel_slot *slot) { |
| 1013 |
return slot->upstream_message_overhead; |
| 1014 |
} |
| 1015 |
|
| 1016 |
void aws_channel_handler_destroy(struct aws_channel_handler *handler) { |
| 1017 |
AWS_ASSERT(handler->vtable && handler->vtable->destroy); |
| 1018 |
handler->vtable->destroy(handler); |
| 1019 |
} |
| 1020 |
|
| 1021 |
int aws_channel_handler_process_read_message( |
| 1022 |
struct aws_channel_handler *handler, |
| 1023 |
struct aws_channel_slot *slot, |
| 1024 |
struct aws_io_message *message) { |
| 1025 |
|
| 1026 |
AWS_ASSERT(handler->vtable && handler->vtable->process_read_message); |
| 1027 |
return handler->vtable->process_read_message(handler, slot, message); |
| 1028 |
} |
| 1029 |
|
| 1030 |
int aws_channel_handler_process_write_message( |
| 1031 |
struct aws_channel_handler *handler, |
| 1032 |
struct aws_channel_slot *slot, |
| 1033 |
struct aws_io_message *message) { |
| 1034 |
|
| 1035 |
AWS_ASSERT(handler->vtable && handler->vtable->process_write_message); |
| 1036 |
return handler->vtable->process_write_message(handler, slot, message); |
| 1037 |
} |
| 1038 |
|
| 1039 |
int aws_channel_handler_increment_read_window( |
| 1040 |
struct aws_channel_handler *handler, |
| 1041 |
struct aws_channel_slot *slot, |
| 1042 |
size_t size) { |
| 1043 |
|
| 1044 |
AWS_ASSERT(handler->vtable && handler->vtable->increment_read_window); |
| 1045 |
|
| 1046 |
return handler->vtable->increment_read_window(handler, slot, size); |
| 1047 |
} |
| 1048 |
|
| 1049 |
int aws_channel_handler_shutdown( |
| 1050 |
struct aws_channel_handler *handler, |
| 1051 |
struct aws_channel_slot *slot, |
| 1052 |
enum aws_channel_direction dir, |
| 1053 |
int error_code, |
| 1054 |
bool free_scarce_resources_immediately) { |
| 1055 |
|
| 1056 |
AWS_ASSERT(handler->vtable && handler->vtable->shutdown); |
| 1057 |
return handler->vtable->shutdown(handler, slot, dir, error_code, free_scarce_resources_immediately); |
| 1058 |
} |
| 1059 |
|
| 1060 |
size_t aws_channel_handler_initial_window_size(struct aws_channel_handler *handler) { |
| 1061 |
AWS_ASSERT(handler->vtable && handler->vtable->initial_window_size); |
| 1062 |
return handler->vtable->initial_window_size(handler); |
| 1063 |
} |
| 1064 |
|
| 1065 |
struct aws_channel_slot *aws_channel_get_first_slot(struct aws_channel *channel) { |
| 1066 |
return channel->first; |
| 1067 |
} |
| 1068 |
|
| 1069 |
static void s_reset_statistics(struct aws_channel *channel) { |
| 1070 |
AWS_FATAL_ASSERT(aws_channel_thread_is_callers_thread(channel)); |
| 1071 |
|
| 1072 |
struct aws_channel_slot *current_slot = channel->first; |
| 1073 |
while (current_slot) { |
| 1074 |
struct aws_channel_handler *handler = current_slot->handler; |
| 1075 |
if (handler != NULL && handler->vtable->reset_statistics != NULL) { |
| 1076 |
handler->vtable->reset_statistics(handler); |
| 1077 |
} |
| 1078 |
current_slot = current_slot->adj_right; |
| 1079 |
} |
| 1080 |
} |
| 1081 |
|
| 1082 |
static void s_channel_gather_statistics_task(struct aws_task *task, void *arg, enum aws_task_status status) { |
| 1083 |
if (status != AWS_TASK_STATUS_RUN_READY) { |
| 1084 |
return; |
| 1085 |
} |
| 1086 |
|
| 1087 |
struct aws_channel *channel = arg; |
| 1088 |
if (channel->statistics_handler == NULL) { |
| 1089 |
return; |
| 1090 |
} |
| 1091 |
|
| 1092 |
if (channel->channel_state == AWS_CHANNEL_SHUTTING_DOWN || channel->channel_state == AWS_CHANNEL_SHUT_DOWN) { |
| 1093 |
return; |
| 1094 |
} |
| 1095 |
|
| 1096 |
uint64_t now_ns = 0; |
| 1097 |
if (aws_channel_current_clock_time(channel, &now_ns)) { |
| 1098 |
return; |
| 1099 |
} |
| 1100 |
|
| 1101 |
uint64_t now_ms = aws_timestamp_convert(now_ns, AWS_TIMESTAMP_NANOS, AWS_TIMESTAMP_MILLIS, NULL); |
| 1102 |
|
| 1103 |
struct aws_array_list *statistics_list = &channel->statistic_list; |
| 1104 |
aws_array_list_clear(statistics_list); |
| 1105 |
|
| 1106 |
struct aws_channel_slot *current_slot = channel->first; |
| 1107 |
while (current_slot) { |
| 1108 |
struct aws_channel_handler *handler = current_slot->handler; |
| 1109 |
if (handler != NULL && handler->vtable->gather_statistics != NULL) { |
| 1110 |
handler->vtable->gather_statistics(handler, statistics_list); |
| 1111 |
} |
| 1112 |
current_slot = current_slot->adj_right; |
| 1113 |
} |
| 1114 |
|
| 1115 |
struct aws_crt_statistics_sample_interval sample_interval = { |
| 1116 |
.begin_time_ms = channel->statistics_interval_start_time_ms, .end_time_ms = now_ms}; |
| 1117 |
|
| 1118 |
aws_crt_statistics_handler_process_statistics( |
| 1119 |
channel->statistics_handler, &sample_interval, statistics_list, channel); |
| 1120 |
|
| 1121 |
s_reset_statistics(channel); |
| 1122 |
|
| 1123 |
uint64_t reschedule_interval_ns = aws_timestamp_convert( |
| 1124 |
aws_crt_statistics_handler_get_report_interval_ms(channel->statistics_handler), |
| 1125 |
AWS_TIMESTAMP_MILLIS, |
| 1126 |
AWS_TIMESTAMP_NANOS, |
| 1127 |
NULL); |
| 1128 |
|
| 1129 |
aws_event_loop_schedule_task_future(channel->loop, task, now_ns + reschedule_interval_ns); |
| 1130 |
|
| 1131 |
channel->statistics_interval_start_time_ms = now_ms; |
| 1132 |
} |
| 1133 |
|
| 1134 |
int aws_channel_set_statistics_handler(struct aws_channel *channel, struct aws_crt_statistics_handler *handler) { |
| 1135 |
AWS_FATAL_ASSERT(aws_channel_thread_is_callers_thread(channel)); |
| 1136 |
|
| 1137 |
if (channel->statistics_handler) { |
| 1138 |
aws_crt_statistics_handler_destroy(channel->statistics_handler); |
| 1139 |
aws_event_loop_cancel_task(channel->loop, &channel->statistics_task); |
| 1140 |
channel->statistics_handler = NULL; |
| 1141 |
} |
| 1142 |
|
| 1143 |
if (handler != NULL) { |
| 1144 |
aws_task_init(&channel->statistics_task, s_channel_gather_statistics_task, channel, "gather_statistics"); |
| 1145 |
|
| 1146 |
uint64_t now_ns = 0; |
| 1147 |
if (aws_channel_current_clock_time(channel, &now_ns)) { |
| 1148 |
return AWS_OP_ERR; |
| 1149 |
} |
| 1150 |
|
| 1151 |
uint64_t report_time_ns = now_ns + aws_timestamp_convert( |
| 1152 |
aws_crt_statistics_handler_get_report_interval_ms(handler), |
| 1153 |
AWS_TIMESTAMP_MILLIS, |
| 1154 |
AWS_TIMESTAMP_NANOS, |
| 1155 |
NULL); |
| 1156 |
|
| 1157 |
channel->statistics_interval_start_time_ms = |
| 1158 |
aws_timestamp_convert(now_ns, AWS_TIMESTAMP_NANOS, AWS_TIMESTAMP_MILLIS, NULL); |
| 1159 |
s_reset_statistics(channel); |
| 1160 |
|
| 1161 |
aws_event_loop_schedule_task_future(channel->loop, &channel->statistics_task, report_time_ns); |
| 1162 |
} |
| 1163 |
|
| 1164 |
channel->statistics_handler = handler; |
| 1165 |
|
| 1166 |
return AWS_OP_SUCCESS; |
| 1167 |
} |
| 1168 |
|
| 1169 |
struct aws_event_loop *aws_channel_get_event_loop(struct aws_channel *channel) { |
| 1170 |
return channel->loop; |
| 1171 |
} |
| 1172 |
|
| 1173 |
int aws_channel_trigger_read(struct aws_channel *channel) { |
| 1174 |
if (channel == NULL) { |
| 1175 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 1176 |
} |
| 1177 |
|
| 1178 |
if (!aws_channel_thread_is_callers_thread(channel)) { |
| 1179 |
return aws_raise_error(AWS_ERROR_INVALID_STATE); |
| 1180 |
} |
| 1181 |
|
| 1182 |
struct aws_channel_slot *slot = channel->first; |
| 1183 |
if (slot == NULL) { |
| 1184 |
return aws_raise_error(AWS_ERROR_INVALID_STATE); |
| 1185 |
} |
| 1186 |
|
| 1187 |
struct aws_channel_handler *handler = slot->handler; |
| 1188 |
if (handler == NULL) { |
| 1189 |
return aws_raise_error(AWS_ERROR_INVALID_STATE); |
| 1190 |
} |
| 1191 |
|
| 1192 |
if (handler->vtable->trigger_read != NULL) { |
| 1193 |
handler->vtable->trigger_read(handler); |
| 1194 |
} |
| 1195 |
|
| 1196 |
return AWS_OP_SUCCESS; |
| 1197 |
} |