nsis: Improved support for parallel installation of 32 and 64 bit code
[qemu/kevin.git] / include / block / aio.h
blob2efdf416cf62dc119ea2773fdb1308fb29cdb28d
1 /*
2 * QEMU aio implementation
4 * Copyright IBM, Corp. 2008
6 * Authors:
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.
14 #ifndef QEMU_AIO_H
15 #define QEMU_AIO_H
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/timer.h"
24 typedef struct BlockDriverAIOCB BlockDriverAIOCB;
25 typedef void BlockDriverCompletionFunc(void *opaque, int ret);
27 typedef struct AIOCBInfo {
28 void (*cancel)(BlockDriverAIOCB *acb);
29 size_t aiocb_size;
30 } AIOCBInfo;
32 struct BlockDriverAIOCB {
33 const AIOCBInfo *aiocb_info;
34 BlockDriverState *bs;
35 BlockDriverCompletionFunc *cb;
36 void *opaque;
39 void *qemu_aio_get(const AIOCBInfo *aiocb_info, BlockDriverState *bs,
40 BlockDriverCompletionFunc *cb, void *opaque);
41 void qemu_aio_release(void *p);
43 typedef struct AioHandler AioHandler;
44 typedef void QEMUBHFunc(void *opaque);
45 typedef void IOHandler(void *opaque);
47 struct AioContext {
48 GSource source;
50 /* The list of registered AIO handlers */
51 QLIST_HEAD(, AioHandler) aio_handlers;
53 /* This is a simple lock used to protect the aio_handlers list.
54 * Specifically, it's used to ensure that no callbacks are removed while
55 * we're walking and dispatching callbacks.
57 int walking_handlers;
59 /* lock to protect between bh's adders and deleter */
60 QemuMutex bh_lock;
61 /* Anchor of the list of Bottom Halves belonging to the context */
62 struct QEMUBH *first_bh;
64 /* A simple lock used to protect the first_bh list, and ensure that
65 * no callbacks are removed while we're walking and dispatching callbacks.
67 int walking_bh;
69 /* Used for aio_notify. */
70 EventNotifier notifier;
72 /* GPollFDs for aio_poll() */
73 GArray *pollfds;
75 /* Thread pool for performing work and receiving completion callbacks */
76 struct ThreadPool *thread_pool;
78 /* TimerLists for calling timers - one per clock type */
79 QEMUTimerListGroup tlg;
82 /**
83 * aio_context_new: Allocate a new AioContext.
85 * AioContext provide a mini event-loop that can be waited on synchronously.
86 * They also provide bottom halves, a service to execute a piece of code
87 * as soon as possible.
89 AioContext *aio_context_new(void);
91 /**
92 * aio_context_ref:
93 * @ctx: The AioContext to operate on.
95 * Add a reference to an AioContext.
97 void aio_context_ref(AioContext *ctx);
99 /**
100 * aio_context_unref:
101 * @ctx: The AioContext to operate on.
103 * Drop a reference to an AioContext.
105 void aio_context_unref(AioContext *ctx);
108 * aio_bh_new: Allocate a new bottom half structure.
110 * Bottom halves are lightweight callbacks whose invocation is guaranteed
111 * to be wait-free, thread-safe and signal-safe. The #QEMUBH structure
112 * is opaque and must be allocated prior to its use.
114 QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque);
117 * aio_notify: Force processing of pending events.
119 * Similar to signaling a condition variable, aio_notify forces
120 * aio_wait to exit, so that the next call will re-examine pending events.
121 * The caller of aio_notify will usually call aio_wait again very soon,
122 * or go through another iteration of the GLib main loop. Hence, aio_notify
123 * also has the side effect of recalculating the sets of file descriptors
124 * that the main loop waits for.
126 * Calling aio_notify is rarely necessary, because for example scheduling
127 * a bottom half calls it already.
129 void aio_notify(AioContext *ctx);
132 * aio_bh_poll: Poll bottom halves for an AioContext.
134 * These are internal functions used by the QEMU main loop.
135 * And notice that multiple occurrences of aio_bh_poll cannot
136 * be called concurrently
138 int aio_bh_poll(AioContext *ctx);
141 * qemu_bh_schedule: Schedule a bottom half.
143 * Scheduling a bottom half interrupts the main loop and causes the
144 * execution of the callback that was passed to qemu_bh_new.
146 * Bottom halves that are scheduled from a bottom half handler are instantly
147 * invoked. This can create an infinite loop if a bottom half handler
148 * schedules itself.
150 * @bh: The bottom half to be scheduled.
152 void qemu_bh_schedule(QEMUBH *bh);
155 * qemu_bh_cancel: Cancel execution of a bottom half.
157 * Canceling execution of a bottom half undoes the effect of calls to
158 * qemu_bh_schedule without freeing its resources yet. While cancellation
159 * itself is also wait-free and thread-safe, it can of course race with the
160 * loop that executes bottom halves unless you are holding the iothread
161 * mutex. This makes it mostly useless if you are not holding the mutex.
163 * @bh: The bottom half to be canceled.
165 void qemu_bh_cancel(QEMUBH *bh);
168 *qemu_bh_delete: Cancel execution of a bottom half and free its resources.
170 * Deleting a bottom half frees the memory that was allocated for it by
171 * qemu_bh_new. It also implies canceling the bottom half if it was
172 * scheduled.
173 * This func is async. The bottom half will do the delete action at the finial
174 * end.
176 * @bh: The bottom half to be deleted.
178 void qemu_bh_delete(QEMUBH *bh);
180 /* Return whether there are any pending callbacks from the GSource
181 * attached to the AioContext.
183 * This is used internally in the implementation of the GSource.
185 bool aio_pending(AioContext *ctx);
187 /* Progress in completing AIO work to occur. This can issue new pending
188 * aio as a result of executing I/O completion or bh callbacks.
190 * If there is no pending AIO operation or completion (bottom half),
191 * return false. If there are pending AIO operations of bottom halves,
192 * return true.
194 * If there are no pending bottom halves, but there are pending AIO
195 * operations, it may not be possible to make any progress without
196 * blocking. If @blocking is true, this function will wait until one
197 * or more AIO events have completed, to ensure something has moved
198 * before returning.
200 bool aio_poll(AioContext *ctx, bool blocking);
202 #ifdef CONFIG_POSIX
203 /* Register a file descriptor and associated callbacks. Behaves very similarly
204 * to qemu_set_fd_handler2. Unlike qemu_set_fd_handler2, these callbacks will
205 * be invoked when using qemu_aio_wait().
207 * Code that invokes AIO completion functions should rely on this function
208 * instead of qemu_set_fd_handler[2].
210 void aio_set_fd_handler(AioContext *ctx,
211 int fd,
212 IOHandler *io_read,
213 IOHandler *io_write,
214 void *opaque);
215 #endif
217 /* Register an event notifier and associated callbacks. Behaves very similarly
218 * to event_notifier_set_handler. Unlike event_notifier_set_handler, these callbacks
219 * will be invoked when using qemu_aio_wait().
221 * Code that invokes AIO completion functions should rely on this function
222 * instead of event_notifier_set_handler.
224 void aio_set_event_notifier(AioContext *ctx,
225 EventNotifier *notifier,
226 EventNotifierHandler *io_read);
228 /* Return a GSource that lets the main loop poll the file descriptors attached
229 * to this AioContext.
231 GSource *aio_get_g_source(AioContext *ctx);
233 /* Return the ThreadPool bound to this AioContext */
234 struct ThreadPool *aio_get_thread_pool(AioContext *ctx);
236 /* Functions to operate on the main QEMU AioContext. */
238 bool qemu_aio_wait(void);
239 void qemu_aio_set_event_notifier(EventNotifier *notifier,
240 EventNotifierHandler *io_read);
242 #ifdef CONFIG_POSIX
243 void qemu_aio_set_fd_handler(int fd,
244 IOHandler *io_read,
245 IOHandler *io_write,
246 void *opaque);
247 #endif
250 * aio_timer_new:
251 * @ctx: the aio context
252 * @type: the clock type
253 * @scale: the scale
254 * @cb: the callback to call on timer expiry
255 * @opaque: the opaque pointer to pass to the callback
257 * Allocate a new timer attached to the context @ctx.
258 * The function is responsible for memory allocation.
260 * The preferred interface is aio_timer_init. Use that
261 * unless you really need dynamic memory allocation.
263 * Returns: a pointer to the new timer
265 static inline QEMUTimer *aio_timer_new(AioContext *ctx, QEMUClockType type,
266 int scale,
267 QEMUTimerCB *cb, void *opaque)
269 return timer_new_tl(ctx->tlg.tl[type], scale, cb, opaque);
273 * aio_timer_init:
274 * @ctx: the aio context
275 * @ts: the timer
276 * @type: the clock type
277 * @scale: the scale
278 * @cb: the callback to call on timer expiry
279 * @opaque: the opaque pointer to pass to the callback
281 * Initialise a new timer attached to the context @ctx.
282 * The caller is responsible for memory allocation.
284 static inline void aio_timer_init(AioContext *ctx,
285 QEMUTimer *ts, QEMUClockType type,
286 int scale,
287 QEMUTimerCB *cb, void *opaque)
289 timer_init(ts, ctx->tlg.tl[type], scale, cb, opaque);
292 #endif