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"
23 #include "qapi/error.h"
24 #include "qemu/thread.h"
31 GDestroyNotify destroy
;
34 GDestroyNotify destroyResult
;
38 QIOTask
*qio_task_new(Object
*source
,
41 GDestroyNotify destroy
)
45 task
= g_new0(QIOTask
, 1);
47 task
->source
= source
;
50 task
->opaque
= opaque
;
51 task
->destroy
= destroy
;
53 trace_qio_task_new(task
, source
, func
, opaque
);
58 static void qio_task_free(QIOTask
*task
)
61 task
->destroy(task
->opaque
);
63 if (task
->destroyResult
) {
64 task
->destroyResult(task
->result
);
67 error_free(task
->err
);
69 object_unref(task
->source
);
75 struct QIOTaskThreadData
{
79 GDestroyNotify destroy
;
85 static gboolean
gio_task_thread_result(gpointer opaque
)
87 struct QIOTaskThreadData
*data
= opaque
;
89 trace_qio_task_thread_result(data
->task
);
91 qio_task_complete(data
->task
);
93 qio_task_abort(data
->task
, data
->err
);
96 error_free(data
->err
);
98 data
->destroy(data
->opaque
);
107 static gpointer
qio_task_thread_worker(gpointer opaque
)
109 struct QIOTaskThreadData
*data
= opaque
;
111 trace_qio_task_thread_run(data
->task
);
112 data
->ret
= data
->worker(data
->task
, &data
->err
, data
->opaque
);
113 if (data
->ret
< 0 && data
->err
== NULL
) {
114 error_setg(&data
->err
, "Task worker failed but did not set an error");
117 /* We're running in the background thread, and must only
118 * ever report the task results in the main event loop
119 * thread. So we schedule an idle callback to report
122 trace_qio_task_thread_exit(data
->task
);
123 g_idle_add(gio_task_thread_result
, data
);
128 void qio_task_run_in_thread(QIOTask
*task
,
129 QIOTaskWorker worker
,
131 GDestroyNotify destroy
)
133 struct QIOTaskThreadData
*data
= g_new0(struct QIOTaskThreadData
, 1);
137 data
->worker
= worker
;
138 data
->opaque
= opaque
;
139 data
->destroy
= destroy
;
141 trace_qio_task_thread_start(task
, worker
, opaque
);
142 qemu_thread_create(&thread
,
144 qio_task_thread_worker
,
146 QEMU_THREAD_DETACHED
);
150 void qio_task_complete(QIOTask
*task
)
152 task
->func(task
->source
, NULL
, task
->opaque
);
153 trace_qio_task_complete(task
);
157 void qio_task_abort(QIOTask
*task
,
160 task
->func(task
->source
, err
, task
->opaque
);
161 trace_qio_task_abort(task
);
166 void qio_task_set_error(QIOTask
*task
,
169 error_propagate(&task
->err
, err
);
173 bool qio_task_propagate_error(QIOTask
*task
,
177 error_propagate(errp
, task
->err
);
185 void qio_task_set_result_pointer(QIOTask
*task
,
187 GDestroyNotify destroy
)
189 task
->result
= result
;
190 task
->destroyResult
= destroy
;
194 gpointer
qio_task_get_result_pointer(QIOTask
*task
)
200 Object
*qio_task_get_source(QIOTask
*task
)