blob: 1ae7b864881a41669d4a3a924c418ed6f9107de1 [file] [log] [blame]
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +00001/*
2 * QEMU I/O task
3 *
4 * Copyright (c) 2015 Red Hat, Inc.
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 */
20
Peter Maydellcae9fc52016-01-29 17:50:03 +000021#include "qemu/osdep.h"
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +000022#include "io/task.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010023#include "qapi/error.h"
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +000024#include "qemu/thread.h"
25#include "trace.h"
26
Daniel P. Berrangé52d6cfe2019-02-11 18:24:27 +000027struct QIOTaskThreadData {
28 QIOTaskWorker worker;
29 gpointer opaque;
30 GDestroyNotify destroy;
31 GMainContext *context;
Daniel P. Berrangédbb44502019-02-11 18:24:28 +000032 GSource *completion;
Daniel P. Berrangé52d6cfe2019-02-11 18:24:27 +000033};
34
35
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +000036struct QIOTask {
37 Object *source;
38 QIOTaskFunc func;
39 gpointer opaque;
40 GDestroyNotify destroy;
Daniel P. Berrange1a447e42016-08-11 14:40:44 +010041 Error *err;
Daniel P. Berrange52dd99e2016-08-11 14:36:21 +010042 gpointer result;
43 GDestroyNotify destroyResult;
Daniel P. Berrangédbb44502019-02-11 18:24:28 +000044 QemuMutex thread_lock;
45 QemuCond thread_cond;
Daniel P. Berrangé52d6cfe2019-02-11 18:24:27 +000046 struct QIOTaskThreadData *thread;
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +000047};
48
49
50QIOTask *qio_task_new(Object *source,
51 QIOTaskFunc func,
52 gpointer opaque,
53 GDestroyNotify destroy)
54{
55 QIOTask *task;
56
57 task = g_new0(QIOTask, 1);
58
59 task->source = source;
60 object_ref(source);
61 task->func = func;
62 task->opaque = opaque;
63 task->destroy = destroy;
Daniel P. Berrangédbb44502019-02-11 18:24:28 +000064 qemu_mutex_init(&task->thread_lock);
65 qemu_cond_init(&task->thread_cond);
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +000066
67 trace_qio_task_new(task, source, func, opaque);
68
69 return task;
70}
71
72static void qio_task_free(QIOTask *task)
73{
Daniel P. Berrangédbb44502019-02-11 18:24:28 +000074 qemu_mutex_lock(&task->thread_lock);
Daniel P. Berrangé52d6cfe2019-02-11 18:24:27 +000075 if (task->thread) {
76 if (task->thread->destroy) {
77 task->thread->destroy(task->thread->opaque);
78 }
79
80 if (task->thread->context) {
81 g_main_context_unref(task->thread->context);
82 }
83
84 g_free(task->thread);
85 }
86
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +000087 if (task->destroy) {
88 task->destroy(task->opaque);
89 }
Daniel P. Berrange52dd99e2016-08-11 14:36:21 +010090 if (task->destroyResult) {
91 task->destroyResult(task->result);
92 }
Daniel P. Berrange1a447e42016-08-11 14:40:44 +010093 if (task->err) {
94 error_free(task->err);
95 }
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +000096 object_unref(task->source);
97
Daniel P. Berrangédbb44502019-02-11 18:24:28 +000098 qemu_mutex_unlock(&task->thread_lock);
99 qemu_mutex_destroy(&task->thread_lock);
100 qemu_cond_destroy(&task->thread_cond);
101
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +0000102 g_free(task);
103}
104
105
Peter Xu7c287682018-03-05 14:43:19 +0800106static gboolean qio_task_thread_result(gpointer opaque)
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +0000107{
Daniel P. Berrangé52d6cfe2019-02-11 18:24:27 +0000108 QIOTask *task = opaque;
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +0000109
Daniel P. Berrangé52d6cfe2019-02-11 18:24:27 +0000110 trace_qio_task_thread_result(task);
111 qio_task_complete(task);
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +0000112
113 return FALSE;
114}
115
116
117static gpointer qio_task_thread_worker(gpointer opaque)
118{
Daniel P. Berrangé52d6cfe2019-02-11 18:24:27 +0000119 QIOTask *task = opaque;
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +0000120
Daniel P. Berrangé52d6cfe2019-02-11 18:24:27 +0000121 trace_qio_task_thread_run(task);
122
123 task->thread->worker(task, task->thread->opaque);
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +0000124
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
128 * the worker results
129 */
Daniel P. Berrangé52d6cfe2019-02-11 18:24:27 +0000130 trace_qio_task_thread_exit(task);
Peter Xua17536c2018-03-05 14:43:22 +0800131
Daniel P. Berrangédbb44502019-02-11 18:24:28 +0000132 qemu_mutex_lock(&task->thread_lock);
133
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);
Alberto Garciab65cb862019-08-12 18:58:28 +0300139 g_source_unref(task->thread->completion);
Daniel P. Berrangédbb44502019-02-11 18:24:28 +0000140 trace_qio_task_thread_source_attach(task, task->thread->completion);
141
142 qemu_cond_signal(&task->thread_cond);
143 qemu_mutex_unlock(&task->thread_lock);
Peter Xua17536c2018-03-05 14:43:22 +0800144
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +0000145 return NULL;
146}
147
148
149void qio_task_run_in_thread(QIOTask *task,
150 QIOTaskWorker worker,
151 gpointer opaque,
Peter Xua17536c2018-03-05 14:43:22 +0800152 GDestroyNotify destroy,
153 GMainContext *context)
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +0000154{
155 struct QIOTaskThreadData *data = g_new0(struct QIOTaskThreadData, 1);
156 QemuThread thread;
157
Peter Xua17536c2018-03-05 14:43:22 +0800158 if (context) {
159 g_main_context_ref(context);
160 }
161
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +0000162 data->worker = worker;
163 data->opaque = opaque;
164 data->destroy = destroy;
Peter Xua17536c2018-03-05 14:43:22 +0800165 data->context = context;
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +0000166
Daniel P. Berrangé52d6cfe2019-02-11 18:24:27 +0000167 task->thread = data;
168
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +0000169 trace_qio_task_thread_start(task, worker, opaque);
170 qemu_thread_create(&thread,
171 "io-task-worker",
172 qio_task_thread_worker,
Daniel P. Berrangé52d6cfe2019-02-11 18:24:27 +0000173 task,
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +0000174 QEMU_THREAD_DETACHED);
175}
176
177
Daniel P. Berrangédbb44502019-02-11 18:24:28 +0000178void qio_task_wait_thread(QIOTask *task)
179{
180 qemu_mutex_lock(&task->thread_lock);
181 g_assert(task->thread != NULL);
182 while (task->thread->completion == NULL) {
183 qemu_cond_wait(&task->thread_cond, &task->thread_lock);
184 }
185
186 trace_qio_task_thread_source_cancel(task, task->thread->completion);
187 g_source_destroy(task->thread->completion);
188 qemu_mutex_unlock(&task->thread_lock);
189
190 qio_task_thread_result(task);
191}
192
193
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +0000194void qio_task_complete(QIOTask *task)
195{
Daniel P. Berrange60e705c2016-08-11 15:20:58 +0100196 task->func(task, task->opaque);
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +0000197 trace_qio_task_complete(task);
198 qio_task_free(task);
199}
200
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +0000201
Daniel P. Berrange1a447e42016-08-11 14:40:44 +0100202void qio_task_set_error(QIOTask *task,
203 Error *err)
204{
205 error_propagate(&task->err, err);
206}
207
208
209bool qio_task_propagate_error(QIOTask *task,
210 Error **errp)
211{
212 if (task->err) {
213 error_propagate(errp, task->err);
Daniel P. Berrange80fb34e2017-01-25 11:10:53 +0000214 task->err = NULL;
Daniel P. Berrange1a447e42016-08-11 14:40:44 +0100215 return true;
216 }
217
218 return false;
219}
220
221
Daniel P. Berrange52dd99e2016-08-11 14:36:21 +0100222void qio_task_set_result_pointer(QIOTask *task,
223 gpointer result,
224 GDestroyNotify destroy)
225{
226 task->result = result;
227 task->destroyResult = destroy;
228}
229
230
231gpointer qio_task_get_result_pointer(QIOTask *task)
232{
233 return task->result;
234}
235
236
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +0000237Object *qio_task_get_source(QIOTask *task)
238{
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +0000239 return task->source;
240}