Coverage Report

Created: 2025-06-13 06:55

/src/glib/gio/gsimpleasyncresult.c
Line
Count
Source (jump to first uncovered line)
1
/* GIO - GLib Input, Output and Streaming Library
2
 *
3
 * Copyright (C) 2006-2007 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
 * Author: Alexander Larsson <alexl@redhat.com>
21
 */
22
23
#include "config.h"
24
25
#include <string.h>
26
27
#include "gsimpleasyncresult.h"
28
#include "gasyncresult.h"
29
#include "gcancellable.h"
30
#include "gioscheduler.h"
31
#include <gio/gioerror.h>
32
#include "glibintl.h"
33
34
35
/**
36
 * SECTION:gsimpleasyncresult
37
 * @short_description: Simple asynchronous results implementation
38
 * @include: gio/gio.h
39
 * @see_also: #GAsyncResult, #GTask
40
 *
41
 * As of GLib 2.46, #GSimpleAsyncResult is deprecated in favor of
42
 * #GTask, which provides a simpler API.
43
 *
44
 * #GSimpleAsyncResult implements #GAsyncResult.
45
 *
46
 * GSimpleAsyncResult handles #GAsyncReadyCallbacks, error
47
 * reporting, operation cancellation and the final state of an operation,
48
 * completely transparent to the application. Results can be returned
49
 * as a pointer e.g. for functions that return data that is collected
50
 * asynchronously, a boolean value for checking the success or failure
51
 * of an operation, or a #gssize for operations which return the number
52
 * of bytes modified by the operation; all of the simple return cases
53
 * are covered.
54
 *
55
 * Most of the time, an application will not need to know of the details
56
 * of this API; it is handled transparently, and any necessary operations
57
 * are handled by #GAsyncResult's interface. However, if implementing a
58
 * new GIO module, for writing language bindings, or for complex
59
 * applications that need better control of how asynchronous operations
60
 * are completed, it is important to understand this functionality.
61
 *
62
 * GSimpleAsyncResults are tagged with the calling function to ensure
63
 * that asynchronous functions and their finishing functions are used
64
 * together correctly.
65
 *
66
 * To create a new #GSimpleAsyncResult, call g_simple_async_result_new().
67
 * If the result needs to be created for a #GError, use
68
 * g_simple_async_result_new_from_error() or
69
 * g_simple_async_result_new_take_error(). If a #GError is not available
70
 * (e.g. the asynchronous operation's doesn't take a #GError argument),
71
 * but the result still needs to be created for an error condition, use
72
 * g_simple_async_result_new_error() (or g_simple_async_result_set_error_va()
73
 * if your application or binding requires passing a variable argument list
74
 * directly), and the error can then be propagated through the use of
75
 * g_simple_async_result_propagate_error().
76
 *
77
 * An asynchronous operation can be made to ignore a cancellation event by
78
 * calling g_simple_async_result_set_handle_cancellation() with a
79
 * #GSimpleAsyncResult for the operation and %FALSE. This is useful for
80
 * operations that are dangerous to cancel, such as close (which would
81
 * cause a leak if cancelled before being run).
82
 *
83
 * GSimpleAsyncResult can integrate into GLib's event loop, #GMainLoop,
84
 * or it can use #GThreads.
85
 * g_simple_async_result_complete() will finish an I/O task directly
86
 * from the point where it is called. g_simple_async_result_complete_in_idle()
87
 * will finish it from an idle handler in the 
88
 * [thread-default main context][g-main-context-push-thread-default]
89
 * where the #GSimpleAsyncResult was created.
90
 * g_simple_async_result_run_in_thread() will run the job in a
91
 * separate thread and then use
92
 * g_simple_async_result_complete_in_idle() to deliver the result.
93
 *
94
 * To set the results of an asynchronous function,
95
 * g_simple_async_result_set_op_res_gpointer(),
96
 * g_simple_async_result_set_op_res_gboolean(), and
97
 * g_simple_async_result_set_op_res_gssize()
98
 * are provided, setting the operation's result to a gpointer, gboolean, or
99
 * gssize, respectively.
100
 *
101
 * Likewise, to get the result of an asynchronous function,
102
 * g_simple_async_result_get_op_res_gpointer(),
103
 * g_simple_async_result_get_op_res_gboolean(), and
104
 * g_simple_async_result_get_op_res_gssize() are
105
 * provided, getting the operation's result as a gpointer, gboolean, and
106
 * gssize, respectively.
107
 *
108
 * For the details of the requirements implementations must respect, see
109
 * #GAsyncResult.  A typical implementation of an asynchronous operation
110
 * using GSimpleAsyncResult looks something like this:
111
 *
112
 * |[<!-- language="C" -->
113
 * static void
114
 * baked_cb (Cake    *cake,
115
 *           gpointer user_data)
116
 * {
117
 *   // In this example, this callback is not given a reference to the cake,
118
 *   // so the GSimpleAsyncResult has to take a reference to it.
119
 *   GSimpleAsyncResult *result = user_data;
120
 *
121
 *   if (cake == NULL)
122
 *     g_simple_async_result_set_error (result,
123
 *                                      BAKER_ERRORS,
124
 *                                      BAKER_ERROR_NO_FLOUR,
125
 *                                      "Go to the supermarket");
126
 *   else
127
 *     g_simple_async_result_set_op_res_gpointer (result,
128
 *                                                g_object_ref (cake),
129
 *                                                g_object_unref);
130
 *
131
 *
132
 *   // In this example, we assume that baked_cb is called as a callback from
133
 *   // the mainloop, so it's safe to complete the operation synchronously here.
134
 *   // If, however, _baker_prepare_cake () might call its callback without
135
 *   // first returning to the mainloop — inadvisable, but some APIs do so —
136
 *   // we would need to use g_simple_async_result_complete_in_idle().
137
 *   g_simple_async_result_complete (result);
138
 *   g_object_unref (result);
139
 * }
140
 *
141
 * void
142
 * baker_bake_cake_async (Baker              *self,
143
 *                        guint               radius,
144
 *                        GAsyncReadyCallback callback,
145
 *                        gpointer            user_data)
146
 * {
147
 *   GSimpleAsyncResult *simple;
148
 *   Cake               *cake;
149
 *
150
 *   if (radius < 3)
151
 *     {
152
 *       g_simple_async_report_error_in_idle (G_OBJECT (self),
153
 *                                            callback,
154
 *                                            user_data,
155
 *                                            BAKER_ERRORS,
156
 *                                            BAKER_ERROR_TOO_SMALL,
157
 *                                            "%ucm radius cakes are silly",
158
 *                                            radius);
159
 *       return;
160
 *     }
161
 *
162
 *   simple = g_simple_async_result_new (G_OBJECT (self),
163
 *                                       callback,
164
 *                                       user_data,
165
 *                                       baker_bake_cake_async);
166
 *   cake = _baker_get_cached_cake (self, radius);
167
 *
168
 *   if (cake != NULL)
169
 *     {
170
 *       g_simple_async_result_set_op_res_gpointer (simple,
171
 *                                                  g_object_ref (cake),
172
 *                                                  g_object_unref);
173
 *       g_simple_async_result_complete_in_idle (simple);
174
 *       g_object_unref (simple);
175
 *       // Drop the reference returned by _baker_get_cached_cake();
176
 *       // the GSimpleAsyncResult has taken its own reference.
177
 *       g_object_unref (cake);
178
 *       return;
179
 *     }
180
 *
181
 *   _baker_prepare_cake (self, radius, baked_cb, simple);
182
 * }
183
 *
184
 * Cake *
185
 * baker_bake_cake_finish (Baker        *self,
186
 *                         GAsyncResult *result,
187
 *                         GError      **error)
188
 * {
189
 *   GSimpleAsyncResult *simple;
190
 *   Cake               *cake;
191
 *
192
 *   g_return_val_if_fail (g_simple_async_result_is_valid (result,
193
 *                                                         G_OBJECT (self),
194
 *                                                         baker_bake_cake_async),
195
 *                         NULL);
196
 *
197
 *   simple = (GSimpleAsyncResult *) result;
198
 *
199
 *   if (g_simple_async_result_propagate_error (simple, error))
200
 *     return NULL;
201
 *
202
 *   cake = CAKE (g_simple_async_result_get_op_res_gpointer (simple));
203
 *   return g_object_ref (cake);
204
 * }
205
 * ]|
206
 */
207
208
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
209
210
static void g_simple_async_result_async_result_iface_init (GAsyncResultIface       *iface);
211
212
struct _GSimpleAsyncResult
213
{
214
  GObject parent_instance;
215
216
  GObject *source_object;
217
  GAsyncReadyCallback callback;
218
  gpointer user_data;
219
  GMainContext *context;
220
  GError *error;
221
  gboolean failed;
222
  gboolean handle_cancellation;
223
  GCancellable *check_cancellable;
224
225
  gpointer source_tag;
226
227
  union {
228
    gpointer v_pointer;
229
    gboolean v_boolean;
230
    gssize   v_ssize;
231
  } op_res;
232
233
  GDestroyNotify destroy_op_res;
234
};
235
236
struct _GSimpleAsyncResultClass
237
{
238
  GObjectClass parent_class;
239
};
240
241
242
G_DEFINE_TYPE_WITH_CODE (GSimpleAsyncResult, g_simple_async_result, G_TYPE_OBJECT,
243
       G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_RESULT,
244
            g_simple_async_result_async_result_iface_init))
245
246
static void
247
clear_op_res (GSimpleAsyncResult *simple)
248
0
{
249
0
  if (simple->destroy_op_res)
250
0
    simple->destroy_op_res (simple->op_res.v_pointer);
251
0
  simple->destroy_op_res = NULL;
252
0
  simple->op_res.v_ssize = 0;
253
0
}
254
255
static void
256
g_simple_async_result_finalize (GObject *object)
257
0
{
258
0
  GSimpleAsyncResult *simple;
259
260
0
  simple = G_SIMPLE_ASYNC_RESULT (object);
261
262
0
  if (simple->source_object)
263
0
    g_object_unref (simple->source_object);
264
265
0
  if (simple->check_cancellable)
266
0
    g_object_unref (simple->check_cancellable);
267
268
0
  g_main_context_unref (simple->context);
269
270
0
  clear_op_res (simple);
271
272
0
  if (simple->error)
273
0
    g_error_free (simple->error);
274
275
0
  G_OBJECT_CLASS (g_simple_async_result_parent_class)->finalize (object);
276
0
}
277
278
static void
279
g_simple_async_result_class_init (GSimpleAsyncResultClass *klass)
280
0
{
281
0
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
282
 
283
0
  gobject_class->finalize = g_simple_async_result_finalize;
284
0
}
285
286
static void
287
g_simple_async_result_init (GSimpleAsyncResult *simple)
288
0
{
289
0
  simple->handle_cancellation = TRUE;
290
291
0
  simple->context = g_main_context_ref_thread_default ();
292
0
}
293
294
/**
295
 * g_simple_async_result_new:
296
 * @source_object: (nullable): a #GObject, or %NULL.
297
 * @callback: (scope async): a #GAsyncReadyCallback.
298
 * @user_data: user data passed to @callback.
299
 * @source_tag: the asynchronous function.
300
 *
301
 * Creates a #GSimpleAsyncResult.
302
 *
303
 * The common convention is to create the #GSimpleAsyncResult in the
304
 * function that starts the asynchronous operation and use that same
305
 * function as the @source_tag.
306
 *
307
 * If your operation supports cancellation with #GCancellable (which it
308
 * probably should) then you should provide the user's cancellable to
309
 * g_simple_async_result_set_check_cancellable() immediately after
310
 * this function returns.
311
 *
312
 * Returns: a #GSimpleAsyncResult.
313
 *
314
 * Deprecated: 2.46: Use g_task_new() instead.
315
 **/
316
GSimpleAsyncResult *
317
g_simple_async_result_new (GObject             *source_object,
318
                           GAsyncReadyCallback  callback,
319
                           gpointer             user_data,
320
                           gpointer             source_tag)
321
0
{
322
0
  GSimpleAsyncResult *simple;
323
324
0
  g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
325
326
0
  simple = g_object_new (G_TYPE_SIMPLE_ASYNC_RESULT, NULL);
327
0
  simple->callback = callback;
328
0
  if (source_object)
329
0
    simple->source_object = g_object_ref (source_object);
330
0
  else
331
0
    simple->source_object = NULL;
332
0
  simple->user_data = user_data;
333
0
  simple->source_tag = source_tag;
334
 
335
0
  return simple;
336
0
}
337
338
/**
339
 * g_simple_async_result_new_from_error:
340
 * @source_object: (nullable): a #GObject, or %NULL.
341
 * @callback: (scope async): a #GAsyncReadyCallback.
342
 * @user_data: user data passed to @callback.
343
 * @error: a #GError
344
 *
345
 * Creates a #GSimpleAsyncResult from an error condition.
346
 *
347
 * Returns: a #GSimpleAsyncResult.
348
 *
349
 * Deprecated: 2.46: Use g_task_new() and g_task_return_error() instead.
350
 **/
351
GSimpleAsyncResult *
352
g_simple_async_result_new_from_error (GObject             *source_object,
353
                                      GAsyncReadyCallback  callback,
354
                                      gpointer             user_data,
355
                                      const GError        *error)
356
0
{
357
0
  GSimpleAsyncResult *simple;
358
359
0
  g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
360
361
0
  simple = g_simple_async_result_new (source_object,
362
0
              callback,
363
0
              user_data, NULL);
364
0
  g_simple_async_result_set_from_error (simple, error);
365
366
0
  return simple;
367
0
}
368
369
/**
370
 * g_simple_async_result_new_take_error: (skip)
371
 * @source_object: (nullable): a #GObject, or %NULL
372
 * @callback: (scope async): a #GAsyncReadyCallback.
373
 * @user_data: user data passed to @callback.
374
 * @error: a #GError
375
 *
376
 * Creates a #GSimpleAsyncResult from an error condition, and takes over the
377
 * caller's ownership of @error, so the caller does not need to free it anymore.
378
 *
379
 * Returns: a #GSimpleAsyncResult
380
 *
381
 * Since: 2.28
382
 *
383
 * Deprecated: 2.46: Use g_task_new() and g_task_return_error() instead.
384
 **/
385
GSimpleAsyncResult *
386
g_simple_async_result_new_take_error (GObject             *source_object,
387
                                      GAsyncReadyCallback  callback,
388
                                      gpointer             user_data,
389
                                      GError              *error)
390
0
{
391
0
  GSimpleAsyncResult *simple;
392
393
0
  g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
394
395
0
  simple = g_simple_async_result_new (source_object,
396
0
              callback,
397
0
              user_data, NULL);
398
0
  g_simple_async_result_take_error (simple, error);
399
400
0
  return simple;
401
0
}
402
403
/**
404
 * g_simple_async_result_new_error:
405
 * @source_object: (nullable): a #GObject, or %NULL.
406
 * @callback: (scope async): a #GAsyncReadyCallback.
407
 * @user_data: user data passed to @callback.
408
 * @domain: a #GQuark.
409
 * @code: an error code.
410
 * @format: a string with format characters.
411
 * @...: a list of values to insert into @format.
412
 *
413
 * Creates a new #GSimpleAsyncResult with a set error.
414
 *
415
 * Returns: a #GSimpleAsyncResult.
416
 *
417
 * Deprecated: 2.46: Use g_task_new() and g_task_return_new_error() instead.
418
 **/
419
GSimpleAsyncResult *
420
g_simple_async_result_new_error (GObject             *source_object,
421
                                 GAsyncReadyCallback  callback,
422
                                 gpointer             user_data,
423
                                 GQuark               domain,
424
                                 gint                 code,
425
                                 const char          *format,
426
                                 ...)
427
0
{
428
0
  GSimpleAsyncResult *simple;
429
0
  va_list args;
430
 
431
0
  g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
432
0
  g_return_val_if_fail (domain != 0, NULL);
433
0
  g_return_val_if_fail (format != NULL, NULL);
434
435
0
  simple = g_simple_async_result_new (source_object,
436
0
              callback,
437
0
              user_data, NULL);
438
439
0
  va_start (args, format);
440
0
  g_simple_async_result_set_error_va (simple, domain, code, format, args);
441
0
  va_end (args);
442
 
443
0
  return simple;
444
0
}
445
446
447
static gpointer
448
g_simple_async_result_get_user_data (GAsyncResult *res)
449
0
{
450
0
  return G_SIMPLE_ASYNC_RESULT (res)->user_data;
451
0
}
452
453
static GObject *
454
g_simple_async_result_get_source_object (GAsyncResult *res)
455
0
{
456
0
  if (G_SIMPLE_ASYNC_RESULT (res)->source_object)
457
0
    return g_object_ref (G_SIMPLE_ASYNC_RESULT (res)->source_object);
458
0
  return NULL;
459
0
}
460
461
static gboolean
462
g_simple_async_result_is_tagged (GAsyncResult *res,
463
         gpointer      source_tag)
464
0
{
465
0
  return G_SIMPLE_ASYNC_RESULT (res)->source_tag == source_tag;
466
0
}
467
468
static void
469
g_simple_async_result_async_result_iface_init (GAsyncResultIface *iface)
470
0
{
471
0
  iface->get_user_data = g_simple_async_result_get_user_data;
472
0
  iface->get_source_object = g_simple_async_result_get_source_object;
473
0
  iface->is_tagged = g_simple_async_result_is_tagged;
474
0
}
475
476
/**
477
 * g_simple_async_result_set_handle_cancellation:
478
 * @simple: a #GSimpleAsyncResult.
479
 * @handle_cancellation: a #gboolean.
480
 *
481
 * Sets whether to handle cancellation within the asynchronous operation.
482
 *
483
 * This function has nothing to do with
484
 * g_simple_async_result_set_check_cancellable().  It only refers to the
485
 * #GCancellable passed to g_simple_async_result_run_in_thread().
486
 *
487
 * Deprecated: 2.46
488
 **/
489
void
490
g_simple_async_result_set_handle_cancellation (GSimpleAsyncResult *simple,
491
                                               gboolean            handle_cancellation)
492
0
{
493
0
  g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
494
0
  simple->handle_cancellation = handle_cancellation;
495
0
}
496
497
/**
498
 * g_simple_async_result_get_source_tag: (skip)
499
 * @simple: a #GSimpleAsyncResult.
500
 *
501
 * Gets the source tag for the #GSimpleAsyncResult.
502
 *
503
 * Returns: a #gpointer to the source object for the #GSimpleAsyncResult.
504
 *
505
 * Deprecated: 2.46. Use #GTask and g_task_get_source_tag() instead.
506
 **/
507
gpointer
508
g_simple_async_result_get_source_tag (GSimpleAsyncResult *simple)
509
0
{
510
0
  g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
511
0
  return simple->source_tag;
512
0
}
513
514
/**
515
 * g_simple_async_result_propagate_error:
516
 * @simple: a #GSimpleAsyncResult.
517
 * @dest: (out): a location to propagate the error to.
518
 *
519
 * Propagates an error from within the simple asynchronous result to
520
 * a given destination.
521
 *
522
 * If the #GCancellable given to a prior call to
523
 * g_simple_async_result_set_check_cancellable() is cancelled then this
524
 * function will return %TRUE with @dest set appropriately.
525
 *
526
 * Returns: %TRUE if the error was propagated to @dest. %FALSE otherwise.
527
 *
528
 * Deprecated: 2.46: Use #GTask instead.
529
 **/
530
gboolean
531
g_simple_async_result_propagate_error (GSimpleAsyncResult  *simple,
532
                                       GError             **dest)
533
0
{
534
0
  g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
535
536
0
  if (g_cancellable_set_error_if_cancelled (simple->check_cancellable, dest))
537
0
    return TRUE;
538
539
0
  if (simple->failed)
540
0
    {
541
0
      g_propagate_error (dest, simple->error);
542
0
      simple->error = NULL;
543
0
      return TRUE;
544
0
    }
545
546
0
  return FALSE;
547
0
}
548
549
/**
550
 * g_simple_async_result_set_op_res_gpointer: (skip)
551
 * @simple: a #GSimpleAsyncResult.
552
 * @op_res: a pointer result from an asynchronous function.
553
 * @destroy_op_res: a #GDestroyNotify function.
554
 *
555
 * Sets the operation result within the asynchronous result to a pointer.
556
 *
557
 * Deprecated: 2.46: Use #GTask and g_task_return_pointer() instead.
558
 **/
559
void
560
g_simple_async_result_set_op_res_gpointer (GSimpleAsyncResult *simple,
561
                                           gpointer            op_res,
562
                                           GDestroyNotify      destroy_op_res)
563
0
{
564
0
  g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
565
566
0
  clear_op_res (simple);
567
0
  simple->op_res.v_pointer = op_res;
568
0
  simple->destroy_op_res = destroy_op_res;
569
0
}
570
571
/**
572
 * g_simple_async_result_get_op_res_gpointer: (skip)
573
 * @simple: a #GSimpleAsyncResult.
574
 *
575
 * Gets a pointer result as returned by the asynchronous function.
576
 *
577
 * Returns: a pointer from the result.
578
 *
579
 * Deprecated: 2.46: Use #GTask and g_task_propagate_pointer() instead.
580
 **/
581
gpointer
582
g_simple_async_result_get_op_res_gpointer (GSimpleAsyncResult *simple)
583
0
{
584
0
  g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
585
0
  return simple->op_res.v_pointer;
586
0
}
587
588
/**
589
 * g_simple_async_result_set_op_res_gssize:
590
 * @simple: a #GSimpleAsyncResult.
591
 * @op_res: a #gssize.
592
 *
593
 * Sets the operation result within the asynchronous result to
594
 * the given @op_res.
595
 *
596
 * Deprecated: 2.46: Use #GTask and g_task_return_int() instead.
597
 **/
598
void
599
g_simple_async_result_set_op_res_gssize (GSimpleAsyncResult *simple,
600
                                         gssize              op_res)
601
0
{
602
0
  g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
603
0
  clear_op_res (simple);
604
0
  simple->op_res.v_ssize = op_res;
605
0
}
606
607
/**
608
 * g_simple_async_result_get_op_res_gssize:
609
 * @simple: a #GSimpleAsyncResult.
610
 *
611
 * Gets a gssize from the asynchronous result.
612
 *
613
 * Returns: a gssize returned from the asynchronous function.
614
 *
615
 * Deprecated: 2.46: Use #GTask and g_task_propagate_int() instead.
616
 **/
617
gssize
618
g_simple_async_result_get_op_res_gssize (GSimpleAsyncResult *simple)
619
0
{
620
0
  g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), 0);
621
0
  return simple->op_res.v_ssize;
622
0
}
623
624
/**
625
 * g_simple_async_result_set_op_res_gboolean:
626
 * @simple: a #GSimpleAsyncResult.
627
 * @op_res: a #gboolean.
628
 *
629
 * Sets the operation result to a boolean within the asynchronous result.
630
 *
631
 * Deprecated: 2.46: Use #GTask and g_task_return_boolean() instead.
632
 **/
633
void
634
g_simple_async_result_set_op_res_gboolean (GSimpleAsyncResult *simple,
635
                                           gboolean            op_res)
636
0
{
637
0
  g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
638
0
  clear_op_res (simple);
639
0
  simple->op_res.v_boolean = !!op_res;
640
0
}
641
642
/**
643
 * g_simple_async_result_get_op_res_gboolean:
644
 * @simple: a #GSimpleAsyncResult.
645
 *
646
 * Gets the operation result boolean from within the asynchronous result.
647
 *
648
 * Returns: %TRUE if the operation's result was %TRUE, %FALSE
649
 *     if the operation's result was %FALSE.
650
 *
651
 * Deprecated: 2.46: Use #GTask and g_task_propagate_boolean() instead.
652
 **/
653
gboolean
654
g_simple_async_result_get_op_res_gboolean (GSimpleAsyncResult *simple)
655
0
{
656
0
  g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
657
0
  return simple->op_res.v_boolean;
658
0
}
659
660
/**
661
 * g_simple_async_result_set_from_error:
662
 * @simple: a #GSimpleAsyncResult.
663
 * @error: #GError.
664
 *
665
 * Sets the result from a #GError.
666
 *
667
 * Deprecated: 2.46: Use #GTask and g_task_return_error() instead.
668
 **/
669
void
670
g_simple_async_result_set_from_error (GSimpleAsyncResult *simple,
671
                                      const GError       *error)
672
0
{
673
0
  g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
674
0
  g_return_if_fail (error != NULL);
675
676
0
  if (simple->error)
677
0
    g_error_free (simple->error);
678
0
  simple->error = g_error_copy (error);
679
0
  simple->failed = TRUE;
680
0
}
681
682
/**
683
 * g_simple_async_result_take_error: (skip)
684
 * @simple: a #GSimpleAsyncResult
685
 * @error: a #GError
686
 *
687
 * Sets the result from @error, and takes over the caller's ownership
688
 * of @error, so the caller does not need to free it any more.
689
 *
690
 * Since: 2.28
691
 *
692
 * Deprecated: 2.46: Use #GTask and g_task_return_error() instead.
693
 **/
694
void
695
g_simple_async_result_take_error (GSimpleAsyncResult *simple,
696
                                  GError             *error)
697
0
{
698
0
  g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
699
0
  g_return_if_fail (error != NULL);
700
701
0
  if (simple->error)
702
0
    g_error_free (simple->error);
703
0
  simple->error = error;
704
0
  simple->failed = TRUE;
705
0
}
706
707
/**
708
 * g_simple_async_result_set_error_va: (skip)
709
 * @simple: a #GSimpleAsyncResult.
710
 * @domain: a #GQuark (usually %G_IO_ERROR).
711
 * @code: an error code.
712
 * @format: a formatted error reporting string.
713
 * @args: va_list of arguments.
714
 *
715
 * Sets an error within the asynchronous result without a #GError.
716
 * Unless writing a binding, see g_simple_async_result_set_error().
717
 *
718
 * Deprecated: 2.46: Use #GTask and g_task_return_error() instead.
719
 **/
720
void
721
g_simple_async_result_set_error_va (GSimpleAsyncResult *simple,
722
                                    GQuark              domain,
723
                                    gint                code,
724
                                    const char         *format,
725
                                    va_list             args)
726
0
{
727
0
  g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
728
0
  g_return_if_fail (domain != 0);
729
0
  g_return_if_fail (format != NULL);
730
731
0
  if (simple->error)
732
0
    g_error_free (simple->error);
733
0
  simple->error = g_error_new_valist (domain, code, format, args);
734
0
  simple->failed = TRUE;
735
0
}
736
737
/**
738
 * g_simple_async_result_set_error: (skip)
739
 * @simple: a #GSimpleAsyncResult.
740
 * @domain: a #GQuark (usually %G_IO_ERROR).
741
 * @code: an error code.
742
 * @format: a formatted error reporting string.
743
 * @...: a list of variables to fill in @format.
744
 *
745
 * Sets an error within the asynchronous result without a #GError.
746
 *
747
 * Deprecated: 2.46: Use #GTask and g_task_return_new_error() instead.
748
 **/
749
void
750
g_simple_async_result_set_error (GSimpleAsyncResult *simple,
751
                                 GQuark              domain,
752
                                 gint                code,
753
                                 const char         *format,
754
                                 ...)
755
0
{
756
0
  va_list args;
757
758
0
  g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
759
0
  g_return_if_fail (domain != 0);
760
0
  g_return_if_fail (format != NULL);
761
762
0
  va_start (args, format);
763
0
  g_simple_async_result_set_error_va (simple, domain, code, format, args);
764
0
  va_end (args);
765
0
}
766
767
/**
768
 * g_simple_async_result_complete:
769
 * @simple: a #GSimpleAsyncResult.
770
 *
771
 * Completes an asynchronous I/O job immediately. Must be called in
772
 * the thread where the asynchronous result was to be delivered, as it
773
 * invokes the callback directly. If you are in a different thread use
774
 * g_simple_async_result_complete_in_idle().
775
 *
776
 * Calling this function takes a reference to @simple for as long as
777
 * is needed to complete the call.
778
 *
779
 * Deprecated: 2.46: Use #GTask instead.
780
 **/
781
void
782
g_simple_async_result_complete (GSimpleAsyncResult *simple)
783
0
{
784
0
#ifndef G_DISABLE_CHECKS
785
0
  GSource *current_source;
786
0
  GMainContext *current_context;
787
0
#endif
788
789
0
  g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
790
791
0
#ifndef G_DISABLE_CHECKS
792
0
  current_source = g_main_current_source ();
793
0
  if (current_source && !g_source_is_destroyed (current_source))
794
0
    {
795
0
      current_context = g_source_get_context (current_source);
796
0
      if (simple->context != current_context)
797
0
  g_warning ("g_simple_async_result_complete() called from wrong context!");
798
0
    }
799
0
#endif
800
801
0
  if (simple->callback)
802
0
    {
803
0
      g_main_context_push_thread_default (simple->context);
804
0
      simple->callback (simple->source_object,
805
0
      G_ASYNC_RESULT (simple),
806
0
      simple->user_data);
807
0
      g_main_context_pop_thread_default (simple->context);
808
0
    }
809
0
}
810
811
static gboolean
812
complete_in_idle_cb (gpointer data)
813
0
{
814
0
  GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (data);
815
816
0
  g_simple_async_result_complete (simple);
817
818
0
  return FALSE;
819
0
}
820
821
/**
822
 * g_simple_async_result_complete_in_idle:
823
 * @simple: a #GSimpleAsyncResult.
824
 *
825
 * Completes an asynchronous function in an idle handler in the
826
 * [thread-default main context][g-main-context-push-thread-default]
827
 * of the thread that @simple was initially created in
828
 * (and re-pushes that context around the invocation of the callback).
829
 *
830
 * Calling this function takes a reference to @simple for as long as
831
 * is needed to complete the call.
832
 *
833
 * Deprecated: 2.46: Use #GTask instead.
834
 */
835
void
836
g_simple_async_result_complete_in_idle (GSimpleAsyncResult *simple)
837
0
{
838
0
  GSource *source;
839
840
0
  g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
841
842
0
  g_object_ref (simple);
843
 
844
0
  source = g_idle_source_new ();
845
0
  g_source_set_priority (source, G_PRIORITY_DEFAULT);
846
0
  g_source_set_callback (source, complete_in_idle_cb, simple, g_object_unref);
847
0
  g_source_set_static_name (source, "[gio] complete_in_idle_cb");
848
849
0
  g_source_attach (source, simple->context);
850
0
  g_source_unref (source);
851
0
}
852
853
typedef struct {
854
  GSimpleAsyncResult *simple;
855
  GCancellable *cancellable;
856
  GSimpleAsyncThreadFunc func;
857
} RunInThreadData;
858
859
860
static gboolean
861
complete_in_idle_cb_for_thread (gpointer _data)
862
0
{
863
0
  RunInThreadData *data = _data;
864
0
  GSimpleAsyncResult *simple;
865
866
0
  simple = data->simple;
867
 
868
0
  if (simple->handle_cancellation &&
869
0
      g_cancellable_is_cancelled (data->cancellable))
870
0
    g_simple_async_result_set_error (simple,
871
0
                                     G_IO_ERROR,
872
0
                                     G_IO_ERROR_CANCELLED,
873
0
                                     "%s", _("Operation was cancelled"));
874
 
875
0
  g_simple_async_result_complete (simple);
876
877
0
  if (data->cancellable)
878
0
    g_object_unref (data->cancellable);
879
0
  g_object_unref (data->simple);
880
0
  g_free (data);
881
 
882
0
  return FALSE;
883
0
}
884
885
static gboolean
886
run_in_thread (GIOSchedulerJob *job,
887
               GCancellable    *c,
888
               gpointer         _data)
889
0
{
890
0
  RunInThreadData *data = _data;
891
0
  GSimpleAsyncResult *simple = data->simple;
892
0
  GSource *source;
893
 
894
0
  if (simple->handle_cancellation &&
895
0
      g_cancellable_is_cancelled (c))
896
0
    g_simple_async_result_set_error (simple,
897
0
                                     G_IO_ERROR,
898
0
                                     G_IO_ERROR_CANCELLED,
899
0
                                     "%s", _("Operation was cancelled"));
900
0
  else
901
0
    data->func (simple,
902
0
                simple->source_object,
903
0
                c);
904
905
0
  source = g_idle_source_new ();
906
0
  g_source_set_priority (source, G_PRIORITY_DEFAULT);
907
0
  g_source_set_callback (source, complete_in_idle_cb_for_thread, data, NULL);
908
0
  g_source_set_static_name (source, "[gio] complete_in_idle_cb_for_thread");
909
910
0
  g_source_attach (source, simple->context);
911
0
  g_source_unref (source);
912
913
0
  return FALSE;
914
0
}
915
916
/**
917
 * g_simple_async_result_run_in_thread: (skip)
918
 * @simple: a #GSimpleAsyncResult.
919
 * @func: a #GSimpleAsyncThreadFunc.
920
 * @io_priority: the io priority of the request.
921
 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
922
 *
923
 * Runs the asynchronous job in a separate thread and then calls
924
 * g_simple_async_result_complete_in_idle() on @simple to return
925
 * the result to the appropriate main loop.
926
 *
927
 * Calling this function takes a reference to @simple for as long as
928
 * is needed to run the job and report its completion.
929
 *
930
 * Deprecated: 2.46: Use #GTask and g_task_run_in_thread() instead.
931
 */
932
void
933
g_simple_async_result_run_in_thread (GSimpleAsyncResult     *simple,
934
                                     GSimpleAsyncThreadFunc  func,
935
                                     int                     io_priority,
936
                                     GCancellable           *cancellable)
937
0
{
938
0
  RunInThreadData *data;
939
940
0
  g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
941
0
  g_return_if_fail (func != NULL);
942
943
0
  data = g_new (RunInThreadData, 1);
944
0
  data->func = func;
945
0
  data->simple = g_object_ref (simple);
946
0
  data->cancellable = cancellable;
947
0
  if (cancellable)
948
0
    g_object_ref (cancellable);
949
0
  G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
950
0
  g_io_scheduler_push_job (run_in_thread, data, NULL, io_priority, cancellable);
951
0
  G_GNUC_END_IGNORE_DEPRECATIONS;
952
0
}
953
954
/**
955
 * g_simple_async_result_is_valid:
956
 * @result: the #GAsyncResult passed to the _finish function.
957
 * @source: (nullable): the #GObject passed to the _finish function.
958
 * @source_tag: (nullable): the asynchronous function.
959
 *
960
 * Ensures that the data passed to the _finish function of an async
961
 * operation is consistent.  Three checks are performed.
962
 *
963
 * First, @result is checked to ensure that it is really a
964
 * #GSimpleAsyncResult.  Second, @source is checked to ensure that it
965
 * matches the source object of @result.  Third, @source_tag is
966
 * checked to ensure that it is equal to the @source_tag argument given
967
 * to g_simple_async_result_new() (which, by convention, is a pointer
968
 * to the _async function corresponding to the _finish function from
969
 * which this function is called).  (Alternatively, if either
970
 * @source_tag or @result's source tag is %NULL, then the source tag
971
 * check is skipped.)
972
 *
973
 * Returns: #TRUE if all checks passed or #FALSE if any failed.
974
 *
975
 * Since: 2.20
976
 *
977
 * Deprecated: 2.46: Use #GTask and g_task_is_valid() instead.
978
 **/
979
gboolean
980
g_simple_async_result_is_valid (GAsyncResult *result,
981
                                GObject      *source,
982
                                gpointer      source_tag)
983
0
{
984
0
  GSimpleAsyncResult *simple;
985
0
  GObject *cmp_source;
986
0
  gpointer result_source_tag;
987
988
0
  if (!G_IS_SIMPLE_ASYNC_RESULT (result))
989
0
    return FALSE;
990
0
  simple = (GSimpleAsyncResult *)result;
991
992
0
  cmp_source = g_async_result_get_source_object (result);
993
0
  if (cmp_source != source)
994
0
    {
995
0
      if (cmp_source != NULL)
996
0
        g_object_unref (cmp_source);
997
0
      return FALSE;
998
0
    }
999
0
  if (cmp_source != NULL)
1000
0
    g_object_unref (cmp_source);
1001
1002
0
  result_source_tag = g_simple_async_result_get_source_tag (simple);
1003
0
  return source_tag == NULL || result_source_tag == NULL ||
1004
0
         source_tag == result_source_tag;
1005
0
}
1006
1007
/**
1008
 * g_simple_async_report_error_in_idle: (skip)
1009
 * @object: (nullable): a #GObject, or %NULL.
1010
 * @callback: a #GAsyncReadyCallback.
1011
 * @user_data: user data passed to @callback.
1012
 * @domain: a #GQuark containing the error domain (usually %G_IO_ERROR).
1013
 * @code: a specific error code.
1014
 * @format: a formatted error reporting string.
1015
 * @...: a list of variables to fill in @format.
1016
 *
1017
 * Reports an error in an asynchronous function in an idle function by
1018
 * directly setting the contents of the #GAsyncResult with the given error
1019
 * information.
1020
 *
1021
 * Deprecated: 2.46: Use g_task_report_error().
1022
 **/
1023
void
1024
g_simple_async_report_error_in_idle (GObject             *object,
1025
                                     GAsyncReadyCallback  callback,
1026
                                     gpointer             user_data,
1027
                                     GQuark               domain,
1028
                                     gint                 code,
1029
                                     const char          *format,
1030
                                     ...)
1031
0
{
1032
0
  GSimpleAsyncResult *simple;
1033
0
  va_list args;
1034
 
1035
0
  g_return_if_fail (!object || G_IS_OBJECT (object));
1036
0
  g_return_if_fail (domain != 0);
1037
0
  g_return_if_fail (format != NULL);
1038
1039
0
  simple = g_simple_async_result_new (object,
1040
0
              callback,
1041
0
              user_data, NULL);
1042
1043
0
  va_start (args, format);
1044
0
  g_simple_async_result_set_error_va (simple, domain, code, format, args);
1045
0
  va_end (args);
1046
0
  g_simple_async_result_complete_in_idle (simple);
1047
0
  g_object_unref (simple);
1048
0
}
1049
1050
/**
1051
 * g_simple_async_report_gerror_in_idle:
1052
 * @object: (nullable): a #GObject, or %NULL
1053
 * @callback: (scope async): a #GAsyncReadyCallback.
1054
 * @user_data: user data passed to @callback.
1055
 * @error: the #GError to report
1056
 *
1057
 * Reports an error in an idle function. Similar to
1058
 * g_simple_async_report_error_in_idle(), but takes a #GError rather
1059
 * than building a new one.
1060
 *
1061
 * Deprecated: 2.46: Use g_task_report_error().
1062
 **/
1063
void
1064
g_simple_async_report_gerror_in_idle (GObject *object,
1065
              GAsyncReadyCallback callback,
1066
              gpointer user_data,
1067
              const GError *error)
1068
0
{
1069
0
  GSimpleAsyncResult *simple;
1070
 
1071
0
  g_return_if_fail (!object || G_IS_OBJECT (object));
1072
0
  g_return_if_fail (error != NULL);
1073
1074
0
  simple = g_simple_async_result_new_from_error (object,
1075
0
             callback,
1076
0
             user_data,
1077
0
             error);
1078
0
  g_simple_async_result_complete_in_idle (simple);
1079
0
  g_object_unref (simple);
1080
0
}
1081
1082
/**
1083
 * g_simple_async_report_take_gerror_in_idle: (skip)
1084
 * @object: (nullable): a #GObject, or %NULL
1085
 * @callback: a #GAsyncReadyCallback.
1086
 * @user_data: user data passed to @callback.
1087
 * @error: the #GError to report
1088
 *
1089
 * Reports an error in an idle function. Similar to
1090
 * g_simple_async_report_gerror_in_idle(), but takes over the caller's
1091
 * ownership of @error, so the caller does not have to free it any more.
1092
 *
1093
 * Since: 2.28
1094
 *
1095
 * Deprecated: 2.46: Use g_task_report_error().
1096
 **/
1097
void
1098
g_simple_async_report_take_gerror_in_idle (GObject *object,
1099
                                           GAsyncReadyCallback callback,
1100
                                           gpointer user_data,
1101
                                           GError *error)
1102
0
{
1103
0
  GSimpleAsyncResult *simple;
1104
1105
0
  g_return_if_fail (!object || G_IS_OBJECT (object));
1106
0
  g_return_if_fail (error != NULL);
1107
1108
0
  simple = g_simple_async_result_new_take_error (object,
1109
0
                                                 callback,
1110
0
                                                 user_data,
1111
0
                                                 error);
1112
0
  g_simple_async_result_complete_in_idle (simple);
1113
0
  g_object_unref (simple);
1114
0
}
1115
1116
/**
1117
 * g_simple_async_result_set_check_cancellable:
1118
 * @simple: a #GSimpleAsyncResult
1119
 * @check_cancellable: (nullable): a #GCancellable to check, or %NULL to unset
1120
 *
1121
 * Sets a #GCancellable to check before dispatching results.
1122
 *
1123
 * This function has one very specific purpose: the provided cancellable
1124
 * is checked at the time of g_simple_async_result_propagate_error() If
1125
 * it is cancelled, these functions will return an "Operation was
1126
 * cancelled" error (%G_IO_ERROR_CANCELLED).
1127
 *
1128
 * Implementors of cancellable asynchronous functions should use this in
1129
 * order to provide a guarantee to their callers that cancelling an
1130
 * async operation will reliably result in an error being returned for
1131
 * that operation (even if a positive result for the operation has
1132
 * already been sent as an idle to the main context to be dispatched).
1133
 *
1134
 * The checking described above is done regardless of any call to the
1135
 * unrelated g_simple_async_result_set_handle_cancellation() function.
1136
 *
1137
 * Since: 2.32
1138
 *
1139
 * Deprecated: 2.46: Use #GTask instead.
1140
 **/
1141
void
1142
g_simple_async_result_set_check_cancellable (GSimpleAsyncResult *simple,
1143
                                             GCancellable *check_cancellable)
1144
0
{
1145
0
  g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
1146
0
  g_return_if_fail (check_cancellable == NULL || G_IS_CANCELLABLE (check_cancellable));
1147
1148
0
  g_clear_object (&simple->check_cancellable);
1149
0
  if (check_cancellable)
1150
0
    simple->check_cancellable = g_object_ref (check_cancellable);
1151
0
}
1152
1153
G_GNUC_END_IGNORE_DEPRECATIONS