aboutsummaryrefslogtreecommitdiff
path: root/include/io/task.h
blob: 6818dfedd005c86fd3fe39311ae1b6ef2982b67b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
 * QEMU I/O task
 *
 * Copyright (c) 2015 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifndef QIO_TASK_H
#define QIO_TASK_H

typedef struct QIOTask QIOTask;

typedef void (*QIOTaskFunc)(QIOTask *task,
                            gpointer opaque);

typedef void (*QIOTaskWorker)(QIOTask *task,
                              gpointer opaque);

/**
 * QIOTask:
 *
 * The QIOTask object provides a simple mechanism for reporting
 * success / failure of long running background operations.
 *
 * A object on which the operation is to be performed could have
 * a public API which accepts a task callback:
 *
 * <example>
 *   <title>Task function signature</title>
 *   <programlisting>
 *  void myobject_operation(QMyObject *obj,
 *                          QIOTaskFunc *func,
 *                          gpointer opaque,
 *                          GDestroyNotify notify);
 *   </programlisting>
 * </example>
 *
 * The 'func' parameter is the callback to be invoked, and 'opaque'
 * is data to pass to it. The optional 'notify' function is used
 * to free 'opaque' when no longer needed.
 *
 * When the operation completes, the 'func' callback will be
 * invoked, allowing the calling code to determine the result
 * of the operation. An example QIOTaskFunc implementation may
 * look like
 *
 * <example>
 *   <title>Task callback implementation</title>
 *   <programlisting>
 *  static void myobject_operation_notify(QIOTask *task,
 *                                        gpointer opaque)
 *  {
 *      Error *err = NULL;
 *      if (qio_task_propagate_error(task, &err)) {
 *          ...deal with the failure...
 *          error_free(err);
 *      } else {
 *          QMyObject *src = QMY_OBJECT(qio_task_get_source(task));
 *          ...deal with the completion...
 *      }
 *  }
 *   </programlisting>
 * </example>
 *
 * Now, lets say the implementation of the method using the
 * task wants to set a timer to run once a second checking
 * for completion of some activity. It would do something
 * like
 *
 * <example>
 *   <title>Task function implementation</title>
 *   <programlisting>
 *    void myobject_operation(QMyObject *obj,
 *                            QIOTaskFunc *func,
 *                            gpointer opaque,
 *                            GDestroyNotify notify)
 *    {
 *      QIOTask *task;
 *
 *      task = qio_task_new(OBJECT(obj), func, opaque, notify);
 *
 *      g_timeout_add_full(G_PRIORITY_DEFAULT,
 *                         1000,
 *                         myobject_operation_timer,
 *                         task,
 *                         NULL);
 *    }
 *   </programlisting>
 * </example>
 *
 * It could equally have setup a watch on a file descriptor or
 * created a background thread, or something else entirely.
 * Notice that the source object is passed to the task, and
 * QIOTask will hold a reference on that. This ensure that
 * the QMyObject instance cannot be garbage collected while
 * the async task is still in progress.
 *
 * In this case, myobject_operation_timer will fire after
 * 3 secs and do
 *
 * <example>
 *   <title>Task timer function</title>
 *   <programlisting>
 *   gboolean myobject_operation_timer(gpointer opaque)
 *   {
 *      QIOTask *task = QIO_TASK(opaque);
 *      Error *err = NULL;
 *
 *      ...check something important...
 *       if (err) {
 *           qio_task_set_error(task, err);
 *           qio_task_complete(task);
 *           return FALSE;
 *       } else if (...work is completed ...) {
 *           qio_task_complete(task);
 *           return FALSE;
 *       }
 *       ...carry on polling ...
 *       return TRUE;
 *   }
 *   </programlisting>
 * </example>
 *
 * The 'qio_task_complete' call in this method will trigger
 * the callback func 'myobject_operation_notify' shown
 * earlier to deal with the results.
 *
 * Once this function returns false, object_unref will be called
 * automatically on the task causing it to be released and the
 * ref on QMyObject dropped too.
 *
 * The QIOTask module can also be used to perform operations
 * in a background thread context, while still reporting the
 * results in the main event thread. This allows code which
 * cannot easily be rewritten to be asychronous (such as DNS
 * lookups) to be easily run non-blocking. Reporting the
 * results in the main thread context means that the caller
 * typically does not need to be concerned about thread
 * safety wrt the QEMU global mutex.
 *
 * For example, the socket_listen() method will block the caller
 * while DNS lookups take place if given a name, instead of IP
 * address. The C library often do not provide a practical async
 * DNS API, so the to get non-blocking DNS lookups in a portable
 * manner requires use of a thread. So achieve a non-blocking
 * socket listen using QIOTask would require:
 *
 * <example>
 *    static void myobject_listen_worker(QIOTask *task,
 *                                       gpointer opaque)
 *    {
 *       QMyObject obj = QMY_OBJECT(qio_task_get_source(task));
 *       SocketAddress *addr = opaque;
 *       Error *err = NULL;
 *
 *       obj->fd = socket_listen(addr, &err);
 *
         qio_task_set_error(task, err);
 *    }
 *
 *    void myobject_listen_async(QMyObject *obj,
 *                               SocketAddress *addr,
 *                               QIOTaskFunc *func,
 *                               gpointer opaque,
 *                               GDestroyNotify notify)
 *    {
 *      QIOTask *task;
 *      SocketAddress *addrCopy;
 *
 *      addrCopy = QAPI_CLONE(SocketAddress, addr);
 *      task = qio_task_new(OBJECT(obj), func, opaque, notify);
 *
 *      qio_task_run_in_thread(task, myobject_listen_worker,
 *                             addrCopy,
 *                             qapi_free_SocketAddress);
 *    }
 * </example>
 *
 * NB, The 'func' callback passed into myobject_listen_async
 * will be invoked from the main event thread, despite the
 * actual operation being performed in a different thread.
 */

/**
 * qio_task_new:
 * @source: the object on which the operation is invoked
 * @func: the callback to invoke when the task completes
 * @opaque: opaque data to pass to @func when invoked
 * @destroy: optional callback to free @opaque
 *
 * Creates a new task struct to track completion of a
 * background operation running on the object @source.
 * When the operation completes or fails, the callback
 * @func will be invoked. The callback can access the
 * 'err' attribute in the task object to determine if
 * the operation was successful or not.
 *
 * The returned task will be released when qio_task_complete()
 * is invoked.
 *
 * Returns: the task struct
 */
QIOTask *qio_task_new(Object *source,
                      QIOTaskFunc func,
                      gpointer opaque,
                      GDestroyNotify destroy);

/**
 * qio_task_run_in_thread:
 * @task: the task struct
 * @worker: the function to invoke in a thread
 * @opaque: opaque data to pass to @worker
 * @destroy: function to free @opaque
 * @context: the context to run the complete hook. If %NULL, the
 *           default context will be used.
 *
 * Run a task in a background thread. When @worker
 * returns it will call qio_task_complete() in
 * the thread that is running the main loop associated
 * with @context.
 */
void qio_task_run_in_thread(QIOTask *task,
                            QIOTaskWorker worker,
                            gpointer opaque,
                            GDestroyNotify destroy,
                            GMainContext *context);


/**
 * qio_task_wait_thread:
 * @task: the task struct
 *
 * Wait for completion of a task that was previously
 * invoked using qio_task_run_in_thread. This MUST
 * ONLY be invoked if the task has not already
 * completed, since after the completion callback
 * is invoked, @task will have been freed.
 *
 * To avoid racing with execution of the completion
 * callback provided with qio_task_new, this method
 * MUST ONLY be invoked from the thread that is
 * running the main loop associated with @context
 * parameter to qio_task_run_in_thread.
 *
 * When the thread has completed, the completion
 * callback provided to qio_task_new will be invoked.
 * When that callback returns @task will be freed,
 * so @task must not be referenced after this
 * method completes.
 */
void qio_task_wait_thread(QIOTask *task);


/**
 * qio_task_complete:
 * @task: the task struct
 *
 * Invoke the completion callback for @task and
 * then free its memory.
 */
void qio_task_complete(QIOTask *task);


/**
 * qio_task_set_error:
 * @task: the task struct
 * @err: pointer to the error, or NULL
 *
 * Associate an error with the task, which can later
 * be retrieved with the qio_task_propagate_error()
 * method. This method takes ownership of @err, so
 * it is not valid to access it after this call
 * completes. If @err is NULL this is a no-op. If
 * this is call multiple times, only the first
 * provided @err will be recorded, later ones will
 * be discarded and freed.
 */
void qio_task_set_error(QIOTask *task,
                        Error *err);


/**
 * qio_task_propagate_error:
 * @task: the task struct
 * @errp: pointer to a NULL-initialized error object
 *
 * Propagate the error associated with @task
 * into @errp.
 *
 * Returns: true if an error was propagated, false otherwise
 */
bool qio_task_propagate_error(QIOTask *task,
                              Error **errp);


/**
 * qio_task_set_result_pointer:
 * @task: the task struct
 * @result: pointer to the result data
 *
 * Associate an opaque result with the task,
 * which can later be retrieved with the
 * qio_task_get_result_pointer() method
 *
 */
void qio_task_set_result_pointer(QIOTask *task,
                                 gpointer result,
                                 GDestroyNotify notify);


/**
 * qio_task_get_result_pointer:
 * @task: the task struct
 *
 * Retrieve the opaque result data associated
 * with the task, if any.
 *
 * Returns: the task result, or NULL
 */
gpointer qio_task_get_result_pointer(QIOTask *task);


/**
 * qio_task_get_source:
 * @task: the task struct
 *
 * Get the source object associated with the background
 * task. The caller does not own a reference on the
 * returned Object, and so should call object_ref()
 * if it wants to keep the object pointer outside the
 * lifetime of the QIOTask object.
 *
 * Returns: the source object
 */
Object *qio_task_get_source(QIOTask *task);

#endif /* QIO_TASK_H */