Debian Codegraph @

src/dex-thread.c

raw symbols SCIP
1 /* dex-thread.c
2 *
3 * Copyright 2025 Christian Hergert <[email protected]>
4 *
5 * This library is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 2.1 of the
8 * License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: LGPL-2.1-or-later
19 */
20
21 #include "config.h"
22
23 #include <gobject/gvaluecollector.h>
24
25 #include "dex-block-private.h"
26 #include "dex-future-private.h"
27 #include "dex-promise.h"
28 #include "dex-thread.h"
29 #include "dex-thread-storage-private.h"
30 #include "dex-waiter-private.h"
31
32 typedef struct _ThreadSpawn
33 {
34 DexScheduler *scheduler;
35 DexPromise *promise;
36 DexThreadFunc thread_func;
37 gpointer thread_func_data;
38 } ThreadSpawn;
39
40 static DexFuture *
41 propagate_future (DexFuture *completed,
42 gpointer user_data)
43 {
44 DexPromise *promise = user_data;
45 const GValue *value;
46 GError *error = NULL;
47
48 if ((value = dex_future_get_value (completed, &error)))
49 dex_promise_resolve (promise, value);
50 else
51 dex_promise_reject (promise, g_steal_pointer (&error));
52
53 return dex_future_new_true ();
54 }
55
56 static DexFuture *
57 do_nothing (DexFuture *completed,
58 gpointer user_data)
59 {
60 /* This function passes through the result but the main point
61 * is that the user_data_destroy is called from this thread.
62 */
63 return dex_ref (completed);
64 }
65
66 static gpointer
67 dex_trampoline_thread (gpointer data)
68 {
69 ThreadSpawn *state = data;
70 DexFuture *future;
71
72 g_assert (state != NULL);
73 g_assert (state->thread_func != NULL);
74 g_assert (DEX_IS_SCHEDULER (state->scheduler));
75 g_assert (DEX_IS_PROMISE (state->promise));
76
77 future = state->thread_func (state->thread_func_data);
78
79 dex_future_disown_full (dex_block_new (g_steal_pointer (&future),
80 state->scheduler,
81 DEX_BLOCK_KIND_FINALLY,
82 propagate_future,
83 dex_ref (state->promise),
84 dex_unref),
85 state->scheduler);
86
87 dex_clear (&state->scheduler);
88 dex_clear (&state->promise);
89
90 /* Thread func data is cleaned up in calling thread (or main thread)
91 * instead of here so that we don't risk data races on finalizers.
92 */
93 state->thread_func = NULL;
94 state->thread_func_data = NULL;
95
96 g_free (state);
97
98 return NULL;
99 }
100
101 /**
102 * dex_thread_spawn:
103 * @thread_name: (nullable): the name for the thread
104 * @thread_func: the function to call on a thread
105 * @user_data: closure data for @thread_func
106 * @user_data_destroy: callback to free @user_data which will be called
107 * on the same thread calling this function.
108 *
109 * Spawns a new thread named @thread_name running @thread_func with
110 * @user_data passed to it.
111 *
112 * @thread_func must return a [[email protected]].
113 *
114 * If this function is called from a thread that is not running a
115 * [[email protected]] then the default scheduler will be used
116 * to call @user_data_destroy.
117 *
118 * If the resulting [[email protected]] has not resolved or rejected,
119 * then the same scheduler used to call @user_data_destroy will be
120 * used to propagate the result to the caller.
121 *
122 * Returns: (transfer full): a [[email protected]] that resolves or rejects
123 * the value or error returned from @thread_func as a [[email protected]].
124 *
125 * Since: 1.0
126 */
127 DexFuture *
128 dex_thread_spawn (const char *thread_name,
129 DexThreadFunc thread_func,
130 gpointer user_data,
131 GDestroyNotify user_data_destroy)
132 {
133 DexThreadStorage *storage;
134 DexScheduler *scheduler;
135 ThreadSpawn *state;
136 DexFuture *ret;
137
138 dex_return_error_if_fail (thread_func != NULL);
139
140 if (thread_name == NULL)
141 thread_name = "[dex-thread]";
142
143 storage = dex_thread_storage_peek ();
144
145 if (storage == NULL)
146 scheduler = dex_scheduler_get_default ();
147 else
148 scheduler = storage->scheduler;
149
150 g_assert (DEX_IS_SCHEDULER (scheduler));
151
152 state = g_new0 (ThreadSpawn, 1);
153 state->scheduler = dex_ref (scheduler);
154 state->thread_func = thread_func;
155 state->thread_func_data = user_data;
156 state->promise = dex_promise_new ();
157
158 dex_future_set_static_name (DEX_FUTURE (state->promise),
159 g_intern_string (thread_name));
160
161 ret = dex_block_new (dex_ref (state->promise),
162 scheduler,
163 DEX_BLOCK_KIND_FINALLY,
164 do_nothing,
165 user_data,
166 user_data_destroy);
167
168 g_thread_unref (g_thread_new (thread_name, dex_trampoline_thread, state));
169
170 return DEX_FUTURE (ret);
171 }
172
173 /**
174 * dex_thread_wait_for:
175 * @future: (transfer full): a [[email protected]]
176 * @error: a location for a [[email protected]] or %NULL
177 *
178 * Use this when running on a thread spawned with `dex_thread_spawn()` and
179 * you need to block the thread until @future has resolved or rejected.
180 *
181 * Returns: %TRUE if @future resolved, otherwise %FALSE and @error is
182 * set to the rejection.
183 *
184 * Since: 1.0
185 */
186 gboolean
187 dex_thread_wait_for (DexFuture *future,
188 GError **error)
189 {
190 DexThreadStorage *storage;
191 gboolean ret;
192
193 g_return_val_if_fail (DEX_IS_FUTURE (future), FALSE);
194
195 if ((storage = dex_thread_storage_peek ()) && storage->scheduler != NULL)
196 {
197 g_set_error_literal (error,
198 G_IO_ERROR,
199 G_IO_ERROR_FAILED,
200 "Attempt to wait for future on scheduler controlled thread. This is not allowed.");
201 return FALSE;
202 }
203
204 /* Short-circuit when @future is already completed */
205 if (!dex_future_is_pending (future))
206 {
207 ret = !!dex_future_get_value (future, error);
208 dex_unref (future);
209 }
210 else
211 {
212 DexWaiter *waiter;
213
214 /* Keep future alive during waiter lifetime */
215 waiter = dex_waiter_new (future);
216 dex_waiter_wait (waiter);
217 ret = !!dex_future_get_value (DEX_FUTURE (waiter), error);
218 dex_unref (waiter);
219 dex_unref (future);
220 }
221
222 return ret;
223 }