xref: /qemu/include/block/aio.h (revision 0462a32b4f63b2448b4a196381138afd50719dc4)
1a76bab49Saliguori /*
2a76bab49Saliguori  * QEMU aio implementation
3a76bab49Saliguori  *
4a76bab49Saliguori  * Copyright IBM, Corp. 2008
5a76bab49Saliguori  *
6a76bab49Saliguori  * Authors:
7a76bab49Saliguori  *  Anthony Liguori   <aliguori@us.ibm.com>
8a76bab49Saliguori  *
9a76bab49Saliguori  * This work is licensed under the terms of the GNU GPL, version 2.  See
10a76bab49Saliguori  * the COPYING file in the top-level directory.
11a76bab49Saliguori  *
12a76bab49Saliguori  */
13a76bab49Saliguori 
14a76bab49Saliguori #ifndef QEMU_AIO_H
15a76bab49Saliguori #define QEMU_AIO_H
16a76bab49Saliguori 
1773fd282eSStefan Hajnoczi #ifdef CONFIG_LINUX_IO_URING
1873fd282eSStefan Hajnoczi #include <liburing.h>
1973fd282eSStefan Hajnoczi #endif
2068ba85ceSMarkus Armbruster #include "qemu/coroutine-core.h"
211de7afc9SPaolo Bonzini #include "qemu/queue.h"
221de7afc9SPaolo Bonzini #include "qemu/event_notifier.h"
2351483f6cSPeter Maydell #include "qemu/lockcnt.h"
24dcc772e2SLiu Ping Fan #include "qemu/thread.h"
25dae21b98SAlex Bligh #include "qemu/timer.h"
26aead9dc9SPaolo Bonzini #include "block/graph-lock.h"
279c86c97fSAlexander Bulekov #include "hw/qdev-core.h"
289c86c97fSAlexander Bulekov 
29a76bab49Saliguori 
307c84b1b8SMarkus Armbruster typedef struct BlockAIOCB BlockAIOCB;
31097310b5SMarkus Armbruster typedef void BlockCompletionFunc(void *opaque, int ret);
3285e8dab1SPaolo Bonzini 
33d7331bedSStefan Hajnoczi typedef struct AIOCBInfo {
347c84b1b8SMarkus Armbruster     void (*cancel_async)(BlockAIOCB *acb);
358c82e9a4SStefan Hajnoczi     size_t aiocb_size;
36d7331bedSStefan Hajnoczi } AIOCBInfo;
3785e8dab1SPaolo Bonzini 
387c84b1b8SMarkus Armbruster struct BlockAIOCB {
39d7331bedSStefan Hajnoczi     const AIOCBInfo *aiocb_info;
4085e8dab1SPaolo Bonzini     BlockDriverState *bs;
41097310b5SMarkus Armbruster     BlockCompletionFunc *cb;
4285e8dab1SPaolo Bonzini     void *opaque;
43f197fe2bSFam Zheng     int refcnt;
4485e8dab1SPaolo Bonzini };
4585e8dab1SPaolo Bonzini 
46d7331bedSStefan Hajnoczi void *qemu_aio_get(const AIOCBInfo *aiocb_info, BlockDriverState *bs,
47097310b5SMarkus Armbruster                    BlockCompletionFunc *cb, void *opaque);
488007429aSFam Zheng void qemu_aio_unref(void *p);
49f197fe2bSFam Zheng void qemu_aio_ref(void *p);
5085e8dab1SPaolo Bonzini 
51f627aab1SPaolo Bonzini typedef struct AioHandler AioHandler;
524749079cSStefan Hajnoczi typedef QLIST_HEAD(, AioHandler) AioHandlerList;
53f627aab1SPaolo Bonzini typedef void QEMUBHFunc(void *opaque);
54f6a51c84SStefan Hajnoczi typedef bool AioPollFn(void *opaque);
55f627aab1SPaolo Bonzini typedef void IOHandler(void *opaque);
56f627aab1SPaolo Bonzini 
57dc67daeeSMaciej S. Szmigiero struct ThreadPoolAio;
580187f5c9SPaolo Bonzini struct LinuxAioState;
593cbc17eeSPaolo Bonzini typedef struct LuringState LuringState;
600187f5c9SPaolo Bonzini 
61aa38e19fSStefan Hajnoczi /* Is polling disabled? */
62aa38e19fSStefan Hajnoczi bool aio_poll_disabled(AioContext *ctx);
63aa38e19fSStefan Hajnoczi 
641f050a46SStefan Hajnoczi /* Callbacks for file descriptor monitoring implementations */
651f050a46SStefan Hajnoczi typedef struct {
661f050a46SStefan Hajnoczi     /*
671f050a46SStefan Hajnoczi      * update:
681f050a46SStefan Hajnoczi      * @ctx: the AioContext
69b321051cSStefan Hajnoczi      * @old_node: the existing handler or NULL if this file descriptor is being
70b321051cSStefan Hajnoczi      *            monitored for the first time
71b321051cSStefan Hajnoczi      * @new_node: the new handler or NULL if this file descriptor is being
72b321051cSStefan Hajnoczi      *            removed
731f050a46SStefan Hajnoczi      *
74b321051cSStefan Hajnoczi      * Add/remove/modify a monitored file descriptor.
751f050a46SStefan Hajnoczi      *
761f050a46SStefan Hajnoczi      * Called with ctx->list_lock acquired.
771f050a46SStefan Hajnoczi      */
78b321051cSStefan Hajnoczi     void (*update)(AioContext *ctx, AioHandler *old_node, AioHandler *new_node);
791f050a46SStefan Hajnoczi 
801f050a46SStefan Hajnoczi     /*
811f050a46SStefan Hajnoczi      * wait:
821f050a46SStefan Hajnoczi      * @ctx: the AioContext
831f050a46SStefan Hajnoczi      * @ready_list: list for handlers that become ready
841f050a46SStefan Hajnoczi      * @timeout: maximum duration to wait, in nanoseconds
851f050a46SStefan Hajnoczi      *
861f050a46SStefan Hajnoczi      * Wait for file descriptors to become ready and place them on ready_list.
871f050a46SStefan Hajnoczi      *
881f050a46SStefan Hajnoczi      * Called with ctx->list_lock incremented but not locked.
891f050a46SStefan Hajnoczi      *
901f050a46SStefan Hajnoczi      * Returns: number of ready file descriptors.
911f050a46SStefan Hajnoczi      */
921f050a46SStefan Hajnoczi     int (*wait)(AioContext *ctx, AioHandlerList *ready_list, int64_t timeout);
93aa38e19fSStefan Hajnoczi 
94aa38e19fSStefan Hajnoczi     /*
95aa38e19fSStefan Hajnoczi      * need_wait:
96aa38e19fSStefan Hajnoczi      * @ctx: the AioContext
97aa38e19fSStefan Hajnoczi      *
98aa38e19fSStefan Hajnoczi      * Tell aio_poll() when to stop userspace polling early because ->wait()
99aa38e19fSStefan Hajnoczi      * has fds ready.
100aa38e19fSStefan Hajnoczi      *
101aa38e19fSStefan Hajnoczi      * File descriptor monitoring implementations that cannot poll fd readiness
102aa38e19fSStefan Hajnoczi      * from userspace should use aio_poll_disabled() here.  This ensures that
103aa38e19fSStefan Hajnoczi      * file descriptors are not starved by handlers that frequently make
104aa38e19fSStefan Hajnoczi      * progress via userspace polling.
105aa38e19fSStefan Hajnoczi      *
106aa38e19fSStefan Hajnoczi      * Returns: true if ->wait() should be called, false otherwise.
107aa38e19fSStefan Hajnoczi      */
108aa38e19fSStefan Hajnoczi     bool (*need_wait)(AioContext *ctx);
1091f050a46SStefan Hajnoczi } FDMonOps;
1101f050a46SStefan Hajnoczi 
1118c6b0356SStefan Hajnoczi /*
1128c6b0356SStefan Hajnoczi  * Each aio_bh_poll() call carves off a slice of the BH list, so that newly
1138c6b0356SStefan Hajnoczi  * scheduled BHs are not processed until the next aio_bh_poll() call.  All
1148c6b0356SStefan Hajnoczi  * active aio_bh_poll() calls chain their slices together in a list, so that
1158c6b0356SStefan Hajnoczi  * nested aio_bh_poll() calls process all scheduled bottom halves.
1168c6b0356SStefan Hajnoczi  */
1178c6b0356SStefan Hajnoczi typedef QSLIST_HEAD(, QEMUBH) BHList;
1188c6b0356SStefan Hajnoczi typedef struct BHListSlice BHListSlice;
1198c6b0356SStefan Hajnoczi struct BHListSlice {
1208c6b0356SStefan Hajnoczi     BHList bh_list;
1218c6b0356SStefan Hajnoczi     QSIMPLEQ_ENTRY(BHListSlice) next;
1228c6b0356SStefan Hajnoczi };
1238c6b0356SStefan Hajnoczi 
12473fd282eSStefan Hajnoczi typedef QSLIST_HEAD(, AioHandler) AioHandlerSList;
12573fd282eSStefan Hajnoczi 
126*518db101SKevin Wolf typedef struct AioPolledEvent {
127*518db101SKevin Wolf     int64_t ns;        /* current polling time in nanoseconds */
128*518db101SKevin Wolf } AioPolledEvent;
129*518db101SKevin Wolf 
1306a1751b7SAlex Bligh struct AioContext {
131e3713e00SPaolo Bonzini     GSource source;
132e3713e00SPaolo Bonzini 
1337c690fd1SPaolo Bonzini     /* Used by AioContext users to protect from multi-threaded access.  */
1343fe71223SPaolo Bonzini     QemuRecMutex lock;
13598563fc3SStefan Hajnoczi 
136aead9dc9SPaolo Bonzini     /*
137aead9dc9SPaolo Bonzini      * Keep track of readers and writers of the block layer graph.
138aead9dc9SPaolo Bonzini      * This is essential to avoid performing additions and removal
139aead9dc9SPaolo Bonzini      * of nodes and edges from block graph while some
140aead9dc9SPaolo Bonzini      * other thread is traversing it.
141aead9dc9SPaolo Bonzini      */
142aead9dc9SPaolo Bonzini     BdrvGraphRWlock *bdrv_graph;
143aead9dc9SPaolo Bonzini 
1447c690fd1SPaolo Bonzini     /* The list of registered AIO handlers.  Protected by ctx->list_lock. */
1454749079cSStefan Hajnoczi     AioHandlerList aio_handlers;
1464749079cSStefan Hajnoczi 
1474749079cSStefan Hajnoczi     /* The list of AIO handlers to be deleted.  Protected by ctx->list_lock. */
1484749079cSStefan Hajnoczi     AioHandlerList deleted_aio_handlers;
149a915f4bcSPaolo Bonzini 
150eabc9779SPaolo Bonzini     /* Used to avoid unnecessary event_notifier_set calls in aio_notify;
1513c18a92dSPaolo Bonzini      * only written from the AioContext home thread, or under the BQL in
1523c18a92dSPaolo Bonzini      * the case of the main AioContext.  However, it is read from any
1533c18a92dSPaolo Bonzini      * thread so it is still accessed with atomic primitives.
1543c18a92dSPaolo Bonzini      *
1553c18a92dSPaolo Bonzini      * If this field is 0, everything (file descriptors, bottom halves,
1563c18a92dSPaolo Bonzini      * timers) will be re-evaluated before the next blocking poll() or
1573c18a92dSPaolo Bonzini      * io_uring wait; therefore, the event_notifier_set call can be
1583c18a92dSPaolo Bonzini      * skipped.  If it is non-zero, you may need to wake up a concurrent
1593c18a92dSPaolo Bonzini      * aio_poll or the glib main event loop, making event_notifier_set
1603c18a92dSPaolo Bonzini      * necessary.
161eabc9779SPaolo Bonzini      *
162eabc9779SPaolo Bonzini      * Bit 0 is reserved for GSource usage of the AioContext, and is 1
16354a16a63SCao jin      * between a call to aio_ctx_prepare and the next call to aio_ctx_check.
164eabc9779SPaolo Bonzini      * Bits 1-31 simply count the number of active calls to aio_poll
165eabc9779SPaolo Bonzini      * that are in the prepare or poll phase.
166eabc9779SPaolo Bonzini      *
167eabc9779SPaolo Bonzini      * The GSource and aio_poll must use a different mechanism because
168eabc9779SPaolo Bonzini      * there is no certainty that a call to GSource's prepare callback
169eabc9779SPaolo Bonzini      * (via g_main_context_prepare) is indeed followed by check and
170eabc9779SPaolo Bonzini      * dispatch.  It's not clear whether this would be a bug, but let's
171eabc9779SPaolo Bonzini      * play safe and allow it---it will just cause extra calls to
172eabc9779SPaolo Bonzini      * event_notifier_set until the next call to dispatch.
173eabc9779SPaolo Bonzini      *
174eabc9779SPaolo Bonzini      * Instead, the aio_poll calls include both the prepare and the
175eabc9779SPaolo Bonzini      * dispatch phase, hence a simple counter is enough for them.
1760ceb849bSPaolo Bonzini      */
177eabc9779SPaolo Bonzini     uint32_t notify_me;
1780ceb849bSPaolo Bonzini 
1797c690fd1SPaolo Bonzini     /* A lock to protect between QEMUBH and AioHandler adders and deleter,
1807c690fd1SPaolo Bonzini      * and to ensure that no callbacks are removed while we're walking and
1817c690fd1SPaolo Bonzini      * dispatching them.
182d7c99a12SPaolo Bonzini      */
183d7c99a12SPaolo Bonzini     QemuLockCnt list_lock;
1840ceb849bSPaolo Bonzini 
1858c6b0356SStefan Hajnoczi     /* Bottom Halves pending aio_bh_poll() processing */
1868c6b0356SStefan Hajnoczi     BHList bh_list;
1878c6b0356SStefan Hajnoczi 
1888c6b0356SStefan Hajnoczi     /* Chained BH list slices for each nested aio_bh_poll() call */
1898c6b0356SStefan Hajnoczi     QSIMPLEQ_HEAD(, BHListSlice) bh_slice_list;
190f627aab1SPaolo Bonzini 
19105e514b1SPaolo Bonzini     /* Used by aio_notify.
19205e514b1SPaolo Bonzini      *
19305e514b1SPaolo Bonzini      * "notified" is used to avoid expensive event_notifier_test_and_clear
19405e514b1SPaolo Bonzini      * calls.  When it is clear, the EventNotifier is clear, or one thread
19505e514b1SPaolo Bonzini      * is going to clear "notified" before processing more events.  False
19605e514b1SPaolo Bonzini      * positives are possible, i.e. "notified" could be set even though the
19705e514b1SPaolo Bonzini      * EventNotifier is clear.
19805e514b1SPaolo Bonzini      *
19905e514b1SPaolo Bonzini      * Note that event_notifier_set *cannot* be optimized the same way.  For
20005e514b1SPaolo Bonzini      * more information on the problem that would result, see "#ifdef BUG2"
20105e514b1SPaolo Bonzini      * in the docs/aio_notify_accept.promela formal model.
20205e514b1SPaolo Bonzini      */
20305e514b1SPaolo Bonzini     bool notified;
2042f4dc3c1SPaolo Bonzini     EventNotifier notifier;
2056b5f8762SStefan Hajnoczi 
2060c330a73SPaolo Bonzini     QSLIST_HEAD(, Coroutine) scheduled_coroutines;
2070c330a73SPaolo Bonzini     QEMUBH *co_schedule_bh;
2080c330a73SPaolo Bonzini 
20971ad4713SNicolas Saenz Julienne     int thread_pool_min;
21071ad4713SNicolas Saenz Julienne     int thread_pool_max;
2117c690fd1SPaolo Bonzini     /* Thread pool for performing work and receiving completion callbacks.
2127c690fd1SPaolo Bonzini      * Has its own locking.
2137c690fd1SPaolo Bonzini      */
214dc67daeeSMaciej S. Szmigiero     struct ThreadPoolAio *thread_pool;
215dae21b98SAlex Bligh 
2160187f5c9SPaolo Bonzini #ifdef CONFIG_LINUX_AIO
2170187f5c9SPaolo Bonzini     struct LinuxAioState *linux_aio;
2180187f5c9SPaolo Bonzini #endif
2196663a0a3SAarushi Mehta #ifdef CONFIG_LINUX_IO_URING
2203cbc17eeSPaolo Bonzini     LuringState *linux_io_uring;
22173fd282eSStefan Hajnoczi 
22273fd282eSStefan Hajnoczi     /* State for file descriptor monitoring using Linux io_uring */
22373fd282eSStefan Hajnoczi     struct io_uring fdmon_io_uring;
22473fd282eSStefan Hajnoczi     AioHandlerSList submit_list;
2256663a0a3SAarushi Mehta #endif
2260187f5c9SPaolo Bonzini 
2277c690fd1SPaolo Bonzini     /* TimerLists for calling timers - one per clock type.  Has its own
2287c690fd1SPaolo Bonzini      * locking.
2297c690fd1SPaolo Bonzini      */
230dae21b98SAlex Bligh     QEMUTimerListGroup tlg;
231c1e1e5faSFam Zheng 
2324a1cba38SStefan Hajnoczi     /* Number of AioHandlers without .io_poll() */
2334a1cba38SStefan Hajnoczi     int poll_disable_cnt;
2344a1cba38SStefan Hajnoczi 
23582a41186SStefan Hajnoczi     /* Polling mode parameters */
23682a41186SStefan Hajnoczi     int64_t poll_max_ns;    /* maximum polling time in nanoseconds */
23782a41186SStefan Hajnoczi     int64_t poll_grow;      /* polling time growth factor */
23882a41186SStefan Hajnoczi     int64_t poll_shrink;    /* polling time shrink factor */
2394a1cba38SStefan Hajnoczi 
2401793ad02SStefano Garzarella     /* AIO engine parameters */
2411793ad02SStefano Garzarella     int64_t aio_max_batch;  /* maximum number of requests in a batch */
2421793ad02SStefano Garzarella 
243d37d0e36SStefan Hajnoczi     /*
244d37d0e36SStefan Hajnoczi      * List of handlers participating in userspace polling.  Protected by
245d37d0e36SStefan Hajnoczi      * ctx->list_lock.  Iterated and modified mostly by the event loop thread
246d37d0e36SStefan Hajnoczi      * from aio_poll() with ctx->list_lock incremented.  aio_set_fd_handler()
247d37d0e36SStefan Hajnoczi      * only touches the list to delete nodes if ctx->list_lock's count is zero.
248d37d0e36SStefan Hajnoczi      */
249d37d0e36SStefan Hajnoczi     AioHandlerList poll_aio_handlers;
250d37d0e36SStefan Hajnoczi 
251684e508cSStefan Hajnoczi     /* Are we in polling mode or monitoring file descriptors? */
252684e508cSStefan Hajnoczi     bool poll_started;
253684e508cSStefan Hajnoczi 
254fbe3fc5cSFam Zheng     /* epoll(7) state used when built with CONFIG_EPOLL */
255fbe3fc5cSFam Zheng     int epollfd;
2561f050a46SStefan Hajnoczi 
2571f050a46SStefan Hajnoczi     const FDMonOps *fdmon_ops;
2586a1751b7SAlex Bligh };
259f627aab1SPaolo Bonzini 
260f627aab1SPaolo Bonzini /**
261f627aab1SPaolo Bonzini  * aio_context_new: Allocate a new AioContext.
262f627aab1SPaolo Bonzini  *
263f627aab1SPaolo Bonzini  * AioContext provide a mini event-loop that can be waited on synchronously.
264f627aab1SPaolo Bonzini  * They also provide bottom halves, a service to execute a piece of code
265f627aab1SPaolo Bonzini  * as soon as possible.
266f627aab1SPaolo Bonzini  */
2672f78e491SChrysostomos Nanakos AioContext *aio_context_new(Error **errp);
268f627aab1SPaolo Bonzini 
269f627aab1SPaolo Bonzini /**
270e3713e00SPaolo Bonzini  * aio_context_ref:
271e3713e00SPaolo Bonzini  * @ctx: The AioContext to operate on.
272e3713e00SPaolo Bonzini  *
273e3713e00SPaolo Bonzini  * Add a reference to an AioContext.
274e3713e00SPaolo Bonzini  */
275e3713e00SPaolo Bonzini void aio_context_ref(AioContext *ctx);
276e3713e00SPaolo Bonzini 
277e3713e00SPaolo Bonzini /**
278e3713e00SPaolo Bonzini  * aio_context_unref:
279e3713e00SPaolo Bonzini  * @ctx: The AioContext to operate on.
280e3713e00SPaolo Bonzini  *
281e3713e00SPaolo Bonzini  * Drop a reference to an AioContext.
282e3713e00SPaolo Bonzini  */
283e3713e00SPaolo Bonzini void aio_context_unref(AioContext *ctx);
284e3713e00SPaolo Bonzini 
285e3713e00SPaolo Bonzini /**
2860f08586cSStefan Hajnoczi  * aio_bh_schedule_oneshot_full: Allocate a new bottom half structure that will
2870f08586cSStefan Hajnoczi  * run only once and as soon as possible.
2880f08586cSStefan Hajnoczi  *
2890f08586cSStefan Hajnoczi  * @name: A human-readable identifier for debugging purposes.
2905b8bb359SPaolo Bonzini  */
2910f08586cSStefan Hajnoczi void aio_bh_schedule_oneshot_full(AioContext *ctx, QEMUBHFunc *cb, void *opaque,
2920f08586cSStefan Hajnoczi                                   const char *name);
2935b8bb359SPaolo Bonzini 
2945b8bb359SPaolo Bonzini /**
2950f08586cSStefan Hajnoczi  * aio_bh_schedule_oneshot: Allocate a new bottom half structure that will run
2960f08586cSStefan Hajnoczi  * only once and as soon as possible.
2970f08586cSStefan Hajnoczi  *
2980f08586cSStefan Hajnoczi  * A convenience wrapper for aio_bh_schedule_oneshot_full() that uses cb as the
2990f08586cSStefan Hajnoczi  * name string.
3000f08586cSStefan Hajnoczi  */
3010f08586cSStefan Hajnoczi #define aio_bh_schedule_oneshot(ctx, cb, opaque) \
3020f08586cSStefan Hajnoczi     aio_bh_schedule_oneshot_full((ctx), (cb), (opaque), (stringify(cb)))
3030f08586cSStefan Hajnoczi 
3040f08586cSStefan Hajnoczi /**
3050f08586cSStefan Hajnoczi  * aio_bh_new_full: Allocate a new bottom half structure.
306f627aab1SPaolo Bonzini  *
307f627aab1SPaolo Bonzini  * Bottom halves are lightweight callbacks whose invocation is guaranteed
308f627aab1SPaolo Bonzini  * to be wait-free, thread-safe and signal-safe.  The #QEMUBH structure
309f627aab1SPaolo Bonzini  * is opaque and must be allocated prior to its use.
3100f08586cSStefan Hajnoczi  *
3110f08586cSStefan Hajnoczi  * @name: A human-readable identifier for debugging purposes.
3129c86c97fSAlexander Bulekov  * @reentrancy_guard: A guard set when entering a cb to prevent
3139c86c97fSAlexander Bulekov  * device-reentrancy issues
314f627aab1SPaolo Bonzini  */
3150f08586cSStefan Hajnoczi QEMUBH *aio_bh_new_full(AioContext *ctx, QEMUBHFunc *cb, void *opaque,
3169c86c97fSAlexander Bulekov                         const char *name, MemReentrancyGuard *reentrancy_guard);
3170f08586cSStefan Hajnoczi 
3180f08586cSStefan Hajnoczi /**
3190f08586cSStefan Hajnoczi  * aio_bh_new: Allocate a new bottom half structure
3200f08586cSStefan Hajnoczi  *
3210f08586cSStefan Hajnoczi  * A convenience wrapper for aio_bh_new_full() that uses the cb as the name
3220f08586cSStefan Hajnoczi  * string.
3230f08586cSStefan Hajnoczi  */
3240f08586cSStefan Hajnoczi #define aio_bh_new(ctx, cb, opaque) \
3259c86c97fSAlexander Bulekov     aio_bh_new_full((ctx), (cb), (opaque), (stringify(cb)), NULL)
3269c86c97fSAlexander Bulekov 
3279c86c97fSAlexander Bulekov /**
3289c86c97fSAlexander Bulekov  * aio_bh_new_guarded: Allocate a new bottom half structure with a
3299c86c97fSAlexander Bulekov  * reentrancy_guard
3309c86c97fSAlexander Bulekov  *
3319c86c97fSAlexander Bulekov  * A convenience wrapper for aio_bh_new_full() that uses the cb as the name
3329c86c97fSAlexander Bulekov  * string.
3339c86c97fSAlexander Bulekov  */
3349c86c97fSAlexander Bulekov #define aio_bh_new_guarded(ctx, cb, opaque, guard) \
3359c86c97fSAlexander Bulekov     aio_bh_new_full((ctx), (cb), (opaque), (stringify(cb)), guard)
336f627aab1SPaolo Bonzini 
337f627aab1SPaolo Bonzini /**
3382f4dc3c1SPaolo Bonzini  * aio_notify: Force processing of pending events.
3392f4dc3c1SPaolo Bonzini  *
3402f4dc3c1SPaolo Bonzini  * Similar to signaling a condition variable, aio_notify forces
341722f8d90SYaowei Bai  * aio_poll to exit, so that the next call will re-examine pending events.
342722f8d90SYaowei Bai  * The caller of aio_notify will usually call aio_poll again very soon,
3432f4dc3c1SPaolo Bonzini  * or go through another iteration of the GLib main loop.  Hence, aio_notify
3442f4dc3c1SPaolo Bonzini  * also has the side effect of recalculating the sets of file descriptors
3452f4dc3c1SPaolo Bonzini  * that the main loop waits for.
3462f4dc3c1SPaolo Bonzini  *
3472f4dc3c1SPaolo Bonzini  * Calling aio_notify is rarely necessary, because for example scheduling
3482f4dc3c1SPaolo Bonzini  * a bottom half calls it already.
3492f4dc3c1SPaolo Bonzini  */
3502f4dc3c1SPaolo Bonzini void aio_notify(AioContext *ctx);
3512f4dc3c1SPaolo Bonzini 
3522f4dc3c1SPaolo Bonzini /**
35305e514b1SPaolo Bonzini  * aio_notify_accept: Acknowledge receiving an aio_notify.
35405e514b1SPaolo Bonzini  *
35505e514b1SPaolo Bonzini  * aio_notify() uses an EventNotifier in order to wake up a sleeping
35605e514b1SPaolo Bonzini  * aio_poll() or g_main_context_iteration().  Calls to aio_notify() are
35705e514b1SPaolo Bonzini  * usually rare, but the AioContext has to clear the EventNotifier on
35805e514b1SPaolo Bonzini  * every aio_poll() or g_main_context_iteration() in order to avoid
35905e514b1SPaolo Bonzini  * busy waiting.  This event_notifier_test_and_clear() cannot be done
36005e514b1SPaolo Bonzini  * using the usual aio_context_set_event_notifier(), because it must
36105e514b1SPaolo Bonzini  * be done before processing all events (file descriptors, bottom halves,
36205e514b1SPaolo Bonzini  * timers).
36305e514b1SPaolo Bonzini  *
36405e514b1SPaolo Bonzini  * aio_notify_accept() is an optimized event_notifier_test_and_clear()
36505e514b1SPaolo Bonzini  * that is specific to an AioContext's notifier; it is used internally
36605e514b1SPaolo Bonzini  * to clear the EventNotifier only if aio_notify() had been called.
36705e514b1SPaolo Bonzini  */
36805e514b1SPaolo Bonzini void aio_notify_accept(AioContext *ctx);
36905e514b1SPaolo Bonzini 
37005e514b1SPaolo Bonzini /**
371df281b80SPavel Dovgalyuk  * aio_bh_call: Executes callback function of the specified BH.
372df281b80SPavel Dovgalyuk  */
373df281b80SPavel Dovgalyuk void aio_bh_call(QEMUBH *bh);
374df281b80SPavel Dovgalyuk 
375df281b80SPavel Dovgalyuk /**
376f627aab1SPaolo Bonzini  * aio_bh_poll: Poll bottom halves for an AioContext.
377f627aab1SPaolo Bonzini  *
378f627aab1SPaolo Bonzini  * These are internal functions used by the QEMU main loop.
379dcc772e2SLiu Ping Fan  * And notice that multiple occurrences of aio_bh_poll cannot
380dcc772e2SLiu Ping Fan  * be called concurrently
381f627aab1SPaolo Bonzini  */
382f627aab1SPaolo Bonzini int aio_bh_poll(AioContext *ctx);
383f627aab1SPaolo Bonzini 
384f627aab1SPaolo Bonzini /**
385f627aab1SPaolo Bonzini  * qemu_bh_schedule: Schedule a bottom half.
386f627aab1SPaolo Bonzini  *
387f627aab1SPaolo Bonzini  * Scheduling a bottom half interrupts the main loop and causes the
388f627aab1SPaolo Bonzini  * execution of the callback that was passed to qemu_bh_new.
389f627aab1SPaolo Bonzini  *
390f627aab1SPaolo Bonzini  * Bottom halves that are scheduled from a bottom half handler are instantly
391f627aab1SPaolo Bonzini  * invoked.  This can create an infinite loop if a bottom half handler
392f627aab1SPaolo Bonzini  * schedules itself.
393f627aab1SPaolo Bonzini  *
394f627aab1SPaolo Bonzini  * @bh: The bottom half to be scheduled.
395f627aab1SPaolo Bonzini  */
396f627aab1SPaolo Bonzini void qemu_bh_schedule(QEMUBH *bh);
397f627aab1SPaolo Bonzini 
398f627aab1SPaolo Bonzini /**
399f627aab1SPaolo Bonzini  * qemu_bh_cancel: Cancel execution of a bottom half.
400f627aab1SPaolo Bonzini  *
401f627aab1SPaolo Bonzini  * Canceling execution of a bottom half undoes the effect of calls to
402f627aab1SPaolo Bonzini  * qemu_bh_schedule without freeing its resources yet.  While cancellation
403f627aab1SPaolo Bonzini  * itself is also wait-free and thread-safe, it can of course race with the
404f627aab1SPaolo Bonzini  * loop that executes bottom halves unless you are holding the iothread
405f627aab1SPaolo Bonzini  * mutex.  This makes it mostly useless if you are not holding the mutex.
406f627aab1SPaolo Bonzini  *
407f627aab1SPaolo Bonzini  * @bh: The bottom half to be canceled.
408f627aab1SPaolo Bonzini  */
409f627aab1SPaolo Bonzini void qemu_bh_cancel(QEMUBH *bh);
410f627aab1SPaolo Bonzini 
411f627aab1SPaolo Bonzini /**
412f627aab1SPaolo Bonzini  *qemu_bh_delete: Cancel execution of a bottom half and free its resources.
413f627aab1SPaolo Bonzini  *
414f627aab1SPaolo Bonzini  * Deleting a bottom half frees the memory that was allocated for it by
415f627aab1SPaolo Bonzini  * qemu_bh_new.  It also implies canceling the bottom half if it was
416f627aab1SPaolo Bonzini  * scheduled.
417dcc772e2SLiu Ping Fan  * This func is async. The bottom half will do the delete action at the finial
418dcc772e2SLiu Ping Fan  * end.
419f627aab1SPaolo Bonzini  *
420f627aab1SPaolo Bonzini  * @bh: The bottom half to be deleted.
421f627aab1SPaolo Bonzini  */
422f627aab1SPaolo Bonzini void qemu_bh_delete(QEMUBH *bh);
423f627aab1SPaolo Bonzini 
424cd9ba1ebSPaolo Bonzini /* Return whether there are any pending callbacks from the GSource
425a3462c65SPaolo Bonzini  * attached to the AioContext, before g_poll is invoked.
426a3462c65SPaolo Bonzini  *
427a3462c65SPaolo Bonzini  * This is used internally in the implementation of the GSource.
428a3462c65SPaolo Bonzini  */
429a3462c65SPaolo Bonzini bool aio_prepare(AioContext *ctx);
430a3462c65SPaolo Bonzini 
431a3462c65SPaolo Bonzini /* Return whether there are any pending callbacks from the GSource
432a3462c65SPaolo Bonzini  * attached to the AioContext, after g_poll is invoked.
433cd9ba1ebSPaolo Bonzini  *
434cd9ba1ebSPaolo Bonzini  * This is used internally in the implementation of the GSource.
435cd9ba1ebSPaolo Bonzini  */
436cd9ba1ebSPaolo Bonzini bool aio_pending(AioContext *ctx);
437cd9ba1ebSPaolo Bonzini 
438e4c7e2d1SPaolo Bonzini /* Dispatch any pending callbacks from the GSource attached to the AioContext.
439e4c7e2d1SPaolo Bonzini  *
440e4c7e2d1SPaolo Bonzini  * This is used internally in the implementation of the GSource.
441e4c7e2d1SPaolo Bonzini  */
442a153bf52SPaolo Bonzini void aio_dispatch(AioContext *ctx);
443e4c7e2d1SPaolo Bonzini 
4447c0628b2SPaolo Bonzini /* Progress in completing AIO work to occur.  This can issue new pending
4457c0628b2SPaolo Bonzini  * aio as a result of executing I/O completion or bh callbacks.
446bcdc1857SPaolo Bonzini  *
447acfb23adSPaolo Bonzini  * Return whether any progress was made by executing AIO or bottom half
448acfb23adSPaolo Bonzini  * handlers.  If @blocking == true, this should always be true except
449acfb23adSPaolo Bonzini  * if someone called aio_notify.
4507c0628b2SPaolo Bonzini  *
4517c0628b2SPaolo Bonzini  * If there are no pending bottom halves, but there are pending AIO
4527c0628b2SPaolo Bonzini  * operations, it may not be possible to make any progress without
4537c0628b2SPaolo Bonzini  * blocking.  If @blocking is true, this function will wait until one
4547c0628b2SPaolo Bonzini  * or more AIO events have completed, to ensure something has moved
4557c0628b2SPaolo Bonzini  * before returning.
4567c0628b2SPaolo Bonzini  */
457c428b392SPaolo Bonzini bool no_coroutine_fn aio_poll(AioContext *ctx, bool blocking);
458a76bab49Saliguori 
459a76bab49Saliguori /* Register a file descriptor and associated callbacks.  Behaves very similarly
4606484e422SFam Zheng  * to qemu_set_fd_handler.  Unlike qemu_set_fd_handler, these callbacks will
46187f68d31SPaolo Bonzini  * be invoked when using aio_poll().
462a76bab49Saliguori  *
463a76bab49Saliguori  * Code that invokes AIO completion functions should rely on this function
464a76bab49Saliguori  * instead of qemu_set_fd_handler[2].
465a76bab49Saliguori  */
466a915f4bcSPaolo Bonzini void aio_set_fd_handler(AioContext *ctx,
467a915f4bcSPaolo Bonzini                         int fd,
468a76bab49Saliguori                         IOHandler *io_read,
469a76bab49Saliguori                         IOHandler *io_write,
470f6a51c84SStefan Hajnoczi                         AioPollFn *io_poll,
471826cc324SStefan Hajnoczi                         IOHandler *io_poll_ready,
472a76bab49Saliguori                         void *opaque);
4739958c351SPaolo Bonzini 
4749958c351SPaolo Bonzini /* Register an event notifier and associated callbacks.  Behaves very similarly
4759958c351SPaolo Bonzini  * to event_notifier_set_handler.  Unlike event_notifier_set_handler, these callbacks
47687f68d31SPaolo Bonzini  * will be invoked when using aio_poll().
4779958c351SPaolo Bonzini  *
4789958c351SPaolo Bonzini  * Code that invokes AIO completion functions should rely on this function
4799958c351SPaolo Bonzini  * instead of event_notifier_set_handler.
4809958c351SPaolo Bonzini  */
481a915f4bcSPaolo Bonzini void aio_set_event_notifier(AioContext *ctx,
482a915f4bcSPaolo Bonzini                             EventNotifier *notifier,
483f6a51c84SStefan Hajnoczi                             EventNotifierHandler *io_read,
484826cc324SStefan Hajnoczi                             AioPollFn *io_poll,
485826cc324SStefan Hajnoczi                             EventNotifierHandler *io_poll_ready);
486a915f4bcSPaolo Bonzini 
4875bdbaebcSHanna Czenczek /*
4885bdbaebcSHanna Czenczek  * Set polling begin/end callbacks for an event notifier that has already been
489684e508cSStefan Hajnoczi  * registered with aio_set_event_notifier.  Do nothing if the event notifier is
490684e508cSStefan Hajnoczi  * not registered.
4915bdbaebcSHanna Czenczek  *
4925bdbaebcSHanna Czenczek  * Note that if the io_poll_end() callback (or the entire notifier) is removed
4935bdbaebcSHanna Czenczek  * during polling, it will not be called, so an io_poll_begin() is not
4945bdbaebcSHanna Czenczek  * necessarily always followed by an io_poll_end().
495684e508cSStefan Hajnoczi  */
496684e508cSStefan Hajnoczi void aio_set_event_notifier_poll(AioContext *ctx,
497684e508cSStefan Hajnoczi                                  EventNotifier *notifier,
498684e508cSStefan Hajnoczi                                  EventNotifierHandler *io_poll_begin,
499684e508cSStefan Hajnoczi                                  EventNotifierHandler *io_poll_end);
500684e508cSStefan Hajnoczi 
501e3713e00SPaolo Bonzini /* Return a GSource that lets the main loop poll the file descriptors attached
502e3713e00SPaolo Bonzini  * to this AioContext.
503e3713e00SPaolo Bonzini  */
504e3713e00SPaolo Bonzini GSource *aio_get_g_source(AioContext *ctx);
505e3713e00SPaolo Bonzini 
506dc67daeeSMaciej S. Szmigiero /* Return the ThreadPoolAio bound to this AioContext */
507dc67daeeSMaciej S. Szmigiero struct ThreadPoolAio *aio_get_thread_pool(AioContext *ctx);
5089b34277dSStefan Hajnoczi 
509ed6e2161SNishanth Aravamudan /* Setup the LinuxAioState bound to this AioContext */
510ed6e2161SNishanth Aravamudan struct LinuxAioState *aio_setup_linux_aio(AioContext *ctx, Error **errp);
511ed6e2161SNishanth Aravamudan 
5120187f5c9SPaolo Bonzini /* Return the LinuxAioState bound to this AioContext */
5130187f5c9SPaolo Bonzini struct LinuxAioState *aio_get_linux_aio(AioContext *ctx);
5140187f5c9SPaolo Bonzini 
5156663a0a3SAarushi Mehta /* Setup the LuringState bound to this AioContext */
5163cbc17eeSPaolo Bonzini LuringState *aio_setup_linux_io_uring(AioContext *ctx, Error **errp);
5176663a0a3SAarushi Mehta 
5186663a0a3SAarushi Mehta /* Return the LuringState bound to this AioContext */
5193cbc17eeSPaolo Bonzini LuringState *aio_get_linux_io_uring(AioContext *ctx);
5204e29e831SAlex Bligh /**
52189a603a0SArtem Pisarenko  * aio_timer_new_with_attrs:
52289a603a0SArtem Pisarenko  * @ctx: the aio context
52389a603a0SArtem Pisarenko  * @type: the clock type
52489a603a0SArtem Pisarenko  * @scale: the scale
52589a603a0SArtem Pisarenko  * @attributes: 0, or one to multiple OR'ed QEMU_TIMER_ATTR_<id> values
52689a603a0SArtem Pisarenko  *              to assign
52789a603a0SArtem Pisarenko  * @cb: the callback to call on timer expiry
52889a603a0SArtem Pisarenko  * @opaque: the opaque pointer to pass to the callback
52989a603a0SArtem Pisarenko  *
53089a603a0SArtem Pisarenko  * Allocate a new timer (with attributes) attached to the context @ctx.
53189a603a0SArtem Pisarenko  * The function is responsible for memory allocation.
53289a603a0SArtem Pisarenko  *
53389a603a0SArtem Pisarenko  * The preferred interface is aio_timer_init or aio_timer_init_with_attrs.
53489a603a0SArtem Pisarenko  * Use that unless you really need dynamic memory allocation.
53589a603a0SArtem Pisarenko  *
53689a603a0SArtem Pisarenko  * Returns: a pointer to the new timer
53789a603a0SArtem Pisarenko  */
aio_timer_new_with_attrs(AioContext * ctx,QEMUClockType type,int scale,int attributes,QEMUTimerCB * cb,void * opaque)53889a603a0SArtem Pisarenko static inline QEMUTimer *aio_timer_new_with_attrs(AioContext *ctx,
53989a603a0SArtem Pisarenko                                                   QEMUClockType type,
54089a603a0SArtem Pisarenko                                                   int scale, int attributes,
54189a603a0SArtem Pisarenko                                                   QEMUTimerCB *cb, void *opaque)
54289a603a0SArtem Pisarenko {
54389a603a0SArtem Pisarenko     return timer_new_full(&ctx->tlg, type, scale, attributes, cb, opaque);
54489a603a0SArtem Pisarenko }
54589a603a0SArtem Pisarenko 
54689a603a0SArtem Pisarenko /**
5474e29e831SAlex Bligh  * aio_timer_new:
5484e29e831SAlex Bligh  * @ctx: the aio context
5494e29e831SAlex Bligh  * @type: the clock type
5504e29e831SAlex Bligh  * @scale: the scale
5514e29e831SAlex Bligh  * @cb: the callback to call on timer expiry
5524e29e831SAlex Bligh  * @opaque: the opaque pointer to pass to the callback
5534e29e831SAlex Bligh  *
5544e29e831SAlex Bligh  * Allocate a new timer attached to the context @ctx.
55589a603a0SArtem Pisarenko  * See aio_timer_new_with_attrs for details.
5564e29e831SAlex Bligh  *
5574e29e831SAlex Bligh  * Returns: a pointer to the new timer
5584e29e831SAlex Bligh  */
aio_timer_new(AioContext * ctx,QEMUClockType type,int scale,QEMUTimerCB * cb,void * opaque)5594e29e831SAlex Bligh static inline QEMUTimer *aio_timer_new(AioContext *ctx, QEMUClockType type,
5604e29e831SAlex Bligh                                        int scale,
5614e29e831SAlex Bligh                                        QEMUTimerCB *cb, void *opaque)
5624e29e831SAlex Bligh {
56389a603a0SArtem Pisarenko     return timer_new_full(&ctx->tlg, type, scale, 0, cb, opaque);
56489a603a0SArtem Pisarenko }
56589a603a0SArtem Pisarenko 
56689a603a0SArtem Pisarenko /**
56789a603a0SArtem Pisarenko  * aio_timer_init_with_attrs:
56889a603a0SArtem Pisarenko  * @ctx: the aio context
56989a603a0SArtem Pisarenko  * @ts: the timer
57089a603a0SArtem Pisarenko  * @type: the clock type
57189a603a0SArtem Pisarenko  * @scale: the scale
57289a603a0SArtem Pisarenko  * @attributes: 0, or one to multiple OR'ed QEMU_TIMER_ATTR_<id> values
57389a603a0SArtem Pisarenko  *              to assign
57489a603a0SArtem Pisarenko  * @cb: the callback to call on timer expiry
57589a603a0SArtem Pisarenko  * @opaque: the opaque pointer to pass to the callback
57689a603a0SArtem Pisarenko  *
57789a603a0SArtem Pisarenko  * Initialise a new timer (with attributes) attached to the context @ctx.
57889a603a0SArtem Pisarenko  * The caller is responsible for memory allocation.
57989a603a0SArtem Pisarenko  */
aio_timer_init_with_attrs(AioContext * ctx,QEMUTimer * ts,QEMUClockType type,int scale,int attributes,QEMUTimerCB * cb,void * opaque)58089a603a0SArtem Pisarenko static inline void aio_timer_init_with_attrs(AioContext *ctx,
58189a603a0SArtem Pisarenko                                              QEMUTimer *ts, QEMUClockType type,
58289a603a0SArtem Pisarenko                                              int scale, int attributes,
58389a603a0SArtem Pisarenko                                              QEMUTimerCB *cb, void *opaque)
58489a603a0SArtem Pisarenko {
58589a603a0SArtem Pisarenko     timer_init_full(ts, &ctx->tlg, type, scale, attributes, cb, opaque);
5864e29e831SAlex Bligh }
5874e29e831SAlex Bligh 
5884e29e831SAlex Bligh /**
5894e29e831SAlex Bligh  * aio_timer_init:
5904e29e831SAlex Bligh  * @ctx: the aio context
5914e29e831SAlex Bligh  * @ts: the timer
5924e29e831SAlex Bligh  * @type: the clock type
5934e29e831SAlex Bligh  * @scale: the scale
5944e29e831SAlex Bligh  * @cb: the callback to call on timer expiry
5954e29e831SAlex Bligh  * @opaque: the opaque pointer to pass to the callback
5964e29e831SAlex Bligh  *
5974e29e831SAlex Bligh  * Initialise a new timer attached to the context @ctx.
59889a603a0SArtem Pisarenko  * See aio_timer_init_with_attrs for details.
5994e29e831SAlex Bligh  */
aio_timer_init(AioContext * ctx,QEMUTimer * ts,QEMUClockType type,int scale,QEMUTimerCB * cb,void * opaque)6004e29e831SAlex Bligh static inline void aio_timer_init(AioContext *ctx,
6014e29e831SAlex Bligh                                   QEMUTimer *ts, QEMUClockType type,
6024e29e831SAlex Bligh                                   int scale,
6034e29e831SAlex Bligh                                   QEMUTimerCB *cb, void *opaque)
6044e29e831SAlex Bligh {
60589a603a0SArtem Pisarenko     timer_init_full(ts, &ctx->tlg, type, scale, 0, cb, opaque);
6064e29e831SAlex Bligh }
6074e29e831SAlex Bligh 
608845ca10dSPaolo Bonzini /**
609845ca10dSPaolo Bonzini  * aio_compute_timeout:
610845ca10dSPaolo Bonzini  * @ctx: the aio context
611845ca10dSPaolo Bonzini  *
612845ca10dSPaolo Bonzini  * Compute the timeout that a blocking aio_poll should use.
613845ca10dSPaolo Bonzini  */
614845ca10dSPaolo Bonzini int64_t aio_compute_timeout(AioContext *ctx);
615845ca10dSPaolo Bonzini 
616c1e1e5faSFam Zheng /**
6170c330a73SPaolo Bonzini  * aio_co_schedule:
6180c330a73SPaolo Bonzini  * @ctx: the aio context
6190c330a73SPaolo Bonzini  * @co: the coroutine
6200c330a73SPaolo Bonzini  *
6210c330a73SPaolo Bonzini  * Start a coroutine on a remote AioContext.
6220c330a73SPaolo Bonzini  *
6230c330a73SPaolo Bonzini  * The coroutine must not be entered by anyone else while aio_co_schedule()
6240c330a73SPaolo Bonzini  * is active.  In addition the coroutine must have yielded unless ctx
6250c330a73SPaolo Bonzini  * is the context in which the coroutine is running (i.e. the value of
6260c330a73SPaolo Bonzini  * qemu_get_current_aio_context() from the coroutine itself).
6270c330a73SPaolo Bonzini  */
62843695601SMarkus Armbruster void aio_co_schedule(AioContext *ctx, Coroutine *co);
6290c330a73SPaolo Bonzini 
6300c330a73SPaolo Bonzini /**
63126b0b698SKevin Wolf  * aio_co_reschedule_self:
63226b0b698SKevin Wolf  * @new_ctx: the new context
63326b0b698SKevin Wolf  *
63426b0b698SKevin Wolf  * Move the currently running coroutine to new_ctx. If the coroutine is already
63526b0b698SKevin Wolf  * running in new_ctx, do nothing.
636e669e800SStefan Hajnoczi  *
637e669e800SStefan Hajnoczi  * Note that this function cannot reschedule from iohandler_ctx to
638e669e800SStefan Hajnoczi  * qemu_aio_context.
63926b0b698SKevin Wolf  */
64026b0b698SKevin Wolf void coroutine_fn aio_co_reschedule_self(AioContext *new_ctx);
64126b0b698SKevin Wolf 
64226b0b698SKevin Wolf /**
6430c330a73SPaolo Bonzini  * aio_co_wake:
6440c330a73SPaolo Bonzini  * @co: the coroutine
6450c330a73SPaolo Bonzini  *
6460c330a73SPaolo Bonzini  * Restart a coroutine on the AioContext where it was running last, thus
6470c330a73SPaolo Bonzini  * preventing coroutines from jumping from one context to another when they
6480c330a73SPaolo Bonzini  * go to sleep.
6490c330a73SPaolo Bonzini  *
6500c330a73SPaolo Bonzini  * aio_co_wake may be executed either in coroutine or non-coroutine
6510c330a73SPaolo Bonzini  * context.  The coroutine must not be entered by anyone else while
6520c330a73SPaolo Bonzini  * aio_co_wake() is active.
6530c330a73SPaolo Bonzini  */
65443695601SMarkus Armbruster void aio_co_wake(Coroutine *co);
6550c330a73SPaolo Bonzini 
6560c330a73SPaolo Bonzini /**
6578865852eSFam Zheng  * aio_co_enter:
6588865852eSFam Zheng  * @ctx: the context to run the coroutine
6598865852eSFam Zheng  * @co: the coroutine to run
6608865852eSFam Zheng  *
6618865852eSFam Zheng  * Enter a coroutine in the specified AioContext.
6628865852eSFam Zheng  */
66343695601SMarkus Armbruster void aio_co_enter(AioContext *ctx, Coroutine *co);
6648865852eSFam Zheng 
6658865852eSFam Zheng /**
666e4370165SPaolo Bonzini  * Return the AioContext whose event loop runs in the current thread.
667e4370165SPaolo Bonzini  *
668e4370165SPaolo Bonzini  * If called from an IOThread this will be the IOThread's AioContext.  If
6695f50be9bSPaolo Bonzini  * called from the main thread or with the "big QEMU lock" taken it
6705f50be9bSPaolo Bonzini  * will be the main loop AioContext.
671e669e800SStefan Hajnoczi  *
672e669e800SStefan Hajnoczi  * Note that the return value is never the main loop's iohandler_ctx and the
673e669e800SStefan Hajnoczi  * return value is the main loop AioContext instead.
674e4370165SPaolo Bonzini  */
675e4370165SPaolo Bonzini AioContext *qemu_get_current_aio_context(void);
676e4370165SPaolo Bonzini 
6775f50be9bSPaolo Bonzini void qemu_set_current_aio_context(AioContext *ctx);
6785f50be9bSPaolo Bonzini 
679e4370165SPaolo Bonzini /**
68037fcee5dSFam Zheng  * aio_context_setup:
68137fcee5dSFam Zheng  * @ctx: the aio context
68237fcee5dSFam Zheng  *
68337fcee5dSFam Zheng  * Initialize the aio context.
68437fcee5dSFam Zheng  */
6857e003465SCao jin void aio_context_setup(AioContext *ctx);
68637fcee5dSFam Zheng 
6874a1cba38SStefan Hajnoczi /**
688cd0a6d2bSJie Wang  * aio_context_destroy:
689cd0a6d2bSJie Wang  * @ctx: the aio context
690cd0a6d2bSJie Wang  *
691cd0a6d2bSJie Wang  * Destroy the aio context.
692cd0a6d2bSJie Wang  */
693cd0a6d2bSJie Wang void aio_context_destroy(AioContext *ctx);
694cd0a6d2bSJie Wang 
695ba607ca8SStefan Hajnoczi /* Used internally, do not call outside AioContext code */
696ba607ca8SStefan Hajnoczi void aio_context_use_g_source(AioContext *ctx);
697ba607ca8SStefan Hajnoczi 
698cd0a6d2bSJie Wang /**
6994a1cba38SStefan Hajnoczi  * aio_context_set_poll_params:
7004a1cba38SStefan Hajnoczi  * @ctx: the aio context
7014a1cba38SStefan Hajnoczi  * @max_ns: how long to busy poll for, in nanoseconds
70282a41186SStefan Hajnoczi  * @grow: polling time growth factor
70382a41186SStefan Hajnoczi  * @shrink: polling time shrink factor
7044a1cba38SStefan Hajnoczi  *
7054a1cba38SStefan Hajnoczi  * Poll mode can be disabled by setting poll_max_ns to 0.
7064a1cba38SStefan Hajnoczi  */
7074a1cba38SStefan Hajnoczi void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
70882a41186SStefan Hajnoczi                                  int64_t grow, int64_t shrink,
7094a1cba38SStefan Hajnoczi                                  Error **errp);
7104a1cba38SStefan Hajnoczi 
7111793ad02SStefano Garzarella /**
7121793ad02SStefano Garzarella  * aio_context_set_aio_params:
7131793ad02SStefano Garzarella  * @ctx: the aio context
7141793ad02SStefano Garzarella  * @max_batch: maximum number of requests in a batch, 0 means that the
7151793ad02SStefano Garzarella  *             engine will use its default
7161793ad02SStefano Garzarella  */
717897a06c6SPhilippe Mathieu-Daudé void aio_context_set_aio_params(AioContext *ctx, int64_t max_batch);
7181793ad02SStefano Garzarella 
71971ad4713SNicolas Saenz Julienne /**
72071ad4713SNicolas Saenz Julienne  * aio_context_set_thread_pool_params:
72171ad4713SNicolas Saenz Julienne  * @ctx: the aio context
72271ad4713SNicolas Saenz Julienne  * @min: min number of threads to have readily available in the thread pool
72371ad4713SNicolas Saenz Julienne  * @min: max number of threads the thread pool can contain
72471ad4713SNicolas Saenz Julienne  */
72571ad4713SNicolas Saenz Julienne void aio_context_set_thread_pool_params(AioContext *ctx, int64_t min,
72671ad4713SNicolas Saenz Julienne                                         int64_t max, Error **errp);
727a76bab49Saliguori #endif
728