]>
Commit | Line | Data |
---|---|---|
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 | ||
52d6cfec DB |
27 | struct QIOTaskThreadData { |
28 | QIOTaskWorker worker; | |
29 | gpointer opaque; | |
30 | GDestroyNotify destroy; | |
31 | GMainContext *context; | |
dbb44504 | 32 | GSource *completion; |
52d6cfec DB |
33 | }; |
34 | ||
35 | ||
b02db2d9 DB |
36 | struct QIOTask { |
37 | Object *source; | |
38 | QIOTaskFunc func; | |
39 | gpointer opaque; | |
40 | GDestroyNotify destroy; | |
1a447e4f | 41 | Error *err; |
52dd99e8 DB |
42 | gpointer result; |
43 | GDestroyNotify destroyResult; | |
dbb44504 DB |
44 | QemuMutex thread_lock; |
45 | QemuCond thread_cond; | |
52d6cfec | 46 | struct QIOTaskThreadData *thread; |
b02db2d9 DB |
47 | }; |
48 | ||
49 | ||
50 | QIOTask *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; | |
dbb44504 DB |
64 | qemu_mutex_init(&task->thread_lock); |
65 | qemu_cond_init(&task->thread_cond); | |
b02db2d9 DB |
66 | |
67 | trace_qio_task_new(task, source, func, opaque); | |
68 | ||
69 | return task; | |
70 | } | |
71 | ||
72 | static void qio_task_free(QIOTask *task) | |
73 | { | |
dbb44504 | 74 | qemu_mutex_lock(&task->thread_lock); |
52d6cfec DB |
75 | 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 | ||
b02db2d9 DB |
87 | if (task->destroy) { |
88 | task->destroy(task->opaque); | |
89 | } | |
52dd99e8 DB |
90 | if (task->destroyResult) { |
91 | task->destroyResult(task->result); | |
92 | } | |
1a447e4f DB |
93 | if (task->err) { |
94 | error_free(task->err); | |
95 | } | |
b02db2d9 DB |
96 | object_unref(task->source); |
97 | ||
dbb44504 DB |
98 | qemu_mutex_unlock(&task->thread_lock); |
99 | qemu_mutex_destroy(&task->thread_lock); | |
100 | qemu_cond_destroy(&task->thread_cond); | |
101 | ||
b02db2d9 DB |
102 | g_free(task); |
103 | } | |
104 | ||
105 | ||
7c28768f | 106 | static gboolean qio_task_thread_result(gpointer opaque) |
b02db2d9 | 107 | { |
52d6cfec | 108 | QIOTask *task = opaque; |
b02db2d9 | 109 | |
52d6cfec DB |
110 | trace_qio_task_thread_result(task); |
111 | qio_task_complete(task); | |
b02db2d9 DB |
112 | |
113 | return FALSE; | |
114 | } | |
115 | ||
116 | ||
117 | static gpointer qio_task_thread_worker(gpointer opaque) | |
118 | { | |
52d6cfec | 119 | QIOTask *task = opaque; |
b02db2d9 | 120 | |
52d6cfec DB |
121 | trace_qio_task_thread_run(task); |
122 | ||
123 | task->thread->worker(task, task->thread->opaque); | |
b02db2d9 DB |
124 | |
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 | */ | |
52d6cfec | 130 | trace_qio_task_thread_exit(task); |
a17536c5 | 131 | |
dbb44504 DB |
132 | 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); | |
b65cb867 | 139 | g_source_unref(task->thread->completion); |
dbb44504 DB |
140 | 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); | |
a17536c5 | 144 | |
b02db2d9 DB |
145 | return NULL; |
146 | } | |
147 | ||
148 | ||
149 | void qio_task_run_in_thread(QIOTask *task, | |
150 | QIOTaskWorker worker, | |
151 | gpointer opaque, | |
a17536c5 PX |
152 | GDestroyNotify destroy, |
153 | GMainContext *context) | |
b02db2d9 DB |
154 | { |
155 | struct QIOTaskThreadData *data = g_new0(struct QIOTaskThreadData, 1); | |
156 | QemuThread thread; | |
157 | ||
a17536c5 PX |
158 | if (context) { |
159 | g_main_context_ref(context); | |
160 | } | |
161 | ||
b02db2d9 DB |
162 | data->worker = worker; |
163 | data->opaque = opaque; | |
164 | data->destroy = destroy; | |
a17536c5 | 165 | data->context = context; |
b02db2d9 | 166 | |
52d6cfec DB |
167 | task->thread = data; |
168 | ||
b02db2d9 DB |
169 | trace_qio_task_thread_start(task, worker, opaque); |
170 | qemu_thread_create(&thread, | |
171 | "io-task-worker", | |
172 | qio_task_thread_worker, | |
52d6cfec | 173 | task, |
b02db2d9 DB |
174 | QEMU_THREAD_DETACHED); |
175 | } | |
176 | ||
177 | ||
dbb44504 DB |
178 | void 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 | ||
b02db2d9 DB |
194 | void qio_task_complete(QIOTask *task) |
195 | { | |
60e705c5 | 196 | task->func(task, task->opaque); |
b02db2d9 DB |
197 | trace_qio_task_complete(task); |
198 | qio_task_free(task); | |
199 | } | |
200 | ||
b02db2d9 | 201 | |
1a447e4f DB |
202 | void qio_task_set_error(QIOTask *task, |
203 | Error *err) | |
204 | { | |
205 | error_propagate(&task->err, err); | |
206 | } | |
207 | ||
208 | ||
209 | bool qio_task_propagate_error(QIOTask *task, | |
210 | Error **errp) | |
211 | { | |
212 | if (task->err) { | |
213 | error_propagate(errp, task->err); | |
80fb34ed | 214 | task->err = NULL; |
1a447e4f DB |
215 | return true; |
216 | } | |
217 | ||
218 | return false; | |
219 | } | |
220 | ||
221 | ||
52dd99e8 DB |
222 | void 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 | ||
231 | gpointer qio_task_get_result_pointer(QIOTask *task) | |
232 | { | |
233 | return task->result; | |
234 | } | |
235 | ||
236 | ||
b02db2d9 DB |
237 | Object *qio_task_get_source(QIOTask *task) |
238 | { | |
b02db2d9 DB |
239 | return task->source; |
240 | } |