Skip to content

Commit

Permalink
move shceduling context to dispatch queue context
Browse files Browse the repository at this point in the history
  • Loading branch information
xiazhvera committed Nov 27, 2024
1 parent 3c34a54 commit 86b1e08
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
1 change: 0 additions & 1 deletion source/darwin/dispatch_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ struct dispatch_loop {
struct aws_string *dispatch_queue_id;

struct {
struct dispatch_scheduling_state scheduling_state;
struct aws_linked_list cross_thread_tasks;
struct dispatch_loop_context *context;
bool suspended;
Expand Down
22 changes: 12 additions & 10 deletions source/darwin/dispatch_queue_event_loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ static struct aws_event_loop_vtable s_vtable = {
struct dispatch_loop_context {
struct aws_mutex lock;
struct dispatch_loop *io_dispatch_loop;
struct dispatch_scheduling_state scheduling_state;
struct aws_allocator *allocator;
struct aws_ref_count ref_count;
};
Expand Down Expand Up @@ -186,16 +187,16 @@ struct aws_event_loop *aws_event_loop_new_with_dispatch_queue(
goto clean_up;
}

dispatch_loop->synced_data.scheduling_state.will_schedule = false;
dispatch_loop->allocator = alloc;
dispatch_loop->base_loop = loop;

aws_linked_list_init(&dispatch_loop->local_cross_thread_tasks);
aws_linked_list_init(&dispatch_loop->synced_data.scheduling_state.scheduled_services);
aws_linked_list_init(&dispatch_loop->synced_data.cross_thread_tasks);

struct dispatch_loop_context *context = aws_mem_calloc(alloc, 1, sizeof(struct dispatch_loop_context));
aws_ref_count_init(&context->ref_count, context, s_dispatch_loop_context_destroy);
context->scheduling_state.will_schedule = false;
aws_linked_list_init(&context->scheduling_state.scheduled_services);
aws_mutex_init(&context->lock);
context->io_dispatch_loop = dispatch_loop;
context->allocator = alloc;
Expand Down Expand Up @@ -330,7 +331,7 @@ static bool begin_iteration(struct scheduled_service_entry *entry) {
&dispatch_loop->synced_data.cross_thread_tasks, &dispatch_loop->local_cross_thread_tasks);

// mark us as running an iteration and remove from the pending list
dispatch_loop->synced_data.scheduling_state.will_schedule = true;
dispatch_loop->synced_data.context->scheduling_state.will_schedule = true;
aws_linked_list_remove(&entry->node);
aws_mutex_unlock(&contxt->lock);

Expand All @@ -349,7 +350,7 @@ static void end_iteration(struct scheduled_service_entry *entry) {
return;
}

dispatch_loop->synced_data.scheduling_state.will_schedule = false;
dispatch_loop->synced_data.context->scheduling_state.will_schedule = false;

// if there are any cross-thread tasks, reschedule an iteration for now
if (!aws_linked_list_empty(&dispatch_loop->synced_data.cross_thread_tasks)) {
Expand All @@ -365,7 +366,7 @@ static void end_iteration(struct scheduled_service_entry *entry) {
// only schedule an iteration if there isn't an existing dispatched iteration for the next task time or
// earlier
if (s_should_schedule_iteration(
&dispatch_loop->synced_data.scheduling_state.scheduled_services, next_task_time)) {
&dispatch_loop->synced_data.context->scheduling_state.scheduled_services, next_task_time)) {
s_try_schedule_new_iteration(contxt, next_task_time);
}
}
Expand Down Expand Up @@ -438,11 +439,12 @@ static void s_try_schedule_new_iteration(struct dispatch_loop_context *dispatch_
struct dispatch_loop *dispatch_loop = dispatch_loop_context->io_dispatch_loop;
if (!dispatch_loop || dispatch_loop->synced_data.suspended)
return;
if (!s_should_schedule_iteration(&dispatch_loop->synced_data.scheduling_state.scheduled_services, timestamp)) {
if (!s_should_schedule_iteration(
&dispatch_loop->synced_data.context->scheduling_state.scheduled_services, timestamp)) {
return;
}
struct scheduled_service_entry *entry = s_scheduled_service_entry_new(dispatch_loop_context, timestamp);
aws_linked_list_push_front(&dispatch_loop->synced_data.scheduling_state.scheduled_services, &entry->node);
aws_linked_list_push_front(&dispatch_loop->synced_data.context->scheduling_state.scheduled_services, &entry->node);
dispatch_async_f(dispatch_loop->dispatch_queue, entry, s_run_iteration);
}

Expand All @@ -468,11 +470,11 @@ static void s_schedule_task_common(struct aws_event_loop *event_loop, struct aws
* iteration that is processing the `cross_thread_tasks`.
*/

if (was_empty && !dispatch_loop->synced_data.scheduling_state.will_schedule) {
if (was_empty && !dispatch_loop->synced_data.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. */
should_schedule =
s_should_schedule_iteration(&dispatch_loop->synced_data.scheduling_state.scheduled_services, run_at_nanos);
should_schedule = s_should_schedule_iteration(
&dispatch_loop->synced_data.context->scheduling_state.scheduled_services, run_at_nanos);
}

// If there is no scheduled iteration, start one right now to process the `cross_thread_task`.
Expand Down
1 change: 0 additions & 1 deletion source/darwin/nw_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -1591,4 +1591,3 @@ static bool s_socket_is_open_fn(struct aws_socket *socket) {

return nw_socket->last_error == AWS_OP_SUCCESS;
}

0 comments on commit 86b1e08

Please sign in to comment.