1/*
2 * AioContext wait support
3 *
4 * Copyright (C) 2018 Red Hat, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25#ifndef QEMU_AIO_WAIT_H
26#define QEMU_AIO_WAIT_H
27
28#include "block/aio.h"
29
30/**
31 * AioWait:
32 *
33 * An object that facilitates synchronous waiting on a condition. A single
34 * global AioWait object (global_aio_wait) is used internally.
35 *
36 * The main loop can wait on an operation running in an IOThread as follows:
37 *
38 * AioContext *ctx = ...;
39 * MyWork work = { .done = false };
40 * schedule_my_work_in_iothread(ctx, &work);
41 * AIO_WAIT_WHILE(ctx, !work.done);
42 *
43 * The IOThread must call aio_wait_kick() to notify the main loop when
44 * work.done changes:
45 *
46 * static void do_work(...)
47 * {
48 * ...
49 * work.done = true;
50 * aio_wait_kick();
51 * }
52 */
53typedef struct {
54 /* Number of waiting AIO_WAIT_WHILE() callers. Accessed with atomic ops. */
55 unsigned num_waiters;
56} AioWait;
57
58extern AioWait global_aio_wait;
59
60/**
61 * AIO_WAIT_WHILE:
62 * @ctx: the aio context, or NULL if multiple aio contexts (for which the
63 * caller does not hold a lock) are involved in the polling condition.
64 * @cond: wait while this conditional expression is true
65 *
66 * Wait while a condition is true. Use this to implement synchronous
67 * operations that require event loop activity.
68 *
69 * The caller must be sure that something calls aio_wait_kick() when the value
70 * of @cond might have changed.
71 *
72 * The caller's thread must be the IOThread that owns @ctx or the main loop
73 * thread (with @ctx acquired exactly once). This function cannot be used to
74 * wait on conditions between two IOThreads since that could lead to deadlock,
75 * go via the main loop instead.
76 */
77#define AIO_WAIT_WHILE(ctx, cond) ({ \
78 bool waited_ = false; \
79 AioWait *wait_ = &global_aio_wait; \
80 AioContext *ctx_ = (ctx); \
81 /* Increment wait_->num_waiters before evaluating cond. */ \
82 atomic_inc(&wait_->num_waiters); \
83 if (ctx_ && in_aio_context_home_thread(ctx_)) { \
84 while ((cond)) { \
85 aio_poll(ctx_, true); \
86 waited_ = true; \
87 } \
88 } else { \
89 assert(qemu_get_current_aio_context() == \
90 qemu_get_aio_context()); \
91 while ((cond)) { \
92 if (ctx_) { \
93 aio_context_release(ctx_); \
94 } \
95 aio_poll(qemu_get_aio_context(), true); \
96 if (ctx_) { \
97 aio_context_acquire(ctx_); \
98 } \
99 waited_ = true; \
100 } \
101 } \
102 atomic_dec(&wait_->num_waiters); \
103 waited_; })
104
105/**
106 * aio_wait_kick:
107 * Wake up the main thread if it is waiting on AIO_WAIT_WHILE(). During
108 * synchronous operations performed in an IOThread, the main thread lets the
109 * IOThread's event loop run, waiting for the operation to complete. A
110 * aio_wait_kick() call will wake up the main thread.
111 */
112void aio_wait_kick(void);
113
114/**
115 * aio_wait_bh_oneshot:
116 * @ctx: the aio context
117 * @cb: the BH callback function
118 * @opaque: user data for the BH callback function
119 *
120 * Run a BH in @ctx and wait for it to complete.
121 *
122 * Must be called from the main loop thread with @ctx acquired exactly once.
123 * Note that main loop event processing may occur.
124 */
125void aio_wait_bh_oneshot(AioContext *ctx, QEMUBHFunc *cb, void *opaque);
126
127#endif /* QEMU_AIO_WAIT_H */
128