]>
Commit | Line | Data |
---|---|---|
00dccaf1 KW |
1 | /* |
2 | * QEMU coroutine implementation | |
3 | * | |
4 | * Copyright IBM, Corp. 2011 | |
5 | * | |
6 | * Authors: | |
7 | * Stefan Hajnoczi <[email protected]> | |
b96e9247 | 8 | * Kevin Wolf <[email protected]> |
00dccaf1 KW |
9 | * |
10 | * This work is licensed under the terms of the GNU LGPL, version 2 or later. | |
11 | * See the COPYING.LIB file in the top-level directory. | |
12 | * | |
13 | */ | |
14 | ||
15 | #ifndef QEMU_COROUTINE_H | |
16 | #define QEMU_COROUTINE_H | |
17 | ||
1de7afc9 PB |
18 | #include "qemu/queue.h" |
19 | #include "qemu/timer.h" | |
00dccaf1 KW |
20 | |
21 | /** | |
22 | * Coroutines are a mechanism for stack switching and can be used for | |
23 | * cooperative userspace threading. These functions provide a simple but | |
24 | * useful flavor of coroutines that is suitable for writing sequential code, | |
25 | * rather than callbacks, for operations that need to give up control while | |
26 | * waiting for events to complete. | |
27 | * | |
28 | * These functions are re-entrant and may be used outside the global mutex. | |
29 | */ | |
30 | ||
31 | /** | |
32 | * Mark a function that executes in coroutine context | |
33 | * | |
34 | * Functions that execute in coroutine context cannot be called directly from | |
35 | * normal functions. In the future it would be nice to enable compiler or | |
36 | * static checker support for catching such errors. This annotation might make | |
37 | * it possible and in the meantime it serves as documentation. | |
38 | * | |
39 | * For example: | |
40 | * | |
41 | * static void coroutine_fn foo(void) { | |
42 | * .... | |
43 | * } | |
44 | */ | |
45 | #define coroutine_fn | |
46 | ||
47 | typedef struct Coroutine Coroutine; | |
48 | ||
49 | /** | |
50 | * Coroutine entry point | |
51 | * | |
52 | * When the coroutine is entered for the first time, opaque is passed in as an | |
53 | * argument. | |
54 | * | |
55 | * When this function returns, the coroutine is destroyed automatically and | |
56 | * execution continues in the caller who last entered the coroutine. | |
57 | */ | |
58 | typedef void coroutine_fn CoroutineEntry(void *opaque); | |
59 | ||
60 | /** | |
61 | * Create a new coroutine | |
62 | * | |
63 | * Use qemu_coroutine_enter() to actually transfer control to the coroutine. | |
0b8b8753 | 64 | * The opaque argument is passed as the argument to the entry point. |
00dccaf1 | 65 | */ |
0b8b8753 | 66 | Coroutine *qemu_coroutine_create(CoroutineEntry *entry, void *opaque); |
00dccaf1 KW |
67 | |
68 | /** | |
69 | * Transfer control to a coroutine | |
00dccaf1 | 70 | */ |
0b8b8753 | 71 | void qemu_coroutine_enter(Coroutine *coroutine); |
00dccaf1 | 72 | |
536fca7f KW |
73 | /** |
74 | * Transfer control to a coroutine if it's not active (i.e. part of the call | |
75 | * stack of the running coroutine). Otherwise, do nothing. | |
76 | */ | |
77 | void qemu_coroutine_enter_if_inactive(Coroutine *co); | |
78 | ||
ba9e75ce FZ |
79 | /** |
80 | * Transfer control to a coroutine and associate it with ctx | |
81 | */ | |
82 | void qemu_aio_coroutine_enter(AioContext *ctx, Coroutine *co); | |
83 | ||
00dccaf1 KW |
84 | /** |
85 | * Transfer control back to a coroutine's caller | |
86 | * | |
87 | * This function does not return until the coroutine is re-entered using | |
88 | * qemu_coroutine_enter(). | |
89 | */ | |
90 | void coroutine_fn qemu_coroutine_yield(void); | |
91 | ||
aa1361d5 KW |
92 | /** |
93 | * Get the AioContext of the given coroutine | |
94 | */ | |
95 | AioContext *coroutine_fn qemu_coroutine_get_aio_context(Coroutine *co); | |
96 | ||
00dccaf1 KW |
97 | /** |
98 | * Get the currently executing coroutine | |
99 | */ | |
100 | Coroutine *coroutine_fn qemu_coroutine_self(void); | |
101 | ||
102 | /** | |
103 | * Return whether or not currently inside a coroutine | |
104 | * | |
105 | * This can be used to write functions that work both when in coroutine context | |
106 | * and when not in coroutine context. Note that such functions cannot use the | |
107 | * coroutine_fn annotation since they work outside coroutine context. | |
108 | */ | |
109 | bool qemu_in_coroutine(void); | |
110 | ||
f643e469 SH |
111 | /** |
112 | * Return true if the coroutine is currently entered | |
113 | * | |
114 | * A coroutine is "entered" if it has not yielded from the current | |
115 | * qemu_coroutine_enter() call used to run it. This does not mean that the | |
116 | * coroutine is currently executing code since it may have transferred control | |
117 | * to another coroutine using qemu_coroutine_enter(). | |
118 | * | |
119 | * When several coroutines enter each other there may be no way to know which | |
120 | * ones have already been entered. In such situations this function can be | |
121 | * used to avoid recursively entering coroutines. | |
122 | */ | |
123 | bool qemu_coroutine_entered(Coroutine *co); | |
b96e9247 | 124 | |
b96e9247 KW |
125 | /** |
126 | * Provides a mutex that can be used to synchronise coroutines | |
127 | */ | |
fed20a70 | 128 | struct CoWaitRecord; |
e70372fc | 129 | struct CoMutex { |
fed20a70 PB |
130 | /* Count of pending lockers; 0 for a free mutex, 1 for an |
131 | * uncontended mutex. | |
132 | */ | |
133 | unsigned locked; | |
134 | ||
480cff63 PB |
135 | /* Context that is holding the lock. Useful to avoid spinning |
136 | * when two coroutines on the same AioContext try to get the lock. :) | |
137 | */ | |
138 | AioContext *ctx; | |
139 | ||
fed20a70 PB |
140 | /* A queue of waiters. Elements are added atomically in front of |
141 | * from_push. to_pop is only populated, and popped from, by whoever | |
142 | * is in charge of the next wakeup. This can be an unlocker or, | |
143 | * through the handoff protocol, a locker that is about to go to sleep. | |
144 | */ | |
145 | QSLIST_HEAD(, CoWaitRecord) from_push, to_pop; | |
146 | ||
147 | unsigned handoff, sequence; | |
148 | ||
0e438cdc | 149 | Coroutine *holder; |
e70372fc | 150 | }; |
b96e9247 KW |
151 | |
152 | /** | |
153 | * Initialises a CoMutex. This must be called before any other operation is used | |
154 | * on the CoMutex. | |
155 | */ | |
156 | void qemu_co_mutex_init(CoMutex *mutex); | |
157 | ||
158 | /** | |
159 | * Locks the mutex. If the lock cannot be taken immediately, control is | |
160 | * transferred to the caller of the current coroutine. | |
161 | */ | |
162 | void coroutine_fn qemu_co_mutex_lock(CoMutex *mutex); | |
163 | ||
164 | /** | |
165 | * Unlocks the mutex and schedules the next coroutine that was waiting for this | |
166 | * lock to be run. | |
167 | */ | |
168 | void coroutine_fn qemu_co_mutex_unlock(CoMutex *mutex); | |
169 | ||
944f3d5d KW |
170 | /** |
171 | * Assert that the current coroutine holds @mutex. | |
172 | */ | |
173 | static inline coroutine_fn void qemu_co_mutex_assert_locked(CoMutex *mutex) | |
174 | { | |
175 | /* | |
176 | * mutex->holder doesn't need any synchronisation if the assertion holds | |
177 | * true because the mutex protects it. If it doesn't hold true, we still | |
178 | * don't mind if another thread takes or releases mutex behind our back, | |
179 | * because the condition will be false no matter whether we read NULL or | |
180 | * the pointer for any other coroutine. | |
181 | */ | |
d73415a3 | 182 | assert(qatomic_read(&mutex->locked) && |
944f3d5d KW |
183 | mutex->holder == qemu_coroutine_self()); |
184 | } | |
f8c6e1cb PB |
185 | |
186 | /** | |
187 | * CoQueues are a mechanism to queue coroutines in order to continue executing | |
1ace7cea PB |
188 | * them later. They are similar to condition variables, but they need help |
189 | * from an external mutex in order to maintain thread-safety. | |
f8c6e1cb PB |
190 | */ |
191 | typedef struct CoQueue { | |
192 | QSIMPLEQ_HEAD(, Coroutine) entries; | |
193 | } CoQueue; | |
194 | ||
195 | /** | |
196 | * Initialise a CoQueue. This must be called before any other operation is used | |
197 | * on the CoQueue. | |
198 | */ | |
199 | void qemu_co_queue_init(CoQueue *queue); | |
200 | ||
201 | /** | |
202 | * Adds the current coroutine to the CoQueue and transfers control to the | |
1ace7cea PB |
203 | * caller of the coroutine. The mutex is unlocked during the wait and |
204 | * locked again afterwards. | |
f8c6e1cb | 205 | */ |
1a957cf9 PB |
206 | #define qemu_co_queue_wait(queue, lock) \ |
207 | qemu_co_queue_wait_impl(queue, QEMU_MAKE_LOCKABLE(lock)) | |
208 | void coroutine_fn qemu_co_queue_wait_impl(CoQueue *queue, QemuLockable *lock); | |
f8c6e1cb PB |
209 | |
210 | /** | |
5261dd7b PB |
211 | * Removes the next coroutine from the CoQueue, and wake it up. |
212 | * Returns true if a coroutine was removed, false if the queue is empty. | |
0e70260b | 213 | * OK to run from coroutine and non-coroutine context. |
f8c6e1cb | 214 | */ |
0e70260b | 215 | bool qemu_co_queue_next(CoQueue *queue); |
f8c6e1cb PB |
216 | |
217 | /** | |
5261dd7b | 218 | * Empties the CoQueue; all coroutines are woken up. |
0e70260b | 219 | * OK to run from coroutine and non-coroutine context. |
f8c6e1cb | 220 | */ |
0e70260b | 221 | void qemu_co_queue_restart_all(CoQueue *queue); |
f8c6e1cb PB |
222 | |
223 | /** | |
5261dd7b PB |
224 | * Removes the next coroutine from the CoQueue, and wake it up. Unlike |
225 | * qemu_co_queue_next, this function releases the lock during aio_co_wake | |
226 | * because it is meant to be used outside coroutine context; in that case, the | |
227 | * coroutine is entered immediately, before qemu_co_enter_next returns. | |
228 | * | |
229 | * If used in coroutine context, qemu_co_enter_next is equivalent to | |
230 | * qemu_co_queue_next. | |
f8c6e1cb | 231 | */ |
5261dd7b PB |
232 | #define qemu_co_enter_next(queue, lock) \ |
233 | qemu_co_enter_next_impl(queue, QEMU_MAKE_LOCKABLE(lock)) | |
234 | bool qemu_co_enter_next_impl(CoQueue *queue, QemuLockable *lock); | |
f8c6e1cb PB |
235 | |
236 | /** | |
237 | * Checks if the CoQueue is empty. | |
238 | */ | |
239 | bool qemu_co_queue_empty(CoQueue *queue); | |
240 | ||
241 | ||
050de36b | 242 | typedef struct CoRwTicket CoRwTicket; |
12888904 | 243 | typedef struct CoRwlock { |
a7b91d35 | 244 | CoMutex mutex; |
050de36b PB |
245 | |
246 | /* Number of readers, or -1 if owned for writing. */ | |
247 | int owners; | |
248 | ||
249 | /* Waiting coroutines. */ | |
250 | QSIMPLEQ_HEAD(, CoRwTicket) tickets; | |
12888904 AK |
251 | } CoRwlock; |
252 | ||
253 | /** | |
254 | * Initialises a CoRwlock. This must be called before any other operation | |
255 | * is used on the CoRwlock | |
256 | */ | |
257 | void qemu_co_rwlock_init(CoRwlock *lock); | |
258 | ||
259 | /** | |
260 | * Read locks the CoRwlock. If the lock cannot be taken immediately because | |
261 | * of a parallel writer, control is transferred to the caller of the current | |
262 | * coroutine. | |
263 | */ | |
264 | void qemu_co_rwlock_rdlock(CoRwlock *lock); | |
265 | ||
667221c1 PB |
266 | /** |
267 | * Write Locks the CoRwlock from a reader. This is a bit more efficient than | |
268 | * @qemu_co_rwlock_unlock followed by a separate @qemu_co_rwlock_wrlock. | |
050de36b PB |
269 | * Note that if the lock cannot be upgraded immediately, control is transferred |
270 | * to the caller of the current coroutine; another writer might run while | |
271 | * @qemu_co_rwlock_upgrade blocks. | |
667221c1 PB |
272 | */ |
273 | void qemu_co_rwlock_upgrade(CoRwlock *lock); | |
274 | ||
275 | /** | |
276 | * Downgrades a write-side critical section to a reader. Downgrading with | |
277 | * @qemu_co_rwlock_downgrade never blocks, unlike @qemu_co_rwlock_unlock | |
278 | * followed by @qemu_co_rwlock_rdlock. This makes it more efficient, but | |
279 | * may also sometimes be necessary for correctness. | |
280 | */ | |
281 | void qemu_co_rwlock_downgrade(CoRwlock *lock); | |
282 | ||
12888904 AK |
283 | /** |
284 | * Write Locks the mutex. If the lock cannot be taken immediately because | |
285 | * of a parallel reader, control is transferred to the caller of the current | |
286 | * coroutine. | |
287 | */ | |
288 | void qemu_co_rwlock_wrlock(CoRwlock *lock); | |
289 | ||
290 | /** | |
291 | * Unlocks the read/write lock and schedules the next coroutine that was | |
292 | * waiting for this lock to be run. | |
293 | */ | |
294 | void qemu_co_rwlock_unlock(CoRwlock *lock); | |
295 | ||
29a6ea24 PB |
296 | typedef struct QemuCoSleep { |
297 | Coroutine *to_wake; | |
298 | } QemuCoSleep; | |
3d692649 VSO |
299 | |
300 | /** | |
29a6ea24 PB |
301 | * Yield the coroutine for a given duration. Initializes @w so that, |
302 | * during this yield, it can be passed to qemu_co_sleep_wake() to | |
303 | * terminate the sleep. | |
3d692649 | 304 | */ |
29a6ea24 PB |
305 | void coroutine_fn qemu_co_sleep_ns_wakeable(QemuCoSleep *w, |
306 | QEMUClockType type, int64_t ns); | |
307 | ||
0a6f0c76 PB |
308 | /** |
309 | * Yield the coroutine until the next call to qemu_co_sleep_wake. | |
310 | */ | |
311 | void coroutine_fn qemu_co_sleep(QemuCoSleep *w); | |
312 | ||
3d692649 VSO |
313 | static inline void coroutine_fn qemu_co_sleep_ns(QEMUClockType type, int64_t ns) |
314 | { | |
29a6ea24 PB |
315 | QemuCoSleep w = { 0 }; |
316 | qemu_co_sleep_ns_wakeable(&w, type, ns); | |
3d692649 VSO |
317 | } |
318 | ||
3ab7bd19 | 319 | /** |
3d692649 VSO |
320 | * Wake a coroutine if it is sleeping in qemu_co_sleep_ns. The timer will be |
321 | * deleted. @sleep_state must be the variable whose address was given to | |
322 | * qemu_co_sleep_ns() and should be checked to be non-NULL before calling | |
323 | * qemu_co_sleep_wake(). | |
3ab7bd19 | 324 | */ |
29a6ea24 | 325 | void qemu_co_sleep_wake(QemuCoSleep *w); |
3ab7bd19 | 326 | |
9f05d0c3 MH |
327 | /** |
328 | * Yield until a file descriptor becomes readable | |
329 | * | |
330 | * Note that this function clobbers the handlers for the file descriptor. | |
331 | */ | |
332 | void coroutine_fn yield_until_fd_readable(int fd); | |
ac2662a9 | 333 | |
4c41c69e HN |
334 | /** |
335 | * Increase coroutine pool size | |
336 | */ | |
337 | void qemu_coroutine_increase_pool_batch_size(unsigned int additional_pool_size); | |
338 | ||
339 | /** | |
340 | * Devcrease coroutine pool size | |
341 | */ | |
342 | void qemu_coroutine_decrease_pool_batch_size(unsigned int additional_pool_size); | |
343 | ||
1a957cf9 PB |
344 | #include "qemu/lockable.h" |
345 | ||
c097f1e6 MAL |
346 | /** |
347 | * Sends a (part of) iovec down a socket, yielding when the socket is full, or | |
348 | * Receives data into a (part of) iovec from a socket, | |
349 | * yielding when there is no data in the socket. | |
350 | * The same interface as qemu_sendv_recvv(), with added yielding. | |
351 | * XXX should mark these as coroutine_fn | |
352 | */ | |
353 | ssize_t qemu_co_sendv_recvv(int sockfd, struct iovec *iov, unsigned iov_cnt, | |
354 | size_t offset, size_t bytes, bool do_send); | |
355 | #define qemu_co_recvv(sockfd, iov, iov_cnt, offset, bytes) \ | |
356 | qemu_co_sendv_recvv(sockfd, iov, iov_cnt, offset, bytes, false) | |
357 | #define qemu_co_sendv(sockfd, iov, iov_cnt, offset, bytes) \ | |
358 | qemu_co_sendv_recvv(sockfd, iov, iov_cnt, offset, bytes, true) | |
359 | ||
360 | /** | |
361 | * The same as above, but with just a single buffer | |
362 | */ | |
363 | ssize_t qemu_co_send_recv(int sockfd, void *buf, size_t bytes, bool do_send); | |
364 | #define qemu_co_recv(sockfd, buf, bytes) \ | |
365 | qemu_co_send_recv(sockfd, buf, bytes, false) | |
366 | #define qemu_co_send(sockfd, buf, bytes) \ | |
367 | qemu_co_send_recv(sockfd, buf, bytes, true) | |
368 | ||
00dccaf1 | 369 | #endif /* QEMU_COROUTINE_H */ |