Debian Codegraph @

crt/aws-c-common/include/aws/common/task_scheduler.h

raw symbols SCIP
1 #ifndef AWS_COMMON_TASK_SCHEDULER_H
2 #define AWS_COMMON_TASK_SCHEDULER_H
3
4 /**
5 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
6 * SPDX-License-Identifier: Apache-2.0.
7 */
8
9 #include <aws/common/common.h>
10 #include <aws/common/linked_list.h>
11 #include <aws/common/priority_queue.h>
12
13 AWS_PUSH_SANE_WARNING_LEVEL
14
15 struct aws_task;
16
17 typedef enum aws_task_status {
18 AWS_TASK_STATUS_RUN_READY,
19 AWS_TASK_STATUS_CANCELED,
20 } aws_task_status;
21
22 /**
23 * A scheduled function.
24 */
25 typedef void(aws_task_fn)(struct aws_task *task, void *arg, enum aws_task_status);
26
27 /*
28 * A task object.
29 * Once added to the scheduler, a task must remain in memory until its function is executed.
30 */
31 struct aws_task {
32 aws_task_fn *fn;
33 void *arg;
34 uint64_t timestamp;
35 struct aws_linked_list_node node;
36 struct aws_priority_queue_node priority_queue_node;
37 const char *type_tag;
38
39 /* honor the ABI compat */
40 union {
41 bool scheduled;
42 size_t reserved;
43 } abi_extension;
44 };
45
46 struct aws_task_scheduler {
47 struct aws_allocator *alloc;
48 struct aws_priority_queue timed_queue; /* Tasks scheduled to run at specific times */
49 struct aws_linked_list timed_list; /* If timed_queue runs out of memory, further timed tests are stored here */
50 struct aws_linked_list asap_list; /* Tasks scheduled to run as soon as possible */
51 };
52
53 AWS_EXTERN_C_BEGIN
54
55 /**
56 * Init an aws_task
57 */
58 AWS_COMMON_API
59 void aws_task_init(struct aws_task *task, aws_task_fn *fn, void *arg, const char *type_tag);
60
61 /*
62 * Runs or cancels a task
63 */
64 AWS_COMMON_API
65 void aws_task_run(struct aws_task *task, enum aws_task_status status);
66
67 /**
68 * Initializes a task scheduler instance.
69 */
70 AWS_COMMON_API
71 int aws_task_scheduler_init(struct aws_task_scheduler *scheduler, struct aws_allocator *alloc);
72
73 /**
74 * Empties and executes all queued tasks, passing the AWS_TASK_STATUS_CANCELED status to the task function.
75 * Cleans up any memory allocated, and prepares the instance for reuse or deletion.
76 */
77 AWS_COMMON_API
78 void aws_task_scheduler_clean_up(struct aws_task_scheduler *scheduler);
79
80 AWS_COMMON_API
81 bool aws_task_scheduler_is_valid(const struct aws_task_scheduler *scheduler);
82
83 /**
84 * Returns whether the scheduler has any scheduled tasks.
85 * next_task_time (optional) will be set to time of the next task, note that 0 will be set if tasks were
86 * added via aws_task_scheduler_schedule_now() and UINT64_MAX will be set if no tasks are scheduled at all.
87 */
88 AWS_COMMON_API
89 bool aws_task_scheduler_has_tasks(const struct aws_task_scheduler *scheduler, uint64_t *next_task_time);
90
91 /**
92 * Schedules a task to run immediately.
93 * The task should not be cleaned up or modified until its function is executed.
94 */
95 AWS_COMMON_API
96 void aws_task_scheduler_schedule_now(struct aws_task_scheduler *scheduler, struct aws_task *task);
97
98 /**
99 * Schedules a task to run at time_to_run.
100 * The task should not be cleaned up or modified until its function is executed.
101 */
102 AWS_COMMON_API
103 void aws_task_scheduler_schedule_future(
104 struct aws_task_scheduler *scheduler,
105 struct aws_task *task,
106 uint64_t time_to_run);
107
108 /**
109 * Removes task from the scheduler and invokes the task with the AWS_TASK_STATUS_CANCELED status.
110 */
111 AWS_COMMON_API
112 void aws_task_scheduler_cancel_task(struct aws_task_scheduler *scheduler, struct aws_task *task);
113
114 /**
115 * Sequentially execute all tasks scheduled to run at, or before current_time.
116 * AWS_TASK_STATUS_RUN_READY will be passed to the task function as the task status.
117 *
118 * If a task schedules another task, the new task will not be executed until the next call to this function.
119 */
120 AWS_COMMON_API
121 void aws_task_scheduler_run_all(struct aws_task_scheduler *scheduler, uint64_t current_time);
122
123 /**
124 * Convert a status value to a c-string suitable for logging
125 */
126 AWS_COMMON_API
127 const char *aws_task_status_to_c_str(enum aws_task_status status);
128
129 AWS_EXTERN_C_END
130 AWS_POP_SANE_WARNING_LEVEL
131
132 #endif /* AWS_COMMON_TASK_SCHEDULER_H */