]>
git.proxmox.com Git - mirror_qemu.git/blob - include/qemu/coroutine.h
2 * QEMU coroutine implementation
4 * Copyright IBM, Corp. 2011
7 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
8 * Kevin Wolf <kwolf@redhat.com>
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.
15 #ifndef QEMU_COROUTINE_H
16 #define QEMU_COROUTINE_H
18 #include "qemu/queue.h"
19 #include "qemu/timer.h"
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.
28 * These functions are re-entrant and may be used outside the global mutex.
32 * Mark a function that executes in coroutine context
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.
41 * static void coroutine_fn foo(void) {
47 typedef struct Coroutine Coroutine
;
50 * Coroutine entry point
52 * When the coroutine is entered for the first time, opaque is passed in as an
55 * When this function returns, the coroutine is destroyed automatically and
56 * execution continues in the caller who last entered the coroutine.
58 typedef void coroutine_fn
CoroutineEntry(void *opaque
);
61 * Create a new coroutine
63 * Use qemu_coroutine_enter() to actually transfer control to the coroutine.
64 * The opaque argument is passed as the argument to the entry point.
66 Coroutine
*qemu_coroutine_create(CoroutineEntry
*entry
, void *opaque
);
69 * Transfer control to a coroutine
71 void qemu_coroutine_enter(Coroutine
*coroutine
);
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.
77 void qemu_coroutine_enter_if_inactive(Coroutine
*co
);
80 * Transfer control back to a coroutine's caller
82 * This function does not return until the coroutine is re-entered using
83 * qemu_coroutine_enter().
85 void coroutine_fn
qemu_coroutine_yield(void);
88 * Get the currently executing coroutine
90 Coroutine
*coroutine_fn
qemu_coroutine_self(void);
93 * Return whether or not currently inside a coroutine
95 * This can be used to write functions that work both when in coroutine context
96 * and when not in coroutine context. Note that such functions cannot use the
97 * coroutine_fn annotation since they work outside coroutine context.
99 bool qemu_in_coroutine(void);
102 * Return true if the coroutine is currently entered
104 * A coroutine is "entered" if it has not yielded from the current
105 * qemu_coroutine_enter() call used to run it. This does not mean that the
106 * coroutine is currently executing code since it may have transferred control
107 * to another coroutine using qemu_coroutine_enter().
109 * When several coroutines enter each other there may be no way to know which
110 * ones have already been entered. In such situations this function can be
111 * used to avoid recursively entering coroutines.
113 bool qemu_coroutine_entered(Coroutine
*co
);
117 * CoQueues are a mechanism to queue coroutines in order to continue executing
118 * them later. They provide the fundamental primitives on which coroutine locks
121 typedef struct CoQueue
{
122 QSIMPLEQ_HEAD(, Coroutine
) entries
;
126 * Initialise a CoQueue. This must be called before any other operation is used
129 void qemu_co_queue_init(CoQueue
*queue
);
132 * Adds the current coroutine to the CoQueue and transfers control to the
133 * caller of the coroutine.
135 void coroutine_fn
qemu_co_queue_wait(CoQueue
*queue
);
138 * Restarts the next coroutine in the CoQueue and removes it from the queue.
140 * Returns true if a coroutine was restarted, false if the queue is empty.
142 bool coroutine_fn
qemu_co_queue_next(CoQueue
*queue
);
145 * Restarts all coroutines in the CoQueue and leaves the queue empty.
147 void coroutine_fn
qemu_co_queue_restart_all(CoQueue
*queue
);
150 * Enter the next coroutine in the queue
152 bool qemu_co_enter_next(CoQueue
*queue
);
155 * Checks if the CoQueue is empty.
157 bool qemu_co_queue_empty(CoQueue
*queue
);
161 * Provides a mutex that can be used to synchronise coroutines
164 typedef struct CoMutex
{
165 /* Count of pending lockers; 0 for a free mutex, 1 for an
170 /* Context that is holding the lock. Useful to avoid spinning
171 * when two coroutines on the same AioContext try to get the lock. :)
175 /* A queue of waiters. Elements are added atomically in front of
176 * from_push. to_pop is only populated, and popped from, by whoever
177 * is in charge of the next wakeup. This can be an unlocker or,
178 * through the handoff protocol, a locker that is about to go to sleep.
180 QSLIST_HEAD(, CoWaitRecord
) from_push
, to_pop
;
182 unsigned handoff
, sequence
;
188 * Initialises a CoMutex. This must be called before any other operation is used
191 void qemu_co_mutex_init(CoMutex
*mutex
);
194 * Locks the mutex. If the lock cannot be taken immediately, control is
195 * transferred to the caller of the current coroutine.
197 void coroutine_fn
qemu_co_mutex_lock(CoMutex
*mutex
);
200 * Unlocks the mutex and schedules the next coroutine that was waiting for this
203 void coroutine_fn
qemu_co_mutex_unlock(CoMutex
*mutex
);
205 typedef struct CoRwlock
{
212 * Initialises a CoRwlock. This must be called before any other operation
213 * is used on the CoRwlock
215 void qemu_co_rwlock_init(CoRwlock
*lock
);
218 * Read locks the CoRwlock. If the lock cannot be taken immediately because
219 * of a parallel writer, control is transferred to the caller of the current
222 void qemu_co_rwlock_rdlock(CoRwlock
*lock
);
225 * Write Locks the mutex. If the lock cannot be taken immediately because
226 * of a parallel reader, control is transferred to the caller of the current
229 void qemu_co_rwlock_wrlock(CoRwlock
*lock
);
232 * Unlocks the read/write lock and schedules the next coroutine that was
233 * waiting for this lock to be run.
235 void qemu_co_rwlock_unlock(CoRwlock
*lock
);
238 * Yield the coroutine for a given duration
240 * Behaves similarly to co_sleep_ns(), but the sleeping coroutine will be
241 * resumed when using aio_poll().
243 void coroutine_fn
co_aio_sleep_ns(AioContext
*ctx
, QEMUClockType type
,
247 * Yield until a file descriptor becomes readable
249 * Note that this function clobbers the handlers for the file descriptor.
251 void coroutine_fn
yield_until_fd_readable(int fd
);
253 #endif /* QEMU_COROUTINE_H */