Debian Codegraph @

src/libsystemd/sd-future/sd-future.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-future.h"
4
5 #include "alloc-util.h"
6 #include "errno-util.h"
7 #include "log.h"
8 #include "macro.h"
9 #include "set.h"
10
11 struct sd_future {
12 unsigned n_ref;
13
14 int state;
15 int result;
16
17 Set *waiters;
18
19 sd_future_func_t callback;
20 void *userdata;
21
22 const sd_future_ops *ops;
23
24 /* Opaque per-future state owned by the future implementation (the code that called
25 * sd_future_new()). The ops vtable above receives this pointer in its callbacks, and
26 * external code can fetch it via sd_future_get_private(). */
27 void *private;
28 };
29
30 static int fiber_resume_trampoline(sd_future *f) {
31 /* The future's result is what the fiber should resume with. Impls choose the value at
32 * resolution time — e.g. a deadline timer resolves with -ETIME, a wait future resolves
33 * with the target's result, a normal IO/sleep future resolves with 0 on success. */
34 return sd_fiber_resume(sd_future_get_userdata(f), sd_future_result(f));
35 }
36
37 int sd_future_resolve(sd_future *f, int result) {
38 int r = 0;
39
40 assert_return(f, -EINVAL);
41
42 if (f->state != SD_FUTURE_PENDING)
43 return 0;
44
45 /* Hold a self-ref across callback/waiter dispatch: callbacks (e.g. bus_fiber_resolved()
46 * dropping the tracking-set's ref) may legitimately release what would otherwise be the
47 * last reference, and we still access f->waiters below. The cleanup unrefs at scope exit,
48 * which is when freeing is safe again. */
49 _unused_ _cleanup_(sd_future_unrefp) sd_future *self = sd_future_ref(f);
50
51 f->state = SD_FUTURE_RESOLVED;
52 f->result = result;
53
54 if (f->callback)
55 RET_GATHER(r, f->callback(f));
56
57 /* We'd like the set to not be modified while iterating over it, hence take ownership over it in
58 * a local variable. Otherwise code invoked via sd_future_resolve() could try to modify the set while
59 * we're iterating over it (for example wait_future_free()). */
60 Set *waiters = TAKE_PTR(f->waiters);
61 sd_future *w;
62 SET_FOREACH(w, waiters)
63 RET_GATHER(r, sd_future_resolve(w, result));
64
65 set_free(waiters);
66
67 return r;
68 }
69
70 static sd_future* sd_future_free(sd_future *f) {
71 if (!f)
72 return NULL;
73
74 if (f->state == SD_FUTURE_PENDING)
75 sd_future_resolve(f, -ECANCELED);
76
77 set_free(f->waiters);
78
79 if (f->ops->free)
80 f->ops->free(f);
81
82 return mfree(f);
83 }
84
85 DEFINE_TRIVIAL_REF_UNREF_FUNC(sd_future, sd_future, sd_future_free);
86 DEFINE_POINTER_ARRAY_CLEAR_FUNC(sd_future*, sd_future_unref);
87 DEFINE_POINTER_ARRAY_FREE_FUNC(sd_future*, sd_future_unref);
88
89 sd_future* sd_future_cancel_wait_unref(sd_future *f) {
90 int r;
91
92 if (!f)
93 return NULL;
94
95 /* We have to be able to suspend until the fiber we're waiting for finishes, and that's only
96 * possible if we're running on a fiber ourselves. */
97 if (!sd_fiber_is_running())
98 return sd_future_unref(f);
99
100 r = sd_future_cancel(f);
101 if (r < 0)
102 log_debug_errno(r, "Failed to cancel future, ignoring: %m");
103
104 if (f->state == SD_FUTURE_PENDING) {
105 /* Fast path: when f's resolve callback already targets the current fiber (the default for
106 * futures created on this fiber), we can suspend directly and let the existing trampoline
107 * wake us up — no need to allocate a wait future just to learn about the resolution.
108 * Otherwise fall back to sd_fiber_await() which sets up an explicit waiter. */
109 if (f->callback == fiber_resume_trampoline && f->userdata == sd_fiber_get_current())
110 r = sd_fiber_suspend();
111 else
112 r = sd_fiber_await(f);
113 if (r < 0 && r != -ECANCELED)
114 log_debug_errno(r, "Failed to wait for future to finish, ignoring: %m");
115 }
116
117 return sd_future_unref(f);
118 }
119
120 DEFINE_POINTER_ARRAY_CLEAR_FUNC(sd_future*, sd_future_cancel_wait_unref);
121 DEFINE_POINTER_ARRAY_FREE_FUNC(sd_future*, sd_future_cancel_wait_unref);
122
123 int sd_future_new(const sd_future_ops *ops, sd_future **ret) {
124 assert_return(ops, -EINVAL);
125 assert_return(ops->size >= endoffsetof_field(sd_future_ops, set_priority), -EINVAL);
126 assert_return(ops->alloc, -EINVAL);
127 assert_return(ops->free, -EINVAL);
128 assert_return(ret, -EINVAL);
129
130 sd_future *f = new(sd_future, 1);
131 if (!f)
132 return -ENOMEM;
133
134 *f = (sd_future) {
135 .n_ref = 1,
136 .state = SD_FUTURE_PENDING,
137 .ops = ops,
138 };
139
140 f->private = ops->alloc();
141 if (!f->private) {
142 free(f);
143 return -ENOMEM;
144 }
145
146 /* If we're being created on a fiber, default the callback to resuming that fiber on resolve —
147 * this is almost always what you want, and it saves the usual set_callback boilerplate before
148 * sd_fiber_suspend(). Callers that want different behavior can override with
149 * sd_future_set_callback(). */
150 sd_future *fiber = sd_fiber_get_current();
151 if (fiber)
152 (void) sd_future_set_callback(f, fiber_resume_trampoline, fiber);
153
154 *ret = f;
155 return 0;
156 }
157
158 int sd_future_state(sd_future *f) {
159 assert_return(f, -EINVAL);
160 return f->state;
161 }
162
163 int sd_future_result(sd_future *f) {
164 assert_return(f, -EINVAL);
165 assert_return(f->state == SD_FUTURE_RESOLVED, -EBUSY);
166 return f->result;
167 }
168
169 void* sd_future_get_userdata(sd_future *f) {
170 assert_return(f, NULL);
171 return f->userdata;
172 }
173
174 void* sd_future_get_private(sd_future *f) {
175 assert_return(f, NULL);
176 return f->private;
177 }
178
179 const sd_future_ops* sd_future_get_ops(sd_future *f) {
180 assert_return(f, NULL);
181 return f->ops;
182 }
183
184 int sd_future_set_callback(sd_future *f, sd_future_func_t callback, void *userdata) {
185 assert_return(f, -EINVAL);
186
187 f->callback = callback;
188 f->userdata = userdata;
189 return 0;
190 }
191
192 int sd_future_set_priority(sd_future *f, int64_t priority) {
193 assert_return(f, -EINVAL);
194 assert_return(f->state == SD_FUTURE_PENDING, -ESTALE);
195 assert_return(f->ops->set_priority, -EOPNOTSUPP);
196
197 return f->ops->set_priority(f, priority);
198 }
199
200 int sd_future_cancel(sd_future *f) {
201 assert_return(f, -EINVAL);
202 assert_return(f->ops->cancel, -EOPNOTSUPP);
203
204 if (f->state == SD_FUTURE_RESOLVED)
205 return 0;
206
207 return f->ops->cancel(f);
208 }
209
210 typedef struct WaitFuture {
211 sd_future *target;
212 } WaitFuture;
213
214 static void* wait_future_alloc(void) {
215 return new0(WaitFuture, 1);
216 }
217
218 static void wait_future_free(sd_future *f) {
219 WaitFuture *wf = ASSERT_PTR(sd_future_get_private(ASSERT_PTR(f)));
220
221 set_remove(wf->target->waiters, f);
222 sd_future_unref(wf->target);
223 free(wf);
224 }
225
226 static int wait_future_cancel(sd_future *f) {
227 WaitFuture *wf = ASSERT_PTR(sd_future_get_private(ASSERT_PTR(f)));
228
229 set_remove(wf->target->waiters, f);
230 return sd_future_resolve(f, -ECANCELED);
231 }
232
233 static const sd_future_ops wait_future_ops = {
234 .size = sizeof(sd_future_ops),
235 .alloc = wait_future_alloc,
236 .free = wait_future_free,
237 .cancel = wait_future_cancel,
238 };
239
240 int sd_future_new_wait(sd_future *target, sd_future **ret) {
241 int r;
242
243 assert_return(target, -EINVAL);
244 assert_return(ret, -EINVAL);
245
246 _cleanup_(sd_future_unrefp) sd_future *f = NULL;
247 r = sd_future_new(&wait_future_ops, &f);
248 if (r < 0)
249 return r;
250
251 WaitFuture *wf = sd_future_get_private(f);
252 wf->target = sd_future_ref(target);
253
254 if (target->state == SD_FUTURE_RESOLVED)
255 r = sd_future_resolve(f, target->result);
256 else
257 r = set_ensure_put(&target->waiters, &trivial_hash_ops, f);
258 if (r < 0)
259 return r;
260
261 *ret = TAKE_PTR(f);
262 return 0;
263 }