blob: 451f26f8b4959eb1041cd7bc7d3e3803799a021c [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
Chetan Pantc8198bd2020-10-14 13:40:33 +00009 * version 2.1 of the License, or (at your option) any later version.
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +000010 *
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"
Philippe Mathieu-Daudé78f8d492020-05-04 13:56:55 +020025#include "qom/object.h"
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +000026#include "trace.h"
27
Daniel P. Berrangé52d6cfe2019-02-11 18:24:27 +000028struct QIOTaskThreadData {
29 QIOTaskWorker worker;
30 gpointer opaque;
31 GDestroyNotify destroy;
32 GMainContext *context;
Daniel P. Berrangédbb44502019-02-11 18:24:28 +000033 GSource *completion;
Daniel P. Berrangé52d6cfe2019-02-11 18:24:27 +000034};
35
36
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +000037struct QIOTask {
38 Object *source;
39 QIOTaskFunc func;
40 gpointer opaque;
41 GDestroyNotify destroy;
Daniel P. Berrange1a447e42016-08-11 14:40:44 +010042 Error *err;
Daniel P. Berrange52dd99e2016-08-11 14:36:21 +010043 gpointer result;
44 GDestroyNotify destroyResult;
Daniel P. Berrangédbb44502019-02-11 18:24:28 +000045 QemuMutex thread_lock;
46 QemuCond thread_cond;
Daniel P. Berrangé52d6cfe2019-02-11 18:24:27 +000047 struct QIOTaskThreadData *thread;
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +000048};
49
50
51QIOTask *qio_task_new(Object *source,
52 QIOTaskFunc func,
53 gpointer opaque,
54 GDestroyNotify destroy)
55{
56 QIOTask *task;
57
58 task = g_new0(QIOTask, 1);
59
60 task->source = source;
61 object_ref(source);
62 task->func = func;
63 task->opaque = opaque;
64 task->destroy = destroy;
Daniel P. Berrangédbb44502019-02-11 18:24:28 +000065 qemu_mutex_init(&task->thread_lock);
66 qemu_cond_init(&task->thread_cond);
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +000067
68 trace_qio_task_new(task, source, func, opaque);
69
70 return task;
71}
72
73static void qio_task_free(QIOTask *task)
74{
Daniel P. Berrangédbb44502019-02-11 18:24:28 +000075 qemu_mutex_lock(&task->thread_lock);
Daniel P. Berrangé52d6cfe2019-02-11 18:24:27 +000076 if (task->thread) {
77 if (task->thread->destroy) {
78 task->thread->destroy(task->thread->opaque);
79 }
80
81 if (task->thread->context) {
82 g_main_context_unref(task->thread->context);
83 }
84
85 g_free(task->thread);
86 }
87
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +000088 if (task->destroy) {
89 task->destroy(task->opaque);
90 }
Daniel P. Berrange52dd99e2016-08-11 14:36:21 +010091 if (task->destroyResult) {
92 task->destroyResult(task->result);
93 }
Daniel P. Berrange1a447e42016-08-11 14:40:44 +010094 if (task->err) {
95 error_free(task->err);
96 }
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +000097 object_unref(task->source);
98
Daniel P. Berrangédbb44502019-02-11 18:24:28 +000099 qemu_mutex_unlock(&task->thread_lock);
100 qemu_mutex_destroy(&task->thread_lock);
101 qemu_cond_destroy(&task->thread_cond);
102
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +0000103 g_free(task);
104}
105
106
Peter Xu7c287682018-03-05 14:43:19 +0800107static gboolean qio_task_thread_result(gpointer opaque)
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +0000108{
Daniel P. Berrangé52d6cfe2019-02-11 18:24:27 +0000109 QIOTask *task = opaque;
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +0000110
Daniel P. Berrangé52d6cfe2019-02-11 18:24:27 +0000111 trace_qio_task_thread_result(task);
112 qio_task_complete(task);
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +0000113
114 return FALSE;
115}
116
117
118static gpointer qio_task_thread_worker(gpointer opaque)
119{
Daniel P. Berrangé52d6cfe2019-02-11 18:24:27 +0000120 QIOTask *task = opaque;
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +0000121
Daniel P. Berrangé52d6cfe2019-02-11 18:24:27 +0000122 trace_qio_task_thread_run(task);
123
124 task->thread->worker(task, task->thread->opaque);
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +0000125
126 /* We're running in the background thread, and must only
127 * ever report the task results in the main event loop
128 * thread. So we schedule an idle callback to report
129 * the worker results
130 */
Daniel P. Berrangé52d6cfe2019-02-11 18:24:27 +0000131 trace_qio_task_thread_exit(task);
Peter Xua17536c2018-03-05 14:43:22 +0800132
Daniel P. Berrangédbb44502019-02-11 18:24:28 +0000133 qemu_mutex_lock(&task->thread_lock);
134
135 task->thread->completion = g_idle_source_new();
136 g_source_set_callback(task->thread->completion,
137 qio_task_thread_result, task, NULL);
138 g_source_attach(task->thread->completion,
139 task->thread->context);
Alberto Garciab65cb862019-08-12 18:58:28 +0300140 g_source_unref(task->thread->completion);
Daniel P. Berrangédbb44502019-02-11 18:24:28 +0000141 trace_qio_task_thread_source_attach(task, task->thread->completion);
142
143 qemu_cond_signal(&task->thread_cond);
144 qemu_mutex_unlock(&task->thread_lock);
Peter Xua17536c2018-03-05 14:43:22 +0800145
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +0000146 return NULL;
147}
148
149
150void qio_task_run_in_thread(QIOTask *task,
151 QIOTaskWorker worker,
152 gpointer opaque,
Peter Xua17536c2018-03-05 14:43:22 +0800153 GDestroyNotify destroy,
154 GMainContext *context)
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +0000155{
156 struct QIOTaskThreadData *data = g_new0(struct QIOTaskThreadData, 1);
157 QemuThread thread;
158
Peter Xua17536c2018-03-05 14:43:22 +0800159 if (context) {
160 g_main_context_ref(context);
161 }
162
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +0000163 data->worker = worker;
164 data->opaque = opaque;
165 data->destroy = destroy;
Peter Xua17536c2018-03-05 14:43:22 +0800166 data->context = context;
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +0000167
Daniel P. Berrangé52d6cfe2019-02-11 18:24:27 +0000168 task->thread = data;
169
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +0000170 trace_qio_task_thread_start(task, worker, opaque);
171 qemu_thread_create(&thread,
172 "io-task-worker",
173 qio_task_thread_worker,
Daniel P. Berrangé52d6cfe2019-02-11 18:24:27 +0000174 task,
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +0000175 QEMU_THREAD_DETACHED);
176}
177
178
Daniel P. Berrangédbb44502019-02-11 18:24:28 +0000179void qio_task_wait_thread(QIOTask *task)
180{
181 qemu_mutex_lock(&task->thread_lock);
182 g_assert(task->thread != NULL);
183 while (task->thread->completion == NULL) {
184 qemu_cond_wait(&task->thread_cond, &task->thread_lock);
185 }
186
187 trace_qio_task_thread_source_cancel(task, task->thread->completion);
188 g_source_destroy(task->thread->completion);
189 qemu_mutex_unlock(&task->thread_lock);
190
191 qio_task_thread_result(task);
192}
193
194
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +0000195void qio_task_complete(QIOTask *task)
196{
Daniel P. Berrange60e705c2016-08-11 15:20:58 +0100197 task->func(task, task->opaque);
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +0000198 trace_qio_task_complete(task);
199 qio_task_free(task);
200}
201
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +0000202
Daniel P. Berrange1a447e42016-08-11 14:40:44 +0100203void qio_task_set_error(QIOTask *task,
204 Error *err)
205{
206 error_propagate(&task->err, err);
207}
208
209
210bool qio_task_propagate_error(QIOTask *task,
211 Error **errp)
212{
213 if (task->err) {
214 error_propagate(errp, task->err);
Daniel P. Berrange80fb34e2017-01-25 11:10:53 +0000215 task->err = NULL;
Daniel P. Berrange1a447e42016-08-11 14:40:44 +0100216 return true;
217 }
218
219 return false;
220}
221
222
Daniel P. Berrange52dd99e2016-08-11 14:36:21 +0100223void qio_task_set_result_pointer(QIOTask *task,
224 gpointer result,
225 GDestroyNotify destroy)
226{
227 task->result = result;
228 task->destroyResult = destroy;
229}
230
231
232gpointer qio_task_get_result_pointer(QIOTask *task)
233{
234 return task->result;
235}
236
237
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +0000238Object *qio_task_get_source(QIOTask *task)
239{
Daniel P. Berrangeb02db2d2015-03-18 17:25:45 +0000240 return task->source;
241}