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/>.
24 #include "qemu-common.h"
25 #include "qom/object.h"
27 typedef struct QIOTask QIOTask;
29 typedef void (*QIOTaskFunc)(Object *source,
33 typedef int (*QIOTaskWorker)(QIOTask *task,
40 * The QIOTask object provides a simple mechanism for reporting
41 * success / failure of long running background operations.
43 * A object on which the operation is to be performed could have
44 * a public API which accepts a task callback:
47 * <title>Task callback function signature</title>
49 * void myobject_operation(QMyObject *obj,
52 * GDestroyNotify *notify);
56 * The 'func' parameter is the callback to be invoked, and 'opaque'
57 * is data to pass to it. The optional 'notify' function is used
58 * to free 'opaque' when no longer needed.
60 * Now, lets say the implementation of this method wants to set
61 * a timer to run once a second checking for completion of some
62 * activity. It would do something like
65 * <title>Task callback function implementation</title>
67 * void myobject_operation(QMyObject *obj,
70 * GDestroyNotify *notify)
74 * task = qio_task_new(OBJECT(obj), func, opaque, notify);
76 * g_timeout_add_full(G_PRIORITY_DEFAULT,
78 * myobject_operation_timer,
85 * It could equally have setup a watch on a file descriptor or
86 * created a background thread, or something else entirely.
87 * Notice that the source object is passed to the task, and
88 * QIOTask will hold a reference on that. This ensure that
89 * the QMyObject instance cannot be garbage collected while
90 * the async task is still in progress.
92 * In this case, myobject_operation_timer will fire after
96 * <title>Task timer function</title>
98 * gboolean myobject_operation_timer(gpointer opaque)
100 * QIOTask *task = QIO_TASK(opaque);
103 * ...check something important...
105 * qio_task_abort(task, err);
108 * } else if (...work is completed ...) {
109 * qio_task_complete(task);
112 * ...carry on polling ...
118 * Once this function returns false, object_unref will be called
119 * automatically on the task causing it to be released and the
120 * ref on QMyObject dropped too.
122 * The QIOTask module can also be used to perform operations
123 * in a background thread context, while still reporting the
124 * results in the main event thread. This allows code which
125 * cannot easily be rewritten to be asychronous (such as DNS
126 * lookups) to be easily run non-blocking. Reporting the
127 * results in the main thread context means that the caller
128 * typically does not need to be concerned about thread
129 * safety wrt the QEMU global mutex.
131 * For example, the socket_listen() method will block the caller
132 * while DNS lookups take place if given a name, instead of IP
133 * address. The C library often do not provide a practical async
134 * DNS API, so the to get non-blocking DNS lookups in a portable
135 * manner requires use of a thread. So achieve a non-blocking
136 * socket listen using QIOTask would require:
139 * static int myobject_listen_worker(QIOTask *task,
143 * QMyObject obj = QMY_OBJECT(qio_task_get_source(task));
144 * SocketAddress *addr = opaque;
146 * obj->fd = socket_listen(addr, errp);
153 * void myobject_listen_async(QMyObject *obj,
154 * SocketAddress *addr,
157 * GDestroyNotify *notify)
160 * SocketAddress *addrCopy;
162 * qapi_copy_SocketAddress(&addrCopy, addr);
163 * task = qio_task_new(OBJECT(obj), func, opaque, notify);
165 * qio_task_run_in_thread(task, myobject_listen_worker,
167 * qapi_free_SocketAddress);
171 * NB, The 'func' callback passed into myobject_listen_async
172 * will be invoked from the main event thread, despite the
173 * actual operation being performed in a different thread.
178 * @source: the object on which the operation is invoked
179 * @func: the callback to invoke when the task completes
180 * @opaque: opaque data to pass to @func when invoked
181 * @destroy: optional callback to free @opaque
183 * Creates a new task struct to track completion of a
184 * background operation running on the object @source.
185 * When the operation completes or fails, the callback
186 * @func will be invoked. The callback can access the
187 * 'err' attribute in the task object to determine if
188 * the operation was successful or not.
190 * The returned task will be released when one of
191 * qio_task_abort() or qio_task_complete() are invoked.
193 * Returns: the task struct
195 QIOTask *qio_task_new(Object *source,
198 GDestroyNotify destroy);
201 * qio_task_run_in_thread:
202 * @task: the task struct
203 * @worker: the function to invoke in a thread
204 * @opaque: opaque data to pass to @worker
205 * @destroy: function to free @opaque
207 * Run a task in a background thread. If @worker
208 * returns 0 it will call qio_task_complete() in
209 * the main event thread context. If @worker
210 * returns -1 it will call qio_task_abort() in
211 * the main event thread context.
213 void qio_task_run_in_thread(QIOTask *task,
214 QIOTaskWorker worker,
216 GDestroyNotify destroy);
220 * @task: the task struct
222 * Mark the operation as succesfully completed
223 * and free the memory for @task.
225 void qio_task_complete(QIOTask *task);
229 * @task: the task struct
230 * @err: the error to record for the operation
232 * Mark the operation as failed, with @err providing
233 * details about the failure. The @err may be freed
234 * afer the function returns, as the notification
235 * callback is invoked synchronously. The @task will
236 * be freed when this call completes.
238 void qio_task_abort(QIOTask *task,
243 * qio_task_get_source:
244 * @task: the task struct
246 * Get the source object associated with the background
247 * task. This returns a new reference to the object,
248 * which the caller must released with object_unref()
249 * when no longer required.
251 * Returns: the source object
253 Object *qio_task_get_source(QIOTask *task);
255 #endif /* QIO_TASK_H__ */