/src/gstreamer/subprojects/gstreamer/gst/gstpromise.c
Line | Count | Source |
1 | | /* GStreamer |
2 | | * Copyright (C) 2017 Matthew Waters <matthew@centricular.com> |
3 | | * |
4 | | * This library is free software; you can redistribute it and/or |
5 | | * modify it under the terms of the GNU Library General Public |
6 | | * License as published by the Free Software Foundation; either |
7 | | * version 2 of the License, or (at your option) any later version. |
8 | | * |
9 | | * This library is distributed in the hope that it will be useful, |
10 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | | * Library General Public License for more details. |
13 | | * |
14 | | * You should have received a copy of the GNU Library General Public |
15 | | * License along with this library; if not, write to the |
16 | | * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, |
17 | | * Boston, MA 02110-1301, USA. |
18 | | */ |
19 | | |
20 | | #ifdef HAVE_CONFIG_H |
21 | | # include "config.h" |
22 | | #endif |
23 | | |
24 | | #define GST_DISABLE_MINIOBJECT_INLINE_FUNCTIONS |
25 | | #include "gst_private.h" |
26 | | |
27 | | #include "gstpromise.h" |
28 | | |
29 | | #define GST_CAT_DEFAULT gst_promise_debug |
30 | | GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT); |
31 | | |
32 | | /** |
33 | | * SECTION:gstpromise |
34 | | * @title: GstPromise |
35 | | * @short_description: a miniobject for future/promise-like functionality |
36 | | * |
37 | | * The #GstPromise object implements the container for values that may |
38 | | * be available later. i.e. a Future or a Promise in |
39 | | * <https://en.wikipedia.org/wiki/Futures_and_promises>. |
40 | | * As with all Future/Promise-like functionality, there is the concept of the |
41 | | * producer of the value and the consumer of the value. |
42 | | * |
43 | | * A #GstPromise is created with gst_promise_new() by the consumer and passed |
44 | | * to the producer to avoid thread safety issues with the change callback. |
45 | | * A #GstPromise can be replied to with a value (or an error) by the producer |
46 | | * with gst_promise_reply(). The exact value returned is defined by the API |
47 | | * contract of the producer and %NULL may be a valid reply. |
48 | | * gst_promise_interrupt() is for the consumer to |
49 | | * indicate to the producer that the value is not needed anymore and producing |
50 | | * that value can stop. The @GST_PROMISE_RESULT_EXPIRED state set by a call |
51 | | * to gst_promise_expire() indicates to the consumer that a value will never |
52 | | * be produced and is intended to be called by a third party that implements |
53 | | * some notion of message handling such as #GstBus. |
54 | | * A callback can also be installed at #GstPromise creation for |
55 | | * result changes with gst_promise_new_with_change_func(). |
56 | | * The change callback can be used to chain #GstPromises's together as in the |
57 | | * following example. |
58 | | * |[<!-- language="C" --> |
59 | | * const GstStructure *reply; |
60 | | * GstPromise *p; |
61 | | * if (gst_promise_wait (promise) != GST_PROMISE_RESULT_REPLIED) |
62 | | * return; // interrupted or expired value |
63 | | * reply = gst_promise_get_reply (promise); |
64 | | * if (error in reply) |
65 | | * return; // propagate error |
66 | | * p = gst_promise_new_with_change_func (another_promise_change_func, user_data, notify); |
67 | | * pass p to promise-using API |
68 | | * ]| |
69 | | * |
70 | | * Each #GstPromise starts out with a #GstPromiseResult of |
71 | | * %GST_PROMISE_RESULT_PENDING and only ever transitions once |
72 | | * into one of the other #GstPromiseResult's. |
73 | | * |
74 | | * In order to support multi-threaded code, gst_promise_reply(), |
75 | | * gst_promise_interrupt() and gst_promise_expire() may all be from |
76 | | * different threads with some restrictions and the final result of the promise |
77 | | * is whichever call is made first. There are two restrictions on ordering: |
78 | | * |
79 | | * 1. That gst_promise_reply() and gst_promise_interrupt() cannot be called |
80 | | * after gst_promise_expire() |
81 | | * 2. That gst_promise_reply() and gst_promise_interrupt() |
82 | | * cannot be called twice. |
83 | | * |
84 | | * The change function set with gst_promise_new_with_change_func() is |
85 | | * called directly from either the gst_promise_reply(), |
86 | | * gst_promise_interrupt() or gst_promise_expire() and can be called |
87 | | * from an arbitrary thread. #GstPromise using APIs can restrict this to |
88 | | * a single thread or a subset of threads but that is entirely up to the API |
89 | | * that uses #GstPromise. |
90 | | */ |
91 | | |
92 | | static const int immutable_structure_refcount = 2; |
93 | | |
94 | 0 | #define GST_PROMISE_REPLY(p) (((GstPromiseImpl *)(p))->reply) |
95 | 0 | #define GST_PROMISE_RESULT(p) (((GstPromiseImpl *)(p))->result) |
96 | 0 | #define GST_PROMISE_LOCK(p) (&(((GstPromiseImpl *)(p))->lock)) |
97 | 0 | #define GST_PROMISE_COND(p) (&(((GstPromiseImpl *)(p))->cond)) |
98 | 0 | #define GST_PROMISE_CHANGE_FUNC(p) (((GstPromiseImpl *)(p))->change_func) |
99 | 0 | #define GST_PROMISE_CHANGE_DATA(p) (((GstPromiseImpl *)(p))->user_data) |
100 | 0 | #define GST_PROMISE_CHANGE_NOTIFY(p) (((GstPromiseImpl *)(p))->notify) |
101 | | |
102 | | typedef struct |
103 | | { |
104 | | GstPromise promise; |
105 | | |
106 | | GstPromiseResult result; |
107 | | GstStructure *reply; |
108 | | |
109 | | GMutex lock; |
110 | | GCond cond; |
111 | | GstPromiseChangeFunc change_func; |
112 | | gpointer user_data; |
113 | | GDestroyNotify notify; |
114 | | } GstPromiseImpl; |
115 | | |
116 | | /** |
117 | | * gst_promise_wait: |
118 | | * @promise: a #GstPromise |
119 | | * |
120 | | * Wait for @promise to move out of the %GST_PROMISE_RESULT_PENDING state. |
121 | | * If @promise is not in %GST_PROMISE_RESULT_PENDING then it will return |
122 | | * immediately with the current result. |
123 | | * |
124 | | * Returns: the result of the promise |
125 | | * |
126 | | * Since: 1.14 |
127 | | */ |
128 | | GstPromiseResult |
129 | | gst_promise_wait (GstPromise * promise) |
130 | 0 | { |
131 | 0 | GstPromiseResult ret; |
132 | |
|
133 | 0 | g_return_val_if_fail (promise != NULL, GST_PROMISE_RESULT_EXPIRED); |
134 | | |
135 | 0 | g_mutex_lock (GST_PROMISE_LOCK (promise)); |
136 | 0 | ret = GST_PROMISE_RESULT (promise); |
137 | |
|
138 | 0 | while (ret == GST_PROMISE_RESULT_PENDING) { |
139 | 0 | GST_LOG ("%p waiting", promise); |
140 | 0 | g_cond_wait (GST_PROMISE_COND (promise), GST_PROMISE_LOCK (promise)); |
141 | 0 | ret = GST_PROMISE_RESULT (promise); |
142 | 0 | } |
143 | 0 | GST_LOG ("%p waited", promise); |
144 | |
|
145 | 0 | g_mutex_unlock (GST_PROMISE_LOCK (promise)); |
146 | |
|
147 | 0 | return ret; |
148 | 0 | } |
149 | | |
150 | | /** |
151 | | * gst_promise_reply: |
152 | | * @promise: (allow-none): a #GstPromise |
153 | | * @s: (transfer full) (nullable): a #GstStructure with the the reply contents |
154 | | * |
155 | | * Set a reply on @promise. This will wake up any waiters with |
156 | | * %GST_PROMISE_RESULT_REPLIED. Called by the producer of the value to |
157 | | * indicate success (or failure). |
158 | | * |
159 | | * If @promise has already been interrupted by the consumer, then this reply |
160 | | * is not visible to the consumer. |
161 | | * |
162 | | * Since: 1.14 |
163 | | */ |
164 | | void |
165 | | gst_promise_reply (GstPromise * promise, GstStructure * s) |
166 | 0 | { |
167 | 0 | GstPromiseChangeFunc change_func = NULL; |
168 | 0 | gpointer change_data = NULL; |
169 | | |
170 | | /* Caller requested that no reply is necessary */ |
171 | 0 | if (promise == NULL) |
172 | 0 | return; |
173 | | |
174 | 0 | g_mutex_lock (GST_PROMISE_LOCK (promise)); |
175 | 0 | if (GST_PROMISE_RESULT (promise) != GST_PROMISE_RESULT_PENDING && |
176 | 0 | GST_PROMISE_RESULT (promise) != GST_PROMISE_RESULT_INTERRUPTED) { |
177 | 0 | g_warning ("Promise result isn't PENDING or INTERRUPTED"); |
178 | 0 | g_mutex_unlock (GST_PROMISE_LOCK (promise)); |
179 | 0 | return; |
180 | 0 | } |
181 | | |
182 | | /* XXX: is this necessary and valid? */ |
183 | 0 | if (GST_PROMISE_REPLY (promise) && GST_PROMISE_REPLY (promise) != s) |
184 | 0 | gst_structure_free (GST_PROMISE_REPLY (promise)); |
185 | | |
186 | | /* Only reply iff we are currently in pending */ |
187 | 0 | if (GST_PROMISE_RESULT (promise) == GST_PROMISE_RESULT_PENDING) { |
188 | 0 | if (s |
189 | 0 | && !gst_structure_set_parent_refcount (s, |
190 | 0 | (int *) &immutable_structure_refcount)) { |
191 | 0 | g_critical ("Input structure has a parent already!"); |
192 | 0 | g_mutex_unlock (GST_PROMISE_LOCK (promise)); |
193 | 0 | return; |
194 | 0 | } |
195 | | |
196 | 0 | GST_PROMISE_RESULT (promise) = GST_PROMISE_RESULT_REPLIED; |
197 | 0 | GST_LOG ("%p replied", promise); |
198 | |
|
199 | 0 | GST_PROMISE_REPLY (promise) = s; |
200 | |
|
201 | 0 | change_func = GST_PROMISE_CHANGE_FUNC (promise); |
202 | 0 | change_data = GST_PROMISE_CHANGE_DATA (promise); |
203 | 0 | } else { |
204 | | /* eat the value */ |
205 | 0 | if (s) |
206 | 0 | gst_structure_free (s); |
207 | 0 | } |
208 | | |
209 | 0 | g_cond_broadcast (GST_PROMISE_COND (promise)); |
210 | 0 | g_mutex_unlock (GST_PROMISE_LOCK (promise)); |
211 | |
|
212 | 0 | if (change_func) |
213 | 0 | change_func (promise, change_data); |
214 | 0 | } |
215 | | |
216 | | /** |
217 | | * gst_promise_get_reply: |
218 | | * @promise: a #GstPromise |
219 | | * |
220 | | * Retrieve the reply set on @promise. @promise must be in |
221 | | * %GST_PROMISE_RESULT_REPLIED and the returned structure is owned by @promise |
222 | | * |
223 | | * Returns: (transfer none) (nullable): The reply set on @promise |
224 | | * |
225 | | * Since: 1.14 |
226 | | */ |
227 | | const GstStructure * |
228 | | gst_promise_get_reply (GstPromise * promise) |
229 | 0 | { |
230 | 0 | g_return_val_if_fail (promise != NULL, NULL); |
231 | | |
232 | 0 | g_mutex_lock (GST_PROMISE_LOCK (promise)); |
233 | 0 | if (GST_PROMISE_RESULT (promise) != GST_PROMISE_RESULT_REPLIED) { |
234 | 0 | g_warning ("Promise result isn't REPLIED"); |
235 | 0 | g_mutex_unlock (GST_PROMISE_LOCK (promise)); |
236 | 0 | return NULL; |
237 | 0 | } |
238 | | |
239 | 0 | g_mutex_unlock (GST_PROMISE_LOCK (promise)); |
240 | |
|
241 | 0 | return GST_PROMISE_REPLY (promise); |
242 | 0 | } |
243 | | |
244 | | /** |
245 | | * gst_promise_interrupt: |
246 | | * @promise: a #GstPromise |
247 | | * |
248 | | * Interrupt waiting for a @promise. This will wake up any waiters with |
249 | | * %GST_PROMISE_RESULT_INTERRUPTED. Called when the consumer does not want |
250 | | * the value produced anymore. |
251 | | * |
252 | | * Since: 1.14 |
253 | | */ |
254 | | void |
255 | | gst_promise_interrupt (GstPromise * promise) |
256 | 0 | { |
257 | 0 | GstPromiseChangeFunc change_func = NULL; |
258 | 0 | gpointer change_data = NULL; |
259 | |
|
260 | 0 | g_return_if_fail (promise != NULL); |
261 | | |
262 | 0 | g_mutex_lock (GST_PROMISE_LOCK (promise)); |
263 | 0 | if (GST_PROMISE_RESULT (promise) != GST_PROMISE_RESULT_PENDING && |
264 | 0 | GST_PROMISE_RESULT (promise) != GST_PROMISE_RESULT_REPLIED) { |
265 | 0 | g_warning ("Promise result isn't PENDING or REPLIED"); |
266 | 0 | g_mutex_unlock (GST_PROMISE_LOCK (promise)); |
267 | 0 | return; |
268 | 0 | } |
269 | | /* only interrupt if we are currently in pending */ |
270 | 0 | if (GST_PROMISE_RESULT (promise) == GST_PROMISE_RESULT_PENDING) { |
271 | 0 | GST_PROMISE_RESULT (promise) = GST_PROMISE_RESULT_INTERRUPTED; |
272 | 0 | g_cond_broadcast (GST_PROMISE_COND (promise)); |
273 | 0 | GST_LOG ("%p interrupted", promise); |
274 | |
|
275 | 0 | change_func = GST_PROMISE_CHANGE_FUNC (promise); |
276 | 0 | change_data = GST_PROMISE_CHANGE_DATA (promise); |
277 | 0 | } |
278 | 0 | g_mutex_unlock (GST_PROMISE_LOCK (promise)); |
279 | |
|
280 | 0 | if (change_func) |
281 | 0 | change_func (promise, change_data); |
282 | 0 | } |
283 | | |
284 | | /** |
285 | | * gst_promise_expire: |
286 | | * @promise: a #GstPromise |
287 | | * |
288 | | * Expire a @promise. This will wake up any waiters with |
289 | | * %GST_PROMISE_RESULT_EXPIRED. Called by a message loop when the parent |
290 | | * message is handled and/or destroyed (possibly unanswered). |
291 | | * |
292 | | * Since: 1.14 |
293 | | */ |
294 | | void |
295 | | gst_promise_expire (GstPromise * promise) |
296 | 0 | { |
297 | 0 | GstPromiseChangeFunc change_func = NULL; |
298 | 0 | gpointer change_data = NULL; |
299 | |
|
300 | 0 | g_return_if_fail (promise != NULL); |
301 | | |
302 | 0 | g_mutex_lock (GST_PROMISE_LOCK (promise)); |
303 | 0 | if (GST_PROMISE_RESULT (promise) == GST_PROMISE_RESULT_PENDING) { |
304 | 0 | GST_PROMISE_RESULT (promise) = GST_PROMISE_RESULT_EXPIRED; |
305 | 0 | g_cond_broadcast (GST_PROMISE_COND (promise)); |
306 | 0 | GST_LOG ("%p expired", promise); |
307 | |
|
308 | 0 | change_func = GST_PROMISE_CHANGE_FUNC (promise); |
309 | 0 | change_data = GST_PROMISE_CHANGE_DATA (promise); |
310 | 0 | GST_PROMISE_CHANGE_FUNC (promise) = NULL; |
311 | 0 | GST_PROMISE_CHANGE_DATA (promise) = NULL; |
312 | 0 | } |
313 | 0 | g_mutex_unlock (GST_PROMISE_LOCK (promise)); |
314 | |
|
315 | 0 | if (change_func) |
316 | 0 | change_func (promise, change_data); |
317 | 0 | } |
318 | | |
319 | | static void |
320 | | gst_promise_free (GstMiniObject * object) |
321 | 0 | { |
322 | 0 | GstPromise *promise = (GstPromise *) object; |
323 | | |
324 | | /* the promise *must* be dealt with in some way before destruction */ |
325 | 0 | g_warn_if_fail (GST_PROMISE_RESULT (promise) != GST_PROMISE_RESULT_PENDING); |
326 | |
|
327 | 0 | if (GST_PROMISE_CHANGE_NOTIFY (promise)) |
328 | 0 | GST_PROMISE_CHANGE_NOTIFY (promise) (GST_PROMISE_CHANGE_DATA (promise)); |
329 | |
|
330 | 0 | if (GST_PROMISE_REPLY (promise)) { |
331 | 0 | gst_structure_set_parent_refcount (GST_PROMISE_REPLY (promise), NULL); |
332 | 0 | gst_structure_free (GST_PROMISE_REPLY (promise)); |
333 | 0 | } |
334 | 0 | g_mutex_clear (GST_PROMISE_LOCK (promise)); |
335 | 0 | g_cond_clear (GST_PROMISE_COND (promise)); |
336 | 0 | GST_LOG ("%p finalized", promise); |
337 | |
|
338 | | #ifdef USE_POISONING |
339 | | memset (promise, 0xff, sizeof (GstPromiseImpl)); |
340 | | #endif |
341 | |
|
342 | 0 | g_free (promise); |
343 | 0 | } |
344 | | |
345 | | static void |
346 | | gst_promise_init (GstPromise * promise) |
347 | 0 | { |
348 | 0 | static gsize _init = 0; |
349 | |
|
350 | 0 | if (g_once_init_enter (&_init)) { |
351 | 0 | GST_DEBUG_CATEGORY_INIT (gst_promise_debug, "gstpromise", 0, "gstpromise"); |
352 | 0 | g_once_init_leave (&_init, 1); |
353 | 0 | } |
354 | |
|
355 | 0 | gst_mini_object_init (GST_MINI_OBJECT (promise), 0, GST_TYPE_PROMISE, NULL, |
356 | 0 | NULL, gst_promise_free); |
357 | |
|
358 | 0 | GST_PROMISE_REPLY (promise) = NULL; |
359 | 0 | GST_PROMISE_RESULT (promise) = GST_PROMISE_RESULT_PENDING; |
360 | 0 | g_mutex_init (GST_PROMISE_LOCK (promise)); |
361 | 0 | g_cond_init (GST_PROMISE_COND (promise)); |
362 | 0 | } |
363 | | |
364 | | /** |
365 | | * gst_promise_new: |
366 | | * |
367 | | * Returns: a new #GstPromise |
368 | | * |
369 | | * Since: 1.14 |
370 | | */ |
371 | | GstPromise * |
372 | | gst_promise_new (void) |
373 | 0 | { |
374 | 0 | GstPromise *promise = GST_PROMISE (g_new0 (GstPromiseImpl, 1)); |
375 | |
|
376 | 0 | gst_promise_init (promise); |
377 | 0 | GST_LOG ("new promise %p", promise); |
378 | |
|
379 | 0 | return promise; |
380 | 0 | } |
381 | | |
382 | | /** |
383 | | * gst_promise_new_with_change_func: |
384 | | * @func: (scope notified) (closure user_data): a #GstPromiseChangeFunc to call |
385 | | * @user_data: argument to call @func with |
386 | | * @notify: notification function that @user_data is no longer needed |
387 | | * |
388 | | * @func will be called exactly once when transitioning out of |
389 | | * %GST_PROMISE_RESULT_PENDING into any of the other #GstPromiseResult |
390 | | * states. |
391 | | * |
392 | | * Returns: a new #GstPromise |
393 | | * |
394 | | * Since: 1.14 |
395 | | */ |
396 | | GstPromise * |
397 | | gst_promise_new_with_change_func (GstPromiseChangeFunc func, gpointer user_data, |
398 | | GDestroyNotify notify) |
399 | 0 | { |
400 | 0 | GstPromise *promise = gst_promise_new (); |
401 | |
|
402 | 0 | GST_PROMISE_CHANGE_FUNC (promise) = func; |
403 | 0 | GST_PROMISE_CHANGE_DATA (promise) = user_data; |
404 | 0 | GST_PROMISE_CHANGE_NOTIFY (promise) = notify; |
405 | |
|
406 | 0 | return promise; |
407 | 0 | } |
408 | | |
409 | 44 | GST_DEFINE_MINI_OBJECT_TYPE (GstPromise, gst_promise); |
410 | 44 | |
411 | 44 | /** |
412 | 44 | * gst_promise_ref: |
413 | 44 | * @promise: a #GstPromise. |
414 | 44 | * |
415 | 44 | * Increases the refcount of the given @promise by one. |
416 | 44 | * |
417 | 44 | * Returns: (transfer full): @promise |
418 | 44 | * |
419 | 44 | * Since: 1.14 |
420 | 44 | */ |
421 | 44 | GstPromise * |
422 | 44 | gst_promise_ref (GstPromise * promise) |
423 | 44 | { |
424 | 0 | return (GstPromise *) gst_mini_object_ref (GST_MINI_OBJECT_CAST (promise)); |
425 | 0 | } |
426 | | |
427 | | /** |
428 | | * gst_promise_unref: |
429 | | * @promise: (transfer full): a #GstPromise. |
430 | | * |
431 | | * Decreases the refcount of the promise. If the refcount reaches 0, the |
432 | | * promise will be freed. |
433 | | * |
434 | | * Since: 1.14 |
435 | | */ |
436 | | void |
437 | | gst_promise_unref (GstPromise * promise) |
438 | 0 | { |
439 | 0 | gst_mini_object_unref (GST_MINI_OBJECT_CAST (promise)); |
440 | 0 | } |
441 | | |
442 | | /** |
443 | | * gst_clear_promise: (skip) |
444 | | * @promise_ptr: a pointer to a #GstPromise reference |
445 | | * |
446 | | * Clears a reference to a #GstPromise. |
447 | | * |
448 | | * @promise_ptr must not be `NULL`. |
449 | | * |
450 | | * If the reference is `NULL` then this function does nothing. Otherwise, the |
451 | | * reference count of the promise is decreased and the pointer is set to `NULL`. |
452 | | * |
453 | | * Since: 1.24 |
454 | | */ |
455 | | void |
456 | | gst_clear_promise (GstPromise ** promise_ptr) |
457 | 0 | { |
458 | | gst_clear_mini_object ((GstMiniObject **) promise_ptr); |
459 | 0 | } |