]>
Commit | Line | Data |
---|---|---|
7719f3c9 SH |
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" | |
3c18a92d | 29 | #include "qemu/main-loop.h" |
7719f3c9 SH |
30 | |
31 | /** | |
32 | * AioWait: | |
33 | * | |
cfe29d82 KW |
34 | * An object that facilitates synchronous waiting on a condition. A single |
35 | * global AioWait object (global_aio_wait) is used internally. | |
36 | * | |
37 | * The main loop can wait on an operation running in an IOThread as follows: | |
7719f3c9 | 38 | * |
7719f3c9 SH |
39 | * AioContext *ctx = ...; |
40 | * MyWork work = { .done = false }; | |
41 | * schedule_my_work_in_iothread(ctx, &work); | |
cfe29d82 | 42 | * AIO_WAIT_WHILE(ctx, !work.done); |
7719f3c9 SH |
43 | * |
44 | * The IOThread must call aio_wait_kick() to notify the main loop when | |
45 | * work.done changes: | |
46 | * | |
47 | * static void do_work(...) | |
48 | * { | |
49 | * ... | |
50 | * work.done = true; | |
cfe29d82 | 51 | * aio_wait_kick(); |
7719f3c9 SH |
52 | * } |
53 | */ | |
54 | typedef struct { | |
7376eda7 SH |
55 | /* Number of waiting AIO_WAIT_WHILE() callers. Accessed with atomic ops. */ |
56 | unsigned num_waiters; | |
7719f3c9 SH |
57 | } AioWait; |
58 | ||
cfe29d82 KW |
59 | extern AioWait global_aio_wait; |
60 | ||
7719f3c9 SH |
61 | /** |
62 | * AIO_WAIT_WHILE: | |
4d22bbf4 KW |
63 | * @ctx: the aio context, or NULL if multiple aio contexts (for which the |
64 | * caller does not hold a lock) are involved in the polling condition. | |
7719f3c9 SH |
65 | * @cond: wait while this conditional expression is true |
66 | * | |
67 | * Wait while a condition is true. Use this to implement synchronous | |
68 | * operations that require event loop activity. | |
69 | * | |
70 | * The caller must be sure that something calls aio_wait_kick() when the value | |
71 | * of @cond might have changed. | |
72 | * | |
73 | * The caller's thread must be the IOThread that owns @ctx or the main loop | |
74 | * thread (with @ctx acquired exactly once). This function cannot be used to | |
75 | * wait on conditions between two IOThreads since that could lead to deadlock, | |
76 | * go via the main loop instead. | |
77 | */ | |
cfe29d82 | 78 | #define AIO_WAIT_WHILE(ctx, cond) ({ \ |
7376eda7 | 79 | bool waited_ = false; \ |
cfe29d82 | 80 | AioWait *wait_ = &global_aio_wait; \ |
7376eda7 | 81 | AioContext *ctx_ = (ctx); \ |
48657448 | 82 | /* Increment wait_->num_waiters before evaluating cond. */ \ |
d73415a3 | 83 | qatomic_inc(&wait_->num_waiters); \ |
4d22bbf4 | 84 | if (ctx_ && in_aio_context_home_thread(ctx_)) { \ |
1cc8e54a KW |
85 | while ((cond)) { \ |
86 | aio_poll(ctx_, true); \ | |
87 | waited_ = true; \ | |
7376eda7 SH |
88 | } \ |
89 | } else { \ | |
90 | assert(qemu_get_current_aio_context() == \ | |
91 | qemu_get_aio_context()); \ | |
1cc8e54a | 92 | while ((cond)) { \ |
4d22bbf4 KW |
93 | if (ctx_) { \ |
94 | aio_context_release(ctx_); \ | |
95 | } \ | |
1cc8e54a | 96 | aio_poll(qemu_get_aio_context(), true); \ |
4d22bbf4 KW |
97 | if (ctx_) { \ |
98 | aio_context_acquire(ctx_); \ | |
99 | } \ | |
1cc8e54a | 100 | waited_ = true; \ |
7376eda7 | 101 | } \ |
7376eda7 | 102 | } \ |
d73415a3 | 103 | qatomic_dec(&wait_->num_waiters); \ |
7719f3c9 SH |
104 | waited_; }) |
105 | ||
106 | /** | |
107 | * aio_wait_kick: | |
7719f3c9 SH |
108 | * Wake up the main thread if it is waiting on AIO_WAIT_WHILE(). During |
109 | * synchronous operations performed in an IOThread, the main thread lets the | |
110 | * IOThread's event loop run, waiting for the operation to complete. A | |
111 | * aio_wait_kick() call will wake up the main thread. | |
112 | */ | |
cfe29d82 | 113 | void aio_wait_kick(void); |
7719f3c9 | 114 | |
b89d92f3 SH |
115 | /** |
116 | * aio_wait_bh_oneshot: | |
117 | * @ctx: the aio context | |
118 | * @cb: the BH callback function | |
119 | * @opaque: user data for the BH callback function | |
120 | * | |
121 | * Run a BH in @ctx and wait for it to complete. | |
122 | * | |
123 | * Must be called from the main loop thread with @ctx acquired exactly once. | |
124 | * Note that main loop event processing may occur. | |
125 | */ | |
126 | void aio_wait_bh_oneshot(AioContext *ctx, QEMUBHFunc *cb, void *opaque); | |
127 | ||
3c18a92d PB |
128 | /** |
129 | * in_aio_context_home_thread: | |
130 | * @ctx: the aio context | |
131 | * | |
132 | * Return whether we are running in the thread that normally runs @ctx. Note | |
133 | * that acquiring/releasing ctx does not affect the outcome, each AioContext | |
134 | * still only has one home thread that is responsible for running it. | |
135 | */ | |
136 | static inline bool in_aio_context_home_thread(AioContext *ctx) | |
137 | { | |
138 | if (ctx == qemu_get_current_aio_context()) { | |
139 | return true; | |
140 | } | |
141 | ||
142 | if (ctx == qemu_get_aio_context()) { | |
143 | return qemu_mutex_iothread_locked(); | |
144 | } else { | |
145 | return false; | |
146 | } | |
147 | } | |
148 | ||
6834c3f4 | 149 | #endif /* QEMU_AIO_WAIT_H */ |