5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2005, 2006, 2007 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
20 /****h* silcutil/SILC Finite State Machine
24 * SILC FSM Interface implements a finite state machine. The FSM can be
25 * used to implement all kinds of machines and protocols. The FSM supports
26 * also threads and can be synchronized by using mutex locks. The FSM
27 * also supports real system threads. It is possible to create new FSM
28 * thread and then execute in real system thread, if platform supports
31 * The FSM provides also asynchronous events that can be used to wait for
32 * some events or states to occur. The FSM events may be used as condition
33 * variables and signallers. The FSM events can safely be used in FSM
34 * threads that are executed in real system threads.
36 * To synchronize machines that use FSM threads that are executed in real
37 * system threads the SILC Mutex API (silcmutex.h) may be used. Normal
38 * multi-threaded coding conventions apply when programming with real FSM
39 * threads. If the FSM threads are not real system threads, synchronization
47 /****s* silcutil/SilcFSMAPI/SilcFSM
51 * typedef struct SilcFSMObject *SilcFSM;
55 * The actual FSM context and is allocated with silc_fsm_alloc and
56 * given as argument to all silc_fsm_* functions. It is freed by
57 * silc_fsm_free function. It is also possible to use pre-allocated
58 * FSM context by using SilcFSMStruct instead of SilcFSM.
61 typedef struct SilcFSMObject *SilcFSM;
63 /****s* silcutil/SilcFSMAPI/SilcFSMStruct
67 * typedef struct SilcFSMObject SilcFSMStruct;
71 * The actual FSM context and can be used as pre-allocated FSM context,
72 * instead of SilcFSM context. This context is initialized with the
73 * silc_fsm_init function. It need not be uninitialized.
76 typedef struct SilcFSMObject SilcFSMStruct;
78 /****s* silcutil/SilcFSMAPI/SilcFSMThread
82 * typedef struct SilcFSMObject *SilcFSMThread;
86 * FSM thread context. The SILC FSM supports threads, virtual machine
87 * threads (inside FSM) and actual real system threads if platorm
88 * supports them. In a complex machine certain complex operations may
89 * be desired to execute in a thread. The SilcFSMThread is allocated
90 * by silc_fsm_thread_alloc and feed by silc_fsm_free. It is also
91 * possible to use pre-allocated thread by using SilcFSMThreadStruct
92 * instead of SilcFSMThread.
95 typedef struct SilcFSMObject *SilcFSMThread;
97 /****s* silcutil/SilcFSMAPI/SilcFSMThreadStruct
101 * typedef struct SilcFSMObject SilcFSMThreadStruct;
105 * FSM thread context and can be used as a pre-allocated FSM thread context,
106 * instead of SilcFSMThread context. This context is initialized with the
107 * silc_fsm_thread_init function. It need not be uninitialized.
110 typedef struct SilcFSMObject SilcFSMThreadStruct;
112 /****d* silcutil/SilcFSMAPI/SILC_FSM_CONTINUE
116 * #define SILC_FSM_CONTINUE ...
120 * Moves to next state synchronously. This type is used is returned
121 * from state functions to immediately move to next state.
125 * SILC_FSM_STATE(silc_foo_state)
129 * // Move to next state now
130 * silc_fsm_next(fsm, silc_foo_next_state);
131 * return SILC_FSM_CONTINUE;
135 #if defined(SILC_DEBUG)
136 #define SILC_FSM_CONTINUE \
137 fsm->next_state(fsm, fsm->fsm_context, fsm->state_context);
139 #define SILC_FSM_CONTINUE SILC_FSM_ST_CONTINUE;
140 #endif /* SILC_DEBUG */
142 /****d* silcutil/SilcFSMAPI/SILC_FSM_YIELD
146 * #define SILC_FSM_YIELD ...
150 * Moves to next state through the machine scheduler. Other threads
151 * running in the machine will get running time with SILC_FSM_YIELD.
152 * When using real threads, using SILC_FSM_YIELD is usually unnecessary.
153 * This type is returned in the state function.
156 #define SILC_FSM_YIELD SILC_FSM_ST_YIELD;
158 /****d* silcutil/SilcFSMAPI/SILC_FSM_WAIT
162 * #define SILC_FSM_WAIT ...
166 * Suspends the machine or thread until it is awaken. This is used
167 * when asynchronous call is made or timer is set, or something else
168 * that requires waiting. This type is returned in the state function.
171 #define SILC_FSM_WAIT SILC_FSM_ST_WAIT
173 /****d* silcutil/SilcFSMAPI/SILC_FSM_FINISH
177 * #define SILC_FSM_FINISH ...
181 * Finishes the machine or thread and calls its destructor, if defined.
182 * If the machine is finished when it has running threads the machine
183 * will fatally fail. User must always finish the threads before
184 * finishing the machine. This type is returned in the state function.
187 #define SILC_FSM_FINISH SILC_FSM_ST_FINISH
189 /****f* silcutil/SilcFSMAPI/SilcFSMDestructor
193 * typedef void (*SilcFSMDestructor)(SilcFSM fsm, void *fsm_context,
194 * void *destructor_context);
198 * The destructor callback that was set in silc_fsm_alloc or in
199 * silc_fsm_init function. It will be called when a state function
200 * returns SILC_FSM_FINISH. This function will be called through
201 * the scheduler; it will not be called immediately after the state
202 * function returns SILC_FSM_FINISH, but will be called later. The
203 * `fsm' can be freed in this function.
206 typedef void (*SilcFSMDestructor)(SilcFSM fsm, void *fsm_context,
207 void *destructor_context);
209 /****f* silcutil/SilcFSMAPI/SilcFSMThreadDestructor
213 * typedef void (*SilcFSMThreadDestructor)(SilcFSMThread thread,
214 * void *thread_context,
215 * void *destructor_context);
219 * The destructor callback that was set in silc_fsm_thread_alloc or in
220 * silc_fsm_thread_init function. It will be called when a state function
221 * returns SILC_FSM_FINISH. This function will be called through the
222 * scheduler; it will not be called immediately after the state function
223 * returns SILC_FSM_FINISH, but will be called later. The `thread' can
224 * be freed in this function.
228 * Even if the `thread' was executed in real system thread, this callback
229 * is always received in the main machine thread, not in the created
233 typedef void (*SilcFSMThreadDestructor)(SilcFSMThread thread,
234 void *thread_context,
235 void *destructor_context);
237 /****d* silcutil/SilcFSMAPI/SILC_FSM_STATE
241 * #define SILC_FSM_STATE(name)
245 * This macro is used to declare an FSM state function. The `fsm' is
246 * the SilcFSM or SilcFSMThread context, the `fsm_context' is the context
247 * given as argument to silc_fsm_alloc, silc_fsm_init, silc_fsm_thread_init,
248 * or silc_fsm_thread_alloc function. The `state_context' is the optional
249 * state specific context set with silc_fsm_set_state_context function.
253 #define SILC_FSM_STATE(name) \
254 int name(struct SilcFSMObject *fsm, void *fsm_context, void *state_context)
257 /* State function callback */
258 typedef int (*SilcFSMStateCallback)(struct SilcFSMObject *fsm,
260 void *state_context);
262 /****d* silcutil/SilcFSMAPI/SILC_FSM_CALL
266 * SILC_FSM_CALL(function)
270 * Macro used to call asynchronous calls from state function. If the
271 * call is not really asynchronous then this will cause the machine to
272 * directly proceed to next state. If the call is truly asynchronous
273 * then this will set the machine to wait state. The silc_fsm_next
274 * must be called before this macro, so that the next state is set.
278 * The state function returns in this macro.
283 * silc_fsm_next(fsm, some_next_state);
284 * SILC_FSM_CALL(silc_some_async_call(server, some_callback, context));
286 * // More complex example
287 * silc_fsm_next(fsm, some_next_state);
288 * SILC_FSM_CALL((some_context->operation =
289 * silc_some_async_call(server, some_callback, context)));
292 #define SILC_FSM_CALL(function) \
294 SILC_VERIFY(!silc_fsm_set_call(fsm, TRUE)); \
296 if (!silc_fsm_set_call(fsm, FALSE)) \
297 return SILC_FSM_CONTINUE; \
298 return SILC_FSM_WAIT; \
301 /****d* silcutil/SilcFSMAPI/SILC_FSM_CALL_CONTINUE
305 * SILC_FSM_CALL_CONTINUE(fsm)
309 * Macro used to proceed after asynchornous call. This is called in the
310 * callback of the asynchronous call to continue in the state machine.
314 * void some_callback(void *context) {
315 * SilcFSM fsm = context;
317 * // Continue to the next state
318 * SILC_FSM_CALL_CONTINUE(fsm);
322 #define SILC_FSM_CALL_CONTINUE(fsm) \
324 if (!silc_fsm_set_call(fsm, FALSE)) \
325 silc_fsm_continue(fsm); \
328 /****d* silcutil/SilcFSMAPI/SILC_FSM_CALL_CONTINUE_SYNC
332 * SILC_FSM_CALL_CONTINUE_SYNC(fsm)
336 * Macro used to proceed after asynchornous call. This is called in the
337 * callback of the asynchronous call to continue in the state machine.
338 * This continues to the next state synchronously, not through the
343 * void some_callback(void *context) {
344 * SilcFSM fsm = context;
346 * // Continue to the next state immediately
347 * SILC_FSM_CALL_CONTINUE_SYNC(fsm);
351 #define SILC_FSM_CALL_CONTINUE_SYNC(fsm) \
353 if (!silc_fsm_set_call(fsm, FALSE)) \
354 silc_fsm_continue_sync(fsm); \
357 /****d* silcutil/SilcFSMAPI/SILC_FSM_THREAD_WAIT
361 * SILC_FSM_THREAD_WAIT(thread)
365 * Macro used to wait for the `thread' to terminate. The machine or
366 * thread will be suspended while it is waiting for the thread to
367 * terminate. The machine or thread will continue once the waited
368 * thread has terminated.
372 * The state function returns in this macro.
374 * This macro is the only way to safely make sure that the thread has
375 * terminated by the time FSM continues from the waiting state. Using
376 * FSM events to signal from the thread before SILC_FSM_FINISH is returned
377 * works with normal FSM threads, but especially with real system threads
378 * it does not guarantee that the FSM won't continue before the thread has
379 * actually terminated. Usually this is not a problem, but it can be a
380 * problem if the FSM is waiting to be freed. In this case using this
381 * macro is strongly recommended.
384 #define SILC_FSM_THREAD_WAIT(thread) \
386 silc_fsm_thread_wait(fsm, thread); \
387 return SILC_FSM_WAIT; \
390 /****f* silcutil/SilcFSMAPI/silc_fsm_alloc
394 * SilcFSM silc_fsm_alloc(void *fsm_context,
395 * SilcFSMDestructor destructor,
396 * void *destructor_context,
397 * SilcSchedule schedule);
401 * Allocates SILC Finite State Machine context. The `destructor' with
402 * `destructor_context' will be called when the machines finishes. The
403 * caller must free the returned context with silc_fsm_free. The
404 * `fsm_context' is delivered to every FSM state function. The `schedule'
405 * is the caller's scheduler and the FSM will be run in the scheduler.
409 * SilcAsyncOperation silc_async_call(Callback callback, void *cb_context)
411 * SilcAsyncOperation op;
415 * // Allocate async operation so that caller can control us, like abort
416 * op = silc_async_alloc(silc_async_call_abort, NULL, ourcontext);
419 * fsm = silc_fsm_alloc(ourcontext, fsm_destructor, ourcontext,
421 * silc_fsm_start(fsm, first_state);
424 * // Return async operation for upper layer
429 SilcFSM silc_fsm_alloc(void *fsm_context,
430 SilcFSMDestructor destructor,
431 void *destructor_context,
432 SilcSchedule schedule);
434 /****f* silcutil/SilcFSMAPI/silc_fsm_init
438 * SilcBool silc_fsm_init(SilcFSM fsm,
440 * SilcFSMDestructor destructor,
441 * void *destructor_context,
442 * SilcSchedule schedule);
446 * Initializes a pre-allocated SilcFSM context. This call is equivalent
447 * to silc_fsm_alloc except that this takes the pre-allocated context
448 * as argument. The silc_fsm_free must not be called if this was called.
449 * Returns TRUE if the initialization is Ok or FALSE if error occurred.
450 * This function does not allocate any memory. The `schedule' is the
451 * caller's scheduler and the FSM will be run in the scheduler.
457 * silc_fsm_init(&fsm, application, fsm_destructor, application, schedule);
458 * silc_fsm_start(&fsm, first_state);
461 SilcBool silc_fsm_init(SilcFSM fsm,
463 SilcFSMDestructor destructor,
464 void *destructor_context,
465 SilcSchedule schedule);
467 /****f* silcutil/SilcFSMAPI/silc_fsm_thread_alloc
471 * SilcFSMThread silc_fsm_thread_alloc(SilcFSM fsm,
472 * void *thread_context,
473 * SilcFSMThreadDestructor destructor,
474 * void *destructor_context,
475 * SilcBool real_thread);
479 * Allocates FSM thread context. The thread will be executed in the
480 * FSM machine indicated by `fsm'. The caller must free the returned
481 * thread context with silc_fsm_free. If the 'real_thread' is TRUE
482 * then the thread will actually be executed in real thread, if platform
483 * supports them. The `thread_context' is delivered to every state
484 * function in the thread.
488 * If the system does not support threads, then this function will revert
489 * back to normal FSM threads.
491 * If the `real_thread' is TRUE then FSM will allocate new SilcSchedule
492 * for the FSM thread. If you need scheduler in the real thread it is
493 * strongly recommended that you use the SilcSchedule that is allocated
494 * for the thread. You can retrieve the SilcSchedule from the thread
495 * using silc_fsm_get_schedule function. Note that, the allocated
496 * SilcSchedule will become invalid after the thread finishes.
498 * If `real_thread' is FALSE the silc_fsm_get_schedule will return
499 * the SilcSchedule that was originally given to silc_fsm_alloc or
504 * SILC_FSM_STATE(silc_foo_state)
506 * SilcFSMThread thread;
509 * // Execute the route lookup in thread
510 * thread = silc_fsm_thread_alloc(fsm, fsm_context, NULL, NULL, FALSE);
511 * silc_fsm_start(thread, silc_route_lookup_start);
513 * // Wait here for the thread to terminate. Set the state where to go
514 * // after the thread has terminated.
515 * silc_fsm_next(fsm, silc_foo_route_lookup_finished);
516 * SILC_FSM_THREAD_WAIT(thread);
520 SilcFSMThread silc_fsm_thread_alloc(SilcFSM fsm,
521 void *thread_context,
522 SilcFSMThreadDestructor destructor,
523 void *destructor_context,
524 SilcBool real_thread);
526 /****f* silcutil/SilcFSMAPI/silc_fsm_thread_init
530 * void silc_fsm_thread_init(SilcFSMThread thread,
532 * void *thread_context,
533 * SilcFSMThreadDestructor destructor,
534 * void *destructor_context,
535 * SilcBool real_thread);
539 * Initializes a pre-allocated SilcFSMThread context. This call is
540 * equivalent to silc_fsm_thread_alloc except that this takes the
541 * pre-allocated context as argument. The silc_fsm_free must not be
542 * called if this was called. If the `real_thread' is TRUE then the
543 * thread will actually be executed in real thread, if platform supports
548 * See the notes from the silc_fsm_thread_alloc.
552 * SilcFSMThreadStruct thread;
554 * silc_fsm_thread_init(&thread, fsm, application, NULL, NULL, FALSE);
555 * silc_fsm_start(&thread, first_state);
558 void silc_fsm_thread_init(SilcFSMThread thread,
560 void *thread_context,
561 SilcFSMThreadDestructor destructor,
562 void *destructor_context,
563 SilcBool real_thread);
565 /****f* silcutil/SilcFSMAPI/silc_fsm_free
569 * void silc_fsm_free(void *fsm);
573 * Free the SILC FSM context that was allocated with silc_fsm_alloc,
574 * or free the SILC FSM thread context that was allocated with
575 * silc_fsm_thread_alloc. This function is used with both SilcFSM
576 * and SilcFSMThread contexts.
580 * When freeing FSM, it must not have any active threads.
583 void silc_fsm_free(void *fsm);
585 /****f* silcutil/SilcFSMAPI/silc_fsm_start
589 * void silc_fsm_start(void *fsm, SilcFSMStateCallback start_state);
593 * This function must be called after the SILC FSM context was created.
594 * This actually starts the state machine. Note that, the machine is
595 * started later after this function returns. The `start_state' is the
596 * state where the machine or thread is started. This function is used
597 * with both SilcFSM and SilcFSMThread contexts.
603 * fsm = silc_fsm_alloc(context, destructor, context, schedule);
604 * silc_fsm_start(fsm, first_state);
607 void silc_fsm_start(void *fsm, SilcFSMStateCallback start_state);
609 /****f* silcutil/SilcFSMAPI/silc_fsm_start_sync
613 * void silc_fsm_start_sync(void *fsm, SilcFSMStateCallback start_state);
617 * This function is same as silc_fsm_start, except that the FSM will
618 * be started immediately inside this function. After this function
619 * returns the `start_state' has already been executed. If the machine
620 * is completely synchronous (no waiting used in the machine) then
621 * the machine will have finished once this function returns. Also
622 * note that if the machine is completely synchronous the destructor
623 * will also be called from inside this function. This function is used
624 * with both SilcFSM and SilcFSMThread contexts.
627 void silc_fsm_start_sync(void *fsm, SilcFSMStateCallback start_state);
629 /****f* silcutil/SilcFSMAPI/silc_fsm_next
633 * void silc_fsm_next(void *fsm, SilcFSMStateCallback next_state);
637 * Set the next state to be executed. If the state function that
638 * call this function returns SILC_FSM_CONTINUE, the `next_state'
639 * will be executed immediately. If it returns SILC_FSM_YIELD it
640 * yields the thread and the `next_state' will be run after other
641 * threads have run first. This function must always be used to set
642 * the next state in the machine or thread. This function is used
643 * with both SilcFSM and SilcFSMThread contexts.
647 * // Move to next state
648 * silc_fsm_next(fsm, next_state);
649 * return SILC_FSM_CONTINUE;
652 void silc_fsm_next(void *fsm, SilcFSMStateCallback next_state);
654 /****f* silcutil/SilcFSMAPI/silc_fsm_next_later
658 * void silc_fsm_next_later(void *fsm, SilcFSMStateCallback next_state,
659 * SilcUInt32 seconds, SilcUInt32 useconds);
663 * Set the next state to be executed later, at the specified time.
664 * The SILC_FSM_WAIT must be returned in the state function if this
665 * function is called. If any other state is returned machine operation
666 * is undefined. The machine or thread will move to `next_state' after
667 * the specified timeout. This function is used with both SilcFSM and
668 * SilcFSMThread contexts.
672 * If both `seconds' and `useconds' are 0, the effect is same as calling
673 * silc_fsm_next function, and SILC_FSM_CONTINUE must be returned.
675 * If silc_fsm_continue or silc_fsm_continue_sync is called while the
676 * machine or thread is in SILC_FSM_WAIT state the timeout is automatically
677 * canceled and the state moves to the next state.
681 * // Move to next state after 10 seconds
682 * silc_fsm_next_later(fsm, next_state, 10, 0);
683 * return SILC_FSM_WAIT;
686 void silc_fsm_next_later(void *fsm, SilcFSMStateCallback next_state,
687 SilcUInt32 seconds, SilcUInt32 useconds);
689 /****f* silcutil/SilcFSMAPI/silc_fsm_continue
693 * void silc_fsm_continue(void *fsm);
697 * Continues in the state machine from a SILC_FSM_WAIT state. This can
698 * be called from outside waiting FSM to continue to the next state.
699 * This function can be used instead of SILC_FSM_CALL_CONTINUE macro
700 * in case the SILC_FSM_CALL was not used. This must not be used if
701 * SILC_FSM_CALL was used. This function is used with both SilcFSM and
702 * SilcFSMThread contexts.
705 void silc_fsm_continue(void *fsm);
707 /****f* silcutil/SilcFSMAPI/silc_fsm_continue_sync
711 * void silc_fsm_continue_sync(void *fsm);
715 * Continues immediately in the state machine from a SILC_FSM_WAIT state.
716 * This can be called from outside waiting FSM to immediately continue to
717 * the next state. This function can be used instead of the
718 * SILC_FSM_CALL_CONTINUE_SYNC macro in case the SILC_FSM_CALL was not used.
719 * This must not be used if SILC_FSM_CALL was used. This function is used
720 * with both SilcFSM and SilcFSMThread contexts.
723 void silc_fsm_continue_sync(void *fsm);
725 /****f* silcutil/SilcFSMAPI/silc_fsm_finish
729 * void silc_fsm_finish(void *fsm);
733 * Finishes the `fsm'. This function may be used in case the FSM
734 * needs to be finished outside FSM states. Usually FSM is finished
735 * by returning SILC_FSM_FINISH from the state, but if this is not
736 * possible this function may be called. This function is used with
737 * both SilcFSM and SilcFSMThread contexts.
739 * If the `fsm' is a machine and it has running threads, the machine
740 * will fatally fail. The caller must first finish the threads and
744 void silc_fsm_finish(void *fsm);
746 /****f* silcutil/SilcFSMAPI/silc_fsm_set_context
750 * void silc_fsm_set_context(void *fsm, void *fsm_context);
754 * Set new context for the `fsm'. This function can be used to change
755 * the context inside the `fsm', if needed. This function is used with
756 * both SilcFSM and SilcFSMThread contexts. The context is the
757 * `fsm_context' in the state function (SILC_FSM_STATE).
760 void silc_fsm_set_context(void *fsm, void *fsm_context);
762 /****f* silcutil/SilcFSMAPI/silc_fsm_get_context
766 * void *silc_fsm_get_context(void *fsm);
770 * Returns the context associated with the `fsm'. It is the context that
771 * was given to silc_fsm_alloc, silc_fsm_init, silc_fsm_thread_alloc or
772 * silc_fsm_thread_init. This function is used with both SilcFSM and
773 * SilcFSMThread contexts.
776 void *silc_fsm_get_context(void *fsm);
778 /****f* silcutil/SilcFSMAPI/silc_fsm_set_state_context
782 * void silc_fsm_set_state_context(void *fsm, void *state_context);
786 * Set's a state specific context for the `fsm'. This function can be
787 * used to change the state context inside the `fsm', if needed. This
788 * function is used with both SilcFSM and SilcFSMThread contexts. The
789 * context is the `state_context' in the state function (SILC_FSM_STATE).
792 void silc_fsm_set_state_context(void *fsm, void *state_context);
794 /****f* silcutil/SilcFSMAPI/silc_fsm_get_state_context
798 * void *silc_fsm_get_state_context(void *fsm);
802 * Returns the state context associated with the `fsm'. It is the context
803 * that was set with silc_fsm_set_state_context function. This function
804 * is used with both SilcFSM and SilcFSMThread contexts.
807 void *silc_fsm_get_state_context(void *fsm);
809 /****f* silcutil/SilcFSMAPI/silc_fsm_get_schedule
813 * SilcSchedule silc_fsm_get_schedule(void *fsm);
817 * Returns the SilcSchedule that has been associated with the `fsm'.
818 * If caller needs scheduler it may retrieve it with this function. This
819 * function is used with both SilcFSM and SilcFSMThread contexts.
821 * If the `fsm' is thread and real system threads are being used, and this
822 * is called from the thread, it will return the SilcSchedule that was
823 * allocated by the FSM for the thread. It is strongly recommended to
824 * use this SilcSchedule if you are using real threads, and you need
825 * scheduler in the thread. Note that, once the thread finishes the
826 * returned SilcSchedule becomes invalid.
828 * In other times this returns the SilcSchedule pointer that was given
829 * to silc_fsm_alloc or silc_fsm_init.
832 SilcSchedule silc_fsm_get_schedule(void *fsm);
834 /****f* silcutil/SilcFSMAPI/silc_fsm_get_machine
838 * SilcFSM silc_fsm_get_machine(SilcFSMThread thread);
842 * Returns the machine from the FSM thread indicated by `thread'.
845 SilcFSM silc_fsm_get_machine(SilcFSMThread thread);
847 /****f* silcutil/SilcFSMAPI/silc_fsm_is_started
851 * SilcBool silc_fsm_is_started(void *fsm);
855 * Returns TRUE if the machine or thread `fsm' has been started and has
856 * not been finished yet. This function is used with both SilcFSM and
857 * SilcFSMThread contexts.
860 SilcBool silc_fsm_is_started(void *fsm);
864 /****s* silcutil/SilcFSMAPI/SilcFSMEvent
868 * typedef struct SilcFSMEventObject *SilcFSMEvent;
872 * The FSM event context allocated with silc_fsm_event_alloc. The
873 * caller must free it with silc_fsm_event_free. It is also possible
874 * to use pre-allocated SilcFSMEventStruct instead of SilcFSMEvent context.
877 typedef struct SilcFSMEventObject *SilcFSMEvent;
879 /****s* silcutil/SilcFSMAPI/SilcFSMEventStruct
883 * typedef struct SilcFSMEventObject SilcFSMEventStruct;
887 * The FSM event context that can be used as pre-allocated context.
888 * It is initialized with silc_fsm_event_init. It need not be
892 typedef struct SilcFSMEventObject SilcFSMEventStruct;
894 /****f* silcutil/SilcFSMAPI/silc_fsm_event_alloc
898 * SilcFSMEvent silc_fsm_event_alloc(SilcFSM fsm);
902 * Allocates asynchronous FSM event. FSM events are asynchronous events
903 * that can be waited and signalled. They can be used as condition
904 * variables and signallers. They can be used for example to wait that
905 * some event happens, some thread moves to a specific state or similar.
906 * The FSM Events may also be used in FSM threads that are executed in
907 * real system threads. It is safe to wait and signal the event from
910 * Use the macros SILC_FSM_EVENT_WAIT and SILC_FSM_EVENT_TIMEDWAIT to wait
911 * for the event. Use the SILC_FSM_EVENT_SIGNAL macro to signal all the
915 SilcFSMEvent silc_fsm_event_alloc(SilcFSM fsm);
917 /****f* silcutil/SilcFSMAPI/silc_fsm_event_init
921 * void silc_fsm_event_init(SilcFSMEvent event, SilcFSM fsm);
925 * Initializes a pre-allocates FSM event context. This call is
926 * equivalent to silc_fsm_event_alloc except this use the pre-allocated
927 * context. This fuction does not allocate any memory.
930 void silc_fsm_event_init(SilcFSMEvent event, SilcFSM fsm);
932 /****f* silcutil/SilcFSMAPI/silc_fsm_event_free
936 * void silc_fsm_event_free(SilcFSMEvent event);
940 * Free the event allocated by silc_fsm_event_alloc function.
943 void silc_fsm_event_free(SilcFSMEvent event);
945 /****d* silcutil/SilcFSMAPI/SILC_FSM_EVENT_WAIT
949 * SILC_FSM_EVENT_WAIT(event)
953 * Macro used to wait for the `event' to be signalled. The machine
954 * or thread will be suspended while it is waiting for the event.
955 * This macro can only be used in FSM state functions. When the
956 * event is signalled the FSM will re-enter the current state (or
957 * state that was set with silc_fsm_next before waiting).
961 * // Signalling example
962 * ctx->async_event = silc_fsm_event_alloc(fsm);
965 * SILC_FSM_STATE(silc_foo_state)
969 * // Wait here for async call to complete
970 * SILC_FSM_EVENT_WAIT(ctx->async_event);
972 * // Async call completed
973 * if (ctx->async_success == FALSE)
979 #define SILC_FSM_EVENT_WAIT(event) \
981 if (silc_fsm_event_wait(event, fsm) == 0) \
982 return SILC_FSM_WAIT; \
985 /****d* silcutil/SilcFSMAPI/SILC_FSM_EVENT_TIMEDWAIT
989 * SILC_FSM_EVENT_TIMEDWAIT(event, seconds, useconds, timedout)
993 * Macro used to wait for the `event' to be signalled, or until
994 * the timeout specified by `seconds' and `useconds' has elapsed. If
995 * the timeout occurs before the event is signalled, the machine
996 * will wakeup. The `timedout' is SilcBool pointer and if it is
997 * non-NULL indication of whether timeout occurred or not is saved to
998 * the pointer. This macro can only be used in FSM state functions.
999 * When the event is signalled or timedout the FSM will re-enter
1000 * the current state (or state that was set with silc_fsm_next before
1005 * SILC_FSM_STATE(silc_foo_state)
1007 * SilcBool timedout;
1010 * // Wait here for async call to complete, or 10 seconds for timeout
1011 * SILC_FSM_EVENT_TIMEDWAIT(ctx->async_event, 10, 0, &timedout);
1013 * // See if timeout occurred
1014 * if (timedout == TRUE)
1017 * // Async call completed
1018 * if (ctx->async_success == FALSE)
1024 #define SILC_FSM_EVENT_TIMEDWAIT(event, seconds, useconds, ret_to) \
1026 if (silc_fsm_event_timedwait(event, fsm, seconds, useconds, ret_to) == 0) \
1027 return SILC_FSM_WAIT; \
1030 /****f* silcutil/SilcFSMAPI/SILC_FSM_EVENT_SIGNAL
1034 * SILC_FSM_EVENT_SIGNAL(event)
1038 * Signals the `event' and awakens everybody that are waiting for this
1039 * event. This macro never blocks. It can be safely called at any place
1040 * in state function and in asynchronous callbacks or other functions.
1044 * SILC_FSM_STATE(silc_foo_async_completion)
1048 * // Notify all waiters
1049 * ctx->async_success = TRUE;
1050 * SILC_FSM_EVENT_SIGNAL(ctx->async_event);
1055 #define SILC_FSM_EVENT_SIGNAL(event) \
1057 silc_fsm_event_signal(event); \
1060 #include "silcfsm_i.h"
1062 #endif /* SILCFSM_H */