@@ -295,6 +295,18 @@ void qemu_mutex_lock_iothread_impl(const char *file, int line);
*/
void qemu_mutex_unlock_iothread(void);
+/**
+ * qemu_idle_add: Add an idle function to a GMainContext
+ *
+ * This function is similar to GLib's g_idle_add() but it allows
+ * specifying a GMainContext instead of using the global one.
+ *
+ * The returned GSource is owned by the GMainContext and is deleted
+ * automatically after @func returns false. It can also be deleted by
+ * removing it from the GMainContext using g_source_destroy().
+ */
+GSource *qemu_idle_add(GSourceFunc func, gpointer data, GMainContext *ctx);
+
/* internal interfaces */
void qemu_fd_register(int fd);
@@ -19,6 +19,7 @@
*/
#include "qemu/osdep.h"
+#include "qemu/main-loop.h"
#include "io/task.h"
#include "qapi/error.h"
#include "qemu/thread.h"
@@ -130,13 +131,8 @@ static gpointer qio_task_thread_worker(gpointer opaque)
trace_qio_task_thread_exit(task);
qemu_mutex_lock(&task->thread_lock);
-
- task->thread->completion = g_idle_source_new();
- g_source_set_callback(task->thread->completion,
- qio_task_thread_result, task, NULL);
- g_source_attach(task->thread->completion,
- task->thread->context);
- g_source_unref(task->thread->completion);
+ task->thread->completion = qemu_idle_add(qio_task_thread_result, task,
+ task->thread->context);
trace_qio_task_thread_source_attach(task, task->thread->completion);
qemu_cond_signal(&task->thread_cond);
@@ -171,6 +171,15 @@ int qemu_init_main_loop(Error **errp)
return 0;
}
+GSource *qemu_idle_add(GSourceFunc func, gpointer data, GMainContext *ctx)
+{
+ GSource *idle = g_idle_source_new();
+ g_source_set_callback(idle, func, data, NULL);
+ g_source_attach(idle, ctx);
+ g_source_unref(idle);
+ return idle;
+}
+
static int max_priority;
#ifndef _WIN32