| 1 |
#ifndef AWS_COMMON_TASK_SCHEDULER_H |
| 2 |
#define AWS_COMMON_TASK_SCHEDULER_H |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 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 |
|
| 24 |
|
| 25 |
typedef void(aws_task_fn)(struct aws_task *task, void *arg, enum aws_task_status); |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 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 |
|
| 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; |
| 49 |
struct aws_linked_list timed_list; |
| 50 |
struct aws_linked_list asap_list; |
| 51 |
}; |
| 52 |
|
| 53 |
AWS_EXTERN_C_BEGIN |
| 54 |
|
| 55 |
|
| 56 |
|
| 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 |
|
| 63 |
|
| 64 |
AWS_COMMON_API |
| 65 |
void aws_task_run(struct aws_task *task, enum aws_task_status status); |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
AWS_COMMON_API |
| 71 |
int aws_task_scheduler_init(struct aws_task_scheduler *scheduler, struct aws_allocator *alloc); |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 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 |
|
| 85 |
|
| 86 |
|
| 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 |
|
| 93 |
|
| 94 |
|
| 95 |
AWS_COMMON_API |
| 96 |
void aws_task_scheduler_schedule_now(struct aws_task_scheduler *scheduler, struct aws_task *task); |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 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 |
|
| 110 |
|
| 111 |
AWS_COMMON_API |
| 112 |
void aws_task_scheduler_cancel_task(struct aws_task_scheduler *scheduler, struct aws_task *task); |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
AWS_COMMON_API |
| 121 |
void aws_task_scheduler_run_all(struct aws_task_scheduler *scheduler, uint64_t current_time); |
| 122 |
|
| 123 |
|
| 124 |
|
| 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 |