xref: /openbmc/qemu/include/block/aio.h (revision 5f50be9b5810293141bb53cfd0cb46e765367d56)
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
2026b0b698SKevin Wolf #include "qemu/coroutine.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"
25737e150eSPaolo Bonzini 
267c84b1b8SMarkus Armbruster typedef struct BlockAIOCB BlockAIOCB;
27097310b5SMarkus Armbruster typedef void BlockCompletionFunc(void *opaque, int ret);
28737e150eSPaolo Bonzini 
29737e150eSPaolo Bonzini typedef struct AIOCBInfo {
307c84b1b8SMarkus Armbruster     void (*cancel_async)(BlockAIOCB *acb);
317c84b1b8SMarkus Armbruster     AioContext *(*get_aio_context)(BlockAIOCB *acb);
32737e150eSPaolo Bonzini     size_t aiocb_size;
33737e150eSPaolo Bonzini } AIOCBInfo;
34737e150eSPaolo Bonzini 
357c84b1b8SMarkus Armbruster struct BlockAIOCB {
36737e150eSPaolo Bonzini     const AIOCBInfo *aiocb_info;
37737e150eSPaolo Bonzini     BlockDriverState *bs;
38097310b5SMarkus Armbruster     BlockCompletionFunc *cb;
39737e150eSPaolo Bonzini     void *opaque;
40f197fe2bSFam Zheng     int refcnt;
41737e150eSPaolo Bonzini };
42737e150eSPaolo Bonzini 
43737e150eSPaolo Bonzini void *qemu_aio_get(const AIOCBInfo *aiocb_info, BlockDriverState *bs,
44097310b5SMarkus Armbruster                    BlockCompletionFunc *cb, void *opaque);
458007429aSFam Zheng void qemu_aio_unref(void *p);
46f197fe2bSFam Zheng void qemu_aio_ref(void *p);
47737e150eSPaolo Bonzini 
48737e150eSPaolo Bonzini typedef struct AioHandler AioHandler;
494749079cSStefan Hajnoczi typedef QLIST_HEAD(, AioHandler) AioHandlerList;
50737e150eSPaolo Bonzini typedef void QEMUBHFunc(void *opaque);
51f6a51c84SStefan Hajnoczi typedef bool AioPollFn(void *opaque);
52737e150eSPaolo Bonzini typedef void IOHandler(void *opaque);
53737e150eSPaolo Bonzini 
540c330a73SPaolo Bonzini struct Coroutine;
550187f5c9SPaolo Bonzini struct ThreadPool;
560187f5c9SPaolo Bonzini struct LinuxAioState;
576663a0a3SAarushi Mehta struct LuringState;
580187f5c9SPaolo Bonzini 
59aa38e19fSStefan Hajnoczi /* Is polling disabled? */
60aa38e19fSStefan Hajnoczi bool aio_poll_disabled(AioContext *ctx);
61aa38e19fSStefan Hajnoczi 
621f050a46SStefan Hajnoczi /* Callbacks for file descriptor monitoring implementations */
631f050a46SStefan Hajnoczi typedef struct {
641f050a46SStefan Hajnoczi     /*
651f050a46SStefan Hajnoczi      * update:
661f050a46SStefan Hajnoczi      * @ctx: the AioContext
67b321051cSStefan Hajnoczi      * @old_node: the existing handler or NULL if this file descriptor is being
68b321051cSStefan Hajnoczi      *            monitored for the first time
69b321051cSStefan Hajnoczi      * @new_node: the new handler or NULL if this file descriptor is being
70b321051cSStefan Hajnoczi      *            removed
711f050a46SStefan Hajnoczi      *
72b321051cSStefan Hajnoczi      * Add/remove/modify a monitored file descriptor.
731f050a46SStefan Hajnoczi      *
741f050a46SStefan Hajnoczi      * Called with ctx->list_lock acquired.
751f050a46SStefan Hajnoczi      */
76b321051cSStefan Hajnoczi     void (*update)(AioContext *ctx, AioHandler *old_node, AioHandler *new_node);
771f050a46SStefan Hajnoczi 
781f050a46SStefan Hajnoczi     /*
791f050a46SStefan Hajnoczi      * wait:
801f050a46SStefan Hajnoczi      * @ctx: the AioContext
811f050a46SStefan Hajnoczi      * @ready_list: list for handlers that become ready
821f050a46SStefan Hajnoczi      * @timeout: maximum duration to wait, in nanoseconds
831f050a46SStefan Hajnoczi      *
841f050a46SStefan Hajnoczi      * Wait for file descriptors to become ready and place them on ready_list.
851f050a46SStefan Hajnoczi      *
861f050a46SStefan Hajnoczi      * Called with ctx->list_lock incremented but not locked.
871f050a46SStefan Hajnoczi      *
881f050a46SStefan Hajnoczi      * Returns: number of ready file descriptors.
891f050a46SStefan Hajnoczi      */
901f050a46SStefan Hajnoczi     int (*wait)(AioContext *ctx, AioHandlerList *ready_list, int64_t timeout);
91aa38e19fSStefan Hajnoczi 
92aa38e19fSStefan Hajnoczi     /*
93aa38e19fSStefan Hajnoczi      * need_wait:
94aa38e19fSStefan Hajnoczi      * @ctx: the AioContext
95aa38e19fSStefan Hajnoczi      *
96aa38e19fSStefan Hajnoczi      * Tell aio_poll() when to stop userspace polling early because ->wait()
97aa38e19fSStefan Hajnoczi      * has fds ready.
98aa38e19fSStefan Hajnoczi      *
99aa38e19fSStefan Hajnoczi      * File descriptor monitoring implementations that cannot poll fd readiness
100aa38e19fSStefan Hajnoczi      * from userspace should use aio_poll_disabled() here.  This ensures that
101aa38e19fSStefan Hajnoczi      * file descriptors are not starved by handlers that frequently make
102aa38e19fSStefan Hajnoczi      * progress via userspace polling.
103aa38e19fSStefan Hajnoczi      *
104aa38e19fSStefan Hajnoczi      * Returns: true if ->wait() should be called, false otherwise.
105aa38e19fSStefan Hajnoczi      */
106aa38e19fSStefan Hajnoczi     bool (*need_wait)(AioContext *ctx);
1071f050a46SStefan Hajnoczi } FDMonOps;
1081f050a46SStefan Hajnoczi 
1098c6b0356SStefan Hajnoczi /*
1108c6b0356SStefan Hajnoczi  * Each aio_bh_poll() call carves off a slice of the BH list, so that newly
1118c6b0356SStefan Hajnoczi  * scheduled BHs are not processed until the next aio_bh_poll() call.  All
1128c6b0356SStefan Hajnoczi  * active aio_bh_poll() calls chain their slices together in a list, so that
1138c6b0356SStefan Hajnoczi  * nested aio_bh_poll() calls process all scheduled bottom halves.
1148c6b0356SStefan Hajnoczi  */
1158c6b0356SStefan Hajnoczi typedef QSLIST_HEAD(, QEMUBH) BHList;
1168c6b0356SStefan Hajnoczi typedef struct BHListSlice BHListSlice;
1178c6b0356SStefan Hajnoczi struct BHListSlice {
1188c6b0356SStefan Hajnoczi     BHList bh_list;
1198c6b0356SStefan Hajnoczi     QSIMPLEQ_ENTRY(BHListSlice) next;
1208c6b0356SStefan Hajnoczi };
1218c6b0356SStefan Hajnoczi 
12273fd282eSStefan Hajnoczi typedef QSLIST_HEAD(, AioHandler) AioHandlerSList;
12373fd282eSStefan Hajnoczi 
1246a1751b7SAlex Bligh struct AioContext {
125737e150eSPaolo Bonzini     GSource source;
126737e150eSPaolo Bonzini 
1277c690fd1SPaolo Bonzini     /* Used by AioContext users to protect from multi-threaded access.  */
1283fe71223SPaolo Bonzini     QemuRecMutex lock;
12998563fc3SStefan Hajnoczi 
1307c690fd1SPaolo Bonzini     /* The list of registered AIO handlers.  Protected by ctx->list_lock. */
1314749079cSStefan Hajnoczi     AioHandlerList aio_handlers;
1324749079cSStefan Hajnoczi 
1334749079cSStefan Hajnoczi     /* The list of AIO handlers to be deleted.  Protected by ctx->list_lock. */
1344749079cSStefan Hajnoczi     AioHandlerList deleted_aio_handlers;
135737e150eSPaolo Bonzini 
136eabc9779SPaolo Bonzini     /* Used to avoid unnecessary event_notifier_set calls in aio_notify;
1373c18a92dSPaolo Bonzini      * only written from the AioContext home thread, or under the BQL in
1383c18a92dSPaolo Bonzini      * the case of the main AioContext.  However, it is read from any
1393c18a92dSPaolo Bonzini      * thread so it is still accessed with atomic primitives.
1403c18a92dSPaolo Bonzini      *
1413c18a92dSPaolo Bonzini      * If this field is 0, everything (file descriptors, bottom halves,
1423c18a92dSPaolo Bonzini      * timers) will be re-evaluated before the next blocking poll() or
1433c18a92dSPaolo Bonzini      * io_uring wait; therefore, the event_notifier_set call can be
1443c18a92dSPaolo Bonzini      * skipped.  If it is non-zero, you may need to wake up a concurrent
1453c18a92dSPaolo Bonzini      * aio_poll or the glib main event loop, making event_notifier_set
1463c18a92dSPaolo Bonzini      * necessary.
147eabc9779SPaolo Bonzini      *
148eabc9779SPaolo Bonzini      * Bit 0 is reserved for GSource usage of the AioContext, and is 1
14954a16a63SCao jin      * between a call to aio_ctx_prepare and the next call to aio_ctx_check.
150eabc9779SPaolo Bonzini      * Bits 1-31 simply count the number of active calls to aio_poll
151eabc9779SPaolo Bonzini      * that are in the prepare or poll phase.
152eabc9779SPaolo Bonzini      *
153eabc9779SPaolo Bonzini      * The GSource and aio_poll must use a different mechanism because
154eabc9779SPaolo Bonzini      * there is no certainty that a call to GSource's prepare callback
155eabc9779SPaolo Bonzini      * (via g_main_context_prepare) is indeed followed by check and
156eabc9779SPaolo Bonzini      * dispatch.  It's not clear whether this would be a bug, but let's
157eabc9779SPaolo Bonzini      * play safe and allow it---it will just cause extra calls to
158eabc9779SPaolo Bonzini      * event_notifier_set until the next call to dispatch.
159eabc9779SPaolo Bonzini      *
160eabc9779SPaolo Bonzini      * Instead, the aio_poll calls include both the prepare and the
161eabc9779SPaolo Bonzini      * dispatch phase, hence a simple counter is enough for them.
1620ceb849bSPaolo Bonzini      */
163eabc9779SPaolo Bonzini     uint32_t notify_me;
1640ceb849bSPaolo Bonzini 
1657c690fd1SPaolo Bonzini     /* A lock to protect between QEMUBH and AioHandler adders and deleter,
1667c690fd1SPaolo Bonzini      * and to ensure that no callbacks are removed while we're walking and
1677c690fd1SPaolo Bonzini      * dispatching them.
168d7c99a12SPaolo Bonzini      */
169d7c99a12SPaolo Bonzini     QemuLockCnt list_lock;
1700ceb849bSPaolo Bonzini 
1718c6b0356SStefan Hajnoczi     /* Bottom Halves pending aio_bh_poll() processing */
1728c6b0356SStefan Hajnoczi     BHList bh_list;
1738c6b0356SStefan Hajnoczi 
1748c6b0356SStefan Hajnoczi     /* Chained BH list slices for each nested aio_bh_poll() call */
1758c6b0356SStefan Hajnoczi     QSIMPLEQ_HEAD(, BHListSlice) bh_slice_list;
176737e150eSPaolo Bonzini 
17705e514b1SPaolo Bonzini     /* Used by aio_notify.
17805e514b1SPaolo Bonzini      *
17905e514b1SPaolo Bonzini      * "notified" is used to avoid expensive event_notifier_test_and_clear
18005e514b1SPaolo Bonzini      * calls.  When it is clear, the EventNotifier is clear, or one thread
18105e514b1SPaolo Bonzini      * is going to clear "notified" before processing more events.  False
18205e514b1SPaolo Bonzini      * positives are possible, i.e. "notified" could be set even though the
18305e514b1SPaolo Bonzini      * EventNotifier is clear.
18405e514b1SPaolo Bonzini      *
18505e514b1SPaolo Bonzini      * Note that event_notifier_set *cannot* be optimized the same way.  For
18605e514b1SPaolo Bonzini      * more information on the problem that would result, see "#ifdef BUG2"
18705e514b1SPaolo Bonzini      * in the docs/aio_notify_accept.promela formal model.
18805e514b1SPaolo Bonzini      */
18905e514b1SPaolo Bonzini     bool notified;
190737e150eSPaolo Bonzini     EventNotifier notifier;
1916b5f8762SStefan Hajnoczi 
1920c330a73SPaolo Bonzini     QSLIST_HEAD(, Coroutine) scheduled_coroutines;
1930c330a73SPaolo Bonzini     QEMUBH *co_schedule_bh;
1940c330a73SPaolo Bonzini 
1957c690fd1SPaolo Bonzini     /* Thread pool for performing work and receiving completion callbacks.
1967c690fd1SPaolo Bonzini      * Has its own locking.
1977c690fd1SPaolo Bonzini      */
1989b34277dSStefan Hajnoczi     struct ThreadPool *thread_pool;
199dae21b98SAlex Bligh 
2000187f5c9SPaolo Bonzini #ifdef CONFIG_LINUX_AIO
2016663a0a3SAarushi Mehta     /*
2026663a0a3SAarushi Mehta      * State for native Linux AIO.  Uses aio_context_acquire/release for
2030187f5c9SPaolo Bonzini      * locking.
2040187f5c9SPaolo Bonzini      */
2050187f5c9SPaolo Bonzini     struct LinuxAioState *linux_aio;
2060187f5c9SPaolo Bonzini #endif
2076663a0a3SAarushi Mehta #ifdef CONFIG_LINUX_IO_URING
2086663a0a3SAarushi Mehta     /*
2096663a0a3SAarushi Mehta      * State for Linux io_uring.  Uses aio_context_acquire/release for
2106663a0a3SAarushi Mehta      * locking.
2116663a0a3SAarushi Mehta      */
2126663a0a3SAarushi Mehta     struct LuringState *linux_io_uring;
21373fd282eSStefan Hajnoczi 
21473fd282eSStefan Hajnoczi     /* State for file descriptor monitoring using Linux io_uring */
21573fd282eSStefan Hajnoczi     struct io_uring fdmon_io_uring;
21673fd282eSStefan Hajnoczi     AioHandlerSList submit_list;
2176663a0a3SAarushi Mehta #endif
2180187f5c9SPaolo Bonzini 
2197c690fd1SPaolo Bonzini     /* TimerLists for calling timers - one per clock type.  Has its own
2207c690fd1SPaolo Bonzini      * locking.
2217c690fd1SPaolo Bonzini      */
222dae21b98SAlex Bligh     QEMUTimerListGroup tlg;
223c1e1e5faSFam Zheng 
224c1e1e5faSFam Zheng     int external_disable_cnt;
225fbe3fc5cSFam Zheng 
2264a1cba38SStefan Hajnoczi     /* Number of AioHandlers without .io_poll() */
2274a1cba38SStefan Hajnoczi     int poll_disable_cnt;
2284a1cba38SStefan Hajnoczi 
22982a41186SStefan Hajnoczi     /* Polling mode parameters */
23082a41186SStefan Hajnoczi     int64_t poll_ns;        /* current polling time in nanoseconds */
23182a41186SStefan Hajnoczi     int64_t poll_max_ns;    /* maximum polling time in nanoseconds */
23282a41186SStefan Hajnoczi     int64_t poll_grow;      /* polling time growth factor */
23382a41186SStefan Hajnoczi     int64_t poll_shrink;    /* polling time shrink factor */
2344a1cba38SStefan Hajnoczi 
235d37d0e36SStefan Hajnoczi     /*
236d37d0e36SStefan Hajnoczi      * List of handlers participating in userspace polling.  Protected by
237d37d0e36SStefan Hajnoczi      * ctx->list_lock.  Iterated and modified mostly by the event loop thread
238d37d0e36SStefan Hajnoczi      * from aio_poll() with ctx->list_lock incremented.  aio_set_fd_handler()
239d37d0e36SStefan Hajnoczi      * only touches the list to delete nodes if ctx->list_lock's count is zero.
240d37d0e36SStefan Hajnoczi      */
241d37d0e36SStefan Hajnoczi     AioHandlerList poll_aio_handlers;
242d37d0e36SStefan Hajnoczi 
243684e508cSStefan Hajnoczi     /* Are we in polling mode or monitoring file descriptors? */
244684e508cSStefan Hajnoczi     bool poll_started;
245684e508cSStefan Hajnoczi 
246fbe3fc5cSFam Zheng     /* epoll(7) state used when built with CONFIG_EPOLL */
247fbe3fc5cSFam Zheng     int epollfd;
2481f050a46SStefan Hajnoczi 
2491f050a46SStefan Hajnoczi     const FDMonOps *fdmon_ops;
2506a1751b7SAlex Bligh };
251737e150eSPaolo Bonzini 
252737e150eSPaolo Bonzini /**
253737e150eSPaolo Bonzini  * aio_context_new: Allocate a new AioContext.
254737e150eSPaolo Bonzini  *
255737e150eSPaolo Bonzini  * AioContext provide a mini event-loop that can be waited on synchronously.
256737e150eSPaolo Bonzini  * They also provide bottom halves, a service to execute a piece of code
257737e150eSPaolo Bonzini  * as soon as possible.
258737e150eSPaolo Bonzini  */
2592f78e491SChrysostomos Nanakos AioContext *aio_context_new(Error **errp);
260737e150eSPaolo Bonzini 
261737e150eSPaolo Bonzini /**
262737e150eSPaolo Bonzini  * aio_context_ref:
263737e150eSPaolo Bonzini  * @ctx: The AioContext to operate on.
264737e150eSPaolo Bonzini  *
265737e150eSPaolo Bonzini  * Add a reference to an AioContext.
266737e150eSPaolo Bonzini  */
267737e150eSPaolo Bonzini void aio_context_ref(AioContext *ctx);
268737e150eSPaolo Bonzini 
269737e150eSPaolo Bonzini /**
270737e150eSPaolo Bonzini  * aio_context_unref:
271737e150eSPaolo Bonzini  * @ctx: The AioContext to operate on.
272737e150eSPaolo Bonzini  *
273737e150eSPaolo Bonzini  * Drop a reference to an AioContext.
274737e150eSPaolo Bonzini  */
275737e150eSPaolo Bonzini void aio_context_unref(AioContext *ctx);
276737e150eSPaolo Bonzini 
27798563fc3SStefan Hajnoczi /* Take ownership of the AioContext.  If the AioContext will be shared between
27849110174SPaolo Bonzini  * threads, and a thread does not want to be interrupted, it will have to
27949110174SPaolo Bonzini  * take ownership around calls to aio_poll().  Otherwise, aio_poll()
28049110174SPaolo Bonzini  * automatically takes care of calling aio_context_acquire and
28149110174SPaolo Bonzini  * aio_context_release.
28298563fc3SStefan Hajnoczi  *
2837c690fd1SPaolo Bonzini  * Note that this is separate from bdrv_drained_begin/bdrv_drained_end.  A
2847c690fd1SPaolo Bonzini  * thread still has to call those to avoid being interrupted by the guest.
2857c690fd1SPaolo Bonzini  *
2867c690fd1SPaolo Bonzini  * Bottom halves, timers and callbacks can be created or removed without
2877c690fd1SPaolo Bonzini  * acquiring the AioContext.
28898563fc3SStefan Hajnoczi  */
28998563fc3SStefan Hajnoczi void aio_context_acquire(AioContext *ctx);
29098563fc3SStefan Hajnoczi 
29198563fc3SStefan Hajnoczi /* Relinquish ownership of the AioContext. */
29298563fc3SStefan Hajnoczi void aio_context_release(AioContext *ctx);
29398563fc3SStefan Hajnoczi 
294737e150eSPaolo Bonzini /**
2955b8bb359SPaolo Bonzini  * aio_bh_schedule_oneshot: Allocate a new bottom half structure that will run
2965b8bb359SPaolo Bonzini  * only once and as soon as possible.
2975b8bb359SPaolo Bonzini  */
2985b8bb359SPaolo Bonzini void aio_bh_schedule_oneshot(AioContext *ctx, QEMUBHFunc *cb, void *opaque);
2995b8bb359SPaolo Bonzini 
3005b8bb359SPaolo Bonzini /**
301737e150eSPaolo Bonzini  * aio_bh_new: Allocate a new bottom half structure.
302737e150eSPaolo Bonzini  *
303737e150eSPaolo Bonzini  * Bottom halves are lightweight callbacks whose invocation is guaranteed
304737e150eSPaolo Bonzini  * to be wait-free, thread-safe and signal-safe.  The #QEMUBH structure
305737e150eSPaolo Bonzini  * is opaque and must be allocated prior to its use.
306737e150eSPaolo Bonzini  */
307737e150eSPaolo Bonzini QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque);
308737e150eSPaolo Bonzini 
309737e150eSPaolo Bonzini /**
310737e150eSPaolo Bonzini  * aio_notify: Force processing of pending events.
311737e150eSPaolo Bonzini  *
312737e150eSPaolo Bonzini  * Similar to signaling a condition variable, aio_notify forces
313722f8d90SYaowei Bai  * aio_poll to exit, so that the next call will re-examine pending events.
314722f8d90SYaowei Bai  * The caller of aio_notify will usually call aio_poll again very soon,
315737e150eSPaolo Bonzini  * or go through another iteration of the GLib main loop.  Hence, aio_notify
316737e150eSPaolo Bonzini  * also has the side effect of recalculating the sets of file descriptors
317737e150eSPaolo Bonzini  * that the main loop waits for.
318737e150eSPaolo Bonzini  *
319737e150eSPaolo Bonzini  * Calling aio_notify is rarely necessary, because for example scheduling
320737e150eSPaolo Bonzini  * a bottom half calls it already.
321737e150eSPaolo Bonzini  */
322737e150eSPaolo Bonzini void aio_notify(AioContext *ctx);
323737e150eSPaolo Bonzini 
324737e150eSPaolo Bonzini /**
32505e514b1SPaolo Bonzini  * aio_notify_accept: Acknowledge receiving an aio_notify.
32605e514b1SPaolo Bonzini  *
32705e514b1SPaolo Bonzini  * aio_notify() uses an EventNotifier in order to wake up a sleeping
32805e514b1SPaolo Bonzini  * aio_poll() or g_main_context_iteration().  Calls to aio_notify() are
32905e514b1SPaolo Bonzini  * usually rare, but the AioContext has to clear the EventNotifier on
33005e514b1SPaolo Bonzini  * every aio_poll() or g_main_context_iteration() in order to avoid
33105e514b1SPaolo Bonzini  * busy waiting.  This event_notifier_test_and_clear() cannot be done
33205e514b1SPaolo Bonzini  * using the usual aio_context_set_event_notifier(), because it must
33305e514b1SPaolo Bonzini  * be done before processing all events (file descriptors, bottom halves,
33405e514b1SPaolo Bonzini  * timers).
33505e514b1SPaolo Bonzini  *
33605e514b1SPaolo Bonzini  * aio_notify_accept() is an optimized event_notifier_test_and_clear()
33705e514b1SPaolo Bonzini  * that is specific to an AioContext's notifier; it is used internally
33805e514b1SPaolo Bonzini  * to clear the EventNotifier only if aio_notify() had been called.
33905e514b1SPaolo Bonzini  */
34005e514b1SPaolo Bonzini void aio_notify_accept(AioContext *ctx);
34105e514b1SPaolo Bonzini 
34205e514b1SPaolo Bonzini /**
343df281b80SPavel Dovgalyuk  * aio_bh_call: Executes callback function of the specified BH.
344df281b80SPavel Dovgalyuk  */
345df281b80SPavel Dovgalyuk void aio_bh_call(QEMUBH *bh);
346df281b80SPavel Dovgalyuk 
347df281b80SPavel Dovgalyuk /**
348737e150eSPaolo Bonzini  * aio_bh_poll: Poll bottom halves for an AioContext.
349737e150eSPaolo Bonzini  *
350737e150eSPaolo Bonzini  * These are internal functions used by the QEMU main loop.
351dcc772e2SLiu Ping Fan  * And notice that multiple occurrences of aio_bh_poll cannot
352dcc772e2SLiu Ping Fan  * be called concurrently
353737e150eSPaolo Bonzini  */
354737e150eSPaolo Bonzini int aio_bh_poll(AioContext *ctx);
355737e150eSPaolo Bonzini 
356737e150eSPaolo Bonzini /**
357737e150eSPaolo Bonzini  * qemu_bh_schedule: Schedule a bottom half.
358737e150eSPaolo Bonzini  *
359737e150eSPaolo Bonzini  * Scheduling a bottom half interrupts the main loop and causes the
360737e150eSPaolo Bonzini  * execution of the callback that was passed to qemu_bh_new.
361737e150eSPaolo Bonzini  *
362737e150eSPaolo Bonzini  * Bottom halves that are scheduled from a bottom half handler are instantly
363737e150eSPaolo Bonzini  * invoked.  This can create an infinite loop if a bottom half handler
364737e150eSPaolo Bonzini  * schedules itself.
365737e150eSPaolo Bonzini  *
366737e150eSPaolo Bonzini  * @bh: The bottom half to be scheduled.
367737e150eSPaolo Bonzini  */
368737e150eSPaolo Bonzini void qemu_bh_schedule(QEMUBH *bh);
369737e150eSPaolo Bonzini 
370737e150eSPaolo Bonzini /**
371737e150eSPaolo Bonzini  * qemu_bh_cancel: Cancel execution of a bottom half.
372737e150eSPaolo Bonzini  *
373737e150eSPaolo Bonzini  * Canceling execution of a bottom half undoes the effect of calls to
374737e150eSPaolo Bonzini  * qemu_bh_schedule without freeing its resources yet.  While cancellation
375737e150eSPaolo Bonzini  * itself is also wait-free and thread-safe, it can of course race with the
376737e150eSPaolo Bonzini  * loop that executes bottom halves unless you are holding the iothread
377737e150eSPaolo Bonzini  * mutex.  This makes it mostly useless if you are not holding the mutex.
378737e150eSPaolo Bonzini  *
379737e150eSPaolo Bonzini  * @bh: The bottom half to be canceled.
380737e150eSPaolo Bonzini  */
381737e150eSPaolo Bonzini void qemu_bh_cancel(QEMUBH *bh);
382737e150eSPaolo Bonzini 
383737e150eSPaolo Bonzini /**
384737e150eSPaolo Bonzini  *qemu_bh_delete: Cancel execution of a bottom half and free its resources.
385737e150eSPaolo Bonzini  *
386737e150eSPaolo Bonzini  * Deleting a bottom half frees the memory that was allocated for it by
387737e150eSPaolo Bonzini  * qemu_bh_new.  It also implies canceling the bottom half if it was
388737e150eSPaolo Bonzini  * scheduled.
389dcc772e2SLiu Ping Fan  * This func is async. The bottom half will do the delete action at the finial
390dcc772e2SLiu Ping Fan  * end.
391737e150eSPaolo Bonzini  *
392737e150eSPaolo Bonzini  * @bh: The bottom half to be deleted.
393737e150eSPaolo Bonzini  */
394737e150eSPaolo Bonzini void qemu_bh_delete(QEMUBH *bh);
395737e150eSPaolo Bonzini 
396737e150eSPaolo Bonzini /* Return whether there are any pending callbacks from the GSource
397a3462c65SPaolo Bonzini  * attached to the AioContext, before g_poll is invoked.
398a3462c65SPaolo Bonzini  *
399a3462c65SPaolo Bonzini  * This is used internally in the implementation of the GSource.
400a3462c65SPaolo Bonzini  */
401a3462c65SPaolo Bonzini bool aio_prepare(AioContext *ctx);
402a3462c65SPaolo Bonzini 
403a3462c65SPaolo Bonzini /* Return whether there are any pending callbacks from the GSource
404a3462c65SPaolo Bonzini  * attached to the AioContext, after g_poll is invoked.
405737e150eSPaolo Bonzini  *
406737e150eSPaolo Bonzini  * This is used internally in the implementation of the GSource.
407737e150eSPaolo Bonzini  */
408737e150eSPaolo Bonzini bool aio_pending(AioContext *ctx);
409737e150eSPaolo Bonzini 
410e4c7e2d1SPaolo Bonzini /* Dispatch any pending callbacks from the GSource attached to the AioContext.
411e4c7e2d1SPaolo Bonzini  *
412e4c7e2d1SPaolo Bonzini  * This is used internally in the implementation of the GSource.
413e4c7e2d1SPaolo Bonzini  */
414a153bf52SPaolo Bonzini void aio_dispatch(AioContext *ctx);
415e4c7e2d1SPaolo Bonzini 
416737e150eSPaolo Bonzini /* Progress in completing AIO work to occur.  This can issue new pending
417737e150eSPaolo Bonzini  * aio as a result of executing I/O completion or bh callbacks.
418737e150eSPaolo Bonzini  *
419acfb23adSPaolo Bonzini  * Return whether any progress was made by executing AIO or bottom half
420acfb23adSPaolo Bonzini  * handlers.  If @blocking == true, this should always be true except
421acfb23adSPaolo Bonzini  * if someone called aio_notify.
422737e150eSPaolo Bonzini  *
423737e150eSPaolo Bonzini  * If there are no pending bottom halves, but there are pending AIO
424737e150eSPaolo Bonzini  * operations, it may not be possible to make any progress without
425737e150eSPaolo Bonzini  * blocking.  If @blocking is true, this function will wait until one
426737e150eSPaolo Bonzini  * or more AIO events have completed, to ensure something has moved
427737e150eSPaolo Bonzini  * before returning.
428737e150eSPaolo Bonzini  */
429737e150eSPaolo Bonzini bool aio_poll(AioContext *ctx, bool blocking);
430737e150eSPaolo Bonzini 
431737e150eSPaolo Bonzini /* Register a file descriptor and associated callbacks.  Behaves very similarly
4326484e422SFam Zheng  * to qemu_set_fd_handler.  Unlike qemu_set_fd_handler, these callbacks will
43387f68d31SPaolo Bonzini  * be invoked when using aio_poll().
434737e150eSPaolo Bonzini  *
435737e150eSPaolo Bonzini  * Code that invokes AIO completion functions should rely on this function
436737e150eSPaolo Bonzini  * instead of qemu_set_fd_handler[2].
437737e150eSPaolo Bonzini  */
438737e150eSPaolo Bonzini void aio_set_fd_handler(AioContext *ctx,
439737e150eSPaolo Bonzini                         int fd,
440dca21ef2SFam Zheng                         bool is_external,
441737e150eSPaolo Bonzini                         IOHandler *io_read,
442737e150eSPaolo Bonzini                         IOHandler *io_write,
443f6a51c84SStefan Hajnoczi                         AioPollFn *io_poll,
444737e150eSPaolo Bonzini                         void *opaque);
445737e150eSPaolo Bonzini 
446684e508cSStefan Hajnoczi /* Set polling begin/end callbacks for a file descriptor that has already been
447684e508cSStefan Hajnoczi  * registered with aio_set_fd_handler.  Do nothing if the file descriptor is
448684e508cSStefan Hajnoczi  * not registered.
449684e508cSStefan Hajnoczi  */
450684e508cSStefan Hajnoczi void aio_set_fd_poll(AioContext *ctx, int fd,
451684e508cSStefan Hajnoczi                      IOHandler *io_poll_begin,
452684e508cSStefan Hajnoczi                      IOHandler *io_poll_end);
453684e508cSStefan Hajnoczi 
454737e150eSPaolo Bonzini /* Register an event notifier and associated callbacks.  Behaves very similarly
455737e150eSPaolo Bonzini  * to event_notifier_set_handler.  Unlike event_notifier_set_handler, these callbacks
45687f68d31SPaolo Bonzini  * will be invoked when using aio_poll().
457737e150eSPaolo Bonzini  *
458737e150eSPaolo Bonzini  * Code that invokes AIO completion functions should rely on this function
459737e150eSPaolo Bonzini  * instead of event_notifier_set_handler.
460737e150eSPaolo Bonzini  */
461737e150eSPaolo Bonzini void aio_set_event_notifier(AioContext *ctx,
462737e150eSPaolo Bonzini                             EventNotifier *notifier,
463dca21ef2SFam Zheng                             bool is_external,
464f6a51c84SStefan Hajnoczi                             EventNotifierHandler *io_read,
465f6a51c84SStefan Hajnoczi                             AioPollFn *io_poll);
466737e150eSPaolo Bonzini 
467684e508cSStefan Hajnoczi /* Set polling begin/end callbacks for an event notifier that has already been
468684e508cSStefan Hajnoczi  * registered with aio_set_event_notifier.  Do nothing if the event notifier is
469684e508cSStefan Hajnoczi  * not registered.
470684e508cSStefan Hajnoczi  */
471684e508cSStefan Hajnoczi void aio_set_event_notifier_poll(AioContext *ctx,
472684e508cSStefan Hajnoczi                                  EventNotifier *notifier,
473684e508cSStefan Hajnoczi                                  EventNotifierHandler *io_poll_begin,
474684e508cSStefan Hajnoczi                                  EventNotifierHandler *io_poll_end);
475684e508cSStefan Hajnoczi 
476737e150eSPaolo Bonzini /* Return a GSource that lets the main loop poll the file descriptors attached
477737e150eSPaolo Bonzini  * to this AioContext.
478737e150eSPaolo Bonzini  */
479737e150eSPaolo Bonzini GSource *aio_get_g_source(AioContext *ctx);
480737e150eSPaolo Bonzini 
4819b34277dSStefan Hajnoczi /* Return the ThreadPool bound to this AioContext */
4829b34277dSStefan Hajnoczi struct ThreadPool *aio_get_thread_pool(AioContext *ctx);
4839b34277dSStefan Hajnoczi 
484ed6e2161SNishanth Aravamudan /* Setup the LinuxAioState bound to this AioContext */
485ed6e2161SNishanth Aravamudan struct LinuxAioState *aio_setup_linux_aio(AioContext *ctx, Error **errp);
486ed6e2161SNishanth Aravamudan 
4870187f5c9SPaolo Bonzini /* Return the LinuxAioState bound to this AioContext */
4880187f5c9SPaolo Bonzini struct LinuxAioState *aio_get_linux_aio(AioContext *ctx);
4890187f5c9SPaolo Bonzini 
4906663a0a3SAarushi Mehta /* Setup the LuringState bound to this AioContext */
4916663a0a3SAarushi Mehta struct LuringState *aio_setup_linux_io_uring(AioContext *ctx, Error **errp);
4926663a0a3SAarushi Mehta 
4936663a0a3SAarushi Mehta /* Return the LuringState bound to this AioContext */
4946663a0a3SAarushi Mehta struct LuringState *aio_get_linux_io_uring(AioContext *ctx);
4954e29e831SAlex Bligh /**
49689a603a0SArtem Pisarenko  * aio_timer_new_with_attrs:
49789a603a0SArtem Pisarenko  * @ctx: the aio context
49889a603a0SArtem Pisarenko  * @type: the clock type
49989a603a0SArtem Pisarenko  * @scale: the scale
50089a603a0SArtem Pisarenko  * @attributes: 0, or one to multiple OR'ed QEMU_TIMER_ATTR_<id> values
50189a603a0SArtem Pisarenko  *              to assign
50289a603a0SArtem Pisarenko  * @cb: the callback to call on timer expiry
50389a603a0SArtem Pisarenko  * @opaque: the opaque pointer to pass to the callback
50489a603a0SArtem Pisarenko  *
50589a603a0SArtem Pisarenko  * Allocate a new timer (with attributes) attached to the context @ctx.
50689a603a0SArtem Pisarenko  * The function is responsible for memory allocation.
50789a603a0SArtem Pisarenko  *
50889a603a0SArtem Pisarenko  * The preferred interface is aio_timer_init or aio_timer_init_with_attrs.
50989a603a0SArtem Pisarenko  * Use that unless you really need dynamic memory allocation.
51089a603a0SArtem Pisarenko  *
51189a603a0SArtem Pisarenko  * Returns: a pointer to the new timer
51289a603a0SArtem Pisarenko  */
51389a603a0SArtem Pisarenko static inline QEMUTimer *aio_timer_new_with_attrs(AioContext *ctx,
51489a603a0SArtem Pisarenko                                                   QEMUClockType type,
51589a603a0SArtem Pisarenko                                                   int scale, int attributes,
51689a603a0SArtem Pisarenko                                                   QEMUTimerCB *cb, void *opaque)
51789a603a0SArtem Pisarenko {
51889a603a0SArtem Pisarenko     return timer_new_full(&ctx->tlg, type, scale, attributes, cb, opaque);
51989a603a0SArtem Pisarenko }
52089a603a0SArtem Pisarenko 
52189a603a0SArtem Pisarenko /**
5224e29e831SAlex Bligh  * aio_timer_new:
5234e29e831SAlex Bligh  * @ctx: the aio context
5244e29e831SAlex Bligh  * @type: the clock type
5254e29e831SAlex Bligh  * @scale: the scale
5264e29e831SAlex Bligh  * @cb: the callback to call on timer expiry
5274e29e831SAlex Bligh  * @opaque: the opaque pointer to pass to the callback
5284e29e831SAlex Bligh  *
5294e29e831SAlex Bligh  * Allocate a new timer attached to the context @ctx.
53089a603a0SArtem Pisarenko  * See aio_timer_new_with_attrs for details.
5314e29e831SAlex Bligh  *
5324e29e831SAlex Bligh  * Returns: a pointer to the new timer
5334e29e831SAlex Bligh  */
5344e29e831SAlex Bligh static inline QEMUTimer *aio_timer_new(AioContext *ctx, QEMUClockType type,
5354e29e831SAlex Bligh                                        int scale,
5364e29e831SAlex Bligh                                        QEMUTimerCB *cb, void *opaque)
5374e29e831SAlex Bligh {
53889a603a0SArtem Pisarenko     return timer_new_full(&ctx->tlg, type, scale, 0, cb, opaque);
53989a603a0SArtem Pisarenko }
54089a603a0SArtem Pisarenko 
54189a603a0SArtem Pisarenko /**
54289a603a0SArtem Pisarenko  * aio_timer_init_with_attrs:
54389a603a0SArtem Pisarenko  * @ctx: the aio context
54489a603a0SArtem Pisarenko  * @ts: the timer
54589a603a0SArtem Pisarenko  * @type: the clock type
54689a603a0SArtem Pisarenko  * @scale: the scale
54789a603a0SArtem Pisarenko  * @attributes: 0, or one to multiple OR'ed QEMU_TIMER_ATTR_<id> values
54889a603a0SArtem Pisarenko  *              to assign
54989a603a0SArtem Pisarenko  * @cb: the callback to call on timer expiry
55089a603a0SArtem Pisarenko  * @opaque: the opaque pointer to pass to the callback
55189a603a0SArtem Pisarenko  *
55289a603a0SArtem Pisarenko  * Initialise a new timer (with attributes) attached to the context @ctx.
55389a603a0SArtem Pisarenko  * The caller is responsible for memory allocation.
55489a603a0SArtem Pisarenko  */
55589a603a0SArtem Pisarenko static inline void aio_timer_init_with_attrs(AioContext *ctx,
55689a603a0SArtem Pisarenko                                              QEMUTimer *ts, QEMUClockType type,
55789a603a0SArtem Pisarenko                                              int scale, int attributes,
55889a603a0SArtem Pisarenko                                              QEMUTimerCB *cb, void *opaque)
55989a603a0SArtem Pisarenko {
56089a603a0SArtem Pisarenko     timer_init_full(ts, &ctx->tlg, type, scale, attributes, cb, opaque);
5614e29e831SAlex Bligh }
5624e29e831SAlex Bligh 
5634e29e831SAlex Bligh /**
5644e29e831SAlex Bligh  * aio_timer_init:
5654e29e831SAlex Bligh  * @ctx: the aio context
5664e29e831SAlex Bligh  * @ts: the timer
5674e29e831SAlex Bligh  * @type: the clock type
5684e29e831SAlex Bligh  * @scale: the scale
5694e29e831SAlex Bligh  * @cb: the callback to call on timer expiry
5704e29e831SAlex Bligh  * @opaque: the opaque pointer to pass to the callback
5714e29e831SAlex Bligh  *
5724e29e831SAlex Bligh  * Initialise a new timer attached to the context @ctx.
57389a603a0SArtem Pisarenko  * See aio_timer_init_with_attrs for details.
5744e29e831SAlex Bligh  */
5754e29e831SAlex Bligh static inline void aio_timer_init(AioContext *ctx,
5764e29e831SAlex Bligh                                   QEMUTimer *ts, QEMUClockType type,
5774e29e831SAlex Bligh                                   int scale,
5784e29e831SAlex Bligh                                   QEMUTimerCB *cb, void *opaque)
5794e29e831SAlex Bligh {
58089a603a0SArtem Pisarenko     timer_init_full(ts, &ctx->tlg, type, scale, 0, cb, opaque);
5814e29e831SAlex Bligh }
5824e29e831SAlex Bligh 
583845ca10dSPaolo Bonzini /**
584845ca10dSPaolo Bonzini  * aio_compute_timeout:
585845ca10dSPaolo Bonzini  * @ctx: the aio context
586845ca10dSPaolo Bonzini  *
587845ca10dSPaolo Bonzini  * Compute the timeout that a blocking aio_poll should use.
588845ca10dSPaolo Bonzini  */
589845ca10dSPaolo Bonzini int64_t aio_compute_timeout(AioContext *ctx);
590845ca10dSPaolo Bonzini 
591c1e1e5faSFam Zheng /**
592c1e1e5faSFam Zheng  * aio_disable_external:
593c1e1e5faSFam Zheng  * @ctx: the aio context
594c1e1e5faSFam Zheng  *
595c1e1e5faSFam Zheng  * Disable the further processing of external clients.
596c1e1e5faSFam Zheng  */
597c1e1e5faSFam Zheng static inline void aio_disable_external(AioContext *ctx)
598c1e1e5faSFam Zheng {
599d73415a3SStefan Hajnoczi     qatomic_inc(&ctx->external_disable_cnt);
600c1e1e5faSFam Zheng }
601c1e1e5faSFam Zheng 
602c1e1e5faSFam Zheng /**
603c1e1e5faSFam Zheng  * aio_enable_external:
604c1e1e5faSFam Zheng  * @ctx: the aio context
605c1e1e5faSFam Zheng  *
606c1e1e5faSFam Zheng  * Enable the processing of external clients.
607c1e1e5faSFam Zheng  */
608c1e1e5faSFam Zheng static inline void aio_enable_external(AioContext *ctx)
609c1e1e5faSFam Zheng {
610321d1dbaSStefan Hajnoczi     int old;
611321d1dbaSStefan Hajnoczi 
612d73415a3SStefan Hajnoczi     old = qatomic_fetch_dec(&ctx->external_disable_cnt);
613321d1dbaSStefan Hajnoczi     assert(old > 0);
614321d1dbaSStefan Hajnoczi     if (old == 1) {
615321d1dbaSStefan Hajnoczi         /* Kick event loop so it re-arms file descriptors */
616321d1dbaSStefan Hajnoczi         aio_notify(ctx);
617321d1dbaSStefan Hajnoczi     }
618c1e1e5faSFam Zheng }
619c1e1e5faSFam Zheng 
620c1e1e5faSFam Zheng /**
6215ceb9e39SFam Zheng  * aio_external_disabled:
6225ceb9e39SFam Zheng  * @ctx: the aio context
6235ceb9e39SFam Zheng  *
6245ceb9e39SFam Zheng  * Return true if the external clients are disabled.
6255ceb9e39SFam Zheng  */
6265ceb9e39SFam Zheng static inline bool aio_external_disabled(AioContext *ctx)
6275ceb9e39SFam Zheng {
628d73415a3SStefan Hajnoczi     return qatomic_read(&ctx->external_disable_cnt);
6295ceb9e39SFam Zheng }
6305ceb9e39SFam Zheng 
6315ceb9e39SFam Zheng /**
632c1e1e5faSFam Zheng  * aio_node_check:
633c1e1e5faSFam Zheng  * @ctx: the aio context
634c1e1e5faSFam Zheng  * @is_external: Whether or not the checked node is an external event source.
635c1e1e5faSFam Zheng  *
636c1e1e5faSFam Zheng  * Check if the node's is_external flag is okay to be polled by the ctx at this
637c1e1e5faSFam Zheng  * moment. True means green light.
638c1e1e5faSFam Zheng  */
639c1e1e5faSFam Zheng static inline bool aio_node_check(AioContext *ctx, bool is_external)
640c1e1e5faSFam Zheng {
641d73415a3SStefan Hajnoczi     return !is_external || !qatomic_read(&ctx->external_disable_cnt);
642c1e1e5faSFam Zheng }
643c1e1e5faSFam Zheng 
64437fcee5dSFam Zheng /**
6450c330a73SPaolo Bonzini  * aio_co_schedule:
6460c330a73SPaolo Bonzini  * @ctx: the aio context
6470c330a73SPaolo Bonzini  * @co: the coroutine
6480c330a73SPaolo Bonzini  *
6490c330a73SPaolo Bonzini  * Start a coroutine on a remote AioContext.
6500c330a73SPaolo Bonzini  *
6510c330a73SPaolo Bonzini  * The coroutine must not be entered by anyone else while aio_co_schedule()
6520c330a73SPaolo Bonzini  * is active.  In addition the coroutine must have yielded unless ctx
6530c330a73SPaolo Bonzini  * is the context in which the coroutine is running (i.e. the value of
6540c330a73SPaolo Bonzini  * qemu_get_current_aio_context() from the coroutine itself).
6550c330a73SPaolo Bonzini  */
6560c330a73SPaolo Bonzini void aio_co_schedule(AioContext *ctx, struct Coroutine *co);
6570c330a73SPaolo Bonzini 
6580c330a73SPaolo Bonzini /**
65926b0b698SKevin Wolf  * aio_co_reschedule_self:
66026b0b698SKevin Wolf  * @new_ctx: the new context
66126b0b698SKevin Wolf  *
66226b0b698SKevin Wolf  * Move the currently running coroutine to new_ctx. If the coroutine is already
66326b0b698SKevin Wolf  * running in new_ctx, do nothing.
66426b0b698SKevin Wolf  */
66526b0b698SKevin Wolf void coroutine_fn aio_co_reschedule_self(AioContext *new_ctx);
66626b0b698SKevin Wolf 
66726b0b698SKevin Wolf /**
6680c330a73SPaolo Bonzini  * aio_co_wake:
6690c330a73SPaolo Bonzini  * @co: the coroutine
6700c330a73SPaolo Bonzini  *
6710c330a73SPaolo Bonzini  * Restart a coroutine on the AioContext where it was running last, thus
6720c330a73SPaolo Bonzini  * preventing coroutines from jumping from one context to another when they
6730c330a73SPaolo Bonzini  * go to sleep.
6740c330a73SPaolo Bonzini  *
6750c330a73SPaolo Bonzini  * aio_co_wake may be executed either in coroutine or non-coroutine
6760c330a73SPaolo Bonzini  * context.  The coroutine must not be entered by anyone else while
6770c330a73SPaolo Bonzini  * aio_co_wake() is active.
6780c330a73SPaolo Bonzini  */
6790c330a73SPaolo Bonzini void aio_co_wake(struct Coroutine *co);
6800c330a73SPaolo Bonzini 
6810c330a73SPaolo Bonzini /**
6828865852eSFam Zheng  * aio_co_enter:
6838865852eSFam Zheng  * @ctx: the context to run the coroutine
6848865852eSFam Zheng  * @co: the coroutine to run
6858865852eSFam Zheng  *
6868865852eSFam Zheng  * Enter a coroutine in the specified AioContext.
6878865852eSFam Zheng  */
6888865852eSFam Zheng void aio_co_enter(AioContext *ctx, struct Coroutine *co);
6898865852eSFam Zheng 
6908865852eSFam Zheng /**
691e4370165SPaolo Bonzini  * Return the AioContext whose event loop runs in the current thread.
692e4370165SPaolo Bonzini  *
693e4370165SPaolo Bonzini  * If called from an IOThread this will be the IOThread's AioContext.  If
694*5f50be9bSPaolo Bonzini  * called from the main thread or with the "big QEMU lock" taken it
695*5f50be9bSPaolo Bonzini  * will be the main loop AioContext.
696e4370165SPaolo Bonzini  */
697e4370165SPaolo Bonzini AioContext *qemu_get_current_aio_context(void);
698e4370165SPaolo Bonzini 
699*5f50be9bSPaolo Bonzini void qemu_set_current_aio_context(AioContext *ctx);
700*5f50be9bSPaolo Bonzini 
701e4370165SPaolo Bonzini /**
70237fcee5dSFam Zheng  * aio_context_setup:
70337fcee5dSFam Zheng  * @ctx: the aio context
70437fcee5dSFam Zheng  *
70537fcee5dSFam Zheng  * Initialize the aio context.
70637fcee5dSFam Zheng  */
7077e003465SCao jin void aio_context_setup(AioContext *ctx);
70837fcee5dSFam Zheng 
7094a1cba38SStefan Hajnoczi /**
710cd0a6d2bSJie Wang  * aio_context_destroy:
711cd0a6d2bSJie Wang  * @ctx: the aio context
712cd0a6d2bSJie Wang  *
713cd0a6d2bSJie Wang  * Destroy the aio context.
714cd0a6d2bSJie Wang  */
715cd0a6d2bSJie Wang void aio_context_destroy(AioContext *ctx);
716cd0a6d2bSJie Wang 
717ba607ca8SStefan Hajnoczi /* Used internally, do not call outside AioContext code */
718ba607ca8SStefan Hajnoczi void aio_context_use_g_source(AioContext *ctx);
719ba607ca8SStefan Hajnoczi 
720cd0a6d2bSJie Wang /**
7214a1cba38SStefan Hajnoczi  * aio_context_set_poll_params:
7224a1cba38SStefan Hajnoczi  * @ctx: the aio context
7234a1cba38SStefan Hajnoczi  * @max_ns: how long to busy poll for, in nanoseconds
72482a41186SStefan Hajnoczi  * @grow: polling time growth factor
72582a41186SStefan Hajnoczi  * @shrink: polling time shrink factor
7264a1cba38SStefan Hajnoczi  *
7274a1cba38SStefan Hajnoczi  * Poll mode can be disabled by setting poll_max_ns to 0.
7284a1cba38SStefan Hajnoczi  */
7294a1cba38SStefan Hajnoczi void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
73082a41186SStefan Hajnoczi                                  int64_t grow, int64_t shrink,
7314a1cba38SStefan Hajnoczi                                  Error **errp);
7324a1cba38SStefan Hajnoczi 
733737e150eSPaolo Bonzini #endif
734