2 * QEMU aio implementation
4 * Copyright IBM, Corp. 2008
7 * Anthony Liguori <aliguori@us.ibm.com>
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-common.h"
18 #include "qemu-queue.h"
19 #include "event_notifier.h"
21 typedef struct BlockDriverAIOCB BlockDriverAIOCB
;
22 typedef void BlockDriverCompletionFunc(void *opaque
, int ret
);
24 typedef struct AIOPool
{
25 void (*cancel
)(BlockDriverAIOCB
*acb
);
27 BlockDriverAIOCB
*free_aiocb
;
30 struct BlockDriverAIOCB
{
33 BlockDriverCompletionFunc
*cb
;
35 BlockDriverAIOCB
*next
;
38 void *qemu_aio_get(AIOPool
*pool
, BlockDriverState
*bs
,
39 BlockDriverCompletionFunc
*cb
, void *opaque
);
40 void qemu_aio_release(void *p
);
42 typedef struct AioHandler AioHandler
;
43 typedef void QEMUBHFunc(void *opaque
);
44 typedef void IOHandler(void *opaque
);
46 typedef struct AioContext
{
47 /* The list of registered AIO handlers */
48 QLIST_HEAD(, AioHandler
) aio_handlers
;
50 /* This is a simple lock used to protect the aio_handlers list.
51 * Specifically, it's used to ensure that no callbacks are removed while
52 * we're walking and dispatching callbacks.
56 /* Anchor of the list of Bottom Halves belonging to the context */
57 struct QEMUBH
*first_bh
;
59 /* A simple lock used to protect the first_bh list, and ensure that
60 * no callbacks are removed while we're walking and dispatching callbacks.
65 /* Returns 1 if there are still outstanding AIO requests; 0 otherwise */
66 typedef int (AioFlushEventNotifierHandler
)(EventNotifier
*e
);
69 * aio_context_new: Allocate a new AioContext.
71 * AioContext provide a mini event-loop that can be waited on synchronously.
72 * They also provide bottom halves, a service to execute a piece of code
73 * as soon as possible.
75 AioContext
*aio_context_new(void);
78 * aio_bh_new: Allocate a new bottom half structure.
80 * Bottom halves are lightweight callbacks whose invocation is guaranteed
81 * to be wait-free, thread-safe and signal-safe. The #QEMUBH structure
82 * is opaque and must be allocated prior to its use.
84 QEMUBH
*aio_bh_new(AioContext
*ctx
, QEMUBHFunc
*cb
, void *opaque
);
87 * aio_bh_poll: Poll bottom halves for an AioContext.
89 * These are internal functions used by the QEMU main loop.
91 int aio_bh_poll(AioContext
*ctx
);
92 void aio_bh_update_timeout(AioContext
*ctx
, uint32_t *timeout
);
95 * qemu_bh_schedule: Schedule a bottom half.
97 * Scheduling a bottom half interrupts the main loop and causes the
98 * execution of the callback that was passed to qemu_bh_new.
100 * Bottom halves that are scheduled from a bottom half handler are instantly
101 * invoked. This can create an infinite loop if a bottom half handler
104 * @bh: The bottom half to be scheduled.
106 void qemu_bh_schedule(QEMUBH
*bh
);
109 * qemu_bh_cancel: Cancel execution of a bottom half.
111 * Canceling execution of a bottom half undoes the effect of calls to
112 * qemu_bh_schedule without freeing its resources yet. While cancellation
113 * itself is also wait-free and thread-safe, it can of course race with the
114 * loop that executes bottom halves unless you are holding the iothread
115 * mutex. This makes it mostly useless if you are not holding the mutex.
117 * @bh: The bottom half to be canceled.
119 void qemu_bh_cancel(QEMUBH
*bh
);
122 *qemu_bh_delete: Cancel execution of a bottom half and free its resources.
124 * Deleting a bottom half frees the memory that was allocated for it by
125 * qemu_bh_new. It also implies canceling the bottom half if it was
128 * @bh: The bottom half to be deleted.
130 void qemu_bh_delete(QEMUBH
*bh
);
132 /* Flush any pending AIO operation. This function will block until all
133 * outstanding AIO operations have been completed or cancelled. */
134 void aio_flush(AioContext
*ctx
);
136 /* Return whether there are any pending callbacks from the GSource
137 * attached to the AioContext.
139 * This is used internally in the implementation of the GSource.
141 bool aio_pending(AioContext
*ctx
);
143 /* Progress in completing AIO work to occur. This can issue new pending
144 * aio as a result of executing I/O completion or bh callbacks.
146 * If there is no pending AIO operation or completion (bottom half),
147 * return false. If there are pending bottom halves, return true.
149 * If there are no pending bottom halves, but there are pending AIO
150 * operations, it may not be possible to make any progress without
151 * blocking. If @blocking is true, this function will wait until one
152 * or more AIO events have completed, to ensure something has moved
155 * If @blocking is false, this function will also return false if the
156 * function cannot make any progress without blocking.
158 bool aio_poll(AioContext
*ctx
, bool blocking
);
161 /* Returns 1 if there are still outstanding AIO requests; 0 otherwise */
162 typedef int (AioFlushHandler
)(void *opaque
);
164 /* Register a file descriptor and associated callbacks. Behaves very similarly
165 * to qemu_set_fd_handler2. Unlike qemu_set_fd_handler2, these callbacks will
166 * be invoked when using either qemu_aio_wait() or qemu_aio_flush().
168 * Code that invokes AIO completion functions should rely on this function
169 * instead of qemu_set_fd_handler[2].
171 void aio_set_fd_handler(AioContext
*ctx
,
175 AioFlushHandler
*io_flush
,
179 /* Register an event notifier and associated callbacks. Behaves very similarly
180 * to event_notifier_set_handler. Unlike event_notifier_set_handler, these callbacks
181 * will be invoked when using either qemu_aio_wait() or qemu_aio_flush().
183 * Code that invokes AIO completion functions should rely on this function
184 * instead of event_notifier_set_handler.
186 void aio_set_event_notifier(AioContext
*ctx
,
187 EventNotifier
*notifier
,
188 EventNotifierHandler
*io_read
,
189 AioFlushEventNotifierHandler
*io_flush
);
191 /* Functions to operate on the main QEMU AioContext. */
193 void qemu_aio_flush(void);
194 bool qemu_aio_wait(void);
195 void qemu_aio_set_event_notifier(EventNotifier
*notifier
,
196 EventNotifierHandler
*io_read
,
197 AioFlushEventNotifierHandler
*io_flush
);
200 void qemu_aio_set_fd_handler(int fd
,
203 AioFlushHandler
*io_flush
,