Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

async channel shutdown #172

Merged
merged 4 commits into from
Aug 6, 2019
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 37 additions & 39 deletions source/channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,21 +303,17 @@ struct channel_shutdown_task_args {

static int s_channel_shutdown(struct aws_channel *channel, int error_code, bool shutdown_immediately);

static void s_on_shutdown_completion_task(struct aws_task *task, void *arg, enum aws_task_status status);

static void s_shutdown_task(struct aws_channel_task *task, void *arg, enum aws_task_status status) {

(void)task;

struct shutdown_task *shutdown_task = arg;

struct aws_channel *channel = shutdown_task->channel;
int error_code = shutdown_task->error_code;
bool shutdown_immediately = shutdown_task->shutdown_immediately;
if (status == AWS_TASK_STATUS_RUN_READY) {
TingDaoK marked this conversation as resolved.
Show resolved Hide resolved
s_channel_shutdown(shutdown_task->channel, shutdown_task->error_code, shutdown_task->shutdown_immediately);
}
}

static void s_on_shutdown_completion_task(struct aws_task *task, void *arg, enum aws_task_status status);

static int s_channel_shutdown(struct aws_channel *channel, int error_code, bool shutdown_immediately) {
if (aws_channel_thread_is_callers_thread(channel)) {
if (channel->channel_state < AWS_CHANNEL_SHUTTING_DOWN) {
AWS_LOGF_DEBUG(AWS_LS_IO_CHANNEL, "id=%p: beginning shutdown process", (void *)channel);

Expand All @@ -331,7 +327,8 @@ static int s_channel_shutdown(struct aws_channel *channel, int error_code, bool
(void *)channel,
(void *)slot);

return aws_channel_slot_shutdown(slot, AWS_CHANNEL_DIR_READ, error_code, shutdown_immediately);
aws_channel_slot_shutdown(slot, AWS_CHANNEL_DIR_READ, error_code, shutdown_immediately);
return;
}

channel->channel_state = AWS_CHANNEL_SHUT_DOWN;
Expand All @@ -348,38 +345,39 @@ static int s_channel_shutdown(struct aws_channel *channel, int error_code, bool
aws_event_loop_schedule_task_now(channel->loop, &channel->shutdown_notify_task.task);
}
}
} else {
AWS_LOGF_TRACE(
AWS_LS_IO_CHANNEL,
"id=%p: channel shutdown called from outside the "
"event-loop thread, scheduling task.",
(void *)channel);

bool need_to_schedule = true;
aws_mutex_lock(&channel->cross_thread_tasks.lock);
if (channel->cross_thread_tasks.shutdown_task.task.task_fn) {
need_to_schedule = false;
AWS_LOGF_DEBUG(
AWS_LS_IO_CHANNEL,
"id=%p: Channel shutdown is already pending, not scheduling another.",
(void *)channel);
}
}

} else {
aws_channel_task_init(
&channel->cross_thread_tasks.shutdown_task.task,
s_shutdown_task,
&channel->cross_thread_tasks.shutdown_task,
"channel_cross_thread_shutdown");
channel->cross_thread_tasks.shutdown_task.shutdown_immediately = shutdown_immediately;
channel->cross_thread_tasks.shutdown_task.channel = channel;
channel->cross_thread_tasks.shutdown_task.error_code = error_code;
}
static int s_channel_shutdown(struct aws_channel *channel, int error_code, bool shutdown_immediately) {

aws_mutex_unlock(&channel->cross_thread_tasks.lock);
AWS_LOGF_TRACE(
AWS_LS_IO_CHANNEL,
"id=%p: channel shutdown called from outside the "
"event-loop thread, scheduling task.",
TingDaoK marked this conversation as resolved.
Show resolved Hide resolved
(void *)channel);

if (need_to_schedule) {
aws_channel_schedule_task_now(channel, &channel->cross_thread_tasks.shutdown_task.task);
}
bool need_to_schedule = true;
aws_mutex_lock(&channel->cross_thread_tasks.lock);
if (channel->cross_thread_tasks.shutdown_task.task.task_fn) {
need_to_schedule = false;
AWS_LOGF_DEBUG(
AWS_LS_IO_CHANNEL, "id=%p: Channel shutdown is already pending, not scheduling another.", (void *)channel);

} else {
aws_channel_task_init(
&channel->cross_thread_tasks.shutdown_task.task,
s_shutdown_task,
&channel->cross_thread_tasks.shutdown_task,
"channel_cross_thread_shutdown");
TingDaoK marked this conversation as resolved.
Show resolved Hide resolved
channel->cross_thread_tasks.shutdown_task.shutdown_immediately = shutdown_immediately;
channel->cross_thread_tasks.shutdown_task.channel = channel;
channel->cross_thread_tasks.shutdown_task.error_code = error_code;
}

aws_mutex_unlock(&channel->cross_thread_tasks.lock);

if (need_to_schedule) {
aws_channel_schedule_task_now(channel, &channel->cross_thread_tasks.shutdown_task.task);
}

return AWS_OP_SUCCESS;
Expand Down