Debian Codegraph @

src/dex-uring-future.c

raw symbols SCIP
1 /* dex-uring-future.c
2 *
3 * Copyright 2022 Christian Hergert <[email protected]>
4 *
5 * This library is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 2.1 of the
8 * License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: LGPL-2.1-or-later
19 */
20
21 #include "config.h"
22
23 #include <fcntl.h>
24
25 #include <liburing.h>
26
27 #include <gio/gio.h>
28
29 #include "dex-fd-private.h"
30 #include "dex-future-private.h"
31 #include "dex-uring-future-private.h"
32
33 typedef enum _DexUringType
34 {
35 DEX_URING_TYPE_CLOSE = 1,
36 DEX_URING_TYPE_OPEN,
37 DEX_URING_TYPE_READ,
38 DEX_URING_TYPE_WRITE,
39 } DexUringType;
40
41 struct _DexUringFuture
42 {
43 DexFuture parent_instance;
44 DexUringType type;
45 union {
46 struct {
47 DexFD *fd;
48 int result;
49 } close;
50 struct {
51 char *path;
52 int flags;
53 int mode;
54 int result;
55 } open;
56 struct {
57 int fd;
58 gpointer buffer;
59 gsize count;
60 goffset offset;
61 gssize result;
62 } read;
63 struct {
64 int fd;
65 gconstpointer buffer;
66 gsize count;
67 goffset offset;
68 gssize result;
69 } write;
70 };
71 };
72
73 typedef struct _DexUringFutureClass
74 {
75 DexFutureClass parent_class;
76 } DexUringFutureClass;
77
78 DEX_DEFINE_FINAL_TYPE (DexUringFuture, dex_uring_future, DEX_TYPE_FUTURE)
79
80 #undef DEX_TYPE_URING_FUTURE
81 #define DEX_TYPE_URING_FUTURE dex_uring_future_type
82
83 static void
84 dex_uring_future_finalize (DexObject *object)
85 {
86 DexUringFuture *uring_future = DEX_URING_FUTURE (object);
87
88 if (uring_future->type == DEX_URING_TYPE_CLOSE)
89 g_clear_pointer (&uring_future->close.fd, dex_fd_free);
90 else if (uring_future->type == DEX_URING_TYPE_OPEN)
91 g_clear_pointer (&uring_future->open.path, g_free);
92
93 DEX_OBJECT_CLASS (dex_uring_future_parent_class)->finalize (object);
94 }
95
96 static void
97 dex_uring_future_class_init (DexUringFutureClass *uring_future_class)
98 {
99 DexObjectClass *object_class = DEX_OBJECT_CLASS (uring_future_class);
100
101 object_class->finalize = dex_uring_future_finalize;
102 }
103
104 static void
105 dex_uring_future_init (DexUringFuture *uring_future)
106 {
107 }
108
109 static GError *
110 create_error (int error_code)
111 {
112 return g_error_new_literal (G_IO_ERROR,
113 g_io_error_from_errno (-error_code),
114 g_strerror (-error_code));
115 }
116
117 static void
118 complete_boolean (DexUringFuture *uring_future,
119 int value)
120 {
121 if (value < 0)
122 dex_future_complete (DEX_FUTURE (uring_future),
123 NULL,
124 create_error (value));
125 else
126 dex_future_complete (DEX_FUTURE (uring_future),
127 &(GValue) { G_TYPE_BOOLEAN, {{.v_int = TRUE}}},
128 NULL);
129 }
130
131 static void
132 complete_fd (DexUringFuture *uring_future,
133 int value)
134 {
135 if (value < 0)
136 dex_future_complete (DEX_FUTURE (uring_future),
137 NULL,
138 create_error (value));
139 else
140 {
141 GValue gvalue = G_VALUE_INIT;
142
143 g_value_init (&gvalue, DEX_TYPE_FD);
144 g_value_take_boxed (&gvalue, g_memdup2 (&value, sizeof value));
145 dex_future_complete_steal (DEX_FUTURE (uring_future), &gvalue, NULL);
146 }
147 }
148
149 static void
150 complete_ssize (DexUringFuture *uring_future,
151 gssize value)
152 {
153 if (value < 0)
154 dex_future_complete (DEX_FUTURE (uring_future),
155 NULL,
156 create_error (-value));
157 else
158 dex_future_complete (DEX_FUTURE (uring_future),
159 &(GValue) { G_TYPE_INT64, {{.v_int64 = value}}},
160 NULL);
161 }
162
163 void
164 dex_uring_future_complete (DexUringFuture *uring_future)
165 {
166 switch (uring_future->type)
167 {
168 case DEX_URING_TYPE_CLOSE:
169 complete_boolean (uring_future, uring_future->close.result);
170 break;
171
172 case DEX_URING_TYPE_OPEN:
173 complete_fd (uring_future, uring_future->open.result);
174 break;
175
176 case DEX_URING_TYPE_READ:
177 complete_ssize (uring_future, uring_future->read.result);
178 break;
179
180 case DEX_URING_TYPE_WRITE:
181 complete_ssize (uring_future, uring_future->write.result);
182 break;
183
184 default:
185 g_assert_not_reached ();
186 }
187 }
188
189 void
190 dex_uring_future_cqe (DexUringFuture *uring_future,
191 struct io_uring_cqe *cqe)
192 {
193 switch (uring_future->type)
194 {
195 case DEX_URING_TYPE_CLOSE:
196 uring_future->close.result = cqe->res;
197 break;
198
199 case DEX_URING_TYPE_OPEN:
200 uring_future->open.result = cqe->res;
201 break;
202
203 case DEX_URING_TYPE_READ:
204 uring_future->read.result = cqe->res;
205 break;
206
207 case DEX_URING_TYPE_WRITE:
208 uring_future->write.result = cqe->res;
209 break;
210
211 default:
212 g_assert_not_reached ();
213 }
214 }
215
216 void
217 dex_uring_future_sqe (DexUringFuture *uring_future,
218 struct io_uring_sqe *sqe)
219 {
220 switch (uring_future->type)
221 {
222 case DEX_URING_TYPE_CLOSE:
223 io_uring_prep_close (sqe, dex_fd_steal (uring_future->close.fd));
224 break;
225
226 case DEX_URING_TYPE_OPEN:
227 io_uring_prep_openat (sqe,
228 AT_FDCWD,
229 uring_future->open.path,
230 uring_future->open.flags,
231 uring_future->open.mode);
232 break;
233
234 case DEX_URING_TYPE_READ:
235 io_uring_prep_read (sqe,
236 uring_future->read.fd,
237 uring_future->read.buffer,
238 uring_future->read.count,
239 uring_future->read.offset);
240 break;
241
242 case DEX_URING_TYPE_WRITE:
243 io_uring_prep_write (sqe,
244 uring_future->write.fd,
245 uring_future->write.buffer,
246 uring_future->write.count,
247 uring_future->write.offset);
248 break;
249
250 default:
251 g_assert_not_reached ();
252 }
253 }
254
255 DexUringFuture *
256 dex_uring_future_new_close (int fd)
257 {
258 DexUringFuture *future;
259
260 future = (DexUringFuture *)dex_object_create_instance (DEX_TYPE_URING_FUTURE);
261 future->type = DEX_URING_TYPE_CLOSE;
262 future->close.fd = g_memdup2 (&fd, sizeof fd);
263 future->close.result = -1;
264
265 return future;
266 }
267
268 DexUringFuture *
269 dex_uring_future_new_open (const char *path,
270 int flags,
271 int mode)
272 {
273 DexUringFuture *future;
274
275 future = (DexUringFuture *)dex_object_create_instance (DEX_TYPE_URING_FUTURE);
276 future->type = DEX_URING_TYPE_OPEN;
277 future->open.path = g_strdup (path);
278 future->open.flags = flags;
279 future->open.mode = mode;
280 future->open.result = -1;
281
282 return future;
283 }
284
285 DexUringFuture *
286 dex_uring_future_new_read (int fd,
287 gpointer buffer,
288 gsize count,
289 goffset offset)
290 {
291 DexUringFuture *future;
292
293 future = (DexUringFuture *)dex_object_create_instance (DEX_TYPE_URING_FUTURE);
294 future->type = DEX_URING_TYPE_READ;
295 future->read.fd = fd;
296 future->read.buffer = buffer;
297 future->read.count = count;
298 future->read.offset = offset;
299
300 return future;
301 }
302
303 DexUringFuture *
304 dex_uring_future_new_write (int fd,
305 gconstpointer buffer,
306 gsize count,
307 goffset offset)
308 {
309 DexUringFuture *future;
310
311 future = (DexUringFuture *)dex_object_create_instance (DEX_TYPE_URING_FUTURE);
312 future->type = DEX_URING_TYPE_WRITE;
313 future->write.fd = fd;
314 future->write.buffer = buffer;
315 future->write.count = count;
316 future->write.offset = offset;
317
318 return future;
319 }