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