console: use pixman for fill+blit
[qemu/ar7.git] / include / block / aio.h
blob183679374fa5bbde66f0bde067fb74e618f8bdf0
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-common.h"
18 #include "qemu/queue.h"
19 #include "qemu/event_notifier.h"
21 typedef struct BlockDriverAIOCB BlockDriverAIOCB;
22 typedef void BlockDriverCompletionFunc(void *opaque, int ret);
24 typedef struct AIOCBInfo {
25 void (*cancel)(BlockDriverAIOCB *acb);
26 size_t aiocb_size;
27 } AIOCBInfo;
29 struct BlockDriverAIOCB {
30 const AIOCBInfo *aiocb_info;
31 BlockDriverState *bs;
32 BlockDriverCompletionFunc *cb;
33 void *opaque;
36 void *qemu_aio_get(const AIOCBInfo *aiocb_info, BlockDriverState *bs,
37 BlockDriverCompletionFunc *cb, void *opaque);
38 void qemu_aio_release(void *p);
40 typedef struct AioHandler AioHandler;
41 typedef void QEMUBHFunc(void *opaque);
42 typedef void IOHandler(void *opaque);
44 typedef struct AioContext {
45 GSource source;
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.
54 int walking_handlers;
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.
62 int walking_bh;
64 /* Used for aio_notify. */
65 EventNotifier notifier;
67 /* GPollFDs for aio_poll() */
68 GArray *pollfds;
70 /* Thread pool for performing work and receiving completion callbacks */
71 struct ThreadPool *thread_pool;
72 } AioContext;
74 /* Returns 1 if there are still outstanding AIO requests; 0 otherwise */
75 typedef int (AioFlushEventNotifierHandler)(EventNotifier *e);
77 /**
78 * aio_context_new: Allocate a new AioContext.
80 * AioContext provide a mini event-loop that can be waited on synchronously.
81 * They also provide bottom halves, a service to execute a piece of code
82 * as soon as possible.
84 AioContext *aio_context_new(void);
86 /**
87 * aio_context_ref:
88 * @ctx: The AioContext to operate on.
90 * Add a reference to an AioContext.
92 void aio_context_ref(AioContext *ctx);
94 /**
95 * aio_context_unref:
96 * @ctx: The AioContext to operate on.
98 * Drop a reference to an AioContext.
100 void aio_context_unref(AioContext *ctx);
103 * aio_bh_new: Allocate a new bottom half structure.
105 * Bottom halves are lightweight callbacks whose invocation is guaranteed
106 * to be wait-free, thread-safe and signal-safe. The #QEMUBH structure
107 * is opaque and must be allocated prior to its use.
109 QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque);
112 * aio_notify: Force processing of pending events.
114 * Similar to signaling a condition variable, aio_notify forces
115 * aio_wait to exit, so that the next call will re-examine pending events.
116 * The caller of aio_notify will usually call aio_wait again very soon,
117 * or go through another iteration of the GLib main loop. Hence, aio_notify
118 * also has the side effect of recalculating the sets of file descriptors
119 * that the main loop waits for.
121 * Calling aio_notify is rarely necessary, because for example scheduling
122 * a bottom half calls it already.
124 void aio_notify(AioContext *ctx);
127 * aio_bh_poll: Poll bottom halves for an AioContext.
129 * These are internal functions used by the QEMU main loop.
131 int aio_bh_poll(AioContext *ctx);
134 * qemu_bh_schedule: Schedule a bottom half.
136 * Scheduling a bottom half interrupts the main loop and causes the
137 * execution of the callback that was passed to qemu_bh_new.
139 * Bottom halves that are scheduled from a bottom half handler are instantly
140 * invoked. This can create an infinite loop if a bottom half handler
141 * schedules itself.
143 * @bh: The bottom half to be scheduled.
145 void qemu_bh_schedule(QEMUBH *bh);
148 * qemu_bh_cancel: Cancel execution of a bottom half.
150 * Canceling execution of a bottom half undoes the effect of calls to
151 * qemu_bh_schedule without freeing its resources yet. While cancellation
152 * itself is also wait-free and thread-safe, it can of course race with the
153 * loop that executes bottom halves unless you are holding the iothread
154 * mutex. This makes it mostly useless if you are not holding the mutex.
156 * @bh: The bottom half to be canceled.
158 void qemu_bh_cancel(QEMUBH *bh);
161 *qemu_bh_delete: Cancel execution of a bottom half and free its resources.
163 * Deleting a bottom half frees the memory that was allocated for it by
164 * qemu_bh_new. It also implies canceling the bottom half if it was
165 * scheduled.
167 * @bh: The bottom half to be deleted.
169 void qemu_bh_delete(QEMUBH *bh);
171 /* Return whether there are any pending callbacks from the GSource
172 * attached to the AioContext.
174 * This is used internally in the implementation of the GSource.
176 bool aio_pending(AioContext *ctx);
178 /* Progress in completing AIO work to occur. This can issue new pending
179 * aio as a result of executing I/O completion or bh callbacks.
181 * If there is no pending AIO operation or completion (bottom half),
182 * return false. If there are pending AIO operations of bottom halves,
183 * return true.
185 * If there are no pending bottom halves, but there are pending AIO
186 * operations, it may not be possible to make any progress without
187 * blocking. If @blocking is true, this function will wait until one
188 * or more AIO events have completed, to ensure something has moved
189 * before returning.
191 bool aio_poll(AioContext *ctx, bool blocking);
193 #ifdef CONFIG_POSIX
194 /* Returns 1 if there are still outstanding AIO requests; 0 otherwise */
195 typedef int (AioFlushHandler)(void *opaque);
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 qemu_aio_wait().
201 * Code that invokes AIO completion functions should rely on this function
202 * instead of qemu_set_fd_handler[2].
204 void aio_set_fd_handler(AioContext *ctx,
205 int fd,
206 IOHandler *io_read,
207 IOHandler *io_write,
208 AioFlushHandler *io_flush,
209 void *opaque);
210 #endif
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 qemu_aio_wait().
216 * Code that invokes AIO completion functions should rely on this function
217 * instead of event_notifier_set_handler.
219 void aio_set_event_notifier(AioContext *ctx,
220 EventNotifier *notifier,
221 EventNotifierHandler *io_read,
222 AioFlushEventNotifierHandler *io_flush);
224 /* Return a GSource that lets the main loop poll the file descriptors attached
225 * to this AioContext.
227 GSource *aio_get_g_source(AioContext *ctx);
229 /* Return the ThreadPool bound to this AioContext */
230 struct ThreadPool *aio_get_thread_pool(AioContext *ctx);
232 /* Functions to operate on the main QEMU AioContext. */
234 bool qemu_aio_wait(void);
235 void qemu_aio_set_event_notifier(EventNotifier *notifier,
236 EventNotifierHandler *io_read,
237 AioFlushEventNotifierHandler *io_flush);
239 #ifdef CONFIG_POSIX
240 void qemu_aio_set_fd_handler(int fd,
241 IOHandler *io_read,
242 IOHandler *io_write,
243 AioFlushHandler *io_flush,
244 void *opaque);
245 #endif
247 #endif