5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2005, 2006 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 FSM semaphores. 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 semaphores because of their versatility. The FSM
32 * semaphores can be used as a conditional variables and signallers, and
33 * also as a mutual exclusion locks to protect critical sections. The FSM
34 * semaphores can safely be used to synchronize also FSM threads that are
35 * executed in real system threads. This makes SILC FSM very effective
36 * tool to implement complex machines whether they are executed in single
37 * thread or in multiple threads.
44 /****s* silcutil/SilcFSMAPI/SilcFSM
48 * typedef struct SilcFSMObject *SilcFSM;
52 * The actual FSM context and is allocated with silc_fsm_alloc and
53 * given as argument to all silc_fsm_* functions. It is freed by
54 * silc_fsm_free function. It is also possible to use pre-allocated
55 * FSM context by using SilcFSMStruct instead of SilcFSM.
58 typedef struct SilcFSMObject *SilcFSM;
60 /****s* silcutil/SilcFSMAPI/SilcFSMStruct
64 * typedef struct SilcFSMObject SilcFSMStruct;
68 * The actual FSM context and can be used as pre-allocated FSM context,
69 * instead of SilcFSM context. This context is initialized with the
70 * silc_fsm_init function. It need not be uninitialized.
73 typedef struct SilcFSMObject SilcFSMStruct;
75 /****s* silcutil/SilcFSMAPI/SilcFSMThread
79 * typedef struct SilcFSMObject *SilcFSMThread;
83 * FSM thread context. The SILC FSM supports threads, virtual machine
84 * threads (inside FSM) and actual real system threads if platorm
85 * supports them. In a complex machine certain complex operations may
86 * be desired to execute in a thread. The SilcFSMThread is allocated
87 * by silc_fsm_thread_alloc and feed by silc_fsm_free. It is also
88 * possible to use pre-allocated thread by using SilcFSMThreadStruct
89 * instead of SilcFSMThread.
92 typedef struct SilcFSMObject *SilcFSMThread;
94 /****s* silcutil/SilcFSMAPI/SilcFSM
98 * typedef struct SilcFSMObject SilcFSMThreadStruct;
102 * FSM thread context and can be used as a pre-allocated FSM thread context,
103 * instead of SilcFSMThread context. This context is initialized with the
104 * silc_fsm_thread_init function. It need not be uninitialized.
107 typedef struct SilcFSMObject SilcFSMThreadStruct;
109 /****d* silcutil/SilcFSMAPI/SilcFSMStatus
113 * typedef enum { ... } SilcFSMStatus;
117 * Status values that the FSM state functions return.
122 SILC_FSM_CONTINUE, /* Continue immediately to next state. */
123 SILC_FSM_WAIT, /* Wait for some async call or timeout */
124 SILC_FSM_FINISH, /* Finish state machine and call destructor
129 /****f* silcutil/SilcFSMAPI/SilcFSMDestructor
133 * typedef void (*SilcFSMDestructor)(SilcFSM fsm, void *fsm_context,
134 * void *destructor_context);
138 * The destructor callback that was set in silc_fsm_alloc or in
139 * silc_fsm_init function. It will be called when a state function
140 * returns SILC_FSM_FINISH. This function will be called through
141 * the scheduler; it will not be called immediately after the state
142 * function returns SILC_FSM_FINISH, but will be called later.
143 * The `fsm' may be freed in this function.
146 typedef void (*SilcFSMDestructor)(SilcFSM fsm, void *fsm_context,
147 void *destructor_context);
149 /****f* silcutil/SilcFSMAPI/SilcFSMThreadDestructor
153 * typedef void (*SilcFSMThreadDestructor)(SilcFSMThread thread,
154 * void *thread_context,
155 * void *destructor_context);
159 * The destructor callback that was set in silc_fsm_thread_alloc or in
160 * silc_fsm_thread_init function. It will be called when a state function
161 * returns SILC_FSM_FINISH. This function will be called through
162 * the scheduler; it will not be called immediately after the state
163 * function returns SILC_FSM_FINISH, but will be called later. The
164 * `thread' may be freed in this function.
168 * Even if the `thread' was executed in real system thread, this callback
169 * is always received in the main machine thread, not in the created
173 typedef void (*SilcFSMThreadDestructor)(SilcFSMThread thread,
174 void *thread_context,
175 void *destructor_context);
177 /****d* silcutil/SilcFSMAPI/SILC_FSM_STATE
181 * #define SILC_FSM_STATE(name)
185 * This macro is used to declare a FSM state function. The `fsm' is
186 * the SilcFSM or SilcFSMThread context, the `fsm_context' is the context
187 * given as argument to silc_fsm_alloc, silc_fsm_init, silc_fsm_thread_init,
188 * or silc_fsm_thread_alloc function. The `state_context' is the optional
189 * state specific context set with silc_fsm_set_state_context function.
193 #define SILC_FSM_STATE(name) \
194 SilcFSMStatus name(struct SilcFSMObject *fsm, void *fsm_context, \
198 /* State function callback */
199 typedef SilcFSMStatus (*SilcFSMStateCallback)(struct SilcFSMObject *fsm,
201 void *state_context);
203 /****d* silcutil/SilcFSMAPI/SILC_FSM_CALL
207 * SILC_FSM_CALL(function)
211 * Macro used to call asynchronous calls from state function. If the
212 * call is not really asynchronous then this will cause the machine to
213 * directly proceed to next state. If the call is truly asynchronous
214 * then this will set the machine to wait state. The silc_fsm_next
215 * must be called before this macro, so that the next state is set.
219 * The state function returns in this macro.
224 * silc_fsm_next(fsm, some_next_state);
225 * SILC_FSM_CALL(silc_some_async_call(server, some_callback, context));
227 * // More complex example
228 * silc_fsm_next(fsm, some_next_state);
229 * SILC_FSM_CALL((some_context->operation =
230 * silc_some_async_call(server, some_callback, context)));
233 #define SILC_FSM_CALL(function) \
235 assert(!silc_fsm_set_call(fsm, TRUE)); \
237 if (!silc_fsm_set_call(fsm, FALSE)) \
238 return SILC_FSM_CONTINUE; \
239 return SILC_FSM_WAIT; \
242 /****d* silcutil/SilcFSMAPI/SILC_FSM_CALL_CONTINUE
246 * SILC_FSM_CALL_CONTINUE(fsm)
250 * Macro used to proceed after asynchornous call. This is called in the
251 * callback of the asynchronous call to continue in the state machine.
255 * void some_callback(void *context) {
256 * SilcFSM fsm = context;
258 * // Continue to the next state
259 * SILC_FSM_CALL_CONTINUE(fsm);
263 #define SILC_FSM_CALL_CONTINUE(fsm) \
265 if (!silc_fsm_set_call(fsm, FALSE)) \
266 silc_fsm_continue(fsm); \
269 /****d* silcutil/SilcFSMAPI/SILC_FSM_CALL_CONTINUE_SYNC
273 * SILC_FSM_CALL_CONTINUE_SYNC(fsm)
277 * Macro used to proceed after asynchornous call. This is called in the
278 * callback of the asynchronous call to continue in the state machine.
279 * This continues to the next state synchronously, not through the
284 * void some_callback(void *context) {
285 * SilcFSM fsm = context;
287 * // Continue to the next state immediately
288 * SILC_FSM_CALL_CONTINUE_SYNC(fsm);
292 #define SILC_FSM_CALL_CONTINUE_SYNC(fsm) \
294 if (!silc_fsm_set_call(fsm, FALSE)) \
295 silc_fsm_continue_sync(fsm); \
298 /****d* silcutil/SilcFSMAPI/SILC_FSM_THREAD_WAIT
302 * SILC_FSM_THREAD_WAIT(thread)
306 * Macro used to wait for the `thread' to terminate. The machine or
307 * thread will be suspended while it is waiting for the thread to
312 * The state function returns in this macro.
314 * This macro is the only way to safely make sure that the thread has
315 * terminated by the time FSM continues from the waiting state. Using
316 * semaphores to signal from the thread before SILC_FSM_FINISH is returned
317 * works with normal FSM threads, but especially with real system threads
318 * it does not guarantee that the FSM won't continue before the thread has
319 * actually terminated. Usually this is not a problem, but it can be a
320 * problem if the FSM is waiting to be freed. In this case using this
321 * macro is strongly recommended.
324 #define SILC_FSM_THREAD_WAIT(thread) \
326 silc_fsm_thread_wait(fsm, thread); \
327 return SILC_FSM_WAIT; \
330 /****f* silcutil/SilcFSMAPI/silc_fsm_alloc
334 * SilcFSM silc_fsm_alloc(void *fsm_context,
335 * SilcFSMDestructor destructor,
336 * void *destructor_context,
337 * SilcSchedule schedule);
341 * Allocates SILC Finite State Machine context. The `destructor' with
342 * `destructor_context' will be called when the machines finishes. The
343 * caller must free the returned context with silc_fsm_free. The
344 * `fsm_context' is delivered to every FSM state function. The `schedule'
345 * is the caller's scheduler and the FSM will be run in the scheduler.
349 * SilcAsyncOperation silc_async_call(Callback callback, void *cb_context)
351 * SilcAsyncOperation op;
355 * // Allocate async operation so that caller can control us, like abort
356 * op = silc_async_alloc(silc_async_call_abort, NULL, ourcontext);
359 * fsm = silc_fsm_alloc(ourcontext, fsm_destructor, ourcontext,
361 * silc_fsm_start(fsm, first_state);
364 * // Return async operation for upper layer
369 SilcFSM silc_fsm_alloc(void *fsm_context,
370 SilcFSMDestructor destructor,
371 void *destructor_context,
372 SilcSchedule schedule);
374 /****f* silcutil/SilcFSMAPI/silc_fsm_init
378 * SilcBool silc_fsm_init(SilcFSM fsm,
380 * SilcFSMDestructor destructor,
381 * void *destructor_context,
382 * SilcSchedule schedule);
386 * Initializes a pre-allocated SilcFSM context. This call is equivalent
387 * to silc_fsm_alloc except that this takes the pre-allocated context
388 * as argument. The silc_fsm_free must not be called if this was called.
389 * Returns TRUE if the initialization is Ok or FALSE if error occurred.
390 * This function does not allocate any memory. The `schedule' is the
391 * caller's scheduler and the FSM will be run in the scheduler.
397 * silc_fsm_init(&fsm, application, fsm_destructor, application, schedule);
398 * silc_fsm_start(&fsm, first_state);
401 SilcBool silc_fsm_init(SilcFSM fsm,
403 SilcFSMDestructor destructor,
404 void *destructor_context,
405 SilcSchedule schedule);
407 /****f* silcutil/SilcFSMAPI/silc_fsm_thread_alloc
411 * SilcFSMThread silc_fsm_thread_alloc(SilcFSM fsm,
412 * void *thread_context,
413 * SilcFSMThreadDestructor destructor,
414 * void *destructor_context,
415 * SilcBool real_thread);
419 * Allocates FSM thread context. The thread will be executed in the
420 * FSM machine indicated by `fsm'. The caller must free the returned
421 * thread context with silc_fsm_free. If the 'real_thread' is TRUE
422 * then the thread will actually be executed in real thread, if platform
423 * supports them. The `thread_context' is delivered to every state
424 * function in the thread.
428 * Note the limitations on using `real_thread' boolean to indicate running
429 * the FSM thread in a real system thread:
431 * If the system does not support threads, then this function will revert
432 * back to normal FSM threads.
434 * If the `real_thread' is TRUE then FSM will allocate new SilcSchedule
435 * for the FSM thread. This is done because the SilcSchedule that the
436 * `fsm' use cannot be used in the thread. This is limitation in the
437 * SilcSchedule implementation. If you need scheduler in the real thread
438 * it is strongly recommended that you use the SilcSchedule that is
439 * allocated for the thread. You can retrieve the SilcSchedule from the
440 * thread using silc_fsm_get_schedule function. Note that, the allocated
441 * SilcSchedule will become invalid after the thread finishes.
443 * You may still however use the original SilcSchedule if you wish. In
444 * this case note its limitation: you may only add and/or remove tasks,
445 * tasks cannot be executed in the thread. You will need to deliver the
446 * original SilcSchedule to the thread in the `thread_context' if you wish
449 * If `real_thread' is FALSE then no limitations on what can be run in
450 * the thread exist. In this case silc_fsm_get_schedule will return
451 * the SilcSchedule that was originally given to silc_fsm_alloc or
456 * SILC_FSM_STATE(silc_foo_state)
458 * SilcFSMThread thread;
461 * // Execute the route lookup in thread
462 * thread = silc_fsm_thread_alloc(fsm, fsm_context, NULL, NULL, FALSE);
463 * silc_fsm_start(thread, silc_route_lookup_start);
465 * // Wait here for the thread to terminate. Set the state where to go
466 * // after the thread has terminated.
467 * silc_fsm_next(fsm, silc_foo_route_lookup_finished);
468 * SILC_FSM_THREAD_WAIT(thread);
472 SilcFSMThread silc_fsm_thread_alloc(SilcFSM fsm,
473 void *thread_context,
474 SilcFSMThreadDestructor destructor,
475 void *destructor_context,
476 SilcBool real_thread);
478 /****f* silcutil/SilcFSMAPI/silc_fsm_thread_init
482 * void silc_fsm_thread_init(SilcFSMThread thread,
484 * void *thread_context,
485 * SilcFSMThreadDestructor destructor,
486 * void *destructor_context,
487 * SilcBool real_thread);
491 * Initializes a pre-allocated SilcFSMThread context. This call is
492 * equivalent to silc_fsm_thread_alloc except that this takes the
493 * pre-allocated context as argument. The silc_fsm_free must not be
494 * called if this was called. If the `real_thread' is TRUE then the
495 * thread will actually be executed in real thread, if platform supports
500 * See the notes from the silc_fsm_thread_alloc.
504 * SilcFSMThreadStruct thread;
506 * silc_fsm_thread_init(&thread, fsm, application, NULL, NULL, FALSE);
507 * silc_fsm_start(&thread, first_state);
510 void silc_fsm_thread_init(SilcFSMThread thread,
512 void *thread_context,
513 SilcFSMThreadDestructor destructor,
514 void *destructor_context,
515 SilcBool real_thread);
517 /****f* silcutil/SilcFSMAPI/silc_fsm_free
521 * void silc_fsm_free(void *fsm);
525 * Free the SILC FSM context that was allocated with silc_fsm_alloc,
526 * or free the SILC FSM thread context that was allocated with
527 * silc_fsm_thread_alloc. This function is used with both SilcFSM
528 * and SilcFSMThread contexts.
532 * When freeing FSM, it must not have any active threads.
535 void silc_fsm_free(void *fsm);
537 /****f* silcutil/SilcFSMAPI/silc_fsm_start
541 * void silc_fsm_start(void *fsm, SilcFSMStateCallback start_state);
545 * This function must be called after the SILC FSM context was created.
546 * This actually starts the state machine. Note that, the machine is
547 * started later after this function returns. The `start_state' is the
548 * state where the machine or thread is started. This function is used
549 * with both SilcFSM and SilcFSMThread contexts.
555 * fsm = silc_fsm_alloc(context, destructor, context, schedule);
556 * silc_fsm_start(fsm, first_state);
559 void silc_fsm_start(void *fsm, SilcFSMStateCallback start_state);
561 /****f* silcutil/SilcFSMAPI/silc_fsm_start_sync
565 * void silc_fsm_start_sync(void *fsm, SilcFSMStateCallback start_state);
569 * This function is same as silc_fsm_start, except that the FSM will
570 * be started immediately inside this function. After this function
571 * returns the `start_state' has already been executed. If the machine
572 * is completely synchronous (no waiting used in the machine) then
573 * the machine will have finished once this function returns. Also
574 * note that if the machine is completely synchronous the destructor
575 * will also be called from inside this function. This function is used
576 * with both SilcFSM and SilcFSMThread contexts.
579 void silc_fsm_start_sync(void *fsm, SilcFSMStateCallback start_state);
581 /****f* silcutil/SilcFSMAPI/silc_fsm_next
585 * void silc_fsm_next(void *fsm, SilcFSMStateCallback next_state);
589 * Set the next state to be executed. If the state function that
590 * call this function returns SILC_FSM_CONTINUE, the `next_state'
591 * will be executed immediately. This function must always be used
592 * to set the next state in the machine or thread. This function is
593 * used with both SilcFSM and SilcFSMThread contexts.
597 * // Move to next state
598 * silc_fsm_next(fsm, next_state);
599 * return SILC_FSM_CONTINUE;
602 void silc_fsm_next(void *fsm, SilcFSMStateCallback next_state);
604 /****f* silcutil/SilcFSMAPI/silc_fsm_next_later
608 * void silc_fsm_next_later(void *fsm, SilcFSMStateCallback next_state,
609 * SilcUInt32 seconds, SilcUInt32 useconds);
613 * Set the next state to be executed later, at the specified time.
614 * The SILC_FSM_WAIT must be returned in the state function if this
615 * function is called. If any other state is returned machine operation
616 * is undefined. The machine or thread will move to `next_state' after
617 * the specified timeout. This function is used with both SilcFSM and
618 * SilcFSMThread contexts.
622 * If both `seconds' and `useconds' are 0, the effect is same as calling
623 * silc_fsm_next function, and SILC_FSM_CONTINUE must be returned.
627 * // Move to next state after 10 seconds
628 * silc_fsm_next_later(fsm, next_state, 10, 0);
629 * return SILC_FSM_WAIT;
632 void silc_fsm_next_later(void *fsm, SilcFSMStateCallback next_state,
633 SilcUInt32 seconds, SilcUInt32 useconds);
635 /****f* silcutil/SilcFSMAPI/silc_fsm_continue
639 * void silc_fsm_continue(void *fsm);
643 * Continues in the state machine from a SILC_FSM_WAIT state. This can
644 * be called from outside waiting FSM to continue to the next state.
645 * This function can be used instead of SILC_FSM_CALL_CONTINUE macro
646 * in case the SILC_FSM_CALL was not used. This must not be used if
647 * SILC_FSM_CALL was used. This function is used with both SilcFSM and
648 * SilcFSMThread contexts.
651 void silc_fsm_continue(void *fsm);
653 /****f* silcutil/SilcFSMAPI/silc_fsm_continue_sync
657 * void silc_fsm_continue_sync(void *fsm);
661 * Continues immediately in the state machine from a SILC_FSM_WAIT state.
662 * This can be called from outside waiting FSM to immediately continue to
663 * the next state. This function can be used instead of the
664 * SILC_FSM_CALL_CONTINUE_SYNC macro in case the SILC_FSM_CALL was not used.
665 * This must not be used if SILC_FSM_CALL was used. This function is used
666 * with both SilcFSM and SilcFSMThread contexts.
669 void silc_fsm_continue_sync(void *fsm);
671 /****f* silcutil/SilcFSMAPI/silc_fsm_set_context
675 * void silc_fsm_set_context(void *fsm, void *fsm_context);
679 * Set new context for the `fsm'. This function can be used to change
680 * the context inside the `fsm', if needed. This function is used with
681 * both SilcFSM and SilcFSMThread contexts. The context is the
682 * `fsm_context' in the state function (SILC_FSM_STATE).
685 void silc_fsm_set_context(void *fsm, void *fsm_context);
687 /****f* silcutil/SilcFSMAPI/silc_fsm_get_context
691 * void *silc_fsm_get_context(void *fsm);
695 * Returns the context associated with the `fsm'. It is the context that
696 * was given to silc_fsm_alloc, silc_fsm_init, silc_fsm_thread_alloc or
697 * silc_fsm_thread_init. This function is used with both SilcFSM and
698 * SilcFSMThread contexts.
701 void *silc_fsm_get_context(void *fsm);
703 /****f* silcutil/SilcFSMAPI/silc_fsm_set_state_context
707 * void silc_fsm_set_state_context(void *fsm, void *state_context);
711 * Set's a state specific context for the `fsm'. This function can be
712 * used to change the state context inside the `fsm', if needed. This
713 * function is used with both SilcFSM and SilcFSMThread contexts. The
714 * context is the `state_context' in the state function (SILC_FSM_STATE).
717 void silc_fsm_set_state_context(void *fsm, void *state_context);
719 /****f* silcutil/SilcFSMAPI/silc_fsm_get_state_context
723 * void *silc_fsm_get_state_context(void *fsm);
727 * Returns the state context associated with the `fsm'. It is the context
728 * that was set with silc_fsm_set_state_context function. This function
729 * is used with both SilcFSM and SilcFSMThread contexts.
732 void *silc_fsm_get_state_context(void *fsm);
734 /****f* silcutil/SilcFSMAPI/silc_fsm_get_schedule
738 * SilcSchedule silc_fsm_get_schedule(void *fsm);
742 * Returns the SilcSchedule that has been associated with the `fsm'.
743 * If caller needs scheduler it may retrieve it with this function. This
744 * function is used with both SilcFSM and SilcFSMThread contexts.
746 * If the `fsm' is thread and real system threads are being used, and this
747 * is called from the thread, it will return the SilcSchedule that was
748 * allocated by the FSM for the thread. It is strongly recommended to
749 * use this SilcSchedule if you are using real threads, and you need
750 * scheduler in the thread. Note that, once the thread finishes the
751 * returned SilcSchedule becomes invalid.
753 * In other times this returns the SilcSchedule pointer that was given
754 * to silc_fsm_alloc or silc_fsm_init.
757 SilcSchedule silc_fsm_get_schedule(void *fsm);
759 /****f* silcutil/SilcFSMAPI/silc_fsm_get_machine
763 * SilcFSM silc_fsm_get_machine(SilcFSMThread thread);
767 * Returns the machine from the FSM thread indicated by `thread'.
770 SilcFSM silc_fsm_get_machine(SilcFSMThread thread);
775 /****s* silcutil/SilcFSMAPI/SilcFSMSema
779 * typedef struct SilcFSMSemaObject *SilcFSMSema;
783 * The FSM semaphore context allocated with silc_fsm_sema_alloc. The
784 * caller must free it with silc_fsm_sema_free. It is also possible
785 * to use pre-allocated SilcFSMSemaStruct instead of SilcFSMSema context.
788 typedef struct SilcFSMSemaObject *SilcFSMSema;
790 /****s* silcutil/SilcFSMAPI/SilcFSMSemaStruct
794 * typedef struct SilcFSMSemaObject SilcFSMSemaStruct;
798 * The FSM semaphore context that can be used as pre-allocated context.
799 * It is initialized with silc_fsm_sema_init. It need not be
803 typedef struct SilcFSMSemaObject SilcFSMSemaStruct;
805 /****f* silcutil/SilcFSMAPI/silc_fsm_sema_alloc
809 * SilcFSMSema silc_fsm_sema_alloc(SilcFSM fsm, SilcUInt32 value);
813 * Allocates FSM semaphore with initial value of `value'. Semaphores are
814 * counters for resources shared between machine and threads. Semaphores
815 * can be waited until the semaphore value is non-zero. The FSM will be
816 * suspended when waiting for semaphore. When the semaphore is incremented
817 * all that are waiting for the semaphore will be signalled and awaken.
819 * Semaphores can be used to wait for example when thread terminates, or
820 * when thread moves into a specific state, or to protect critical
821 * sections. The FSM semaphores can be used also in FSM threads that are
822 * executed in real system threads.
824 * Use the macros SILC_FSM_SEMA_WAIT and SILC_FSM_SEMA_TIMEDWAIT to wait
825 * for semaphore. Use the SILC_FSM_SEMA_POST macro to increment the
826 * counter and wake up all waiters.
829 SilcFSMSema silc_fsm_sema_alloc(SilcFSM fsm, SilcUInt32 value);
831 /****f* silcutil/SilcFSMAPI/silc_fsm_sema_init
835 * void silc_fsm_sema_init(SilcFSMSema sema, SilcFSM fsm, SilcUInt32 value);
839 * Initializes a pre-allocates semaphore context. This call is
840 * equivalent to silc_fsm_sema_alloc except this use the pre-allocated
841 * context. This fuction does not allocate any memory.
844 void silc_fsm_sema_init(SilcFSMSema sema, SilcFSM fsm, SilcUInt32 value);
846 /****f* silcutil/SilcFSMAPI/silc_fsm_sema_free
850 * void silc_fsm_sema_free(SilcFSMSema sema);
854 * Free the semaphore allocated by silc_fsm_sema_alloc function.
857 void silc_fsm_sema_free(SilcFSMSema sema);
859 /****d* silcutil/SilcFSMAPI/SILC_FSM_SEMA_WAIT
863 * SILC_FSM_SEMA_WAIT(semaphore)
867 * Macro used to wait for the `semaphore' to become non-zero. The
868 * machine will be suspended while it is waiting for the semaphore.
869 * This macro can only be used in FSM state functions. When the
870 * semaphore is signalled the FSM will re-enter the current state (or
871 * state that was set with silc_fsm_next before waiting).
875 * // Signalling example
876 * ctx->async_sema = silc_fsm_sema_alloc(fsm, 0);
879 * SILC_FSM_STATE(silc_foo_state)
883 * // Wait here for async call to complete
884 * SILC_FSM_SEMA_WAIT(ctx->async_sema);
886 * // Async call completed
887 * if (ctx->async_success == FALSE)
892 * // Mutual exclusion example
893 * ctx->lock = silc_fsm_sema_alloc(fsm, 1);
896 * SILC_FSM_STATE(silc_foo_state)
899 * SILC_FSM_SEMA_WAIT(ctx->lock);
900 * very critical stuff...
901 * SILC_FSM_SEMA_POST(ctx->lock);
906 #define SILC_FSM_SEMA_WAIT(sema) \
908 if (silc_fsm_sema_wait(sema, fsm) == 0) \
909 return SILC_FSM_WAIT; \
912 /****d* silcutil/SilcFSMAPI/SILC_FSM_SEMA_TIMEDWAIT
916 * SILC_FSM_SEMA_TIMEDWAIT(semaphore, seconds, useconds, timedout)
920 * Macro used to wait for the `semaphore' to become non-zero, or until
921 * the timeout specified by `seconds' and `useconds' has elapsed. If
922 * the timeout occurs before the semaphore becomes non-zero, the machine
923 * will wakeup. The `timedout' is SilcBool pointer and if it is
924 * non-NULL indication of whether timeout occurred or not is saved to
925 * the pointer. This macro can only be used in FSM state functions.
926 * When the semaphore is signalled or timedout the FSM will re-enter
927 * the current state (or state that was set with silc_fsm_next before
932 * SILC_FSM_STATE(silc_foo_state)
937 * // Wait here for async call to complete, or 10 seconds for timeout
938 * SILC_FSM_SEMA_TIMEDWAIT(ctx->async_sema, 10, 0, &timedout);
940 * // See if timeout occurred
941 * if (timedout == TRUE)
944 * // Async call completed
945 * if (ctx->async_success == FALSE)
951 #define SILC_FSM_SEMA_TIMEDWAIT(sema, seconds, useconds, ret_to) \
953 if (silc_fsm_sema_timedwait(sema, fsm, seconds, useconds, ret_to) == 0) \
954 return SILC_FSM_WAIT; \
957 /****f* silcutil/SilcFSMAPI/SILC_FSM_SEMA_POST
961 * SILC_FSM_SEMA_POST(semaphore)
965 * Increases the semaphore counter and awakens everybody that are
966 * waiting for this semaphore. This macro never blocks. It can be
967 * safely called at any place in state function and in asynchronous
968 * callbacks or other functions.
972 * SILC_FSM_STATE(silc_foo_async_completion)
976 * // Notify all waiters
977 * ctx->async_success = TRUE;
978 * SILC_FSM_SEMA_POST(ctx->async_sema);
983 #define SILC_FSM_SEMA_POST(sema) \
985 silc_fsm_sema_post(sema); \
988 #include "silcfsm_i.h"
990 #endif /* SILCFSM_H */