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

Add additional priority queue related APIs #1067

Merged
merged 10 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
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
11 changes: 11 additions & 0 deletions include/aws/common/priority_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ void aws_priority_queue_init_static(
size_t item_size,
aws_priority_queue_compare_fn *pred);

/**
* Initializes a queue node to a default value that indicates the node is not in the queue. If we used the
* classical indexing for array-based heaps (starting at 1) we wouldn't need this, but because zero is a valid node
* index and we use SIZE_MAX as "not-in-the-heap" we need an init so that people aren't hard-coding magic SIZE_MAXs
* around all over the place.
*
* @param node priority queue node to initialize with a default value
*/
bretambrose marked this conversation as resolved.
Show resolved Hide resolved
AWS_COMMON_API
void aws_priority_queue_node_init(struct aws_priority_queue_node *node);

/**
* Checks that the backpointer at a specific index of the queue is
* NULL or points to a correctly allocated aws_priority_queue_node.
Expand Down
4 changes: 4 additions & 0 deletions source/priority_queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -400,3 +400,7 @@ size_t aws_priority_queue_size(const struct aws_priority_queue *queue) {
size_t aws_priority_queue_capacity(const struct aws_priority_queue *queue) {
return aws_array_list_capacity(&queue->container);
}

void aws_priority_queue_node_init(struct aws_priority_queue_node *node) {
node->current_index = SIZE_MAX;
}
4 changes: 2 additions & 2 deletions source/task_scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ void aws_task_scheduler_schedule_now(struct aws_task_scheduler *scheduler, struc
(void *)task,
task->type_tag);

task->priority_queue_node.current_index = SIZE_MAX;
aws_priority_queue_node_init(&task->priority_queue_node);
aws_linked_list_node_reset(&task->node);
task->timestamp = 0;

Expand All @@ -161,7 +161,7 @@ void aws_task_scheduler_schedule_future(

task->timestamp = time_to_run;

task->priority_queue_node.current_index = SIZE_MAX;
aws_priority_queue_node_init(&task->priority_queue_node);
aws_linked_list_node_reset(&task->node);
int err = aws_priority_queue_push_ref(&scheduler->timed_queue, &task, &task->priority_queue_node);
if (AWS_UNLIKELY(err)) {
Expand Down