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
19 #include "qemu-queue.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.
65 Coroutine
*qemu_coroutine_create(CoroutineEntry
*entry
);
68 * Transfer control to a coroutine
70 * The opaque argument is passed as the argument to the entry point when
71 * entering the coroutine for the first time. It is subsequently ignored.
73 void qemu_coroutine_enter(Coroutine
*coroutine
, void *opaque
);
76 * Transfer control back to a coroutine's caller
78 * This function does not return until the coroutine is re-entered using
79 * qemu_coroutine_enter().
81 void coroutine_fn
qemu_coroutine_yield(void);
84 * Get the currently executing coroutine
86 Coroutine
*coroutine_fn
qemu_coroutine_self(void);
89 * Return whether or not currently inside a coroutine
91 * This can be used to write functions that work both when in coroutine context
92 * and when not in coroutine context. Note that such functions cannot use the
93 * coroutine_fn annotation since they work outside coroutine context.
95 bool qemu_in_coroutine(void);
100 * CoQueues are a mechanism to queue coroutines in order to continue executing
101 * them later. They provide the fundamental primitives on which coroutine locks
104 typedef struct CoQueue
{
105 QTAILQ_HEAD(, Coroutine
) entries
;
109 * Initialise a CoQueue. This must be called before any other operation is used
112 void qemu_co_queue_init(CoQueue
*queue
);
115 * Adds the current coroutine to the CoQueue and transfers control to the
116 * caller of the coroutine.
118 void coroutine_fn
qemu_co_queue_wait(CoQueue
*queue
);
121 * Adds the current coroutine to the head of the CoQueue and transfers control to the
122 * caller of the coroutine.
124 void coroutine_fn
qemu_co_queue_wait_insert_head(CoQueue
*queue
);
127 * Restarts the next coroutine in the CoQueue and removes it from the queue.
129 * Returns true if a coroutine was restarted, false if the queue is empty.
131 bool qemu_co_queue_next(CoQueue
*queue
);
134 * Checks if the CoQueue is empty.
136 bool qemu_co_queue_empty(CoQueue
*queue
);
140 * Provides a mutex that can be used to synchronise coroutines
142 typedef struct CoMutex
{
148 * Initialises a CoMutex. This must be called before any other operation is used
151 void qemu_co_mutex_init(CoMutex
*mutex
);
154 * Locks the mutex. If the lock cannot be taken immediately, control is
155 * transferred to the caller of the current coroutine.
157 void coroutine_fn
qemu_co_mutex_lock(CoMutex
*mutex
);
160 * Unlocks the mutex and schedules the next coroutine that was waiting for this
163 void coroutine_fn
qemu_co_mutex_unlock(CoMutex
*mutex
);
165 typedef struct CoRwlock
{
172 * Initialises a CoRwlock. This must be called before any other operation
173 * is used on the CoRwlock
175 void qemu_co_rwlock_init(CoRwlock
*lock
);
178 * Read locks the CoRwlock. If the lock cannot be taken immediately because
179 * of a parallel writer, control is transferred to the caller of the current
182 void qemu_co_rwlock_rdlock(CoRwlock
*lock
);
185 * Write Locks the mutex. If the lock cannot be taken immediately because
186 * of a parallel reader, control is transferred to the caller of the current
189 void qemu_co_rwlock_wrlock(CoRwlock
*lock
);
192 * Unlocks the read/write lock and schedules the next coroutine that was
193 * waiting for this lock to be run.
195 void qemu_co_rwlock_unlock(CoRwlock
*lock
);
197 #endif /* QEMU_COROUTINE_H */