Debian Codegraph @

src/dex-async-result.c

raw symbols SCIP
1 /*
2 * dex-async-result.c
3 *
4 * Copyright 2022-2023 Christian Hergert <>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this library; if not, see <http://www.gnu.org/licenses/>.
18 *
19 * SPDX-License-Identifier: LGPL-2.1-or-later
20 */
21
22 #include "config.h"
23
24 #include "dex-async-result.h"
25 #include "dex-cancellable.h"
26 #include "dex-compat-private.h"
27 #include "dex-error.h"
28
29 /**
30 * DexAsyncResult:
31 *
32 * `DexAsyncResult` is used to integrate a `DexFuture` with `GAsyncResult`.
33 *
34 * Use this class when you need to expose the traditional async/finish
35 * behavior of `GAsyncResult`.
36 */
37
38 struct _DexAsyncResult
39 {
40 GObject parent_instance;
41 GMutex mutex;
42 GMainContext *main_context;
43 gpointer source_object;
44 GCancellable *cancellable;
45 GAsyncReadyCallback callback;
46 gpointer user_data;
47 gpointer tag;
48 DexFuture *future;
49 char *name;
50 int priority;
51 guint name_is_static : 1;
52 guint await_once : 1;
53 guint await_returned : 1;
54 };
55
56 static gboolean
57 dex_async_result_is_tagged (GAsyncResult *async_result,
58 gpointer tag)
59 {
60 DexAsyncResult *self = DEX_ASYNC_RESULT (async_result);
61
62 return self->tag == tag;
63 }
64
65 static gpointer
66 dex_async_result_get_user_data (GAsyncResult *async_result)
67 {
68 DexAsyncResult *self = DEX_ASYNC_RESULT (async_result);
69
70 return self->user_data;
71 }
72
73 static GObject *
74 dex_async_result_get_source_object (GAsyncResult *async_result)
75 {
76 DexAsyncResult *self = DEX_ASYNC_RESULT (async_result);
77
78 return self->source_object ? g_object_ref (self->source_object) : NULL;
79 }
80
81 static void
82 async_result_iface_init (GAsyncResultIface *iface)
83 {
84 iface->get_user_data = dex_async_result_get_user_data;
85 iface->get_source_object = dex_async_result_get_source_object;
86 iface->is_tagged = dex_async_result_is_tagged;
87 }
88
89 G_DEFINE_FINAL_TYPE_WITH_CODE (DexAsyncResult, dex_async_result, G_TYPE_OBJECT,
90 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_RESULT, async_result_iface_init))
91
92 /**
93 * dex_async_result_new:
94 *
95 * Creates a new [[email protected]].
96 *
97 * Use this to integrate with `GAsyncResult` by calling
98 * [[email protected]] to complete when the
99 * dependent future resolves.
100 *
101 * Returns: (transfer full):
102 */
103 DexAsyncResult *
104 dex_async_result_new (gpointer source_object,
105 GCancellable *cancellable,
106 GAsyncReadyCallback callback,
107 gpointer user_data)
108 {
109 DexAsyncResult *self;
110
111 g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
112
113 self = g_object_new (DEX_TYPE_ASYNC_RESULT, NULL);
114 self->user_data = user_data;
115 self->callback = callback;
116 g_set_object (&self->source_object, source_object);
117 g_set_object (&self->cancellable, cancellable);
118 self->main_context = g_main_context_ref_thread_default ();
119
120 return self;
121 }
122
123 static void
124 dex_async_result_finalize (GObject *object)
125 {
126 DexAsyncResult *self = (DexAsyncResult *)object;
127
128 dex_clear (&self->future);
129 g_clear_object (&self->source_object);
130 g_clear_object (&self->cancellable);
131 g_clear_pointer (&self->main_context, g_main_context_unref);
132
133 if (!self->name_is_static)
134 g_clear_pointer (&self->name, g_free);
135
136 g_mutex_clear (&self->mutex);
137
138 G_OBJECT_CLASS (dex_async_result_parent_class)->finalize (object);
139 }
140
141 static void
142 dex_async_result_class_init (DexAsyncResultClass *klass)
143 {
144 GObjectClass *object_class = G_OBJECT_CLASS (klass);
145
146 object_class->finalize = dex_async_result_finalize;
147 }
148
149 static void
150 dex_async_result_init (DexAsyncResult *self)
151 {
152 g_mutex_init (&self->mutex);
153 }
154
155 const char *
156 dex_async_result_get_name (DexAsyncResult *self)
157 {
158 g_return_val_if_fail (DEX_IS_ASYNC_RESULT (self), NULL);
159
160 return self->name;
161 }
162
163 void
164 dex_async_result_set_name (DexAsyncResult *async_result,
165 const char *name)
166 {
167 g_return_if_fail (DEX_IS_ASYNC_RESULT (async_result));
168
169 g_mutex_lock (&async_result->mutex);
170 if (async_result->name == NULL)
171 async_result->name = g_strdup (name);
172 g_mutex_unlock (&async_result->mutex);
173 }
174
175 void
176 dex_async_result_set_static_name (DexAsyncResult *async_result,
177 const char *name)
178 {
179 DexAsyncResult *self = DEX_ASYNC_RESULT (async_result);
180
181 g_return_if_fail (DEX_IS_ASYNC_RESULT (self));
182
183 g_mutex_lock (&self->mutex);
184 if (self->name == NULL)
185 {
186 self->name = (gpointer)name;
187 self->name_is_static = TRUE;
188 }
189 g_mutex_unlock (&self->mutex);
190 }
191
192 /**
193 * dex_async_result_dup_future:
194 * @async_result: a [[email protected]]
195 *
196 * Gets the future for the [[email protected]], or %NULL if a future
197 * is not available.
198 *
199 * Returns: (transfer full) (nullable): a [[email protected]] or %NULL
200 */
201 DexFuture *
202 dex_async_result_dup_future (DexAsyncResult *async_result)
203 {
204 DexFuture *future = NULL;
205
206 g_return_val_if_fail (DEX_IS_ASYNC_RESULT (async_result), NULL);
207
208 g_mutex_lock (&async_result->mutex);
209 if (async_result->future != NULL)
210 future = dex_ref (async_result->future);
211 g_mutex_unlock (&async_result->mutex);
212
213 return g_steal_pointer (&future);
214 }
215
216 static const GValue *
217 dex_async_result_propagate (DexAsyncResult *async_result,
218 GError **error)
219 {
220 const GValue *value = NULL;
221 DexFuture *future;
222
223 g_assert (DEX_IS_ASYNC_RESULT (async_result));
224
225 if ((future = dex_async_result_dup_future (async_result)))
226 {
227 value = dex_future_get_value (future, error);
228 dex_unref (future);
229 return value;
230 }
231
232 g_set_error (error,
233 DEX_ERROR,
234 DEX_ERROR_PENDING,
235 "Future pending");
236
237 return NULL;
238 }
239
240 gpointer
241 dex_async_result_propagate_pointer (DexAsyncResult *async_result,
242 GError **error)
243 {
244 const GValue *value;
245
246 g_return_val_if_fail (DEX_IS_ASYNC_RESULT (async_result), NULL);
247
248 if ((value = dex_async_result_propagate (async_result, error)))
249 {
250 if (G_VALUE_HOLDS_OBJECT (value))
251 return g_value_dup_object (value);
252 else if (G_VALUE_HOLDS_BOXED (value))
253 return g_value_dup_boxed (value);
254 else if (G_VALUE_HOLDS_VARIANT (value))
255 return g_value_dup_variant (value);
256 else if (G_VALUE_HOLDS_POINTER (value))
257 return g_value_get_pointer (value);
258 else
259 g_critical ("Cannot propagate pointer of type %s",
260 G_VALUE_TYPE_NAME (value));
261 }
262
263 return NULL;
264 }
265
266 gssize
267 dex_async_result_propagate_int (DexAsyncResult *async_result,
268 GError **error)
269 {
270 const GValue *value;
271
272 g_return_val_if_fail (DEX_IS_ASYNC_RESULT (async_result), 0);
273
274 if ((value = dex_async_result_propagate (async_result, error)))
275 {
276 if (G_VALUE_HOLDS_INT (value))
277 return g_value_get_int (value);
278 else if (G_VALUE_HOLDS_UINT (value))
279 return g_value_get_uint (value);
280 else if (G_VALUE_HOLDS_INT64 (value))
281 return g_value_get_int64 (value);
282 else if (G_VALUE_HOLDS_UINT64 (value))
283 return g_value_get_uint64 (value);
284 else if (G_VALUE_HOLDS_LONG (value))
285 return g_value_get_long (value);
286 else if (G_VALUE_HOLDS_ULONG (value))
287 return g_value_get_ulong (value);
288 else
289 g_critical ("Cannot propagate int from type %s",
290 G_VALUE_TYPE_NAME (value));
291 }
292
293 return 0;
294 }
295
296 double
297 dex_async_result_propagate_double (DexAsyncResult *async_result,
298 GError **error)
299 {
300 const GValue *value;
301
302 g_return_val_if_fail (DEX_IS_ASYNC_RESULT (async_result), 0);
303
304 if ((value = dex_async_result_propagate (async_result, error)))
305 {
306 if (G_VALUE_HOLDS_DOUBLE (value))
307 return g_value_get_double (value);
308 else if (G_VALUE_HOLDS_FLOAT (value))
309 return g_value_get_float (value);
310 }
311
312 return .0;
313 }
314
315 gboolean
316 dex_async_result_propagate_boolean (DexAsyncResult *async_result,
317 GError **error)
318 {
319 const GValue *value;
320
321 g_return_val_if_fail (DEX_IS_ASYNC_RESULT (async_result), FALSE);
322
323 if ((value = dex_async_result_propagate (async_result, error)))
324 {
325 if (G_VALUE_HOLDS_BOOLEAN (value))
326 return g_value_get_boolean (value);
327
328 g_critical ("%s() got future of type %s, expected gboolean",
329 G_STRFUNC, G_VALUE_TYPE_NAME (value));
330
331 return FALSE;
332 }
333
334 return FALSE;
335 }
336
337 static gboolean
338 dex_async_result_complete_in_idle_cb (gpointer user_data)
339 {
340 DexAsyncResult *async_result = user_data;
341
342 g_assert (DEX_IS_ASYNC_RESULT (async_result));
343
344 async_result->callback (async_result->source_object,
345 G_ASYNC_RESULT (async_result),
346 g_steal_pointer (&async_result->user_data));
347
348 dex_clear (&async_result->future);
349
350 return G_SOURCE_REMOVE;
351 }
352
353 static DexFuture *
354 dex_async_result_await_cb (DexFuture *future,
355 gpointer user_data)
356 {
357 DexAsyncResult *async_result = user_data;
358
359 g_assert (DEX_IS_FUTURE (future));
360 g_assert (DEX_IS_ASYNC_RESULT (async_result));
361 g_assert (async_result->await_once);
362 g_assert (async_result->main_context != NULL);
363
364 g_mutex_lock (&async_result->mutex);
365
366 g_assert (!async_result->await_returned);
367
368 if (async_result->callback != NULL)
369 {
370 GSource *idle_source = g_idle_source_new ();
371
372 g_source_set_priority (idle_source, async_result->priority);
373 g_source_set_callback (idle_source,
374 dex_async_result_complete_in_idle_cb,
375 g_object_ref (async_result),
376 g_object_unref);
377
378 if (async_result->name_is_static)
379 _g_source_set_static_name (idle_source, async_result->name);
380 else
381 g_source_set_name (idle_source, async_result->name);
382
383 g_source_attach (idle_source, async_result->main_context);
384 g_source_unref (idle_source);
385 }
386
387 async_result->await_returned = TRUE;
388
389 g_mutex_unlock (&async_result->mutex);
390
391 return NULL;
392 }
393
394 /**
395 * dex_async_result_await:
396 * @async_result: a [[email protected]]
397 * @future: (transfer full): a [[email protected]]
398 *
399 * Tracks the result of @future and uses the value to complete @async_result,
400 * eventually calling the registered [[email protected]].
401 */
402 void
403 dex_async_result_await (DexAsyncResult *async_result,
404 DexFuture *future)
405 {
406 DexFuture *new_future = NULL;
407 DexFuture *cancellable;
408
409 g_return_if_fail (DEX_IS_ASYNC_RESULT (async_result));
410 g_return_if_fail (DEX_IS_FUTURE (future));
411
412 g_mutex_lock (&async_result->mutex);
413 if G_UNLIKELY (async_result->await_once != FALSE)
414 {
415 g_mutex_unlock (&async_result->mutex);
416 g_critical ("%s() called more than once on %s @ %p [%s]",
417 G_STRFUNC, G_OBJECT_TYPE_NAME (async_result),
418 async_result,
419 async_result->name ? async_result->name : "unnamed task");
420 return;
421 }
422 async_result->await_once = TRUE;
423 g_mutex_unlock (&async_result->mutex);
424
425 if (async_result->cancellable == NULL)
426 cancellable = NULL;
427 else
428 cancellable = dex_cancellable_new_from_cancellable (async_result->cancellable);
429
430 /* We have to be careful about ownership here and ensure we
431 * drop our references when the callback is executed.
432 */
433 g_object_ref (async_result);
434 new_future = dex_future_finally (dex_future_first (future, cancellable, NULL),
435 dex_async_result_await_cb,
436 g_object_ref (async_result),
437 g_object_unref);
438 g_mutex_lock (&async_result->mutex);
439 g_assert (async_result->future == NULL);
440 async_result->future = g_steal_pointer (&new_future);
441 g_mutex_unlock (&async_result->mutex);
442 g_object_unref (async_result);
443 }
444
445 void
446 dex_async_result_set_priority (DexAsyncResult *async_result,
447 int priority)
448 {
449 g_return_if_fail (DEX_IS_ASYNC_RESULT (async_result));
450
451 g_mutex_lock (&async_result->mutex);
452 async_result->priority = priority;
453 g_mutex_unlock (&async_result->mutex);
454 }