xref: /openbmc/qemu/include/block/aio.h (revision f6a51c84cdc97e0178aab7690788d3891d2fcf0a)
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 
17737e150eSPaolo Bonzini #include "qemu-common.h"
181de7afc9SPaolo Bonzini #include "qemu/queue.h"
191de7afc9SPaolo Bonzini #include "qemu/event_notifier.h"
20dcc772e2SLiu Ping Fan #include "qemu/thread.h"
21dae21b98SAlex Bligh #include "qemu/timer.h"
22737e150eSPaolo Bonzini 
237c84b1b8SMarkus Armbruster typedef struct BlockAIOCB BlockAIOCB;
24097310b5SMarkus Armbruster typedef void BlockCompletionFunc(void *opaque, int ret);
25737e150eSPaolo Bonzini 
26737e150eSPaolo Bonzini typedef struct AIOCBInfo {
277c84b1b8SMarkus Armbruster     void (*cancel_async)(BlockAIOCB *acb);
287c84b1b8SMarkus Armbruster     AioContext *(*get_aio_context)(BlockAIOCB *acb);
29737e150eSPaolo Bonzini     size_t aiocb_size;
30737e150eSPaolo Bonzini } AIOCBInfo;
31737e150eSPaolo Bonzini 
327c84b1b8SMarkus Armbruster struct BlockAIOCB {
33737e150eSPaolo Bonzini     const AIOCBInfo *aiocb_info;
34737e150eSPaolo Bonzini     BlockDriverState *bs;
35097310b5SMarkus Armbruster     BlockCompletionFunc *cb;
36737e150eSPaolo Bonzini     void *opaque;
37f197fe2bSFam Zheng     int refcnt;
38737e150eSPaolo Bonzini };
39737e150eSPaolo Bonzini 
40737e150eSPaolo Bonzini void *qemu_aio_get(const AIOCBInfo *aiocb_info, BlockDriverState *bs,
41097310b5SMarkus Armbruster                    BlockCompletionFunc *cb, void *opaque);
428007429aSFam Zheng void qemu_aio_unref(void *p);
43f197fe2bSFam Zheng void qemu_aio_ref(void *p);
44737e150eSPaolo Bonzini 
45737e150eSPaolo Bonzini typedef struct AioHandler AioHandler;
46737e150eSPaolo Bonzini typedef void QEMUBHFunc(void *opaque);
47*f6a51c84SStefan Hajnoczi typedef bool AioPollFn(void *opaque);
48737e150eSPaolo Bonzini typedef void IOHandler(void *opaque);
49737e150eSPaolo Bonzini 
500187f5c9SPaolo Bonzini struct ThreadPool;
510187f5c9SPaolo Bonzini struct LinuxAioState;
520187f5c9SPaolo Bonzini 
536a1751b7SAlex Bligh struct AioContext {
54737e150eSPaolo Bonzini     GSource source;
55737e150eSPaolo Bonzini 
5698563fc3SStefan Hajnoczi     /* Protects all fields from multi-threaded access */
573fe71223SPaolo Bonzini     QemuRecMutex lock;
5898563fc3SStefan Hajnoczi 
59737e150eSPaolo Bonzini     /* The list of registered AIO handlers */
60737e150eSPaolo Bonzini     QLIST_HEAD(, AioHandler) aio_handlers;
61737e150eSPaolo Bonzini 
62737e150eSPaolo Bonzini     /* This is a simple lock used to protect the aio_handlers list.
63737e150eSPaolo Bonzini      * Specifically, it's used to ensure that no callbacks are removed while
64737e150eSPaolo Bonzini      * we're walking and dispatching callbacks.
65737e150eSPaolo Bonzini      */
66737e150eSPaolo Bonzini     int walking_handlers;
67737e150eSPaolo Bonzini 
68eabc9779SPaolo Bonzini     /* Used to avoid unnecessary event_notifier_set calls in aio_notify;
69eabc9779SPaolo Bonzini      * accessed with atomic primitives.  If this field is 0, everything
70eabc9779SPaolo Bonzini      * (file descriptors, bottom halves, timers) will be re-evaluated
71eabc9779SPaolo Bonzini      * before the next blocking poll(), thus the event_notifier_set call
72eabc9779SPaolo Bonzini      * can be skipped.  If it is non-zero, you may need to wake up a
73eabc9779SPaolo Bonzini      * concurrent aio_poll or the glib main event loop, making
74eabc9779SPaolo Bonzini      * event_notifier_set necessary.
75eabc9779SPaolo Bonzini      *
76eabc9779SPaolo Bonzini      * Bit 0 is reserved for GSource usage of the AioContext, and is 1
7754a16a63SCao jin      * between a call to aio_ctx_prepare and the next call to aio_ctx_check.
78eabc9779SPaolo Bonzini      * Bits 1-31 simply count the number of active calls to aio_poll
79eabc9779SPaolo Bonzini      * that are in the prepare or poll phase.
80eabc9779SPaolo Bonzini      *
81eabc9779SPaolo Bonzini      * The GSource and aio_poll must use a different mechanism because
82eabc9779SPaolo Bonzini      * there is no certainty that a call to GSource's prepare callback
83eabc9779SPaolo Bonzini      * (via g_main_context_prepare) is indeed followed by check and
84eabc9779SPaolo Bonzini      * dispatch.  It's not clear whether this would be a bug, but let's
85eabc9779SPaolo Bonzini      * play safe and allow it---it will just cause extra calls to
86eabc9779SPaolo Bonzini      * event_notifier_set until the next call to dispatch.
87eabc9779SPaolo Bonzini      *
88eabc9779SPaolo Bonzini      * Instead, the aio_poll calls include both the prepare and the
89eabc9779SPaolo Bonzini      * dispatch phase, hence a simple counter is enough for them.
900ceb849bSPaolo Bonzini      */
91eabc9779SPaolo Bonzini     uint32_t notify_me;
920ceb849bSPaolo Bonzini 
93dcc772e2SLiu Ping Fan     /* lock to protect between bh's adders and deleter */
94dcc772e2SLiu Ping Fan     QemuMutex bh_lock;
950ceb849bSPaolo Bonzini 
96737e150eSPaolo Bonzini     /* Anchor of the list of Bottom Halves belonging to the context */
97737e150eSPaolo Bonzini     struct QEMUBH *first_bh;
98737e150eSPaolo Bonzini 
99737e150eSPaolo Bonzini     /* A simple lock used to protect the first_bh list, and ensure that
100737e150eSPaolo Bonzini      * no callbacks are removed while we're walking and dispatching callbacks.
101737e150eSPaolo Bonzini      */
102737e150eSPaolo Bonzini     int walking_bh;
103737e150eSPaolo Bonzini 
10405e514b1SPaolo Bonzini     /* Used by aio_notify.
10505e514b1SPaolo Bonzini      *
10605e514b1SPaolo Bonzini      * "notified" is used to avoid expensive event_notifier_test_and_clear
10705e514b1SPaolo Bonzini      * calls.  When it is clear, the EventNotifier is clear, or one thread
10805e514b1SPaolo Bonzini      * is going to clear "notified" before processing more events.  False
10905e514b1SPaolo Bonzini      * positives are possible, i.e. "notified" could be set even though the
11005e514b1SPaolo Bonzini      * EventNotifier is clear.
11105e514b1SPaolo Bonzini      *
11205e514b1SPaolo Bonzini      * Note that event_notifier_set *cannot* be optimized the same way.  For
11305e514b1SPaolo Bonzini      * more information on the problem that would result, see "#ifdef BUG2"
11405e514b1SPaolo Bonzini      * in the docs/aio_notify_accept.promela formal model.
11505e514b1SPaolo Bonzini      */
11605e514b1SPaolo Bonzini     bool notified;
117737e150eSPaolo Bonzini     EventNotifier notifier;
1186b5f8762SStefan Hajnoczi 
1199b34277dSStefan Hajnoczi     /* Thread pool for performing work and receiving completion callbacks */
1209b34277dSStefan Hajnoczi     struct ThreadPool *thread_pool;
121dae21b98SAlex Bligh 
1220187f5c9SPaolo Bonzini #ifdef CONFIG_LINUX_AIO
1230187f5c9SPaolo Bonzini     /* State for native Linux AIO.  Uses aio_context_acquire/release for
1240187f5c9SPaolo Bonzini      * locking.
1250187f5c9SPaolo Bonzini      */
1260187f5c9SPaolo Bonzini     struct LinuxAioState *linux_aio;
1270187f5c9SPaolo Bonzini #endif
1280187f5c9SPaolo Bonzini 
129dae21b98SAlex Bligh     /* TimerLists for calling timers - one per clock type */
130dae21b98SAlex Bligh     QEMUTimerListGroup tlg;
131c1e1e5faSFam Zheng 
132c1e1e5faSFam Zheng     int external_disable_cnt;
133fbe3fc5cSFam Zheng 
134fbe3fc5cSFam Zheng     /* epoll(7) state used when built with CONFIG_EPOLL */
135fbe3fc5cSFam Zheng     int epollfd;
136fbe3fc5cSFam Zheng     bool epoll_enabled;
137fbe3fc5cSFam Zheng     bool epoll_available;
1386a1751b7SAlex Bligh };
139737e150eSPaolo Bonzini 
140737e150eSPaolo Bonzini /**
141737e150eSPaolo Bonzini  * aio_context_new: Allocate a new AioContext.
142737e150eSPaolo Bonzini  *
143737e150eSPaolo Bonzini  * AioContext provide a mini event-loop that can be waited on synchronously.
144737e150eSPaolo Bonzini  * They also provide bottom halves, a service to execute a piece of code
145737e150eSPaolo Bonzini  * as soon as possible.
146737e150eSPaolo Bonzini  */
1472f78e491SChrysostomos Nanakos AioContext *aio_context_new(Error **errp);
148737e150eSPaolo Bonzini 
149737e150eSPaolo Bonzini /**
150737e150eSPaolo Bonzini  * aio_context_ref:
151737e150eSPaolo Bonzini  * @ctx: The AioContext to operate on.
152737e150eSPaolo Bonzini  *
153737e150eSPaolo Bonzini  * Add a reference to an AioContext.
154737e150eSPaolo Bonzini  */
155737e150eSPaolo Bonzini void aio_context_ref(AioContext *ctx);
156737e150eSPaolo Bonzini 
157737e150eSPaolo Bonzini /**
158737e150eSPaolo Bonzini  * aio_context_unref:
159737e150eSPaolo Bonzini  * @ctx: The AioContext to operate on.
160737e150eSPaolo Bonzini  *
161737e150eSPaolo Bonzini  * Drop a reference to an AioContext.
162737e150eSPaolo Bonzini  */
163737e150eSPaolo Bonzini void aio_context_unref(AioContext *ctx);
164737e150eSPaolo Bonzini 
16598563fc3SStefan Hajnoczi /* Take ownership of the AioContext.  If the AioContext will be shared between
16649110174SPaolo Bonzini  * threads, and a thread does not want to be interrupted, it will have to
16749110174SPaolo Bonzini  * take ownership around calls to aio_poll().  Otherwise, aio_poll()
16849110174SPaolo Bonzini  * automatically takes care of calling aio_context_acquire and
16949110174SPaolo Bonzini  * aio_context_release.
17098563fc3SStefan Hajnoczi  *
17149110174SPaolo Bonzini  * Access to timers and BHs from a thread that has not acquired AioContext
17249110174SPaolo Bonzini  * is possible.  Access to callbacks for now must be done while the AioContext
17349110174SPaolo Bonzini  * is owned by the thread (FIXME).
17498563fc3SStefan Hajnoczi  */
17598563fc3SStefan Hajnoczi void aio_context_acquire(AioContext *ctx);
17698563fc3SStefan Hajnoczi 
17798563fc3SStefan Hajnoczi /* Relinquish ownership of the AioContext. */
17898563fc3SStefan Hajnoczi void aio_context_release(AioContext *ctx);
17998563fc3SStefan Hajnoczi 
180737e150eSPaolo Bonzini /**
1815b8bb359SPaolo Bonzini  * aio_bh_schedule_oneshot: Allocate a new bottom half structure that will run
1825b8bb359SPaolo Bonzini  * only once and as soon as possible.
1835b8bb359SPaolo Bonzini  */
1845b8bb359SPaolo Bonzini void aio_bh_schedule_oneshot(AioContext *ctx, QEMUBHFunc *cb, void *opaque);
1855b8bb359SPaolo Bonzini 
1865b8bb359SPaolo Bonzini /**
187737e150eSPaolo Bonzini  * aio_bh_new: Allocate a new bottom half structure.
188737e150eSPaolo Bonzini  *
189737e150eSPaolo Bonzini  * Bottom halves are lightweight callbacks whose invocation is guaranteed
190737e150eSPaolo Bonzini  * to be wait-free, thread-safe and signal-safe.  The #QEMUBH structure
191737e150eSPaolo Bonzini  * is opaque and must be allocated prior to its use.
192737e150eSPaolo Bonzini  */
193737e150eSPaolo Bonzini QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque);
194737e150eSPaolo Bonzini 
195737e150eSPaolo Bonzini /**
196737e150eSPaolo Bonzini  * aio_notify: Force processing of pending events.
197737e150eSPaolo Bonzini  *
198737e150eSPaolo Bonzini  * Similar to signaling a condition variable, aio_notify forces
199722f8d90SYaowei Bai  * aio_poll to exit, so that the next call will re-examine pending events.
200722f8d90SYaowei Bai  * The caller of aio_notify will usually call aio_poll again very soon,
201737e150eSPaolo Bonzini  * or go through another iteration of the GLib main loop.  Hence, aio_notify
202737e150eSPaolo Bonzini  * also has the side effect of recalculating the sets of file descriptors
203737e150eSPaolo Bonzini  * that the main loop waits for.
204737e150eSPaolo Bonzini  *
205737e150eSPaolo Bonzini  * Calling aio_notify is rarely necessary, because for example scheduling
206737e150eSPaolo Bonzini  * a bottom half calls it already.
207737e150eSPaolo Bonzini  */
208737e150eSPaolo Bonzini void aio_notify(AioContext *ctx);
209737e150eSPaolo Bonzini 
210737e150eSPaolo Bonzini /**
21105e514b1SPaolo Bonzini  * aio_notify_accept: Acknowledge receiving an aio_notify.
21205e514b1SPaolo Bonzini  *
21305e514b1SPaolo Bonzini  * aio_notify() uses an EventNotifier in order to wake up a sleeping
21405e514b1SPaolo Bonzini  * aio_poll() or g_main_context_iteration().  Calls to aio_notify() are
21505e514b1SPaolo Bonzini  * usually rare, but the AioContext has to clear the EventNotifier on
21605e514b1SPaolo Bonzini  * every aio_poll() or g_main_context_iteration() in order to avoid
21705e514b1SPaolo Bonzini  * busy waiting.  This event_notifier_test_and_clear() cannot be done
21805e514b1SPaolo Bonzini  * using the usual aio_context_set_event_notifier(), because it must
21905e514b1SPaolo Bonzini  * be done before processing all events (file descriptors, bottom halves,
22005e514b1SPaolo Bonzini  * timers).
22105e514b1SPaolo Bonzini  *
22205e514b1SPaolo Bonzini  * aio_notify_accept() is an optimized event_notifier_test_and_clear()
22305e514b1SPaolo Bonzini  * that is specific to an AioContext's notifier; it is used internally
22405e514b1SPaolo Bonzini  * to clear the EventNotifier only if aio_notify() had been called.
22505e514b1SPaolo Bonzini  */
22605e514b1SPaolo Bonzini void aio_notify_accept(AioContext *ctx);
22705e514b1SPaolo Bonzini 
22805e514b1SPaolo Bonzini /**
229df281b80SPavel Dovgalyuk  * aio_bh_call: Executes callback function of the specified BH.
230df281b80SPavel Dovgalyuk  */
231df281b80SPavel Dovgalyuk void aio_bh_call(QEMUBH *bh);
232df281b80SPavel Dovgalyuk 
233df281b80SPavel Dovgalyuk /**
234737e150eSPaolo Bonzini  * aio_bh_poll: Poll bottom halves for an AioContext.
235737e150eSPaolo Bonzini  *
236737e150eSPaolo Bonzini  * These are internal functions used by the QEMU main loop.
237dcc772e2SLiu Ping Fan  * And notice that multiple occurrences of aio_bh_poll cannot
238dcc772e2SLiu Ping Fan  * be called concurrently
239737e150eSPaolo Bonzini  */
240737e150eSPaolo Bonzini int aio_bh_poll(AioContext *ctx);
241737e150eSPaolo Bonzini 
242737e150eSPaolo Bonzini /**
243737e150eSPaolo Bonzini  * qemu_bh_schedule: Schedule a bottom half.
244737e150eSPaolo Bonzini  *
245737e150eSPaolo Bonzini  * Scheduling a bottom half interrupts the main loop and causes the
246737e150eSPaolo Bonzini  * execution of the callback that was passed to qemu_bh_new.
247737e150eSPaolo Bonzini  *
248737e150eSPaolo Bonzini  * Bottom halves that are scheduled from a bottom half handler are instantly
249737e150eSPaolo Bonzini  * invoked.  This can create an infinite loop if a bottom half handler
250737e150eSPaolo Bonzini  * schedules itself.
251737e150eSPaolo Bonzini  *
252737e150eSPaolo Bonzini  * @bh: The bottom half to be scheduled.
253737e150eSPaolo Bonzini  */
254737e150eSPaolo Bonzini void qemu_bh_schedule(QEMUBH *bh);
255737e150eSPaolo Bonzini 
256737e150eSPaolo Bonzini /**
257737e150eSPaolo Bonzini  * qemu_bh_cancel: Cancel execution of a bottom half.
258737e150eSPaolo Bonzini  *
259737e150eSPaolo Bonzini  * Canceling execution of a bottom half undoes the effect of calls to
260737e150eSPaolo Bonzini  * qemu_bh_schedule without freeing its resources yet.  While cancellation
261737e150eSPaolo Bonzini  * itself is also wait-free and thread-safe, it can of course race with the
262737e150eSPaolo Bonzini  * loop that executes bottom halves unless you are holding the iothread
263737e150eSPaolo Bonzini  * mutex.  This makes it mostly useless if you are not holding the mutex.
264737e150eSPaolo Bonzini  *
265737e150eSPaolo Bonzini  * @bh: The bottom half to be canceled.
266737e150eSPaolo Bonzini  */
267737e150eSPaolo Bonzini void qemu_bh_cancel(QEMUBH *bh);
268737e150eSPaolo Bonzini 
269737e150eSPaolo Bonzini /**
270737e150eSPaolo Bonzini  *qemu_bh_delete: Cancel execution of a bottom half and free its resources.
271737e150eSPaolo Bonzini  *
272737e150eSPaolo Bonzini  * Deleting a bottom half frees the memory that was allocated for it by
273737e150eSPaolo Bonzini  * qemu_bh_new.  It also implies canceling the bottom half if it was
274737e150eSPaolo Bonzini  * scheduled.
275dcc772e2SLiu Ping Fan  * This func is async. The bottom half will do the delete action at the finial
276dcc772e2SLiu Ping Fan  * end.
277737e150eSPaolo Bonzini  *
278737e150eSPaolo Bonzini  * @bh: The bottom half to be deleted.
279737e150eSPaolo Bonzini  */
280737e150eSPaolo Bonzini void qemu_bh_delete(QEMUBH *bh);
281737e150eSPaolo Bonzini 
282737e150eSPaolo Bonzini /* Return whether there are any pending callbacks from the GSource
283a3462c65SPaolo Bonzini  * attached to the AioContext, before g_poll is invoked.
284a3462c65SPaolo Bonzini  *
285a3462c65SPaolo Bonzini  * This is used internally in the implementation of the GSource.
286a3462c65SPaolo Bonzini  */
287a3462c65SPaolo Bonzini bool aio_prepare(AioContext *ctx);
288a3462c65SPaolo Bonzini 
289a3462c65SPaolo Bonzini /* Return whether there are any pending callbacks from the GSource
290a3462c65SPaolo Bonzini  * attached to the AioContext, after g_poll is invoked.
291737e150eSPaolo Bonzini  *
292737e150eSPaolo Bonzini  * This is used internally in the implementation of the GSource.
293737e150eSPaolo Bonzini  */
294737e150eSPaolo Bonzini bool aio_pending(AioContext *ctx);
295737e150eSPaolo Bonzini 
296e4c7e2d1SPaolo Bonzini /* Dispatch any pending callbacks from the GSource attached to the AioContext.
297e4c7e2d1SPaolo Bonzini  *
298e4c7e2d1SPaolo Bonzini  * This is used internally in the implementation of the GSource.
299721671adSStefan Hajnoczi  *
300721671adSStefan Hajnoczi  * @dispatch_fds: true to process fds, false to skip them
301721671adSStefan Hajnoczi  *                (can be used as an optimization by callers that know there
302721671adSStefan Hajnoczi  *                are no fds ready)
303e4c7e2d1SPaolo Bonzini  */
304721671adSStefan Hajnoczi bool aio_dispatch(AioContext *ctx, bool dispatch_fds);
305e4c7e2d1SPaolo Bonzini 
306737e150eSPaolo Bonzini /* Progress in completing AIO work to occur.  This can issue new pending
307737e150eSPaolo Bonzini  * aio as a result of executing I/O completion or bh callbacks.
308737e150eSPaolo Bonzini  *
309acfb23adSPaolo Bonzini  * Return whether any progress was made by executing AIO or bottom half
310acfb23adSPaolo Bonzini  * handlers.  If @blocking == true, this should always be true except
311acfb23adSPaolo Bonzini  * if someone called aio_notify.
312737e150eSPaolo Bonzini  *
313737e150eSPaolo Bonzini  * If there are no pending bottom halves, but there are pending AIO
314737e150eSPaolo Bonzini  * operations, it may not be possible to make any progress without
315737e150eSPaolo Bonzini  * blocking.  If @blocking is true, this function will wait until one
316737e150eSPaolo Bonzini  * or more AIO events have completed, to ensure something has moved
317737e150eSPaolo Bonzini  * before returning.
318737e150eSPaolo Bonzini  */
319737e150eSPaolo Bonzini bool aio_poll(AioContext *ctx, bool blocking);
320737e150eSPaolo Bonzini 
321737e150eSPaolo Bonzini /* Register a file descriptor and associated callbacks.  Behaves very similarly
3226484e422SFam Zheng  * to qemu_set_fd_handler.  Unlike qemu_set_fd_handler, these callbacks will
32387f68d31SPaolo Bonzini  * be invoked when using aio_poll().
324737e150eSPaolo Bonzini  *
325737e150eSPaolo Bonzini  * Code that invokes AIO completion functions should rely on this function
326737e150eSPaolo Bonzini  * instead of qemu_set_fd_handler[2].
327737e150eSPaolo Bonzini  */
328737e150eSPaolo Bonzini void aio_set_fd_handler(AioContext *ctx,
329737e150eSPaolo Bonzini                         int fd,
330dca21ef2SFam Zheng                         bool is_external,
331737e150eSPaolo Bonzini                         IOHandler *io_read,
332737e150eSPaolo Bonzini                         IOHandler *io_write,
333*f6a51c84SStefan Hajnoczi                         AioPollFn *io_poll,
334737e150eSPaolo Bonzini                         void *opaque);
335737e150eSPaolo Bonzini 
336737e150eSPaolo Bonzini /* Register an event notifier and associated callbacks.  Behaves very similarly
337737e150eSPaolo Bonzini  * to event_notifier_set_handler.  Unlike event_notifier_set_handler, these callbacks
33887f68d31SPaolo Bonzini  * will be invoked when using aio_poll().
339737e150eSPaolo Bonzini  *
340737e150eSPaolo Bonzini  * Code that invokes AIO completion functions should rely on this function
341737e150eSPaolo Bonzini  * instead of event_notifier_set_handler.
342737e150eSPaolo Bonzini  */
343737e150eSPaolo Bonzini void aio_set_event_notifier(AioContext *ctx,
344737e150eSPaolo Bonzini                             EventNotifier *notifier,
345dca21ef2SFam Zheng                             bool is_external,
346*f6a51c84SStefan Hajnoczi                             EventNotifierHandler *io_read,
347*f6a51c84SStefan Hajnoczi                             AioPollFn *io_poll);
348737e150eSPaolo Bonzini 
349737e150eSPaolo Bonzini /* Return a GSource that lets the main loop poll the file descriptors attached
350737e150eSPaolo Bonzini  * to this AioContext.
351737e150eSPaolo Bonzini  */
352737e150eSPaolo Bonzini GSource *aio_get_g_source(AioContext *ctx);
353737e150eSPaolo Bonzini 
3549b34277dSStefan Hajnoczi /* Return the ThreadPool bound to this AioContext */
3559b34277dSStefan Hajnoczi struct ThreadPool *aio_get_thread_pool(AioContext *ctx);
3569b34277dSStefan Hajnoczi 
3570187f5c9SPaolo Bonzini /* Return the LinuxAioState bound to this AioContext */
3580187f5c9SPaolo Bonzini struct LinuxAioState *aio_get_linux_aio(AioContext *ctx);
3590187f5c9SPaolo Bonzini 
3604e29e831SAlex Bligh /**
3614e29e831SAlex Bligh  * aio_timer_new:
3624e29e831SAlex Bligh  * @ctx: the aio context
3634e29e831SAlex Bligh  * @type: the clock type
3644e29e831SAlex Bligh  * @scale: the scale
3654e29e831SAlex Bligh  * @cb: the callback to call on timer expiry
3664e29e831SAlex Bligh  * @opaque: the opaque pointer to pass to the callback
3674e29e831SAlex Bligh  *
3684e29e831SAlex Bligh  * Allocate a new timer attached to the context @ctx.
3694e29e831SAlex Bligh  * The function is responsible for memory allocation.
3704e29e831SAlex Bligh  *
3714e29e831SAlex Bligh  * The preferred interface is aio_timer_init. Use that
3724e29e831SAlex Bligh  * unless you really need dynamic memory allocation.
3734e29e831SAlex Bligh  *
3744e29e831SAlex Bligh  * Returns: a pointer to the new timer
3754e29e831SAlex Bligh  */
3764e29e831SAlex Bligh static inline QEMUTimer *aio_timer_new(AioContext *ctx, QEMUClockType type,
3774e29e831SAlex Bligh                                        int scale,
3784e29e831SAlex Bligh                                        QEMUTimerCB *cb, void *opaque)
3794e29e831SAlex Bligh {
3804e29e831SAlex Bligh     return timer_new_tl(ctx->tlg.tl[type], scale, cb, opaque);
3814e29e831SAlex Bligh }
3824e29e831SAlex Bligh 
3834e29e831SAlex Bligh /**
3844e29e831SAlex Bligh  * aio_timer_init:
3854e29e831SAlex Bligh  * @ctx: the aio context
3864e29e831SAlex Bligh  * @ts: the timer
3874e29e831SAlex Bligh  * @type: the clock type
3884e29e831SAlex Bligh  * @scale: the scale
3894e29e831SAlex Bligh  * @cb: the callback to call on timer expiry
3904e29e831SAlex Bligh  * @opaque: the opaque pointer to pass to the callback
3914e29e831SAlex Bligh  *
3924e29e831SAlex Bligh  * Initialise a new timer attached to the context @ctx.
3934e29e831SAlex Bligh  * The caller is responsible for memory allocation.
3944e29e831SAlex Bligh  */
3954e29e831SAlex Bligh static inline void aio_timer_init(AioContext *ctx,
3964e29e831SAlex Bligh                                   QEMUTimer *ts, QEMUClockType type,
3974e29e831SAlex Bligh                                   int scale,
3984e29e831SAlex Bligh                                   QEMUTimerCB *cb, void *opaque)
3994e29e831SAlex Bligh {
400f186aa97SPaolo Bonzini     timer_init_tl(ts, ctx->tlg.tl[type], scale, cb, opaque);
4014e29e831SAlex Bligh }
4024e29e831SAlex Bligh 
403845ca10dSPaolo Bonzini /**
404845ca10dSPaolo Bonzini  * aio_compute_timeout:
405845ca10dSPaolo Bonzini  * @ctx: the aio context
406845ca10dSPaolo Bonzini  *
407845ca10dSPaolo Bonzini  * Compute the timeout that a blocking aio_poll should use.
408845ca10dSPaolo Bonzini  */
409845ca10dSPaolo Bonzini int64_t aio_compute_timeout(AioContext *ctx);
410845ca10dSPaolo Bonzini 
411c1e1e5faSFam Zheng /**
412c1e1e5faSFam Zheng  * aio_disable_external:
413c1e1e5faSFam Zheng  * @ctx: the aio context
414c1e1e5faSFam Zheng  *
415c1e1e5faSFam Zheng  * Disable the further processing of external clients.
416c1e1e5faSFam Zheng  */
417c1e1e5faSFam Zheng static inline void aio_disable_external(AioContext *ctx)
418c1e1e5faSFam Zheng {
419c1e1e5faSFam Zheng     atomic_inc(&ctx->external_disable_cnt);
420c1e1e5faSFam Zheng }
421c1e1e5faSFam Zheng 
422c1e1e5faSFam Zheng /**
423c1e1e5faSFam Zheng  * aio_enable_external:
424c1e1e5faSFam Zheng  * @ctx: the aio context
425c1e1e5faSFam Zheng  *
426c1e1e5faSFam Zheng  * Enable the processing of external clients.
427c1e1e5faSFam Zheng  */
428c1e1e5faSFam Zheng static inline void aio_enable_external(AioContext *ctx)
429c1e1e5faSFam Zheng {
430c1e1e5faSFam Zheng     assert(ctx->external_disable_cnt > 0);
431c1e1e5faSFam Zheng     atomic_dec(&ctx->external_disable_cnt);
432c1e1e5faSFam Zheng }
433c1e1e5faSFam Zheng 
434c1e1e5faSFam Zheng /**
4355ceb9e39SFam Zheng  * aio_external_disabled:
4365ceb9e39SFam Zheng  * @ctx: the aio context
4375ceb9e39SFam Zheng  *
4385ceb9e39SFam Zheng  * Return true if the external clients are disabled.
4395ceb9e39SFam Zheng  */
4405ceb9e39SFam Zheng static inline bool aio_external_disabled(AioContext *ctx)
4415ceb9e39SFam Zheng {
4425ceb9e39SFam Zheng     return atomic_read(&ctx->external_disable_cnt);
4435ceb9e39SFam Zheng }
4445ceb9e39SFam Zheng 
4455ceb9e39SFam Zheng /**
446c1e1e5faSFam Zheng  * aio_node_check:
447c1e1e5faSFam Zheng  * @ctx: the aio context
448c1e1e5faSFam Zheng  * @is_external: Whether or not the checked node is an external event source.
449c1e1e5faSFam Zheng  *
450c1e1e5faSFam Zheng  * Check if the node's is_external flag is okay to be polled by the ctx at this
451c1e1e5faSFam Zheng  * moment. True means green light.
452c1e1e5faSFam Zheng  */
453c1e1e5faSFam Zheng static inline bool aio_node_check(AioContext *ctx, bool is_external)
454c1e1e5faSFam Zheng {
455c1e1e5faSFam Zheng     return !is_external || !atomic_read(&ctx->external_disable_cnt);
456c1e1e5faSFam Zheng }
457c1e1e5faSFam Zheng 
45837fcee5dSFam Zheng /**
459e4370165SPaolo Bonzini  * Return the AioContext whose event loop runs in the current thread.
460e4370165SPaolo Bonzini  *
461e4370165SPaolo Bonzini  * If called from an IOThread this will be the IOThread's AioContext.  If
462e4370165SPaolo Bonzini  * called from another thread it will be the main loop AioContext.
463e4370165SPaolo Bonzini  */
464e4370165SPaolo Bonzini AioContext *qemu_get_current_aio_context(void);
465e4370165SPaolo Bonzini 
466e4370165SPaolo Bonzini /**
467e4370165SPaolo Bonzini  * @ctx: the aio context
468e4370165SPaolo Bonzini  *
469e4370165SPaolo Bonzini  * Return whether we are running in the I/O thread that manages @ctx.
470e4370165SPaolo Bonzini  */
471e4370165SPaolo Bonzini static inline bool aio_context_in_iothread(AioContext *ctx)
472e4370165SPaolo Bonzini {
473e4370165SPaolo Bonzini     return ctx == qemu_get_current_aio_context();
474e4370165SPaolo Bonzini }
475e4370165SPaolo Bonzini 
476e4370165SPaolo Bonzini /**
47737fcee5dSFam Zheng  * aio_context_setup:
47837fcee5dSFam Zheng  * @ctx: the aio context
47937fcee5dSFam Zheng  *
48037fcee5dSFam Zheng  * Initialize the aio context.
48137fcee5dSFam Zheng  */
4827e003465SCao jin void aio_context_setup(AioContext *ctx);
48337fcee5dSFam Zheng 
484737e150eSPaolo Bonzini #endif
485