Skip to content

Commit

Permalink
fix destory function
Browse files Browse the repository at this point in the history
  • Loading branch information
xiazhvera committed Dec 10, 2024
1 parent 793579a commit c60a056
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
14 changes: 7 additions & 7 deletions source/darwin/dispatch_queue_event_loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,6 @@ struct aws_event_loop *aws_event_loop_new_with_dispatch_queue(

static void s_dispatch_queue_destroy_task(void *context) {
struct dispatch_loop *dispatch_loop = context;

aws_task_scheduler_clean_up(&dispatch_loop->scheduler);
s_lock_dispatch_loop_context(dispatch_loop->context);
dispatch_loop->context->suspended = true;

Expand All @@ -304,16 +302,17 @@ static void s_dispatch_queue_destroy_task(void *context) {
&dispatch_loop->synced_cross_thread_data.cross_thread_tasks, &local_cross_thread_tasks);
s_unlock_cross_thread_data(dispatch_loop);

aws_task_scheduler_clean_up(&dispatch_loop->scheduler); /* Tasks in scheduler get cancelled*/
while (!aws_linked_list_empty(&local_cross_thread_tasks)) {
struct aws_linked_list_node *node = aws_linked_list_pop_front(&local_cross_thread_tasks);
struct aws_task *task = AWS_CONTAINER_OF(node, struct aws_task, node);
task->fn(task, task->arg, AWS_TASK_STATUS_CANCELED);
}

aws_mutex_lock(&dispatch_loop->synced_cross_thread_data.lock);
s_lock_cross_thread_data(dispatch_loop);
dispatch_loop->synced_cross_thread_data.is_executing = false;
s_unlock_cross_thread_data(dispatch_loop);

aws_mutex_unlock(&dispatch_loop->synced_cross_thread_data.lock);
s_unlock_dispatch_loop_context(dispatch_loop->context);
s_dispatch_event_loop_destroy(dispatch_loop->base_loop);
}
Expand Down Expand Up @@ -513,13 +512,13 @@ static void s_schedule_task_common(struct aws_event_loop *event_loop, struct aws
struct dispatch_loop *dispatch_loop = event_loop->impl_data;

s_lock_dispatch_loop_context(dispatch_loop->context);
bool should_schedule = false;

bool was_empty = aws_linked_list_empty(&dispatch_loop->synced_cross_thread_data.cross_thread_tasks);
s_lock_cross_thread_data(dispatch_loop);
task->timestamp = run_at_nanos;

bool was_empty = aws_linked_list_empty(&dispatch_loop->synced_cross_thread_data.cross_thread_tasks);
// As we dont have control to dispatch queue thread, all tasks are treated as cross thread tasks
aws_linked_list_push_back(&dispatch_loop->synced_cross_thread_data.cross_thread_tasks, &task->node);
s_unlock_cross_thread_data(dispatch_loop);

/**
* To avoid explicit scheduling event loop iterations, the actual "iteration scheduling" should happened at the end
Expand All @@ -531,6 +530,7 @@ static void s_schedule_task_common(struct aws_event_loop *event_loop, struct aws
* iteration that is processing the `cross_thread_tasks`.
*/

bool should_schedule = false;
if (was_empty && !dispatch_loop->context->scheduling_state.will_schedule) {
/** If there is no currently running iteration, then we check if we have already scheduled an iteration
* scheduled before this task's run time. */
Expand Down
4 changes: 2 additions & 2 deletions tests/event_loop_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ static int s_test_event_loop_canceled_tasks_run_in_el_thread(struct aws_allocato
&task1_args.condition_variable, &task1_args.mutex, s_task_ran_predicate, &task1_args));
ASSERT_TRUE(task1_args.invoked);
ASSERT_TRUE(task1_args.was_in_thread);
// The dispatch queue will schedule tasks on thread pools, it is unpredicatable which thread we run the task on,
// The dispatch queue will schedule tasks on thread pools, it is unpredictable which thread we run the task on,
// therefore we do not validate the thread id for dispatch queue.
if (aws_event_loop_get_default_type() != AWS_EVENT_LOOP_DISPATCH_QUEUE) {
ASSERT_FALSE(aws_thread_thread_id_equal(task1_args.thread_id, aws_thread_current_thread_id()));
Expand All @@ -172,7 +172,7 @@ static int s_test_event_loop_canceled_tasks_run_in_el_thread(struct aws_allocato
aws_mutex_unlock(&task2_args.mutex);

ASSERT_TRUE(task2_args.was_in_thread);
// The dispatch queue will schedule tasks on thread pools, it is unpredicatable which thread we run the task on,
// The dispatch queue will schedule tasks on thread pools, it is unpredictable which thread we run the task on,
// therefore we do not validate the thread id for dispatch queue.
if (aws_event_loop_get_default_type() != AWS_EVENT_LOOP_DISPATCH_QUEUE) {
ASSERT_TRUE(aws_thread_thread_id_equal(task2_args.thread_id, aws_thread_current_thread_id()));
Expand Down

0 comments on commit c60a056

Please sign in to comment.