Debian Codegraph @

crt/aws-c-common/source/thread_scheduler.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5 #include <aws/common/clock.h>
6 #include <aws/common/condition_variable.h>
7 #include <aws/common/mutex.h>
8 #include <aws/common/ref_count.h>
9 #include <aws/common/task_scheduler.h>
10 #include <aws/common/thread.h>
11 #include <aws/common/thread_scheduler.h>
12
13 struct aws_thread_scheduler {
14 struct aws_allocator *allocator;
15 struct aws_ref_count ref_count;
16 struct aws_thread thread;
17 struct aws_task_scheduler scheduler;
18 struct aws_atomic_var should_exit;
19
20 struct {
21 struct aws_linked_list scheduling_queue;
22 struct aws_linked_list cancel_queue;
23 struct aws_mutex mutex;
24 struct aws_condition_variable c_var;
25 } thread_data;
26 };
27
28 struct cancellation_node {
29 struct aws_task *task_to_cancel;
30 struct aws_linked_list_node node;
31 };
32
33 static void s_destroy_callback(void *arg) {
34 struct aws_thread_scheduler *scheduler = arg;
35 aws_atomic_store_int(&scheduler->should_exit, 1U);
36 aws_condition_variable_notify_all(&scheduler->thread_data.c_var);
37 aws_thread_join(&scheduler->thread);
38 aws_task_scheduler_clean_up(&scheduler->scheduler);
39 aws_condition_variable_clean_up(&scheduler->thread_data.c_var);
40 aws_mutex_clean_up(&scheduler->thread_data.mutex);
41 aws_thread_clean_up(&scheduler->thread);
42 aws_mem_release(scheduler->allocator, scheduler);
43 }
44
45 static bool s_thread_should_wake(void *arg) {
46 struct aws_thread_scheduler *scheduler = arg;
47
48 uint64_t current_time = 0;
49 aws_high_res_clock_get_ticks(&current_time);
50
51 uint64_t next_scheduled_task = 0;
52 aws_task_scheduler_has_tasks(&scheduler->scheduler, &next_scheduled_task);
53 return aws_atomic_load_int(&scheduler->should_exit) ||
54 !aws_linked_list_empty(&scheduler->thread_data.scheduling_queue) ||
55 !aws_linked_list_empty(&scheduler->thread_data.cancel_queue) || (next_scheduled_task <= current_time);
56 }
57
58 static void s_thread_fn(void *arg) {
59 struct aws_thread_scheduler *scheduler = arg;
60
61 while (!aws_atomic_load_int(&scheduler->should_exit)) {
62
63 /* move tasks from the mutex protected list to the scheduler. This is because we don't want to hold the lock
64 * for the scheduler during run_all and then try and acquire the lock from another thread to schedule something
65 * because that potentially would block the calling thread. */
66 struct aws_linked_list list_cpy;
67 aws_linked_list_init(&list_cpy);
68 struct aws_linked_list cancel_list_cpy;
69 aws_linked_list_init(&cancel_list_cpy);
70
71 AWS_FATAL_ASSERT(!aws_mutex_lock(&scheduler->thread_data.mutex) && "mutex lock failed!");
72 aws_linked_list_swap_contents(&scheduler->thread_data.scheduling_queue, &list_cpy);
73 aws_linked_list_swap_contents(&scheduler->thread_data.cancel_queue, &cancel_list_cpy);
74 AWS_FATAL_ASSERT(!aws_mutex_unlock(&scheduler->thread_data.mutex) && "mutex unlock failed!");
75
76 while (!aws_linked_list_empty(&list_cpy)) {
77 struct aws_linked_list_node *node = aws_linked_list_pop_front(&list_cpy);
78 struct aws_task *task = AWS_CONTAINER_OF(node, struct aws_task, node);
79 if (task->timestamp) {
80 aws_task_scheduler_schedule_future(&scheduler->scheduler, task, task->timestamp);
81 } else {
82 aws_task_scheduler_schedule_now(&scheduler->scheduler, task);
83 }
84 }
85
86 /* now cancel the tasks. */
87 while (!aws_linked_list_empty(&cancel_list_cpy)) {
88 struct aws_linked_list_node *node = aws_linked_list_pop_front(&cancel_list_cpy);
89 struct cancellation_node *cancellation_node = AWS_CONTAINER_OF(node, struct cancellation_node, node);
90 aws_task_scheduler_cancel_task(&scheduler->scheduler, cancellation_node->task_to_cancel);
91 aws_mem_release(scheduler->allocator, cancellation_node);
92 }
93
94 /* now run everything */
95 uint64_t current_time = 0;
96 aws_high_res_clock_get_ticks(&current_time);
97 aws_task_scheduler_run_all(&scheduler->scheduler, current_time);
98
99 uint64_t next_scheduled_task = 0;
100 aws_task_scheduler_has_tasks(&scheduler->scheduler, &next_scheduled_task);
101
102 int64_t timeout = 0;
103 if (next_scheduled_task == UINT64_MAX) {
104 /* at least wake up once per 30 seconds. */
105 timeout = (int64_t)30 * (int64_t)AWS_TIMESTAMP_NANOS;
106 } else {
107 timeout = (int64_t)(next_scheduled_task - current_time);
108 }
109
110 if (timeout > 0) {
111 AWS_FATAL_ASSERT(!aws_mutex_lock(&scheduler->thread_data.mutex) && "mutex lock failed!");
112
113 aws_condition_variable_wait_for_pred(
114 &scheduler->thread_data.c_var, &scheduler->thread_data.mutex, timeout, s_thread_should_wake, scheduler);
115 AWS_FATAL_ASSERT(!aws_mutex_unlock(&scheduler->thread_data.mutex) && "mutex unlock failed!");
116 }
117 }
118 }
119
120 struct aws_thread_scheduler *aws_thread_scheduler_new(
121 struct aws_allocator *allocator,
122 const struct aws_thread_options *thread_options) {
123 struct aws_thread_scheduler *scheduler = aws_mem_calloc(allocator, 1, sizeof(struct aws_thread_scheduler));
124
125 if (!scheduler) {
126 return NULL;
127 }
128
129 if (aws_thread_init(&scheduler->thread, allocator)) {
130 goto clean_up;
131 }
132
133 AWS_FATAL_ASSERT(!aws_mutex_init(&scheduler->thread_data.mutex) && "mutex init failed!");
134 AWS_FATAL_ASSERT(!aws_condition_variable_init(&scheduler->thread_data.c_var) && "condition variable init failed!");
135
136 if (aws_task_scheduler_init(&scheduler->scheduler, allocator)) {
137 goto thread_init;
138 }
139
140 scheduler->allocator = allocator;
141 aws_atomic_init_int(&scheduler->should_exit, 0U);
142 aws_ref_count_init(&scheduler->ref_count, scheduler, s_destroy_callback);
143 aws_linked_list_init(&scheduler->thread_data.scheduling_queue);
144 aws_linked_list_init(&scheduler->thread_data.cancel_queue);
145
146 if (aws_thread_launch(&scheduler->thread, s_thread_fn, scheduler, thread_options)) {
147 goto scheduler_init;
148 }
149
150 return scheduler;
151
152 scheduler_init:
153 aws_task_scheduler_clean_up(&scheduler->scheduler);
154
155 thread_init:
156 aws_condition_variable_clean_up(&scheduler->thread_data.c_var);
157 aws_mutex_clean_up(&scheduler->thread_data.mutex);
158 aws_thread_clean_up(&scheduler->thread);
159
160 clean_up:
161 aws_mem_release(allocator, scheduler);
162
163 return NULL;
164 }
165
166 void aws_thread_scheduler_acquire(struct aws_thread_scheduler *scheduler) {
167 aws_ref_count_acquire(&scheduler->ref_count);
168 }
169
170 void aws_thread_scheduler_release(const struct aws_thread_scheduler *scheduler) {
171 aws_ref_count_release((struct aws_ref_count *)&scheduler->ref_count);
172 }
173
174 void aws_thread_scheduler_schedule_future(
175 struct aws_thread_scheduler *scheduler,
176 struct aws_task *task,
177 uint64_t time_to_run) {
178 task->timestamp = time_to_run;
179 AWS_FATAL_ASSERT(!aws_mutex_lock(&scheduler->thread_data.mutex) && "mutex lock failed!");
180 aws_linked_list_push_back(&scheduler->thread_data.scheduling_queue, &task->node);
181 AWS_FATAL_ASSERT(!aws_mutex_unlock(&scheduler->thread_data.mutex) && "mutex unlock failed!");
182 aws_condition_variable_notify_one(&scheduler->thread_data.c_var);
183 }
184 void aws_thread_scheduler_schedule_now(struct aws_thread_scheduler *scheduler, struct aws_task *task) {
185 aws_thread_scheduler_schedule_future(scheduler, task, 0U);
186 }
187
188 void aws_thread_scheduler_cancel_task(struct aws_thread_scheduler *scheduler, struct aws_task *task) {
189 struct cancellation_node *cancellation_node =
190 aws_mem_calloc(scheduler->allocator, 1, sizeof(struct cancellation_node));
191 AWS_FATAL_ASSERT(cancellation_node && "allocation failed for cancellation node!");
192 AWS_FATAL_ASSERT(!aws_mutex_lock(&scheduler->thread_data.mutex) && "mutex lock failed!");
193 struct aws_task *found_task = NULL;
194
195 /* remove tasks that are still in the scheduling queue, but haven't made it to the scheduler yet. */
196 struct aws_linked_list_node *node = aws_linked_list_empty(&scheduler->thread_data.scheduling_queue)
197 ? NULL
198 : aws_linked_list_front(&scheduler->thread_data.scheduling_queue);
199 while (node != NULL) {
200 struct aws_task *potential_task = AWS_CONTAINER_OF(node, struct aws_task, node);
201
202 if (potential_task == task) {
203 found_task = potential_task;
204 break;
205 }
206
207 if (aws_linked_list_node_next_is_valid(node)) {
208 node = aws_linked_list_next(node);
209 } else {
210 node = NULL;
211 }
212 }
213
214 if (found_task) {
215 aws_linked_list_remove(&found_task->node);
216 }
217
218 cancellation_node->task_to_cancel = task;
219
220 /* regardless put it in the cancel queue so the thread can call the task with canceled status. */
221 aws_linked_list_push_back(&scheduler->thread_data.cancel_queue, &cancellation_node->node);
222 AWS_FATAL_ASSERT(!aws_mutex_unlock(&scheduler->thread_data.mutex) && "mutex unlock failed!");
223 /* notify so the loop knows to wakeup and process the cancellations. */
224 aws_condition_variable_notify_one(&scheduler->thread_data.c_var);
225 }