Debian Codegraph @

src/dex-state-machine.c

raw symbols SCIP
1 /*
2 * dex-state-machine.c
3 *
4 * Copyright 2026 Christian Hergert
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 Lesser General Public License
17 * along with this library; 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 <gio/gio.h>
25
26 #include "dex-closure.h"
27 #include "dex-error.h"
28 #include "dex-future.h"
29 #include "dex-future-private.h"
30 #include "dex-limiter.h"
31 #include "dex-object-private.h"
32 #include "dex-promise.h"
33 #include "dex-state-machine.h"
34
35 /**
36 * DexStateMachine:
37 *
38 * `DexStateMachine` provides a serialized asynchronous state machine.
39 *
40 * Transitions are declared up front with a static table of
41 * [[email protected]] entries. Requests made with
42 * [[email protected]] are serialized through an internal
43 * [[email protected]] with a max concurrency of one. Transition callbacks run
44 * from a fiber, so they may use `dex_await()` and related APIs while still
45 * appearing as synchronous functions.
46 */
47
48 /**
49 * DexStateTransitionContext:
50 *
51 * `DexStateTransitionContext` is an opaque per-callback structure with
52 * information and state access for a [[email protected]] callback.
53 *
54 * It is only valid for the duration of the callback and must not be stored.
55 *
56 * [[email protected]_from] and
57 * [[email protected]_to] return the declared edge that
58 * caused the callback to run. [[email protected]_state]
59 * and [[email protected]_state] access the real state in
60 * the [[email protected]]. Use
61 * [[email protected]_to] to follow another declared
62 * edge before queued transition requests are processed.
63 *
64 * Since: 1.2
65 */
66
67 struct _DexStateMachine
68 {
69 DexObject parent_instance;
70 GType state_enum_type;
71 DexLimiter *limiter;
72 DexScheduler *scheduler;
73 DexStateTransition *transitions;
74 DexStateTransitionContext *active_context;
75 GQueue waiters;
76 guint n_transitions;
77 gsize stack_size;
78 guint state;
79 guint requested_state;
80 gpointer user_data;
81 GDestroyNotify user_data_destroy;
82 };
83
84 struct _DexStateTransitionContext
85 {
86 DexStateMachine *state_machine;
87 DexStateTransitionContext *previous_context;
88 DexPromise *interrupt;
89 guint from;
90 guint to;
91 guint state_set : 1;
92 guint interrupted : 1;
93 };
94
95 typedef struct _DexStateMachineClass
96 {
97 DexObjectClass parent_class;
98 } DexStateMachineClass;
99
100 typedef struct _DexStateMachineWaiter
101 {
102 DexFuture parent_instance;
103 GList link;
104 DexWeakRef state_machine_wr;
105 guint state;
106 guint queued : 1;
107 } DexStateMachineWaiter;
108
109 typedef struct _DexStateMachineWaiterClass
110 {
111 DexFutureClass parent_class;
112 } DexStateMachineWaiterClass;
113
114 GType dex_state_machine_waiter_get_type (void);
115
116 DEX_DEFINE_FINAL_TYPE (DexStateMachine, dex_state_machine, DEX_TYPE_OBJECT)
117 DEX_DEFINE_FINAL_TYPE (DexStateMachineWaiter, dex_state_machine_waiter, DEX_TYPE_FUTURE)
118 DEX_DEFINE_CLOSURE_TYPE (DexStateMachineRun, dex_state_machine_run,
119 DEX_DEFINE_CLOSURE_POINTER (DexStateMachine *, state_machine, dex_unref),
120 DEX_DEFINE_CLOSURE_VALUE (guint, target))
121
122 #undef DEX_TYPE_STATE_MACHINE
123 #define DEX_TYPE_STATE_MACHINE dex_state_machine_type
124
125 static void
126 dex_state_machine_waiter_complete (DexStateMachineWaiter *waiter,
127 GType state_enum_type,
128 guint state)
129 {
130 GValue value = G_VALUE_INIT;
131
132 g_assert (waiter != NULL);
133 g_assert (!waiter->queued);
134 g_assert (G_TYPE_IS_ENUM (state_enum_type));
135
136 g_value_init (&value, state_enum_type);
137 g_value_set_enum (&value, state);
138 dex_future_complete (DEX_FUTURE (waiter), &value, NULL);
139 g_value_unset (&value);
140 }
141
142 static void
143 dex_state_machine_waiter_discard (DexFuture *future)
144 {
145 DexStateMachineWaiter *waiter = (DexStateMachineWaiter *)future;
146 DexStateMachine *state_machine;
147 gboolean unref = FALSE;
148
149 g_assert (waiter != NULL);
150
151 if (!(state_machine = dex_weak_ref_get (&waiter->state_machine_wr)))
152 return;
153
154 dex_object_lock (state_machine);
155 if (waiter->queued)
156 {
157 g_queue_unlink (&state_machine->waiters, &waiter->link);
158 waiter->queued = FALSE;
159 unref = TRUE;
160 }
161 dex_object_unlock (state_machine);
162
163 if (unref)
164 dex_unref (waiter);
165
166 dex_unref (state_machine);
167 }
168
169 static void
170 dex_state_machine_waiter_finalize (DexObject *object)
171 {
172 DexStateMachineWaiter *waiter = (DexStateMachineWaiter *)object;
173
174 g_assert (!waiter->queued);
175
176 dex_weak_ref_clear (&waiter->state_machine_wr);
177
178 DEX_OBJECT_CLASS (dex_state_machine_waiter_parent_class)->finalize (object);
179 }
180
181 static void
182 dex_state_machine_waiter_class_init (DexStateMachineWaiterClass *waiter_class)
183 {
184 DexObjectClass *object_class = DEX_OBJECT_CLASS (waiter_class);
185 DexFutureClass *future_class = DEX_FUTURE_CLASS (waiter_class);
186
187 object_class->finalize = dex_state_machine_waiter_finalize;
188
189 future_class->discard = dex_state_machine_waiter_discard;
190 }
191
192 static void
193 dex_state_machine_waiter_init (DexStateMachineWaiter *waiter)
194 {
195 waiter->link.data = waiter;
196 }
197
198 static DexStateMachineWaiter *
199 dex_state_machine_waiter_new (DexStateMachine *state_machine,
200 guint state)
201 {
202 DexStateMachineWaiter *waiter;
203
204 g_assert (DEX_IS_STATE_MACHINE (state_machine));
205
206 waiter = (DexStateMachineWaiter *)dex_object_create_instance (dex_state_machine_waiter_type);
207 waiter->state = state;
208 dex_weak_ref_init (&waiter->state_machine_wr, state_machine);
209
210 return waiter;
211 }
212
213 static gboolean
214 dex_state_machine_is_valid_state (DexStateMachine *state_machine,
215 guint state)
216 {
217 GEnumClass *enum_class;
218
219 g_assert (DEX_IS_STATE_MACHINE (state_machine));
220
221 enum_class = g_type_class_get (state_machine->state_enum_type);
222
223 return g_enum_get_value (enum_class, state) != NULL;
224 }
225
226 static char *
227 dex_state_machine_dup_state_name (DexStateMachine *state_machine,
228 guint state)
229 {
230 GEnumClass *enum_class;
231 GEnumValue *value;
232 char *ret;
233
234 g_assert (DEX_IS_STATE_MACHINE (state_machine));
235
236 enum_class = g_type_class_get (state_machine->state_enum_type);
237 value = g_enum_get_value (enum_class, state);
238 ret = g_strdup (value != NULL ? value->value_nick : "<invalid>");
239
240 return ret;
241 }
242
243 static gboolean
244 dex_state_machine_validate_transitions (GType state_enum_type,
245 guint initial_state,
246 const DexStateTransition *transitions,
247 guint n_transitions)
248 {
249 GEnumClass *enum_class;
250 gboolean ret = TRUE;
251
252 g_assert (g_type_is_a (state_enum_type, G_TYPE_ENUM));
253 g_assert (transitions != NULL || n_transitions == 0);
254
255 enum_class = g_type_class_get (state_enum_type);
256
257 if (g_enum_get_value (enum_class, initial_state) == NULL)
258 ret = FALSE;
259
260 for (guint i = 0; ret && i < n_transitions; i++)
261 {
262 if (transitions[i].func == NULL ||
263 g_enum_get_value (enum_class, transitions[i].from) == NULL ||
264 g_enum_get_value (enum_class, transitions[i].to) == NULL)
265 {
266 ret = FALSE;
267 break;
268 }
269
270 for (guint j = i + 1; j < n_transitions; j++)
271 {
272 if (transitions[i].from == transitions[j].from &&
273 transitions[i].to == transitions[j].to)
274 {
275 ret = FALSE;
276 break;
277 }
278 }
279 }
280
281 return ret;
282 }
283
284 static const DexStateTransition *
285 dex_state_machine_lookup (DexStateMachine *state_machine,
286 guint from,
287 guint to)
288 {
289 g_assert (DEX_IS_STATE_MACHINE (state_machine));
290
291 for (guint i = 0; i < state_machine->n_transitions; i++)
292 {
293 if (state_machine->transitions[i].from == from &&
294 state_machine->transitions[i].to == to)
295 return &state_machine->transitions[i];
296 }
297
298 return NULL;
299 }
300
301 static gboolean
302 dex_state_machine_set_invalid_transition_error (DexStateMachine *state_machine,
303 guint from,
304 guint to,
305 GError **error)
306 {
307 g_autofree char *from_name = NULL;
308 g_autofree char *to_name = NULL;
309
310 g_assert (DEX_IS_STATE_MACHINE (state_machine));
311
312 from_name = dex_state_machine_dup_state_name (state_machine, from);
313 to_name = dex_state_machine_dup_state_name (state_machine, to);
314
315 g_set_error (error,
316 DEX_ERROR,
317 DEX_ERROR_INVALID_TRANSITION,
318 "Invalid transition from `%s` to `%s`",
319 from_name,
320 to_name);
321
322 return FALSE;
323 }
324
325 static gboolean
326 dex_state_machine_set_state (DexStateMachine *state_machine,
327 guint state)
328 {
329 GQueue waiters = G_QUEUE_INIT;
330
331 g_assert (DEX_IS_STATE_MACHINE (state_machine));
332
333 g_return_val_if_fail (dex_state_machine_is_valid_state (state_machine, state), FALSE);
334
335 dex_object_lock (state_machine);
336 state_machine->state = state;
337 for (const GList *iter = state_machine->waiters.head; iter; )
338 {
339 DexStateMachineWaiter *waiter = iter->data;
340
341 iter = iter->next;
342
343 if (waiter->state == state)
344 {
345 g_queue_unlink (&state_machine->waiters, &waiter->link);
346 g_queue_push_tail_link (&waiters, &waiter->link);
347 waiter->queued = FALSE;
348 }
349 }
350 dex_object_unlock (state_machine);
351
352 while (waiters.head != NULL)
353 {
354 DexStateMachineWaiter *waiter = g_queue_pop_head_link (&waiters)->data;
355
356 dex_state_machine_waiter_complete (waiter, state_machine->state_enum_type, state);
357 dex_unref (waiter);
358 }
359
360 return TRUE;
361 }
362
363 static gboolean
364 dex_state_machine_set_requested_state (DexStateMachine *state_machine,
365 guint state)
366 {
367 g_assert (DEX_IS_STATE_MACHINE (state_machine));
368
369 if (!dex_state_machine_is_valid_state (state_machine, state))
370 return FALSE;
371
372 dex_object_lock (state_machine);
373 state_machine->requested_state = state;
374 dex_object_unlock (state_machine);
375
376 return TRUE;
377 }
378
379 /* continue_to() synchronously enters another transition while the outer
380 * callback is still on the stack. Keep active_context stacked so interrupt()
381 * targets the innermost/current callback, then restores the outer interrupt
382 * scope when the continuation returns.
383 */
384 static void
385 dex_state_machine_push_context (DexStateMachine *state_machine,
386 DexStateTransitionContext *context)
387 {
388 g_assert (DEX_IS_STATE_MACHINE (state_machine));
389 g_assert (context != NULL);
390 g_assert (context->state_machine == state_machine);
391
392 dex_object_lock (state_machine);
393 context->previous_context = state_machine->active_context;
394 state_machine->active_context = context;
395 dex_object_unlock (state_machine);
396 }
397
398 static DexPromise *
399 dex_state_machine_pop_context (DexStateMachine *state_machine,
400 DexStateTransitionContext *context)
401 {
402 DexPromise *interrupt;
403
404 g_assert (DEX_IS_STATE_MACHINE (state_machine));
405 g_assert (context != NULL);
406 g_assert (context->state_machine == state_machine);
407
408 dex_object_lock (state_machine);
409 g_assert (state_machine->active_context == context);
410 state_machine->active_context = context->previous_context;
411 interrupt = g_steal_pointer (&context->interrupt);
412 dex_object_unlock (state_machine);
413
414 return interrupt;
415 }
416
417 static gboolean
418 dex_state_machine_transition_to (DexStateMachine *state_machine,
419 guint from,
420 guint target,
421 GError **error)
422 {
423 DexStateTransitionContext context = {0};
424 g_autoptr(DexPromise) interrupt = NULL;
425 const DexStateTransition *transition;
426 gboolean succeeded;
427
428 g_assert (DEX_IS_STATE_MACHINE (state_machine));
429
430 if (!dex_state_machine_is_valid_state (state_machine, target))
431 return dex_state_machine_set_invalid_transition_error (state_machine, from, target, error);
432
433 if (!(transition = dex_state_machine_lookup (state_machine, from, target)))
434 return dex_state_machine_set_invalid_transition_error (state_machine, from, target, error);
435
436 context.state_machine = state_machine;
437 context.from = from;
438 context.to = target;
439
440 dex_state_machine_push_context (state_machine, &context);
441
442 succeeded = transition->func (&context, state_machine->user_data, error);
443 interrupt = dex_state_machine_pop_context (state_machine, &context);
444
445 if (interrupt != NULL)
446 dex_promise_reject (interrupt,
447 g_error_new_literal (G_IO_ERROR,
448 G_IO_ERROR_CANCELLED,
449 "State transition context completed"));
450
451 if (!succeeded)
452 return FALSE;
453
454 if (!context.state_set)
455 dex_state_machine_set_state (state_machine, target);
456
457 return TRUE;
458 }
459
460 static DexFuture *
461 dex_state_machine_transition_fiber (gpointer user_data)
462 {
463 DexStateMachineRun *run = user_data;
464 DexStateMachine *state_machine = run->state_machine;
465 GError *error = NULL;
466 guint current;
467 guint final;
468 guint target;
469
470 g_assert (DEX_IS_STATE_MACHINE (state_machine));
471
472 dex_object_lock (state_machine);
473 current = state_machine->state;
474 dex_object_unlock (state_machine);
475
476 target = run->target;
477
478 if (!dex_state_machine_transition_to (state_machine, current, target, &error))
479 return dex_future_new_for_error (g_steal_pointer (&error));
480
481 final = dex_state_machine_get_state (state_machine);
482
483 return dex_future_new_enum (state_machine->state_enum_type, final);
484 }
485
486 static void
487 dex_state_machine_finalize (DexObject *object)
488 {
489 DexStateMachine *state_machine = DEX_STATE_MACHINE (object);
490
491 while (state_machine->waiters.head != NULL)
492 {
493 DexStateMachineWaiter *waiter = g_queue_pop_head_link (&state_machine->waiters)->data;
494
495 waiter->queued = FALSE;
496 dex_future_complete (DEX_FUTURE (waiter),
497 NULL,
498 g_error_new_literal (G_IO_ERROR,
499 G_IO_ERROR_CANCELLED,
500 "State machine was finalized"));
501 dex_unref (waiter);
502 }
503
504 dex_clear (&state_machine->limiter);
505 dex_clear (&state_machine->scheduler);
506 g_clear_pointer (&state_machine->transitions, g_free);
507
508 if (state_machine->user_data_destroy != NULL)
509 g_clear_pointer (&state_machine->user_data, state_machine->user_data_destroy);
510
511 DEX_OBJECT_CLASS (dex_state_machine_parent_class)->finalize (object);
512 }
513
514 static void
515 dex_state_machine_class_init (DexStateMachineClass *state_machine_class)
516 {
517 DexObjectClass *object_class = DEX_OBJECT_CLASS (state_machine_class);
518
519 object_class->finalize = dex_state_machine_finalize;
520
521 g_type_ensure (dex_state_machine_waiter_get_type ());
522 }
523
524 static void
525 dex_state_machine_init (DexStateMachine *state_machine)
526 {
527 state_machine->limiter = dex_limiter_new (1);
528 }
529
530 /**
531 * dex_state_transition_context_get_from:
532 * @context: a [[email protected]]
533 *
534 * Gets the source state for the transition edge being executed.
535 *
536 * This value is fixed for the lifetime of @context and does not change if
537 * [[email protected]_state] is called.
538 *
539 * Returns: the source state for the transition edge
540 *
541 * Since: 1.2
542 */
543 guint
544 dex_state_transition_context_get_from (DexStateTransitionContext *context)
545 {
546 g_return_val_if_fail (context != NULL, 0);
547
548 return context->from;
549 }
550
551 /**
552 * dex_state_transition_context_get_to:
553 * @context: a [[email protected]]
554 *
555 * Gets the target state for the transition edge being executed.
556 *
557 * This value is fixed for the lifetime of @context and does not change if
558 * [[email protected]_state] is called.
559 *
560 * Returns: the target state for the transition edge
561 *
562 * Since: 1.2
563 */
564 guint
565 dex_state_transition_context_get_to (DexStateTransitionContext *context)
566 {
567 g_return_val_if_fail (context != NULL, 0);
568
569 return context->to;
570 }
571
572 /**
573 * dex_state_transition_context_get_state:
574 * @context: a [[email protected]]
575 *
576 * Gets the real current state from the [[email protected]].
577 *
578 * Returns: the current state
579 *
580 * Since: 1.2
581 */
582 guint
583 dex_state_transition_context_get_state (DexStateTransitionContext *context)
584 {
585 g_return_val_if_fail (context != NULL, 0);
586
587 return dex_state_machine_get_state (context->state_machine);
588 }
589
590 /**
591 * dex_state_transition_context_set_state:
592 * @context: a [[email protected]]
593 * @state: the new state
594 *
595 * Sets the real current state in the [[email protected]].
596 *
597 * This may be used by transition callbacks to expose intermediate states while
598 * doing asynchronous work. @state must be a valid value in the state machine's
599 * enum type. This method may only be used while the transition callback is
600 * active.
601 *
602 * Since: 1.2
603 */
604 void
605 dex_state_transition_context_set_state (DexStateTransitionContext *context,
606 guint state)
607 {
608 g_return_if_fail (context != NULL);
609
610 if (dex_state_machine_set_state (context->state_machine, state))
611 context->state_set = TRUE;
612 }
613
614 /**
615 * dex_state_transition_context_wait_for_interrupt:
616 * @context: a [[email protected]]
617 *
618 * Creates a future that resolves when the active transition context is
619 * interrupted.
620 *
621 * This is a cooperative mechanism for long-running transition callbacks. The
622 * returned future resolves to %TRUE when [[email protected]]
623 * is called while @context is active. If the context was already interrupted
624 * before this function is called, the returned future is already resolved.
625 *
626 * If @context completes before it is interrupted, the returned future is
627 * rejected with %G_IO_ERROR_CANCELLED.
628 *
629 * Returns: (transfer full): a future resolving to %TRUE when interrupted
630 *
631 * Since: 1.2
632 */
633 DexFuture *
634 dex_state_transition_context_wait_for_interrupt (DexStateTransitionContext *context)
635 {
636 DexStateMachine *state_machine;
637 DexPromise *interrupt;
638
639 dex_return_error_if_fail (context != NULL);
640
641 state_machine = context->state_machine;
642
643 dex_return_error_if_fail (DEX_IS_STATE_MACHINE (state_machine));
644
645 dex_object_lock (state_machine);
646 if (context->interrupted)
647 {
648 dex_object_unlock (state_machine);
649 return dex_future_new_true ();
650 }
651
652 if (context->interrupt == NULL)
653 context->interrupt = dex_promise_new ();
654
655 interrupt = dex_ref (context->interrupt);
656 dex_object_unlock (state_machine);
657
658 return DEX_FUTURE (interrupt);
659 }
660
661 /**
662 * dex_state_transition_context_continue_to:
663 * @context: a [[email protected]]
664 * @target: the target state for the next edge
665 * @error: a location for a `GError`, or %NULL
666 *
667 * Attempts to continue from the current transition to @target immediately.
668 *
669 * The continuation runs while the state machine still holds its internal
670 * serialization slot, so queued [[email protected]] requests
671 * are not processed first. If @context has not explicitly set the state with
672 * [[email protected]_state], the current edge target is
673 * committed before the next edge is executed.
674 *
675 * The next transition callback is called before this function returns. If that
676 * callback uses `dex_await()`, the same transition fiber suspends and resumes,
677 * while the state machine still holds the serialization slot. Chained
678 * continuations therefore use the normal C call stack and should be reserved
679 * for short, bounded chains rather than unbounded graph traversal.
680 *
681 * The next edge is looked up from the real current state to @target. If no
682 * such edge exists, %FALSE is returned and @error is set to
683 * [[email protected]_TRANSITION]. If the next edge callback fails, its
684 * error is propagated.
685 *
686 * Returns: %TRUE if the continuation succeeded; otherwise %FALSE
687 *
688 * Since: 1.2
689 */
690 gboolean
691 dex_state_transition_context_continue_to (DexStateTransitionContext *context,
692 guint target,
693 GError **error)
694 {
695 DexStateMachine *state_machine;
696 guint from;
697
698 g_return_val_if_fail (context != NULL, FALSE);
699
700 state_machine = context->state_machine;
701
702 g_return_val_if_fail (DEX_IS_STATE_MACHINE (state_machine), FALSE);
703
704 from = context->state_set ? dex_state_machine_get_state (state_machine) : context->to;
705
706 if (!dex_state_machine_is_valid_state (state_machine, target))
707 return dex_state_machine_set_invalid_transition_error (state_machine, from, target, error);
708
709 if (dex_state_machine_lookup (state_machine, from, target) == NULL)
710 return dex_state_machine_set_invalid_transition_error (state_machine, from, target, error);
711
712 if (!context->state_set)
713 {
714 if (!dex_state_machine_set_state (state_machine, context->to))
715 return FALSE;
716
717 context->state_set = TRUE;
718 }
719
720 return dex_state_machine_transition_to (state_machine, from, target, error);
721 }
722
723 /**
724 * dex_state_machine_new: (skip)
725 * @state_enum_type: a `GType` for a `GEnum`
726 * @initial_state: the initial state value
727 * @transitions: (array length=n_transitions): transition entries
728 * @n_transitions: the number of transition entries
729 * @scheduler: (nullable): scheduler to spawn transition fibers on, or %NULL
730 * for the thread default
731 * @stack_size: stack size for transition fibers, or zero to use the default
732 * @user_data: (nullable): user data for transition callbacks
733 * @user_data_destroy: (nullable): destroy notify for @user_data
734 *
735 * Creates a new [[email protected]].
736 *
737 * The transition table is copied and validated before the state machine is
738 * returned. Each transition must have a valid @from state, valid @to state,
739 * and non-%NULL callback. Duplicate edges are rejected.
740 *
741 * Returns: (transfer full): a new [[email protected]]
742 */
743 DexStateMachine *
744 dex_state_machine_new (GType state_enum_type,
745 guint initial_state,
746 const DexStateTransition *transitions,
747 guint n_transitions,
748 DexScheduler *scheduler,
749 gsize stack_size,
750 gpointer user_data,
751 GDestroyNotify user_data_destroy)
752 {
753 DexStateMachine *state_machine;
754
755 g_return_val_if_fail (dex_state_machine_type != G_TYPE_INVALID, NULL);
756 g_return_val_if_fail (g_type_is_a (state_enum_type, G_TYPE_ENUM), NULL);
757 g_return_val_if_fail (transitions != NULL || n_transitions == 0, NULL);
758 g_return_val_if_fail (scheduler == NULL || DEX_IS_SCHEDULER (scheduler), NULL);
759 g_return_val_if_fail (dex_state_machine_validate_transitions (state_enum_type,
760 initial_state,
761 transitions,
762 n_transitions), NULL);
763
764 state_machine = (DexStateMachine *)dex_object_create_instance (dex_state_machine_type);
765 state_machine->state_enum_type = state_enum_type;
766 state_machine->state = initial_state;
767 state_machine->requested_state = initial_state;
768 state_machine->n_transitions = n_transitions;
769 state_machine->scheduler = scheduler ? dex_ref (scheduler) : NULL;
770 state_machine->stack_size = stack_size;
771 state_machine->user_data = user_data;
772 state_machine->user_data_destroy = user_data_destroy;
773
774 if (n_transitions > 0)
775 state_machine->transitions = g_memdup2 (transitions, sizeof *transitions * n_transitions);
776
777 return state_machine;
778 }
779
780 /**
781 * dex_state_machine_transition:
782 * @state_machine: a [[email protected]]
783 * @target: the target state
784 *
785 * Requests a transition to @target.
786 *
787 * If @target is a valid value for the state machine's enum type, it becomes
788 * the requested state immediately and can be read with
789 * [[email protected]_requested_state]. The requested state is the
790 * most recent valid target passed to this method. It may differ from the
791 * current state and does not imply that the transition will succeed.
792 *
793 * Transition requests are serialized. The matching callback is run from a
794 * fiber and may use `dex_await()` to wait for asynchronous work. If the
795 * callback succeeds without calling
796 * [[email protected]_state], the state machine commits
797 * @target. If the callback updates the state directly, the returned future
798 * resolves to the current state after the callback returns.
799 *
800 * Returns: (transfer full): a future resolving to the final enum state
801 */
802 DexFuture *
803 dex_state_machine_transition (DexStateMachine *state_machine,
804 guint target)
805 {
806 DexStateMachineRun *run;
807
808 dex_return_error_if_fail (DEX_IS_STATE_MACHINE (state_machine));
809
810 dex_state_machine_set_requested_state (state_machine, target);
811
812 run = dex_state_machine_run_new ();
813 run->state_machine = dex_ref (state_machine);
814 run->target = target;
815
816 return dex_limiter_run (state_machine->limiter,
817 state_machine->scheduler,
818 state_machine->stack_size,
819 dex_state_machine_transition_fiber,
820 run,
821 (GDestroyNotify)dex_state_machine_run_free);
822 }
823
824 /**
825 * dex_state_machine_wait_for_state:
826 * @state_machine: a [[email protected]]
827 * @state: the state to wait for
828 *
829 * Waits until @state_machine enters @state.
830 *
831 * If @state_machine is already in @state, the returned future is already
832 * resolved. Otherwise, the future resolves the next time the current state is
833 * set to @state. This does not request a transition; use
834 * [[email protected]] to move the state machine.
835 *
836 * Returns: (transfer full): a future resolving to @state
837 *
838 * Since: 1.2
839 */
840 DexFuture *
841 dex_state_machine_wait_for_state (DexStateMachine *state_machine,
842 guint state)
843 {
844 DexStateMachineWaiter *waiter;
845
846 dex_return_error_if_fail (DEX_IS_STATE_MACHINE (state_machine));
847 dex_return_error_if_fail (dex_state_machine_is_valid_state (state_machine, state));
848
849 waiter = dex_state_machine_waiter_new (state_machine, state);
850
851 dex_object_lock (state_machine);
852 if (state_machine->state == state)
853 {
854 dex_object_unlock (state_machine);
855 dex_unref (waiter);
856 return dex_future_new_enum (state_machine->state_enum_type, state);
857 }
858
859 dex_ref (waiter);
860 waiter->queued = TRUE;
861 g_queue_push_tail_link (&state_machine->waiters, &waiter->link);
862 dex_object_unlock (state_machine);
863
864 return DEX_FUTURE (waiter);
865 }
866
867 /**
868 * dex_state_machine_interrupt:
869 * @state_machine: a [[email protected]]
870 *
871 * Cooperatively interrupts the active transition callback.
872 *
873 * If a transition callback is currently active, its interrupt future returned
874 * by [[email protected]_for_interrupt] is resolved. This
875 * does not request a transition or change the current state; the active
876 * callback decides how to handle the interrupt.
877 *
878 * Returns: %TRUE if an active transition context was marked interrupted;
879 * otherwise %FALSE
880 *
881 * Since: 1.2
882 */
883 gboolean
884 dex_state_machine_interrupt (DexStateMachine *state_machine)
885 {
886 DexStateTransitionContext *context;
887 DexPromise *interrupt = NULL;
888 gboolean ret = FALSE;
889
890 g_return_val_if_fail (DEX_IS_STATE_MACHINE (state_machine), FALSE);
891
892 dex_object_lock (state_machine);
893 if ((context = state_machine->active_context))
894 {
895 context->interrupted = TRUE;
896 if (context->interrupt != NULL)
897 interrupt = dex_ref (context->interrupt);
898 ret = TRUE;
899 }
900 dex_object_unlock (state_machine);
901
902 if (interrupt != NULL)
903 {
904 dex_promise_resolve_boolean (interrupt, TRUE);
905 dex_unref (interrupt);
906 }
907
908 return ret;
909 }
910
911 /**
912 * dex_state_machine_get_requested_state:
913 * @state_machine: a [[email protected]]
914 *
915 * Gets the most recent valid state requested with
916 * [[email protected]].
917 *
918 * The requested state is initialized to the initial state passed to
919 * [[email protected]]. It is updated immediately when
920 * [[email protected]] is called with a valid target state,
921 * before that transition is run. It is not a guarantee that the transition
922 * will run or succeed, and it may differ from the current state returned by
923 * [[email protected]_state].
924 *
925 * Returns: the most recent valid requested state
926 *
927 * Since: 1.2
928 */
929 guint
930 dex_state_machine_get_requested_state (DexStateMachine *state_machine)
931 {
932 guint ret;
933
934 g_return_val_if_fail (DEX_IS_STATE_MACHINE (state_machine), 0);
935
936 dex_object_lock (state_machine);
937 ret = state_machine->requested_state;
938 dex_object_unlock (state_machine);
939
940 return ret;
941 }
942
943 /**
944 * dex_state_machine_get_state:
945 * @state_machine: a [[email protected]]
946 *
947 * Gets the current state of @state_machine.
948 *
949 * Returns: the current state
950 */
951 guint
952 dex_state_machine_get_state (DexStateMachine *state_machine)
953 {
954 guint ret;
955
956 g_return_val_if_fail (DEX_IS_STATE_MACHINE (state_machine), 0);
957
958 dex_object_lock (state_machine);
959 ret = state_machine->state;
960 dex_object_unlock (state_machine);
961
962 return ret;
963 }