io: add ability to associate an opaque "result" with with a task
[qemu/ar7.git] / io / task.c
blob675e196156b6bae85a3ed4242206cee07687350c
1 /*
2 * QEMU I/O task
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"
22 #include "io/task.h"
23 #include "qapi/error.h"
24 #include "qemu/thread.h"
25 #include "trace.h"
27 struct QIOTask {
28 Object *source;
29 QIOTaskFunc func;
30 gpointer opaque;
31 GDestroyNotify destroy;
32 gpointer result;
33 GDestroyNotify destroyResult;
37 QIOTask *qio_task_new(Object *source,
38 QIOTaskFunc func,
39 gpointer opaque,
40 GDestroyNotify destroy)
42 QIOTask *task;
44 task = g_new0(QIOTask, 1);
46 task->source = source;
47 object_ref(source);
48 task->func = func;
49 task->opaque = opaque;
50 task->destroy = destroy;
52 trace_qio_task_new(task, source, func, opaque);
54 return task;
57 static void qio_task_free(QIOTask *task)
59 if (task->destroy) {
60 task->destroy(task->opaque);
62 if (task->destroyResult) {
63 task->destroyResult(task->result);
65 object_unref(task->source);
67 g_free(task);
71 struct QIOTaskThreadData {
72 QIOTask *task;
73 QIOTaskWorker worker;
74 gpointer opaque;
75 GDestroyNotify destroy;
76 Error *err;
77 int ret;
81 static gboolean gio_task_thread_result(gpointer opaque)
83 struct QIOTaskThreadData *data = opaque;
85 trace_qio_task_thread_result(data->task);
86 if (data->ret == 0) {
87 qio_task_complete(data->task);
88 } else {
89 qio_task_abort(data->task, data->err);
92 error_free(data->err);
93 if (data->destroy) {
94 data->destroy(data->opaque);
97 g_free(data);
99 return FALSE;
103 static gpointer qio_task_thread_worker(gpointer opaque)
105 struct QIOTaskThreadData *data = opaque;
107 trace_qio_task_thread_run(data->task);
108 data->ret = data->worker(data->task, &data->err, data->opaque);
109 if (data->ret < 0 && data->err == NULL) {
110 error_setg(&data->err, "Task worker failed but did not set an error");
113 /* We're running in the background thread, and must only
114 * ever report the task results in the main event loop
115 * thread. So we schedule an idle callback to report
116 * the worker results
118 trace_qio_task_thread_exit(data->task);
119 g_idle_add(gio_task_thread_result, data);
120 return NULL;
124 void qio_task_run_in_thread(QIOTask *task,
125 QIOTaskWorker worker,
126 gpointer opaque,
127 GDestroyNotify destroy)
129 struct QIOTaskThreadData *data = g_new0(struct QIOTaskThreadData, 1);
130 QemuThread thread;
132 data->task = task;
133 data->worker = worker;
134 data->opaque = opaque;
135 data->destroy = destroy;
137 trace_qio_task_thread_start(task, worker, opaque);
138 qemu_thread_create(&thread,
139 "io-task-worker",
140 qio_task_thread_worker,
141 data,
142 QEMU_THREAD_DETACHED);
146 void qio_task_complete(QIOTask *task)
148 task->func(task->source, NULL, task->opaque);
149 trace_qio_task_complete(task);
150 qio_task_free(task);
153 void qio_task_abort(QIOTask *task,
154 Error *err)
156 task->func(task->source, err, task->opaque);
157 trace_qio_task_abort(task);
158 qio_task_free(task);
162 void qio_task_set_result_pointer(QIOTask *task,
163 gpointer result,
164 GDestroyNotify destroy)
166 task->result = result;
167 task->destroyResult = destroy;
171 gpointer qio_task_get_result_pointer(QIOTask *task)
173 return task->result;
177 Object *qio_task_get_source(QIOTask *task)
179 return task->source;