]>
Commit | Line | Data |
---|---|---|
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 | ||
17 | #include "qemu-common.h" | |
a915f4bc | 18 | #include "qemu-queue.h" |
9958c351 | 19 | #include "event_notifier.h" |
a76bab49 | 20 | |
85e8dab1 PB |
21 | typedef struct BlockDriverAIOCB BlockDriverAIOCB; |
22 | typedef void BlockDriverCompletionFunc(void *opaque, int ret); | |
23 | ||
24 | typedef struct AIOPool { | |
25 | void (*cancel)(BlockDriverAIOCB *acb); | |
26 | int aiocb_size; | |
27 | BlockDriverAIOCB *free_aiocb; | |
28 | } AIOPool; | |
29 | ||
30 | struct BlockDriverAIOCB { | |
31 | AIOPool *pool; | |
32 | BlockDriverState *bs; | |
33 | BlockDriverCompletionFunc *cb; | |
34 | void *opaque; | |
35 | BlockDriverAIOCB *next; | |
36 | }; | |
37 | ||
38 | void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs, | |
39 | BlockDriverCompletionFunc *cb, void *opaque); | |
40 | void qemu_aio_release(void *p); | |
41 | ||
f627aab1 PB |
42 | typedef struct AioHandler AioHandler; |
43 | typedef void QEMUBHFunc(void *opaque); | |
44 | typedef void IOHandler(void *opaque); | |
45 | ||
46 | typedef struct AioContext { | |
a915f4bc PB |
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 | ||
f627aab1 PB |
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 | } AioContext; | |
64 | ||
a76bab49 | 65 | /* Returns 1 if there are still outstanding AIO requests; 0 otherwise */ |
9958c351 | 66 | typedef int (AioFlushEventNotifierHandler)(EventNotifier *e); |
a76bab49 | 67 | |
f627aab1 PB |
68 | /** |
69 | * aio_context_new: Allocate a new AioContext. | |
70 | * | |
71 | * AioContext provide a mini event-loop that can be waited on synchronously. | |
72 | * They also provide bottom halves, a service to execute a piece of code | |
73 | * as soon as possible. | |
74 | */ | |
75 | AioContext *aio_context_new(void); | |
76 | ||
77 | /** | |
78 | * aio_bh_new: Allocate a new bottom half structure. | |
79 | * | |
80 | * Bottom halves are lightweight callbacks whose invocation is guaranteed | |
81 | * to be wait-free, thread-safe and signal-safe. The #QEMUBH structure | |
82 | * is opaque and must be allocated prior to its use. | |
83 | */ | |
84 | QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque); | |
85 | ||
86 | /** | |
87 | * aio_bh_poll: Poll bottom halves for an AioContext. | |
88 | * | |
89 | * These are internal functions used by the QEMU main loop. | |
90 | */ | |
91 | int aio_bh_poll(AioContext *ctx); | |
92 | void aio_bh_update_timeout(AioContext *ctx, uint32_t *timeout); | |
93 | ||
94 | /** | |
95 | * qemu_bh_schedule: Schedule a bottom half. | |
96 | * | |
97 | * Scheduling a bottom half interrupts the main loop and causes the | |
98 | * execution of the callback that was passed to qemu_bh_new. | |
99 | * | |
100 | * Bottom halves that are scheduled from a bottom half handler are instantly | |
101 | * invoked. This can create an infinite loop if a bottom half handler | |
102 | * schedules itself. | |
103 | * | |
104 | * @bh: The bottom half to be scheduled. | |
105 | */ | |
106 | void qemu_bh_schedule(QEMUBH *bh); | |
107 | ||
108 | /** | |
109 | * qemu_bh_cancel: Cancel execution of a bottom half. | |
110 | * | |
111 | * Canceling execution of a bottom half undoes the effect of calls to | |
112 | * qemu_bh_schedule without freeing its resources yet. While cancellation | |
113 | * itself is also wait-free and thread-safe, it can of course race with the | |
114 | * loop that executes bottom halves unless you are holding the iothread | |
115 | * mutex. This makes it mostly useless if you are not holding the mutex. | |
116 | * | |
117 | * @bh: The bottom half to be canceled. | |
118 | */ | |
119 | void qemu_bh_cancel(QEMUBH *bh); | |
120 | ||
121 | /** | |
122 | *qemu_bh_delete: Cancel execution of a bottom half and free its resources. | |
123 | * | |
124 | * Deleting a bottom half frees the memory that was allocated for it by | |
125 | * qemu_bh_new. It also implies canceling the bottom half if it was | |
126 | * scheduled. | |
127 | * | |
128 | * @bh: The bottom half to be deleted. | |
129 | */ | |
130 | void qemu_bh_delete(QEMUBH *bh); | |
131 | ||
a76bab49 AL |
132 | /* Flush any pending AIO operation. This function will block until all |
133 | * outstanding AIO operations have been completed or cancelled. */ | |
a915f4bc | 134 | void aio_flush(AioContext *ctx); |
a76bab49 | 135 | |
986c28d6 AA |
136 | /* Wait for a single AIO completion to occur. This function will wait |
137 | * until a single AIO event has completed and it will ensure something | |
138 | * has moved before returning. This can issue new pending aio as | |
bcdc1857 PB |
139 | * result of executing I/O completion or bh callbacks. |
140 | * | |
141 | * Return whether there is still any pending AIO operation. */ | |
a915f4bc | 142 | bool aio_wait(AioContext *ctx); |
a76bab49 | 143 | |
9958c351 PB |
144 | #ifdef CONFIG_POSIX |
145 | /* Returns 1 if there are still outstanding AIO requests; 0 otherwise */ | |
146 | typedef int (AioFlushHandler)(void *opaque); | |
147 | ||
a76bab49 AL |
148 | /* Register a file descriptor and associated callbacks. Behaves very similarly |
149 | * to qemu_set_fd_handler2. Unlike qemu_set_fd_handler2, these callbacks will | |
150 | * be invoked when using either qemu_aio_wait() or qemu_aio_flush(). | |
151 | * | |
152 | * Code that invokes AIO completion functions should rely on this function | |
153 | * instead of qemu_set_fd_handler[2]. | |
154 | */ | |
a915f4bc PB |
155 | void aio_set_fd_handler(AioContext *ctx, |
156 | int fd, | |
157 | IOHandler *io_read, | |
158 | IOHandler *io_write, | |
159 | AioFlushHandler *io_flush, | |
160 | void *opaque); | |
9958c351 PB |
161 | #endif |
162 | ||
163 | /* Register an event notifier and associated callbacks. Behaves very similarly | |
164 | * to event_notifier_set_handler. Unlike event_notifier_set_handler, these callbacks | |
165 | * will be invoked when using either qemu_aio_wait() or qemu_aio_flush(). | |
166 | * | |
167 | * Code that invokes AIO completion functions should rely on this function | |
168 | * instead of event_notifier_set_handler. | |
169 | */ | |
a915f4bc PB |
170 | void aio_set_event_notifier(AioContext *ctx, |
171 | EventNotifier *notifier, | |
172 | EventNotifierHandler *io_read, | |
173 | AioFlushEventNotifierHandler *io_flush); | |
174 | ||
175 | /* Functions to operate on the main QEMU AioContext. */ | |
176 | ||
177 | void qemu_aio_flush(void); | |
178 | bool qemu_aio_wait(void); | |
9958c351 PB |
179 | void qemu_aio_set_event_notifier(EventNotifier *notifier, |
180 | EventNotifierHandler *io_read, | |
181 | AioFlushEventNotifierHandler *io_flush); | |
a76bab49 | 182 | |
a915f4bc PB |
183 | #ifdef CONFIG_POSIX |
184 | void qemu_aio_set_fd_handler(int fd, | |
185 | IOHandler *io_read, | |
186 | IOHandler *io_write, | |
187 | AioFlushHandler *io_flush, | |
188 | void *opaque); | |
189 | #endif | |
190 | ||
a76bab49 | 191 | #endif |