]>
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 | ||
1de7afc9 PB |
17 | #include "qemu/queue.h" |
18 | #include "qemu/event_notifier.h" | |
dcc772e2 | 19 | #include "qemu/thread.h" |
dae21b98 | 20 | #include "qemu/timer.h" |
a76bab49 | 21 | |
7c84b1b8 | 22 | typedef struct BlockAIOCB BlockAIOCB; |
097310b5 | 23 | typedef void BlockCompletionFunc(void *opaque, int ret); |
85e8dab1 | 24 | |
d7331bed | 25 | typedef struct AIOCBInfo { |
7c84b1b8 MA |
26 | void (*cancel_async)(BlockAIOCB *acb); |
27 | AioContext *(*get_aio_context)(BlockAIOCB *acb); | |
8c82e9a4 | 28 | size_t aiocb_size; |
d7331bed | 29 | } AIOCBInfo; |
85e8dab1 | 30 | |
7c84b1b8 | 31 | struct BlockAIOCB { |
d7331bed | 32 | const AIOCBInfo *aiocb_info; |
85e8dab1 | 33 | BlockDriverState *bs; |
097310b5 | 34 | BlockCompletionFunc *cb; |
85e8dab1 | 35 | void *opaque; |
f197fe2b | 36 | int refcnt; |
85e8dab1 PB |
37 | }; |
38 | ||
d7331bed | 39 | void *qemu_aio_get(const AIOCBInfo *aiocb_info, BlockDriverState *bs, |
097310b5 | 40 | BlockCompletionFunc *cb, void *opaque); |
8007429a | 41 | void qemu_aio_unref(void *p); |
f197fe2b | 42 | void qemu_aio_ref(void *p); |
85e8dab1 | 43 | |
f627aab1 | 44 | typedef struct AioHandler AioHandler; |
4749079c | 45 | typedef QLIST_HEAD(, AioHandler) AioHandlerList; |
f627aab1 | 46 | typedef void QEMUBHFunc(void *opaque); |
f6a51c84 | 47 | typedef bool AioPollFn(void *opaque); |
f627aab1 PB |
48 | typedef void IOHandler(void *opaque); |
49 | ||
0c330a73 | 50 | struct Coroutine; |
0187f5c9 PB |
51 | struct ThreadPool; |
52 | struct LinuxAioState; | |
6663a0a3 | 53 | struct LuringState; |
0187f5c9 | 54 | |
1f050a46 SH |
55 | /* Callbacks for file descriptor monitoring implementations */ |
56 | typedef struct { | |
57 | /* | |
58 | * update: | |
59 | * @ctx: the AioContext | |
b321051c SH |
60 | * @old_node: the existing handler or NULL if this file descriptor is being |
61 | * monitored for the first time | |
62 | * @new_node: the new handler or NULL if this file descriptor is being | |
63 | * removed | |
1f050a46 | 64 | * |
b321051c | 65 | * Add/remove/modify a monitored file descriptor. |
1f050a46 SH |
66 | * |
67 | * Called with ctx->list_lock acquired. | |
68 | */ | |
b321051c | 69 | void (*update)(AioContext *ctx, AioHandler *old_node, AioHandler *new_node); |
1f050a46 SH |
70 | |
71 | /* | |
72 | * wait: | |
73 | * @ctx: the AioContext | |
74 | * @ready_list: list for handlers that become ready | |
75 | * @timeout: maximum duration to wait, in nanoseconds | |
76 | * | |
77 | * Wait for file descriptors to become ready and place them on ready_list. | |
78 | * | |
79 | * Called with ctx->list_lock incremented but not locked. | |
80 | * | |
81 | * Returns: number of ready file descriptors. | |
82 | */ | |
83 | int (*wait)(AioContext *ctx, AioHandlerList *ready_list, int64_t timeout); | |
84 | } FDMonOps; | |
85 | ||
8c6b0356 SH |
86 | /* |
87 | * Each aio_bh_poll() call carves off a slice of the BH list, so that newly | |
88 | * scheduled BHs are not processed until the next aio_bh_poll() call. All | |
89 | * active aio_bh_poll() calls chain their slices together in a list, so that | |
90 | * nested aio_bh_poll() calls process all scheduled bottom halves. | |
91 | */ | |
92 | typedef QSLIST_HEAD(, QEMUBH) BHList; | |
93 | typedef struct BHListSlice BHListSlice; | |
94 | struct BHListSlice { | |
95 | BHList bh_list; | |
96 | QSIMPLEQ_ENTRY(BHListSlice) next; | |
97 | }; | |
98 | ||
6a1751b7 | 99 | struct AioContext { |
e3713e00 PB |
100 | GSource source; |
101 | ||
7c690fd1 | 102 | /* Used by AioContext users to protect from multi-threaded access. */ |
3fe71223 | 103 | QemuRecMutex lock; |
98563fc3 | 104 | |
7c690fd1 | 105 | /* The list of registered AIO handlers. Protected by ctx->list_lock. */ |
4749079c SH |
106 | AioHandlerList aio_handlers; |
107 | ||
108 | /* The list of AIO handlers to be deleted. Protected by ctx->list_lock. */ | |
109 | AioHandlerList deleted_aio_handlers; | |
a915f4bc | 110 | |
eabc9779 PB |
111 | /* Used to avoid unnecessary event_notifier_set calls in aio_notify; |
112 | * accessed with atomic primitives. If this field is 0, everything | |
113 | * (file descriptors, bottom halves, timers) will be re-evaluated | |
114 | * before the next blocking poll(), thus the event_notifier_set call | |
115 | * can be skipped. If it is non-zero, you may need to wake up a | |
116 | * concurrent aio_poll or the glib main event loop, making | |
117 | * event_notifier_set necessary. | |
118 | * | |
119 | * Bit 0 is reserved for GSource usage of the AioContext, and is 1 | |
54a16a63 | 120 | * between a call to aio_ctx_prepare and the next call to aio_ctx_check. |
eabc9779 PB |
121 | * Bits 1-31 simply count the number of active calls to aio_poll |
122 | * that are in the prepare or poll phase. | |
123 | * | |
124 | * The GSource and aio_poll must use a different mechanism because | |
125 | * there is no certainty that a call to GSource's prepare callback | |
126 | * (via g_main_context_prepare) is indeed followed by check and | |
127 | * dispatch. It's not clear whether this would be a bug, but let's | |
128 | * play safe and allow it---it will just cause extra calls to | |
129 | * event_notifier_set until the next call to dispatch. | |
130 | * | |
131 | * Instead, the aio_poll calls include both the prepare and the | |
132 | * dispatch phase, hence a simple counter is enough for them. | |
0ceb849b | 133 | */ |
eabc9779 | 134 | uint32_t notify_me; |
0ceb849b | 135 | |
7c690fd1 PB |
136 | /* A lock to protect between QEMUBH and AioHandler adders and deleter, |
137 | * and to ensure that no callbacks are removed while we're walking and | |
138 | * dispatching them. | |
d7c99a12 PB |
139 | */ |
140 | QemuLockCnt list_lock; | |
0ceb849b | 141 | |
8c6b0356 SH |
142 | /* Bottom Halves pending aio_bh_poll() processing */ |
143 | BHList bh_list; | |
144 | ||
145 | /* Chained BH list slices for each nested aio_bh_poll() call */ | |
146 | QSIMPLEQ_HEAD(, BHListSlice) bh_slice_list; | |
f627aab1 | 147 | |
05e514b1 PB |
148 | /* Used by aio_notify. |
149 | * | |
150 | * "notified" is used to avoid expensive event_notifier_test_and_clear | |
151 | * calls. When it is clear, the EventNotifier is clear, or one thread | |
152 | * is going to clear "notified" before processing more events. False | |
153 | * positives are possible, i.e. "notified" could be set even though the | |
154 | * EventNotifier is clear. | |
155 | * | |
156 | * Note that event_notifier_set *cannot* be optimized the same way. For | |
157 | * more information on the problem that would result, see "#ifdef BUG2" | |
158 | * in the docs/aio_notify_accept.promela formal model. | |
159 | */ | |
160 | bool notified; | |
2f4dc3c1 | 161 | EventNotifier notifier; |
6b5f8762 | 162 | |
0c330a73 PB |
163 | QSLIST_HEAD(, Coroutine) scheduled_coroutines; |
164 | QEMUBH *co_schedule_bh; | |
165 | ||
7c690fd1 PB |
166 | /* Thread pool for performing work and receiving completion callbacks. |
167 | * Has its own locking. | |
168 | */ | |
9b34277d | 169 | struct ThreadPool *thread_pool; |
dae21b98 | 170 | |
0187f5c9 | 171 | #ifdef CONFIG_LINUX_AIO |
6663a0a3 AM |
172 | /* |
173 | * State for native Linux AIO. Uses aio_context_acquire/release for | |
0187f5c9 PB |
174 | * locking. |
175 | */ | |
176 | struct LinuxAioState *linux_aio; | |
177 | #endif | |
6663a0a3 AM |
178 | #ifdef CONFIG_LINUX_IO_URING |
179 | /* | |
180 | * State for Linux io_uring. Uses aio_context_acquire/release for | |
181 | * locking. | |
182 | */ | |
183 | struct LuringState *linux_io_uring; | |
184 | #endif | |
0187f5c9 | 185 | |
7c690fd1 PB |
186 | /* TimerLists for calling timers - one per clock type. Has its own |
187 | * locking. | |
188 | */ | |
dae21b98 | 189 | QEMUTimerListGroup tlg; |
c1e1e5fa FZ |
190 | |
191 | int external_disable_cnt; | |
fbe3fc5c | 192 | |
4a1cba38 SH |
193 | /* Number of AioHandlers without .io_poll() */ |
194 | int poll_disable_cnt; | |
195 | ||
82a41186 SH |
196 | /* Polling mode parameters */ |
197 | int64_t poll_ns; /* current polling time in nanoseconds */ | |
198 | int64_t poll_max_ns; /* maximum polling time in nanoseconds */ | |
199 | int64_t poll_grow; /* polling time growth factor */ | |
200 | int64_t poll_shrink; /* polling time shrink factor */ | |
4a1cba38 | 201 | |
684e508c SH |
202 | /* Are we in polling mode or monitoring file descriptors? */ |
203 | bool poll_started; | |
204 | ||
fbe3fc5c FZ |
205 | /* epoll(7) state used when built with CONFIG_EPOLL */ |
206 | int epollfd; | |
1f050a46 SH |
207 | |
208 | const FDMonOps *fdmon_ops; | |
6a1751b7 | 209 | }; |
f627aab1 | 210 | |
f627aab1 PB |
211 | /** |
212 | * aio_context_new: Allocate a new AioContext. | |
213 | * | |
214 | * AioContext provide a mini event-loop that can be waited on synchronously. | |
215 | * They also provide bottom halves, a service to execute a piece of code | |
216 | * as soon as possible. | |
217 | */ | |
2f78e491 | 218 | AioContext *aio_context_new(Error **errp); |
f627aab1 | 219 | |
e3713e00 PB |
220 | /** |
221 | * aio_context_ref: | |
222 | * @ctx: The AioContext to operate on. | |
223 | * | |
224 | * Add a reference to an AioContext. | |
225 | */ | |
226 | void aio_context_ref(AioContext *ctx); | |
227 | ||
228 | /** | |
229 | * aio_context_unref: | |
230 | * @ctx: The AioContext to operate on. | |
231 | * | |
232 | * Drop a reference to an AioContext. | |
233 | */ | |
234 | void aio_context_unref(AioContext *ctx); | |
235 | ||
98563fc3 | 236 | /* Take ownership of the AioContext. If the AioContext will be shared between |
49110174 PB |
237 | * threads, and a thread does not want to be interrupted, it will have to |
238 | * take ownership around calls to aio_poll(). Otherwise, aio_poll() | |
239 | * automatically takes care of calling aio_context_acquire and | |
240 | * aio_context_release. | |
98563fc3 | 241 | * |
7c690fd1 PB |
242 | * Note that this is separate from bdrv_drained_begin/bdrv_drained_end. A |
243 | * thread still has to call those to avoid being interrupted by the guest. | |
244 | * | |
245 | * Bottom halves, timers and callbacks can be created or removed without | |
246 | * acquiring the AioContext. | |
98563fc3 SH |
247 | */ |
248 | void aio_context_acquire(AioContext *ctx); | |
249 | ||
250 | /* Relinquish ownership of the AioContext. */ | |
251 | void aio_context_release(AioContext *ctx); | |
252 | ||
5b8bb359 PB |
253 | /** |
254 | * aio_bh_schedule_oneshot: Allocate a new bottom half structure that will run | |
255 | * only once and as soon as possible. | |
256 | */ | |
257 | void aio_bh_schedule_oneshot(AioContext *ctx, QEMUBHFunc *cb, void *opaque); | |
258 | ||
f627aab1 PB |
259 | /** |
260 | * aio_bh_new: Allocate a new bottom half structure. | |
261 | * | |
262 | * Bottom halves are lightweight callbacks whose invocation is guaranteed | |
263 | * to be wait-free, thread-safe and signal-safe. The #QEMUBH structure | |
264 | * is opaque and must be allocated prior to its use. | |
265 | */ | |
266 | QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque); | |
267 | ||
2f4dc3c1 PB |
268 | /** |
269 | * aio_notify: Force processing of pending events. | |
270 | * | |
271 | * Similar to signaling a condition variable, aio_notify forces | |
722f8d90 YB |
272 | * aio_poll to exit, so that the next call will re-examine pending events. |
273 | * The caller of aio_notify will usually call aio_poll again very soon, | |
2f4dc3c1 PB |
274 | * or go through another iteration of the GLib main loop. Hence, aio_notify |
275 | * also has the side effect of recalculating the sets of file descriptors | |
276 | * that the main loop waits for. | |
277 | * | |
278 | * Calling aio_notify is rarely necessary, because for example scheduling | |
279 | * a bottom half calls it already. | |
280 | */ | |
281 | void aio_notify(AioContext *ctx); | |
282 | ||
05e514b1 PB |
283 | /** |
284 | * aio_notify_accept: Acknowledge receiving an aio_notify. | |
285 | * | |
286 | * aio_notify() uses an EventNotifier in order to wake up a sleeping | |
287 | * aio_poll() or g_main_context_iteration(). Calls to aio_notify() are | |
288 | * usually rare, but the AioContext has to clear the EventNotifier on | |
289 | * every aio_poll() or g_main_context_iteration() in order to avoid | |
290 | * busy waiting. This event_notifier_test_and_clear() cannot be done | |
291 | * using the usual aio_context_set_event_notifier(), because it must | |
292 | * be done before processing all events (file descriptors, bottom halves, | |
293 | * timers). | |
294 | * | |
295 | * aio_notify_accept() is an optimized event_notifier_test_and_clear() | |
296 | * that is specific to an AioContext's notifier; it is used internally | |
297 | * to clear the EventNotifier only if aio_notify() had been called. | |
298 | */ | |
299 | void aio_notify_accept(AioContext *ctx); | |
300 | ||
df281b80 PD |
301 | /** |
302 | * aio_bh_call: Executes callback function of the specified BH. | |
303 | */ | |
304 | void aio_bh_call(QEMUBH *bh); | |
305 | ||
f627aab1 PB |
306 | /** |
307 | * aio_bh_poll: Poll bottom halves for an AioContext. | |
308 | * | |
309 | * These are internal functions used by the QEMU main loop. | |
dcc772e2 LPF |
310 | * And notice that multiple occurrences of aio_bh_poll cannot |
311 | * be called concurrently | |
f627aab1 PB |
312 | */ |
313 | int aio_bh_poll(AioContext *ctx); | |
f627aab1 PB |
314 | |
315 | /** | |
316 | * qemu_bh_schedule: Schedule a bottom half. | |
317 | * | |
318 | * Scheduling a bottom half interrupts the main loop and causes the | |
319 | * execution of the callback that was passed to qemu_bh_new. | |
320 | * | |
321 | * Bottom halves that are scheduled from a bottom half handler are instantly | |
322 | * invoked. This can create an infinite loop if a bottom half handler | |
323 | * schedules itself. | |
324 | * | |
325 | * @bh: The bottom half to be scheduled. | |
326 | */ | |
327 | void qemu_bh_schedule(QEMUBH *bh); | |
328 | ||
329 | /** | |
330 | * qemu_bh_cancel: Cancel execution of a bottom half. | |
331 | * | |
332 | * Canceling execution of a bottom half undoes the effect of calls to | |
333 | * qemu_bh_schedule without freeing its resources yet. While cancellation | |
334 | * itself is also wait-free and thread-safe, it can of course race with the | |
335 | * loop that executes bottom halves unless you are holding the iothread | |
336 | * mutex. This makes it mostly useless if you are not holding the mutex. | |
337 | * | |
338 | * @bh: The bottom half to be canceled. | |
339 | */ | |
340 | void qemu_bh_cancel(QEMUBH *bh); | |
341 | ||
342 | /** | |
343 | *qemu_bh_delete: Cancel execution of a bottom half and free its resources. | |
344 | * | |
345 | * Deleting a bottom half frees the memory that was allocated for it by | |
346 | * qemu_bh_new. It also implies canceling the bottom half if it was | |
347 | * scheduled. | |
dcc772e2 LPF |
348 | * This func is async. The bottom half will do the delete action at the finial |
349 | * end. | |
f627aab1 PB |
350 | * |
351 | * @bh: The bottom half to be deleted. | |
352 | */ | |
353 | void qemu_bh_delete(QEMUBH *bh); | |
354 | ||
cd9ba1eb | 355 | /* Return whether there are any pending callbacks from the GSource |
a3462c65 PB |
356 | * attached to the AioContext, before g_poll is invoked. |
357 | * | |
358 | * This is used internally in the implementation of the GSource. | |
359 | */ | |
360 | bool aio_prepare(AioContext *ctx); | |
361 | ||
362 | /* Return whether there are any pending callbacks from the GSource | |
363 | * attached to the AioContext, after g_poll is invoked. | |
cd9ba1eb PB |
364 | * |
365 | * This is used internally in the implementation of the GSource. | |
366 | */ | |
367 | bool aio_pending(AioContext *ctx); | |
368 | ||
e4c7e2d1 PB |
369 | /* Dispatch any pending callbacks from the GSource attached to the AioContext. |
370 | * | |
371 | * This is used internally in the implementation of the GSource. | |
372 | */ | |
a153bf52 | 373 | void aio_dispatch(AioContext *ctx); |
e4c7e2d1 | 374 | |
7c0628b2 PB |
375 | /* Progress in completing AIO work to occur. This can issue new pending |
376 | * aio as a result of executing I/O completion or bh callbacks. | |
bcdc1857 | 377 | * |
acfb23ad PB |
378 | * Return whether any progress was made by executing AIO or bottom half |
379 | * handlers. If @blocking == true, this should always be true except | |
380 | * if someone called aio_notify. | |
7c0628b2 PB |
381 | * |
382 | * If there are no pending bottom halves, but there are pending AIO | |
383 | * operations, it may not be possible to make any progress without | |
384 | * blocking. If @blocking is true, this function will wait until one | |
385 | * or more AIO events have completed, to ensure something has moved | |
386 | * before returning. | |
7c0628b2 PB |
387 | */ |
388 | bool aio_poll(AioContext *ctx, bool blocking); | |
a76bab49 AL |
389 | |
390 | /* Register a file descriptor and associated callbacks. Behaves very similarly | |
6484e422 | 391 | * to qemu_set_fd_handler. Unlike qemu_set_fd_handler, these callbacks will |
87f68d31 | 392 | * be invoked when using aio_poll(). |
a76bab49 AL |
393 | * |
394 | * Code that invokes AIO completion functions should rely on this function | |
395 | * instead of qemu_set_fd_handler[2]. | |
396 | */ | |
a915f4bc PB |
397 | void aio_set_fd_handler(AioContext *ctx, |
398 | int fd, | |
dca21ef2 | 399 | bool is_external, |
a915f4bc PB |
400 | IOHandler *io_read, |
401 | IOHandler *io_write, | |
f6a51c84 | 402 | AioPollFn *io_poll, |
a915f4bc | 403 | void *opaque); |
9958c351 | 404 | |
684e508c SH |
405 | /* Set polling begin/end callbacks for a file descriptor that has already been |
406 | * registered with aio_set_fd_handler. Do nothing if the file descriptor is | |
407 | * not registered. | |
408 | */ | |
409 | void aio_set_fd_poll(AioContext *ctx, int fd, | |
410 | IOHandler *io_poll_begin, | |
411 | IOHandler *io_poll_end); | |
412 | ||
9958c351 PB |
413 | /* Register an event notifier and associated callbacks. Behaves very similarly |
414 | * to event_notifier_set_handler. Unlike event_notifier_set_handler, these callbacks | |
87f68d31 | 415 | * will be invoked when using aio_poll(). |
9958c351 PB |
416 | * |
417 | * Code that invokes AIO completion functions should rely on this function | |
418 | * instead of event_notifier_set_handler. | |
419 | */ | |
a915f4bc PB |
420 | void aio_set_event_notifier(AioContext *ctx, |
421 | EventNotifier *notifier, | |
dca21ef2 | 422 | bool is_external, |
f6a51c84 SH |
423 | EventNotifierHandler *io_read, |
424 | AioPollFn *io_poll); | |
a915f4bc | 425 | |
684e508c SH |
426 | /* Set polling begin/end callbacks for an event notifier that has already been |
427 | * registered with aio_set_event_notifier. Do nothing if the event notifier is | |
428 | * not registered. | |
429 | */ | |
430 | void aio_set_event_notifier_poll(AioContext *ctx, | |
431 | EventNotifier *notifier, | |
432 | EventNotifierHandler *io_poll_begin, | |
433 | EventNotifierHandler *io_poll_end); | |
434 | ||
e3713e00 PB |
435 | /* Return a GSource that lets the main loop poll the file descriptors attached |
436 | * to this AioContext. | |
437 | */ | |
438 | GSource *aio_get_g_source(AioContext *ctx); | |
439 | ||
9b34277d SH |
440 | /* Return the ThreadPool bound to this AioContext */ |
441 | struct ThreadPool *aio_get_thread_pool(AioContext *ctx); | |
442 | ||
ed6e2161 NA |
443 | /* Setup the LinuxAioState bound to this AioContext */ |
444 | struct LinuxAioState *aio_setup_linux_aio(AioContext *ctx, Error **errp); | |
445 | ||
0187f5c9 PB |
446 | /* Return the LinuxAioState bound to this AioContext */ |
447 | struct LinuxAioState *aio_get_linux_aio(AioContext *ctx); | |
448 | ||
6663a0a3 AM |
449 | /* Setup the LuringState bound to this AioContext */ |
450 | struct LuringState *aio_setup_linux_io_uring(AioContext *ctx, Error **errp); | |
451 | ||
452 | /* Return the LuringState bound to this AioContext */ | |
453 | struct LuringState *aio_get_linux_io_uring(AioContext *ctx); | |
4e29e831 | 454 | /** |
89a603a0 | 455 | * aio_timer_new_with_attrs: |
4e29e831 AB |
456 | * @ctx: the aio context |
457 | * @type: the clock type | |
458 | * @scale: the scale | |
89a603a0 AP |
459 | * @attributes: 0, or one to multiple OR'ed QEMU_TIMER_ATTR_<id> values |
460 | * to assign | |
4e29e831 AB |
461 | * @cb: the callback to call on timer expiry |
462 | * @opaque: the opaque pointer to pass to the callback | |
463 | * | |
89a603a0 | 464 | * Allocate a new timer (with attributes) attached to the context @ctx. |
4e29e831 AB |
465 | * The function is responsible for memory allocation. |
466 | * | |
89a603a0 AP |
467 | * The preferred interface is aio_timer_init or aio_timer_init_with_attrs. |
468 | * Use that unless you really need dynamic memory allocation. | |
469 | * | |
470 | * Returns: a pointer to the new timer | |
471 | */ | |
472 | static inline QEMUTimer *aio_timer_new_with_attrs(AioContext *ctx, | |
473 | QEMUClockType type, | |
474 | int scale, int attributes, | |
475 | QEMUTimerCB *cb, void *opaque) | |
476 | { | |
477 | return timer_new_full(&ctx->tlg, type, scale, attributes, cb, opaque); | |
478 | } | |
479 | ||
480 | /** | |
481 | * aio_timer_new: | |
482 | * @ctx: the aio context | |
483 | * @type: the clock type | |
484 | * @scale: the scale | |
485 | * @cb: the callback to call on timer expiry | |
486 | * @opaque: the opaque pointer to pass to the callback | |
487 | * | |
488 | * Allocate a new timer attached to the context @ctx. | |
489 | * See aio_timer_new_with_attrs for details. | |
4e29e831 AB |
490 | * |
491 | * Returns: a pointer to the new timer | |
492 | */ | |
493 | static inline QEMUTimer *aio_timer_new(AioContext *ctx, QEMUClockType type, | |
494 | int scale, | |
495 | QEMUTimerCB *cb, void *opaque) | |
496 | { | |
89a603a0 AP |
497 | return timer_new_full(&ctx->tlg, type, scale, 0, cb, opaque); |
498 | } | |
499 | ||
500 | /** | |
501 | * aio_timer_init_with_attrs: | |
502 | * @ctx: the aio context | |
503 | * @ts: the timer | |
504 | * @type: the clock type | |
505 | * @scale: the scale | |
506 | * @attributes: 0, or one to multiple OR'ed QEMU_TIMER_ATTR_<id> values | |
507 | * to assign | |
508 | * @cb: the callback to call on timer expiry | |
509 | * @opaque: the opaque pointer to pass to the callback | |
510 | * | |
511 | * Initialise a new timer (with attributes) attached to the context @ctx. | |
512 | * The caller is responsible for memory allocation. | |
513 | */ | |
514 | static inline void aio_timer_init_with_attrs(AioContext *ctx, | |
515 | QEMUTimer *ts, QEMUClockType type, | |
516 | int scale, int attributes, | |
517 | QEMUTimerCB *cb, void *opaque) | |
518 | { | |
519 | timer_init_full(ts, &ctx->tlg, type, scale, attributes, cb, opaque); | |
4e29e831 AB |
520 | } |
521 | ||
522 | /** | |
523 | * aio_timer_init: | |
524 | * @ctx: the aio context | |
525 | * @ts: the timer | |
526 | * @type: the clock type | |
527 | * @scale: the scale | |
528 | * @cb: the callback to call on timer expiry | |
529 | * @opaque: the opaque pointer to pass to the callback | |
530 | * | |
531 | * Initialise a new timer attached to the context @ctx. | |
89a603a0 | 532 | * See aio_timer_init_with_attrs for details. |
4e29e831 AB |
533 | */ |
534 | static inline void aio_timer_init(AioContext *ctx, | |
535 | QEMUTimer *ts, QEMUClockType type, | |
536 | int scale, | |
537 | QEMUTimerCB *cb, void *opaque) | |
538 | { | |
89a603a0 | 539 | timer_init_full(ts, &ctx->tlg, type, scale, 0, cb, opaque); |
4e29e831 AB |
540 | } |
541 | ||
845ca10d PB |
542 | /** |
543 | * aio_compute_timeout: | |
544 | * @ctx: the aio context | |
545 | * | |
546 | * Compute the timeout that a blocking aio_poll should use. | |
547 | */ | |
548 | int64_t aio_compute_timeout(AioContext *ctx); | |
549 | ||
c1e1e5fa FZ |
550 | /** |
551 | * aio_disable_external: | |
552 | * @ctx: the aio context | |
553 | * | |
554 | * Disable the further processing of external clients. | |
555 | */ | |
556 | static inline void aio_disable_external(AioContext *ctx) | |
557 | { | |
558 | atomic_inc(&ctx->external_disable_cnt); | |
559 | } | |
560 | ||
561 | /** | |
562 | * aio_enable_external: | |
563 | * @ctx: the aio context | |
564 | * | |
565 | * Enable the processing of external clients. | |
566 | */ | |
567 | static inline void aio_enable_external(AioContext *ctx) | |
568 | { | |
321d1dba SH |
569 | int old; |
570 | ||
571 | old = atomic_fetch_dec(&ctx->external_disable_cnt); | |
572 | assert(old > 0); | |
573 | if (old == 1) { | |
574 | /* Kick event loop so it re-arms file descriptors */ | |
575 | aio_notify(ctx); | |
576 | } | |
c1e1e5fa FZ |
577 | } |
578 | ||
5ceb9e39 FZ |
579 | /** |
580 | * aio_external_disabled: | |
581 | * @ctx: the aio context | |
582 | * | |
583 | * Return true if the external clients are disabled. | |
584 | */ | |
585 | static inline bool aio_external_disabled(AioContext *ctx) | |
586 | { | |
587 | return atomic_read(&ctx->external_disable_cnt); | |
588 | } | |
589 | ||
c1e1e5fa FZ |
590 | /** |
591 | * aio_node_check: | |
592 | * @ctx: the aio context | |
593 | * @is_external: Whether or not the checked node is an external event source. | |
594 | * | |
595 | * Check if the node's is_external flag is okay to be polled by the ctx at this | |
596 | * moment. True means green light. | |
597 | */ | |
598 | static inline bool aio_node_check(AioContext *ctx, bool is_external) | |
599 | { | |
600 | return !is_external || !atomic_read(&ctx->external_disable_cnt); | |
601 | } | |
602 | ||
0c330a73 PB |
603 | /** |
604 | * aio_co_schedule: | |
605 | * @ctx: the aio context | |
606 | * @co: the coroutine | |
607 | * | |
608 | * Start a coroutine on a remote AioContext. | |
609 | * | |
610 | * The coroutine must not be entered by anyone else while aio_co_schedule() | |
611 | * is active. In addition the coroutine must have yielded unless ctx | |
612 | * is the context in which the coroutine is running (i.e. the value of | |
613 | * qemu_get_current_aio_context() from the coroutine itself). | |
614 | */ | |
615 | void aio_co_schedule(AioContext *ctx, struct Coroutine *co); | |
616 | ||
617 | /** | |
618 | * aio_co_wake: | |
619 | * @co: the coroutine | |
620 | * | |
621 | * Restart a coroutine on the AioContext where it was running last, thus | |
622 | * preventing coroutines from jumping from one context to another when they | |
623 | * go to sleep. | |
624 | * | |
625 | * aio_co_wake may be executed either in coroutine or non-coroutine | |
626 | * context. The coroutine must not be entered by anyone else while | |
627 | * aio_co_wake() is active. | |
628 | */ | |
629 | void aio_co_wake(struct Coroutine *co); | |
630 | ||
8865852e FZ |
631 | /** |
632 | * aio_co_enter: | |
633 | * @ctx: the context to run the coroutine | |
634 | * @co: the coroutine to run | |
635 | * | |
636 | * Enter a coroutine in the specified AioContext. | |
637 | */ | |
638 | void aio_co_enter(AioContext *ctx, struct Coroutine *co); | |
639 | ||
e4370165 PB |
640 | /** |
641 | * Return the AioContext whose event loop runs in the current thread. | |
642 | * | |
643 | * If called from an IOThread this will be the IOThread's AioContext. If | |
644 | * called from another thread it will be the main loop AioContext. | |
645 | */ | |
646 | AioContext *qemu_get_current_aio_context(void); | |
647 | ||
648 | /** | |
d2b63ba8 | 649 | * in_aio_context_home_thread: |
e4370165 PB |
650 | * @ctx: the aio context |
651 | * | |
d2b63ba8 SH |
652 | * Return whether we are running in the thread that normally runs @ctx. Note |
653 | * that acquiring/releasing ctx does not affect the outcome, each AioContext | |
654 | * still only has one home thread that is responsible for running it. | |
e4370165 | 655 | */ |
d2b63ba8 | 656 | static inline bool in_aio_context_home_thread(AioContext *ctx) |
e4370165 PB |
657 | { |
658 | return ctx == qemu_get_current_aio_context(); | |
659 | } | |
660 | ||
37fcee5d FZ |
661 | /** |
662 | * aio_context_setup: | |
663 | * @ctx: the aio context | |
664 | * | |
665 | * Initialize the aio context. | |
666 | */ | |
7e003465 | 667 | void aio_context_setup(AioContext *ctx); |
37fcee5d | 668 | |
cd0a6d2b JW |
669 | /** |
670 | * aio_context_destroy: | |
671 | * @ctx: the aio context | |
672 | * | |
673 | * Destroy the aio context. | |
674 | */ | |
675 | void aio_context_destroy(AioContext *ctx); | |
676 | ||
4a1cba38 SH |
677 | /** |
678 | * aio_context_set_poll_params: | |
679 | * @ctx: the aio context | |
680 | * @max_ns: how long to busy poll for, in nanoseconds | |
82a41186 SH |
681 | * @grow: polling time growth factor |
682 | * @shrink: polling time shrink factor | |
4a1cba38 SH |
683 | * |
684 | * Poll mode can be disabled by setting poll_max_ns to 0. | |
685 | */ | |
686 | void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns, | |
82a41186 | 687 | int64_t grow, int64_t shrink, |
4a1cba38 SH |
688 | Error **errp); |
689 | ||
a76bab49 | 690 | #endif |