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