xref: /openbmc/qemu/include/block/aio-wait.h (revision 7eceff5b)
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.  The main
34  * loop can wait on an operation running in an IOThread as follows:
35  *
36  *   AioWait *wait = ...;
37  *   AioContext *ctx = ...;
38  *   MyWork work = { .done = false };
39  *   schedule_my_work_in_iothread(ctx, &work);
40  *   AIO_WAIT_WHILE(wait, ctx, !work.done);
41  *
42  * The IOThread must call aio_wait_kick() to notify the main loop when
43  * work.done changes:
44  *
45  *   static void do_work(...)
46  *   {
47  *       ...
48  *       work.done = true;
49  *       aio_wait_kick(wait);
50  *   }
51  */
52 typedef struct {
53     /* Is the main loop waiting for a kick?  Accessed with atomic ops. */
54     bool need_kick;
55 } AioWait;
56 
57 /**
58  * AIO_WAIT_WHILE:
59  * @wait: the aio wait object
60  * @ctx: the aio context
61  * @cond: wait while this conditional expression is true
62  *
63  * Wait while a condition is true.  Use this to implement synchronous
64  * operations that require event loop activity.
65  *
66  * The caller must be sure that something calls aio_wait_kick() when the value
67  * of @cond might have changed.
68  *
69  * The caller's thread must be the IOThread that owns @ctx or the main loop
70  * thread (with @ctx acquired exactly once).  This function cannot be used to
71  * wait on conditions between two IOThreads since that could lead to deadlock,
72  * go via the main loop instead.
73  */
74 #define AIO_WAIT_WHILE(wait, ctx, cond) ({                  \
75     bool waited_ = false;                                   \
76     bool busy_ = true;                                      \
77     AioWait *wait_ = (wait);                                \
78     AioContext *ctx_ = (ctx);                               \
79     if (in_aio_context_home_thread(ctx_)) {                 \
80         while ((cond) || busy_) {                           \
81             busy_ = aio_poll(ctx_, (cond));                 \
82             waited_ |= !!(cond) | busy_;                    \
83         }                                                   \
84     } else {                                                \
85         assert(qemu_get_current_aio_context() ==            \
86                qemu_get_aio_context());                     \
87         assert(!wait_->need_kick);                          \
88         /* Set wait_->need_kick before evaluating cond.  */ \
89         atomic_mb_set(&wait_->need_kick, true);             \
90         while (busy_) {                                     \
91             if ((cond)) {                                   \
92                 waited_ = busy_ = true;                     \
93                 aio_context_release(ctx_);                  \
94                 aio_poll(qemu_get_aio_context(), true);     \
95                 aio_context_acquire(ctx_);                  \
96             } else {                                        \
97                 busy_ = aio_poll(ctx_, false);              \
98                 waited_ |= busy_;                           \
99             }                                               \
100         }                                                   \
101         atomic_set(&wait_->need_kick, false);               \
102     }                                                       \
103     waited_; })
104 
105 /**
106  * aio_wait_kick:
107  * @wait: the aio wait object that should re-evaluate its condition
108  *
109  * Wake up the main thread if it is waiting on AIO_WAIT_WHILE().  During
110  * synchronous operations performed in an IOThread, the main thread lets the
111  * IOThread's event loop run, waiting for the operation to complete.  A
112  * aio_wait_kick() call will wake up the main thread.
113  */
114 void aio_wait_kick(AioWait *wait);
115 
116 /**
117  * aio_wait_bh_oneshot:
118  * @ctx: the aio context
119  * @cb: the BH callback function
120  * @opaque: user data for the BH callback function
121  *
122  * Run a BH in @ctx and wait for it to complete.
123  *
124  * Must be called from the main loop thread with @ctx acquired exactly once.
125  * Note that main loop event processing may occur.
126  */
127 void aio_wait_bh_oneshot(AioContext *ctx, QEMUBHFunc *cb, void *opaque);
128 
129 #endif /* QEMU_AIO_WAIT */
130