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 <stdint.h>
29#if !defined(_WIN32)
30#include <sys/uio.h> /* struct iovec (POSIX scatter/gather) */
31#endif
32
33/*
34 * PUBLIC: int xtc_aio_pread __P((int, void *, uint32_t, int64_t));
35 * PUBLIC: int xtc_aio_pwrite __P((int, const void *, uint32_t, int64_t));
36 * PUBLIC: int xtc_aio_fsync __P((int));
37 * PUBLIC: int xtc_aio_fdatasync __P((int));
38 */
39
40int xtc_aio_pread(int fd, void *buf, uint32_t len, int64_t off);
41int xtc_aio_pwrite(int fd, const void *buf, uint32_t len, int64_t off);
42
43#if !defined(_WIN32)
44/*
45 * PUBLIC: int xtc_aio_preadv __P((int, const struct iovec *, int, int64_t));
46 * PUBLIC: int xtc_aio_pwritev __P((int, const struct iovec *, int, int64_t));
47 *
48 * Vectored (scatter/gather) variants: read into / write from the
49 * iovcnt buffers described by iov, at file offset off, as one atomic
50 * positioned op -- the async-fiber analog of preadv(2)/pwritev(2).
51 * They exist so code that already builds struct iovec arrays (a WAL
52 * writer, PostgreSQL's smgr/md layer, any readv/writev consumer) can
53 * move to libxtc without flattening or looping. Same semantics as the
54 * scalar calls: the fiber parks and the loop stays live; returns the
55 * total byte count transferred (>= 0) or a negative errno.
56 *
57 * Where the host has io_uring the op is submitted natively
58 * (IORING_OP_READV/WRITEV); elsewhere it transparently offloads to the
59 * blocking pool (preadv/pwritev on a worker thread) -- identical
60 * observable behavior, as with the scalar API. iovcnt must be in
61 * [1, IOV_MAX]; out of range returns -EINVAL (the AIO surface's
62 * negative-errno convention), so a caller cannot force an unbounded
63 * submission. Direct-I/O builds validate each iovec base and the
64 * total length against the device alignment.
65 *
66 * POSIX only: Windows lacks preadv/pwritev and struct iovec; a
67 * WSABUF/OVERLAPPED-scatter port is future work, so these are not
68 * declared there. The scalar xtc_aio_pread/pwrite remain available on
69 * every platform.
70 */
71int xtc_aio_preadv(int fd, const struct iovec *iov, int iovcnt, int64_t off);
72int xtc_aio_pwritev(int fd, const struct iovec *iov, int iovcnt, int64_t off);
73#endif /* !_WIN32 */
74
75int xtc_aio_fsync(int fd); /* full sync: data + metadata */
76int xtc_aio_fdatasync(int fd); /* data only (the page/WAL flush hot path) */
77
78/*
79 * Internal / test hook: force the blocking-pool offload path even on a
80 * host with a native completion engine (io_uring / IOCP). Lets the
81 * portable fallback be exercised and proven identical to the native
82 * path where the tests run. Also reads XTC_AIO_FORCE_OFFLOAD=1 on
83 * first use. Not part of the stable API.
84 */
85void __xtc_aio_force_offload(int on);
86
87#endif /* XTC_AIO_H */