]> git.proxmox.com Git - qemu.git/blame - qemu-aio.h
Update version for 1.3.0 release
[qemu.git] / qemu-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
17#include "qemu-common.h"
a915f4bc 18#include "qemu-queue.h"
9958c351 19#include "event_notifier.h"
a76bab49 20
85e8dab1
PB
21typedef struct BlockDriverAIOCB BlockDriverAIOCB;
22typedef void BlockDriverCompletionFunc(void *opaque, int ret);
23
d7331bed 24typedef struct AIOCBInfo {
85e8dab1 25 void (*cancel)(BlockDriverAIOCB *acb);
8c82e9a4 26 size_t aiocb_size;
d7331bed 27} AIOCBInfo;
85e8dab1
PB
28
29struct BlockDriverAIOCB {
d7331bed 30 const AIOCBInfo *aiocb_info;
85e8dab1
PB
31 BlockDriverState *bs;
32 BlockDriverCompletionFunc *cb;
33 void *opaque;
85e8dab1
PB
34};
35
d7331bed 36void *qemu_aio_get(const AIOCBInfo *aiocb_info, BlockDriverState *bs,
85e8dab1
PB
37 BlockDriverCompletionFunc *cb, void *opaque);
38void qemu_aio_release(void *p);
39
f627aab1
PB
40typedef struct AioHandler AioHandler;
41typedef void QEMUBHFunc(void *opaque);
42typedef void IOHandler(void *opaque);
43
44typedef struct AioContext {
e3713e00
PB
45 GSource source;
46
a915f4bc
PB
47 /* The list of registered AIO handlers */
48 QLIST_HEAD(, AioHandler) aio_handlers;
49
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.
53 */
54 int walking_handlers;
55
f627aab1
PB
56 /* Anchor of the list of Bottom Halves belonging to the context */
57 struct QEMUBH *first_bh;
58
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.
61 */
62 int walking_bh;
2f4dc3c1
PB
63
64 /* Used for aio_notify. */
65 EventNotifier notifier;
f627aab1
PB
66} AioContext;
67
a76bab49 68/* Returns 1 if there are still outstanding AIO requests; 0 otherwise */
9958c351 69typedef int (AioFlushEventNotifierHandler)(EventNotifier *e);
a76bab49 70
f627aab1
PB
71/**
72 * aio_context_new: Allocate a new AioContext.
73 *
74 * AioContext provide a mini event-loop that can be waited on synchronously.
75 * They also provide bottom halves, a service to execute a piece of code
76 * as soon as possible.
77 */
78AioContext *aio_context_new(void);
79
e3713e00
PB
80/**
81 * aio_context_ref:
82 * @ctx: The AioContext to operate on.
83 *
84 * Add a reference to an AioContext.
85 */
86void aio_context_ref(AioContext *ctx);
87
88/**
89 * aio_context_unref:
90 * @ctx: The AioContext to operate on.
91 *
92 * Drop a reference to an AioContext.
93 */
94void aio_context_unref(AioContext *ctx);
95
f627aab1
PB
96/**
97 * aio_bh_new: Allocate a new bottom half structure.
98 *
99 * Bottom halves are lightweight callbacks whose invocation is guaranteed
100 * to be wait-free, thread-safe and signal-safe. The #QEMUBH structure
101 * is opaque and must be allocated prior to its use.
102 */
103QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque);
104
2f4dc3c1
PB
105/**
106 * aio_notify: Force processing of pending events.
107 *
108 * Similar to signaling a condition variable, aio_notify forces
109 * aio_wait to exit, so that the next call will re-examine pending events.
110 * The caller of aio_notify will usually call aio_wait again very soon,
111 * or go through another iteration of the GLib main loop. Hence, aio_notify
112 * also has the side effect of recalculating the sets of file descriptors
113 * that the main loop waits for.
114 *
115 * Calling aio_notify is rarely necessary, because for example scheduling
116 * a bottom half calls it already.
117 */
118void aio_notify(AioContext *ctx);
119
f627aab1
PB
120/**
121 * aio_bh_poll: Poll bottom halves for an AioContext.
122 *
123 * These are internal functions used by the QEMU main loop.
124 */
125int aio_bh_poll(AioContext *ctx);
f627aab1
PB
126
127/**
128 * qemu_bh_schedule: Schedule a bottom half.
129 *
130 * Scheduling a bottom half interrupts the main loop and causes the
131 * execution of the callback that was passed to qemu_bh_new.
132 *
133 * Bottom halves that are scheduled from a bottom half handler are instantly
134 * invoked. This can create an infinite loop if a bottom half handler
135 * schedules itself.
136 *
137 * @bh: The bottom half to be scheduled.
138 */
139void qemu_bh_schedule(QEMUBH *bh);
140
141/**
142 * qemu_bh_cancel: Cancel execution of a bottom half.
143 *
144 * Canceling execution of a bottom half undoes the effect of calls to
145 * qemu_bh_schedule without freeing its resources yet. While cancellation
146 * itself is also wait-free and thread-safe, it can of course race with the
147 * loop that executes bottom halves unless you are holding the iothread
148 * mutex. This makes it mostly useless if you are not holding the mutex.
149 *
150 * @bh: The bottom half to be canceled.
151 */
152void qemu_bh_cancel(QEMUBH *bh);
153
154/**
155 *qemu_bh_delete: Cancel execution of a bottom half and free its resources.
156 *
157 * Deleting a bottom half frees the memory that was allocated for it by
158 * qemu_bh_new. It also implies canceling the bottom half if it was
159 * scheduled.
160 *
161 * @bh: The bottom half to be deleted.
162 */
163void qemu_bh_delete(QEMUBH *bh);
164
a76bab49
AL
165/* Flush any pending AIO operation. This function will block until all
166 * outstanding AIO operations have been completed or cancelled. */
a915f4bc 167void aio_flush(AioContext *ctx);
a76bab49 168
cd9ba1eb
PB
169/* Return whether there are any pending callbacks from the GSource
170 * attached to the AioContext.
171 *
172 * This is used internally in the implementation of the GSource.
173 */
174bool aio_pending(AioContext *ctx);
175
7c0628b2
PB
176/* Progress in completing AIO work to occur. This can issue new pending
177 * aio as a result of executing I/O completion or bh callbacks.
bcdc1857 178 *
7c0628b2
PB
179 * If there is no pending AIO operation or completion (bottom half),
180 * return false. If there are pending bottom halves, return true.
181 *
182 * If there are no pending bottom halves, but there are pending AIO
183 * operations, it may not be possible to make any progress without
184 * blocking. If @blocking is true, this function will wait until one
185 * or more AIO events have completed, to ensure something has moved
186 * before returning.
187 *
188 * If @blocking is false, this function will also return false if the
189 * function cannot make any progress without blocking.
190 */
191bool aio_poll(AioContext *ctx, bool blocking);
a76bab49 192
9958c351
PB
193#ifdef CONFIG_POSIX
194/* Returns 1 if there are still outstanding AIO requests; 0 otherwise */
195typedef int (AioFlushHandler)(void *opaque);
196
a76bab49
AL
197/* Register a file descriptor and associated callbacks. Behaves very similarly
198 * to qemu_set_fd_handler2. Unlike qemu_set_fd_handler2, these callbacks will
199 * be invoked when using either qemu_aio_wait() or qemu_aio_flush().
200 *
201 * Code that invokes AIO completion functions should rely on this function
202 * instead of qemu_set_fd_handler[2].
203 */
a915f4bc
PB
204void aio_set_fd_handler(AioContext *ctx,
205 int fd,
206 IOHandler *io_read,
207 IOHandler *io_write,
208 AioFlushHandler *io_flush,
209 void *opaque);
9958c351
PB
210#endif
211
212/* Register an event notifier and associated callbacks. Behaves very similarly
213 * to event_notifier_set_handler. Unlike event_notifier_set_handler, these callbacks
214 * will be invoked when using either qemu_aio_wait() or qemu_aio_flush().
215 *
216 * Code that invokes AIO completion functions should rely on this function
217 * instead of event_notifier_set_handler.
218 */
a915f4bc
PB
219void aio_set_event_notifier(AioContext *ctx,
220 EventNotifier *notifier,
221 EventNotifierHandler *io_read,
222 AioFlushEventNotifierHandler *io_flush);
223
e3713e00
PB
224/* Return a GSource that lets the main loop poll the file descriptors attached
225 * to this AioContext.
226 */
227GSource *aio_get_g_source(AioContext *ctx);
228
a915f4bc
PB
229/* Functions to operate on the main QEMU AioContext. */
230
231void qemu_aio_flush(void);
232bool qemu_aio_wait(void);
9958c351
PB
233void qemu_aio_set_event_notifier(EventNotifier *notifier,
234 EventNotifierHandler *io_read,
235 AioFlushEventNotifierHandler *io_flush);
a76bab49 236
a915f4bc
PB
237#ifdef CONFIG_POSIX
238void qemu_aio_set_fd_handler(int fd,
239 IOHandler *io_read,
240 IOHandler *io_write,
241 AioFlushHandler *io_flush,
242 void *opaque);
243#endif
244
a76bab49 245#endif