| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 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 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 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 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 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 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 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 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 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 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 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 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 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 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 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 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 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 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 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 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 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 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 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 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 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 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 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 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 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 |
} |