mirror of
https://github.com/Motorhead1991/qemu.git
synced 2025-08-03 07:43:54 -06:00
glib-compat.h: add new thread API emulation on top of pre-2.31 API
Thread API changed in glib-2.31 significantly. Before that version, conditionals and mutexes were only allocated dynamically, using _new()/_free() interface. in 2.31 and up, they're allocated statically as regular variables, and old interface is deprecated. (Note: glib docs says the new interface is available since version 2.32, but it was actually introduced in version 2.31). Create the new interface using old primitives, by providing non-opaque definitions of the base types (GCond and GMutex) using GOnces. Replace #ifdeffery around GCond and GMutex in trace/simple.c and coroutine-gthread.c too because it does not work anymore with the new glib-compat.h. Signed-off-by: Michael Tokarev <mjt@tls.msk.ru> Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com> [Use GOnce to support lazy initialization; introduce CompatGMutex and CompatGCond. - Paolo] Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
parent
7721a30442
commit
86946a2d83
3 changed files with 138 additions and 60 deletions
|
@ -30,20 +30,14 @@ typedef struct {
|
|||
CoroutineAction action;
|
||||
} CoroutineGThread;
|
||||
|
||||
static GStaticMutex coroutine_lock = G_STATIC_MUTEX_INIT;
|
||||
static CompatGMutex coroutine_lock;
|
||||
static CompatGCond coroutine_cond;
|
||||
|
||||
/* GLib 2.31 and beyond deprecated various parts of the thread API,
|
||||
* but the new interfaces are not available in older GLib versions
|
||||
* so we have to cope with both.
|
||||
*/
|
||||
#if GLIB_CHECK_VERSION(2, 31, 0)
|
||||
/* Default zero-initialisation is sufficient for 2.31+ GCond */
|
||||
static GCond the_coroutine_cond;
|
||||
static GCond *coroutine_cond = &the_coroutine_cond;
|
||||
static inline void init_coroutine_cond(void)
|
||||
{
|
||||
}
|
||||
|
||||
/* Awkwardly, the GPrivate API doesn't provide a way to update the
|
||||
* GDestroyNotify handler for the coroutine key dynamically. So instead
|
||||
* we track whether or not the CoroutineGThread should be freed on
|
||||
|
@ -84,11 +78,6 @@ static inline GThread *create_thread(GThreadFunc func, gpointer data)
|
|||
#else
|
||||
|
||||
/* Handle older GLib versions */
|
||||
static GCond *coroutine_cond;
|
||||
static inline void init_coroutine_cond(void)
|
||||
{
|
||||
coroutine_cond = g_cond_new();
|
||||
}
|
||||
|
||||
static GStaticPrivate coroutine_key = G_STATIC_PRIVATE_INIT;
|
||||
|
||||
|
@ -120,22 +109,20 @@ static void __attribute__((constructor)) coroutine_init(void)
|
|||
g_thread_init(NULL);
|
||||
}
|
||||
#endif
|
||||
|
||||
init_coroutine_cond();
|
||||
}
|
||||
|
||||
static void coroutine_wait_runnable_locked(CoroutineGThread *co)
|
||||
{
|
||||
while (!co->runnable) {
|
||||
g_cond_wait(coroutine_cond, g_static_mutex_get_mutex(&coroutine_lock));
|
||||
g_cond_wait(&coroutine_cond, &coroutine_lock);
|
||||
}
|
||||
}
|
||||
|
||||
static void coroutine_wait_runnable(CoroutineGThread *co)
|
||||
{
|
||||
g_static_mutex_lock(&coroutine_lock);
|
||||
g_mutex_lock(&coroutine_lock);
|
||||
coroutine_wait_runnable_locked(co);
|
||||
g_static_mutex_unlock(&coroutine_lock);
|
||||
g_mutex_unlock(&coroutine_lock);
|
||||
}
|
||||
|
||||
static gpointer coroutine_thread(gpointer opaque)
|
||||
|
@ -177,17 +164,17 @@ CoroutineAction qemu_coroutine_switch(Coroutine *from_,
|
|||
CoroutineGThread *from = DO_UPCAST(CoroutineGThread, base, from_);
|
||||
CoroutineGThread *to = DO_UPCAST(CoroutineGThread, base, to_);
|
||||
|
||||
g_static_mutex_lock(&coroutine_lock);
|
||||
g_mutex_lock(&coroutine_lock);
|
||||
from->runnable = false;
|
||||
from->action = action;
|
||||
to->runnable = true;
|
||||
to->action = action;
|
||||
g_cond_broadcast(coroutine_cond);
|
||||
g_cond_broadcast(&coroutine_cond);
|
||||
|
||||
if (action != COROUTINE_TERMINATE) {
|
||||
coroutine_wait_runnable_locked(from);
|
||||
}
|
||||
g_static_mutex_unlock(&coroutine_lock);
|
||||
g_mutex_unlock(&coroutine_lock);
|
||||
return from->action;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue