]> git.proxmox.com Git - qemu.git/blob - qemu-aio.h
31884a8f165630bca99cce2523ea8aad2d7506dc
[qemu.git] / qemu-aio.h
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"
18 #include "qemu-queue.h"
19 #include "event_notifier.h"
20
21 typedef struct BlockDriverAIOCB BlockDriverAIOCB;
22 typedef void BlockDriverCompletionFunc(void *opaque, int ret);
23
24 typedef struct AIOCBInfo {
25 void (*cancel)(BlockDriverAIOCB *acb);
26 size_t aiocb_size;
27 } AIOCBInfo;
28
29 struct BlockDriverAIOCB {
30 const AIOCBInfo *aiocb_info;
31 BlockDriverState *bs;
32 BlockDriverCompletionFunc *cb;
33 void *opaque;
34 };
35
36 void *qemu_aio_get(const AIOCBInfo *aiocb_info, BlockDriverState *bs,
37 BlockDriverCompletionFunc *cb, void *opaque);
38 void qemu_aio_release(void *p);
39
40 typedef struct AioHandler AioHandler;
41 typedef void QEMUBHFunc(void *opaque);
42 typedef void IOHandler(void *opaque);
43
44 typedef struct AioContext {
45 GSource source;
46
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
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;
63
64 /* Used for aio_notify. */
65 EventNotifier notifier;
66 } AioContext;
67
68 /* Returns 1 if there are still outstanding AIO requests; 0 otherwise */
69 typedef int (AioFlushEventNotifierHandler)(EventNotifier *e);
70
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 */
78 AioContext *aio_context_new(void);
79
80 /**
81 * aio_context_ref:
82 * @ctx: The AioContext to operate on.
83 *
84 * Add a reference to an AioContext.
85 */
86 void 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 */
94 void aio_context_unref(AioContext *ctx);
95
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 */
103 QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque);
104
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 */
118 void aio_notify(AioContext *ctx);
119
120 /**
121 * aio_bh_poll: Poll bottom halves for an AioContext.
122 *
123 * These are internal functions used by the QEMU main loop.
124 */
125 int aio_bh_poll(AioContext *ctx);
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 */
139 void 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 */
152 void 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 */
163 void qemu_bh_delete(QEMUBH *bh);
164
165 /* Return whether there are any pending callbacks from the GSource
166 * attached to the AioContext.
167 *
168 * This is used internally in the implementation of the GSource.
169 */
170 bool aio_pending(AioContext *ctx);
171
172 /* Progress in completing AIO work to occur. This can issue new pending
173 * aio as a result of executing I/O completion or bh callbacks.
174 *
175 * If there is no pending AIO operation or completion (bottom half),
176 * return false. If there are pending bottom halves, return true.
177 *
178 * If there are no pending bottom halves, but there are pending AIO
179 * operations, it may not be possible to make any progress without
180 * blocking. If @blocking is true, this function will wait until one
181 * or more AIO events have completed, to ensure something has moved
182 * before returning.
183 *
184 * If @blocking is false, this function will also return false if the
185 * function cannot make any progress without blocking.
186 */
187 bool aio_poll(AioContext *ctx, bool blocking);
188
189 #ifdef CONFIG_POSIX
190 /* Returns 1 if there are still outstanding AIO requests; 0 otherwise */
191 typedef int (AioFlushHandler)(void *opaque);
192
193 /* Register a file descriptor and associated callbacks. Behaves very similarly
194 * to qemu_set_fd_handler2. Unlike qemu_set_fd_handler2, these callbacks will
195 * be invoked when using qemu_aio_wait().
196 *
197 * Code that invokes AIO completion functions should rely on this function
198 * instead of qemu_set_fd_handler[2].
199 */
200 void aio_set_fd_handler(AioContext *ctx,
201 int fd,
202 IOHandler *io_read,
203 IOHandler *io_write,
204 AioFlushHandler *io_flush,
205 void *opaque);
206 #endif
207
208 /* Register an event notifier and associated callbacks. Behaves very similarly
209 * to event_notifier_set_handler. Unlike event_notifier_set_handler, these callbacks
210 * will be invoked when using qemu_aio_wait().
211 *
212 * Code that invokes AIO completion functions should rely on this function
213 * instead of event_notifier_set_handler.
214 */
215 void aio_set_event_notifier(AioContext *ctx,
216 EventNotifier *notifier,
217 EventNotifierHandler *io_read,
218 AioFlushEventNotifierHandler *io_flush);
219
220 /* Return a GSource that lets the main loop poll the file descriptors attached
221 * to this AioContext.
222 */
223 GSource *aio_get_g_source(AioContext *ctx);
224
225 /* Functions to operate on the main QEMU AioContext. */
226
227 bool qemu_aio_wait(void);
228 void qemu_aio_set_event_notifier(EventNotifier *notifier,
229 EventNotifierHandler *io_read,
230 AioFlushEventNotifierHandler *io_flush);
231
232 #ifdef CONFIG_POSIX
233 void qemu_aio_set_fd_handler(int fd,
234 IOHandler *io_read,
235 IOHandler *io_write,
236 AioFlushHandler *io_flush,
237 void *opaque);
238 #endif
239
240 #endif