libxtc 0.4.0
Async concurrency for C: Tokio + Seastar + BEAM, in one library
Loading...
Searching...
No Matches
xtc_aio.h
1/*-
2 * Copyright (c) 2026, The XTC Project
3 * Use of this source code is governed by the ISC License.
4 *
5 * src/inc/xtc_aio.h
6 * Async file I/O for fibers. A read/write/fsync that suspends the
7 * calling fiber and resumes it on completion, keeping the loop live
8 * for other work meanwhile.
9 *
10 * This is a SINGLE, PORTABLE async-file-I/O API: write storage code
11 * once as though true AIO is always available, and it runs unchanged
12 * everywhere libxtc builds. Where the host has a native completion
13 * engine (io_uring on Linux, IOCP on Windows) the call uses it; where
14 * it does not, the call transparently offloads to the blocking thread
15 * pool -- the SAME API and the same observable behavior (the fiber
16 * parks, the loop keeps running, the call returns the byte count or a
17 * negative errno). The caller never branches on the platform.
18 *
19 * Must be called from a fiber running on a loop; off a loop the op
20 * runs synchronously. Returns the byte count transferred (>= 0) or
21 * a negative errno; xtc_aio_fsync returns 0 or a negative errno.
22 * See xtc_aio(3).
23 */
24
25#ifndef XTC_AIO_H
26#define XTC_AIO_H
27
28#include "xtc_export.h"
29
30#include <stdint.h>
31#if !defined(_WIN32)
32#include <sys/uio.h> /* struct iovec (POSIX scatter/gather) */
33#endif
34
35/*
36 * PUBLIC: int xtc_aio_pread __P((int, void *, uint32_t, int64_t));
37 * PUBLIC: int xtc_aio_pwrite __P((int, const void *, uint32_t, int64_t));
38 * PUBLIC: int xtc_aio_fsync __P((int));
39 * PUBLIC: int xtc_aio_fdatasync __P((int));
40 */
41
42XTC_API int xtc_aio_pread(int fd, void *buf, uint32_t len, int64_t off);
43XTC_API int xtc_aio_pwrite(int fd, const void *buf, uint32_t len, int64_t off);
44
45#if !defined(_WIN32)
46/*
47 * PUBLIC: int xtc_aio_preadv __P((int, const struct iovec *, int, int64_t));
48 * PUBLIC: int xtc_aio_pwritev __P((int, const struct iovec *, int, int64_t));
49 *
50 * Vectored (scatter/gather) variants: read into / write from the
51 * iovcnt buffers described by iov, at file offset off, as one atomic
52 * positioned op -- the async-fiber analog of preadv(2)/pwritev(2).
53 * They exist so code that already builds struct iovec arrays (a WAL
54 * writer, PostgreSQL's smgr/md layer, any readv/writev consumer) can
55 * move to libxtc without flattening or looping. Same semantics as the
56 * scalar calls: the fiber parks and the loop stays live; returns the
57 * total byte count transferred (>= 0) or a negative errno.
58 *
59 * Where the host has io_uring the op is submitted natively
60 * (IORING_OP_READV/WRITEV); elsewhere it transparently offloads to the
61 * blocking pool (preadv/pwritev on a worker thread) -- identical
62 * observable behavior, as with the scalar API. iovcnt must be in
63 * [1, IOV_MAX]; out of range returns -EINVAL (the AIO surface's
64 * negative-errno convention), so a caller cannot force an unbounded
65 * submission. Direct-I/O builds validate each iovec base and the
66 * total length against the device alignment.
67 *
68 * POSIX only: Windows lacks preadv/pwritev and struct iovec; a
69 * WSABUF/OVERLAPPED-scatter port is future work, so these are not
70 * declared there. The scalar xtc_aio_pread/pwrite remain available on
71 * every platform.
72 */
73XTC_API int xtc_aio_preadv(int fd, const struct iovec *iov, int iovcnt, int64_t off);
74XTC_API int xtc_aio_pwritev(int fd, const struct iovec *iov, int iovcnt, int64_t off);
75#endif /* !_WIN32 */
76
77XTC_API int xtc_aio_fsync(int fd); /* full sync: data + metadata */
78XTC_API int xtc_aio_fdatasync(int fd); /* data only (the page/WAL flush hot path) */
79
80/*
81 * The internal / test hook __xtc_aio_force_offload (force the
82 * blocking-pool offload path even on a host with a native completion
83 * engine) is library-internal (the __ prefix) and not part of the
84 * stable API; it lives in "aio_int.h", not in this installed public
85 * header.
86 */
87
88#endif /* XTC_AIO_H */