]> Git Repo - qemu.git/blob - include/block/aio.h
block: Add bdrv_aio_cancel_async
[qemu.git] / include / block / aio.h
1 /*
2  * QEMU aio implementation
3  *
4  * Copyright IBM, Corp. 2008
5  *
6  * Authors:
7  *  Anthony Liguori   <[email protected]>
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/typedefs.h"
18 #include "qemu-common.h"
19 #include "qemu/queue.h"
20 #include "qemu/event_notifier.h"
21 #include "qemu/thread.h"
22 #include "qemu/rfifolock.h"
23 #include "qemu/timer.h"
24
25 typedef struct BlockDriverAIOCB BlockDriverAIOCB;
26 typedef void BlockDriverCompletionFunc(void *opaque, int ret);
27
28 typedef struct AIOCBInfo {
29     void (*cancel)(BlockDriverAIOCB *acb);
30     void (*cancel_async)(BlockDriverAIOCB *acb);
31     AioContext *(*get_aio_context)(BlockDriverAIOCB *acb);
32     size_t aiocb_size;
33 } AIOCBInfo;
34
35 struct BlockDriverAIOCB {
36     const AIOCBInfo *aiocb_info;
37     BlockDriverState *bs;
38     BlockDriverCompletionFunc *cb;
39     void *opaque;
40     int refcnt;
41 };
42
43 void *qemu_aio_get(const AIOCBInfo *aiocb_info, BlockDriverState *bs,
44                    BlockDriverCompletionFunc *cb, void *opaque);
45 void qemu_aio_release(void *p);
46 void qemu_aio_ref(void *p);
47
48 typedef struct AioHandler AioHandler;
49 typedef void QEMUBHFunc(void *opaque);
50 typedef void IOHandler(void *opaque);
51
52 struct AioContext {
53     GSource source;
54
55     /* Protects all fields from multi-threaded access */
56     RFifoLock lock;
57
58     /* The list of registered AIO handlers */
59     QLIST_HEAD(, AioHandler) aio_handlers;
60
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.
64      */
65     int walking_handlers;
66
67     /* Used to avoid unnecessary event_notifier_set calls in aio_notify.
68      * Writes protected by lock or BQL, reads are lockless.
69      */
70     bool dispatching;
71
72     /* lock to protect between bh's adders and deleter */
73     QemuMutex bh_lock;
74
75     /* Anchor of the list of Bottom Halves belonging to the context */
76     struct QEMUBH *first_bh;
77
78     /* A simple lock used to protect the first_bh list, and ensure that
79      * no callbacks are removed while we're walking and dispatching callbacks.
80      */
81     int walking_bh;
82
83     /* Used for aio_notify.  */
84     EventNotifier notifier;
85
86     /* GPollFDs for aio_poll() */
87     GArray *pollfds;
88
89     /* Thread pool for performing work and receiving completion callbacks */
90     struct ThreadPool *thread_pool;
91
92     /* TimerLists for calling timers - one per clock type */
93     QEMUTimerListGroup tlg;
94 };
95
96 /* Used internally to synchronize aio_poll against qemu_bh_schedule.  */
97 void aio_set_dispatching(AioContext *ctx, bool dispatching);
98
99 /**
100  * aio_context_new: Allocate a new AioContext.
101  *
102  * AioContext provide a mini event-loop that can be waited on synchronously.
103  * They also provide bottom halves, a service to execute a piece of code
104  * as soon as possible.
105  */
106 AioContext *aio_context_new(void);
107
108 /**
109  * aio_context_ref:
110  * @ctx: The AioContext to operate on.
111  *
112  * Add a reference to an AioContext.
113  */
114 void aio_context_ref(AioContext *ctx);
115
116 /**
117  * aio_context_unref:
118  * @ctx: The AioContext to operate on.
119  *
120  * Drop a reference to an AioContext.
121  */
122 void aio_context_unref(AioContext *ctx);
123
124 /* Take ownership of the AioContext.  If the AioContext will be shared between
125  * threads, a thread must have ownership when calling aio_poll().
126  *
127  * Note that multiple threads calling aio_poll() means timers, BHs, and
128  * callbacks may be invoked from a different thread than they were registered
129  * from.  Therefore, code must use AioContext acquire/release or use
130  * fine-grained synchronization to protect shared state if other threads will
131  * be accessing it simultaneously.
132  */
133 void aio_context_acquire(AioContext *ctx);
134
135 /* Relinquish ownership of the AioContext. */
136 void aio_context_release(AioContext *ctx);
137
138 /**
139  * aio_bh_new: Allocate a new bottom half structure.
140  *
141  * Bottom halves are lightweight callbacks whose invocation is guaranteed
142  * to be wait-free, thread-safe and signal-safe.  The #QEMUBH structure
143  * is opaque and must be allocated prior to its use.
144  */
145 QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque);
146
147 /**
148  * aio_notify: Force processing of pending events.
149  *
150  * Similar to signaling a condition variable, aio_notify forces
151  * aio_wait to exit, so that the next call will re-examine pending events.
152  * The caller of aio_notify will usually call aio_wait again very soon,
153  * or go through another iteration of the GLib main loop.  Hence, aio_notify
154  * also has the side effect of recalculating the sets of file descriptors
155  * that the main loop waits for.
156  *
157  * Calling aio_notify is rarely necessary, because for example scheduling
158  * a bottom half calls it already.
159  */
160 void aio_notify(AioContext *ctx);
161
162 /**
163  * aio_bh_poll: Poll bottom halves for an AioContext.
164  *
165  * These are internal functions used by the QEMU main loop.
166  * And notice that multiple occurrences of aio_bh_poll cannot
167  * be called concurrently
168  */
169 int aio_bh_poll(AioContext *ctx);
170
171 /**
172  * qemu_bh_schedule: Schedule a bottom half.
173  *
174  * Scheduling a bottom half interrupts the main loop and causes the
175  * execution of the callback that was passed to qemu_bh_new.
176  *
177  * Bottom halves that are scheduled from a bottom half handler are instantly
178  * invoked.  This can create an infinite loop if a bottom half handler
179  * schedules itself.
180  *
181  * @bh: The bottom half to be scheduled.
182  */
183 void qemu_bh_schedule(QEMUBH *bh);
184
185 /**
186  * qemu_bh_cancel: Cancel execution of a bottom half.
187  *
188  * Canceling execution of a bottom half undoes the effect of calls to
189  * qemu_bh_schedule without freeing its resources yet.  While cancellation
190  * itself is also wait-free and thread-safe, it can of course race with the
191  * loop that executes bottom halves unless you are holding the iothread
192  * mutex.  This makes it mostly useless if you are not holding the mutex.
193  *
194  * @bh: The bottom half to be canceled.
195  */
196 void qemu_bh_cancel(QEMUBH *bh);
197
198 /**
199  *qemu_bh_delete: Cancel execution of a bottom half and free its resources.
200  *
201  * Deleting a bottom half frees the memory that was allocated for it by
202  * qemu_bh_new.  It also implies canceling the bottom half if it was
203  * scheduled.
204  * This func is async. The bottom half will do the delete action at the finial
205  * end.
206  *
207  * @bh: The bottom half to be deleted.
208  */
209 void qemu_bh_delete(QEMUBH *bh);
210
211 /* Return whether there are any pending callbacks from the GSource
212  * attached to the AioContext, before g_poll is invoked.
213  *
214  * This is used internally in the implementation of the GSource.
215  */
216 bool aio_prepare(AioContext *ctx);
217
218 /* Return whether there are any pending callbacks from the GSource
219  * attached to the AioContext, after g_poll is invoked.
220  *
221  * This is used internally in the implementation of the GSource.
222  */
223 bool aio_pending(AioContext *ctx);
224
225 /* Dispatch any pending callbacks from the GSource attached to the AioContext.
226  *
227  * This is used internally in the implementation of the GSource.
228  */
229 bool aio_dispatch(AioContext *ctx);
230
231 /* Progress in completing AIO work to occur.  This can issue new pending
232  * aio as a result of executing I/O completion or bh callbacks.
233  *
234  * Return whether any progress was made by executing AIO or bottom half
235  * handlers.  If @blocking == true, this should always be true except
236  * if someone called aio_notify.
237  *
238  * If there are no pending bottom halves, but there are pending AIO
239  * operations, it may not be possible to make any progress without
240  * blocking.  If @blocking is true, this function will wait until one
241  * or more AIO events have completed, to ensure something has moved
242  * before returning.
243  */
244 bool aio_poll(AioContext *ctx, bool blocking);
245
246 /* Register a file descriptor and associated callbacks.  Behaves very similarly
247  * to qemu_set_fd_handler2.  Unlike qemu_set_fd_handler2, these callbacks will
248  * be invoked when using aio_poll().
249  *
250  * Code that invokes AIO completion functions should rely on this function
251  * instead of qemu_set_fd_handler[2].
252  */
253 void aio_set_fd_handler(AioContext *ctx,
254                         int fd,
255                         IOHandler *io_read,
256                         IOHandler *io_write,
257                         void *opaque);
258
259 /* Register an event notifier and associated callbacks.  Behaves very similarly
260  * to event_notifier_set_handler.  Unlike event_notifier_set_handler, these callbacks
261  * will be invoked when using aio_poll().
262  *
263  * Code that invokes AIO completion functions should rely on this function
264  * instead of event_notifier_set_handler.
265  */
266 void aio_set_event_notifier(AioContext *ctx,
267                             EventNotifier *notifier,
268                             EventNotifierHandler *io_read);
269
270 /* Return a GSource that lets the main loop poll the file descriptors attached
271  * to this AioContext.
272  */
273 GSource *aio_get_g_source(AioContext *ctx);
274
275 /* Return the ThreadPool bound to this AioContext */
276 struct ThreadPool *aio_get_thread_pool(AioContext *ctx);
277
278 /**
279  * aio_timer_new:
280  * @ctx: the aio context
281  * @type: the clock type
282  * @scale: the scale
283  * @cb: the callback to call on timer expiry
284  * @opaque: the opaque pointer to pass to the callback
285  *
286  * Allocate a new timer attached to the context @ctx.
287  * The function is responsible for memory allocation.
288  *
289  * The preferred interface is aio_timer_init. Use that
290  * unless you really need dynamic memory allocation.
291  *
292  * Returns: a pointer to the new timer
293  */
294 static inline QEMUTimer *aio_timer_new(AioContext *ctx, QEMUClockType type,
295                                        int scale,
296                                        QEMUTimerCB *cb, void *opaque)
297 {
298     return timer_new_tl(ctx->tlg.tl[type], scale, cb, opaque);
299 }
300
301 /**
302  * aio_timer_init:
303  * @ctx: the aio context
304  * @ts: the timer
305  * @type: the clock type
306  * @scale: the scale
307  * @cb: the callback to call on timer expiry
308  * @opaque: the opaque pointer to pass to the callback
309  *
310  * Initialise a new timer attached to the context @ctx.
311  * The caller is responsible for memory allocation.
312  */
313 static inline void aio_timer_init(AioContext *ctx,
314                                   QEMUTimer *ts, QEMUClockType type,
315                                   int scale,
316                                   QEMUTimerCB *cb, void *opaque)
317 {
318     timer_init(ts, ctx->tlg.tl[type], scale, cb, opaque);
319 }
320
321 /**
322  * aio_compute_timeout:
323  * @ctx: the aio context
324  *
325  * Compute the timeout that a blocking aio_poll should use.
326  */
327 int64_t aio_compute_timeout(AioContext *ctx);
328
329 #endif
This page took 0.0423480000000001 seconds and 4 git commands to generate.