This source file includes following definitions.
- __io_uring_get_cqe
- io_uring_peek_cqe
- io_uring_wait_cqe
- io_uring_submit
- io_uring_get_sqe
1 #include <sys/types.h>
2 #include <sys/stat.h>
3 #include <sys/mman.h>
4 #include <unistd.h>
5 #include <errno.h>
6 #include <string.h>
7
8 #include "liburing.h"
9 #include "barrier.h"
10
11 static int __io_uring_get_cqe(struct io_uring *ring,
12 struct io_uring_cqe **cqe_ptr, int wait)
13 {
14 struct io_uring_cq *cq = &ring->cq;
15 const unsigned mask = *cq->kring_mask;
16 unsigned head;
17 int ret;
18
19 *cqe_ptr = NULL;
20 head = *cq->khead;
21 do {
22
23
24
25
26
27
28
29 read_barrier();
30 if (head != *cq->ktail) {
31 *cqe_ptr = &cq->cqes[head & mask];
32 break;
33 }
34 if (!wait)
35 break;
36 ret = io_uring_enter(ring->ring_fd, 0, 1,
37 IORING_ENTER_GETEVENTS, NULL);
38 if (ret < 0)
39 return -errno;
40 } while (1);
41
42 return 0;
43 }
44
45
46
47
48
49 int io_uring_peek_cqe(struct io_uring *ring, struct io_uring_cqe **cqe_ptr)
50 {
51 return __io_uring_get_cqe(ring, cqe_ptr, 0);
52 }
53
54
55
56
57
58 int io_uring_wait_cqe(struct io_uring *ring, struct io_uring_cqe **cqe_ptr)
59 {
60 return __io_uring_get_cqe(ring, cqe_ptr, 1);
61 }
62
63
64
65
66
67
68 int io_uring_submit(struct io_uring *ring)
69 {
70 struct io_uring_sq *sq = &ring->sq;
71 const unsigned mask = *sq->kring_mask;
72 unsigned ktail, ktail_next, submitted, to_submit;
73 int ret;
74
75
76
77
78
79
80 read_barrier();
81 if (*sq->khead != *sq->ktail) {
82 submitted = *sq->kring_entries;
83 goto submit;
84 }
85
86 if (sq->sqe_head == sq->sqe_tail)
87 return 0;
88
89
90
91
92 submitted = 0;
93 ktail = ktail_next = *sq->ktail;
94 to_submit = sq->sqe_tail - sq->sqe_head;
95 while (to_submit--) {
96 ktail_next++;
97 read_barrier();
98
99 sq->array[ktail & mask] = sq->sqe_head & mask;
100 ktail = ktail_next;
101
102 sq->sqe_head++;
103 submitted++;
104 }
105
106 if (!submitted)
107 return 0;
108
109 if (*sq->ktail != ktail) {
110
111
112
113
114
115
116 write_barrier();
117 *sq->ktail = ktail;
118
119
120
121
122 write_barrier();
123 }
124
125 submit:
126 ret = io_uring_enter(ring->ring_fd, submitted, 0,
127 IORING_ENTER_GETEVENTS, NULL);
128 if (ret < 0)
129 return -errno;
130
131 return ret;
132 }
133
134
135
136
137
138
139
140
141 struct io_uring_sqe *io_uring_get_sqe(struct io_uring *ring)
142 {
143 struct io_uring_sq *sq = &ring->sq;
144 unsigned next = sq->sqe_tail + 1;
145 struct io_uring_sqe *sqe;
146
147
148
149
150 if (next - sq->sqe_head > *sq->kring_entries)
151 return NULL;
152
153 sqe = &sq->sqes[sq->sqe_tail & *sq->kring_mask];
154 sq->sqe_tail = next;
155 return sqe;
156 }