xref: /openbmc/qemu/include/block/aio.h (revision 9c86c97f12c060bf7484dd931f38634e166a81f0)
1737e150eSPaolo Bonzini /*
2737e150eSPaolo Bonzini  * QEMU aio implementation
3737e150eSPaolo Bonzini  *
4737e150eSPaolo Bonzini  * Copyright IBM, Corp. 2008
5737e150eSPaolo Bonzini  *
6737e150eSPaolo Bonzini  * Authors:
7737e150eSPaolo Bonzini  *  Anthony Liguori   <aliguori@us.ibm.com>
8737e150eSPaolo Bonzini  *
9737e150eSPaolo Bonzini  * This work is licensed under the terms of the GNU GPL, version 2.  See
10737e150eSPaolo Bonzini  * the COPYING file in the top-level directory.
11737e150eSPaolo Bonzini  *
12737e150eSPaolo Bonzini  */
13737e150eSPaolo Bonzini 
14737e150eSPaolo Bonzini #ifndef QEMU_AIO_H
15737e150eSPaolo Bonzini #define QEMU_AIO_H
16737e150eSPaolo Bonzini 
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"
23dcc772e2SLiu Ping Fan #include "qemu/thread.h"
24dae21b98SAlex Bligh #include "qemu/timer.h"
25aead9dc9SPaolo Bonzini #include "block/graph-lock.h"
26*9c86c97fSAlexander Bulekov #include "hw/qdev-core.h"
27*9c86c97fSAlexander Bulekov 
28737e150eSPaolo Bonzini 
297c84b1b8SMarkus Armbruster typedef struct BlockAIOCB BlockAIOCB;
30097310b5SMarkus Armbruster typedef void BlockCompletionFunc(void *opaque, int ret);
31737e150eSPaolo Bonzini 
32737e150eSPaolo Bonzini typedef struct AIOCBInfo {
337c84b1b8SMarkus Armbruster     void (*cancel_async)(BlockAIOCB *acb);
347c84b1b8SMarkus Armbruster     AioContext *(*get_aio_context)(BlockAIOCB *acb);
35737e150eSPaolo Bonzini     size_t aiocb_size;
36737e150eSPaolo Bonzini } AIOCBInfo;
37737e150eSPaolo Bonzini 
387c84b1b8SMarkus Armbruster struct BlockAIOCB {
39737e150eSPaolo Bonzini     const AIOCBInfo *aiocb_info;
40737e150eSPaolo Bonzini     BlockDriverState *bs;
41097310b5SMarkus Armbruster     BlockCompletionFunc *cb;
42737e150eSPaolo Bonzini     void *opaque;
43f197fe2bSFam Zheng     int refcnt;
44737e150eSPaolo Bonzini };
45737e150eSPaolo Bonzini 
46737e150eSPaolo Bonzini 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);
50737e150eSPaolo Bonzini 
51737e150eSPaolo Bonzini typedef struct AioHandler AioHandler;
524749079cSStefan Hajnoczi typedef QLIST_HEAD(, AioHandler) AioHandlerList;
53737e150eSPaolo Bonzini typedef void QEMUBHFunc(void *opaque);
54f6a51c84SStefan Hajnoczi typedef bool AioPollFn(void *opaque);
55737e150eSPaolo Bonzini typedef void IOHandler(void *opaque);
56737e150eSPaolo Bonzini 
570187f5c9SPaolo Bonzini struct ThreadPool;
580187f5c9SPaolo Bonzini struct LinuxAioState;
596663a0a3SAarushi Mehta struct 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 
1266a1751b7SAlex Bligh struct AioContext {
127737e150eSPaolo Bonzini     GSource source;
128737e150eSPaolo Bonzini 
1297c690fd1SPaolo Bonzini     /* Used by AioContext users to protect from multi-threaded access.  */
1303fe71223SPaolo Bonzini     QemuRecMutex lock;
13198563fc3SStefan Hajnoczi 
132aead9dc9SPaolo Bonzini     /*
133aead9dc9SPaolo Bonzini      * Keep track of readers and writers of the block layer graph.
134aead9dc9SPaolo Bonzini      * This is essential to avoid performing additions and removal
135aead9dc9SPaolo Bonzini      * of nodes and edges from block graph while some
136aead9dc9SPaolo Bonzini      * other thread is traversing it.
137aead9dc9SPaolo Bonzini      */
138aead9dc9SPaolo Bonzini     BdrvGraphRWlock *bdrv_graph;
139aead9dc9SPaolo Bonzini 
1407c690fd1SPaolo Bonzini     /* The list of registered AIO handlers.  Protected by ctx->list_lock. */
1414749079cSStefan Hajnoczi     AioHandlerList aio_handlers;
1424749079cSStefan Hajnoczi 
1434749079cSStefan Hajnoczi     /* The list of AIO handlers to be deleted.  Protected by ctx->list_lock. */
1444749079cSStefan Hajnoczi     AioHandlerList deleted_aio_handlers;
145737e150eSPaolo Bonzini 
146eabc9779SPaolo Bonzini     /* Used to avoid unnecessary event_notifier_set calls in aio_notify;
1473c18a92dSPaolo Bonzini      * only written from the AioContext home thread, or under the BQL in
1483c18a92dSPaolo Bonzini      * the case of the main AioContext.  However, it is read from any
1493c18a92dSPaolo Bonzini      * thread so it is still accessed with atomic primitives.
1503c18a92dSPaolo Bonzini      *
1513c18a92dSPaolo Bonzini      * If this field is 0, everything (file descriptors, bottom halves,
1523c18a92dSPaolo Bonzini      * timers) will be re-evaluated before the next blocking poll() or
1533c18a92dSPaolo Bonzini      * io_uring wait; therefore, the event_notifier_set call can be
1543c18a92dSPaolo Bonzini      * skipped.  If it is non-zero, you may need to wake up a concurrent
1553c18a92dSPaolo Bonzini      * aio_poll or the glib main event loop, making event_notifier_set
1563c18a92dSPaolo Bonzini      * necessary.
157eabc9779SPaolo Bonzini      *
158eabc9779SPaolo Bonzini      * Bit 0 is reserved for GSource usage of the AioContext, and is 1
15954a16a63SCao jin      * between a call to aio_ctx_prepare and the next call to aio_ctx_check.
160eabc9779SPaolo Bonzini      * Bits 1-31 simply count the number of active calls to aio_poll
161eabc9779SPaolo Bonzini      * that are in the prepare or poll phase.
162eabc9779SPaolo Bonzini      *
163eabc9779SPaolo Bonzini      * The GSource and aio_poll must use a different mechanism because
164eabc9779SPaolo Bonzini      * there is no certainty that a call to GSource's prepare callback
165eabc9779SPaolo Bonzini      * (via g_main_context_prepare) is indeed followed by check and
166eabc9779SPaolo Bonzini      * dispatch.  It's not clear whether this would be a bug, but let's
167eabc9779SPaolo Bonzini      * play safe and allow it---it will just cause extra calls to
168eabc9779SPaolo Bonzini      * event_notifier_set until the next call to dispatch.
169eabc9779SPaolo Bonzini      *
170eabc9779SPaolo Bonzini      * Instead, the aio_poll calls include both the prepare and the
171eabc9779SPaolo Bonzini      * dispatch phase, hence a simple counter is enough for them.
1720ceb849bSPaolo Bonzini      */
173eabc9779SPaolo Bonzini     uint32_t notify_me;
1740ceb849bSPaolo Bonzini 
1757c690fd1SPaolo Bonzini     /* A lock to protect between QEMUBH and AioHandler adders and deleter,
1767c690fd1SPaolo Bonzini      * and to ensure that no callbacks are removed while we're walking and
1777c690fd1SPaolo Bonzini      * dispatching them.
178d7c99a12SPaolo Bonzini      */
179d7c99a12SPaolo Bonzini     QemuLockCnt list_lock;
1800ceb849bSPaolo Bonzini 
1818c6b0356SStefan Hajnoczi     /* Bottom Halves pending aio_bh_poll() processing */
1828c6b0356SStefan Hajnoczi     BHList bh_list;
1838c6b0356SStefan Hajnoczi 
1848c6b0356SStefan Hajnoczi     /* Chained BH list slices for each nested aio_bh_poll() call */
1858c6b0356SStefan Hajnoczi     QSIMPLEQ_HEAD(, BHListSlice) bh_slice_list;
186737e150eSPaolo Bonzini 
18705e514b1SPaolo Bonzini     /* Used by aio_notify.
18805e514b1SPaolo Bonzini      *
18905e514b1SPaolo Bonzini      * "notified" is used to avoid expensive event_notifier_test_and_clear
19005e514b1SPaolo Bonzini      * calls.  When it is clear, the EventNotifier is clear, or one thread
19105e514b1SPaolo Bonzini      * is going to clear "notified" before processing more events.  False
19205e514b1SPaolo Bonzini      * positives are possible, i.e. "notified" could be set even though the
19305e514b1SPaolo Bonzini      * EventNotifier is clear.
19405e514b1SPaolo Bonzini      *
19505e514b1SPaolo Bonzini      * Note that event_notifier_set *cannot* be optimized the same way.  For
19605e514b1SPaolo Bonzini      * more information on the problem that would result, see "#ifdef BUG2"
19705e514b1SPaolo Bonzini      * in the docs/aio_notify_accept.promela formal model.
19805e514b1SPaolo Bonzini      */
19905e514b1SPaolo Bonzini     bool notified;
200737e150eSPaolo Bonzini     EventNotifier notifier;
2016b5f8762SStefan Hajnoczi 
2020c330a73SPaolo Bonzini     QSLIST_HEAD(, Coroutine) scheduled_coroutines;
2030c330a73SPaolo Bonzini     QEMUBH *co_schedule_bh;
2040c330a73SPaolo Bonzini 
20571ad4713SNicolas Saenz Julienne     int thread_pool_min;
20671ad4713SNicolas Saenz Julienne     int thread_pool_max;
2077c690fd1SPaolo Bonzini     /* Thread pool for performing work and receiving completion callbacks.
2087c690fd1SPaolo Bonzini      * Has its own locking.
2097c690fd1SPaolo Bonzini      */
2109b34277dSStefan Hajnoczi     struct ThreadPool *thread_pool;
211dae21b98SAlex Bligh 
2120187f5c9SPaolo Bonzini #ifdef CONFIG_LINUX_AIO
2130187f5c9SPaolo Bonzini     struct LinuxAioState *linux_aio;
2140187f5c9SPaolo Bonzini #endif
2156663a0a3SAarushi Mehta #ifdef CONFIG_LINUX_IO_URING
2166663a0a3SAarushi Mehta     struct LuringState *linux_io_uring;
21773fd282eSStefan Hajnoczi 
21873fd282eSStefan Hajnoczi     /* State for file descriptor monitoring using Linux io_uring */
21973fd282eSStefan Hajnoczi     struct io_uring fdmon_io_uring;
22073fd282eSStefan Hajnoczi     AioHandlerSList submit_list;
2216663a0a3SAarushi Mehta #endif
2220187f5c9SPaolo Bonzini 
2237c690fd1SPaolo Bonzini     /* TimerLists for calling timers - one per clock type.  Has its own
2247c690fd1SPaolo Bonzini      * locking.
2257c690fd1SPaolo Bonzini      */
226dae21b98SAlex Bligh     QEMUTimerListGroup tlg;
227c1e1e5faSFam Zheng 
228c1e1e5faSFam Zheng     int external_disable_cnt;
229fbe3fc5cSFam Zheng 
2304a1cba38SStefan Hajnoczi     /* Number of AioHandlers without .io_poll() */
2314a1cba38SStefan Hajnoczi     int poll_disable_cnt;
2324a1cba38SStefan Hajnoczi 
23382a41186SStefan Hajnoczi     /* Polling mode parameters */
23482a41186SStefan Hajnoczi     int64_t poll_ns;        /* current polling time in nanoseconds */
23582a41186SStefan Hajnoczi     int64_t poll_max_ns;    /* maximum polling time in nanoseconds */
23682a41186SStefan Hajnoczi     int64_t poll_grow;      /* polling time growth factor */
23782a41186SStefan Hajnoczi     int64_t poll_shrink;    /* polling time shrink factor */
2384a1cba38SStefan Hajnoczi 
2391793ad02SStefano Garzarella     /* AIO engine parameters */
2401793ad02SStefano Garzarella     int64_t aio_max_batch;  /* maximum number of requests in a batch */
2411793ad02SStefano Garzarella 
242d37d0e36SStefan Hajnoczi     /*
243d37d0e36SStefan Hajnoczi      * List of handlers participating in userspace polling.  Protected by
244d37d0e36SStefan Hajnoczi      * ctx->list_lock.  Iterated and modified mostly by the event loop thread
245d37d0e36SStefan Hajnoczi      * from aio_poll() with ctx->list_lock incremented.  aio_set_fd_handler()
246d37d0e36SStefan Hajnoczi      * only touches the list to delete nodes if ctx->list_lock's count is zero.
247d37d0e36SStefan Hajnoczi      */
248d37d0e36SStefan Hajnoczi     AioHandlerList poll_aio_handlers;
249d37d0e36SStefan Hajnoczi 
250684e508cSStefan Hajnoczi     /* Are we in polling mode or monitoring file descriptors? */
251684e508cSStefan Hajnoczi     bool poll_started;
252684e508cSStefan Hajnoczi 
253fbe3fc5cSFam Zheng     /* epoll(7) state used when built with CONFIG_EPOLL */
254fbe3fc5cSFam Zheng     int epollfd;
2551f050a46SStefan Hajnoczi 
2561f050a46SStefan Hajnoczi     const FDMonOps *fdmon_ops;
2576a1751b7SAlex Bligh };
258737e150eSPaolo Bonzini 
259737e150eSPaolo Bonzini /**
260737e150eSPaolo Bonzini  * aio_context_new: Allocate a new AioContext.
261737e150eSPaolo Bonzini  *
262737e150eSPaolo Bonzini  * AioContext provide a mini event-loop that can be waited on synchronously.
263737e150eSPaolo Bonzini  * They also provide bottom halves, a service to execute a piece of code
264737e150eSPaolo Bonzini  * as soon as possible.
265737e150eSPaolo Bonzini  */
2662f78e491SChrysostomos Nanakos AioContext *aio_context_new(Error **errp);
267737e150eSPaolo Bonzini 
268737e150eSPaolo Bonzini /**
269737e150eSPaolo Bonzini  * aio_context_ref:
270737e150eSPaolo Bonzini  * @ctx: The AioContext to operate on.
271737e150eSPaolo Bonzini  *
272737e150eSPaolo Bonzini  * Add a reference to an AioContext.
273737e150eSPaolo Bonzini  */
274737e150eSPaolo Bonzini void aio_context_ref(AioContext *ctx);
275737e150eSPaolo Bonzini 
276737e150eSPaolo Bonzini /**
277737e150eSPaolo Bonzini  * aio_context_unref:
278737e150eSPaolo Bonzini  * @ctx: The AioContext to operate on.
279737e150eSPaolo Bonzini  *
280737e150eSPaolo Bonzini  * Drop a reference to an AioContext.
281737e150eSPaolo Bonzini  */
282737e150eSPaolo Bonzini void aio_context_unref(AioContext *ctx);
283737e150eSPaolo Bonzini 
28498563fc3SStefan Hajnoczi /* Take ownership of the AioContext.  If the AioContext will be shared between
28549110174SPaolo Bonzini  * threads, and a thread does not want to be interrupted, it will have to
28649110174SPaolo Bonzini  * take ownership around calls to aio_poll().  Otherwise, aio_poll()
28749110174SPaolo Bonzini  * automatically takes care of calling aio_context_acquire and
28849110174SPaolo Bonzini  * aio_context_release.
28998563fc3SStefan Hajnoczi  *
2907c690fd1SPaolo Bonzini  * Note that this is separate from bdrv_drained_begin/bdrv_drained_end.  A
2917c690fd1SPaolo Bonzini  * thread still has to call those to avoid being interrupted by the guest.
2927c690fd1SPaolo Bonzini  *
2937c690fd1SPaolo Bonzini  * Bottom halves, timers and callbacks can be created or removed without
2947c690fd1SPaolo Bonzini  * acquiring the AioContext.
29598563fc3SStefan Hajnoczi  */
29698563fc3SStefan Hajnoczi void aio_context_acquire(AioContext *ctx);
29798563fc3SStefan Hajnoczi 
29898563fc3SStefan Hajnoczi /* Relinquish ownership of the AioContext. */
29998563fc3SStefan Hajnoczi void aio_context_release(AioContext *ctx);
30098563fc3SStefan Hajnoczi 
301737e150eSPaolo Bonzini /**
3020f08586cSStefan Hajnoczi  * aio_bh_schedule_oneshot_full: Allocate a new bottom half structure that will
3030f08586cSStefan Hajnoczi  * run only once and as soon as possible.
3040f08586cSStefan Hajnoczi  *
3050f08586cSStefan Hajnoczi  * @name: A human-readable identifier for debugging purposes.
3065b8bb359SPaolo Bonzini  */
3070f08586cSStefan Hajnoczi void aio_bh_schedule_oneshot_full(AioContext *ctx, QEMUBHFunc *cb, void *opaque,
3080f08586cSStefan Hajnoczi                                   const char *name);
3095b8bb359SPaolo Bonzini 
3105b8bb359SPaolo Bonzini /**
3110f08586cSStefan Hajnoczi  * aio_bh_schedule_oneshot: Allocate a new bottom half structure that will run
3120f08586cSStefan Hajnoczi  * only once and as soon as possible.
3130f08586cSStefan Hajnoczi  *
3140f08586cSStefan Hajnoczi  * A convenience wrapper for aio_bh_schedule_oneshot_full() that uses cb as the
3150f08586cSStefan Hajnoczi  * name string.
3160f08586cSStefan Hajnoczi  */
3170f08586cSStefan Hajnoczi #define aio_bh_schedule_oneshot(ctx, cb, opaque) \
3180f08586cSStefan Hajnoczi     aio_bh_schedule_oneshot_full((ctx), (cb), (opaque), (stringify(cb)))
3190f08586cSStefan Hajnoczi 
3200f08586cSStefan Hajnoczi /**
3210f08586cSStefan Hajnoczi  * aio_bh_new_full: Allocate a new bottom half structure.
322737e150eSPaolo Bonzini  *
323737e150eSPaolo Bonzini  * Bottom halves are lightweight callbacks whose invocation is guaranteed
324737e150eSPaolo Bonzini  * to be wait-free, thread-safe and signal-safe.  The #QEMUBH structure
325737e150eSPaolo Bonzini  * is opaque and must be allocated prior to its use.
3260f08586cSStefan Hajnoczi  *
3270f08586cSStefan Hajnoczi  * @name: A human-readable identifier for debugging purposes.
328*9c86c97fSAlexander Bulekov  * @reentrancy_guard: A guard set when entering a cb to prevent
329*9c86c97fSAlexander Bulekov  * device-reentrancy issues
330737e150eSPaolo Bonzini  */
3310f08586cSStefan Hajnoczi QEMUBH *aio_bh_new_full(AioContext *ctx, QEMUBHFunc *cb, void *opaque,
332*9c86c97fSAlexander Bulekov                         const char *name, MemReentrancyGuard *reentrancy_guard);
3330f08586cSStefan Hajnoczi 
3340f08586cSStefan Hajnoczi /**
3350f08586cSStefan Hajnoczi  * aio_bh_new: Allocate a new bottom half structure
3360f08586cSStefan Hajnoczi  *
3370f08586cSStefan Hajnoczi  * A convenience wrapper for aio_bh_new_full() that uses the cb as the name
3380f08586cSStefan Hajnoczi  * string.
3390f08586cSStefan Hajnoczi  */
3400f08586cSStefan Hajnoczi #define aio_bh_new(ctx, cb, opaque) \
341*9c86c97fSAlexander Bulekov     aio_bh_new_full((ctx), (cb), (opaque), (stringify(cb)), NULL)
342*9c86c97fSAlexander Bulekov 
343*9c86c97fSAlexander Bulekov /**
344*9c86c97fSAlexander Bulekov  * aio_bh_new_guarded: Allocate a new bottom half structure with a
345*9c86c97fSAlexander Bulekov  * reentrancy_guard
346*9c86c97fSAlexander Bulekov  *
347*9c86c97fSAlexander Bulekov  * A convenience wrapper for aio_bh_new_full() that uses the cb as the name
348*9c86c97fSAlexander Bulekov  * string.
349*9c86c97fSAlexander Bulekov  */
350*9c86c97fSAlexander Bulekov #define aio_bh_new_guarded(ctx, cb, opaque, guard) \
351*9c86c97fSAlexander Bulekov     aio_bh_new_full((ctx), (cb), (opaque), (stringify(cb)), guard)
352737e150eSPaolo Bonzini 
353737e150eSPaolo Bonzini /**
354737e150eSPaolo Bonzini  * aio_notify: Force processing of pending events.
355737e150eSPaolo Bonzini  *
356737e150eSPaolo Bonzini  * Similar to signaling a condition variable, aio_notify forces
357722f8d90SYaowei Bai  * aio_poll to exit, so that the next call will re-examine pending events.
358722f8d90SYaowei Bai  * The caller of aio_notify will usually call aio_poll again very soon,
359737e150eSPaolo Bonzini  * or go through another iteration of the GLib main loop.  Hence, aio_notify
360737e150eSPaolo Bonzini  * also has the side effect of recalculating the sets of file descriptors
361737e150eSPaolo Bonzini  * that the main loop waits for.
362737e150eSPaolo Bonzini  *
363737e150eSPaolo Bonzini  * Calling aio_notify is rarely necessary, because for example scheduling
364737e150eSPaolo Bonzini  * a bottom half calls it already.
365737e150eSPaolo Bonzini  */
366737e150eSPaolo Bonzini void aio_notify(AioContext *ctx);
367737e150eSPaolo Bonzini 
368737e150eSPaolo Bonzini /**
36905e514b1SPaolo Bonzini  * aio_notify_accept: Acknowledge receiving an aio_notify.
37005e514b1SPaolo Bonzini  *
37105e514b1SPaolo Bonzini  * aio_notify() uses an EventNotifier in order to wake up a sleeping
37205e514b1SPaolo Bonzini  * aio_poll() or g_main_context_iteration().  Calls to aio_notify() are
37305e514b1SPaolo Bonzini  * usually rare, but the AioContext has to clear the EventNotifier on
37405e514b1SPaolo Bonzini  * every aio_poll() or g_main_context_iteration() in order to avoid
37505e514b1SPaolo Bonzini  * busy waiting.  This event_notifier_test_and_clear() cannot be done
37605e514b1SPaolo Bonzini  * using the usual aio_context_set_event_notifier(), because it must
37705e514b1SPaolo Bonzini  * be done before processing all events (file descriptors, bottom halves,
37805e514b1SPaolo Bonzini  * timers).
37905e514b1SPaolo Bonzini  *
38005e514b1SPaolo Bonzini  * aio_notify_accept() is an optimized event_notifier_test_and_clear()
38105e514b1SPaolo Bonzini  * that is specific to an AioContext's notifier; it is used internally
38205e514b1SPaolo Bonzini  * to clear the EventNotifier only if aio_notify() had been called.
38305e514b1SPaolo Bonzini  */
38405e514b1SPaolo Bonzini void aio_notify_accept(AioContext *ctx);
38505e514b1SPaolo Bonzini 
38605e514b1SPaolo Bonzini /**
387df281b80SPavel Dovgalyuk  * aio_bh_call: Executes callback function of the specified BH.
388df281b80SPavel Dovgalyuk  */
389df281b80SPavel Dovgalyuk void aio_bh_call(QEMUBH *bh);
390df281b80SPavel Dovgalyuk 
391df281b80SPavel Dovgalyuk /**
392737e150eSPaolo Bonzini  * aio_bh_poll: Poll bottom halves for an AioContext.
393737e150eSPaolo Bonzini  *
394737e150eSPaolo Bonzini  * These are internal functions used by the QEMU main loop.
395dcc772e2SLiu Ping Fan  * And notice that multiple occurrences of aio_bh_poll cannot
396dcc772e2SLiu Ping Fan  * be called concurrently
397737e150eSPaolo Bonzini  */
398737e150eSPaolo Bonzini int aio_bh_poll(AioContext *ctx);
399737e150eSPaolo Bonzini 
400737e150eSPaolo Bonzini /**
401737e150eSPaolo Bonzini  * qemu_bh_schedule: Schedule a bottom half.
402737e150eSPaolo Bonzini  *
403737e150eSPaolo Bonzini  * Scheduling a bottom half interrupts the main loop and causes the
404737e150eSPaolo Bonzini  * execution of the callback that was passed to qemu_bh_new.
405737e150eSPaolo Bonzini  *
406737e150eSPaolo Bonzini  * Bottom halves that are scheduled from a bottom half handler are instantly
407737e150eSPaolo Bonzini  * invoked.  This can create an infinite loop if a bottom half handler
408737e150eSPaolo Bonzini  * schedules itself.
409737e150eSPaolo Bonzini  *
410737e150eSPaolo Bonzini  * @bh: The bottom half to be scheduled.
411737e150eSPaolo Bonzini  */
412737e150eSPaolo Bonzini void qemu_bh_schedule(QEMUBH *bh);
413737e150eSPaolo Bonzini 
414737e150eSPaolo Bonzini /**
415737e150eSPaolo Bonzini  * qemu_bh_cancel: Cancel execution of a bottom half.
416737e150eSPaolo Bonzini  *
417737e150eSPaolo Bonzini  * Canceling execution of a bottom half undoes the effect of calls to
418737e150eSPaolo Bonzini  * qemu_bh_schedule without freeing its resources yet.  While cancellation
419737e150eSPaolo Bonzini  * itself is also wait-free and thread-safe, it can of course race with the
420737e150eSPaolo Bonzini  * loop that executes bottom halves unless you are holding the iothread
421737e150eSPaolo Bonzini  * mutex.  This makes it mostly useless if you are not holding the mutex.
422737e150eSPaolo Bonzini  *
423737e150eSPaolo Bonzini  * @bh: The bottom half to be canceled.
424737e150eSPaolo Bonzini  */
425737e150eSPaolo Bonzini void qemu_bh_cancel(QEMUBH *bh);
426737e150eSPaolo Bonzini 
427737e150eSPaolo Bonzini /**
428737e150eSPaolo Bonzini  *qemu_bh_delete: Cancel execution of a bottom half and free its resources.
429737e150eSPaolo Bonzini  *
430737e150eSPaolo Bonzini  * Deleting a bottom half frees the memory that was allocated for it by
431737e150eSPaolo Bonzini  * qemu_bh_new.  It also implies canceling the bottom half if it was
432737e150eSPaolo Bonzini  * scheduled.
433dcc772e2SLiu Ping Fan  * This func is async. The bottom half will do the delete action at the finial
434dcc772e2SLiu Ping Fan  * end.
435737e150eSPaolo Bonzini  *
436737e150eSPaolo Bonzini  * @bh: The bottom half to be deleted.
437737e150eSPaolo Bonzini  */
438737e150eSPaolo Bonzini void qemu_bh_delete(QEMUBH *bh);
439737e150eSPaolo Bonzini 
440737e150eSPaolo Bonzini /* Return whether there are any pending callbacks from the GSource
441a3462c65SPaolo Bonzini  * attached to the AioContext, before g_poll is invoked.
442a3462c65SPaolo Bonzini  *
443a3462c65SPaolo Bonzini  * This is used internally in the implementation of the GSource.
444a3462c65SPaolo Bonzini  */
445a3462c65SPaolo Bonzini bool aio_prepare(AioContext *ctx);
446a3462c65SPaolo Bonzini 
447a3462c65SPaolo Bonzini /* Return whether there are any pending callbacks from the GSource
448a3462c65SPaolo Bonzini  * attached to the AioContext, after g_poll is invoked.
449737e150eSPaolo Bonzini  *
450737e150eSPaolo Bonzini  * This is used internally in the implementation of the GSource.
451737e150eSPaolo Bonzini  */
452737e150eSPaolo Bonzini bool aio_pending(AioContext *ctx);
453737e150eSPaolo Bonzini 
454e4c7e2d1SPaolo Bonzini /* Dispatch any pending callbacks from the GSource attached to the AioContext.
455e4c7e2d1SPaolo Bonzini  *
456e4c7e2d1SPaolo Bonzini  * This is used internally in the implementation of the GSource.
457e4c7e2d1SPaolo Bonzini  */
458a153bf52SPaolo Bonzini void aio_dispatch(AioContext *ctx);
459e4c7e2d1SPaolo Bonzini 
460737e150eSPaolo Bonzini /* Progress in completing AIO work to occur.  This can issue new pending
461737e150eSPaolo Bonzini  * aio as a result of executing I/O completion or bh callbacks.
462737e150eSPaolo Bonzini  *
463acfb23adSPaolo Bonzini  * Return whether any progress was made by executing AIO or bottom half
464acfb23adSPaolo Bonzini  * handlers.  If @blocking == true, this should always be true except
465acfb23adSPaolo Bonzini  * if someone called aio_notify.
466737e150eSPaolo Bonzini  *
467737e150eSPaolo Bonzini  * If there are no pending bottom halves, but there are pending AIO
468737e150eSPaolo Bonzini  * operations, it may not be possible to make any progress without
469737e150eSPaolo Bonzini  * blocking.  If @blocking is true, this function will wait until one
470737e150eSPaolo Bonzini  * or more AIO events have completed, to ensure something has moved
471737e150eSPaolo Bonzini  * before returning.
472737e150eSPaolo Bonzini  */
473737e150eSPaolo Bonzini bool aio_poll(AioContext *ctx, bool blocking);
474737e150eSPaolo Bonzini 
475737e150eSPaolo Bonzini /* Register a file descriptor and associated callbacks.  Behaves very similarly
4766484e422SFam Zheng  * to qemu_set_fd_handler.  Unlike qemu_set_fd_handler, these callbacks will
47787f68d31SPaolo Bonzini  * be invoked when using aio_poll().
478737e150eSPaolo Bonzini  *
479737e150eSPaolo Bonzini  * Code that invokes AIO completion functions should rely on this function
480737e150eSPaolo Bonzini  * instead of qemu_set_fd_handler[2].
481737e150eSPaolo Bonzini  */
482737e150eSPaolo Bonzini void aio_set_fd_handler(AioContext *ctx,
483737e150eSPaolo Bonzini                         int fd,
484dca21ef2SFam Zheng                         bool is_external,
485737e150eSPaolo Bonzini                         IOHandler *io_read,
486737e150eSPaolo Bonzini                         IOHandler *io_write,
487f6a51c84SStefan Hajnoczi                         AioPollFn *io_poll,
488826cc324SStefan Hajnoczi                         IOHandler *io_poll_ready,
489737e150eSPaolo Bonzini                         void *opaque);
490737e150eSPaolo Bonzini 
491737e150eSPaolo Bonzini /* Register an event notifier and associated callbacks.  Behaves very similarly
492737e150eSPaolo Bonzini  * to event_notifier_set_handler.  Unlike event_notifier_set_handler, these callbacks
49387f68d31SPaolo Bonzini  * will be invoked when using aio_poll().
494737e150eSPaolo Bonzini  *
495737e150eSPaolo Bonzini  * Code that invokes AIO completion functions should rely on this function
496737e150eSPaolo Bonzini  * instead of event_notifier_set_handler.
497737e150eSPaolo Bonzini  */
498737e150eSPaolo Bonzini void aio_set_event_notifier(AioContext *ctx,
499737e150eSPaolo Bonzini                             EventNotifier *notifier,
500dca21ef2SFam Zheng                             bool is_external,
501f6a51c84SStefan Hajnoczi                             EventNotifierHandler *io_read,
502826cc324SStefan Hajnoczi                             AioPollFn *io_poll,
503826cc324SStefan Hajnoczi                             EventNotifierHandler *io_poll_ready);
504737e150eSPaolo Bonzini 
505684e508cSStefan Hajnoczi /* Set polling begin/end callbacks for an event notifier that has already been
506684e508cSStefan Hajnoczi  * registered with aio_set_event_notifier.  Do nothing if the event notifier is
507684e508cSStefan Hajnoczi  * not registered.
508684e508cSStefan Hajnoczi  */
509684e508cSStefan Hajnoczi void aio_set_event_notifier_poll(AioContext *ctx,
510684e508cSStefan Hajnoczi                                  EventNotifier *notifier,
511684e508cSStefan Hajnoczi                                  EventNotifierHandler *io_poll_begin,
512684e508cSStefan Hajnoczi                                  EventNotifierHandler *io_poll_end);
513684e508cSStefan Hajnoczi 
514737e150eSPaolo Bonzini /* Return a GSource that lets the main loop poll the file descriptors attached
515737e150eSPaolo Bonzini  * to this AioContext.
516737e150eSPaolo Bonzini  */
517737e150eSPaolo Bonzini GSource *aio_get_g_source(AioContext *ctx);
518737e150eSPaolo Bonzini 
5199b34277dSStefan Hajnoczi /* Return the ThreadPool bound to this AioContext */
5209b34277dSStefan Hajnoczi struct ThreadPool *aio_get_thread_pool(AioContext *ctx);
5219b34277dSStefan Hajnoczi 
522ed6e2161SNishanth Aravamudan /* Setup the LinuxAioState bound to this AioContext */
523ed6e2161SNishanth Aravamudan struct LinuxAioState *aio_setup_linux_aio(AioContext *ctx, Error **errp);
524ed6e2161SNishanth Aravamudan 
5250187f5c9SPaolo Bonzini /* Return the LinuxAioState bound to this AioContext */
5260187f5c9SPaolo Bonzini struct LinuxAioState *aio_get_linux_aio(AioContext *ctx);
5270187f5c9SPaolo Bonzini 
5286663a0a3SAarushi Mehta /* Setup the LuringState bound to this AioContext */
5296663a0a3SAarushi Mehta struct LuringState *aio_setup_linux_io_uring(AioContext *ctx, Error **errp);
5306663a0a3SAarushi Mehta 
5316663a0a3SAarushi Mehta /* Return the LuringState bound to this AioContext */
5326663a0a3SAarushi Mehta struct LuringState *aio_get_linux_io_uring(AioContext *ctx);
5334e29e831SAlex Bligh /**
53489a603a0SArtem Pisarenko  * aio_timer_new_with_attrs:
53589a603a0SArtem Pisarenko  * @ctx: the aio context
53689a603a0SArtem Pisarenko  * @type: the clock type
53789a603a0SArtem Pisarenko  * @scale: the scale
53889a603a0SArtem Pisarenko  * @attributes: 0, or one to multiple OR'ed QEMU_TIMER_ATTR_<id> values
53989a603a0SArtem Pisarenko  *              to assign
54089a603a0SArtem Pisarenko  * @cb: the callback to call on timer expiry
54189a603a0SArtem Pisarenko  * @opaque: the opaque pointer to pass to the callback
54289a603a0SArtem Pisarenko  *
54389a603a0SArtem Pisarenko  * Allocate a new timer (with attributes) attached to the context @ctx.
54489a603a0SArtem Pisarenko  * The function is responsible for memory allocation.
54589a603a0SArtem Pisarenko  *
54689a603a0SArtem Pisarenko  * The preferred interface is aio_timer_init or aio_timer_init_with_attrs.
54789a603a0SArtem Pisarenko  * Use that unless you really need dynamic memory allocation.
54889a603a0SArtem Pisarenko  *
54989a603a0SArtem Pisarenko  * Returns: a pointer to the new timer
55089a603a0SArtem Pisarenko  */
55189a603a0SArtem Pisarenko static inline QEMUTimer *aio_timer_new_with_attrs(AioContext *ctx,
55289a603a0SArtem Pisarenko                                                   QEMUClockType type,
55389a603a0SArtem Pisarenko                                                   int scale, int attributes,
55489a603a0SArtem Pisarenko                                                   QEMUTimerCB *cb, void *opaque)
55589a603a0SArtem Pisarenko {
55689a603a0SArtem Pisarenko     return timer_new_full(&ctx->tlg, type, scale, attributes, cb, opaque);
55789a603a0SArtem Pisarenko }
55889a603a0SArtem Pisarenko 
55989a603a0SArtem Pisarenko /**
5604e29e831SAlex Bligh  * aio_timer_new:
5614e29e831SAlex Bligh  * @ctx: the aio context
5624e29e831SAlex Bligh  * @type: the clock type
5634e29e831SAlex Bligh  * @scale: the scale
5644e29e831SAlex Bligh  * @cb: the callback to call on timer expiry
5654e29e831SAlex Bligh  * @opaque: the opaque pointer to pass to the callback
5664e29e831SAlex Bligh  *
5674e29e831SAlex Bligh  * Allocate a new timer attached to the context @ctx.
56889a603a0SArtem Pisarenko  * See aio_timer_new_with_attrs for details.
5694e29e831SAlex Bligh  *
5704e29e831SAlex Bligh  * Returns: a pointer to the new timer
5714e29e831SAlex Bligh  */
5724e29e831SAlex Bligh static inline QEMUTimer *aio_timer_new(AioContext *ctx, QEMUClockType type,
5734e29e831SAlex Bligh                                        int scale,
5744e29e831SAlex Bligh                                        QEMUTimerCB *cb, void *opaque)
5754e29e831SAlex Bligh {
57689a603a0SArtem Pisarenko     return timer_new_full(&ctx->tlg, type, scale, 0, cb, opaque);
57789a603a0SArtem Pisarenko }
57889a603a0SArtem Pisarenko 
57989a603a0SArtem Pisarenko /**
58089a603a0SArtem Pisarenko  * aio_timer_init_with_attrs:
58189a603a0SArtem Pisarenko  * @ctx: the aio context
58289a603a0SArtem Pisarenko  * @ts: the timer
58389a603a0SArtem Pisarenko  * @type: the clock type
58489a603a0SArtem Pisarenko  * @scale: the scale
58589a603a0SArtem Pisarenko  * @attributes: 0, or one to multiple OR'ed QEMU_TIMER_ATTR_<id> values
58689a603a0SArtem Pisarenko  *              to assign
58789a603a0SArtem Pisarenko  * @cb: the callback to call on timer expiry
58889a603a0SArtem Pisarenko  * @opaque: the opaque pointer to pass to the callback
58989a603a0SArtem Pisarenko  *
59089a603a0SArtem Pisarenko  * Initialise a new timer (with attributes) attached to the context @ctx.
59189a603a0SArtem Pisarenko  * The caller is responsible for memory allocation.
59289a603a0SArtem Pisarenko  */
59389a603a0SArtem Pisarenko static inline void aio_timer_init_with_attrs(AioContext *ctx,
59489a603a0SArtem Pisarenko                                              QEMUTimer *ts, QEMUClockType type,
59589a603a0SArtem Pisarenko                                              int scale, int attributes,
59689a603a0SArtem Pisarenko                                              QEMUTimerCB *cb, void *opaque)
59789a603a0SArtem Pisarenko {
59889a603a0SArtem Pisarenko     timer_init_full(ts, &ctx->tlg, type, scale, attributes, cb, opaque);
5994e29e831SAlex Bligh }
6004e29e831SAlex Bligh 
6014e29e831SAlex Bligh /**
6024e29e831SAlex Bligh  * aio_timer_init:
6034e29e831SAlex Bligh  * @ctx: the aio context
6044e29e831SAlex Bligh  * @ts: the timer
6054e29e831SAlex Bligh  * @type: the clock type
6064e29e831SAlex Bligh  * @scale: the scale
6074e29e831SAlex Bligh  * @cb: the callback to call on timer expiry
6084e29e831SAlex Bligh  * @opaque: the opaque pointer to pass to the callback
6094e29e831SAlex Bligh  *
6104e29e831SAlex Bligh  * Initialise a new timer attached to the context @ctx.
61189a603a0SArtem Pisarenko  * See aio_timer_init_with_attrs for details.
6124e29e831SAlex Bligh  */
6134e29e831SAlex Bligh static inline void aio_timer_init(AioContext *ctx,
6144e29e831SAlex Bligh                                   QEMUTimer *ts, QEMUClockType type,
6154e29e831SAlex Bligh                                   int scale,
6164e29e831SAlex Bligh                                   QEMUTimerCB *cb, void *opaque)
6174e29e831SAlex Bligh {
61889a603a0SArtem Pisarenko     timer_init_full(ts, &ctx->tlg, type, scale, 0, cb, opaque);
6194e29e831SAlex Bligh }
6204e29e831SAlex Bligh 
621845ca10dSPaolo Bonzini /**
622845ca10dSPaolo Bonzini  * aio_compute_timeout:
623845ca10dSPaolo Bonzini  * @ctx: the aio context
624845ca10dSPaolo Bonzini  *
625845ca10dSPaolo Bonzini  * Compute the timeout that a blocking aio_poll should use.
626845ca10dSPaolo Bonzini  */
627845ca10dSPaolo Bonzini int64_t aio_compute_timeout(AioContext *ctx);
628845ca10dSPaolo Bonzini 
629c1e1e5faSFam Zheng /**
630c1e1e5faSFam Zheng  * aio_disable_external:
631c1e1e5faSFam Zheng  * @ctx: the aio context
632c1e1e5faSFam Zheng  *
633c1e1e5faSFam Zheng  * Disable the further processing of external clients.
634c1e1e5faSFam Zheng  */
635c1e1e5faSFam Zheng static inline void aio_disable_external(AioContext *ctx)
636c1e1e5faSFam Zheng {
637d73415a3SStefan Hajnoczi     qatomic_inc(&ctx->external_disable_cnt);
638c1e1e5faSFam Zheng }
639c1e1e5faSFam Zheng 
640c1e1e5faSFam Zheng /**
641c1e1e5faSFam Zheng  * aio_enable_external:
642c1e1e5faSFam Zheng  * @ctx: the aio context
643c1e1e5faSFam Zheng  *
644c1e1e5faSFam Zheng  * Enable the processing of external clients.
645c1e1e5faSFam Zheng  */
646c1e1e5faSFam Zheng static inline void aio_enable_external(AioContext *ctx)
647c1e1e5faSFam Zheng {
648321d1dbaSStefan Hajnoczi     int old;
649321d1dbaSStefan Hajnoczi 
650d73415a3SStefan Hajnoczi     old = qatomic_fetch_dec(&ctx->external_disable_cnt);
651321d1dbaSStefan Hajnoczi     assert(old > 0);
652321d1dbaSStefan Hajnoczi     if (old == 1) {
653321d1dbaSStefan Hajnoczi         /* Kick event loop so it re-arms file descriptors */
654321d1dbaSStefan Hajnoczi         aio_notify(ctx);
655321d1dbaSStefan Hajnoczi     }
656c1e1e5faSFam Zheng }
657c1e1e5faSFam Zheng 
658c1e1e5faSFam Zheng /**
6595ceb9e39SFam Zheng  * aio_external_disabled:
6605ceb9e39SFam Zheng  * @ctx: the aio context
6615ceb9e39SFam Zheng  *
6625ceb9e39SFam Zheng  * Return true if the external clients are disabled.
6635ceb9e39SFam Zheng  */
6645ceb9e39SFam Zheng static inline bool aio_external_disabled(AioContext *ctx)
6655ceb9e39SFam Zheng {
666d73415a3SStefan Hajnoczi     return qatomic_read(&ctx->external_disable_cnt);
6675ceb9e39SFam Zheng }
6685ceb9e39SFam Zheng 
6695ceb9e39SFam Zheng /**
670c1e1e5faSFam Zheng  * aio_node_check:
671c1e1e5faSFam Zheng  * @ctx: the aio context
672c1e1e5faSFam Zheng  * @is_external: Whether or not the checked node is an external event source.
673c1e1e5faSFam Zheng  *
674c1e1e5faSFam Zheng  * Check if the node's is_external flag is okay to be polled by the ctx at this
675c1e1e5faSFam Zheng  * moment. True means green light.
676c1e1e5faSFam Zheng  */
677c1e1e5faSFam Zheng static inline bool aio_node_check(AioContext *ctx, bool is_external)
678c1e1e5faSFam Zheng {
679d73415a3SStefan Hajnoczi     return !is_external || !qatomic_read(&ctx->external_disable_cnt);
680c1e1e5faSFam Zheng }
681c1e1e5faSFam Zheng 
68237fcee5dSFam Zheng /**
6830c330a73SPaolo Bonzini  * aio_co_schedule:
6840c330a73SPaolo Bonzini  * @ctx: the aio context
6850c330a73SPaolo Bonzini  * @co: the coroutine
6860c330a73SPaolo Bonzini  *
6870c330a73SPaolo Bonzini  * Start a coroutine on a remote AioContext.
6880c330a73SPaolo Bonzini  *
6890c330a73SPaolo Bonzini  * The coroutine must not be entered by anyone else while aio_co_schedule()
6900c330a73SPaolo Bonzini  * is active.  In addition the coroutine must have yielded unless ctx
6910c330a73SPaolo Bonzini  * is the context in which the coroutine is running (i.e. the value of
6920c330a73SPaolo Bonzini  * qemu_get_current_aio_context() from the coroutine itself).
6930c330a73SPaolo Bonzini  */
69443695601SMarkus Armbruster void aio_co_schedule(AioContext *ctx, Coroutine *co);
6950c330a73SPaolo Bonzini 
6960c330a73SPaolo Bonzini /**
69726b0b698SKevin Wolf  * aio_co_reschedule_self:
69826b0b698SKevin Wolf  * @new_ctx: the new context
69926b0b698SKevin Wolf  *
70026b0b698SKevin Wolf  * Move the currently running coroutine to new_ctx. If the coroutine is already
70126b0b698SKevin Wolf  * running in new_ctx, do nothing.
70226b0b698SKevin Wolf  */
70326b0b698SKevin Wolf void coroutine_fn aio_co_reschedule_self(AioContext *new_ctx);
70426b0b698SKevin Wolf 
70526b0b698SKevin Wolf /**
7060c330a73SPaolo Bonzini  * aio_co_wake:
7070c330a73SPaolo Bonzini  * @co: the coroutine
7080c330a73SPaolo Bonzini  *
7090c330a73SPaolo Bonzini  * Restart a coroutine on the AioContext where it was running last, thus
7100c330a73SPaolo Bonzini  * preventing coroutines from jumping from one context to another when they
7110c330a73SPaolo Bonzini  * go to sleep.
7120c330a73SPaolo Bonzini  *
7130c330a73SPaolo Bonzini  * aio_co_wake may be executed either in coroutine or non-coroutine
7140c330a73SPaolo Bonzini  * context.  The coroutine must not be entered by anyone else while
7150c330a73SPaolo Bonzini  * aio_co_wake() is active.
7160c330a73SPaolo Bonzini  */
71743695601SMarkus Armbruster void aio_co_wake(Coroutine *co);
7180c330a73SPaolo Bonzini 
7190c330a73SPaolo Bonzini /**
7208865852eSFam Zheng  * aio_co_enter:
7218865852eSFam Zheng  * @ctx: the context to run the coroutine
7228865852eSFam Zheng  * @co: the coroutine to run
7238865852eSFam Zheng  *
7248865852eSFam Zheng  * Enter a coroutine in the specified AioContext.
7258865852eSFam Zheng  */
72643695601SMarkus Armbruster void aio_co_enter(AioContext *ctx, Coroutine *co);
7278865852eSFam Zheng 
7288865852eSFam Zheng /**
729e4370165SPaolo Bonzini  * Return the AioContext whose event loop runs in the current thread.
730e4370165SPaolo Bonzini  *
731e4370165SPaolo Bonzini  * If called from an IOThread this will be the IOThread's AioContext.  If
7325f50be9bSPaolo Bonzini  * called from the main thread or with the "big QEMU lock" taken it
7335f50be9bSPaolo Bonzini  * will be the main loop AioContext.
734e4370165SPaolo Bonzini  */
735e4370165SPaolo Bonzini AioContext *qemu_get_current_aio_context(void);
736e4370165SPaolo Bonzini 
7375f50be9bSPaolo Bonzini void qemu_set_current_aio_context(AioContext *ctx);
7385f50be9bSPaolo Bonzini 
739e4370165SPaolo Bonzini /**
74037fcee5dSFam Zheng  * aio_context_setup:
74137fcee5dSFam Zheng  * @ctx: the aio context
74237fcee5dSFam Zheng  *
74337fcee5dSFam Zheng  * Initialize the aio context.
74437fcee5dSFam Zheng  */
7457e003465SCao jin void aio_context_setup(AioContext *ctx);
74637fcee5dSFam Zheng 
7474a1cba38SStefan Hajnoczi /**
748cd0a6d2bSJie Wang  * aio_context_destroy:
749cd0a6d2bSJie Wang  * @ctx: the aio context
750cd0a6d2bSJie Wang  *
751cd0a6d2bSJie Wang  * Destroy the aio context.
752cd0a6d2bSJie Wang  */
753cd0a6d2bSJie Wang void aio_context_destroy(AioContext *ctx);
754cd0a6d2bSJie Wang 
755ba607ca8SStefan Hajnoczi /* Used internally, do not call outside AioContext code */
756ba607ca8SStefan Hajnoczi void aio_context_use_g_source(AioContext *ctx);
757ba607ca8SStefan Hajnoczi 
758cd0a6d2bSJie Wang /**
7594a1cba38SStefan Hajnoczi  * aio_context_set_poll_params:
7604a1cba38SStefan Hajnoczi  * @ctx: the aio context
7614a1cba38SStefan Hajnoczi  * @max_ns: how long to busy poll for, in nanoseconds
76282a41186SStefan Hajnoczi  * @grow: polling time growth factor
76382a41186SStefan Hajnoczi  * @shrink: polling time shrink factor
7644a1cba38SStefan Hajnoczi  *
7654a1cba38SStefan Hajnoczi  * Poll mode can be disabled by setting poll_max_ns to 0.
7664a1cba38SStefan Hajnoczi  */
7674a1cba38SStefan Hajnoczi void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
76882a41186SStefan Hajnoczi                                  int64_t grow, int64_t shrink,
7694a1cba38SStefan Hajnoczi                                  Error **errp);
7704a1cba38SStefan Hajnoczi 
7711793ad02SStefano Garzarella /**
7721793ad02SStefano Garzarella  * aio_context_set_aio_params:
7731793ad02SStefano Garzarella  * @ctx: the aio context
7741793ad02SStefano Garzarella  * @max_batch: maximum number of requests in a batch, 0 means that the
7751793ad02SStefano Garzarella  *             engine will use its default
7761793ad02SStefano Garzarella  */
7771793ad02SStefano Garzarella void aio_context_set_aio_params(AioContext *ctx, int64_t max_batch,
7781793ad02SStefano Garzarella                                 Error **errp);
7791793ad02SStefano Garzarella 
78071ad4713SNicolas Saenz Julienne /**
78171ad4713SNicolas Saenz Julienne  * aio_context_set_thread_pool_params:
78271ad4713SNicolas Saenz Julienne  * @ctx: the aio context
78371ad4713SNicolas Saenz Julienne  * @min: min number of threads to have readily available in the thread pool
78471ad4713SNicolas Saenz Julienne  * @min: max number of threads the thread pool can contain
78571ad4713SNicolas Saenz Julienne  */
78671ad4713SNicolas Saenz Julienne void aio_context_set_thread_pool_params(AioContext *ctx, int64_t min,
78771ad4713SNicolas Saenz Julienne                                         int64_t max, Error **errp);
788737e150eSPaolo Bonzini #endif
789