4 * Copyright (c) 2015 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
23 #include "qapi/error.h"
24 #include "qemu/thread.h"
27 struct QIOTaskThreadData
{
30 GDestroyNotify destroy
;
31 GMainContext
*context
;
40 GDestroyNotify destroy
;
43 GDestroyNotify destroyResult
;
44 QemuMutex thread_lock
;
46 struct QIOTaskThreadData
*thread
;
50 QIOTask
*qio_task_new(Object
*source
,
53 GDestroyNotify destroy
)
57 task
= g_new0(QIOTask
, 1);
59 task
->source
= source
;
62 task
->opaque
= opaque
;
63 task
->destroy
= destroy
;
64 qemu_mutex_init(&task
->thread_lock
);
65 qemu_cond_init(&task
->thread_cond
);
67 trace_qio_task_new(task
, source
, func
, opaque
);
72 static void qio_task_free(QIOTask
*task
)
74 qemu_mutex_lock(&task
->thread_lock
);
76 if (task
->thread
->destroy
) {
77 task
->thread
->destroy(task
->thread
->opaque
);
80 if (task
->thread
->context
) {
81 g_main_context_unref(task
->thread
->context
);
88 task
->destroy(task
->opaque
);
90 if (task
->destroyResult
) {
91 task
->destroyResult(task
->result
);
94 error_free(task
->err
);
96 object_unref(task
->source
);
98 qemu_mutex_unlock(&task
->thread_lock
);
99 qemu_mutex_destroy(&task
->thread_lock
);
100 qemu_cond_destroy(&task
->thread_cond
);
106 static gboolean
qio_task_thread_result(gpointer opaque
)
108 QIOTask
*task
= opaque
;
110 trace_qio_task_thread_result(task
);
111 qio_task_complete(task
);
117 static gpointer
qio_task_thread_worker(gpointer opaque
)
119 QIOTask
*task
= opaque
;
121 trace_qio_task_thread_run(task
);
123 task
->thread
->worker(task
, task
->thread
->opaque
);
125 /* We're running in the background thread, and must only
126 * ever report the task results in the main event loop
127 * thread. So we schedule an idle callback to report
130 trace_qio_task_thread_exit(task
);
132 qemu_mutex_lock(&task
->thread_lock
);
134 task
->thread
->completion
= g_idle_source_new();
135 g_source_set_callback(task
->thread
->completion
,
136 qio_task_thread_result
, task
, NULL
);
137 g_source_attach(task
->thread
->completion
,
138 task
->thread
->context
);
139 trace_qio_task_thread_source_attach(task
, task
->thread
->completion
);
141 qemu_cond_signal(&task
->thread_cond
);
142 qemu_mutex_unlock(&task
->thread_lock
);
148 void qio_task_run_in_thread(QIOTask
*task
,
149 QIOTaskWorker worker
,
151 GDestroyNotify destroy
,
152 GMainContext
*context
)
154 struct QIOTaskThreadData
*data
= g_new0(struct QIOTaskThreadData
, 1);
158 g_main_context_ref(context
);
161 data
->worker
= worker
;
162 data
->opaque
= opaque
;
163 data
->destroy
= destroy
;
164 data
->context
= context
;
168 trace_qio_task_thread_start(task
, worker
, opaque
);
169 qemu_thread_create(&thread
,
171 qio_task_thread_worker
,
173 QEMU_THREAD_DETACHED
);
177 void qio_task_wait_thread(QIOTask
*task
)
179 qemu_mutex_lock(&task
->thread_lock
);
180 g_assert(task
->thread
!= NULL
);
181 while (task
->thread
->completion
== NULL
) {
182 qemu_cond_wait(&task
->thread_cond
, &task
->thread_lock
);
185 trace_qio_task_thread_source_cancel(task
, task
->thread
->completion
);
186 g_source_destroy(task
->thread
->completion
);
187 qemu_mutex_unlock(&task
->thread_lock
);
189 qio_task_thread_result(task
);
193 void qio_task_complete(QIOTask
*task
)
195 task
->func(task
, task
->opaque
);
196 trace_qio_task_complete(task
);
201 void qio_task_set_error(QIOTask
*task
,
204 error_propagate(&task
->err
, err
);
208 bool qio_task_propagate_error(QIOTask
*task
,
212 error_propagate(errp
, task
->err
);
221 void qio_task_set_result_pointer(QIOTask
*task
,
223 GDestroyNotify destroy
)
225 task
->result
= result
;
226 task
->destroyResult
= destroy
;
230 gpointer
qio_task_get_result_pointer(QIOTask
*task
)
236 Object
*qio_task_get_source(QIOTask
*task
)