]>
Commit | Line | Data |
---|---|---|
d0e2fce5 AK |
1 | /* |
2 | * GThread coroutine initialization code | |
3 | * | |
4 | * Copyright (C) 2006 Anthony Liguori <[email protected]> | |
5 | * Copyright (C) 2011 Aneesh Kumar K.V <[email protected]> | |
6 | * | |
7 | * This library is free software; you can redistribute it and/or | |
8 | * modify it under the terms of the GNU Lesser General Public | |
9 | * License as published by the Free Software Foundation; either | |
10 | * version 2.0 of the License, or (at your option) any later version. | |
11 | * | |
12 | * This library is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * Lesser General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU Lesser General Public | |
18 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. | |
19 | */ | |
20 | ||
21 | #include <glib.h> | |
22 | #include "qemu-common.h" | |
737e150e | 23 | #include "block/coroutine_int.h" |
d0e2fce5 AK |
24 | |
25 | typedef struct { | |
26 | Coroutine base; | |
27 | GThread *thread; | |
28 | bool runnable; | |
d1b719e9 | 29 | bool free_on_thread_exit; |
d0e2fce5 AK |
30 | CoroutineAction action; |
31 | } CoroutineGThread; | |
32 | ||
d0e2fce5 | 33 | static GStaticMutex coroutine_lock = G_STATIC_MUTEX_INIT; |
d1b719e9 PM |
34 | |
35 | /* GLib 2.31 and beyond deprecated various parts of the thread API, | |
36 | * but the new interfaces are not available in older GLib versions | |
37 | * so we have to cope with both. | |
38 | */ | |
39 | #if GLIB_CHECK_VERSION(2, 31, 0) | |
40 | /* Default zero-initialisation is sufficient for 2.31+ GCond */ | |
41 | static GCond the_coroutine_cond; | |
42 | static GCond *coroutine_cond = &the_coroutine_cond; | |
43 | static inline void init_coroutine_cond(void) | |
44 | { | |
45 | } | |
46 | ||
47 | /* Awkwardly, the GPrivate API doesn't provide a way to update the | |
48 | * GDestroyNotify handler for the coroutine key dynamically. So instead | |
49 | * we track whether or not the CoroutineGThread should be freed on | |
50 | * thread exit / coroutine key update using the free_on_thread_exit | |
51 | * field. | |
52 | */ | |
53 | static void coroutine_destroy_notify(gpointer data) | |
54 | { | |
55 | CoroutineGThread *co = data; | |
56 | if (co && co->free_on_thread_exit) { | |
57 | g_free(co); | |
58 | } | |
59 | } | |
60 | ||
61 | static GPrivate coroutine_key = G_PRIVATE_INIT(coroutine_destroy_notify); | |
62 | ||
63 | static inline CoroutineGThread *get_coroutine_key(void) | |
64 | { | |
65 | return g_private_get(&coroutine_key); | |
66 | } | |
67 | ||
68 | static inline void set_coroutine_key(CoroutineGThread *co, | |
69 | bool free_on_thread_exit) | |
70 | { | |
71 | /* Unlike g_static_private_set() this does not call the GDestroyNotify | |
72 | * if the previous value of the key was NULL. Fortunately we only need | |
73 | * the GDestroyNotify in the non-NULL key case. | |
74 | */ | |
75 | co->free_on_thread_exit = free_on_thread_exit; | |
76 | g_private_replace(&coroutine_key, co); | |
77 | } | |
78 | ||
79 | static inline GThread *create_thread(GThreadFunc func, gpointer data) | |
80 | { | |
81 | return g_thread_new("coroutine", func, data); | |
82 | } | |
83 | ||
84 | #else | |
85 | ||
86 | /* Handle older GLib versions */ | |
87 | static GCond *coroutine_cond; | |
88 | static inline void init_coroutine_cond(void) | |
89 | { | |
90 | coroutine_cond = g_cond_new(); | |
91 | } | |
92 | ||
d0e2fce5 AK |
93 | static GStaticPrivate coroutine_key = G_STATIC_PRIVATE_INIT; |
94 | ||
d1b719e9 PM |
95 | static inline CoroutineGThread *get_coroutine_key(void) |
96 | { | |
97 | return g_static_private_get(&coroutine_key); | |
98 | } | |
99 | ||
100 | static inline void set_coroutine_key(CoroutineGThread *co, | |
101 | bool free_on_thread_exit) | |
102 | { | |
103 | g_static_private_set(&coroutine_key, co, | |
104 | free_on_thread_exit ? (GDestroyNotify)g_free : NULL); | |
105 | } | |
106 | ||
107 | static inline GThread *create_thread(GThreadFunc func, gpointer data) | |
108 | { | |
109 | return g_thread_create_full(func, data, 0, TRUE, TRUE, | |
110 | G_THREAD_PRIORITY_NORMAL, NULL); | |
111 | } | |
112 | ||
113 | #endif | |
114 | ||
115 | ||
d0e2fce5 AK |
116 | static void __attribute__((constructor)) coroutine_init(void) |
117 | { | |
42ed3727 | 118 | #if !GLIB_CHECK_VERSION(2, 31, 0) |
f33cc84d | 119 | if (!g_thread_supported()) { |
d0e2fce5 AK |
120 | g_thread_init(NULL); |
121 | } | |
f33cc84d | 122 | #endif |
d0e2fce5 | 123 | |
d1b719e9 | 124 | init_coroutine_cond(); |
d0e2fce5 AK |
125 | } |
126 | ||
127 | static void coroutine_wait_runnable_locked(CoroutineGThread *co) | |
128 | { | |
129 | while (!co->runnable) { | |
130 | g_cond_wait(coroutine_cond, g_static_mutex_get_mutex(&coroutine_lock)); | |
131 | } | |
132 | } | |
133 | ||
134 | static void coroutine_wait_runnable(CoroutineGThread *co) | |
135 | { | |
136 | g_static_mutex_lock(&coroutine_lock); | |
137 | coroutine_wait_runnable_locked(co); | |
138 | g_static_mutex_unlock(&coroutine_lock); | |
139 | } | |
140 | ||
141 | static gpointer coroutine_thread(gpointer opaque) | |
142 | { | |
143 | CoroutineGThread *co = opaque; | |
144 | ||
d1b719e9 | 145 | set_coroutine_key(co, false); |
d0e2fce5 AK |
146 | coroutine_wait_runnable(co); |
147 | co->base.entry(co->base.entry_arg); | |
148 | qemu_coroutine_switch(&co->base, co->base.caller, COROUTINE_TERMINATE); | |
149 | return NULL; | |
150 | } | |
151 | ||
152 | Coroutine *qemu_coroutine_new(void) | |
153 | { | |
154 | CoroutineGThread *co; | |
155 | ||
7267c094 | 156 | co = g_malloc0(sizeof(*co)); |
d1b719e9 | 157 | co->thread = create_thread(coroutine_thread, co); |
d0e2fce5 | 158 | if (!co->thread) { |
7267c094 | 159 | g_free(co); |
d0e2fce5 AK |
160 | return NULL; |
161 | } | |
162 | return &co->base; | |
163 | } | |
164 | ||
165 | void qemu_coroutine_delete(Coroutine *co_) | |
166 | { | |
167 | CoroutineGThread *co = DO_UPCAST(CoroutineGThread, base, co_); | |
168 | ||
169 | g_thread_join(co->thread); | |
7267c094 | 170 | g_free(co); |
d0e2fce5 AK |
171 | } |
172 | ||
173 | CoroutineAction qemu_coroutine_switch(Coroutine *from_, | |
174 | Coroutine *to_, | |
175 | CoroutineAction action) | |
176 | { | |
177 | CoroutineGThread *from = DO_UPCAST(CoroutineGThread, base, from_); | |
178 | CoroutineGThread *to = DO_UPCAST(CoroutineGThread, base, to_); | |
179 | ||
180 | g_static_mutex_lock(&coroutine_lock); | |
181 | from->runnable = false; | |
182 | from->action = action; | |
183 | to->runnable = true; | |
184 | to->action = action; | |
185 | g_cond_broadcast(coroutine_cond); | |
186 | ||
187 | if (action != COROUTINE_TERMINATE) { | |
188 | coroutine_wait_runnable_locked(from); | |
189 | } | |
190 | g_static_mutex_unlock(&coroutine_lock); | |
191 | return from->action; | |
192 | } | |
193 | ||
194 | Coroutine *qemu_coroutine_self(void) | |
195 | { | |
d1b719e9 | 196 | CoroutineGThread *co = get_coroutine_key(); |
d0e2fce5 | 197 | if (!co) { |
7267c094 | 198 | co = g_malloc0(sizeof(*co)); |
d0e2fce5 | 199 | co->runnable = true; |
d1b719e9 | 200 | set_coroutine_key(co, true); |
d0e2fce5 AK |
201 | } |
202 | ||
203 | return &co->base; | |
204 | } | |
205 | ||
206 | bool qemu_in_coroutine(void) | |
207 | { | |
d1b719e9 | 208 | CoroutineGThread *co = get_coroutine_key(); |
d0e2fce5 AK |
209 | |
210 | return co && co->base.caller; | |
211 | } |