Debian Codegraph @

crt/aws-c-io/tests/vcc/main_loop.c

raw symbols SCIP
1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 /* clang-format off */
17 #include "preamble.h"
18
19 #define DEFAULT_TIMEOUT 100000
20 #define MAX_EVENTS 100
21
22 static void s_on_tasks_to_schedule(
23 struct aws_event_loop *event_loop,
24 struct aws_io_handle *handle,
25 int events,
26 void *user_data _(ghost \claim(c_event_loop))
27 ) {
28 (void)handle;
29 (void)user_data;
30
31 AWS_LOGF_TRACE(AWS_LS_IO_EVENT_LOOP, "id=%p: notified of cross-thread tasks to schedule", (void *)event_loop);
32 struct epoll_loop *epoll_loop = event_loop->impl_data;
33 if (events & AWS_IO_EVENT_TYPE_READABLE) {
34 epoll_loop->should_process_task_pre_queue = true;
35 }
36 }
37
38 static void s_main_loop(void *args _(ghost \claim(c_mutex))) {
39 struct aws_event_loop *event_loop = args;
40 AWS_LOGF_INFO(AWS_LS_IO_EVENT_LOOP, "id=%p: main loop started", (void *)event_loop);
41 struct epoll_loop *epoll_loop = event_loop->impl_data;
42 _(ghost \claim c_event_loop;)
43 _(ghost c_event_loop = \make_claim({event_loop}, event_loop->\closed);)
44
45 /* set thread id to the thread of the event loop */
46 _(unwrap &epoll_loop->running_thread_id)
47 aws_atomic_store_ptr(&epoll_loop->running_thread_id, &epoll_loop->thread_created_on.thread_id);
48 _(wrap &epoll_loop->running_thread_id)
49
50 int err = s_subscribe_to_io_events(
51 event_loop, &epoll_loop->read_task_handle, AWS_IO_EVENT_TYPE_READABLE, s_on_tasks_to_schedule, NULL _(ghost c_event_loop));
52 if (err) {
53 return;
54 }
55
56 int timeout = DEFAULT_TIMEOUT;
57
58 struct epoll_event events[MAX_EVENTS];
59
60 AWS_LOGF_INFO(
61 AWS_LS_IO_EVENT_LOOP,
62 "id=%p: default timeout %d, and max events to process per tick %d",
63 (void *)event_loop,
64 timeout,
65 MAX_EVENTS);
66
67 /*
68 * until stop is called,
69 * call epoll_wait, if a task is scheduled, or a file descriptor has activity, it will
70 * return.
71 *
72 * process all events,
73 *
74 * run all scheduled tasks.
75 *
76 * process queued subscription cleanups.
77 */
78 while (epoll_loop->should_continue)
79 _(invariant \extent_mutable((struct epoll_event[MAX_EVENTS]) events))
80 _(invariant (&epoll_loop->read_task_handle)->\closed)
81 _(invariant \wrapped(&epoll_loop->scheduler))
82 _(writes &epoll_loop->scheduler)
83 _(writes &epoll_loop->should_process_task_pre_queue)
84 _(writes \extent((struct epoll_event[MAX_EVENTS]) events))
85 {
86 AWS_LOGF_TRACE(AWS_LS_IO_EVENT_LOOP, "id=%p: waiting for a maximum of %d ms", (void *)event_loop, timeout);
87 int event_count = epoll_wait(epoll_loop->epoll_fd, events, MAX_EVENTS, timeout);
88 AWS_LOGF_TRACE(
89 AWS_LS_IO_EVENT_LOOP, "id=%p: wake up with %d events to process.", (void *)event_loop, event_count);
90 for (int i = 0; i < event_count; ++i)
91 _(writes &epoll_loop->should_process_task_pre_queue)
92 {
93 struct epoll_event_data *event_data = (struct epoll_event_data *)events[i].data.ptr;
94 _(assert \wrapped(event_data))
95 _(assert \inv(event_data))
96
97 int event_mask = 0;
98 if (events[i].events & EPOLLIN) {
99 event_mask |= AWS_IO_EVENT_TYPE_READABLE;
100 }
101
102 if (events[i].events & EPOLLOUT) {
103 event_mask |= AWS_IO_EVENT_TYPE_WRITABLE;
104 }
105
106 if (events[i].events & EPOLLRDHUP) {
107 event_mask |= AWS_IO_EVENT_TYPE_REMOTE_HANG_UP;
108 }
109
110 if (events[i].events & EPOLLHUP) {
111 event_mask |= AWS_IO_EVENT_TYPE_CLOSED;
112 }
113
114 if (events[i].events & EPOLLERR) {
115 event_mask |= AWS_IO_EVENT_TYPE_ERROR;
116 }
117
118 if (event_data->is_subscribed) {
119 AWS_LOGF_TRACE(
120 AWS_LS_IO_EVENT_LOOP,
121 "id=%p: activity on fd %d, invoking handler.",
122 (void *)event_loop,
123 event_data->handle->data.fd);
124 event_data->on_event(event_loop, event_data->handle, event_mask, event_data->user_data _(ghost c_event_loop));
125 }
126 }
127
128 /* run scheduled tasks */
129 s_process_task_pre_queue(event_loop _(ghost c_event_loop) _(ghost c_mutex));
130
131 uint64_t now_ns = 0;
132 event_loop->clock(&now_ns); /* if clock fails, now_ns will be 0 and tasks scheduled for a specific time
133 will not be run. That's ok, we'll handle them next time around. */
134 AWS_LOGF_TRACE(AWS_LS_IO_EVENT_LOOP, "id=%p: running scheduled tasks.", (void *)event_loop);
135 aws_task_scheduler_run_all(&epoll_loop->scheduler, now_ns);
136
137 /* set timeout for next epoll_wait() call.
138 * if clock fails, or scheduler has no tasks, use default timeout */
139 bool use_default_timeout = false;
140
141 if (event_loop->clock(&now_ns)) {
142 use_default_timeout = true;
143 }
144
145 uint64_t next_run_time_ns;
146 if (!aws_task_scheduler_has_tasks(&epoll_loop->scheduler, &next_run_time_ns)) {
147 use_default_timeout = true;
148 }
149
150 if (use_default_timeout) {
151 AWS_LOGF_TRACE(
152 AWS_LS_IO_EVENT_LOOP, "id=%p: no more scheduled tasks using default timeout.", (void *)event_loop);
153 timeout = DEFAULT_TIMEOUT;
154 } else {
155 /* Translate timestamp (in nanoseconds) to timeout (in milliseconds) */
156 uint64_t timeout_ns = (next_run_time_ns > now_ns) ? (next_run_time_ns - now_ns) : 0;
157 uint64_t timeout_ms64 = aws_timestamp_convert(timeout_ns, AWS_TIMESTAMP_NANOS, AWS_TIMESTAMP_MILLIS, NULL);
158 timeout = timeout_ms64 > INT_MAX ? INT_MAX : (int)timeout_ms64;
159 AWS_LOGF_TRACE(
160 AWS_LS_IO_EVENT_LOOP,
161 "id=%p: detected more scheduled tasks with the next occurring at "
162 "%llu, using timeout of %d.",
163 (void *)event_loop,
164 (unsigned long long)timeout_ns,
165 timeout);
166 }
167 }
168
169 AWS_LOGF_DEBUG(AWS_LS_IO_EVENT_LOOP, "id=%p: exiting main loop", (void *)event_loop);
170 s_unsubscribe_from_io_events(event_loop, &epoll_loop->read_task_handle _(ghost c_event_loop) _(ghost c_mutex));
171 /* set thread id back to NULL. This should be updated again in destroy, before tasks are canceled. */
172 _(unwrap &epoll_loop->running_thread_id)
173 aws_atomic_store_ptr(&epoll_loop->running_thread_id, NULL);
174 _(wrap &epoll_loop->running_thread_id)
175 }
176 /* clang-format on */