Debian Codegraph @

src/dex-aio-backend.c

raw symbols SCIP
1 /* dex-aio-backend.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 <gio/gio.h>
24
25 #include "dex-aio-backend-private.h"
26
27 #ifdef HAVE_LIBURING
28 # include "dex-uring-aio-backend-private.h"
29 #endif
30
31 #include "dex-posix-aio-backend-private.h"
32
33 DEX_DEFINE_ABSTRACT_TYPE (DexAioBackend, dex_aio_backend, DEX_TYPE_OBJECT)
34
35 static void
36 dex_aio_backend_class_init (DexAioBackendClass *aio_backend_class)
37 {
38 }
39
40 static void
41 dex_aio_backend_init (DexAioBackend *aio_backend)
42 {
43 }
44
45 DexAioContext *
46 dex_aio_backend_create_context (DexAioBackend *aio_backend)
47 {
48 g_return_val_if_fail (DEX_IS_AIO_BACKEND (aio_backend), NULL);
49
50 return DEX_AIO_BACKEND_GET_CLASS (aio_backend)->create_context (aio_backend);
51 }
52
53 DexFuture *
54 dex_aio_backend_close (DexAioBackend *aio_backend,
55 DexAioContext *aio_context,
56 int fd)
57 {
58 dex_return_error_if_fail (DEX_IS_AIO_BACKEND (aio_backend));
59 dex_return_error_if_fail (aio_context != NULL);
60 dex_return_error_if_fail (fd > -1);
61
62 return DEX_AIO_BACKEND_GET_CLASS (aio_backend)->close (aio_backend, aio_context, fd);
63 }
64
65 DexFuture *
66 dex_aio_backend_open (DexAioBackend *aio_backend,
67 DexAioContext *aio_context,
68 const char *path,
69 int flags,
70 int mode)
71 {
72 dex_return_error_if_fail (DEX_IS_AIO_BACKEND (aio_backend));
73 dex_return_error_if_fail (aio_context != NULL);
74 dex_return_error_if_fail (path != NULL);
75
76 return DEX_AIO_BACKEND_GET_CLASS (aio_backend)->open (aio_backend, aio_context,
77 path, flags, mode);
78 }
79
80 DexFuture *
81 dex_aio_backend_read (DexAioBackend *aio_backend,
82 DexAioContext *aio_context,
83 int fd,
84 gpointer buffer,
85 gsize count,
86 goffset offset)
87 {
88 dex_return_error_if_fail (DEX_IS_AIO_BACKEND (aio_backend));
89 dex_return_error_if_fail (aio_context != NULL);
90
91 return DEX_AIO_BACKEND_GET_CLASS (aio_backend)->read (aio_backend, aio_context, fd, buffer, count, offset);
92 }
93
94 DexFuture *
95 dex_aio_backend_write (DexAioBackend *aio_backend,
96 DexAioContext *aio_context,
97 int fd,
98 gconstpointer buffer,
99 gsize count,
100 goffset offset)
101 {
102 dex_return_error_if_fail (DEX_IS_AIO_BACKEND (aio_backend));
103 dex_return_error_if_fail (aio_context != NULL);
104
105 return DEX_AIO_BACKEND_GET_CLASS (aio_backend)->write (aio_backend, aio_context, fd, buffer, count, offset);
106 }
107
108 DexAioBackend *
109 dex_aio_backend_get_default (void)
110 {
111 static DexAioBackend *instance;
112
113 if (g_once_init_enter (&instance))
114 {
115 DexAioBackend *backend = NULL;
116
117 #if defined(HAVE_LIBURING)
118 backend = dex_uring_aio_backend_new ();
119 #endif
120
121 if (backend == NULL)
122 backend = dex_posix_aio_backend_new ();
123
124 #if defined(HAVE_LIBURING)
125 /* Only log that we're using a fallback when we
126 * expect to be using liburing.
127 */
128 g_debug ("Using AIO backend %s",
129 DEX_OBJECT_TYPE_NAME (backend));
130 #endif
131
132 g_once_init_leave (&instance, backend);
133 }
134
135 return instance;
136 }