Debian Codegraph @

crt/aws-c-io/tests/vcc/process_task_pre_queue.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 /* `read` not in preamble because we specialize for reading-into a uint64_t
20 var (as used by `s_process_task_pre_queue`) */
21 ssize_t read(int fd, void *buf, uint64_t count)
22 _(requires valid_fd(fd))
23 _(requires count == sizeof(uint64_t))
24 _(writes (uint64_t *)buf)
25 ;
26
27 static void s_process_task_pre_queue(struct aws_event_loop *event_loop _(ghost \claim(c_event_loop)) _(ghost \claim(c_mutex))) {
28 _(assert \always_by_claim(c_event_loop, event_loop))
29 struct epoll_loop *epoll_loop = event_loop->impl_data;
30 _(assert \inv(epoll_loop))
31
32 if (!epoll_loop->should_process_task_pre_queue) {
33 return;
34 }
35
36 AWS_LOGF_TRACE(AWS_LS_IO_EVENT_LOOP, "id=%p: processing cross-thread tasks", (void *)event_loop);
37 epoll_loop->should_process_task_pre_queue = false;
38
39 struct aws_linked_list task_pre_queue;
40 aws_linked_list_init(&task_pre_queue);
41 _(wrap(&task_pre_queue.head))
42 _(wrap(&task_pre_queue.tail))
43 _(wrap(&task_pre_queue))
44
45 uint64_t count_ignore = 0;
46
47 aws_mutex_lock(&epoll_loop->task_pre_queue_mutex _(ghost c_mutex));
48
49 /* several tasks could theoretically have been written (though this should never happen), make sure we drain the
50 * eventfd/pipe. */
51 while (read(epoll_loop->read_task_handle.data.fd, &count_ignore, sizeof(count_ignore)) > -1)
52 _(invariant \thread_local(&epoll_loop->read_task_handle))
53 _(invariant (&epoll_loop->read_task_handle)->\closed)
54 _(invariant \inv(&epoll_loop->read_task_handle))
55 _(invariant \wrapped(&epoll_loop->scheduler))
56 _(writes &count_ignore)
57 {
58 }
59
60 aws_linked_list_swap_contents(&epoll_loop->task_pre_queue, &task_pre_queue);
61
62 aws_mutex_unlock(&epoll_loop->task_pre_queue_mutex _(ghost c_mutex));
63
64 while (!aws_linked_list_empty(&task_pre_queue))
65 _(invariant 0 <= task_pre_queue.length)
66 _(invariant \wrapped(&task_pre_queue))
67 _(invariant \wrapped(&epoll_loop->scheduler))
68 _(writes &task_pre_queue, &epoll_loop->scheduler)
69 {
70 _(ghost struct aws_task *t)
71 struct aws_linked_list_node *node = aws_linked_list_pop_front(&task_pre_queue _(out t));
72 struct aws_task *task = AWS_CONTAINER_OF(node, struct aws_task, node);
73 AWS_LOGF_TRACE(
74 AWS_LS_IO_EVENT_LOOP,
75 "id=%p: task %p pulled to event-loop, scheduling now.",
76 (void *)event_loop,
77 (void *)task);
78 /* Timestamp 0 is used to denote "now" tasks */
79 if (task->timestamp == 0) {
80 aws_task_scheduler_schedule_now(&epoll_loop->scheduler, task);
81 } else {
82 aws_task_scheduler_schedule_future(&epoll_loop->scheduler, task, task->timestamp);
83 }
84 }
85 _(unwrap(&task_pre_queue))
86 _(unwrap(&task_pre_queue.head))
87 _(unwrap(&task_pre_queue.tail))
88 }
89 /* clang-format on */