xref: /qemu/include/qemu/coroutine.h (revision 2c533c54)
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/queue.h"
19 #include "qemu/timer.h"
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.
64  * The opaque argument is passed as the argument to the entry point.
65  */
66 Coroutine *qemu_coroutine_create(CoroutineEntry *entry, void *opaque);
67 
68 /**
69  * Transfer control to a coroutine
70  */
71 void qemu_coroutine_enter(Coroutine *coroutine);
72 
73 /**
74  * Transfer control back to a coroutine's caller
75  *
76  * This function does not return until the coroutine is re-entered using
77  * qemu_coroutine_enter().
78  */
79 void coroutine_fn qemu_coroutine_yield(void);
80 
81 /**
82  * Get the currently executing coroutine
83  */
84 Coroutine *coroutine_fn qemu_coroutine_self(void);
85 
86 /**
87  * Return whether or not currently inside a coroutine
88  *
89  * This can be used to write functions that work both when in coroutine context
90  * and when not in coroutine context.  Note that such functions cannot use the
91  * coroutine_fn annotation since they work outside coroutine context.
92  */
93 bool qemu_in_coroutine(void);
94 
95 
96 
97 /**
98  * CoQueues are a mechanism to queue coroutines in order to continue executing
99  * them later. They provide the fundamental primitives on which coroutine locks
100  * are built.
101  */
102 typedef struct CoQueue {
103     QSIMPLEQ_HEAD(, Coroutine) entries;
104 } CoQueue;
105 
106 /**
107  * Initialise a CoQueue. This must be called before any other operation is used
108  * on the CoQueue.
109  */
110 void qemu_co_queue_init(CoQueue *queue);
111 
112 /**
113  * Adds the current coroutine to the CoQueue and transfers control to the
114  * caller of the coroutine.
115  */
116 void coroutine_fn qemu_co_queue_wait(CoQueue *queue);
117 
118 /**
119  * Restarts the next coroutine in the CoQueue and removes it from the queue.
120  *
121  * Returns true if a coroutine was restarted, false if the queue is empty.
122  */
123 bool coroutine_fn qemu_co_queue_next(CoQueue *queue);
124 
125 /**
126  * Restarts all coroutines in the CoQueue and leaves the queue empty.
127  */
128 void coroutine_fn qemu_co_queue_restart_all(CoQueue *queue);
129 
130 /**
131  * Enter the next coroutine in the queue
132  */
133 bool qemu_co_enter_next(CoQueue *queue);
134 
135 /**
136  * Checks if the CoQueue is empty.
137  */
138 bool qemu_co_queue_empty(CoQueue *queue);
139 
140 
141 /**
142  * Provides a mutex that can be used to synchronise coroutines
143  */
144 typedef struct CoMutex {
145     bool locked;
146     CoQueue queue;
147 } CoMutex;
148 
149 /**
150  * Initialises a CoMutex. This must be called before any other operation is used
151  * on the CoMutex.
152  */
153 void qemu_co_mutex_init(CoMutex *mutex);
154 
155 /**
156  * Locks the mutex. If the lock cannot be taken immediately, control is
157  * transferred to the caller of the current coroutine.
158  */
159 void coroutine_fn qemu_co_mutex_lock(CoMutex *mutex);
160 
161 /**
162  * Unlocks the mutex and schedules the next coroutine that was waiting for this
163  * lock to be run.
164  */
165 void coroutine_fn qemu_co_mutex_unlock(CoMutex *mutex);
166 
167 typedef struct CoRwlock {
168     bool writer;
169     int reader;
170     CoQueue queue;
171 } CoRwlock;
172 
173 /**
174  * Initialises a CoRwlock. This must be called before any other operation
175  * is used on the CoRwlock
176  */
177 void qemu_co_rwlock_init(CoRwlock *lock);
178 
179 /**
180  * Read locks the CoRwlock. If the lock cannot be taken immediately because
181  * of a parallel writer, control is transferred to the caller of the current
182  * coroutine.
183  */
184 void qemu_co_rwlock_rdlock(CoRwlock *lock);
185 
186 /**
187  * Write Locks the mutex. If the lock cannot be taken immediately because
188  * of a parallel reader, control is transferred to the caller of the current
189  * coroutine.
190  */
191 void qemu_co_rwlock_wrlock(CoRwlock *lock);
192 
193 /**
194  * Unlocks the read/write lock and schedules the next coroutine that was
195  * waiting for this lock to be run.
196  */
197 void qemu_co_rwlock_unlock(CoRwlock *lock);
198 
199 /**
200  * Yield the coroutine for a given duration
201  *
202  * Behaves similarly to co_sleep_ns(), but the sleeping coroutine will be
203  * resumed when using aio_poll().
204  */
205 void coroutine_fn co_aio_sleep_ns(AioContext *ctx, QEMUClockType type,
206                                   int64_t ns);
207 
208 /**
209  * Yield until a file descriptor becomes readable
210  *
211  * Note that this function clobbers the handlers for the file descriptor.
212  */
213 void coroutine_fn yield_until_fd_readable(int fd);
214 
215 #endif /* QEMU_COROUTINE_H */
216