block: drop remaining legacy aio functions in comment
[qemu/ar7.git] / include / block / aio.h
blobca551e346f4ed471b86e59751372459e9b1287a7
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"
20 #include "qemu/thread.h"
21 #include "qemu/timer.h"
23 typedef struct BlockAIOCB BlockAIOCB;
24 typedef void BlockCompletionFunc(void *opaque, int ret);
26 typedef struct AIOCBInfo {
27 void (*cancel_async)(BlockAIOCB *acb);
28 AioContext *(*get_aio_context)(BlockAIOCB *acb);
29 size_t aiocb_size;
30 } AIOCBInfo;
32 struct BlockAIOCB {
33 const AIOCBInfo *aiocb_info;
34 BlockDriverState *bs;
35 BlockCompletionFunc *cb;
36 void *opaque;
37 int refcnt;
40 void *qemu_aio_get(const AIOCBInfo *aiocb_info, BlockDriverState *bs,
41 BlockCompletionFunc *cb, void *opaque);
42 void qemu_aio_unref(void *p);
43 void qemu_aio_ref(void *p);
45 typedef struct AioHandler AioHandler;
46 typedef void QEMUBHFunc(void *opaque);
47 typedef void IOHandler(void *opaque);
49 struct ThreadPool;
50 struct LinuxAioState;
52 struct AioContext {
53 GSource source;
55 /* Protects all fields from multi-threaded access */
56 QemuRecMutex lock;
58 /* The list of registered AIO handlers */
59 QLIST_HEAD(, AioHandler) aio_handlers;
61 /* This is a simple lock used to protect the aio_handlers list.
62 * Specifically, it's used to ensure that no callbacks are removed while
63 * we're walking and dispatching callbacks.
65 int walking_handlers;
67 /* Used to avoid unnecessary event_notifier_set calls in aio_notify;
68 * accessed with atomic primitives. If this field is 0, everything
69 * (file descriptors, bottom halves, timers) will be re-evaluated
70 * before the next blocking poll(), thus the event_notifier_set call
71 * can be skipped. If it is non-zero, you may need to wake up a
72 * concurrent aio_poll or the glib main event loop, making
73 * event_notifier_set necessary.
75 * Bit 0 is reserved for GSource usage of the AioContext, and is 1
76 * between a call to aio_ctx_prepare and the next call to aio_ctx_check.
77 * Bits 1-31 simply count the number of active calls to aio_poll
78 * that are in the prepare or poll phase.
80 * The GSource and aio_poll must use a different mechanism because
81 * there is no certainty that a call to GSource's prepare callback
82 * (via g_main_context_prepare) is indeed followed by check and
83 * dispatch. It's not clear whether this would be a bug, but let's
84 * play safe and allow it---it will just cause extra calls to
85 * event_notifier_set until the next call to dispatch.
87 * Instead, the aio_poll calls include both the prepare and the
88 * dispatch phase, hence a simple counter is enough for them.
90 uint32_t notify_me;
92 /* lock to protect between bh's adders and deleter */
93 QemuMutex bh_lock;
95 /* Anchor of the list of Bottom Halves belonging to the context */
96 struct QEMUBH *first_bh;
98 /* A simple lock used to protect the first_bh list, and ensure that
99 * no callbacks are removed while we're walking and dispatching callbacks.
101 int walking_bh;
103 /* Used by aio_notify.
105 * "notified" is used to avoid expensive event_notifier_test_and_clear
106 * calls. When it is clear, the EventNotifier is clear, or one thread
107 * is going to clear "notified" before processing more events. False
108 * positives are possible, i.e. "notified" could be set even though the
109 * EventNotifier is clear.
111 * Note that event_notifier_set *cannot* be optimized the same way. For
112 * more information on the problem that would result, see "#ifdef BUG2"
113 * in the docs/aio_notify_accept.promela formal model.
115 bool notified;
116 EventNotifier notifier;
118 /* Thread pool for performing work and receiving completion callbacks */
119 struct ThreadPool *thread_pool;
121 #ifdef CONFIG_LINUX_AIO
122 /* State for native Linux AIO. Uses aio_context_acquire/release for
123 * locking.
125 struct LinuxAioState *linux_aio;
126 #endif
128 /* TimerLists for calling timers - one per clock type */
129 QEMUTimerListGroup tlg;
131 int external_disable_cnt;
133 /* epoll(7) state used when built with CONFIG_EPOLL */
134 int epollfd;
135 bool epoll_enabled;
136 bool epoll_available;
140 * aio_context_new: Allocate a new AioContext.
142 * AioContext provide a mini event-loop that can be waited on synchronously.
143 * They also provide bottom halves, a service to execute a piece of code
144 * as soon as possible.
146 AioContext *aio_context_new(Error **errp);
149 * aio_context_ref:
150 * @ctx: The AioContext to operate on.
152 * Add a reference to an AioContext.
154 void aio_context_ref(AioContext *ctx);
157 * aio_context_unref:
158 * @ctx: The AioContext to operate on.
160 * Drop a reference to an AioContext.
162 void aio_context_unref(AioContext *ctx);
164 /* Take ownership of the AioContext. If the AioContext will be shared between
165 * threads, and a thread does not want to be interrupted, it will have to
166 * take ownership around calls to aio_poll(). Otherwise, aio_poll()
167 * automatically takes care of calling aio_context_acquire and
168 * aio_context_release.
170 * Access to timers and BHs from a thread that has not acquired AioContext
171 * is possible. Access to callbacks for now must be done while the AioContext
172 * is owned by the thread (FIXME).
174 void aio_context_acquire(AioContext *ctx);
176 /* Relinquish ownership of the AioContext. */
177 void aio_context_release(AioContext *ctx);
180 * aio_bh_schedule_oneshot: Allocate a new bottom half structure that will run
181 * only once and as soon as possible.
183 void aio_bh_schedule_oneshot(AioContext *ctx, QEMUBHFunc *cb, void *opaque);
186 * aio_bh_new: Allocate a new bottom half structure.
188 * Bottom halves are lightweight callbacks whose invocation is guaranteed
189 * to be wait-free, thread-safe and signal-safe. The #QEMUBH structure
190 * is opaque and must be allocated prior to its use.
192 QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque);
195 * aio_notify: Force processing of pending events.
197 * Similar to signaling a condition variable, aio_notify forces
198 * aio_poll to exit, so that the next call will re-examine pending events.
199 * The caller of aio_notify will usually call aio_poll again very soon,
200 * or go through another iteration of the GLib main loop. Hence, aio_notify
201 * also has the side effect of recalculating the sets of file descriptors
202 * that the main loop waits for.
204 * Calling aio_notify is rarely necessary, because for example scheduling
205 * a bottom half calls it already.
207 void aio_notify(AioContext *ctx);
210 * aio_notify_accept: Acknowledge receiving an aio_notify.
212 * aio_notify() uses an EventNotifier in order to wake up a sleeping
213 * aio_poll() or g_main_context_iteration(). Calls to aio_notify() are
214 * usually rare, but the AioContext has to clear the EventNotifier on
215 * every aio_poll() or g_main_context_iteration() in order to avoid
216 * busy waiting. This event_notifier_test_and_clear() cannot be done
217 * using the usual aio_context_set_event_notifier(), because it must
218 * be done before processing all events (file descriptors, bottom halves,
219 * timers).
221 * aio_notify_accept() is an optimized event_notifier_test_and_clear()
222 * that is specific to an AioContext's notifier; it is used internally
223 * to clear the EventNotifier only if aio_notify() had been called.
225 void aio_notify_accept(AioContext *ctx);
228 * aio_bh_call: Executes callback function of the specified BH.
230 void aio_bh_call(QEMUBH *bh);
233 * aio_bh_poll: Poll bottom halves for an AioContext.
235 * These are internal functions used by the QEMU main loop.
236 * And notice that multiple occurrences of aio_bh_poll cannot
237 * be called concurrently
239 int aio_bh_poll(AioContext *ctx);
242 * qemu_bh_schedule: Schedule a bottom half.
244 * Scheduling a bottom half interrupts the main loop and causes the
245 * execution of the callback that was passed to qemu_bh_new.
247 * Bottom halves that are scheduled from a bottom half handler are instantly
248 * invoked. This can create an infinite loop if a bottom half handler
249 * schedules itself.
251 * @bh: The bottom half to be scheduled.
253 void qemu_bh_schedule(QEMUBH *bh);
256 * qemu_bh_cancel: Cancel execution of a bottom half.
258 * Canceling execution of a bottom half undoes the effect of calls to
259 * qemu_bh_schedule without freeing its resources yet. While cancellation
260 * itself is also wait-free and thread-safe, it can of course race with the
261 * loop that executes bottom halves unless you are holding the iothread
262 * mutex. This makes it mostly useless if you are not holding the mutex.
264 * @bh: The bottom half to be canceled.
266 void qemu_bh_cancel(QEMUBH *bh);
269 *qemu_bh_delete: Cancel execution of a bottom half and free its resources.
271 * Deleting a bottom half frees the memory that was allocated for it by
272 * qemu_bh_new. It also implies canceling the bottom half if it was
273 * scheduled.
274 * This func is async. The bottom half will do the delete action at the finial
275 * end.
277 * @bh: The bottom half to be deleted.
279 void qemu_bh_delete(QEMUBH *bh);
281 /* Return whether there are any pending callbacks from the GSource
282 * attached to the AioContext, before g_poll is invoked.
284 * This is used internally in the implementation of the GSource.
286 bool aio_prepare(AioContext *ctx);
288 /* Return whether there are any pending callbacks from the GSource
289 * attached to the AioContext, after g_poll is invoked.
291 * This is used internally in the implementation of the GSource.
293 bool aio_pending(AioContext *ctx);
295 /* Dispatch any pending callbacks from the GSource attached to the AioContext.
297 * This is used internally in the implementation of the GSource.
299 bool aio_dispatch(AioContext *ctx);
301 /* Progress in completing AIO work to occur. This can issue new pending
302 * aio as a result of executing I/O completion or bh callbacks.
304 * Return whether any progress was made by executing AIO or bottom half
305 * handlers. If @blocking == true, this should always be true except
306 * if someone called aio_notify.
308 * If there are no pending bottom halves, but there are pending AIO
309 * operations, it may not be possible to make any progress without
310 * blocking. If @blocking is true, this function will wait until one
311 * or more AIO events have completed, to ensure something has moved
312 * before returning.
314 bool aio_poll(AioContext *ctx, bool blocking);
316 /* Register a file descriptor and associated callbacks. Behaves very similarly
317 * to qemu_set_fd_handler. Unlike qemu_set_fd_handler, these callbacks will
318 * be invoked when using aio_poll().
320 * Code that invokes AIO completion functions should rely on this function
321 * instead of qemu_set_fd_handler[2].
323 void aio_set_fd_handler(AioContext *ctx,
324 int fd,
325 bool is_external,
326 IOHandler *io_read,
327 IOHandler *io_write,
328 void *opaque);
330 /* Register an event notifier and associated callbacks. Behaves very similarly
331 * to event_notifier_set_handler. Unlike event_notifier_set_handler, these callbacks
332 * will be invoked when using aio_poll().
334 * Code that invokes AIO completion functions should rely on this function
335 * instead of event_notifier_set_handler.
337 void aio_set_event_notifier(AioContext *ctx,
338 EventNotifier *notifier,
339 bool is_external,
340 EventNotifierHandler *io_read);
342 /* Return a GSource that lets the main loop poll the file descriptors attached
343 * to this AioContext.
345 GSource *aio_get_g_source(AioContext *ctx);
347 /* Return the ThreadPool bound to this AioContext */
348 struct ThreadPool *aio_get_thread_pool(AioContext *ctx);
350 /* Return the LinuxAioState bound to this AioContext */
351 struct LinuxAioState *aio_get_linux_aio(AioContext *ctx);
354 * aio_timer_new:
355 * @ctx: the aio context
356 * @type: the clock type
357 * @scale: the scale
358 * @cb: the callback to call on timer expiry
359 * @opaque: the opaque pointer to pass to the callback
361 * Allocate a new timer attached to the context @ctx.
362 * The function is responsible for memory allocation.
364 * The preferred interface is aio_timer_init. Use that
365 * unless you really need dynamic memory allocation.
367 * Returns: a pointer to the new timer
369 static inline QEMUTimer *aio_timer_new(AioContext *ctx, QEMUClockType type,
370 int scale,
371 QEMUTimerCB *cb, void *opaque)
373 return timer_new_tl(ctx->tlg.tl[type], scale, cb, opaque);
377 * aio_timer_init:
378 * @ctx: the aio context
379 * @ts: the timer
380 * @type: the clock type
381 * @scale: the scale
382 * @cb: the callback to call on timer expiry
383 * @opaque: the opaque pointer to pass to the callback
385 * Initialise a new timer attached to the context @ctx.
386 * The caller is responsible for memory allocation.
388 static inline void aio_timer_init(AioContext *ctx,
389 QEMUTimer *ts, QEMUClockType type,
390 int scale,
391 QEMUTimerCB *cb, void *opaque)
393 timer_init_tl(ts, ctx->tlg.tl[type], scale, cb, opaque);
397 * aio_compute_timeout:
398 * @ctx: the aio context
400 * Compute the timeout that a blocking aio_poll should use.
402 int64_t aio_compute_timeout(AioContext *ctx);
405 * aio_disable_external:
406 * @ctx: the aio context
408 * Disable the further processing of external clients.
410 static inline void aio_disable_external(AioContext *ctx)
412 atomic_inc(&ctx->external_disable_cnt);
416 * aio_enable_external:
417 * @ctx: the aio context
419 * Enable the processing of external clients.
421 static inline void aio_enable_external(AioContext *ctx)
423 assert(ctx->external_disable_cnt > 0);
424 atomic_dec(&ctx->external_disable_cnt);
428 * aio_external_disabled:
429 * @ctx: the aio context
431 * Return true if the external clients are disabled.
433 static inline bool aio_external_disabled(AioContext *ctx)
435 return atomic_read(&ctx->external_disable_cnt);
439 * aio_node_check:
440 * @ctx: the aio context
441 * @is_external: Whether or not the checked node is an external event source.
443 * Check if the node's is_external flag is okay to be polled by the ctx at this
444 * moment. True means green light.
446 static inline bool aio_node_check(AioContext *ctx, bool is_external)
448 return !is_external || !atomic_read(&ctx->external_disable_cnt);
452 * Return the AioContext whose event loop runs in the current thread.
454 * If called from an IOThread this will be the IOThread's AioContext. If
455 * called from another thread it will be the main loop AioContext.
457 AioContext *qemu_get_current_aio_context(void);
460 * @ctx: the aio context
462 * Return whether we are running in the I/O thread that manages @ctx.
464 static inline bool aio_context_in_iothread(AioContext *ctx)
466 return ctx == qemu_get_current_aio_context();
470 * aio_context_setup:
471 * @ctx: the aio context
473 * Initialize the aio context.
475 void aio_context_setup(AioContext *ctx);
477 #endif