]> git.proxmox.com Git - mirror_qemu.git/blame - io/task.c
io: change the QIOTask callback signature
[mirror_qemu.git] / io / task.c
CommitLineData
b02db2d9
DB
1/*
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
cae9fc56 21#include "qemu/osdep.h"
b02db2d9 22#include "io/task.h"
da34e65c 23#include "qapi/error.h"
b02db2d9
DB
24#include "qemu/thread.h"
25#include "trace.h"
26
27struct QIOTask {
28 Object *source;
29 QIOTaskFunc func;
30 gpointer opaque;
31 GDestroyNotify destroy;
1a447e4f 32 Error *err;
52dd99e8
DB
33 gpointer result;
34 GDestroyNotify destroyResult;
b02db2d9
DB
35};
36
37
38QIOTask *qio_task_new(Object *source,
39 QIOTaskFunc func,
40 gpointer opaque,
41 GDestroyNotify destroy)
42{
43 QIOTask *task;
44
45 task = g_new0(QIOTask, 1);
46
47 task->source = source;
48 object_ref(source);
49 task->func = func;
50 task->opaque = opaque;
51 task->destroy = destroy;
52
53 trace_qio_task_new(task, source, func, opaque);
54
55 return task;
56}
57
58static void qio_task_free(QIOTask *task)
59{
60 if (task->destroy) {
61 task->destroy(task->opaque);
62 }
52dd99e8
DB
63 if (task->destroyResult) {
64 task->destroyResult(task->result);
65 }
1a447e4f
DB
66 if (task->err) {
67 error_free(task->err);
68 }
b02db2d9
DB
69 object_unref(task->source);
70
71 g_free(task);
72}
73
74
75struct QIOTaskThreadData {
76 QIOTask *task;
77 QIOTaskWorker worker;
78 gpointer opaque;
79 GDestroyNotify destroy;
80 Error *err;
81 int ret;
82};
83
84
85static gboolean gio_task_thread_result(gpointer opaque)
86{
87 struct QIOTaskThreadData *data = opaque;
88
89 trace_qio_task_thread_result(data->task);
60e705c5
DB
90 if (data->err) {
91 qio_task_set_error(data->task, data->err);
b02db2d9 92 }
60e705c5 93 qio_task_complete(data->task);
b02db2d9 94
b02db2d9
DB
95 if (data->destroy) {
96 data->destroy(data->opaque);
97 }
98
99 g_free(data);
100
101 return FALSE;
102}
103
104
105static gpointer qio_task_thread_worker(gpointer opaque)
106{
107 struct QIOTaskThreadData *data = opaque;
108
109 trace_qio_task_thread_run(data->task);
110 data->ret = data->worker(data->task, &data->err, data->opaque);
111 if (data->ret < 0 && data->err == NULL) {
112 error_setg(&data->err, "Task worker failed but did not set an error");
113 }
114
115 /* We're running in the background thread, and must only
116 * ever report the task results in the main event loop
117 * thread. So we schedule an idle callback to report
118 * the worker results
119 */
120 trace_qio_task_thread_exit(data->task);
121 g_idle_add(gio_task_thread_result, data);
122 return NULL;
123}
124
125
126void qio_task_run_in_thread(QIOTask *task,
127 QIOTaskWorker worker,
128 gpointer opaque,
129 GDestroyNotify destroy)
130{
131 struct QIOTaskThreadData *data = g_new0(struct QIOTaskThreadData, 1);
132 QemuThread thread;
133
134 data->task = task;
135 data->worker = worker;
136 data->opaque = opaque;
137 data->destroy = destroy;
138
139 trace_qio_task_thread_start(task, worker, opaque);
140 qemu_thread_create(&thread,
141 "io-task-worker",
142 qio_task_thread_worker,
143 data,
144 QEMU_THREAD_DETACHED);
145}
146
147
148void qio_task_complete(QIOTask *task)
149{
60e705c5 150 task->func(task, task->opaque);
b02db2d9
DB
151 trace_qio_task_complete(task);
152 qio_task_free(task);
153}
154
b02db2d9 155
1a447e4f
DB
156void qio_task_set_error(QIOTask *task,
157 Error *err)
158{
159 error_propagate(&task->err, err);
160}
161
162
163bool qio_task_propagate_error(QIOTask *task,
164 Error **errp)
165{
166 if (task->err) {
167 error_propagate(errp, task->err);
168 return true;
169 }
170
171 return false;
172}
173
174
52dd99e8
DB
175void qio_task_set_result_pointer(QIOTask *task,
176 gpointer result,
177 GDestroyNotify destroy)
178{
179 task->result = result;
180 task->destroyResult = destroy;
181}
182
183
184gpointer qio_task_get_result_pointer(QIOTask *task)
185{
186 return task->result;
187}
188
189
b02db2d9
DB
190Object *qio_task_get_source(QIOTask *task)
191{
b02db2d9
DB
192 return task->source;
193}