1/* 2 * QEMU coroutine implementation 3 * 4 * Copyright IBM, Corp. 2011 5 * 6 * Authors: 7 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com> 8 * Kevin Wolf <kwolf@redhat.com> 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 18#include "qemu/coroutine-core.h" 19#include "qemu/atomic.h" 20#include "qemu/queue.h" 21#include "qemu/timer.h" 22 23/** 24 * Coroutines are a mechanism for stack switching and can be used for 25 * cooperative userspace threading. These functions provide a simple but 26 * useful flavor of coroutines that is suitable for writing sequential code, 27 * rather than callbacks, for operations that need to give up control while 28 * waiting for events to complete. 29 * 30 * These functions are re-entrant and may be used outside the BQL. 31 * 32 * Functions that execute in coroutine context cannot be called 33 * directly from normal functions. Use @coroutine_fn to mark such 34 * functions. For example: 35 * 36 * static void coroutine_fn foo(void) { 37 * .... 38 * } 39 * 40 * In the future it would be nice to have the compiler or a static 41 * checker catch misuse of such functions. This annotation might make 42 * it possible and in the meantime it serves as documentation. 43 */ 44 45/** 46 * Provides a mutex that can be used to synchronise coroutines 47 */ 48struct CoWaitRecord; 49struct CoMutex { 50 /* Count of pending lockers; 0 for a free mutex, 1 for an 51 * uncontended mutex. 52 */ 53 unsigned locked; 54 55 /* Context that is holding the lock. Useful to avoid spinning 56 * when two coroutines on the same AioContext try to get the lock. :) 57 */ 58 AioContext *ctx; 59 60 /* A queue of waiters. Elements are added atomically in front of 61 * from_push. to_pop is only populated, and popped from, by whoever 62 * is in charge of the next wakeup. This can be an unlocker or, 63 * through the handoff protocol, a locker that is about to go to sleep. 64 */ 65 QSLIST_HEAD(, CoWaitRecord) from_push, to_pop; 66 67 unsigned handoff, sequence; 68 69 Coroutine *holder; 70}; 71 72/** 73 * Assert that the current coroutine holds @mutex. 74 */ 75static inline coroutine_fn void qemu_co_mutex_assert_locked(CoMutex *mutex) 76{ 77 /* 78 * mutex->holder doesn't need any synchronisation if the assertion holds 79 * true because the mutex protects it. If it doesn't hold true, we still 80 * don't mind if another thread takes or releases mutex behind our back, 81 * because the condition will be false no matter whether we read NULL or 82 * the pointer for any other coroutine. 83 */ 84 assert(qatomic_read(&mutex->locked) && 85 mutex->holder == qemu_coroutine_self()); 86} 87 88#include "qemu/lockable.h" 89 90/** 91 * CoQueues are a mechanism to queue coroutines in order to continue executing 92 * them later. They are similar to condition variables, but they need help 93 * from an external mutex in order to maintain thread-safety. 94 */ 95typedef struct CoQueue { 96 QSIMPLEQ_HEAD(, Coroutine) entries; 97} CoQueue; 98 99/** 100 * Initialise a CoQueue. This must be called before any other operation is used 101 * on the CoQueue. 102 */ 103void qemu_co_queue_init(CoQueue *queue); 104 105typedef enum { 106 /* 107 * Enqueue at front instead of back. Use this to re-queue a request when 108 * its wait condition is not satisfied after being woken up. 109 */ 110 CO_QUEUE_WAIT_FRONT = 0x1, 111} CoQueueWaitFlags; 112 113/** 114 * Adds the current coroutine to the CoQueue and transfers control to the 115 * caller of the coroutine. The mutex is unlocked during the wait and 116 * locked again afterwards. 117 */ 118#define qemu_co_queue_wait(queue, lock) \ 119 qemu_co_queue_wait_impl(queue, QEMU_MAKE_LOCKABLE(lock), 0) 120#define qemu_co_queue_wait_flags(queue, lock, flags) \ 121 qemu_co_queue_wait_impl(queue, QEMU_MAKE_LOCKABLE(lock), (flags)) 122void coroutine_fn qemu_co_queue_wait_impl(CoQueue *queue, QemuLockable *lock, 123 CoQueueWaitFlags flags); 124 125/** 126 * Removes the next coroutine from the CoQueue, and queue it to run after 127 * the currently-running coroutine yields. 128 * Returns true if a coroutine was removed, false if the queue is empty. 129 * Used from coroutine context, use qemu_co_enter_next outside. 130 */ 131bool coroutine_fn qemu_co_queue_next(CoQueue *queue); 132 133/** 134 * Empties the CoQueue and queues the coroutine to run after 135 * the currently-running coroutine yields. 136 * Used from coroutine context, use qemu_co_enter_all outside. 137 */ 138void coroutine_fn qemu_co_queue_restart_all(CoQueue *queue); 139 140/** 141 * Removes the next coroutine from the CoQueue, and wake it up. Unlike 142 * qemu_co_queue_next, this function releases the lock during aio_co_wake 143 * because it is meant to be used outside coroutine context; in that case, the 144 * coroutine is entered immediately, before qemu_co_enter_next returns. 145 * 146 * If used in coroutine context, qemu_co_enter_next is equivalent to 147 * qemu_co_queue_next. 148 */ 149#define qemu_co_enter_next(queue, lock) \ 150 qemu_co_enter_next_impl(queue, QEMU_MAKE_LOCKABLE(lock)) 151bool qemu_co_enter_next_impl(CoQueue *queue, QemuLockable *lock); 152 153/** 154 * Empties the CoQueue, waking the waiting coroutine one at a time. Unlike 155 * qemu_co_queue_all, this function releases the lock during aio_co_wake 156 * because it is meant to be used outside coroutine context; in that case, the 157 * coroutine is entered immediately, before qemu_co_enter_all returns. 158 * 159 * If used in coroutine context, qemu_co_enter_all is equivalent to 160 * qemu_co_queue_all. 161 */ 162#define qemu_co_enter_all(queue, lock) \ 163 qemu_co_enter_all_impl(queue, QEMU_MAKE_LOCKABLE(lock)) 164void qemu_co_enter_all_impl(CoQueue *queue, QemuLockable *lock); 165 166/** 167 * Checks if the CoQueue is empty. 168 */ 169bool qemu_co_queue_empty(CoQueue *queue); 170 171 172typedef struct CoRwTicket CoRwTicket; 173typedef struct CoRwlock { 174 CoMutex mutex; 175 176 /* Number of readers, or -1 if owned for writing. */ 177 int owners; 178 179 /* Waiting coroutines. */ 180 QSIMPLEQ_HEAD(, CoRwTicket) tickets; 181} CoRwlock; 182 183/** 184 * Initialises a CoRwlock. This must be called before any other operation 185 * is used on the CoRwlock 186 */ 187void qemu_co_rwlock_init(CoRwlock *lock); 188 189/** 190 * Read locks the CoRwlock. If the lock cannot be taken immediately because 191 * of a parallel writer, control is transferred to the caller of the current 192 * coroutine. 193 */ 194void coroutine_fn qemu_co_rwlock_rdlock(CoRwlock *lock); 195 196/** 197 * Write Locks the CoRwlock from a reader. This is a bit more efficient than 198 * @qemu_co_rwlock_unlock followed by a separate @qemu_co_rwlock_wrlock. 199 * Note that if the lock cannot be upgraded immediately, control is transferred 200 * to the caller of the current coroutine; another writer might run while 201 * @qemu_co_rwlock_upgrade blocks. 202 */ 203void coroutine_fn qemu_co_rwlock_upgrade(CoRwlock *lock); 204 205/** 206 * Downgrades a write-side critical section to a reader. Downgrading with 207 * @qemu_co_rwlock_downgrade never blocks, unlike @qemu_co_rwlock_unlock 208 * followed by @qemu_co_rwlock_rdlock. This makes it more efficient, but 209 * may also sometimes be necessary for correctness. 210 */ 211void coroutine_fn qemu_co_rwlock_downgrade(CoRwlock *lock); 212 213/** 214 * Write Locks the mutex. If the lock cannot be taken immediately because 215 * of a parallel reader, control is transferred to the caller of the current 216 * coroutine. 217 */ 218void coroutine_fn qemu_co_rwlock_wrlock(CoRwlock *lock); 219 220/** 221 * Unlocks the read/write lock and schedules the next coroutine that was 222 * waiting for this lock to be run. 223 */ 224void coroutine_fn qemu_co_rwlock_unlock(CoRwlock *lock); 225 226typedef struct QemuCoSleep { 227 Coroutine *to_wake; 228} QemuCoSleep; 229 230/** 231 * Yield the coroutine for a given duration. Initializes @w so that, 232 * during this yield, it can be passed to qemu_co_sleep_wake() to 233 * terminate the sleep. 234 */ 235void coroutine_fn qemu_co_sleep_ns_wakeable(QemuCoSleep *w, 236 QEMUClockType type, int64_t ns); 237 238/** 239 * Yield the coroutine until the next call to qemu_co_sleep_wake. 240 */ 241void coroutine_fn qemu_co_sleep(QemuCoSleep *w); 242 243static inline void coroutine_fn qemu_co_sleep_ns(QEMUClockType type, int64_t ns) 244{ 245 QemuCoSleep w = { 0 }; 246 qemu_co_sleep_ns_wakeable(&w, type, ns); 247} 248 249typedef void CleanupFunc(void *opaque); 250/** 251 * Run entry in a coroutine and start timer. Wait for entry to finish or for 252 * timer to elapse, what happen first. If entry finished, return 0, if timer 253 * elapsed earlier, return -ETIMEDOUT. 254 * 255 * Be careful, entry execution is not canceled, user should handle it somehow. 256 * If @clean is provided, it's called after coroutine finish if timeout 257 * happened. 258 */ 259int coroutine_fn qemu_co_timeout(CoroutineEntry *entry, void *opaque, 260 uint64_t timeout_ns, CleanupFunc clean); 261 262/** 263 * Wake a coroutine if it is sleeping in qemu_co_sleep_ns. The timer will be 264 * deleted. @sleep_state must be the variable whose address was given to 265 * qemu_co_sleep_ns() and should be checked to be non-NULL before calling 266 * qemu_co_sleep_wake(). 267 */ 268void qemu_co_sleep_wake(QemuCoSleep *w); 269 270/** 271 * Yield until a file descriptor becomes readable 272 * 273 * Note that this function clobbers the handlers for the file descriptor. 274 */ 275void coroutine_fn yield_until_fd_readable(int fd); 276 277/** 278 * Increase coroutine pool size 279 */ 280void qemu_coroutine_inc_pool_size(unsigned int additional_pool_size); 281 282/** 283 * Decrease coroutine pool size 284 */ 285void qemu_coroutine_dec_pool_size(unsigned int additional_pool_size); 286 287/** 288 * Sends a (part of) iovec down a socket, yielding when the socket is full, or 289 * Receives data into a (part of) iovec from a socket, 290 * yielding when there is no data in the socket. 291 * The same interface as qemu_sendv_recvv(), with added yielding. 292 * XXX should mark these as coroutine_fn 293 */ 294ssize_t coroutine_fn qemu_co_sendv_recvv(int sockfd, struct iovec *iov, 295 unsigned iov_cnt, size_t offset, 296 size_t bytes, bool do_send); 297#define qemu_co_recvv(sockfd, iov, iov_cnt, offset, bytes) \ 298 qemu_co_sendv_recvv(sockfd, iov, iov_cnt, offset, bytes, false) 299#define qemu_co_sendv(sockfd, iov, iov_cnt, offset, bytes) \ 300 qemu_co_sendv_recvv(sockfd, iov, iov_cnt, offset, bytes, true) 301 302/** 303 * The same as above, but with just a single buffer 304 */ 305ssize_t coroutine_fn qemu_co_send_recv(int sockfd, void *buf, size_t bytes, 306 bool do_send); 307#define qemu_co_recv(sockfd, buf, bytes) \ 308 qemu_co_send_recv(sockfd, buf, bytes, false) 309#define qemu_co_send(sockfd, buf, bytes) \ 310 qemu_co_send_recv(sockfd, buf, bytes, true) 311 312#endif /* QEMU_COROUTINE_H */ 313

