1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include <glib.h>
22#include "qemu-common.h"
23#include "qemu/coroutine_int.h"
24
25typedef struct {
26 Coroutine base;
27 GThread *thread;
28 bool runnable;
29 bool free_on_thread_exit;
30 CoroutineAction action;
31} CoroutineGThread;
32
33static CompatGMutex coroutine_lock;
34static CompatGCond coroutine_cond;
35
36
37
38
39
40#if GLIB_CHECK_VERSION(2, 31, 0)
41
42
43
44
45
46
47static void coroutine_destroy_notify(gpointer data)
48{
49 CoroutineGThread *co = data;
50 if (co && co->free_on_thread_exit) {
51 g_free(co);
52 }
53}
54
55static GPrivate coroutine_key = G_PRIVATE_INIT(coroutine_destroy_notify);
56
57static inline CoroutineGThread *get_coroutine_key(void)
58{
59 return g_private_get(&coroutine_key);
60}
61
62static inline void set_coroutine_key(CoroutineGThread *co,
63 bool free_on_thread_exit)
64{
65
66
67
68
69 co->free_on_thread_exit = free_on_thread_exit;
70 g_private_replace(&coroutine_key, co);
71}
72
73static inline GThread *create_thread(GThreadFunc func, gpointer data)
74{
75 return g_thread_new("coroutine", func, data);
76}
77
78#else
79
80
81
82static GStaticPrivate coroutine_key = G_STATIC_PRIVATE_INIT;
83
84static inline CoroutineGThread *get_coroutine_key(void)
85{
86 return g_static_private_get(&coroutine_key);
87}
88
89static inline void set_coroutine_key(CoroutineGThread *co,
90 bool free_on_thread_exit)
91{
92 g_static_private_set(&coroutine_key, co,
93 free_on_thread_exit ? (GDestroyNotify)g_free : NULL);
94}
95
96static inline GThread *create_thread(GThreadFunc func, gpointer data)
97{
98 return g_thread_create_full(func, data, 0, TRUE, TRUE,
99 G_THREAD_PRIORITY_NORMAL, NULL);
100}
101
102#endif
103
104
105static void __attribute__((constructor)) coroutine_init(void)
106{
107#if !GLIB_CHECK_VERSION(2, 31, 0)
108 if (!g_thread_supported()) {
109 g_thread_init(NULL);
110 }
111#endif
112}
113
114static void coroutine_wait_runnable_locked(CoroutineGThread *co)
115{
116 while (!co->runnable) {
117 g_cond_wait(&coroutine_cond, &coroutine_lock);
118 }
119}
120
121static void coroutine_wait_runnable(CoroutineGThread *co)
122{
123 g_mutex_lock(&coroutine_lock);
124 coroutine_wait_runnable_locked(co);
125 g_mutex_unlock(&coroutine_lock);
126}
127
128static gpointer coroutine_thread(gpointer opaque)
129{
130 CoroutineGThread *co = opaque;
131
132 set_coroutine_key(co, false);
133 coroutine_wait_runnable(co);
134 co->base.entry(co->base.entry_arg);
135 qemu_coroutine_switch(&co->base, co->base.caller, COROUTINE_TERMINATE);
136 return NULL;
137}
138
139Coroutine *qemu_coroutine_new(void)
140{
141 CoroutineGThread *co;
142
143 co = g_malloc0(sizeof(*co));
144 co->thread = create_thread(coroutine_thread, co);
145 if (!co->thread) {
146 g_free(co);
147 return NULL;
148 }
149 return &co->base;
150}
151
152void qemu_coroutine_delete(Coroutine *co_)
153{
154 CoroutineGThread *co = DO_UPCAST(CoroutineGThread, base, co_);
155
156 g_thread_join(co->thread);
157 g_free(co);
158}
159
160CoroutineAction qemu_coroutine_switch(Coroutine *from_,
161 Coroutine *to_,
162 CoroutineAction action)
163{
164 CoroutineGThread *from = DO_UPCAST(CoroutineGThread, base, from_);
165 CoroutineGThread *to = DO_UPCAST(CoroutineGThread, base, to_);
166
167 g_mutex_lock(&coroutine_lock);
168 from->runnable = false;
169 from->action = action;
170 to->runnable = true;
171 to->action = action;
172 g_cond_broadcast(&coroutine_cond);
173
174 if (action != COROUTINE_TERMINATE) {
175 coroutine_wait_runnable_locked(from);
176 }
177 g_mutex_unlock(&coroutine_lock);
178 return from->action;
179}
180
181Coroutine *qemu_coroutine_self(void)
182{
183 CoroutineGThread *co = get_coroutine_key();
184 if (!co) {
185 co = g_malloc0(sizeof(*co));
186 co->runnable = true;
187 set_coroutine_key(co, true);
188 }
189
190 return &co->base;
191}
192
193bool qemu_in_coroutine(void)
194{
195 CoroutineGThread *co = get_coroutine_key();
196
197 return co && co->base.caller;
198}
199