2 * QEMU aio implementation
4 * Copyright IBM, Corp. 2008
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
17 #include "qemu/typedefs.h"
18 #include "qemu-common.h"
19 #include "qemu/queue.h"
20 #include "qemu/event_notifier.h"
21 #include "qemu/thread.h"
22 #include "qemu/rfifolock.h"
23 #include "qemu/timer.h"
25 typedef struct BlockDriverAIOCB BlockDriverAIOCB;
26 typedef void BlockDriverCompletionFunc(void *opaque, int ret);
28 typedef struct AIOCBInfo {
29 void (*cancel)(BlockDriverAIOCB *acb);
30 void (*cancel_async)(BlockDriverAIOCB *acb);
31 AioContext *(*get_aio_context)(BlockDriverAIOCB *acb);
35 struct BlockDriverAIOCB {
36 const AIOCBInfo *aiocb_info;
38 BlockDriverCompletionFunc *cb;
43 void *qemu_aio_get(const AIOCBInfo *aiocb_info, BlockDriverState *bs,
44 BlockDriverCompletionFunc *cb, void *opaque);
45 void qemu_aio_release(void *p);
46 void qemu_aio_ref(void *p);
48 typedef struct AioHandler AioHandler;
49 typedef void QEMUBHFunc(void *opaque);
50 typedef void IOHandler(void *opaque);
55 /* Protects all fields from multi-threaded access */
58 /* The list of registered AIO handlers */
59 QLIST_HEAD(, AioHandler) aio_handlers;
61 /* This is a simple lock used to protect the aio_handlers list.
62 * Specifically, it's used to ensure that no callbacks are removed while
63 * we're walking and dispatching callbacks.
67 /* Used to avoid unnecessary event_notifier_set calls in aio_notify.
68 * Writes protected by lock or BQL, reads are lockless.
72 /* lock to protect between bh's adders and deleter */
75 /* Anchor of the list of Bottom Halves belonging to the context */
76 struct QEMUBH *first_bh;
78 /* A simple lock used to protect the first_bh list, and ensure that
79 * no callbacks are removed while we're walking and dispatching callbacks.
83 /* Used for aio_notify. */
84 EventNotifier notifier;
86 /* GPollFDs for aio_poll() */
89 /* Thread pool for performing work and receiving completion callbacks */
90 struct ThreadPool *thread_pool;
92 /* TimerLists for calling timers - one per clock type */
93 QEMUTimerListGroup tlg;
96 /* Used internally to synchronize aio_poll against qemu_bh_schedule. */
97 void aio_set_dispatching(AioContext *ctx, bool dispatching);
100 * aio_context_new: Allocate a new AioContext.
102 * AioContext provide a mini event-loop that can be waited on synchronously.
103 * They also provide bottom halves, a service to execute a piece of code
104 * as soon as possible.
106 AioContext *aio_context_new(void);
110 * @ctx: The AioContext to operate on.
112 * Add a reference to an AioContext.
114 void aio_context_ref(AioContext *ctx);
118 * @ctx: The AioContext to operate on.
120 * Drop a reference to an AioContext.
122 void aio_context_unref(AioContext *ctx);
124 /* Take ownership of the AioContext. If the AioContext will be shared between
125 * threads, a thread must have ownership when calling aio_poll().
127 * Note that multiple threads calling aio_poll() means timers, BHs, and
128 * callbacks may be invoked from a different thread than they were registered
129 * from. Therefore, code must use AioContext acquire/release or use
130 * fine-grained synchronization to protect shared state if other threads will
131 * be accessing it simultaneously.
133 void aio_context_acquire(AioContext *ctx);
135 /* Relinquish ownership of the AioContext. */
136 void aio_context_release(AioContext *ctx);
139 * aio_bh_new: Allocate a new bottom half structure.
141 * Bottom halves are lightweight callbacks whose invocation is guaranteed
142 * to be wait-free, thread-safe and signal-safe. The #QEMUBH structure
143 * is opaque and must be allocated prior to its use.
145 QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque);
148 * aio_notify: Force processing of pending events.
150 * Similar to signaling a condition variable, aio_notify forces
151 * aio_wait to exit, so that the next call will re-examine pending events.
152 * The caller of aio_notify will usually call aio_wait again very soon,
153 * or go through another iteration of the GLib main loop. Hence, aio_notify
154 * also has the side effect of recalculating the sets of file descriptors
155 * that the main loop waits for.
157 * Calling aio_notify is rarely necessary, because for example scheduling
158 * a bottom half calls it already.
160 void aio_notify(AioContext *ctx);
163 * aio_bh_poll: Poll bottom halves for an AioContext.
165 * These are internal functions used by the QEMU main loop.
166 * And notice that multiple occurrences of aio_bh_poll cannot
167 * be called concurrently
169 int aio_bh_poll(AioContext *ctx);
172 * qemu_bh_schedule: Schedule a bottom half.
174 * Scheduling a bottom half interrupts the main loop and causes the
175 * execution of the callback that was passed to qemu_bh_new.
177 * Bottom halves that are scheduled from a bottom half handler are instantly
178 * invoked. This can create an infinite loop if a bottom half handler
181 * @bh: The bottom half to be scheduled.
183 void qemu_bh_schedule(QEMUBH *bh);
186 * qemu_bh_cancel: Cancel execution of a bottom half.
188 * Canceling execution of a bottom half undoes the effect of calls to
189 * qemu_bh_schedule without freeing its resources yet. While cancellation
190 * itself is also wait-free and thread-safe, it can of course race with the
191 * loop that executes bottom halves unless you are holding the iothread
192 * mutex. This makes it mostly useless if you are not holding the mutex.
194 * @bh: The bottom half to be canceled.
196 void qemu_bh_cancel(QEMUBH *bh);
199 *qemu_bh_delete: Cancel execution of a bottom half and free its resources.
201 * Deleting a bottom half frees the memory that was allocated for it by
202 * qemu_bh_new. It also implies canceling the bottom half if it was
204 * This func is async. The bottom half will do the delete action at the finial
207 * @bh: The bottom half to be deleted.
209 void qemu_bh_delete(QEMUBH *bh);
211 /* Return whether there are any pending callbacks from the GSource
212 * attached to the AioContext, before g_poll is invoked.
214 * This is used internally in the implementation of the GSource.
216 bool aio_prepare(AioContext *ctx);
218 /* Return whether there are any pending callbacks from the GSource
219 * attached to the AioContext, after g_poll is invoked.
221 * This is used internally in the implementation of the GSource.
223 bool aio_pending(AioContext *ctx);
225 /* Dispatch any pending callbacks from the GSource attached to the AioContext.
227 * This is used internally in the implementation of the GSource.
229 bool aio_dispatch(AioContext *ctx);
231 /* Progress in completing AIO work to occur. This can issue new pending
232 * aio as a result of executing I/O completion or bh callbacks.
234 * Return whether any progress was made by executing AIO or bottom half
235 * handlers. If @blocking == true, this should always be true except
236 * if someone called aio_notify.
238 * If there are no pending bottom halves, but there are pending AIO
239 * operations, it may not be possible to make any progress without
240 * blocking. If @blocking is true, this function will wait until one
241 * or more AIO events have completed, to ensure something has moved
244 bool aio_poll(AioContext *ctx, bool blocking);
246 /* Register a file descriptor and associated callbacks. Behaves very similarly
247 * to qemu_set_fd_handler2. Unlike qemu_set_fd_handler2, these callbacks will
248 * be invoked when using aio_poll().
250 * Code that invokes AIO completion functions should rely on this function
251 * instead of qemu_set_fd_handler[2].
253 void aio_set_fd_handler(AioContext *ctx,
259 /* Register an event notifier and associated callbacks. Behaves very similarly
260 * to event_notifier_set_handler. Unlike event_notifier_set_handler, these callbacks
261 * will be invoked when using aio_poll().
263 * Code that invokes AIO completion functions should rely on this function
264 * instead of event_notifier_set_handler.
266 void aio_set_event_notifier(AioContext *ctx,
267 EventNotifier *notifier,
268 EventNotifierHandler *io_read);
270 /* Return a GSource that lets the main loop poll the file descriptors attached
271 * to this AioContext.
273 GSource *aio_get_g_source(AioContext *ctx);
275 /* Return the ThreadPool bound to this AioContext */
276 struct ThreadPool *aio_get_thread_pool(AioContext *ctx);
280 * @ctx: the aio context
281 * @type: the clock type
283 * @cb: the callback to call on timer expiry
284 * @opaque: the opaque pointer to pass to the callback
286 * Allocate a new timer attached to the context @ctx.
287 * The function is responsible for memory allocation.
289 * The preferred interface is aio_timer_init. Use that
290 * unless you really need dynamic memory allocation.
292 * Returns: a pointer to the new timer
294 static inline QEMUTimer *aio_timer_new(AioContext *ctx, QEMUClockType type,
296 QEMUTimerCB *cb, void *opaque)
298 return timer_new_tl(ctx->tlg.tl[type], scale, cb, opaque);
303 * @ctx: the aio context
305 * @type: the clock type
307 * @cb: the callback to call on timer expiry
308 * @opaque: the opaque pointer to pass to the callback
310 * Initialise a new timer attached to the context @ctx.
311 * The caller is responsible for memory allocation.
313 static inline void aio_timer_init(AioContext *ctx,
314 QEMUTimer *ts, QEMUClockType type,
316 QEMUTimerCB *cb, void *opaque)
318 timer_init(ts, ctx->tlg.tl[type], scale, cb, opaque);
322 * aio_compute_timeout:
323 * @ctx: the aio context
325 * Compute the timeout that a blocking aio_poll should use.
327 int64_t aio_compute_timeout(AioContext *ctx);