| 1 | /* dex-aio.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.h" |
| 26 | #include "dex-aio-backend-private.h" |
| 27 | #include "dex-scheduler-private.h" |
| 28 | #include "dex-thread-storage-private.h" |
| 29 | |
| 30 | static DexAioContext * |
| 31 | dex_aio_context_current (void) |
| 32 | { |
| 33 | DexThreadStorage *storage = dex_thread_storage_get (); |
| 34 | |
| 35 | if (storage->aio_context) |
| 36 | return storage->aio_context; |
| 37 | |
| 38 | if (storage->scheduler) |
| 39 | return dex_scheduler_get_aio_context (storage->scheduler); |
| 40 | |
| 41 | g_return_val_if_reached (NULL); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * dex_aio_close: |
| 46 | * @aio_context: (nullable): |
| 47 | * @fd: the file descriptor to close |
| 48 | * |
| 49 | * An asynchronous `close()` wrapper. |
| 50 | * |
| 51 | * This function takes ownership of @fd and will close it asynchronously. |
| 52 | * |
| 53 | * Generally you want to provide `NULL` for the @aio_context as that |
| 54 | * will get the default aio context for your scheduler. |
| 55 | * |
| 56 | * Returns: (transfer full): a future that will resolve to %TRUE when the |
| 57 | * close completes or rejects with error. |
| 58 | * |
| 59 | * Since: 1.2 |
| 60 | */ |
| 61 | DexFuture * |
| 62 | dex_aio_close (DexAioContext *aio_context, |
| 63 | int fd) |
| 64 | { |
| 65 | dex_return_error_if_fail (fd > -1); |
| 66 | |
| 67 | if (aio_context == NULL) |
| 68 | aio_context = dex_aio_context_current (); |
| 69 | |
| 70 | return dex_aio_backend_close (aio_context->aio_backend, aio_context, fd); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * dex_aio_open: |
| 75 | * @aio_context: (nullable): |
| 76 | * @path: the path to open |
| 77 | * @flags: flags for `open()` |
| 78 | * @mode: permissions to use when creating the file |
| 79 | * |
| 80 | * An asynchronous `open()` wrapper. |
| 81 | * |
| 82 | * Generally you want to provide `NULL` for the @aio_context as that |
| 83 | * will get the default aio context for your scheduler. |
| 84 | * |
| 85 | * The resulting future resolves to a file descriptor which can be consumed |
| 86 | * with [[email protected]_fd]. |
| 87 | * |
| 88 | * Returns: (transfer full): a future that will resolve when the |
| 89 | * open completes or rejects with error. |
| 90 | * |
| 91 | * Since: 1.2 |
| 92 | */ |
| 93 | DexFuture * |
| 94 | dex_aio_open (DexAioContext *aio_context, |
| 95 | const char *path, |
| 96 | int flags, |
| 97 | int mode) |
| 98 | { |
| 99 | dex_return_error_if_fail (path != NULL); |
| 100 | |
| 101 | if (aio_context == NULL) |
| 102 | aio_context = dex_aio_context_current (); |
| 103 | |
| 104 | return dex_aio_backend_open (aio_context->aio_backend, aio_context, path, flags, mode); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * dex_aio_read: |
| 109 | * @aio_context: (nullable): |
| 110 | * @buffer: (array length=count) (element-type guint8) (out caller-allocates): |
| 111 | * @count: (in): the number of bytes to read |
| 112 | * @offset: the positioned offset within @fd to read from |
| 113 | * |
| 114 | * An asynchronous `pread()` wrapper. |
| 115 | * |
| 116 | * Generally you want to provide `NULL` for the @aio_context as that |
| 117 | * will get the default aio context for your scheduler. |
| 118 | * |
| 119 | * Returns: (transfer full): a future that will resolve when the |
| 120 | * read completes or rejects with error. |
| 121 | */ |
| 122 | DexFuture * |
| 123 | dex_aio_read (DexAioContext *aio_context, |
| 124 | int fd, |
| 125 | gpointer buffer, |
| 126 | gsize count, |
| 127 | goffset offset) |
| 128 | { |
| 129 | if (aio_context == NULL) |
| 130 | aio_context = dex_aio_context_current (); |
| 131 | |
| 132 | return dex_aio_backend_read (aio_context->aio_backend, aio_context, |
| 133 | fd, buffer, count, offset); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * dex_aio_write: |
| 138 | * @aio_context: (nullable): |
| 139 | * @buffer: (array length=count) (element-type guint8): |
| 140 | * @count: the number of bytes to write from @buffer |
| 141 | * @offset: the positioned offset within @fd to write at |
| 142 | * |
| 143 | * An asynchronous `pwrite()` wrapper. |
| 144 | * |
| 145 | * Generally you want to provide `NULL` for the @aio_context as that |
| 146 | * will get the default aio context for your scheduler. |
| 147 | * |
| 148 | * Returns: (transfer full): a future that will resolve when the |
| 149 | * write completes or rejects with error. |
| 150 | */ |
| 151 | DexFuture * |
| 152 | dex_aio_write (DexAioContext *aio_context, |
| 153 | int fd, |
| 154 | gconstpointer buffer, |
| 155 | gsize count, |
| 156 | goffset offset) |
| 157 | { |
| 158 | if (aio_context == NULL) |
| 159 | aio_context = dex_aio_context_current (); |
| 160 | |
| 161 | return dex_aio_backend_write (aio_context->aio_backend, aio_context, |
| 162 | fd, buffer, count, offset); |
| 163 | } |