Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /* GIO - GLib Input, Output and Streaming Library  | 
2  |  |  *  | 
3  |  |  * Copyright 2011-2018 Red Hat, Inc.  | 
4  |  |  *  | 
5  |  |  * SPDX-License-Identifier: LGPL-2.1-or-later  | 
6  |  |  *  | 
7  |  |  * This library is free software; you can redistribute it and/or  | 
8  |  |  * modify it under the terms of the GNU Lesser General Public  | 
9  |  |  * License as published by the Free Software Foundation; either  | 
10  |  |  * version 2.1 of the License, or (at your option) any later version.  | 
11  |  |  *  | 
12  |  |  * This library is distributed in the hope that it will be useful,  | 
13  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of  | 
14  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  | 
15  |  |  * Lesser General Public License for more details.  | 
16  |  |  *  | 
17  |  |  * You should have received a copy of the GNU Lesser General  | 
18  |  |  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.  | 
19  |  |  */  | 
20  |  |  | 
21  |  | #include "config.h"  | 
22  |  | #include "gio_trace.h"  | 
23  |  |  | 
24  |  | #include "gtask.h"  | 
25  |  |  | 
26  |  | #include "gasyncresult.h"  | 
27  |  | #include "gcancellable.h"  | 
28  |  | #include "glib-private.h"  | 
29  |  | #include "gtrace-private.h"  | 
30  |  |  | 
31  |  | #include "glibintl.h"  | 
32  |  |  | 
33  |  | #include <string.h>  | 
34  |  |  | 
35  |  | /**  | 
36  |  |  * SECTION:gtask  | 
37  |  |  * @short_description: Cancellable synchronous or asynchronous task  | 
38  |  |  *     and result  | 
39  |  |  * @include: gio/gio.h  | 
40  |  |  * @see_also: #GAsyncResult  | 
41  |  |  *  | 
42  |  |  * A #GTask represents and manages a cancellable "task".  | 
43  |  |  *  | 
44  |  |  * ## Asynchronous operations  | 
45  |  |  *  | 
46  |  |  * The most common usage of #GTask is as a #GAsyncResult, to  | 
47  |  |  * manage data during an asynchronous operation. You call  | 
48  |  |  * g_task_new() in the "start" method, followed by  | 
49  |  |  * g_task_set_task_data() and the like if you need to keep some  | 
50  |  |  * additional data associated with the task, and then pass the  | 
51  |  |  * task object around through your asynchronous operation.  | 
52  |  |  * Eventually, you will call a method such as  | 
53  |  |  * g_task_return_pointer() or g_task_return_error(), which will  | 
54  |  |  * save the value you give it and then invoke the task's callback  | 
55  |  |  * function in the  | 
56  |  |  * [thread-default main context][g-main-context-push-thread-default]  | 
57  |  |  * where it was created (waiting until the next iteration of the main  | 
58  |  |  * loop first, if necessary). The caller will pass the #GTask back to  | 
59  |  |  * the operation's finish function (as a #GAsyncResult), and you can  | 
60  |  |  * use g_task_propagate_pointer() or the like to extract the  | 
61  |  |  * return value.  | 
62  |  |  *  | 
63  |  |  * Using #GTask requires the thread-default #GMainContext from when the  | 
64  |  |  * #GTask was constructed to be running at least until the task has completed  | 
65  |  |  * and its data has been freed.  | 
66  |  |  *  | 
67  |  |  * If a #GTask has been constructed and its callback set, it is an error to  | 
68  |  |  * not call `g_task_return_*()` on it. GLib will warn at runtime if this happens  | 
69  |  |  * (since 2.76).  | 
70  |  |  *  | 
71  |  |  * Here is an example for using GTask as a GAsyncResult:  | 
72  |  |  * |[<!-- language="C" -->  | 
73  |  |  *     typedef struct { | 
74  |  |  *       CakeFrostingType frosting;  | 
75  |  |  *       char *message;  | 
76  |  |  *     } DecorationData;  | 
77  |  |  *  | 
78  |  |  *     static void  | 
79  |  |  *     decoration_data_free (DecorationData *decoration)  | 
80  |  |  *     { | 
81  |  |  *       g_free (decoration->message);  | 
82  |  |  *       g_slice_free (DecorationData, decoration);  | 
83  |  |  *     }  | 
84  |  |  *  | 
85  |  |  *     static void  | 
86  |  |  *     baked_cb (Cake     *cake,  | 
87  |  |  *               gpointer  user_data)  | 
88  |  |  *     { | 
89  |  |  *       GTask *task = user_data;  | 
90  |  |  *       DecorationData *decoration = g_task_get_task_data (task);  | 
91  |  |  *       GError *error = NULL;  | 
92  |  |  *  | 
93  |  |  *       if (cake == NULL)  | 
94  |  |  *         { | 
95  |  |  *           g_task_return_new_error (task, BAKER_ERROR, BAKER_ERROR_NO_FLOUR,  | 
96  |  |  *                                    "Go to the supermarket");  | 
97  |  |  *           g_object_unref (task);  | 
98  |  |  *           return;  | 
99  |  |  *         }  | 
100  |  |  *  | 
101  |  |  *       if (!cake_decorate (cake, decoration->frosting, decoration->message, &error))  | 
102  |  |  *         { | 
103  |  |  *           g_object_unref (cake);  | 
104  |  |  *           // g_task_return_error() takes ownership of error  | 
105  |  |  *           g_task_return_error (task, error);  | 
106  |  |  *           g_object_unref (task);  | 
107  |  |  *           return;  | 
108  |  |  *         }  | 
109  |  |  *  | 
110  |  |  *       g_task_return_pointer (task, cake, g_object_unref);  | 
111  |  |  *       g_object_unref (task);  | 
112  |  |  *     }  | 
113  |  |  *  | 
114  |  |  *     void  | 
115  |  |  *     baker_bake_cake_async (Baker               *self,  | 
116  |  |  *                            guint                radius,  | 
117  |  |  *                            CakeFlavor           flavor,  | 
118  |  |  *                            CakeFrostingType     frosting,  | 
119  |  |  *                            const char          *message,  | 
120  |  |  *                            GCancellable        *cancellable,  | 
121  |  |  *                            GAsyncReadyCallback  callback,  | 
122  |  |  *                            gpointer             user_data)  | 
123  |  |  *     { | 
124  |  |  *       GTask *task;  | 
125  |  |  *       DecorationData *decoration;  | 
126  |  |  *       Cake  *cake;  | 
127  |  |  *  | 
128  |  |  *       task = g_task_new (self, cancellable, callback, user_data);  | 
129  |  |  *       if (radius < 3)  | 
130  |  |  *         { | 
131  |  |  *           g_task_return_new_error (task, BAKER_ERROR, BAKER_ERROR_TOO_SMALL,  | 
132  |  |  *                                    "%ucm radius cakes are silly",  | 
133  |  |  *                                    radius);  | 
134  |  |  *           g_object_unref (task);  | 
135  |  |  *           return;  | 
136  |  |  *         }  | 
137  |  |  *  | 
138  |  |  *       cake = _baker_get_cached_cake (self, radius, flavor, frosting, message);  | 
139  |  |  *       if (cake != NULL)  | 
140  |  |  *         { | 
141  |  |  *           // _baker_get_cached_cake() returns a reffed cake  | 
142  |  |  *           g_task_return_pointer (task, cake, g_object_unref);  | 
143  |  |  *           g_object_unref (task);  | 
144  |  |  *           return;  | 
145  |  |  *         }  | 
146  |  |  *  | 
147  |  |  *       decoration = g_slice_new (DecorationData);  | 
148  |  |  *       decoration->frosting = frosting;  | 
149  |  |  *       decoration->message = g_strdup (message);  | 
150  |  |  *       g_task_set_task_data (task, decoration, (GDestroyNotify) decoration_data_free);  | 
151  |  |  *  | 
152  |  |  *       _baker_begin_cake (self, radius, flavor, cancellable, baked_cb, task);  | 
153  |  |  *     }  | 
154  |  |  *  | 
155  |  |  *     Cake *  | 
156  |  |  *     baker_bake_cake_finish (Baker         *self,  | 
157  |  |  *                             GAsyncResult  *result,  | 
158  |  |  *                             GError       **error)  | 
159  |  |  *     { | 
160  |  |  *       g_return_val_if_fail (g_task_is_valid (result, self), NULL);  | 
161  |  |  *  | 
162  |  |  *       return g_task_propagate_pointer (G_TASK (result), error);  | 
163  |  |  *     }  | 
164  |  |  * ]|  | 
165  |  |  *  | 
166  |  |  * ## Chained asynchronous operations  | 
167  |  |  *  | 
168  |  |  * #GTask also tries to simplify asynchronous operations that  | 
169  |  |  * internally chain together several smaller asynchronous  | 
170  |  |  * operations. g_task_get_cancellable(), g_task_get_context(),  | 
171  |  |  * and g_task_get_priority() allow you to get back the task's  | 
172  |  |  * #GCancellable, #GMainContext, and [I/O priority][io-priority]  | 
173  |  |  * when starting a new subtask, so you don't have to keep track  | 
174  |  |  * of them yourself. g_task_attach_source() simplifies the case  | 
175  |  |  * of waiting for a source to fire (automatically using the correct  | 
176  |  |  * #GMainContext and priority).  | 
177  |  |  *  | 
178  |  |  * Here is an example for chained asynchronous operations:  | 
179  |  |  *   |[<!-- language="C" -->  | 
180  |  |  *     typedef struct { | 
181  |  |  *       Cake *cake;  | 
182  |  |  *       CakeFrostingType frosting;  | 
183  |  |  *       char *message;  | 
184  |  |  *     } BakingData;  | 
185  |  |  *  | 
186  |  |  *     static void  | 
187  |  |  *     decoration_data_free (BakingData *bd)  | 
188  |  |  *     { | 
189  |  |  *       if (bd->cake)  | 
190  |  |  *         g_object_unref (bd->cake);  | 
191  |  |  *       g_free (bd->message);  | 
192  |  |  *       g_slice_free (BakingData, bd);  | 
193  |  |  *     }  | 
194  |  |  *  | 
195  |  |  *     static void  | 
196  |  |  *     decorated_cb (Cake         *cake,  | 
197  |  |  *                   GAsyncResult *result,  | 
198  |  |  *                   gpointer      user_data)  | 
199  |  |  *     { | 
200  |  |  *       GTask *task = user_data;  | 
201  |  |  *       GError *error = NULL;  | 
202  |  |  *  | 
203  |  |  *       if (!cake_decorate_finish (cake, result, &error))  | 
204  |  |  *         { | 
205  |  |  *           g_object_unref (cake);  | 
206  |  |  *           g_task_return_error (task, error);  | 
207  |  |  *           g_object_unref (task);  | 
208  |  |  *           return;  | 
209  |  |  *         }  | 
210  |  |  *  | 
211  |  |  *       // baking_data_free() will drop its ref on the cake, so we have to  | 
212  |  |  *       // take another here to give to the caller.  | 
213  |  |  *       g_task_return_pointer (task, g_object_ref (cake), g_object_unref);  | 
214  |  |  *       g_object_unref (task);  | 
215  |  |  *     }  | 
216  |  |  *  | 
217  |  |  *     static gboolean  | 
218  |  |  *     decorator_ready (gpointer user_data)  | 
219  |  |  *     { | 
220  |  |  *       GTask *task = user_data;  | 
221  |  |  *       BakingData *bd = g_task_get_task_data (task);  | 
222  |  |  *  | 
223  |  |  *       cake_decorate_async (bd->cake, bd->frosting, bd->message,  | 
224  |  |  *                            g_task_get_cancellable (task),  | 
225  |  |  *                            decorated_cb, task);  | 
226  |  |  *  | 
227  |  |  *       return G_SOURCE_REMOVE;  | 
228  |  |  *     }  | 
229  |  |  *  | 
230  |  |  *     static void  | 
231  |  |  *     baked_cb (Cake     *cake,  | 
232  |  |  *               gpointer  user_data)  | 
233  |  |  *     { | 
234  |  |  *       GTask *task = user_data;  | 
235  |  |  *       BakingData *bd = g_task_get_task_data (task);  | 
236  |  |  *       GError *error = NULL;  | 
237  |  |  *  | 
238  |  |  *       if (cake == NULL)  | 
239  |  |  *         { | 
240  |  |  *           g_task_return_new_error (task, BAKER_ERROR, BAKER_ERROR_NO_FLOUR,  | 
241  |  |  *                                    "Go to the supermarket");  | 
242  |  |  *           g_object_unref (task);  | 
243  |  |  *           return;  | 
244  |  |  *         }  | 
245  |  |  *  | 
246  |  |  *       bd->cake = cake;  | 
247  |  |  *  | 
248  |  |  *       // Bail out now if the user has already cancelled  | 
249  |  |  *       if (g_task_return_error_if_cancelled (task))  | 
250  |  |  *         { | 
251  |  |  *           g_object_unref (task);  | 
252  |  |  *           return;  | 
253  |  |  *         }  | 
254  |  |  *  | 
255  |  |  *       if (cake_decorator_available (cake))  | 
256  |  |  *         decorator_ready (task);  | 
257  |  |  *       else  | 
258  |  |  *         { | 
259  |  |  *           GSource *source;  | 
260  |  |  *  | 
261  |  |  *           source = cake_decorator_wait_source_new (cake);  | 
262  |  |  *           // Attach @source to @task's GMainContext and have it call  | 
263  |  |  *           // decorator_ready() when it is ready.  | 
264  |  |  *           g_task_attach_source (task, source, decorator_ready);  | 
265  |  |  *           g_source_unref (source);  | 
266  |  |  *         }  | 
267  |  |  *     }  | 
268  |  |  *  | 
269  |  |  *     void  | 
270  |  |  *     baker_bake_cake_async (Baker               *self,  | 
271  |  |  *                            guint                radius,  | 
272  |  |  *                            CakeFlavor           flavor,  | 
273  |  |  *                            CakeFrostingType     frosting,  | 
274  |  |  *                            const char          *message,  | 
275  |  |  *                            gint                 priority,  | 
276  |  |  *                            GCancellable        *cancellable,  | 
277  |  |  *                            GAsyncReadyCallback  callback,  | 
278  |  |  *                            gpointer             user_data)  | 
279  |  |  *     { | 
280  |  |  *       GTask *task;  | 
281  |  |  *       BakingData *bd;  | 
282  |  |  *  | 
283  |  |  *       task = g_task_new (self, cancellable, callback, user_data);  | 
284  |  |  *       g_task_set_priority (task, priority);  | 
285  |  |  *  | 
286  |  |  *       bd = g_slice_new0 (BakingData);  | 
287  |  |  *       bd->frosting = frosting;  | 
288  |  |  *       bd->message = g_strdup (message);  | 
289  |  |  *       g_task_set_task_data (task, bd, (GDestroyNotify) baking_data_free);  | 
290  |  |  *  | 
291  |  |  *       _baker_begin_cake (self, radius, flavor, cancellable, baked_cb, task);  | 
292  |  |  *     }  | 
293  |  |  *  | 
294  |  |  *     Cake *  | 
295  |  |  *     baker_bake_cake_finish (Baker         *self,  | 
296  |  |  *                             GAsyncResult  *result,  | 
297  |  |  *                             GError       **error)  | 
298  |  |  *     { | 
299  |  |  *       g_return_val_if_fail (g_task_is_valid (result, self), NULL);  | 
300  |  |  *  | 
301  |  |  *       return g_task_propagate_pointer (G_TASK (result), error);  | 
302  |  |  *     }  | 
303  |  |  * ]|  | 
304  |  |  *  | 
305  |  |  * ## Asynchronous operations from synchronous ones  | 
306  |  |  *  | 
307  |  |  * You can use g_task_run_in_thread() to turn a synchronous  | 
308  |  |  * operation into an asynchronous one, by running it in a thread.  | 
309  |  |  * When it completes, the result will be dispatched to the  | 
310  |  |  * [thread-default main context][g-main-context-push-thread-default]  | 
311  |  |  * where the #GTask was created.  | 
312  |  |  *  | 
313  |  |  * Running a task in a thread:  | 
314  |  |  *   |[<!-- language="C" -->  | 
315  |  |  *     typedef struct { | 
316  |  |  *       guint radius;  | 
317  |  |  *       CakeFlavor flavor;  | 
318  |  |  *       CakeFrostingType frosting;  | 
319  |  |  *       char *message;  | 
320  |  |  *     } CakeData;  | 
321  |  |  *  | 
322  |  |  *     static void  | 
323  |  |  *     cake_data_free (CakeData *cake_data)  | 
324  |  |  *     { | 
325  |  |  *       g_free (cake_data->message);  | 
326  |  |  *       g_slice_free (CakeData, cake_data);  | 
327  |  |  *     }  | 
328  |  |  *  | 
329  |  |  *     static void  | 
330  |  |  *     bake_cake_thread (GTask         *task,  | 
331  |  |  *                       gpointer       source_object,  | 
332  |  |  *                       gpointer       task_data,  | 
333  |  |  *                       GCancellable  *cancellable)  | 
334  |  |  *     { | 
335  |  |  *       Baker *self = source_object;  | 
336  |  |  *       CakeData *cake_data = task_data;  | 
337  |  |  *       Cake *cake;  | 
338  |  |  *       GError *error = NULL;  | 
339  |  |  *  | 
340  |  |  *       cake = bake_cake (baker, cake_data->radius, cake_data->flavor,  | 
341  |  |  *                         cake_data->frosting, cake_data->message,  | 
342  |  |  *                         cancellable, &error);  | 
343  |  |  *       if (cake)  | 
344  |  |  *         g_task_return_pointer (task, cake, g_object_unref);  | 
345  |  |  *       else  | 
346  |  |  *         g_task_return_error (task, error);  | 
347  |  |  *     }  | 
348  |  |  *  | 
349  |  |  *     void  | 
350  |  |  *     baker_bake_cake_async (Baker               *self,  | 
351  |  |  *                            guint                radius,  | 
352  |  |  *                            CakeFlavor           flavor,  | 
353  |  |  *                            CakeFrostingType     frosting,  | 
354  |  |  *                            const char          *message,  | 
355  |  |  *                            GCancellable        *cancellable,  | 
356  |  |  *                            GAsyncReadyCallback  callback,  | 
357  |  |  *                            gpointer             user_data)  | 
358  |  |  *     { | 
359  |  |  *       CakeData *cake_data;  | 
360  |  |  *       GTask *task;  | 
361  |  |  *  | 
362  |  |  *       cake_data = g_slice_new (CakeData);  | 
363  |  |  *       cake_data->radius = radius;  | 
364  |  |  *       cake_data->flavor = flavor;  | 
365  |  |  *       cake_data->frosting = frosting;  | 
366  |  |  *       cake_data->message = g_strdup (message);  | 
367  |  |  *       task = g_task_new (self, cancellable, callback, user_data);  | 
368  |  |  *       g_task_set_task_data (task, cake_data, (GDestroyNotify) cake_data_free);  | 
369  |  |  *       g_task_run_in_thread (task, bake_cake_thread);  | 
370  |  |  *       g_object_unref (task);  | 
371  |  |  *     }  | 
372  |  |  *  | 
373  |  |  *     Cake *  | 
374  |  |  *     baker_bake_cake_finish (Baker         *self,  | 
375  |  |  *                             GAsyncResult  *result,  | 
376  |  |  *                             GError       **error)  | 
377  |  |  *     { | 
378  |  |  *       g_return_val_if_fail (g_task_is_valid (result, self), NULL);  | 
379  |  |  *  | 
380  |  |  *       return g_task_propagate_pointer (G_TASK (result), error);  | 
381  |  |  *     }  | 
382  |  |  * ]|  | 
383  |  |  *  | 
384  |  |  * ## Adding cancellability to uncancellable tasks  | 
385  |  |  *   | 
386  |  |  * Finally, g_task_run_in_thread() and g_task_run_in_thread_sync()  | 
387  |  |  * can be used to turn an uncancellable operation into a  | 
388  |  |  * cancellable one. If you call g_task_set_return_on_cancel(),  | 
389  |  |  * passing %TRUE, then if the task's #GCancellable is cancelled,  | 
390  |  |  * it will return control back to the caller immediately, while  | 
391  |  |  * allowing the task thread to continue running in the background  | 
392  |  |  * (and simply discarding its result when it finally does finish).  | 
393  |  |  * Provided that the task thread is careful about how it uses  | 
394  |  |  * locks and other externally-visible resources, this allows you  | 
395  |  |  * to make "GLib-friendly" asynchronous and cancellable  | 
396  |  |  * synchronous variants of blocking APIs.  | 
397  |  |  *  | 
398  |  |  * Cancelling a task:  | 
399  |  |  *   |[<!-- language="C" -->  | 
400  |  |  *     static void  | 
401  |  |  *     bake_cake_thread (GTask         *task,  | 
402  |  |  *                       gpointer       source_object,  | 
403  |  |  *                       gpointer       task_data,  | 
404  |  |  *                       GCancellable  *cancellable)  | 
405  |  |  *     { | 
406  |  |  *       Baker *self = source_object;  | 
407  |  |  *       CakeData *cake_data = task_data;  | 
408  |  |  *       Cake *cake;  | 
409  |  |  *       GError *error = NULL;  | 
410  |  |  *  | 
411  |  |  *       cake = bake_cake (baker, cake_data->radius, cake_data->flavor,  | 
412  |  |  *                         cake_data->frosting, cake_data->message,  | 
413  |  |  *                         &error);  | 
414  |  |  *       if (error)  | 
415  |  |  *         { | 
416  |  |  *           g_task_return_error (task, error);  | 
417  |  |  *           return;  | 
418  |  |  *         }  | 
419  |  |  *  | 
420  |  |  *       // If the task has already been cancelled, then we don't want to add  | 
421  |  |  *       // the cake to the cake cache. Likewise, we don't  want to have the  | 
422  |  |  *       // task get cancelled in the middle of updating the cache.  | 
423  |  |  *       // g_task_set_return_on_cancel() will return %TRUE here if it managed  | 
424  |  |  *       // to disable return-on-cancel, or %FALSE if the task was cancelled  | 
425  |  |  *       // before it could.  | 
426  |  |  *       if (g_task_set_return_on_cancel (task, FALSE))  | 
427  |  |  *         { | 
428  |  |  *           // If the caller cancels at this point, their  | 
429  |  |  *           // GAsyncReadyCallback won't be invoked until we return,  | 
430  |  |  *           // so we don't have to worry that this code will run at  | 
431  |  |  *           // the same time as that code does. But if there were  | 
432  |  |  *           // other functions that might look at the cake cache,  | 
433  |  |  *           // then we'd probably need a GMutex here as well.  | 
434  |  |  *           baker_add_cake_to_cache (baker, cake);  | 
435  |  |  *           g_task_return_pointer (task, cake, g_object_unref);  | 
436  |  |  *         }  | 
437  |  |  *     }  | 
438  |  |  *  | 
439  |  |  *     void  | 
440  |  |  *     baker_bake_cake_async (Baker               *self,  | 
441  |  |  *                            guint                radius,  | 
442  |  |  *                            CakeFlavor           flavor,  | 
443  |  |  *                            CakeFrostingType     frosting,  | 
444  |  |  *                            const char          *message,  | 
445  |  |  *                            GCancellable        *cancellable,  | 
446  |  |  *                            GAsyncReadyCallback  callback,  | 
447  |  |  *                            gpointer             user_data)  | 
448  |  |  *     { | 
449  |  |  *       CakeData *cake_data;  | 
450  |  |  *       GTask *task;  | 
451  |  |  *  | 
452  |  |  *       cake_data = g_slice_new (CakeData);  | 
453  |  |  *  | 
454  |  |  *       ...  | 
455  |  |  *  | 
456  |  |  *       task = g_task_new (self, cancellable, callback, user_data);  | 
457  |  |  *       g_task_set_task_data (task, cake_data, (GDestroyNotify) cake_data_free);  | 
458  |  |  *       g_task_set_return_on_cancel (task, TRUE);  | 
459  |  |  *       g_task_run_in_thread (task, bake_cake_thread);  | 
460  |  |  *     }  | 
461  |  |  *  | 
462  |  |  *     Cake *  | 
463  |  |  *     baker_bake_cake_sync (Baker               *self,  | 
464  |  |  *                           guint                radius,  | 
465  |  |  *                           CakeFlavor           flavor,  | 
466  |  |  *                           CakeFrostingType     frosting,  | 
467  |  |  *                           const char          *message,  | 
468  |  |  *                           GCancellable        *cancellable,  | 
469  |  |  *                           GError             **error)  | 
470  |  |  *     { | 
471  |  |  *       CakeData *cake_data;  | 
472  |  |  *       GTask *task;  | 
473  |  |  *       Cake *cake;  | 
474  |  |  *  | 
475  |  |  *       cake_data = g_slice_new (CakeData);  | 
476  |  |  *  | 
477  |  |  *       ...  | 
478  |  |  *  | 
479  |  |  *       task = g_task_new (self, cancellable, NULL, NULL);  | 
480  |  |  *       g_task_set_task_data (task, cake_data, (GDestroyNotify) cake_data_free);  | 
481  |  |  *       g_task_set_return_on_cancel (task, TRUE);  | 
482  |  |  *       g_task_run_in_thread_sync (task, bake_cake_thread);  | 
483  |  |  *  | 
484  |  |  *       cake = g_task_propagate_pointer (task, error);  | 
485  |  |  *       g_object_unref (task);  | 
486  |  |  *       return cake;  | 
487  |  |  *     }  | 
488  |  |  * ]|  | 
489  |  |  *  | 
490  |  |  * ## Porting from GSimpleAsyncResult  | 
491  |  |  *   | 
492  |  |  * #GTask's API attempts to be simpler than #GSimpleAsyncResult's  | 
493  |  |  * in several ways:  | 
494  |  |  * - You can save task-specific data with g_task_set_task_data(), and  | 
495  |  |  *   retrieve it later with g_task_get_task_data(). This replaces the  | 
496  |  |  *   abuse of g_simple_async_result_set_op_res_gpointer() for the same  | 
497  |  |  *   purpose with #GSimpleAsyncResult.  | 
498  |  |  * - In addition to the task data, #GTask also keeps track of the  | 
499  |  |  *   [priority][io-priority], #GCancellable, and  | 
500  |  |  *   #GMainContext associated with the task, so tasks that consist of  | 
501  |  |  *   a chain of simpler asynchronous operations will have easy access  | 
502  |  |  *   to those values when starting each sub-task.  | 
503  |  |  * - g_task_return_error_if_cancelled() provides simplified  | 
504  |  |  *   handling for cancellation. In addition, cancellation  | 
505  |  |  *   overrides any other #GTask return value by default, like  | 
506  |  |  *   #GSimpleAsyncResult does when  | 
507  |  |  *   g_simple_async_result_set_check_cancellable() is called.  | 
508  |  |  *   (You can use g_task_set_check_cancellable() to turn off that  | 
509  |  |  *   behavior.) On the other hand, g_task_run_in_thread()  | 
510  |  |  *   guarantees that it will always run your  | 
511  |  |  *   `task_func`, even if the task's #GCancellable  | 
512  |  |  *   is already cancelled before the task gets a chance to run;  | 
513  |  |  *   you can start your `task_func` with a  | 
514  |  |  *   g_task_return_error_if_cancelled() check if you need the  | 
515  |  |  *   old behavior.  | 
516  |  |  * - The "return" methods (eg, g_task_return_pointer())  | 
517  |  |  *   automatically cause the task to be "completed" as well, and  | 
518  |  |  *   there is no need to worry about the "complete" vs "complete  | 
519  |  |  *   in idle" distinction. (#GTask automatically figures out  | 
520  |  |  *   whether the task's callback can be invoked directly, or  | 
521  |  |  *   if it needs to be sent to another #GMainContext, or delayed  | 
522  |  |  *   until the next iteration of the current #GMainContext.)  | 
523  |  |  * - The "finish" functions for #GTask based operations are generally  | 
524  |  |  *   much simpler than #GSimpleAsyncResult ones, normally consisting  | 
525  |  |  *   of only a single call to g_task_propagate_pointer() or the like.  | 
526  |  |  *   Since g_task_propagate_pointer() "steals" the return value from  | 
527  |  |  *   the #GTask, it is not necessary to juggle pointers around to  | 
528  |  |  *   prevent it from being freed twice.  | 
529  |  |  * - With #GSimpleAsyncResult, it was common to call  | 
530  |  |  *   g_simple_async_result_propagate_error() from the  | 
531  |  |  *   `_finish()` wrapper function, and have  | 
532  |  |  *   virtual method implementations only deal with successful  | 
533  |  |  *   returns. This behavior is deprecated, because it makes it  | 
534  |  |  *   difficult for a subclass to chain to a parent class's async  | 
535  |  |  *   methods. Instead, the wrapper function should just be a  | 
536  |  |  *   simple wrapper, and the virtual method should call an  | 
537  |  |  *   appropriate `g_task_propagate_` function.  | 
538  |  |  *   Note that wrapper methods can now use  | 
539  |  |  *   g_async_result_legacy_propagate_error() to do old-style  | 
540  |  |  *   #GSimpleAsyncResult error-returning behavior, and  | 
541  |  |  *   g_async_result_is_tagged() to check if a result is tagged as  | 
542  |  |  *   having come from the `_async()` wrapper  | 
543  |  |  *   function (for "short-circuit" results, such as when passing  | 
544  |  |  *   0 to g_input_stream_read_async()).  | 
545  |  |  *  | 
546  |  |  * ## Thread-safety considerations  | 
547  |  |  *  | 
548  |  |  * Due to some infelicities in the API design, there is a  | 
549  |  |  * thread-safety concern that users of GTask have to be aware of:  | 
550  |  |  *  | 
551  |  |  * If the `main` thread drops its last reference to the source object  | 
552  |  |  * or the task data before the task is finalized, then the finalizers  | 
553  |  |  * of these objects may be called on the worker thread.  | 
554  |  |  *  | 
555  |  |  * This is a problem if the finalizers use non-threadsafe API, and  | 
556  |  |  * can lead to hard-to-debug crashes. Possible workarounds include:  | 
557  |  |  *  | 
558  |  |  * - Clear task data in a signal handler for `notify::completed`  | 
559  |  |  *  | 
560  |  |  * - Keep iterating a main context in the main thread and defer  | 
561  |  |  *   dropping the reference to the source object to that main  | 
562  |  |  *   context when the task is finalized  | 
563  |  |  */  | 
564  |  |  | 
565  |  | /**  | 
566  |  |  * GTask:  | 
567  |  |  *  | 
568  |  |  * The opaque object representing a synchronous or asynchronous task  | 
569  |  |  * and its result.  | 
570  |  |  */  | 
571  |  |  | 
572  |  | struct _GTask { | 
573  |  |   GObject parent_instance;  | 
574  |  |  | 
575  |  |   gpointer source_object;  | 
576  |  |   gpointer source_tag;  | 
577  |  |   gchar *name;  /* (owned); may only be modified before the #GTask is threaded */  | 
578  |  |  | 
579  |  |   gpointer task_data;  | 
580  |  |   GDestroyNotify task_data_destroy;  | 
581  |  |  | 
582  |  |   GMainContext *context;  | 
583  |  |   gint64 creation_time;  | 
584  |  |   gint priority;  | 
585  |  |   GCancellable *cancellable;  | 
586  |  |  | 
587  |  |   GAsyncReadyCallback callback;  | 
588  |  |   gpointer callback_data;  | 
589  |  |  | 
590  |  |   GTaskThreadFunc task_func;  | 
591  |  |   GMutex lock;  | 
592  |  |   GCond cond;  | 
593  |  |  | 
594  |  |   /* This can’t be in the bit field because we access it from TRACE(). */  | 
595  |  |   gboolean thread_cancelled;  | 
596  |  |  | 
597  |  |   /* Protected by the lock when task is threaded: */  | 
598  |  |   guint thread_complete : 1;  | 
599  |  |   guint return_on_cancel : 1;  | 
600  |  |   guint : 0;  | 
601  |  |  | 
602  |  |   /* Unprotected, but written to when task runs in thread: */  | 
603  |  |   guint completed : 1;  | 
604  |  |   guint had_error : 1;  | 
605  |  |   guint result_set : 1;  | 
606  |  |   guint ever_returned : 1;  | 
607  |  |   guint : 0;  | 
608  |  |  | 
609  |  |   /* Read-only once task runs in thread: */  | 
610  |  |   guint check_cancellable : 1;  | 
611  |  |   guint synchronous : 1;  | 
612  |  |   guint blocking_other_task : 1;  | 
613  |  |   guint name_is_static : 1;  | 
614  |  |  | 
615  |  |   GError *error;  | 
616  |  |   union { | 
617  |  |     gpointer pointer;  | 
618  |  |     gssize   size;  | 
619  |  |     gboolean boolean;  | 
620  |  |   } result;  | 
621  |  |   GDestroyNotify result_destroy;  | 
622  |  | };  | 
623  |  |  | 
624  | 0  | #define G_TASK_IS_THREADED(task) ((task)->task_func != NULL)  | 
625  |  |  | 
626  |  | struct _GTaskClass  | 
627  |  | { | 
628  |  |   GObjectClass parent_class;  | 
629  |  | };  | 
630  |  |  | 
631  |  | typedef enum  | 
632  |  | { | 
633  |  |   PROP_COMPLETED = 1,  | 
634  |  | } GTaskProperty;  | 
635  |  |  | 
636  |  | static void g_task_async_result_iface_init (GAsyncResultIface *iface);  | 
637  |  | static void g_task_thread_pool_init (void);  | 
638  |  |  | 
639  |  | G_DEFINE_TYPE_WITH_CODE (GTask, g_task, G_TYPE_OBJECT,  | 
640  |  |                          G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_RESULT,  | 
641  |  |                                                 g_task_async_result_iface_init);  | 
642  |  |                          g_task_thread_pool_init ();)  | 
643  |  |  | 
644  |  | static GThreadPool *task_pool;  | 
645  |  | static GMutex task_pool_mutex;  | 
646  |  | static GPrivate task_private = G_PRIVATE_INIT (NULL);  | 
647  |  | static GSource *task_pool_manager;  | 
648  |  | static guint64 task_wait_time;  | 
649  |  | static gint tasks_running;  | 
650  |  |  | 
651  |  | static guint task_pool_max_counter;  | 
652  |  | static guint tasks_running_counter;  | 
653  |  |  | 
654  |  | /* When the task pool fills up and blocks, and the program keeps  | 
655  |  |  * queueing more tasks, we will slowly add more threads to the pool  | 
656  |  |  * (in case the existing tasks are trying to queue subtasks of their  | 
657  |  |  * own) until tasks start completing again. These "overflow" threads  | 
658  |  |  * will only run one task apiece, and then exit, so the pool will  | 
659  |  |  * eventually get back down to its base size.  | 
660  |  |  *  | 
661  |  |  * The base and multiplier below gives us 10 extra threads after about  | 
662  |  |  * a second of blocking, 30 after 5 seconds, 100 after a minute, and  | 
663  |  |  * 200 after 20 minutes.  | 
664  |  |  *  | 
665  |  |  * We specify maximum pool size of 330 to increase the waiting time up  | 
666  |  |  * to around 30 minutes.  | 
667  |  |  */  | 
668  | 0  | #define G_TASK_POOL_SIZE 10  | 
669  | 0  | #define G_TASK_WAIT_TIME_BASE 100000  | 
670  | 0  | #define G_TASK_WAIT_TIME_MULTIPLIER 1.03  | 
671  | 0  | #define G_TASK_WAIT_TIME_MAX_POOL_SIZE 330  | 
672  |  |  | 
673  |  | static void  | 
674  |  | g_task_init (GTask *task)  | 
675  | 0  | { | 
676  | 0  |   task->check_cancellable = TRUE;  | 
677  | 0  | }  | 
678  |  |  | 
679  |  | #ifdef G_ENABLE_DEBUG  | 
680  |  | G_LOCK_DEFINE_STATIC (task_list);  | 
681  |  | static GPtrArray *task_list = NULL;  | 
682  |  |  | 
683  |  | void  | 
684  |  | g_task_print_alive_tasks (void)  | 
685  |  | { | 
686  |  |   GString *message_str = g_string_new (""); | 
687  |  |  | 
688  |  |   G_LOCK (task_list);  | 
689  |  |  | 
690  |  |   if (task_list != NULL)  | 
691  |  |     { | 
692  |  |       g_string_append_printf (message_str, "%u GTasks still alive:", task_list->len);  | 
693  |  |       for (guint i = 0; i < task_list->len; i++)  | 
694  |  |         { | 
695  |  |           GTask *task = g_ptr_array_index (task_list, i);  | 
696  |  |           const gchar *name = g_task_get_name (task);  | 
697  |  |           g_string_append_printf (message_str,  | 
698  |  |                                   "\n • GTask %p, %s, ref count: %u, ever_returned: %u, completed: %u",  | 
699  |  |                                   task, (name != NULL) ? name : "(no name set)",  | 
700  |  |                                   ((GObject *) task)->ref_count,  | 
701  |  |                                   task->ever_returned, task->completed);  | 
702  |  |         }  | 
703  |  |     }  | 
704  |  |   else  | 
705  |  |     { | 
706  |  |       g_string_append (message_str, "No GTasks still alive");  | 
707  |  |     }  | 
708  |  |  | 
709  |  |   G_UNLOCK (task_list);  | 
710  |  |  | 
711  |  |   g_message ("%s", message_str->str); | 
712  |  |   g_string_free (message_str, TRUE);  | 
713  |  | }  | 
714  |  |  | 
715  |  | static void  | 
716  |  | g_task_constructed (GObject *object)  | 
717  |  | { | 
718  |  |   GTask *task = G_TASK (object);  | 
719  |  |  | 
720  |  |   G_OBJECT_CLASS (g_task_parent_class)->constructed (object);  | 
721  |  |  | 
722  |  |   /* Track pending tasks for debugging purposes */  | 
723  |  |   G_LOCK (task_list);  | 
724  |  |   if (G_UNLIKELY (task_list == NULL))  | 
725  |  |     task_list = g_ptr_array_new ();  | 
726  |  |   g_ptr_array_add (task_list, task);  | 
727  |  |   G_UNLOCK (task_list);  | 
728  |  | }  | 
729  |  | #endif  /* G_ENABLE_DEBUG */  | 
730  |  |  | 
731  |  | static void  | 
732  |  | g_task_finalize (GObject *object)  | 
733  | 0  | { | 
734  | 0  |   GTask *task = G_TASK (object);  | 
735  |  |  | 
736  |  |   /* Warn if a #GTask is finalised without g_task_return() ever having been  | 
737  |  |    * called on it.  | 
738  |  |    *  | 
739  |  |    * Tasks without a callback or which are run in g_task_run_in_thread{,_sync}() | 
740  |  |    * only trigger a debug message as that’s sometimes used as a pattern for  | 
741  |  |    * running work in a worker thread without caring about the result. */  | 
742  | 0  |   if (!task->ever_returned)  | 
743  | 0  |     { | 
744  | 0  |       gchar *owned_task_name = NULL;  | 
745  | 0  |       const gchar *task_name = g_task_get_name (task);  | 
746  |  | 
  | 
747  | 0  |       if (task_name == NULL)  | 
748  | 0  |         task_name = owned_task_name = g_strdup_printf ("%p", task); | 
749  |  | 
  | 
750  | 0  |       if (task->callback != NULL && !G_TASK_IS_THREADED (task))  | 
751  | 0  |         g_critical ("GTask %s (source object: %p, source tag: %p) finalized without " | 
752  | 0  |                     "ever returning (using g_task_return_*()). This potentially "  | 
753  | 0  |                     "indicates a bug in the program.",  | 
754  | 0  |                     task_name, task->source_object, task->source_tag);  | 
755  | 0  |       else  | 
756  | 0  |         g_debug ("GTask %s (source object: %p, source tag: %p) finalized without " | 
757  | 0  |                  "ever returning (using g_task_return_*()). This potentially "  | 
758  | 0  |                  "indicates a bug in the program.",  | 
759  | 0  |                  task_name, task->source_object, task->source_tag);  | 
760  |  | 
  | 
761  | 0  |       g_free (owned_task_name);  | 
762  | 0  |     }  | 
763  |  | 
  | 
764  | 0  |   g_clear_object (&task->source_object);  | 
765  | 0  |   g_clear_object (&task->cancellable);  | 
766  | 0  |   if (!task->name_is_static)  | 
767  | 0  |     g_free (task->name);  | 
768  |  | 
  | 
769  | 0  |   if (task->context)  | 
770  | 0  |     g_main_context_unref (task->context);  | 
771  |  | 
  | 
772  | 0  |   if (task->task_data_destroy)  | 
773  | 0  |     task->task_data_destroy (task->task_data);  | 
774  |  | 
  | 
775  | 0  |   if (task->result_destroy && task->result.pointer)  | 
776  | 0  |     task->result_destroy (task->result.pointer);  | 
777  |  | 
  | 
778  | 0  |   if (task->error)  | 
779  | 0  |       g_error_free (task->error);  | 
780  |  | 
  | 
781  | 0  |   if (G_TASK_IS_THREADED (task))  | 
782  | 0  |     { | 
783  | 0  |       g_mutex_clear (&task->lock);  | 
784  | 0  |       g_cond_clear (&task->cond);  | 
785  | 0  |     }  | 
786  |  |  | 
787  |  |   /* Track pending tasks for debugging purposes */  | 
788  |  | #ifdef G_ENABLE_DEBUG  | 
789  |  |   G_LOCK (task_list);  | 
790  |  |   g_assert (task_list != NULL);  | 
791  |  |   g_ptr_array_remove_fast (task_list, task);  | 
792  |  |   if (G_UNLIKELY (task_list->len == 0))  | 
793  |  |     g_clear_pointer (&task_list, g_ptr_array_unref);  | 
794  |  |   G_UNLOCK (task_list);  | 
795  |  | #endif  /* G_ENABLE_DEBUG */  | 
796  |  | 
  | 
797  | 0  |   G_OBJECT_CLASS (g_task_parent_class)->finalize (object);  | 
798  | 0  | }  | 
799  |  |  | 
800  |  | /**  | 
801  |  |  * g_task_new:  | 
802  |  |  * @source_object: (nullable) (type GObject): the #GObject that owns  | 
803  |  |  *   this task, or %NULL.  | 
804  |  |  * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.  | 
805  |  |  * @callback: (scope async): a #GAsyncReadyCallback.  | 
806  |  |  * @callback_data: user data passed to @callback.  | 
807  |  |  *  | 
808  |  |  * Creates a #GTask acting on @source_object, which will eventually be  | 
809  |  |  * used to invoke @callback in the current  | 
810  |  |  * [thread-default main context][g-main-context-push-thread-default].  | 
811  |  |  *  | 
812  |  |  * Call this in the "start" method of your asynchronous method, and  | 
813  |  |  * pass the #GTask around throughout the asynchronous operation. You  | 
814  |  |  * can use g_task_set_task_data() to attach task-specific data to the  | 
815  |  |  * object, which you can retrieve later via g_task_get_task_data().  | 
816  |  |  *  | 
817  |  |  * By default, if @cancellable is cancelled, then the return value of  | 
818  |  |  * the task will always be %G_IO_ERROR_CANCELLED, even if the task had  | 
819  |  |  * already completed before the cancellation. This allows for  | 
820  |  |  * simplified handling in cases where cancellation may imply that  | 
821  |  |  * other objects that the task depends on have been destroyed. If you  | 
822  |  |  * do not want this behavior, you can use  | 
823  |  |  * g_task_set_check_cancellable() to change it.  | 
824  |  |  *  | 
825  |  |  * Returns: a #GTask.  | 
826  |  |  *  | 
827  |  |  * Since: 2.36  | 
828  |  |  */  | 
829  |  | GTask *  | 
830  |  | g_task_new (gpointer              source_object,  | 
831  |  |             GCancellable         *cancellable,  | 
832  |  |             GAsyncReadyCallback   callback,  | 
833  |  |             gpointer              callback_data)  | 
834  | 0  | { | 
835  | 0  |   GTask *task;  | 
836  | 0  |   GSource *source;  | 
837  |  | 
  | 
838  | 0  |   task = g_object_new (G_TYPE_TASK, NULL);  | 
839  | 0  |   task->source_object = source_object ? g_object_ref (source_object) : NULL;  | 
840  | 0  |   task->cancellable = cancellable ? g_object_ref (cancellable) : NULL;  | 
841  | 0  |   task->callback = callback;  | 
842  | 0  |   task->callback_data = callback_data;  | 
843  | 0  |   task->context = g_main_context_ref_thread_default ();  | 
844  |  | 
  | 
845  | 0  |   source = g_main_current_source ();  | 
846  | 0  |   if (source)  | 
847  | 0  |     task->creation_time = g_source_get_time (source);  | 
848  |  | 
  | 
849  | 0  |   TRACE (GIO_TASK_NEW (task, source_object, cancellable,  | 
850  | 0  |                        callback, callback_data));  | 
851  |  | 
  | 
852  | 0  |   return task;  | 
853  | 0  | }  | 
854  |  |  | 
855  |  | /**  | 
856  |  |  * g_task_report_error:  | 
857  |  |  * @source_object: (nullable) (type GObject): the #GObject that owns  | 
858  |  |  *   this task, or %NULL.  | 
859  |  |  * @callback: (scope async) (closure callback_data): a #GAsyncReadyCallback.  | 
860  |  |  * @callback_data: user data passed to @callback.  | 
861  |  |  * @source_tag: an opaque pointer indicating the source of this task  | 
862  |  |  * @error: (transfer full): error to report  | 
863  |  |  *  | 
864  |  |  * Creates a #GTask and then immediately calls g_task_return_error()  | 
865  |  |  * on it. Use this in the wrapper function of an asynchronous method  | 
866  |  |  * when you want to avoid even calling the virtual method. You can  | 
867  |  |  * then use g_async_result_is_tagged() in the finish method wrapper to  | 
868  |  |  * check if the result there is tagged as having been created by the  | 
869  |  |  * wrapper method, and deal with it appropriately if so.  | 
870  |  |  *  | 
871  |  |  * See also g_task_report_new_error().  | 
872  |  |  *  | 
873  |  |  * Since: 2.36  | 
874  |  |  */  | 
875  |  | void  | 
876  |  | g_task_report_error (gpointer             source_object,  | 
877  |  |                      GAsyncReadyCallback  callback,  | 
878  |  |                      gpointer             callback_data,  | 
879  |  |                      gpointer             source_tag,  | 
880  |  |                      GError              *error)  | 
881  | 0  | { | 
882  | 0  |   GTask *task;  | 
883  |  | 
  | 
884  | 0  |   task = g_task_new (source_object, NULL, callback, callback_data);  | 
885  | 0  |   g_task_set_source_tag (task, source_tag);  | 
886  | 0  |   g_task_set_static_name (task, G_STRFUNC);  | 
887  | 0  |   g_task_return_error (task, error);  | 
888  | 0  |   g_object_unref (task);  | 
889  | 0  | }  | 
890  |  |  | 
891  |  | /**  | 
892  |  |  * g_task_report_new_error:  | 
893  |  |  * @source_object: (nullable) (type GObject): the #GObject that owns  | 
894  |  |  *   this task, or %NULL.  | 
895  |  |  * @callback: (scope async) (closure callback_data): a #GAsyncReadyCallback.  | 
896  |  |  * @callback_data: user data passed to @callback.  | 
897  |  |  * @source_tag: an opaque pointer indicating the source of this task  | 
898  |  |  * @domain: a #GQuark.  | 
899  |  |  * @code: an error code.  | 
900  |  |  * @format: a string with format characters.  | 
901  |  |  * @...: a list of values to insert into @format.  | 
902  |  |  *  | 
903  |  |  * Creates a #GTask and then immediately calls  | 
904  |  |  * g_task_return_new_error() on it. Use this in the wrapper function  | 
905  |  |  * of an asynchronous method when you want to avoid even calling the  | 
906  |  |  * virtual method. You can then use g_async_result_is_tagged() in the  | 
907  |  |  * finish method wrapper to check if the result there is tagged as  | 
908  |  |  * having been created by the wrapper method, and deal with it  | 
909  |  |  * appropriately if so.  | 
910  |  |  *  | 
911  |  |  * See also g_task_report_error().  | 
912  |  |  *  | 
913  |  |  * Since: 2.36  | 
914  |  |  */  | 
915  |  | void  | 
916  |  | g_task_report_new_error (gpointer             source_object,  | 
917  |  |                          GAsyncReadyCallback  callback,  | 
918  |  |                          gpointer             callback_data,  | 
919  |  |                          gpointer             source_tag,  | 
920  |  |                          GQuark               domain,  | 
921  |  |                          gint                 code,  | 
922  |  |                          const char          *format,  | 
923  |  |                          ...)  | 
924  | 0  | { | 
925  | 0  |   GError *error;  | 
926  | 0  |   va_list ap;  | 
927  |  | 
  | 
928  | 0  |   va_start (ap, format);  | 
929  | 0  |   error = g_error_new_valist (domain, code, format, ap);  | 
930  | 0  |   va_end (ap);  | 
931  |  | 
  | 
932  | 0  |   g_task_report_error (source_object, callback, callback_data,  | 
933  | 0  |                        source_tag, error);  | 
934  | 0  | }  | 
935  |  |  | 
936  |  | /**  | 
937  |  |  * g_task_set_task_data:  | 
938  |  |  * @task: the #GTask  | 
939  |  |  * @task_data: (nullable): task-specific data  | 
940  |  |  * @task_data_destroy: (nullable): #GDestroyNotify for @task_data  | 
941  |  |  *  | 
942  |  |  * Sets @task's task data (freeing the existing task data, if any).  | 
943  |  |  *  | 
944  |  |  * Since: 2.36  | 
945  |  |  */  | 
946  |  | void  | 
947  |  | g_task_set_task_data (GTask          *task,  | 
948  |  |                       gpointer        task_data,  | 
949  |  |                       GDestroyNotify  task_data_destroy)  | 
950  | 0  | { | 
951  | 0  |   g_return_if_fail (G_IS_TASK (task));  | 
952  |  |  | 
953  | 0  |   if (task->task_data_destroy)  | 
954  | 0  |     task->task_data_destroy (task->task_data);  | 
955  |  | 
  | 
956  | 0  |   task->task_data = task_data;  | 
957  | 0  |   task->task_data_destroy = task_data_destroy;  | 
958  |  | 
  | 
959  | 0  |   TRACE (GIO_TASK_SET_TASK_DATA (task, task_data, task_data_destroy));  | 
960  | 0  | }  | 
961  |  |  | 
962  |  | /**  | 
963  |  |  * g_task_set_priority:  | 
964  |  |  * @task: the #GTask  | 
965  |  |  * @priority: the [priority][io-priority] of the request  | 
966  |  |  *  | 
967  |  |  * Sets @task's priority. If you do not call this, it will default to  | 
968  |  |  * %G_PRIORITY_DEFAULT.  | 
969  |  |  *  | 
970  |  |  * This will affect the priority of #GSources created with  | 
971  |  |  * g_task_attach_source() and the scheduling of tasks run in threads,  | 
972  |  |  * and can also be explicitly retrieved later via  | 
973  |  |  * g_task_get_priority().  | 
974  |  |  *  | 
975  |  |  * Since: 2.36  | 
976  |  |  */  | 
977  |  | void  | 
978  |  | g_task_set_priority (GTask *task,  | 
979  |  |                      gint   priority)  | 
980  | 0  | { | 
981  | 0  |   g_return_if_fail (G_IS_TASK (task));  | 
982  |  |  | 
983  | 0  |   task->priority = priority;  | 
984  |  | 
  | 
985  | 0  |   TRACE (GIO_TASK_SET_PRIORITY (task, priority));  | 
986  | 0  | }  | 
987  |  |  | 
988  |  | /**  | 
989  |  |  * g_task_set_check_cancellable:  | 
990  |  |  * @task: the #GTask  | 
991  |  |  * @check_cancellable: whether #GTask will check the state of  | 
992  |  |  *   its #GCancellable for you.  | 
993  |  |  *  | 
994  |  |  * Sets or clears @task's check-cancellable flag. If this is %TRUE  | 
995  |  |  * (the default), then g_task_propagate_pointer(), etc, and  | 
996  |  |  * g_task_had_error() will check the task's #GCancellable first, and  | 
997  |  |  * if it has been cancelled, then they will consider the task to have  | 
998  |  |  * returned an "Operation was cancelled" error  | 
999  |  |  * (%G_IO_ERROR_CANCELLED), regardless of any other error or return  | 
1000  |  |  * value the task may have had.  | 
1001  |  |  *  | 
1002  |  |  * If @check_cancellable is %FALSE, then the #GTask will not check the  | 
1003  |  |  * cancellable itself, and it is up to @task's owner to do this (eg,  | 
1004  |  |  * via g_task_return_error_if_cancelled()).  | 
1005  |  |  *  | 
1006  |  |  * If you are using g_task_set_return_on_cancel() as well, then  | 
1007  |  |  * you must leave check-cancellable set %TRUE.  | 
1008  |  |  *  | 
1009  |  |  * Since: 2.36  | 
1010  |  |  */  | 
1011  |  | void  | 
1012  |  | g_task_set_check_cancellable (GTask    *task,  | 
1013  |  |                               gboolean  check_cancellable)  | 
1014  | 0  | { | 
1015  | 0  |   g_return_if_fail (G_IS_TASK (task));  | 
1016  | 0  |   g_return_if_fail (check_cancellable || !task->return_on_cancel);  | 
1017  |  |  | 
1018  | 0  |   task->check_cancellable = check_cancellable;  | 
1019  | 0  | }  | 
1020  |  |  | 
1021  |  | static void g_task_thread_complete (GTask *task);  | 
1022  |  |  | 
1023  |  | /**  | 
1024  |  |  * g_task_set_return_on_cancel:  | 
1025  |  |  * @task: the #GTask  | 
1026  |  |  * @return_on_cancel: whether the task returns automatically when  | 
1027  |  |  *   it is cancelled.  | 
1028  |  |  *  | 
1029  |  |  * Sets or clears @task's return-on-cancel flag. This is only  | 
1030  |  |  * meaningful for tasks run via g_task_run_in_thread() or  | 
1031  |  |  * g_task_run_in_thread_sync().  | 
1032  |  |  *  | 
1033  |  |  * If @return_on_cancel is %TRUE, then cancelling @task's  | 
1034  |  |  * #GCancellable will immediately cause it to return, as though the  | 
1035  |  |  * task's #GTaskThreadFunc had called  | 
1036  |  |  * g_task_return_error_if_cancelled() and then returned.  | 
1037  |  |  *  | 
1038  |  |  * This allows you to create a cancellable wrapper around an  | 
1039  |  |  * uninterruptible function. The #GTaskThreadFunc just needs to be  | 
1040  |  |  * careful that it does not modify any externally-visible state after  | 
1041  |  |  * it has been cancelled. To do that, the thread should call  | 
1042  |  |  * g_task_set_return_on_cancel() again to (atomically) set  | 
1043  |  |  * return-on-cancel %FALSE before making externally-visible changes;  | 
1044  |  |  * if the task gets cancelled before the return-on-cancel flag could  | 
1045  |  |  * be changed, g_task_set_return_on_cancel() will indicate this by  | 
1046  |  |  * returning %FALSE.  | 
1047  |  |  *  | 
1048  |  |  * You can disable and re-enable this flag multiple times if you wish.  | 
1049  |  |  * If the task's #GCancellable is cancelled while return-on-cancel is  | 
1050  |  |  * %FALSE, then calling g_task_set_return_on_cancel() to set it %TRUE  | 
1051  |  |  * again will cause the task to be cancelled at that point.  | 
1052  |  |  *  | 
1053  |  |  * If the task's #GCancellable is already cancelled before you call  | 
1054  |  |  * g_task_run_in_thread()/g_task_run_in_thread_sync(), then the  | 
1055  |  |  * #GTaskThreadFunc will still be run (for consistency), but the task  | 
1056  |  |  * will also be completed right away.  | 
1057  |  |  *  | 
1058  |  |  * Returns: %TRUE if @task's return-on-cancel flag was changed to  | 
1059  |  |  *   match @return_on_cancel. %FALSE if @task has already been  | 
1060  |  |  *   cancelled.  | 
1061  |  |  *  | 
1062  |  |  * Since: 2.36  | 
1063  |  |  */  | 
1064  |  | gboolean  | 
1065  |  | g_task_set_return_on_cancel (GTask    *task,  | 
1066  |  |                              gboolean  return_on_cancel)  | 
1067  | 0  | { | 
1068  | 0  |   g_return_val_if_fail (G_IS_TASK (task), FALSE);  | 
1069  | 0  |   g_return_val_if_fail (task->check_cancellable || !return_on_cancel, FALSE);  | 
1070  |  |  | 
1071  | 0  |   if (!G_TASK_IS_THREADED (task))  | 
1072  | 0  |     { | 
1073  | 0  |       task->return_on_cancel = return_on_cancel;  | 
1074  | 0  |       return TRUE;  | 
1075  | 0  |     }  | 
1076  |  |  | 
1077  | 0  |   g_mutex_lock (&task->lock);  | 
1078  | 0  |   if (task->thread_cancelled)  | 
1079  | 0  |     { | 
1080  | 0  |       if (return_on_cancel && !task->return_on_cancel)  | 
1081  | 0  |         { | 
1082  | 0  |           g_mutex_unlock (&task->lock);  | 
1083  | 0  |           g_task_thread_complete (task);  | 
1084  | 0  |         }  | 
1085  | 0  |       else  | 
1086  | 0  |         g_mutex_unlock (&task->lock);  | 
1087  | 0  |       return FALSE;  | 
1088  | 0  |     }  | 
1089  | 0  |   task->return_on_cancel = return_on_cancel;  | 
1090  | 0  |   g_mutex_unlock (&task->lock);  | 
1091  |  | 
  | 
1092  | 0  |   return TRUE;  | 
1093  | 0  | }  | 
1094  |  |  | 
1095  |  | /**  | 
1096  |  |  * g_task_set_source_tag:  | 
1097  |  |  * @task: the #GTask  | 
1098  |  |  * @source_tag: an opaque pointer indicating the source of this task  | 
1099  |  |  *  | 
1100  |  |  * Sets @task's source tag.  | 
1101  |  |  *  | 
1102  |  |  * You can use this to tag a task return  | 
1103  |  |  * value with a particular pointer (usually a pointer to the function  | 
1104  |  |  * doing the tagging) and then later check it using  | 
1105  |  |  * g_task_get_source_tag() (or g_async_result_is_tagged()) in the  | 
1106  |  |  * task's "finish" function, to figure out if the response came from a  | 
1107  |  |  * particular place.  | 
1108  |  |  *  | 
1109  |  |  * A macro wrapper around this function will automatically set the  | 
1110  |  |  * task’s name to the string form of @source_tag if it’s not already  | 
1111  |  |  * set, for convenience.  | 
1112  |  |  *  | 
1113  |  |  * Since: 2.36  | 
1114  |  |  */  | 
1115  |  | void  | 
1116  |  | (g_task_set_source_tag) (GTask    *task,  | 
1117  |  |                          gpointer  source_tag)  | 
1118  | 0  | { | 
1119  | 0  |   g_return_if_fail (G_IS_TASK (task));  | 
1120  |  |  | 
1121  | 0  |   task->source_tag = source_tag;  | 
1122  |  | 
  | 
1123  | 0  |   TRACE (GIO_TASK_SET_SOURCE_TAG (task, source_tag));  | 
1124  | 0  | }  | 
1125  |  |  | 
1126  |  | /**  | 
1127  |  |  * g_task_set_name:  | 
1128  |  |  * @task: a #GTask  | 
1129  |  |  * @name: (nullable): a human readable name for the task, or %NULL to unset it  | 
1130  |  |  *  | 
1131  |  |  * Sets @task’s name, used in debugging and profiling. The name defaults to  | 
1132  |  |  * %NULL.  | 
1133  |  |  *  | 
1134  |  |  * The task name should describe in a human readable way what the task does.  | 
1135  |  |  * For example, ‘Open file’ or ‘Connect to network host’. It is used to set the  | 
1136  |  |  * name of the #GSource used for idle completion of the task.  | 
1137  |  |  *  | 
1138  |  |  * This function may only be called before the @task is first used in a thread  | 
1139  |  |  * other than the one it was constructed in. It is called automatically by  | 
1140  |  |  * g_task_set_source_tag() if not called already.  | 
1141  |  |  *  | 
1142  |  |  * Since: 2.60  | 
1143  |  |  */  | 
1144  |  | void  | 
1145  |  | (g_task_set_name) (GTask      *task,  | 
1146  |  |                    const char *name)  | 
1147  | 0  | { | 
1148  | 0  |   char *new_name;  | 
1149  |  | 
  | 
1150  | 0  |   g_return_if_fail (G_IS_TASK (task));  | 
1151  |  |  | 
1152  | 0  |   new_name = g_strdup (name);  | 
1153  | 0  |   if (!task->name_is_static)  | 
1154  | 0  |     g_free (task->name);  | 
1155  | 0  |   task->name = g_steal_pointer (&new_name);  | 
1156  | 0  |   task->name_is_static = FALSE;  | 
1157  | 0  | }  | 
1158  |  |  | 
1159  |  | /**  | 
1160  |  |  * g_task_set_static_name:  | 
1161  |  |  * @task: a #GTask  | 
1162  |  |  * @name: (nullable): a human readable name for the task. Must be a string literal  | 
1163  |  |  *  | 
1164  |  |  * Sets @task’s name, used in debugging and profiling.  | 
1165  |  |  *  | 
1166  |  |  * This is a variant of g_task_set_name() that avoids copying @name.  | 
1167  |  |  *  | 
1168  |  |  * Since: 2.76  | 
1169  |  |  */  | 
1170  |  | void  | 
1171  |  | g_task_set_static_name (GTask      *task,  | 
1172  |  |                         const char *name)  | 
1173  | 0  | { | 
1174  | 0  |   g_return_if_fail (G_IS_TASK (task));  | 
1175  |  |  | 
1176  | 0  |   if (!task->name_is_static)  | 
1177  | 0  |     g_free (task->name);  | 
1178  | 0  |   task->name = (char *) name;  | 
1179  | 0  |   task->name_is_static = TRUE;  | 
1180  | 0  | }  | 
1181  |  |  | 
1182  |  | /**  | 
1183  |  |  * g_task_get_source_object:  | 
1184  |  |  * @task: a #GTask  | 
1185  |  |  *  | 
1186  |  |  * Gets the source object from @task. Like  | 
1187  |  |  * g_async_result_get_source_object(), but does not ref the object.  | 
1188  |  |  *  | 
1189  |  |  * Returns: (transfer none) (nullable) (type GObject): @task's source object, or %NULL  | 
1190  |  |  *  | 
1191  |  |  * Since: 2.36  | 
1192  |  |  */  | 
1193  |  | gpointer  | 
1194  |  | g_task_get_source_object (GTask *task)  | 
1195  | 0  | { | 
1196  | 0  |   g_return_val_if_fail (G_IS_TASK (task), NULL);  | 
1197  |  |  | 
1198  | 0  |   return task->source_object;  | 
1199  | 0  | }  | 
1200  |  |  | 
1201  |  | static GObject *  | 
1202  |  | g_task_ref_source_object (GAsyncResult *res)  | 
1203  | 0  | { | 
1204  | 0  |   GTask *task = G_TASK (res);  | 
1205  |  | 
  | 
1206  | 0  |   if (task->source_object)  | 
1207  | 0  |     return g_object_ref (task->source_object);  | 
1208  | 0  |   else  | 
1209  | 0  |     return NULL;  | 
1210  | 0  | }  | 
1211  |  |  | 
1212  |  | /**  | 
1213  |  |  * g_task_get_task_data:  | 
1214  |  |  * @task: a #GTask  | 
1215  |  |  *  | 
1216  |  |  * Gets @task's `task_data`.  | 
1217  |  |  *  | 
1218  |  |  * Returns: (transfer none): @task's `task_data`.  | 
1219  |  |  *  | 
1220  |  |  * Since: 2.36  | 
1221  |  |  */  | 
1222  |  | gpointer  | 
1223  |  | g_task_get_task_data (GTask *task)  | 
1224  | 0  | { | 
1225  | 0  |   g_return_val_if_fail (G_IS_TASK (task), NULL);  | 
1226  |  |  | 
1227  | 0  |   return task->task_data;  | 
1228  | 0  | }  | 
1229  |  |  | 
1230  |  | /**  | 
1231  |  |  * g_task_get_priority:  | 
1232  |  |  * @task: a #GTask  | 
1233  |  |  *  | 
1234  |  |  * Gets @task's priority  | 
1235  |  |  *  | 
1236  |  |  * Returns: @task's priority  | 
1237  |  |  *  | 
1238  |  |  * Since: 2.36  | 
1239  |  |  */  | 
1240  |  | gint  | 
1241  |  | g_task_get_priority (GTask *task)  | 
1242  | 0  | { | 
1243  | 0  |   g_return_val_if_fail (G_IS_TASK (task), G_PRIORITY_DEFAULT);  | 
1244  |  |  | 
1245  | 0  |   return task->priority;  | 
1246  | 0  | }  | 
1247  |  |  | 
1248  |  | /**  | 
1249  |  |  * g_task_get_context:  | 
1250  |  |  * @task: a #GTask  | 
1251  |  |  *  | 
1252  |  |  * Gets the #GMainContext that @task will return its result in (that  | 
1253  |  |  * is, the context that was the  | 
1254  |  |  * [thread-default main context][g-main-context-push-thread-default]  | 
1255  |  |  * at the point when @task was created).  | 
1256  |  |  *  | 
1257  |  |  * This will always return a non-%NULL value, even if the task's  | 
1258  |  |  * context is the default #GMainContext.  | 
1259  |  |  *  | 
1260  |  |  * Returns: (transfer none): @task's #GMainContext  | 
1261  |  |  *  | 
1262  |  |  * Since: 2.36  | 
1263  |  |  */  | 
1264  |  | GMainContext *  | 
1265  |  | g_task_get_context (GTask *task)  | 
1266  | 0  | { | 
1267  | 0  |   g_return_val_if_fail (G_IS_TASK (task), NULL);  | 
1268  |  |  | 
1269  | 0  |   return task->context;  | 
1270  | 0  | }  | 
1271  |  |  | 
1272  |  | /**  | 
1273  |  |  * g_task_get_cancellable:  | 
1274  |  |  * @task: a #GTask  | 
1275  |  |  *  | 
1276  |  |  * Gets @task's #GCancellable  | 
1277  |  |  *  | 
1278  |  |  * Returns: (nullable) (transfer none): @task's #GCancellable  | 
1279  |  |  *  | 
1280  |  |  * Since: 2.36  | 
1281  |  |  */  | 
1282  |  | GCancellable *  | 
1283  |  | g_task_get_cancellable (GTask *task)  | 
1284  | 0  | { | 
1285  | 0  |   g_return_val_if_fail (G_IS_TASK (task), NULL);  | 
1286  |  |  | 
1287  | 0  |   return task->cancellable;  | 
1288  | 0  | }  | 
1289  |  |  | 
1290  |  | /**  | 
1291  |  |  * g_task_get_check_cancellable:  | 
1292  |  |  * @task: the #GTask  | 
1293  |  |  *  | 
1294  |  |  * Gets @task's check-cancellable flag. See  | 
1295  |  |  * g_task_set_check_cancellable() for more details.  | 
1296  |  |  *  | 
1297  |  |  * Since: 2.36  | 
1298  |  |  */  | 
1299  |  | gboolean  | 
1300  |  | g_task_get_check_cancellable (GTask *task)  | 
1301  | 0  | { | 
1302  | 0  |   g_return_val_if_fail (G_IS_TASK (task), FALSE);  | 
1303  |  |  | 
1304  |  |   /* Convert from a bit field to a boolean. */  | 
1305  | 0  |   return task->check_cancellable ? TRUE : FALSE;  | 
1306  | 0  | }  | 
1307  |  |  | 
1308  |  | /**  | 
1309  |  |  * g_task_get_return_on_cancel:  | 
1310  |  |  * @task: the #GTask  | 
1311  |  |  *  | 
1312  |  |  * Gets @task's return-on-cancel flag. See  | 
1313  |  |  * g_task_set_return_on_cancel() for more details.  | 
1314  |  |  *  | 
1315  |  |  * Since: 2.36  | 
1316  |  |  */  | 
1317  |  | gboolean  | 
1318  |  | g_task_get_return_on_cancel (GTask *task)  | 
1319  | 0  | { | 
1320  | 0  |   g_return_val_if_fail (G_IS_TASK (task), FALSE);  | 
1321  |  |  | 
1322  |  |   /* Convert from a bit field to a boolean. */  | 
1323  | 0  |   return task->return_on_cancel ? TRUE : FALSE;  | 
1324  | 0  | }  | 
1325  |  |  | 
1326  |  | /**  | 
1327  |  |  * g_task_get_source_tag:  | 
1328  |  |  * @task: a #GTask  | 
1329  |  |  *  | 
1330  |  |  * Gets @task's source tag. See g_task_set_source_tag().  | 
1331  |  |  *  | 
1332  |  |  * Returns: (transfer none): @task's source tag  | 
1333  |  |  *  | 
1334  |  |  * Since: 2.36  | 
1335  |  |  */  | 
1336  |  | gpointer  | 
1337  |  | g_task_get_source_tag (GTask *task)  | 
1338  | 0  | { | 
1339  | 0  |   g_return_val_if_fail (G_IS_TASK (task), NULL);  | 
1340  |  |  | 
1341  | 0  |   return task->source_tag;  | 
1342  | 0  | }  | 
1343  |  |  | 
1344  |  | /**  | 
1345  |  |  * g_task_get_name:  | 
1346  |  |  * @task: a #GTask  | 
1347  |  |  *  | 
1348  |  |  * Gets @task’s name. See g_task_set_name().  | 
1349  |  |  *  | 
1350  |  |  * Returns: (nullable) (transfer none): @task’s name, or %NULL  | 
1351  |  |  * Since: 2.60  | 
1352  |  |  */  | 
1353  |  | const gchar *  | 
1354  |  | g_task_get_name (GTask *task)  | 
1355  | 0  | { | 
1356  | 0  |   g_return_val_if_fail (G_IS_TASK (task), NULL);  | 
1357  |  |  | 
1358  | 0  |   return task->name;  | 
1359  | 0  | }  | 
1360  |  |  | 
1361  |  | static void  | 
1362  |  | g_task_return_now (GTask *task)  | 
1363  | 0  | { | 
1364  | 0  |   TRACE (GIO_TASK_BEFORE_RETURN (task, task->source_object, task->callback,  | 
1365  | 0  |                                  task->callback_data));  | 
1366  |  | 
  | 
1367  | 0  |   g_main_context_push_thread_default (task->context);  | 
1368  |  | 
  | 
1369  | 0  |   if (task->callback != NULL)  | 
1370  | 0  |     { | 
1371  | 0  |       task->callback (task->source_object,  | 
1372  | 0  |                       G_ASYNC_RESULT (task),  | 
1373  | 0  |                       task->callback_data);  | 
1374  | 0  |     }  | 
1375  |  | 
  | 
1376  | 0  |   task->completed = TRUE;  | 
1377  | 0  |   g_object_notify (G_OBJECT (task), "completed");  | 
1378  |  | 
  | 
1379  | 0  |   g_main_context_pop_thread_default (task->context);  | 
1380  | 0  | }  | 
1381  |  |  | 
1382  |  | static gboolean  | 
1383  |  | complete_in_idle_cb (gpointer task)  | 
1384  | 0  | { | 
1385  | 0  |   g_task_return_now (task);  | 
1386  | 0  |   g_object_unref (task);  | 
1387  | 0  |   return FALSE;  | 
1388  | 0  | }  | 
1389  |  |  | 
1390  |  | typedef enum { | 
1391  |  |   G_TASK_RETURN_SUCCESS,  | 
1392  |  |   G_TASK_RETURN_ERROR,  | 
1393  |  |   G_TASK_RETURN_FROM_THREAD  | 
1394  |  | } GTaskReturnType;  | 
1395  |  |  | 
1396  |  | static void  | 
1397  |  | g_task_return (GTask           *task,  | 
1398  |  |                GTaskReturnType  type)  | 
1399  | 0  | { | 
1400  | 0  |   GSource *source;  | 
1401  |  | 
  | 
1402  | 0  |   if (type != G_TASK_RETURN_FROM_THREAD)  | 
1403  | 0  |     task->ever_returned = TRUE;  | 
1404  |  | 
  | 
1405  | 0  |   if (type == G_TASK_RETURN_SUCCESS)  | 
1406  | 0  |     task->result_set = TRUE;  | 
1407  |  | 
  | 
1408  | 0  |   if (task->synchronous)  | 
1409  | 0  |     return;  | 
1410  |  |  | 
1411  |  |   /* Normally we want to invoke the task's callback when its return  | 
1412  |  |    * value is set. But if the task is running in a thread, then we  | 
1413  |  |    * want to wait until after the task_func returns, to simplify  | 
1414  |  |    * locking/refcounting/etc.  | 
1415  |  |    */  | 
1416  | 0  |   if (G_TASK_IS_THREADED (task) && type != G_TASK_RETURN_FROM_THREAD)  | 
1417  | 0  |     return;  | 
1418  |  |  | 
1419  | 0  |   g_object_ref (task);  | 
1420  |  |  | 
1421  |  |   /* See if we can complete the task immediately. First, we have to be  | 
1422  |  |    * running inside the task's thread/GMainContext.  | 
1423  |  |    */  | 
1424  | 0  |   source = g_main_current_source ();  | 
1425  | 0  |   if (source && g_source_get_context (source) == task->context)  | 
1426  | 0  |     { | 
1427  |  |       /* Second, we can only complete immediately if this is not the  | 
1428  |  |        * same iteration of the main loop that the task was created in.  | 
1429  |  |        */  | 
1430  | 0  |       if (g_source_get_time (source) > task->creation_time)  | 
1431  | 0  |         { | 
1432  |  |           /* Finally, if the task has been cancelled, we shouldn't  | 
1433  |  |            * return synchronously from inside the  | 
1434  |  |            * GCancellable::cancelled handler. It's easier to run  | 
1435  |  |            * another iteration of the main loop than tracking how the  | 
1436  |  |            * cancellation was handled.  | 
1437  |  |            */  | 
1438  | 0  |           if (!g_cancellable_is_cancelled (task->cancellable))  | 
1439  | 0  |             { | 
1440  | 0  |               g_task_return_now (task);  | 
1441  | 0  |               g_object_unref (task);  | 
1442  | 0  |               return;  | 
1443  | 0  |             }  | 
1444  | 0  |         }  | 
1445  | 0  |     }  | 
1446  |  |  | 
1447  |  |   /* Otherwise, complete in the next iteration */  | 
1448  | 0  |   source = g_idle_source_new ();  | 
1449  |  |  | 
1450  |  |   /* Note: in case the task name is NULL we set it as a const string instead  | 
1451  |  |    * of going through the concat path which is more expensive and may show in the  | 
1452  |  |    * profiler if g_task_return is called very often  | 
1453  |  |    */  | 
1454  | 0  |   if (task->name == NULL)  | 
1455  | 0  |     g_source_set_static_name (source, "[gio] (unnamed) complete_in_idle_cb");  | 
1456  | 0  |   else  | 
1457  | 0  |     { | 
1458  | 0  |       gchar *source_name;  | 
1459  |  | 
  | 
1460  | 0  |       source_name = g_strconcat ("[gio] ", task->name, " complete_in_idle_cb", NULL); | 
1461  | 0  |       g_source_set_name (source, source_name);  | 
1462  | 0  |       g_free (source_name);  | 
1463  | 0  |     }  | 
1464  |  | 
  | 
1465  | 0  |   g_task_attach_source (task, source, complete_in_idle_cb);  | 
1466  | 0  |   g_source_unref (source);  | 
1467  | 0  | }  | 
1468  |  |  | 
1469  |  |  | 
1470  |  | /**  | 
1471  |  |  * GTaskThreadFunc:  | 
1472  |  |  * @task: the #GTask  | 
1473  |  |  * @source_object: (type GObject): @task's source object  | 
1474  |  |  * @task_data: @task's task data  | 
1475  |  |  * @cancellable: @task's #GCancellable, or %NULL  | 
1476  |  |  *  | 
1477  |  |  * The prototype for a task function to be run in a thread via  | 
1478  |  |  * g_task_run_in_thread() or g_task_run_in_thread_sync().  | 
1479  |  |  *  | 
1480  |  |  * If the return-on-cancel flag is set on @task, and @cancellable gets  | 
1481  |  |  * cancelled, then the #GTask will be completed immediately (as though  | 
1482  |  |  * g_task_return_error_if_cancelled() had been called), without  | 
1483  |  |  * waiting for the task function to complete. However, the task  | 
1484  |  |  * function will continue running in its thread in the background. The  | 
1485  |  |  * function therefore needs to be careful about how it uses  | 
1486  |  |  * externally-visible state in this case. See  | 
1487  |  |  * g_task_set_return_on_cancel() for more details.  | 
1488  |  |  *  | 
1489  |  |  * Other than in that case, @task will be completed when the  | 
1490  |  |  * #GTaskThreadFunc returns, not when it calls a  | 
1491  |  |  * `g_task_return_` function.  | 
1492  |  |  *  | 
1493  |  |  * Since: 2.36  | 
1494  |  |  */  | 
1495  |  |  | 
1496  |  | static void task_thread_cancelled (GCancellable *cancellable,  | 
1497  |  |                                    gpointer      user_data);  | 
1498  |  |  | 
1499  |  | static void  | 
1500  |  | g_task_thread_complete (GTask *task)  | 
1501  | 0  | { | 
1502  | 0  |   g_mutex_lock (&task->lock);  | 
1503  | 0  |   if (task->thread_complete)  | 
1504  | 0  |     { | 
1505  |  |       /* The task belatedly completed after having been cancelled  | 
1506  |  |        * (or was cancelled in the midst of being completed).  | 
1507  |  |        */  | 
1508  | 0  |       g_mutex_unlock (&task->lock);  | 
1509  | 0  |       return;  | 
1510  | 0  |     }  | 
1511  |  |  | 
1512  | 0  |   TRACE (GIO_TASK_AFTER_RUN_IN_THREAD (task, task->thread_cancelled));  | 
1513  |  | 
  | 
1514  | 0  |   task->thread_complete = TRUE;  | 
1515  | 0  |   g_mutex_unlock (&task->lock);  | 
1516  |  | 
  | 
1517  | 0  |   if (task->cancellable)  | 
1518  | 0  |     g_signal_handlers_disconnect_by_func (task->cancellable, task_thread_cancelled, task);  | 
1519  |  | 
  | 
1520  | 0  |   if (task->synchronous)  | 
1521  | 0  |     g_cond_signal (&task->cond);  | 
1522  | 0  |   else  | 
1523  | 0  |     g_task_return (task, G_TASK_RETURN_FROM_THREAD);  | 
1524  | 0  | }  | 
1525  |  |  | 
1526  |  | static gboolean  | 
1527  |  | task_pool_manager_timeout (gpointer user_data)  | 
1528  | 0  | { | 
1529  | 0  |   g_mutex_lock (&task_pool_mutex);  | 
1530  | 0  |   g_thread_pool_set_max_threads (task_pool, tasks_running + 1, NULL);  | 
1531  | 0  |   g_trace_set_int64_counter (task_pool_max_counter, tasks_running + 1);  | 
1532  | 0  |   g_source_set_ready_time (task_pool_manager, -1);  | 
1533  | 0  |   g_mutex_unlock (&task_pool_mutex);  | 
1534  |  | 
  | 
1535  | 0  |   return TRUE;  | 
1536  | 0  | }  | 
1537  |  |  | 
1538  |  | static void  | 
1539  |  | g_task_thread_setup (void)  | 
1540  | 0  | { | 
1541  | 0  |   g_private_set (&task_private, GUINT_TO_POINTER (TRUE));  | 
1542  | 0  |   g_mutex_lock (&task_pool_mutex);  | 
1543  | 0  |   tasks_running++;  | 
1544  |  | 
  | 
1545  | 0  |   g_trace_set_int64_counter (tasks_running_counter, tasks_running);  | 
1546  |  | 
  | 
1547  | 0  |   if (tasks_running == G_TASK_POOL_SIZE)  | 
1548  | 0  |     task_wait_time = G_TASK_WAIT_TIME_BASE;  | 
1549  | 0  |   else if (tasks_running > G_TASK_POOL_SIZE && tasks_running < G_TASK_WAIT_TIME_MAX_POOL_SIZE)  | 
1550  | 0  |     task_wait_time *= G_TASK_WAIT_TIME_MULTIPLIER;  | 
1551  |  | 
  | 
1552  | 0  |   if (tasks_running >= G_TASK_POOL_SIZE)  | 
1553  | 0  |     g_source_set_ready_time (task_pool_manager, g_get_monotonic_time () + task_wait_time);  | 
1554  |  | 
  | 
1555  | 0  |   g_mutex_unlock (&task_pool_mutex);  | 
1556  | 0  | }  | 
1557  |  |  | 
1558  |  | static void  | 
1559  |  | g_task_thread_cleanup (void)  | 
1560  | 0  | { | 
1561  | 0  |   gint tasks_pending;  | 
1562  |  | 
  | 
1563  | 0  |   g_mutex_lock (&task_pool_mutex);  | 
1564  | 0  |   tasks_pending = g_thread_pool_unprocessed (task_pool);  | 
1565  |  | 
  | 
1566  | 0  |   if (tasks_running > G_TASK_POOL_SIZE)  | 
1567  | 0  |     { | 
1568  | 0  |       g_thread_pool_set_max_threads (task_pool, tasks_running - 1, NULL);  | 
1569  | 0  |       g_trace_set_int64_counter (task_pool_max_counter, tasks_running - 1);  | 
1570  | 0  |     }  | 
1571  | 0  |   else if (tasks_running + tasks_pending < G_TASK_POOL_SIZE)  | 
1572  | 0  |     g_source_set_ready_time (task_pool_manager, -1);  | 
1573  |  | 
  | 
1574  | 0  |   if (tasks_running > G_TASK_POOL_SIZE && tasks_running < G_TASK_WAIT_TIME_MAX_POOL_SIZE)  | 
1575  | 0  |     task_wait_time /= G_TASK_WAIT_TIME_MULTIPLIER;  | 
1576  |  | 
  | 
1577  | 0  |   tasks_running--;  | 
1578  |  | 
  | 
1579  | 0  |   g_trace_set_int64_counter (tasks_running_counter, tasks_running);  | 
1580  |  | 
  | 
1581  | 0  |   g_mutex_unlock (&task_pool_mutex);  | 
1582  | 0  |   g_private_set (&task_private, GUINT_TO_POINTER (FALSE));  | 
1583  | 0  | }  | 
1584  |  |  | 
1585  |  | static void  | 
1586  |  | g_task_thread_pool_thread (gpointer thread_data,  | 
1587  |  |                            gpointer pool_data)  | 
1588  | 0  | { | 
1589  | 0  |   GTask *task = thread_data;  | 
1590  |  | 
  | 
1591  | 0  |   g_task_thread_setup ();  | 
1592  |  | 
  | 
1593  | 0  |   task->task_func (task, task->source_object, task->task_data,  | 
1594  | 0  |                    task->cancellable);  | 
1595  | 0  |   g_task_thread_complete (task);  | 
1596  | 0  |   g_object_unref (task);  | 
1597  |  | 
  | 
1598  | 0  |   g_task_thread_cleanup ();  | 
1599  | 0  | }  | 
1600  |  |  | 
1601  |  | static void  | 
1602  |  | task_thread_cancelled (GCancellable *cancellable,  | 
1603  |  |                        gpointer      user_data)  | 
1604  | 0  | { | 
1605  | 0  |   GTask *task = user_data;  | 
1606  |  |  | 
1607  |  |   /* Move this task to the front of the queue - no need for  | 
1608  |  |    * a complete resorting of the queue.  | 
1609  |  |    */  | 
1610  | 0  |   g_thread_pool_move_to_front (task_pool, task);  | 
1611  |  | 
  | 
1612  | 0  |   g_mutex_lock (&task->lock);  | 
1613  | 0  |   task->thread_cancelled = TRUE;  | 
1614  |  | 
  | 
1615  | 0  |   if (!task->return_on_cancel)  | 
1616  | 0  |     { | 
1617  | 0  |       g_mutex_unlock (&task->lock);  | 
1618  | 0  |       return;  | 
1619  | 0  |     }  | 
1620  |  |  | 
1621  |  |   /* We don't actually set task->error; g_task_return_error() doesn't  | 
1622  |  |    * use a lock, and g_task_propagate_error() will call  | 
1623  |  |    * g_cancellable_set_error_if_cancelled() anyway.  | 
1624  |  |    */  | 
1625  | 0  |   g_mutex_unlock (&task->lock);  | 
1626  | 0  |   g_task_thread_complete (task);  | 
1627  | 0  | }  | 
1628  |  |  | 
1629  |  | static void  | 
1630  |  | task_thread_cancelled_disconnect_notify (gpointer  task,  | 
1631  |  |                                          GClosure *closure)  | 
1632  | 0  | { | 
1633  | 0  |   g_object_unref (task);  | 
1634  | 0  | }  | 
1635  |  |  | 
1636  |  | static void  | 
1637  |  | g_task_start_task_thread (GTask           *task,  | 
1638  |  |                           GTaskThreadFunc  task_func)  | 
1639  | 0  | { | 
1640  | 0  |   g_mutex_init (&task->lock);  | 
1641  | 0  |   g_cond_init (&task->cond);  | 
1642  |  | 
  | 
1643  | 0  |   g_mutex_lock (&task->lock);  | 
1644  |  | 
  | 
1645  | 0  |   TRACE (GIO_TASK_BEFORE_RUN_IN_THREAD (task, task_func));  | 
1646  |  | 
  | 
1647  | 0  |   task->task_func = task_func;  | 
1648  |  | 
  | 
1649  | 0  |   if (task->cancellable)  | 
1650  | 0  |     { | 
1651  | 0  |       if (task->return_on_cancel &&  | 
1652  | 0  |           g_cancellable_set_error_if_cancelled (task->cancellable,  | 
1653  | 0  |                                                 &task->error))  | 
1654  | 0  |         { | 
1655  | 0  |           task->thread_cancelled = task->thread_complete = TRUE;  | 
1656  | 0  |           TRACE (GIO_TASK_AFTER_RUN_IN_THREAD (task, task->thread_cancelled));  | 
1657  | 0  |           g_thread_pool_push (task_pool, g_object_ref (task), NULL);  | 
1658  | 0  |           return;  | 
1659  | 0  |         }  | 
1660  |  |  | 
1661  |  |       /* This introduces a reference count loop between the GTask and  | 
1662  |  |        * GCancellable, but is necessary to avoid a race on finalising the GTask  | 
1663  |  |        * between task_thread_cancelled() (in one thread) and  | 
1664  |  |        * g_task_thread_complete() (in another).  | 
1665  |  |        *  | 
1666  |  |        * Accordingly, the signal handler *must* be removed once the task has  | 
1667  |  |        * completed.  | 
1668  |  |        */  | 
1669  | 0  |       g_signal_connect_data (task->cancellable, "cancelled",  | 
1670  | 0  |                              G_CALLBACK (task_thread_cancelled),  | 
1671  | 0  |                              g_object_ref (task),  | 
1672  | 0  |                              task_thread_cancelled_disconnect_notify,  | 
1673  | 0  |                              G_CONNECT_DEFAULT);  | 
1674  | 0  |     }  | 
1675  |  |  | 
1676  | 0  |   if (g_private_get (&task_private))  | 
1677  | 0  |     task->blocking_other_task = TRUE;  | 
1678  | 0  |   g_thread_pool_push (task_pool, g_object_ref (task), NULL);  | 
1679  | 0  | }  | 
1680  |  |  | 
1681  |  | /**  | 
1682  |  |  * g_task_run_in_thread:  | 
1683  |  |  * @task: a #GTask  | 
1684  |  |  * @task_func: (scope async): a #GTaskThreadFunc  | 
1685  |  |  *  | 
1686  |  |  * Runs @task_func in another thread. When @task_func returns, @task's  | 
1687  |  |  * #GAsyncReadyCallback will be invoked in @task's #GMainContext.  | 
1688  |  |  *  | 
1689  |  |  * This takes a ref on @task until the task completes.  | 
1690  |  |  *  | 
1691  |  |  * See #GTaskThreadFunc for more details about how @task_func is handled.  | 
1692  |  |  *  | 
1693  |  |  * Although GLib currently rate-limits the tasks queued via  | 
1694  |  |  * g_task_run_in_thread(), you should not assume that it will always  | 
1695  |  |  * do this. If you have a very large number of tasks to run (several tens of  | 
1696  |  |  * tasks), but don't want them to all run at once, you should only queue a  | 
1697  |  |  * limited number of them (around ten) at a time.  | 
1698  |  |  *  | 
1699  |  |  * Be aware that if your task depends on other tasks to complete, use of this  | 
1700  |  |  * function could lead to a livelock if the other tasks also use this function  | 
1701  |  |  * and enough of them (around 10) execute in a dependency chain, as that will  | 
1702  |  |  * exhaust the thread pool. If this situation is possible, consider using a  | 
1703  |  |  * separate worker thread or thread pool explicitly, rather than using  | 
1704  |  |  * g_task_run_in_thread().  | 
1705  |  |  *  | 
1706  |  |  * Since: 2.36  | 
1707  |  |  */  | 
1708  |  | void  | 
1709  |  | g_task_run_in_thread (GTask           *task,  | 
1710  |  |                       GTaskThreadFunc  task_func)  | 
1711  | 0  | { | 
1712  | 0  |   g_return_if_fail (G_IS_TASK (task));  | 
1713  |  |  | 
1714  | 0  |   g_object_ref (task);  | 
1715  | 0  |   g_task_start_task_thread (task, task_func);  | 
1716  |  |  | 
1717  |  |   /* The task may already be cancelled, or g_thread_pool_push() may  | 
1718  |  |    * have failed.  | 
1719  |  |    */  | 
1720  | 0  |   if (task->thread_complete)  | 
1721  | 0  |     { | 
1722  | 0  |       g_mutex_unlock (&task->lock);  | 
1723  | 0  |       g_task_return (task, G_TASK_RETURN_FROM_THREAD);  | 
1724  | 0  |     }  | 
1725  | 0  |   else  | 
1726  | 0  |     g_mutex_unlock (&task->lock);  | 
1727  |  | 
  | 
1728  | 0  |   g_object_unref (task);  | 
1729  | 0  | }  | 
1730  |  |  | 
1731  |  | /**  | 
1732  |  |  * g_task_run_in_thread_sync:  | 
1733  |  |  * @task: a #GTask  | 
1734  |  |  * @task_func: (scope async): a #GTaskThreadFunc  | 
1735  |  |  *  | 
1736  |  |  * Runs @task_func in another thread, and waits for it to return or be  | 
1737  |  |  * cancelled. You can use g_task_propagate_pointer(), etc, afterward  | 
1738  |  |  * to get the result of @task_func.  | 
1739  |  |  *  | 
1740  |  |  * See #GTaskThreadFunc for more details about how @task_func is handled.  | 
1741  |  |  *  | 
1742  |  |  * Normally this is used with tasks created with a %NULL  | 
1743  |  |  * `callback`, but note that even if the task does  | 
1744  |  |  * have a callback, it will not be invoked when @task_func returns.  | 
1745  |  |  * #GTask:completed will be set to %TRUE just before this function returns.  | 
1746  |  |  *  | 
1747  |  |  * Although GLib currently rate-limits the tasks queued via  | 
1748  |  |  * g_task_run_in_thread_sync(), you should not assume that it will  | 
1749  |  |  * always do this. If you have a very large number of tasks to run,  | 
1750  |  |  * but don't want them to all run at once, you should only queue a  | 
1751  |  |  * limited number of them at a time.  | 
1752  |  |  *  | 
1753  |  |  * Since: 2.36  | 
1754  |  |  */  | 
1755  |  | void  | 
1756  |  | g_task_run_in_thread_sync (GTask           *task,  | 
1757  |  |                            GTaskThreadFunc  task_func)  | 
1758  | 0  | { | 
1759  | 0  |   g_return_if_fail (G_IS_TASK (task));  | 
1760  |  |  | 
1761  | 0  |   g_object_ref (task);  | 
1762  |  | 
  | 
1763  | 0  |   task->synchronous = TRUE;  | 
1764  | 0  |   g_task_start_task_thread (task, task_func);  | 
1765  |  | 
  | 
1766  | 0  |   while (!task->thread_complete)  | 
1767  | 0  |     g_cond_wait (&task->cond, &task->lock);  | 
1768  |  | 
  | 
1769  | 0  |   g_mutex_unlock (&task->lock);  | 
1770  |  | 
  | 
1771  | 0  |   TRACE (GIO_TASK_BEFORE_RETURN (task, task->source_object,  | 
1772  | 0  |                                  NULL  /* callback */,  | 
1773  | 0  |                                  NULL  /* callback data */));  | 
1774  |  |  | 
1775  |  |   /* Notify of completion in this thread. */  | 
1776  | 0  |   task->completed = TRUE;  | 
1777  | 0  |   g_object_notify (G_OBJECT (task), "completed");  | 
1778  |  | 
  | 
1779  | 0  |   g_object_unref (task);  | 
1780  | 0  | }  | 
1781  |  |  | 
1782  |  | /**  | 
1783  |  |  * g_task_attach_source:  | 
1784  |  |  * @task: a #GTask  | 
1785  |  |  * @source: the source to attach  | 
1786  |  |  * @callback: the callback to invoke when @source triggers  | 
1787  |  |  *  | 
1788  |  |  * A utility function for dealing with async operations where you need  | 
1789  |  |  * to wait for a #GSource to trigger. Attaches @source to @task's  | 
1790  |  |  * #GMainContext with @task's [priority][io-priority], and sets @source's  | 
1791  |  |  * callback to @callback, with @task as the callback's `user_data`.  | 
1792  |  |  *  | 
1793  |  |  * It will set the @source’s name to the task’s name (as set with  | 
1794  |  |  * g_task_set_name()), if one has been set on the task and the source doesn’t  | 
1795  |  |  * yet have a name.  | 
1796  |  |  *  | 
1797  |  |  * This takes a reference on @task until @source is destroyed.  | 
1798  |  |  *  | 
1799  |  |  * Since: 2.36  | 
1800  |  |  */  | 
1801  |  | void  | 
1802  |  | g_task_attach_source (GTask       *task,  | 
1803  |  |                       GSource     *source,  | 
1804  |  |                       GSourceFunc  callback)  | 
1805  | 0  | { | 
1806  | 0  |   g_return_if_fail (G_IS_TASK (task));  | 
1807  |  |  | 
1808  | 0  |   g_source_set_callback (source, callback,  | 
1809  | 0  |                          g_object_ref (task), g_object_unref);  | 
1810  | 0  |   g_source_set_priority (source, task->priority);  | 
1811  | 0  |   if (task->name != NULL && g_source_get_name (source) == NULL)  | 
1812  | 0  |     g_source_set_name (source, task->name);  | 
1813  |  | 
  | 
1814  | 0  |   g_source_attach (source, task->context);  | 
1815  | 0  | }  | 
1816  |  |  | 
1817  |  |  | 
1818  |  | static gboolean  | 
1819  |  | g_task_propagate_error (GTask   *task,  | 
1820  |  |                         GError **error)  | 
1821  | 0  | { | 
1822  | 0  |   gboolean error_set;  | 
1823  |  | 
  | 
1824  | 0  |   if (task->check_cancellable &&  | 
1825  | 0  |       g_cancellable_set_error_if_cancelled (task->cancellable, error))  | 
1826  | 0  |     error_set = TRUE;  | 
1827  | 0  |   else if (task->error)  | 
1828  | 0  |     { | 
1829  | 0  |       g_propagate_error (error, task->error);  | 
1830  | 0  |       task->error = NULL;  | 
1831  | 0  |       task->had_error = TRUE;  | 
1832  | 0  |       error_set = TRUE;  | 
1833  | 0  |     }  | 
1834  | 0  |   else  | 
1835  | 0  |     error_set = FALSE;  | 
1836  |  | 
  | 
1837  | 0  |   TRACE (GIO_TASK_PROPAGATE (task, error_set));  | 
1838  |  | 
  | 
1839  | 0  |   return error_set;  | 
1840  | 0  | }  | 
1841  |  |  | 
1842  |  | /**  | 
1843  |  |  * g_task_return_pointer:  | 
1844  |  |  * @task: a #GTask  | 
1845  |  |  * @result: (nullable) (transfer full): the pointer result of a task  | 
1846  |  |  *     function  | 
1847  |  |  * @result_destroy: (nullable): a #GDestroyNotify function.  | 
1848  |  |  *  | 
1849  |  |  * Sets @task's result to @result and completes the task. If @result  | 
1850  |  |  * is not %NULL, then @result_destroy will be used to free @result if  | 
1851  |  |  * the caller does not take ownership of it with  | 
1852  |  |  * g_task_propagate_pointer().  | 
1853  |  |  *  | 
1854  |  |  * "Completes the task" means that for an ordinary asynchronous task  | 
1855  |  |  * it will either invoke the task's callback, or else queue that  | 
1856  |  |  * callback to be invoked in the proper #GMainContext, or in the next  | 
1857  |  |  * iteration of the current #GMainContext. For a task run via  | 
1858  |  |  * g_task_run_in_thread() or g_task_run_in_thread_sync(), calling this  | 
1859  |  |  * method will save @result to be returned to the caller later, but  | 
1860  |  |  * the task will not actually be completed until the #GTaskThreadFunc  | 
1861  |  |  * exits.  | 
1862  |  |  *  | 
1863  |  |  * Note that since the task may be completed before returning from  | 
1864  |  |  * g_task_return_pointer(), you cannot assume that @result is still  | 
1865  |  |  * valid after calling this, unless you are still holding another  | 
1866  |  |  * reference on it.  | 
1867  |  |  *  | 
1868  |  |  * Since: 2.36  | 
1869  |  |  */  | 
1870  |  | void  | 
1871  |  | g_task_return_pointer (GTask          *task,  | 
1872  |  |                        gpointer        result,  | 
1873  |  |                        GDestroyNotify  result_destroy)  | 
1874  | 0  | { | 
1875  | 0  |   g_return_if_fail (G_IS_TASK (task));  | 
1876  | 0  |   g_return_if_fail (!task->ever_returned);  | 
1877  |  |  | 
1878  | 0  |   task->result.pointer = result;  | 
1879  | 0  |   task->result_destroy = result_destroy;  | 
1880  |  | 
  | 
1881  | 0  |   g_task_return (task, G_TASK_RETURN_SUCCESS);  | 
1882  | 0  | }  | 
1883  |  |  | 
1884  |  | /**  | 
1885  |  |  * g_task_propagate_pointer:  | 
1886  |  |  * @task: a #GTask  | 
1887  |  |  * @error: return location for a #GError  | 
1888  |  |  *  | 
1889  |  |  * Gets the result of @task as a pointer, and transfers ownership  | 
1890  |  |  * of that value to the caller.  | 
1891  |  |  *  | 
1892  |  |  * If the task resulted in an error, or was cancelled, then this will  | 
1893  |  |  * instead return %NULL and set @error.  | 
1894  |  |  *  | 
1895  |  |  * Since this method transfers ownership of the return value (or  | 
1896  |  |  * error) to the caller, you may only call it once.  | 
1897  |  |  *  | 
1898  |  |  * Returns: (transfer full): the task result, or %NULL on error  | 
1899  |  |  *  | 
1900  |  |  * Since: 2.36  | 
1901  |  |  */  | 
1902  |  | gpointer  | 
1903  |  | g_task_propagate_pointer (GTask   *task,  | 
1904  |  |                           GError **error)  | 
1905  | 0  | { | 
1906  | 0  |   g_return_val_if_fail (G_IS_TASK (task), NULL);  | 
1907  |  |  | 
1908  | 0  |   if (g_task_propagate_error (task, error))  | 
1909  | 0  |     return NULL;  | 
1910  |  |  | 
1911  | 0  |   g_return_val_if_fail (task->result_set, NULL);  | 
1912  |  |  | 
1913  | 0  |   task->result_destroy = NULL;  | 
1914  | 0  |   task->result_set = FALSE;  | 
1915  | 0  |   return task->result.pointer;  | 
1916  | 0  | }  | 
1917  |  |  | 
1918  |  | /**  | 
1919  |  |  * g_task_return_int:  | 
1920  |  |  * @task: a #GTask.  | 
1921  |  |  * @result: the integer (#gssize) result of a task function.  | 
1922  |  |  *  | 
1923  |  |  * Sets @task's result to @result and completes the task (see  | 
1924  |  |  * g_task_return_pointer() for more discussion of exactly what this  | 
1925  |  |  * means).  | 
1926  |  |  *  | 
1927  |  |  * Since: 2.36  | 
1928  |  |  */  | 
1929  |  | void  | 
1930  |  | g_task_return_int (GTask  *task,  | 
1931  |  |                    gssize  result)  | 
1932  | 0  | { | 
1933  | 0  |   g_return_if_fail (G_IS_TASK (task));  | 
1934  | 0  |   g_return_if_fail (!task->ever_returned);  | 
1935  |  |  | 
1936  | 0  |   task->result.size = result;  | 
1937  |  | 
  | 
1938  | 0  |   g_task_return (task, G_TASK_RETURN_SUCCESS);  | 
1939  | 0  | }  | 
1940  |  |  | 
1941  |  | /**  | 
1942  |  |  * g_task_propagate_int:  | 
1943  |  |  * @task: a #GTask.  | 
1944  |  |  * @error: return location for a #GError  | 
1945  |  |  *  | 
1946  |  |  * Gets the result of @task as an integer (#gssize).  | 
1947  |  |  *  | 
1948  |  |  * If the task resulted in an error, or was cancelled, then this will  | 
1949  |  |  * instead return -1 and set @error.  | 
1950  |  |  *  | 
1951  |  |  * Since this method transfers ownership of the return value (or  | 
1952  |  |  * error) to the caller, you may only call it once.  | 
1953  |  |  *  | 
1954  |  |  * Returns: the task result, or -1 on error  | 
1955  |  |  *  | 
1956  |  |  * Since: 2.36  | 
1957  |  |  */  | 
1958  |  | gssize  | 
1959  |  | g_task_propagate_int (GTask   *task,  | 
1960  |  |                       GError **error)  | 
1961  | 0  | { | 
1962  | 0  |   g_return_val_if_fail (G_IS_TASK (task), -1);  | 
1963  |  |  | 
1964  | 0  |   if (g_task_propagate_error (task, error))  | 
1965  | 0  |     return -1;  | 
1966  |  |  | 
1967  | 0  |   g_return_val_if_fail (task->result_set, -1);  | 
1968  |  |  | 
1969  | 0  |   task->result_set = FALSE;  | 
1970  | 0  |   return task->result.size;  | 
1971  | 0  | }  | 
1972  |  |  | 
1973  |  | /**  | 
1974  |  |  * g_task_return_boolean:  | 
1975  |  |  * @task: a #GTask.  | 
1976  |  |  * @result: the #gboolean result of a task function.  | 
1977  |  |  *  | 
1978  |  |  * Sets @task's result to @result and completes the task (see  | 
1979  |  |  * g_task_return_pointer() for more discussion of exactly what this  | 
1980  |  |  * means).  | 
1981  |  |  *  | 
1982  |  |  * Since: 2.36  | 
1983  |  |  */  | 
1984  |  | void  | 
1985  |  | g_task_return_boolean (GTask    *task,  | 
1986  |  |                        gboolean  result)  | 
1987  | 0  | { | 
1988  | 0  |   g_return_if_fail (G_IS_TASK (task));  | 
1989  | 0  |   g_return_if_fail (!task->ever_returned);  | 
1990  |  |  | 
1991  | 0  |   task->result.boolean = result;  | 
1992  |  | 
  | 
1993  | 0  |   g_task_return (task, G_TASK_RETURN_SUCCESS);  | 
1994  | 0  | }  | 
1995  |  |  | 
1996  |  | /**  | 
1997  |  |  * g_task_propagate_boolean:  | 
1998  |  |  * @task: a #GTask.  | 
1999  |  |  * @error: return location for a #GError  | 
2000  |  |  *  | 
2001  |  |  * Gets the result of @task as a #gboolean.  | 
2002  |  |  *  | 
2003  |  |  * If the task resulted in an error, or was cancelled, then this will  | 
2004  |  |  * instead return %FALSE and set @error.  | 
2005  |  |  *  | 
2006  |  |  * Since this method transfers ownership of the return value (or  | 
2007  |  |  * error) to the caller, you may only call it once.  | 
2008  |  |  *  | 
2009  |  |  * Returns: the task result, or %FALSE on error  | 
2010  |  |  *  | 
2011  |  |  * Since: 2.36  | 
2012  |  |  */  | 
2013  |  | gboolean  | 
2014  |  | g_task_propagate_boolean (GTask   *task,  | 
2015  |  |                           GError **error)  | 
2016  | 0  | { | 
2017  | 0  |   g_return_val_if_fail (G_IS_TASK (task), FALSE);  | 
2018  |  |  | 
2019  | 0  |   if (g_task_propagate_error (task, error))  | 
2020  | 0  |     return FALSE;  | 
2021  |  |  | 
2022  | 0  |   g_return_val_if_fail (task->result_set, FALSE);  | 
2023  |  |  | 
2024  | 0  |   task->result_set = FALSE;  | 
2025  | 0  |   return task->result.boolean;  | 
2026  | 0  | }  | 
2027  |  |  | 
2028  |  | /**  | 
2029  |  |  * g_task_return_error:  | 
2030  |  |  * @task: a #GTask.  | 
2031  |  |  * @error: (transfer full): the #GError result of a task function.  | 
2032  |  |  *  | 
2033  |  |  * Sets @task's result to @error (which @task assumes ownership of)  | 
2034  |  |  * and completes the task (see g_task_return_pointer() for more  | 
2035  |  |  * discussion of exactly what this means).  | 
2036  |  |  *  | 
2037  |  |  * Note that since the task takes ownership of @error, and since the  | 
2038  |  |  * task may be completed before returning from g_task_return_error(),  | 
2039  |  |  * you cannot assume that @error is still valid after calling this.  | 
2040  |  |  * Call g_error_copy() on the error if you need to keep a local copy  | 
2041  |  |  * as well.  | 
2042  |  |  *  | 
2043  |  |  * See also g_task_return_new_error().  | 
2044  |  |  *  | 
2045  |  |  * Since: 2.36  | 
2046  |  |  */  | 
2047  |  | void  | 
2048  |  | g_task_return_error (GTask  *task,  | 
2049  |  |                      GError *error)  | 
2050  | 0  | { | 
2051  | 0  |   g_return_if_fail (G_IS_TASK (task));  | 
2052  | 0  |   g_return_if_fail (!task->ever_returned);  | 
2053  | 0  |   g_return_if_fail (error != NULL);  | 
2054  |  |  | 
2055  | 0  |   task->error = error;  | 
2056  |  | 
  | 
2057  | 0  |   g_task_return (task, G_TASK_RETURN_ERROR);  | 
2058  | 0  | }  | 
2059  |  |  | 
2060  |  | /**  | 
2061  |  |  * g_task_return_new_error:  | 
2062  |  |  * @task: a #GTask.  | 
2063  |  |  * @domain: a #GQuark.  | 
2064  |  |  * @code: an error code.  | 
2065  |  |  * @format: a string with format characters.  | 
2066  |  |  * @...: a list of values to insert into @format.  | 
2067  |  |  *  | 
2068  |  |  * Sets @task's result to a new #GError created from @domain, @code,  | 
2069  |  |  * @format, and the remaining arguments, and completes the task (see  | 
2070  |  |  * g_task_return_pointer() for more discussion of exactly what this  | 
2071  |  |  * means).  | 
2072  |  |  *  | 
2073  |  |  * See also g_task_return_error().  | 
2074  |  |  *  | 
2075  |  |  * Since: 2.36  | 
2076  |  |  */  | 
2077  |  | void  | 
2078  |  | g_task_return_new_error (GTask           *task,  | 
2079  |  |                          GQuark           domain,  | 
2080  |  |                          gint             code,  | 
2081  |  |                          const char      *format,  | 
2082  |  |                          ...)  | 
2083  | 0  | { | 
2084  | 0  |   GError *error;  | 
2085  | 0  |   va_list args;  | 
2086  |  | 
  | 
2087  | 0  |   va_start (args, format);  | 
2088  | 0  |   error = g_error_new_valist (domain, code, format, args);  | 
2089  | 0  |   va_end (args);  | 
2090  |  | 
  | 
2091  | 0  |   g_task_return_error (task, error);  | 
2092  | 0  | }  | 
2093  |  |  | 
2094  |  | /**  | 
2095  |  |  * g_task_return_error_if_cancelled:  | 
2096  |  |  * @task: a #GTask  | 
2097  |  |  *  | 
2098  |  |  * Checks if @task's #GCancellable has been cancelled, and if so, sets  | 
2099  |  |  * @task's error accordingly and completes the task (see  | 
2100  |  |  * g_task_return_pointer() for more discussion of exactly what this  | 
2101  |  |  * means).  | 
2102  |  |  *  | 
2103  |  |  * Returns: %TRUE if @task has been cancelled, %FALSE if not  | 
2104  |  |  *  | 
2105  |  |  * Since: 2.36  | 
2106  |  |  */  | 
2107  |  | gboolean  | 
2108  |  | g_task_return_error_if_cancelled (GTask *task)  | 
2109  | 0  | { | 
2110  | 0  |   GError *error = NULL;  | 
2111  |  | 
  | 
2112  | 0  |   g_return_val_if_fail (G_IS_TASK (task), FALSE);  | 
2113  | 0  |   g_return_val_if_fail (!task->ever_returned, FALSE);  | 
2114  |  |  | 
2115  | 0  |   if (g_cancellable_set_error_if_cancelled (task->cancellable, &error))  | 
2116  | 0  |     { | 
2117  |  |       /* We explicitly set task->error so this works even when  | 
2118  |  |        * check-cancellable is not set.  | 
2119  |  |        */  | 
2120  | 0  |       g_clear_error (&task->error);  | 
2121  | 0  |       task->error = error;  | 
2122  |  | 
  | 
2123  | 0  |       g_task_return (task, G_TASK_RETURN_ERROR);  | 
2124  | 0  |       return TRUE;  | 
2125  | 0  |     }  | 
2126  | 0  |   else  | 
2127  | 0  |     return FALSE;  | 
2128  | 0  | }  | 
2129  |  |  | 
2130  |  | /**  | 
2131  |  |  * g_task_had_error:  | 
2132  |  |  * @task: a #GTask.  | 
2133  |  |  *  | 
2134  |  |  * Tests if @task resulted in an error.  | 
2135  |  |  *  | 
2136  |  |  * Returns: %TRUE if the task resulted in an error, %FALSE otherwise.  | 
2137  |  |  *  | 
2138  |  |  * Since: 2.36  | 
2139  |  |  */  | 
2140  |  | gboolean  | 
2141  |  | g_task_had_error (GTask *task)  | 
2142  | 0  | { | 
2143  | 0  |   g_return_val_if_fail (G_IS_TASK (task), FALSE);  | 
2144  |  |  | 
2145  | 0  |   if (task->error != NULL || task->had_error)  | 
2146  | 0  |     return TRUE;  | 
2147  |  |  | 
2148  | 0  |   if (task->check_cancellable && g_cancellable_is_cancelled (task->cancellable))  | 
2149  | 0  |     return TRUE;  | 
2150  |  |  | 
2151  | 0  |   return FALSE;  | 
2152  | 0  | }  | 
2153  |  |  | 
2154  |  | static void  | 
2155  |  | value_free (gpointer value)  | 
2156  | 0  | { | 
2157  | 0  |   g_value_unset (value);  | 
2158  | 0  |   g_free (value);  | 
2159  | 0  | }  | 
2160  |  |  | 
2161  |  | /**  | 
2162  |  |  * g_task_return_value:  | 
2163  |  |  * @task: a #GTask  | 
2164  |  |  * @result: (nullable) (transfer none): the #GValue result of  | 
2165  |  |  *                                      a task function  | 
2166  |  |  *  | 
2167  |  |  * Sets @task's result to @result (by copying it) and completes the task.  | 
2168  |  |  *  | 
2169  |  |  * If @result is %NULL then a #GValue of type %G_TYPE_POINTER  | 
2170  |  |  * with a value of %NULL will be used for the result.  | 
2171  |  |  *  | 
2172  |  |  * This is a very generic low-level method intended primarily for use  | 
2173  |  |  * by language bindings; for C code, g_task_return_pointer() and the  | 
2174  |  |  * like will normally be much easier to use.  | 
2175  |  |  *  | 
2176  |  |  * Since: 2.64  | 
2177  |  |  */  | 
2178  |  | void  | 
2179  |  | g_task_return_value (GTask  *task,  | 
2180  |  |                      GValue *result)  | 
2181  | 0  | { | 
2182  | 0  |   GValue *value;  | 
2183  |  | 
  | 
2184  | 0  |   g_return_if_fail (G_IS_TASK (task));  | 
2185  | 0  |   g_return_if_fail (!task->ever_returned);  | 
2186  |  |  | 
2187  | 0  |   value = g_new0 (GValue, 1);  | 
2188  |  | 
  | 
2189  | 0  |   if (result == NULL)  | 
2190  | 0  |     { | 
2191  | 0  |       g_value_init (value, G_TYPE_POINTER);  | 
2192  | 0  |       g_value_set_pointer (value, NULL);  | 
2193  | 0  |     }  | 
2194  | 0  |   else  | 
2195  | 0  |     { | 
2196  | 0  |       g_value_init (value, G_VALUE_TYPE (result));  | 
2197  | 0  |       g_value_copy (result, value);  | 
2198  | 0  |     }  | 
2199  |  | 
  | 
2200  | 0  |   g_task_return_pointer (task, value, value_free);  | 
2201  | 0  | }  | 
2202  |  |  | 
2203  |  | /**  | 
2204  |  |  * g_task_propagate_value:  | 
2205  |  |  * @task: a #GTask  | 
2206  |  |  * @value: (out caller-allocates): return location for the #GValue  | 
2207  |  |  * @error: return location for a #GError  | 
2208  |  |  *  | 
2209  |  |  * Gets the result of @task as a #GValue, and transfers ownership of  | 
2210  |  |  * that value to the caller. As with g_task_return_value(), this is  | 
2211  |  |  * a generic low-level method; g_task_propagate_pointer() and the like  | 
2212  |  |  * will usually be more useful for C code.  | 
2213  |  |  *  | 
2214  |  |  * If the task resulted in an error, or was cancelled, then this will  | 
2215  |  |  * instead set @error and return %FALSE.  | 
2216  |  |  *  | 
2217  |  |  * Since this method transfers ownership of the return value (or  | 
2218  |  |  * error) to the caller, you may only call it once.  | 
2219  |  |  *  | 
2220  |  |  * Returns: %TRUE if @task succeeded, %FALSE on error.  | 
2221  |  |  *  | 
2222  |  |  * Since: 2.64  | 
2223  |  |  */  | 
2224  |  | gboolean  | 
2225  |  | g_task_propagate_value (GTask   *task,  | 
2226  |  |                         GValue  *value,  | 
2227  |  |                         GError **error)  | 
2228  | 0  | { | 
2229  | 0  |   g_return_val_if_fail (G_IS_TASK (task), FALSE);  | 
2230  | 0  |   g_return_val_if_fail (value != NULL, FALSE);  | 
2231  | 0  |   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);  | 
2232  |  |  | 
2233  | 0  |   if (g_task_propagate_error (task, error))  | 
2234  | 0  |     return FALSE;  | 
2235  |  |  | 
2236  | 0  |   g_return_val_if_fail (task->result_set, FALSE);  | 
2237  | 0  |   g_return_val_if_fail (task->result_destroy == value_free, FALSE);  | 
2238  |  |  | 
2239  | 0  |   memcpy (value, task->result.pointer, sizeof (GValue));  | 
2240  | 0  |   g_free (task->result.pointer);  | 
2241  |  | 
  | 
2242  | 0  |   task->result_destroy = NULL;  | 
2243  | 0  |   task->result_set = FALSE;  | 
2244  |  | 
  | 
2245  | 0  |   return TRUE;  | 
2246  | 0  | }  | 
2247  |  |  | 
2248  |  | /**  | 
2249  |  |  * g_task_get_completed:  | 
2250  |  |  * @task: a #GTask.  | 
2251  |  |  *  | 
2252  |  |  * Gets the value of #GTask:completed. This changes from %FALSE to %TRUE after  | 
2253  |  |  * the task’s callback is invoked, and will return %FALSE if called from inside  | 
2254  |  |  * the callback.  | 
2255  |  |  *  | 
2256  |  |  * Returns: %TRUE if the task has completed, %FALSE otherwise.  | 
2257  |  |  *  | 
2258  |  |  * Since: 2.44  | 
2259  |  |  */  | 
2260  |  | gboolean  | 
2261  |  | g_task_get_completed (GTask *task)  | 
2262  | 0  | { | 
2263  | 0  |   g_return_val_if_fail (G_IS_TASK (task), FALSE);  | 
2264  |  |  | 
2265  |  |   /* Convert from a bit field to a boolean. */  | 
2266  | 0  |   return task->completed ? TRUE : FALSE;  | 
2267  | 0  | }  | 
2268  |  |  | 
2269  |  | /**  | 
2270  |  |  * g_task_is_valid:  | 
2271  |  |  * @result: (type Gio.AsyncResult): A #GAsyncResult  | 
2272  |  |  * @source_object: (nullable) (type GObject): the source object  | 
2273  |  |  *   expected to be associated with the task  | 
2274  |  |  *  | 
2275  |  |  * Checks that @result is a #GTask, and that @source_object is its  | 
2276  |  |  * source object (or that @source_object is %NULL and @result has no  | 
2277  |  |  * source object). This can be used in g_return_if_fail() checks.  | 
2278  |  |  *  | 
2279  |  |  * Returns: %TRUE if @result and @source_object are valid, %FALSE  | 
2280  |  |  * if not  | 
2281  |  |  *  | 
2282  |  |  * Since: 2.36  | 
2283  |  |  */  | 
2284  |  | gboolean  | 
2285  |  | g_task_is_valid (gpointer result,  | 
2286  |  |                  gpointer source_object)  | 
2287  | 0  | { | 
2288  | 0  |   if (!G_IS_TASK (result))  | 
2289  | 0  |     return FALSE;  | 
2290  |  |  | 
2291  | 0  |   return G_TASK (result)->source_object == source_object;  | 
2292  | 0  | }  | 
2293  |  |  | 
2294  |  | static gint  | 
2295  |  | g_task_compare_priority (gconstpointer a,  | 
2296  |  |                          gconstpointer b,  | 
2297  |  |                          gpointer      user_data)  | 
2298  | 0  | { | 
2299  | 0  |   const GTask *ta = a;  | 
2300  | 0  |   const GTask *tb = b;  | 
2301  | 0  |   gboolean a_cancelled, b_cancelled;  | 
2302  |  |  | 
2303  |  |   /* Tasks that are causing other tasks to block have higher  | 
2304  |  |    * priority.  | 
2305  |  |    */  | 
2306  | 0  |   if (ta->blocking_other_task && !tb->blocking_other_task)  | 
2307  | 0  |     return -1;  | 
2308  | 0  |   else if (tb->blocking_other_task && !ta->blocking_other_task)  | 
2309  | 0  |     return 1;  | 
2310  |  |  | 
2311  |  |   /* Let already-cancelled tasks finish right away */  | 
2312  | 0  |   a_cancelled = (ta->check_cancellable &&  | 
2313  | 0  |                  g_cancellable_is_cancelled (ta->cancellable));  | 
2314  | 0  |   b_cancelled = (tb->check_cancellable &&  | 
2315  | 0  |                  g_cancellable_is_cancelled (tb->cancellable));  | 
2316  | 0  |   if (a_cancelled && !b_cancelled)  | 
2317  | 0  |     return -1;  | 
2318  | 0  |   else if (b_cancelled && !a_cancelled)  | 
2319  | 0  |     return 1;  | 
2320  |  |  | 
2321  |  |   /* Lower priority == run sooner == negative return value */  | 
2322  | 0  |   return ta->priority - tb->priority;  | 
2323  | 0  | }  | 
2324  |  |  | 
2325  |  | static gboolean  | 
2326  |  | trivial_source_dispatch (GSource     *source,  | 
2327  |  |                          GSourceFunc  callback,  | 
2328  |  |                          gpointer     user_data)  | 
2329  | 0  | { | 
2330  | 0  |   return callback (user_data);  | 
2331  | 0  | }  | 
2332  |  |  | 
2333  |  | GSourceFuncs trivial_source_funcs = { | 
2334  |  |   NULL, /* prepare */  | 
2335  |  |   NULL, /* check */  | 
2336  |  |   trivial_source_dispatch,  | 
2337  |  |   NULL, /* finalize */  | 
2338  |  |   NULL, /* closure */  | 
2339  |  |   NULL  /* marshal */  | 
2340  |  | };  | 
2341  |  |  | 
2342  |  | static void  | 
2343  |  | g_task_thread_pool_init (void)  | 
2344  | 0  | { | 
2345  | 0  |   task_pool = g_thread_pool_new (g_task_thread_pool_thread, NULL,  | 
2346  | 0  |                                  G_TASK_POOL_SIZE, FALSE, NULL);  | 
2347  | 0  |   g_assert (task_pool != NULL);  | 
2348  |  |  | 
2349  | 0  |   g_thread_pool_set_sort_function (task_pool, g_task_compare_priority, NULL);  | 
2350  |  | 
  | 
2351  | 0  |   task_pool_manager = g_source_new (&trivial_source_funcs, sizeof (GSource));  | 
2352  | 0  |   g_source_set_static_name (task_pool_manager, "GTask thread pool manager");  | 
2353  | 0  |   g_source_set_callback (task_pool_manager, task_pool_manager_timeout, NULL, NULL);  | 
2354  | 0  |   g_source_set_ready_time (task_pool_manager, -1);  | 
2355  | 0  |   g_source_attach (task_pool_manager,  | 
2356  | 0  |                    GLIB_PRIVATE_CALL (g_get_worker_context ()));  | 
2357  | 0  |   g_source_unref (task_pool_manager);  | 
2358  | 0  | }  | 
2359  |  |  | 
2360  |  | static void  | 
2361  |  | g_task_get_property (GObject    *object,  | 
2362  |  |                      guint       prop_id,  | 
2363  |  |                      GValue     *value,  | 
2364  |  |                      GParamSpec *pspec)  | 
2365  | 0  | { | 
2366  | 0  |   GTask *task = G_TASK (object);  | 
2367  |  | 
  | 
2368  | 0  |   switch ((GTaskProperty) prop_id)  | 
2369  | 0  |     { | 
2370  | 0  |     case PROP_COMPLETED:  | 
2371  | 0  |       g_value_set_boolean (value, g_task_get_completed (task));  | 
2372  | 0  |       break;  | 
2373  | 0  |     }  | 
2374  | 0  | }  | 
2375  |  |  | 
2376  |  | static void  | 
2377  |  | g_task_class_init (GTaskClass *klass)  | 
2378  | 0  | { | 
2379  | 0  |   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);  | 
2380  |  | 
  | 
2381  |  | #ifdef G_ENABLE_DEBUG  | 
2382  |  |   gobject_class->constructed = g_task_constructed;  | 
2383  |  | #endif  | 
2384  | 0  |   gobject_class->get_property = g_task_get_property;  | 
2385  | 0  |   gobject_class->finalize = g_task_finalize;  | 
2386  |  |  | 
2387  |  |   /**  | 
2388  |  |    * GTask:completed:  | 
2389  |  |    *  | 
2390  |  |    * Whether the task has completed, meaning its callback (if set) has been  | 
2391  |  |    * invoked. This can only happen after g_task_return_pointer(),  | 
2392  |  |    * g_task_return_error() or one of the other return functions have been called  | 
2393  |  |    * on the task.  | 
2394  |  |    *  | 
2395  |  |    * This property is guaranteed to change from %FALSE to %TRUE exactly once.  | 
2396  |  |    *  | 
2397  |  |    * The #GObject::notify signal for this change is emitted in the same main  | 
2398  |  |    * context as the task’s callback, immediately after that callback is invoked.  | 
2399  |  |    *  | 
2400  |  |    * Since: 2.44  | 
2401  |  |    */  | 
2402  | 0  |   g_object_class_install_property (gobject_class, PROP_COMPLETED,  | 
2403  | 0  |     g_param_spec_boolean ("completed", | 
2404  | 0  |                           P_("Task completed"), | 
2405  | 0  |                           P_("Whether the task has completed yet"), | 
2406  | 0  |                           FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));  | 
2407  |  | 
  | 
2408  | 0  |   if (G_UNLIKELY (task_pool_max_counter == 0))  | 
2409  | 0  |     { | 
2410  |  |       /* We use two counters to track characteristics of the GTask thread pool.  | 
2411  |  |        * task pool max size - the value of g_thread_pool_set_max_threads()  | 
2412  |  |        * tasks running - the number of running threads  | 
2413  |  |        */  | 
2414  | 0  |       task_pool_max_counter = g_trace_define_int64_counter ("GIO", "task pool max size", "Maximum number of threads allowed in the GTask thread pool; see g_thread_pool_set_max_threads()"); | 
2415  | 0  |       tasks_running_counter = g_trace_define_int64_counter ("GIO", "tasks running", "Number of currently running tasks in the GTask thread pool"); | 
2416  | 0  |     }  | 
2417  | 0  | }  | 
2418  |  |  | 
2419  |  | static gpointer  | 
2420  |  | g_task_get_user_data (GAsyncResult *res)  | 
2421  | 0  | { | 
2422  | 0  |   return G_TASK (res)->callback_data;  | 
2423  | 0  | }  | 
2424  |  |  | 
2425  |  | static gboolean  | 
2426  |  | g_task_is_tagged (GAsyncResult *res,  | 
2427  |  |                   gpointer      source_tag)  | 
2428  | 0  | { | 
2429  | 0  |   return G_TASK (res)->source_tag == source_tag;  | 
2430  | 0  | }  | 
2431  |  |  | 
2432  |  | static void  | 
2433  |  | g_task_async_result_iface_init (GAsyncResultIface *iface)  | 
2434  | 0  | { | 
2435  | 0  |   iface->get_user_data = g_task_get_user_data;  | 
2436  | 0  |   iface->get_source_object = g_task_ref_source_object;  | 
2437  | 0  |   iface->is_tagged = g_task_is_tagged;  | 
2438  | 0  | }  |