| 1 | Title: Asynchronous IO |
| 2 | |
| 3 | The [[email protected]] APIs already provide robust support for asynchronous IO. |
| 4 | The common API allows for different types of implementation based on the stream implementation. |
| 5 | |
| 6 | Libdex provides wrappers for various APIs. |
| 7 | Coverage is not complete but we do expect additional APIs to be covered in future releases. |
| 8 | |
| 9 | # Files, Directories & Streams |
| 10 | |
| 11 | ## File Management |
| 12 | |
| 13 | See [[email protected]_copy] for copying files. |
| 14 | |
| 15 | See [[email protected]_delete] for deleting files. |
| 16 | |
| 17 | See [[email protected]_move] for moving files. |
| 18 | |
| 19 | ## File Attributes |
| 20 | |
| 21 | See [[email protected]_query_info] and [[email protected]_query_file_type], and [[email protected]_query_exists] for basic querying. |
| 22 | |
| 23 | You can set file attributes using [[email protected]_set_attributes]. |
| 24 | |
| 25 | ## Directories |
| 26 | |
| 27 | You can create a directory or hierarchy of directories using [[email protected]_make_directory] and [[email protected]_make_directory_with_parents] respectively. |
| 28 | |
| 29 | ### Enumerating Files |
| 30 | |
| 31 | You can create a file enumerator for a directory using [[email protected]_enumerate_children]. |
| 32 | |
| 33 | You can also asynchronously enumerate the files of that directory using [[email protected]_enumerator_next_files] which will resolve to a `g_autolist(GFileInfo)` of infos. |
| 34 | |
| 35 | ## Reading and Writing Files |
| 36 | |
| 37 | The [[email protected]_read] will provide a [[email protected]] which can be read from. |
| 38 | |
| 39 | A simpler interface to get the bytes of a file is provided via [[email protected]_load_contents_bytes]. |
| 40 | |
| 41 | The [[email protected]_replace] will replace a file on disk providing a [[email protected]] to write to. |
| 42 | The [[email protected]_replace_contents_bytes] provides a simplified API for this when the content is readily available. |
| 43 | |
| 44 | ## Reading Streams |
| 45 | |
| 46 | See [[email protected]_stream_read], [[email protected]_stream_read_bytes], [[email protected]_stream_skip], and [[email protected]_stream_close] for working with input streams asynchronously. |
| 47 | |
| 48 | ## Writing Streams |
| 49 | |
| 50 | See [[email protected]_stream_write], [[email protected]_stream_write_bytes], [[email protected]_stream_splice], and [[email protected]_stream_close] for writing to streams asynchronously. |
| 51 | |
| 52 | # Sockets |
| 53 | |
| 54 | The [[email protected]_listener_accept], [[email protected]_client_connect], and [[email protected]_lookup_by_name] may be helpful when writing socket servers and clients. |
| 55 | |
| 56 | # D-Bus |
| 57 | |
| 58 | [See the page about D-Bus](dbus.html) |
| 59 | |
| 60 | # Subprocesses |
| 61 | |
| 62 | You can await completion of a subprocess using [[email protected]_wait_check]. |
| 63 | |
| 64 | # Asynchronous IO with File Descriptors |
| 65 | |
| 66 | [[email protected]] and related APIs provides much opportunity for streams to be used asynchronously. |
| 67 | There may be cases where you want similar behavior with traditional file-descriptors. |
| 68 | |
| 69 | Libdex provides a set of AIO-like functions for traditional file-descriptors which may be backed with more efficient mechanisms. |
| 70 | |
| 71 | [[email protected]] typically uses a thread pool of blocking IO operations on Linux and other operating systems because that was the fastest method when the APIs were created. |
| 72 | However, on some operating systems such as Linux, faster methods finally exist. |
| 73 | |
| 74 | On Linux, `io_uring` can be used for asynchronous IO and is provided in the form of a [[email protected]]. |
| 75 | |
| 76 | # Asynchronous Reads |
| 77 | |
| 78 | ```c |
| 79 | DexFuture *dex_aio_read (DexAioContext *aio_context, |
| 80 | int fd, |
| 81 | gpointer buffer, |
| 82 | gsize count, |
| 83 | goffset offset) |
| 84 | ``` |
| 85 | |
| 86 | Use the `dex_aio_read()` function to read from a file-descriptor. |
| 87 | The result will be a future that resolves to a `gint64` containing the number of bytes read. |
| 88 | |
| 89 | If there was a failure, the future will reject using the appropriate error code. |
| 90 | |
| 91 | Your `buffer` must stay alive for the duration of the asynchronous read. |
| 92 | One easy way to make that happen is to wrap the resulting future in a `dex_future_then()` which stores the buffer as `user_data` and releases it when finished. |
| 93 | |
| 94 | If you are doing buffer pooling, more effort may be required. |
| 95 | |
| 96 | # Asynchronous Writes |
| 97 | |
| 98 | ```c |
| 99 | DexFuture *dex_aio_write (DexAioContext *aio_context, |
| 100 | int fd, |
| 101 | gconstpointer buffer, |
| 102 | gsize count, |
| 103 | goffset offset) |
| 104 | ``` |
| 105 | |
| 106 | A similar API exists as `dex_aio_read()` but for writing. |
| 107 | It too will resolve to a `gint64` containing the number of bytes written. |
| 108 | |
| 109 | `buffer` must be kept alive for the duration of the call and it is the callers responsibility to do so. |