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
;
33 GDestroyNotify destroyResult
;
37 QIOTask
*qio_task_new(Object
*source
,
40 GDestroyNotify destroy
)
44 task
= g_new0(QIOTask
, 1);
46 task
->source
= source
;
49 task
->opaque
= opaque
;
50 task
->destroy
= destroy
;
52 trace_qio_task_new(task
, source
, func
, opaque
);
57 static void qio_task_free(QIOTask
*task
)
60 task
->destroy(task
->opaque
);
62 if (task
->destroyResult
) {
63 task
->destroyResult(task
->result
);
65 object_unref(task
->source
);
71 struct QIOTaskThreadData
{
75 GDestroyNotify destroy
;
81 static gboolean
gio_task_thread_result(gpointer opaque
)
83 struct QIOTaskThreadData
*data
= opaque
;
85 trace_qio_task_thread_result(data
->task
);
87 qio_task_complete(data
->task
);
89 qio_task_abort(data
->task
, data
->err
);
92 error_free(data
->err
);
94 data
->destroy(data
->opaque
);
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
118 trace_qio_task_thread_exit(data
->task
);
119 g_idle_add(gio_task_thread_result
, data
);
124 void qio_task_run_in_thread(QIOTask
*task
,
125 QIOTaskWorker worker
,
127 GDestroyNotify destroy
)
129 struct QIOTaskThreadData
*data
= g_new0(struct QIOTaskThreadData
, 1);
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
,
140 qio_task_thread_worker
,
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
);
153 void qio_task_abort(QIOTask
*task
,
156 task
->func(task
->source
, err
, task
->opaque
);
157 trace_qio_task_abort(task
);
162 void qio_task_set_result_pointer(QIOTask
*task
,
164 GDestroyNotify destroy
)
166 task
->result
= result
;
167 task
->destroyResult
= destroy
;
171 gpointer
qio_task_get_result_pointer(QIOTask
*task
)
177 Object
*qio_task_get_source(QIOTask
*task
)