Debian Codegraph @

src/dex-delayed.c

raw symbols SCIP
1 /*
2 * dex-delayed.c
3 *
4 * Copyright 2022 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-delayed.h"
25 #include "dex-future-private.h"
26
27 /**
28 * DexDelayed:
29 *
30 * `DexDelayed` is a future which will resolve or reject the value of another
31 * [[email protected]] when [[email protected]] is called.
32 *
33 * This allows you to gate the resolution of a future which has already
34 * resolved or rejected until a later moment.
35 */
36
37 struct _DexDelayed
38 {
39 DexFuture parent_instance;
40 DexFuture *future;
41 guint corked : 1;
42 };
43
44 typedef struct _DexDelayedClass
45 {
46 DexFutureClass parent_class;
47 } DexDelayedClass;
48
49 DEX_DEFINE_FINAL_TYPE (DexDelayed, dex_delayed, DEX_TYPE_FUTURE)
50
51 static gboolean
52 dex_delayed_propagate (DexFuture *future,
53 DexFuture *completed)
54 {
55 DexDelayed *delayed = DEX_DELAYED (future);
56 gboolean ret = FALSE;
57
58 g_assert (DEX_IS_DELAYED (delayed));
59 g_assert (DEX_IS_FUTURE (completed));
60
61 dex_object_lock (delayed);
62 if (delayed->corked)
63 ret = TRUE;
64 else
65 dex_clear (&delayed->future);
66 dex_object_unlock (delayed);
67
68 return ret;
69 }
70
71 static void
72 dex_delayed_discard (DexFuture *future)
73 {
74 DexDelayed *delayed = DEX_DELAYED (future);
75 DexFuture *awaiting;
76
77 g_assert (DEX_IS_DELAYED (delayed));
78
79 dex_object_lock (delayed);
80 awaiting = g_steal_pointer (&delayed->future);
81 dex_object_unlock (delayed);
82
83 if (awaiting != NULL)
84 {
85 dex_future_discard (awaiting, future);
86 dex_clear (&awaiting);
87 }
88 }
89
90 static void
91 dex_delayed_finalize (DexObject *object)
92 {
93 DexDelayed *delayed = DEX_DELAYED (object);
94
95 dex_clear (&delayed->future);
96
97 DEX_OBJECT_CLASS (dex_delayed_parent_class)->finalize (object);
98 }
99
100 static void
101 dex_delayed_class_init (DexDelayedClass *delayed_class)
102 {
103 DexObjectClass *object_class = DEX_OBJECT_CLASS (delayed_class);
104 DexFutureClass *future_class = DEX_FUTURE_CLASS (delayed_class);
105
106 object_class->finalize = dex_delayed_finalize;
107
108 future_class->propagate = dex_delayed_propagate;
109 future_class->discard = dex_delayed_discard;
110 }
111
112 static void
113 dex_delayed_init (DexDelayed *delayed)
114 {
115 }
116
117 /**
118 * dex_delayed_new:
119 * @future: (transfer none): a [[email protected]]
120 *
121 * Creates a new [[email protected]]
122 *
123 * Returns: (transfer full):
124 */
125 DexFuture *
126 dex_delayed_new (DexFuture *future)
127 {
128 DexDelayed *delayed;
129
130 g_return_val_if_fail (DEX_IS_FUTURE (future), NULL);
131
132 delayed = (DexDelayed *)dex_object_create_instance (DEX_TYPE_DELAYED);
133 delayed->corked = TRUE;
134 delayed->future = dex_ref (future);
135
136 dex_future_chain (future, DEX_FUTURE (delayed));
137
138 return DEX_FUTURE (delayed);
139 }
140
141 /**
142 * dex_delayed_release:
143 * @delayed:
144 *
145 * Completes @delayed using the value provided at construction.
146 */
147 void
148 dex_delayed_release (DexDelayed *delayed)
149 {
150 DexFuture *complete = NULL;
151
152 g_return_if_fail (DEX_IS_DELAYED (delayed));
153
154 dex_object_lock (delayed);
155
156 if (delayed->corked)
157 {
158 delayed->corked = FALSE;
159
160 if (delayed->future != NULL &&
161 dex_future_get_status (delayed->future) != DEX_FUTURE_STATUS_PENDING)
162 complete = g_steal_pointer (&delayed->future);
163 }
164
165 dex_object_unlock (delayed);
166
167 if (complete != NULL)
168 {
169 dex_future_complete_from (DEX_FUTURE (delayed), complete);
170 dex_clear (&complete);
171 }
172 }
173
174 /**
175 * dex_delayed_dup_future:
176 * @delayed: a [[email protected]]
177 *
178 * Retrieves the delayed future provided to [[email protected]].
179 *
180 * This function can only return a [[email protected]] before
181 * [[email protected]] is called. After that, the delayed future is
182 * released and this function will return %NULL.
183 *
184 * Returns: (transfer full) (nullable): a [[email protected]] or %NULL
185 */
186 DexFuture *
187 dex_delayed_dup_future (DexDelayed *delayed)
188 {
189 DexFuture *ret;
190
191 g_return_val_if_fail (DEX_IS_DELAYED (delayed), NULL);
192
193 dex_object_lock (delayed);
194 ret = delayed->future ? dex_ref (delayed->future) : NULL;
195 dex_object_unlock (delayed);
196
197 return ret;
198 }