xref: /qemu/include/block/aio.h (revision d73415a3)
1 /*
2  * QEMU aio implementation
3  *
4  * Copyright IBM, Corp. 2008
5  *
6  * Authors:
7  *  Anthony Liguori   <aliguori@us.ibm.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2.  See
10  * the COPYING file in the top-level directory.
11  *
12  */
13 
14 #ifndef QEMU_AIO_H
15 #define QEMU_AIO_H
16 
17 #ifdef CONFIG_LINUX_IO_URING
18 #include <liburing.h>
19 #endif
20 #include "qemu/queue.h"
21 #include "qemu/event_notifier.h"
22 #include "qemu/thread.h"
23 #include "qemu/timer.h"
24 
25 typedef struct BlockAIOCB BlockAIOCB;
26 typedef void BlockCompletionFunc(void *opaque, int ret);
27 
28 typedef struct AIOCBInfo {
29     void (*cancel_async)(BlockAIOCB *acb);
30     AioContext *(*get_aio_context)(BlockAIOCB *acb);
31     size_t aiocb_size;
32 } AIOCBInfo;
33 
34 struct BlockAIOCB {
35     const AIOCBInfo *aiocb_info;
36     BlockDriverState *bs;
37     BlockCompletionFunc *cb;
38     void *opaque;
39     int refcnt;
40 };
41 
42 void *qemu_aio_get(const AIOCBInfo *aiocb_info, BlockDriverState *bs,
43                    BlockCompletionFunc *cb, void *opaque);
44 void qemu_aio_unref(void *p);
45 void qemu_aio_ref(void *p);
46 
47 typedef struct AioHandler AioHandler;
48 typedef QLIST_HEAD(, AioHandler) AioHandlerList;
49 typedef void QEMUBHFunc(void *opaque);
50 typedef bool AioPollFn(void *opaque);
51 typedef void IOHandler(void *opaque);
52 
53 struct Coroutine;
54 struct ThreadPool;
55 struct LinuxAioState;
56 struct LuringState;
57 
58 /* Is polling disabled? */
59 bool aio_poll_disabled(AioContext *ctx);
60 
61 /* Callbacks for file descriptor monitoring implementations */
62 typedef struct {
63     /*
64      * update:
65      * @ctx: the AioContext
66      * @old_node: the existing handler or NULL if this file descriptor is being
67      *            monitored for the first time
68      * @new_node: the new handler or NULL if this file descriptor is being
69      *            removed
70      *
71      * Add/remove/modify a monitored file descriptor.
72      *
73      * Called with ctx->list_lock acquired.
74      */
75     void (*update)(AioContext *ctx, AioHandler *old_node, AioHandler *new_node);
76 
77     /*
78      * wait:
79      * @ctx: the AioContext
80      * @ready_list: list for handlers that become ready
81      * @timeout: maximum duration to wait, in nanoseconds
82      *
83      * Wait for file descriptors to become ready and place them on ready_list.
84      *
85      * Called with ctx->list_lock incremented but not locked.
86      *
87      * Returns: number of ready file descriptors.
88      */
89     int (*wait)(AioContext *ctx, AioHandlerList *ready_list, int64_t timeout);
90 
91     /*
92      * need_wait:
93      * @ctx: the AioContext
94      *
95      * Tell aio_poll() when to stop userspace polling early because ->wait()
96      * has fds ready.
97      *
98      * File descriptor monitoring implementations that cannot poll fd readiness
99      * from userspace should use aio_poll_disabled() here.  This ensures that
100      * file descriptors are not starved by handlers that frequently make
101      * progress via userspace polling.
102      *
103      * Returns: true if ->wait() should be called, false otherwise.
104      */
105     bool (*need_wait)(AioContext *ctx);
106 } FDMonOps;
107 
108 /*
109  * Each aio_bh_poll() call carves off a slice of the BH list, so that newly
110  * scheduled BHs are not processed until the next aio_bh_poll() call.  All
111  * active aio_bh_poll() calls chain their slices together in a list, so that
112  * nested aio_bh_poll() calls process all scheduled bottom halves.
113  */
114 typedef QSLIST_HEAD(, QEMUBH) BHList;
115 typedef struct BHListSlice BHListSlice;
116 struct BHListSlice {
117     BHList bh_list;
118     QSIMPLEQ_ENTRY(BHListSlice) next;
119 };
120 
121 typedef QSLIST_HEAD(, AioHandler) AioHandlerSList;
122 
123 struct AioContext {
124     GSource source;
125 
126     /* Used by AioContext users to protect from multi-threaded access.  */
127     QemuRecMutex lock;
128 
129     /* The list of registered AIO handlers.  Protected by ctx->list_lock. */
130     AioHandlerList aio_handlers;
131 
132     /* The list of AIO handlers to be deleted.  Protected by ctx->list_lock. */
133     AioHandlerList deleted_aio_handlers;
134 
135     /* Used to avoid unnecessary event_notifier_set calls in aio_notify;
136      * only written from the AioContext home thread, or under the BQL in
137      * the case of the main AioContext.  However, it is read from any
138      * thread so it is still accessed with atomic primitives.
139      *
140      * If this field is 0, everything (file descriptors, bottom halves,
141      * timers) will be re-evaluated before the next blocking poll() or
142      * io_uring wait; therefore, the event_notifier_set call can be
143      * skipped.  If it is non-zero, you may need to wake up a concurrent
144      * aio_poll or the glib main event loop, making event_notifier_set
145      * necessary.
146      *
147      * Bit 0 is reserved for GSource usage of the AioContext, and is 1
148      * between a call to aio_ctx_prepare and the next call to aio_ctx_check.
149      * Bits 1-31 simply count the number of active calls to aio_poll
150      * that are in the prepare or poll phase.
151      *
152      * The GSource and aio_poll must use a different mechanism because
153      * there is no certainty that a call to GSource's prepare callback
154      * (via g_main_context_prepare) is indeed followed by check and
155      * dispatch.  It's not clear whether this would be a bug, but let's
156      * play safe and allow it---it will just cause extra calls to
157      * event_notifier_set until the next call to dispatch.
158      *
159      * Instead, the aio_poll calls include both the prepare and the
160      * dispatch phase, hence a simple counter is enough for them.
161      */
162     uint32_t notify_me;
163 
164     /* A lock to protect between QEMUBH and AioHandler adders and deleter,
165      * and to ensure that no callbacks are removed while we're walking and
166      * dispatching them.
167      */
168     QemuLockCnt list_lock;
169 
170     /* Bottom Halves pending aio_bh_poll() processing */
171     BHList bh_list;
172 
173     /* Chained BH list slices for each nested aio_bh_poll() call */
174     QSIMPLEQ_HEAD(, BHListSlice) bh_slice_list;
175 
176     /* Used by aio_notify.
177      *
178      * "notified" is used to avoid expensive event_notifier_test_and_clear
179      * calls.  When it is clear, the EventNotifier is clear, or one thread
180      * is going to clear "notified" before processing more events.  False
181      * positives are possible, i.e. "notified" could be set even though the
182      * EventNotifier is clear.
183      *
184      * Note that event_notifier_set *cannot* be optimized the same way.  For
185      * more information on the problem that would result, see "#ifdef BUG2"
186      * in the docs/aio_notify_accept.promela formal model.
187      */
188     bool notified;
189     EventNotifier notifier;
190 
191     QSLIST_HEAD(, Coroutine) scheduled_coroutines;
192     QEMUBH *co_schedule_bh;
193 
194     /* Thread pool for performing work and receiving completion callbacks.
195      * Has its own locking.
196      */
197     struct ThreadPool *thread_pool;
198 
199 #ifdef CONFIG_LINUX_AIO
200     /*
201      * State for native Linux AIO.  Uses aio_context_acquire/release for
202      * locking.
203      */
204     struct LinuxAioState *linux_aio;
205 #endif
206 #ifdef CONFIG_LINUX_IO_URING
207     /*
208      * State for Linux io_uring.  Uses aio_context_acquire/release for
209      * locking.
210      */
211     struct LuringState *linux_io_uring;
212 
213     /* State for file descriptor monitoring using Linux io_uring */
214     struct io_uring fdmon_io_uring;
215     AioHandlerSList submit_list;
216 #endif
217 
218     /* TimerLists for calling timers - one per clock type.  Has its own
219      * locking.
220      */
221     QEMUTimerListGroup tlg;
222 
223     int external_disable_cnt;
224 
225     /* Number of AioHandlers without .io_poll() */
226     int poll_disable_cnt;
227 
228     /* Polling mode parameters */
229     int64_t poll_ns;        /* current polling time in nanoseconds */
230     int64_t poll_max_ns;    /* maximum polling time in nanoseconds */
231     int64_t poll_grow;      /* polling time growth factor */
232     int64_t poll_shrink;    /* polling time shrink factor */
233 
234     /*
235      * List of handlers participating in userspace polling.  Protected by
236      * ctx->list_lock.  Iterated and modified mostly by the event loop thread
237      * from aio_poll() with ctx->list_lock incremented.  aio_set_fd_handler()
238      * only touches the list to delete nodes if ctx->list_lock's count is zero.
239      */
240     AioHandlerList poll_aio_handlers;
241 
242     /* Are we in polling mode or monitoring file descriptors? */
243     bool poll_started;
244 
245     /* epoll(7) state used when built with CONFIG_EPOLL */
246     int epollfd;
247 
248     const FDMonOps *fdmon_ops;
249 };
250 
251 /**
252  * aio_context_new: Allocate a new AioContext.
253  *
254  * AioContext provide a mini event-loop that can be waited on synchronously.
255  * They also provide bottom halves, a service to execute a piece of code
256  * as soon as possible.
257  */
258 AioContext *aio_context_new(Error **errp);
259 
260 /**
261  * aio_context_ref:
262  * @ctx: The AioContext to operate on.
263  *
264  * Add a reference to an AioContext.
265  */
266 void aio_context_ref(AioContext *ctx);
267 
268 /**
269  * aio_context_unref:
270  * @ctx: The AioContext to operate on.
271  *
272  * Drop a reference to an AioContext.
273  */
274 void aio_context_unref(AioContext *ctx);
275 
276 /* Take ownership of the AioContext.  If the AioContext will be shared between
277  * threads, and a thread does not want to be interrupted, it will have to
278  * take ownership around calls to aio_poll().  Otherwise, aio_poll()
279  * automatically takes care of calling aio_context_acquire and
280  * aio_context_release.
281  *
282  * Note that this is separate from bdrv_drained_begin/bdrv_drained_end.  A
283  * thread still has to call those to avoid being interrupted by the guest.
284  *
285  * Bottom halves, timers and callbacks can be created or removed without
286  * acquiring the AioContext.
287  */
288 void aio_context_acquire(AioContext *ctx);
289 
290 /* Relinquish ownership of the AioContext. */
291 void aio_context_release(AioContext *ctx);
292 
293 /**
294  * aio_bh_schedule_oneshot: Allocate a new bottom half structure that will run
295  * only once and as soon as possible.
296  */
297 void aio_bh_schedule_oneshot(AioContext *ctx, QEMUBHFunc *cb, void *opaque);
298 
299 /**
300  * aio_bh_new: Allocate a new bottom half structure.
301  *
302  * Bottom halves are lightweight callbacks whose invocation is guaranteed
303  * to be wait-free, thread-safe and signal-safe.  The #QEMUBH structure
304  * is opaque and must be allocated prior to its use.
305  */
306 QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque);
307 
308 /**
309  * aio_notify: Force processing of pending events.
310  *
311  * Similar to signaling a condition variable, aio_notify forces
312  * aio_poll to exit, so that the next call will re-examine pending events.
313  * The caller of aio_notify will usually call aio_poll again very soon,
314  * or go through another iteration of the GLib main loop.  Hence, aio_notify
315  * also has the side effect of recalculating the sets of file descriptors
316  * that the main loop waits for.
317  *
318  * Calling aio_notify is rarely necessary, because for example scheduling
319  * a bottom half calls it already.
320  */
321 void aio_notify(AioContext *ctx);
322 
323 /**
324  * aio_notify_accept: Acknowledge receiving an aio_notify.
325  *
326  * aio_notify() uses an EventNotifier in order to wake up a sleeping
327  * aio_poll() or g_main_context_iteration().  Calls to aio_notify() are
328  * usually rare, but the AioContext has to clear the EventNotifier on
329  * every aio_poll() or g_main_context_iteration() in order to avoid
330  * busy waiting.  This event_notifier_test_and_clear() cannot be done
331  * using the usual aio_context_set_event_notifier(), because it must
332  * be done before processing all events (file descriptors, bottom halves,
333  * timers).
334  *
335  * aio_notify_accept() is an optimized event_notifier_test_and_clear()
336  * that is specific to an AioContext's notifier; it is used internally
337  * to clear the EventNotifier only if aio_notify() had been called.
338  */
339 void aio_notify_accept(AioContext *ctx);
340 
341 /**
342  * aio_bh_call: Executes callback function of the specified BH.
343  */
344 void aio_bh_call(QEMUBH *bh);
345 
346 /**
347  * aio_bh_poll: Poll bottom halves for an AioContext.
348  *
349  * These are internal functions used by the QEMU main loop.
350  * And notice that multiple occurrences of aio_bh_poll cannot
351  * be called concurrently
352  */
353 int aio_bh_poll(AioContext *ctx);
354 
355 /**
356  * qemu_bh_schedule: Schedule a bottom half.
357  *
358  * Scheduling a bottom half interrupts the main loop and causes the
359  * execution of the callback that was passed to qemu_bh_new.
360  *
361  * Bottom halves that are scheduled from a bottom half handler are instantly
362  * invoked.  This can create an infinite loop if a bottom half handler
363  * schedules itself.
364  *
365  * @bh: The bottom half to be scheduled.
366  */
367 void qemu_bh_schedule(QEMUBH *bh);
368 
369 /**
370  * qemu_bh_cancel: Cancel execution of a bottom half.
371  *
372  * Canceling execution of a bottom half undoes the effect of calls to
373  * qemu_bh_schedule without freeing its resources yet.  While cancellation
374  * itself is also wait-free and thread-safe, it can of course race with the
375  * loop that executes bottom halves unless you are holding the iothread
376  * mutex.  This makes it mostly useless if you are not holding the mutex.
377  *
378  * @bh: The bottom half to be canceled.
379  */
380 void qemu_bh_cancel(QEMUBH *bh);
381 
382 /**
383  *qemu_bh_delete: Cancel execution of a bottom half and free its resources.
384  *
385  * Deleting a bottom half frees the memory that was allocated for it by
386  * qemu_bh_new.  It also implies canceling the bottom half if it was
387  * scheduled.
388  * This func is async. The bottom half will do the delete action at the finial
389  * end.
390  *
391  * @bh: The bottom half to be deleted.
392  */
393 void qemu_bh_delete(QEMUBH *bh);
394 
395 /* Return whether there are any pending callbacks from the GSource
396  * attached to the AioContext, before g_poll is invoked.
397  *
398  * This is used internally in the implementation of the GSource.
399  */
400 bool aio_prepare(AioContext *ctx);
401 
402 /* Return whether there are any pending callbacks from the GSource
403  * attached to the AioContext, after g_poll is invoked.
404  *
405  * This is used internally in the implementation of the GSource.
406  */
407 bool aio_pending(AioContext *ctx);
408 
409 /* Dispatch any pending callbacks from the GSource attached to the AioContext.
410  *
411  * This is used internally in the implementation of the GSource.
412  */
413 void aio_dispatch(AioContext *ctx);
414 
415 /* Progress in completing AIO work to occur.  This can issue new pending
416  * aio as a result of executing I/O completion or bh callbacks.
417  *
418  * Return whether any progress was made by executing AIO or bottom half
419  * handlers.  If @blocking == true, this should always be true except
420  * if someone called aio_notify.
421  *
422  * If there are no pending bottom halves, but there are pending AIO
423  * operations, it may not be possible to make any progress without
424  * blocking.  If @blocking is true, this function will wait until one
425  * or more AIO events have completed, to ensure something has moved
426  * before returning.
427  */
428 bool aio_poll(AioContext *ctx, bool blocking);
429 
430 /* Register a file descriptor and associated callbacks.  Behaves very similarly
431  * to qemu_set_fd_handler.  Unlike qemu_set_fd_handler, these callbacks will
432  * be invoked when using aio_poll().
433  *
434  * Code that invokes AIO completion functions should rely on this function
435  * instead of qemu_set_fd_handler[2].
436  */
437 void aio_set_fd_handler(AioContext *ctx,
438                         int fd,
439                         bool is_external,
440                         IOHandler *io_read,
441                         IOHandler *io_write,
442                         AioPollFn *io_poll,
443                         void *opaque);
444 
445 /* Set polling begin/end callbacks for a file descriptor that has already been
446  * registered with aio_set_fd_handler.  Do nothing if the file descriptor is
447  * not registered.
448  */
449 void aio_set_fd_poll(AioContext *ctx, int fd,
450                      IOHandler *io_poll_begin,
451                      IOHandler *io_poll_end);
452 
453 /* Register an event notifier and associated callbacks.  Behaves very similarly
454  * to event_notifier_set_handler.  Unlike event_notifier_set_handler, these callbacks
455  * will be invoked when using aio_poll().
456  *
457  * Code that invokes AIO completion functions should rely on this function
458  * instead of event_notifier_set_handler.
459  */
460 void aio_set_event_notifier(AioContext *ctx,
461                             EventNotifier *notifier,
462                             bool is_external,
463                             EventNotifierHandler *io_read,
464                             AioPollFn *io_poll);
465 
466 /* Set polling begin/end callbacks for an event notifier that has already been
467  * registered with aio_set_event_notifier.  Do nothing if the event notifier is
468  * not registered.
469  */
470 void aio_set_event_notifier_poll(AioContext *ctx,
471                                  EventNotifier *notifier,
472                                  EventNotifierHandler *io_poll_begin,
473                                  EventNotifierHandler *io_poll_end);
474 
475 /* Return a GSource that lets the main loop poll the file descriptors attached
476  * to this AioContext.
477  */
478 GSource *aio_get_g_source(AioContext *ctx);
479 
480 /* Return the ThreadPool bound to this AioContext */
481 struct ThreadPool *aio_get_thread_pool(AioContext *ctx);
482 
483 /* Setup the LinuxAioState bound to this AioContext */
484 struct LinuxAioState *aio_setup_linux_aio(AioContext *ctx, Error **errp);
485 
486 /* Return the LinuxAioState bound to this AioContext */
487 struct LinuxAioState *aio_get_linux_aio(AioContext *ctx);
488 
489 /* Setup the LuringState bound to this AioContext */
490 struct LuringState *aio_setup_linux_io_uring(AioContext *ctx, Error **errp);
491 
492 /* Return the LuringState bound to this AioContext */
493 struct LuringState *aio_get_linux_io_uring(AioContext *ctx);
494 /**
495  * aio_timer_new_with_attrs:
496  * @ctx: the aio context
497  * @type: the clock type
498  * @scale: the scale
499  * @attributes: 0, or one to multiple OR'ed QEMU_TIMER_ATTR_<id> values
500  *              to assign
501  * @cb: the callback to call on timer expiry
502  * @opaque: the opaque pointer to pass to the callback
503  *
504  * Allocate a new timer (with attributes) attached to the context @ctx.
505  * The function is responsible for memory allocation.
506  *
507  * The preferred interface is aio_timer_init or aio_timer_init_with_attrs.
508  * Use that unless you really need dynamic memory allocation.
509  *
510  * Returns: a pointer to the new timer
511  */
512 static inline QEMUTimer *aio_timer_new_with_attrs(AioContext *ctx,
513                                                   QEMUClockType type,
514                                                   int scale, int attributes,
515                                                   QEMUTimerCB *cb, void *opaque)
516 {
517     return timer_new_full(&ctx->tlg, type, scale, attributes, cb, opaque);
518 }
519 
520 /**
521  * aio_timer_new:
522  * @ctx: the aio context
523  * @type: the clock type
524  * @scale: the scale
525  * @cb: the callback to call on timer expiry
526  * @opaque: the opaque pointer to pass to the callback
527  *
528  * Allocate a new timer attached to the context @ctx.
529  * See aio_timer_new_with_attrs for details.
530  *
531  * Returns: a pointer to the new timer
532  */
533 static inline QEMUTimer *aio_timer_new(AioContext *ctx, QEMUClockType type,
534                                        int scale,
535                                        QEMUTimerCB *cb, void *opaque)
536 {
537     return timer_new_full(&ctx->tlg, type, scale, 0, cb, opaque);
538 }
539 
540 /**
541  * aio_timer_init_with_attrs:
542  * @ctx: the aio context
543  * @ts: the timer
544  * @type: the clock type
545  * @scale: the scale
546  * @attributes: 0, or one to multiple OR'ed QEMU_TIMER_ATTR_<id> values
547  *              to assign
548  * @cb: the callback to call on timer expiry
549  * @opaque: the opaque pointer to pass to the callback
550  *
551  * Initialise a new timer (with attributes) attached to the context @ctx.
552  * The caller is responsible for memory allocation.
553  */
554 static inline void aio_timer_init_with_attrs(AioContext *ctx,
555                                              QEMUTimer *ts, QEMUClockType type,
556                                              int scale, int attributes,
557                                              QEMUTimerCB *cb, void *opaque)
558 {
559     timer_init_full(ts, &ctx->tlg, type, scale, attributes, cb, opaque);
560 }
561 
562 /**
563  * aio_timer_init:
564  * @ctx: the aio context
565  * @ts: the timer
566  * @type: the clock type
567  * @scale: the scale
568  * @cb: the callback to call on timer expiry
569  * @opaque: the opaque pointer to pass to the callback
570  *
571  * Initialise a new timer attached to the context @ctx.
572  * See aio_timer_init_with_attrs for details.
573  */
574 static inline void aio_timer_init(AioContext *ctx,
575                                   QEMUTimer *ts, QEMUClockType type,
576                                   int scale,
577                                   QEMUTimerCB *cb, void *opaque)
578 {
579     timer_init_full(ts, &ctx->tlg, type, scale, 0, cb, opaque);
580 }
581 
582 /**
583  * aio_compute_timeout:
584  * @ctx: the aio context
585  *
586  * Compute the timeout that a blocking aio_poll should use.
587  */
588 int64_t aio_compute_timeout(AioContext *ctx);
589 
590 /**
591  * aio_disable_external:
592  * @ctx: the aio context
593  *
594  * Disable the further processing of external clients.
595  */
596 static inline void aio_disable_external(AioContext *ctx)
597 {
598     qatomic_inc(&ctx->external_disable_cnt);
599 }
600 
601 /**
602  * aio_enable_external:
603  * @ctx: the aio context
604  *
605  * Enable the processing of external clients.
606  */
607 static inline void aio_enable_external(AioContext *ctx)
608 {
609     int old;
610 
611     old = qatomic_fetch_dec(&ctx->external_disable_cnt);
612     assert(old > 0);
613     if (old == 1) {
614         /* Kick event loop so it re-arms file descriptors */
615         aio_notify(ctx);
616     }
617 }
618 
619 /**
620  * aio_external_disabled:
621  * @ctx: the aio context
622  *
623  * Return true if the external clients are disabled.
624  */
625 static inline bool aio_external_disabled(AioContext *ctx)
626 {
627     return qatomic_read(&ctx->external_disable_cnt);
628 }
629 
630 /**
631  * aio_node_check:
632  * @ctx: the aio context
633  * @is_external: Whether or not the checked node is an external event source.
634  *
635  * Check if the node's is_external flag is okay to be polled by the ctx at this
636  * moment. True means green light.
637  */
638 static inline bool aio_node_check(AioContext *ctx, bool is_external)
639 {
640     return !is_external || !qatomic_read(&ctx->external_disable_cnt);
641 }
642 
643 /**
644  * aio_co_schedule:
645  * @ctx: the aio context
646  * @co: the coroutine
647  *
648  * Start a coroutine on a remote AioContext.
649  *
650  * The coroutine must not be entered by anyone else while aio_co_schedule()
651  * is active.  In addition the coroutine must have yielded unless ctx
652  * is the context in which the coroutine is running (i.e. the value of
653  * qemu_get_current_aio_context() from the coroutine itself).
654  */
655 void aio_co_schedule(AioContext *ctx, struct Coroutine *co);
656 
657 /**
658  * aio_co_wake:
659  * @co: the coroutine
660  *
661  * Restart a coroutine on the AioContext where it was running last, thus
662  * preventing coroutines from jumping from one context to another when they
663  * go to sleep.
664  *
665  * aio_co_wake may be executed either in coroutine or non-coroutine
666  * context.  The coroutine must not be entered by anyone else while
667  * aio_co_wake() is active.
668  */
669 void aio_co_wake(struct Coroutine *co);
670 
671 /**
672  * aio_co_enter:
673  * @ctx: the context to run the coroutine
674  * @co: the coroutine to run
675  *
676  * Enter a coroutine in the specified AioContext.
677  */
678 void aio_co_enter(AioContext *ctx, struct Coroutine *co);
679 
680 /**
681  * Return the AioContext whose event loop runs in the current thread.
682  *
683  * If called from an IOThread this will be the IOThread's AioContext.  If
684  * called from another thread it will be the main loop AioContext.
685  */
686 AioContext *qemu_get_current_aio_context(void);
687 
688 /**
689  * aio_context_setup:
690  * @ctx: the aio context
691  *
692  * Initialize the aio context.
693  */
694 void aio_context_setup(AioContext *ctx);
695 
696 /**
697  * aio_context_destroy:
698  * @ctx: the aio context
699  *
700  * Destroy the aio context.
701  */
702 void aio_context_destroy(AioContext *ctx);
703 
704 /* Used internally, do not call outside AioContext code */
705 void aio_context_use_g_source(AioContext *ctx);
706 
707 /**
708  * aio_context_set_poll_params:
709  * @ctx: the aio context
710  * @max_ns: how long to busy poll for, in nanoseconds
711  * @grow: polling time growth factor
712  * @shrink: polling time shrink factor
713  *
714  * Poll mode can be disabled by setting poll_max_ns to 0.
715  */
716 void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
717                                  int64_t grow, int64_t shrink,
718                                  Error **errp);
719 
720 #endif
721