Debian Codegraph @

src/dex-waiter.c

raw symbols SCIP
1 /*
2 * dex-waiter.c
3 *
4 * Copyright 2025 Christian Hergert <[email protected]>
5 *
6 * This library is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of the
9 * License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful, but
12 * 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 General Public License along
17 * with this program. 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-waiter-private.h"
25
26 typedef struct _DexWaiter
27 {
28 DexFuture parent_instance;
29 GMutex mutex;
30 GCond cond;
31 } DexWaiter;
32
33 typedef struct _DexWaiterClass
34 {
35 DexFutureClass parent_class;
36 } DexWaiterClass;
37
38 DEX_DEFINE_FINAL_TYPE (DexWaiter, dex_waiter, DEX_TYPE_FUTURE)
39
40 #undef DEX_TYPE_WAITER
41 #define DEX_TYPE_WAITER dex_waiter_type
42
43 static void
44 dex_waiter_discard (DexFuture *future)
45 {
46 }
47
48 static gboolean
49 dex_waiter_propagate (DexFuture *future,
50 DexFuture *completed)
51 {
52 DexWaiter *waiter = (DexWaiter *)future;
53 GError *error = NULL;
54 const GValue *value;
55
56 g_assert (DEX_IS_WAITER (waiter));
57
58 g_mutex_lock (&waiter->mutex);
59 value = dex_future_get_value (completed, &error);
60 dex_future_complete (future, value, error);
61 g_cond_signal (&waiter->cond);
62 g_mutex_unlock (&waiter->mutex);
63
64 return TRUE;
65 }
66
67 static void
68 dex_waiter_finalize (DexObject *object)
69 {
70 DexWaiter *waiter = (DexWaiter *)object;
71
72 g_cond_clear (&waiter->cond);
73 g_mutex_clear (&waiter->mutex);
74
75 DEX_OBJECT_CLASS (dex_waiter_parent_class)->finalize (object);
76 }
77
78 static void
79 dex_waiter_class_init (DexWaiterClass *waiter_class)
80 {
81 DexObjectClass *object_class = DEX_OBJECT_CLASS (waiter_class);
82 DexFutureClass *future_class = DEX_FUTURE_CLASS (waiter_class);
83
84 object_class->finalize = dex_waiter_finalize;
85
86 future_class->propagate = dex_waiter_propagate;
87 future_class->discard = dex_waiter_discard;
88 }
89
90 static void
91 dex_waiter_init (DexWaiter *waiter)
92 {
93 }
94
95 DexWaiter *
96 dex_waiter_new (DexFuture *future)
97 {
98 DexWaiter *waiter;
99
100 g_return_val_if_fail (DEX_IS_FUTURE (future), NULL);
101
102 waiter = (DexWaiter *)dex_object_create_instance (dex_waiter_type);
103 g_mutex_init (&waiter->mutex);
104 g_cond_init (&waiter->cond);
105
106 dex_future_chain (future, DEX_FUTURE (waiter));
107
108 return waiter;
109 }
110
111 void
112 dex_waiter_wait (DexWaiter *self)
113 {
114 g_return_if_fail (DEX_IS_WAITER (self));
115
116 g_mutex_lock (&self->mutex);
117 if (dex_future_is_pending (DEX_FUTURE (self)))
118 g_cond_wait (&self->cond, &self->mutex);
119 g_mutex_unlock (&self->mutex);
120 }