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 or uninitialized 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 or uninitialized 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 or uninitialized. In this
321 * case using this 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. Returns TRUE if the initialization is Ok
495 * or FALSE if error occurred. If the `real_thread' is TRUE then the
496 * thread will actually be executed in real thread, if platform supports
501 * See the notes from the silc_fsm_thread_alloc.
505 * SilcFSMThreadStruct thread;
507 * silc_fsm_thread_init(&thread, fsm, application, NULL, NULL, FALSE);
508 * silc_fsm_start(&thread, first_state);
511 void silc_fsm_thread_init(SilcFSMThread thread,
513 void *thread_context,
514 SilcFSMThreadDestructor destructor,
515 void *destructor_context,
516 SilcBool real_thread);
518 /****f* silcutil/SilcFSMAPI/silc_fsm_free
522 * void silc_fsm_free(void *fsm);
526 * Free the SILC FSM context that was allocated with silc_fsm_alloc,
527 * or free the SILC FSM thread context that was allocated with
528 * silc_fsm_thread_alloc. This function is used with both SilcFSM
529 * and SilcFSMThread contexts.
533 * When freeing FSM, it must not have any active threads.
536 void silc_fsm_free(void *fsm);
538 /****f* silcutil/SilcFSMAPI/silc_fsm_start
542 * void silc_fsm_start(void *fsm, SilcFSMStateCallback start_state);
546 * This function must be called after the SILC FSM context was created.
547 * This actually starts the state machine. Note that, the machine is
548 * started later after this function returns. The `start_state' is the
549 * state where the machine or thread is started. This function is used
550 * with both SilcFSM and SilcFSMThread contexts.
556 * fsm = silc_fsm_alloc(context, destructor, context, schedule);
557 * silc_fsm_start(fsm, first_state);
560 void silc_fsm_start(void *fsm, SilcFSMStateCallback start_state);
562 /****f* silcutil/SilcFSMAPI/silc_fsm_start_sync
566 * void silc_fsm_start_sync(void *fsm, SilcFSMStateCallback start_state);
570 * This function is same as silc_fsm_start, except that the FSM will
571 * be started immediately inside this function. After this function
572 * returns the `start_state' has already been executed. If the machine
573 * is completely synchronous (no waiting used in the machine) then
574 * the machine will have finished once this function returns. Also
575 * note that if the machine is completely synchronous the destructor
576 * will also be called from inside this function. This function is used
577 * with both SilcFSM and SilcFSMThread contexts.
580 void silc_fsm_start_sync(void *fsm, SilcFSMStateCallback start_state);
582 /****f* silcutil/SilcFSMAPI/silc_fsm_next
586 * void silc_fsm_next(void *fsm, SilcFSMStateCallback next_state);
590 * Set the next state to be executed. If the state function that
591 * call this function returns SILC_FSM_CONTINUE, the `next_state'
592 * will be executed immediately. This function must always be used
593 * to set the next state in the machine or thread. This function is
594 * used with both SilcFSM and SilcFSMThread contexts.
598 * // Move to next state
599 * silc_fsm_next(fsm, next_state);
600 * return SILC_FSM_CONTINUE;
603 void silc_fsm_next(void *fsm, SilcFSMStateCallback next_state);
605 /****f* silcutil/SilcFSMAPI/silc_fsm_next_later
609 * void silc_fsm_next_later(void *fsm, SilcFSMStateCallback next_state,
610 * SilcUInt32 seconds, SilcUInt32 useconds);
614 * Set the next state to be executed later, at the specified time.
615 * The SILC_FSM_WAIT must be returned in the state function if this
616 * function is called. If any other state is returned machine operation
617 * is undefined. The machine or thread will move to `next_state' after
618 * the specified timeout. This function is used with both SilcFSM and
619 * SilcFSMThread contexts.
623 * If both `seconds' and `useconds' are 0, the effect is same as calling
624 * silc_fsm_next function, and SILC_FSM_CONTINUE must be returned.
628 * // Move to next state after 10 seconds
629 * silc_fsm_next_later(fsm, next_state, 10, 0);
630 * return SILC_FSM_WAIT;
633 void silc_fsm_next_later(void *fsm, SilcFSMStateCallback next_state,
634 SilcUInt32 seconds, SilcUInt32 useconds);
636 /****f* silcutil/SilcFSMAPI/silc_fsm_continue
640 * void silc_fsm_continue(void *fsm);
644 * Continues in the state machine from a SILC_FSM_WAIT state. This can
645 * be called from outside waiting FSM to continue to the next state.
646 * This function can be used instead of SILC_FSM_CALL_CONTINUE macro
647 * in case the SILC_FSM_CALL was not used. This must not be used if
648 * SILC_FSM_CALL was used. This function is used with both SilcFSM and
649 * SilcFSMThread contexts.
652 void silc_fsm_continue(void *fsm);
654 /****f* silcutil/SilcFSMAPI/silc_fsm_continue_sync
658 * void silc_fsm_continue_sync(void *fsm);
662 * Continues immediately in the state machine from a SILC_FSM_WAIT state.
663 * This can be called from outside waiting FSM to immediately continue to
664 * the next state. This function can be used instead of the
665 * SILC_FSM_CALL_CONTINUE_SYNC macro in case the SILC_FSM_CALL was not used.
666 * This must not be used if SILC_FSM_CALL was used. This function is used
667 * with both SilcFSM and SilcFSMThread contexts.
670 void silc_fsm_continue_sync(void *fsm);
672 /****f* silcutil/SilcFSMAPI/silc_fsm_set_context
676 * void silc_fsm_set_context(void *fsm, void *fsm_context);
680 * Set new context for the `fsm'. This function can be used to change
681 * the context inside the `fsm', if needed. This function is used with
682 * both SilcFSM and SilcFSMThread contexts. The context is the
683 * `fsm_context' in the state function (SILC_FSM_STATE).
686 void silc_fsm_set_context(void *fsm, void *fsm_context);
688 /****f* silcutil/SilcFSMAPI/silc_fsm_get_context
692 * void *silc_fsm_get_context(void *fsm);
696 * Returns the context associated with the `fsm'. It is the context that
697 * was given to silc_fsm_alloc, silc_fsm_init, silc_fsm_thread_alloc or
698 * silc_fsm_thread_init. This function is used with both SilcFSM and
699 * SilcFSMThread contexts.
702 void *silc_fsm_get_context(void *fsm);
704 /****f* silcutil/SilcFSMAPI/silc_fsm_set_state_context
708 * void silc_fsm_set_state_context(void *fsm, void *state_context);
712 * Set's a state specific context for the `fsm'. This function can be
713 * used to change the state context inside the `fsm', if needed. This
714 * function is used with both SilcFSM and SilcFSMThread contexts. The
715 * context is the `state_context' in the state function (SILC_FSM_STATE).
718 void silc_fsm_set_state_context(void *fsm, void *state_context);
720 /****f* silcutil/SilcFSMAPI/silc_fsm_get_state_context
724 * void *silc_fsm_get_state_context(void *fsm);
728 * Returns the state context associated with the `fsm'. It is the context
729 * that was set with silc_fsm_set_state_context function. This function
730 * is used with both SilcFSM and SilcFSMThread contexts.
733 void *silc_fsm_get_state_context(void *fsm);
735 /****f* silcutil/SilcFSMAPI/silc_fsm_get_schedule
739 * SilcSchedule silc_fsm_get_schedule(void *fsm);
743 * Returns the SilcSchedule that has been associated with the `fsm'.
744 * If caller needs scheduler it may retrieve it with this function. This
745 * function is used with both SilcFSM and SilcFSMThread contexts.
747 * If the `fsm' is thread and real system threads are being used, and this
748 * is called from the thread, it will return the SilcSchedule that was
749 * allocated by the FSM for the thread. It is strongly recommended to
750 * use this SilcSchedule if you are using real threads, and you need
751 * scheduler in the thread. Note that, once the thread finishes the
752 * returned SilcSchedule becomes invalid.
754 * In other times this returns the SilcSchedule pointer that was given
755 * to silc_fsm_alloc or silc_fsm_init.
758 SilcSchedule silc_fsm_get_schedule(void *fsm);
760 /****f* silcutil/SilcFSMAPI/silc_fsm_get_machine
764 * SilcFSM silc_fsm_get_machine(SilcFSMThread thread);
768 * Returns the machine from the FSM thread indicated by `thread'.
771 SilcFSM silc_fsm_get_machine(SilcFSMThread thread);
776 /****s* silcutil/SilcFSMAPI/SilcFSMSema
780 * typedef struct SilcFSMSemaObject *SilcFSMSema;
784 * The FSM semaphore context allocated with silc_fsm_sema_alloc. The
785 * caller must free it with silc_fsm_sema_free. It is also possible
786 * to use pre-allocated SilcFSMSemaStruct instead of SilcFSMSema context.
789 typedef struct SilcFSMSemaObject *SilcFSMSema;
791 /****s* silcutil/SilcFSMAPI/SilcFSMSemaStruct
795 * typedef struct SilcFSMSemaObject SilcFSMSemaStruct;
799 * The FSM semaphore context that can be used as pre-allocated context.
800 * It is initialized with silc_fsm_sema_init. It need not be
804 typedef struct SilcFSMSemaObject SilcFSMSemaStruct;
806 /****f* silcutil/SilcFSMAPI/silc_fsm_sema_alloc
810 * SilcFSMSema silc_fsm_sema_alloc(SilcFSM fsm, SilcUInt32 value);
814 * Allocates FSM semaphore with initial value of `value'. Semaphores are
815 * counters for resources shared between machine and threads. Semaphores
816 * can be waited until the semaphore value is non-zero. The FSM will be
817 * suspended when waiting for semaphore. When the semaphore is incremented
818 * all that are waiting for the semaphore will be signalled and awaken.
820 * Semaphores can be used to wait for example when thread terminates, or
821 * when thread moves into a specific state, or to protect critical
822 * sections. The FSM semaphores can be used also in FSM threads that are
823 * executed in real system threads.
825 * Use the macros SILC_FSM_SEMA_WAIT and SILC_FSM_SEMA_TIMEDWAIT to wait
826 * for semaphore. Use the SILC_FSM_SEMA_POST macro to increment the
827 * counter and wake up all waiters.
830 SilcFSMSema silc_fsm_sema_alloc(SilcFSM fsm, SilcUInt32 value);
832 /****f* silcutil/SilcFSMAPI/silc_fsm_sema_init
836 * void silc_fsm_sema_init(SilcFSMSema sema, SilcFSM fsm, SilcUInt32 value);
840 * Initializes a pre-allocates semaphore context. This call is
841 * equivalent to silc_fsm_sema_alloc except this use the pre-allocated
842 * context. This fuction does not allocate any memory.
845 void silc_fsm_sema_init(SilcFSMSema sema, SilcFSM fsm, SilcUInt32 value);
847 /****f* silcutil/SilcFSMAPI/silc_fsm_sema_free
851 * void silc_fsm_sema_free(SilcFSMSema sema);
855 * Free the semaphore allocated by silc_fsm_sema_alloc function.
858 void silc_fsm_sema_free(SilcFSMSema sema);
860 /****d* silcutil/SilcFSMAPI/SILC_FSM_SEMA_WAIT
864 * SILC_FSM_SEMA_WAIT(semaphore)
868 * Macro used to wait for the `semaphore' to become non-zero. The
869 * machine will be suspended while it is waiting for the semaphore.
870 * This macro can only be used in FSM state functions. When the
871 * semaphore is signalled the FSM will re-enter the current state (or
872 * state that was set with silc_fsm_next before waiting).
876 * // Signalling example
877 * ctx->sema = silc_fsm_sema_alloc(fsm, 0);
880 * SILC_FSM_STATE(silc_foo_state)
884 * // Wait here for async call to complete
885 * SILC_FSM_SEMA_WAIT(ctx->async_sema);
887 * // Async call completed
888 * if (ctx->async_success == FALSE)
893 * // Mutual exclusion example
894 * ctx->lock = silc_fsm_sema_alloc(fsm, 1);
897 * SILC_FSM_STATE(silc_foo_state)
900 * SILC_FSM_SEMA_WAIT(ctx->lock);
901 * very critical stuff...
902 * SILC_FSM_SEMA_POST(ctx->lock);
907 #define SILC_FSM_SEMA_WAIT(sema) \
909 if (silc_fsm_sema_wait(sema, fsm) == 0) \
910 return SILC_FSM_WAIT; \
913 /****d* silcutil/SilcFSMAPI/SILC_FSM_SEMA_TIMEDWAIT
917 * SILC_FSM_SEMA_TIMEDWAIT(semaphore, seconds, useconds, timedout)
921 * Macro used to wait for the `semaphore' to become non-zero, or until
922 * the timeout specified by `seconds' and `useconds' has elapsed. If
923 * the timeout occurs before the semaphore becomes non-zero, the machine
924 * will wakeup. The `timedout' is SilcBool pointer and if it is
925 * non-NULL indication of whether timeout occurred or not is saved to
926 * the pointer. This macro can only be used in FSM state functions.
927 * When the semaphore is signalled or timedout the FSM will re-enter
928 * the current state (or state that was set with silc_fsm_next before
933 * SILC_FSM_STATE(silc_foo_state)
939 * // Wait here for async call to complete, or 10 seconds for timeout
940 * SILC_FSM_SEMA_TIMEDWAIT(ctx->async_sema, 10, 0, &timedout);
942 * // See if timeout occurred
943 * if (timedout == TRUE)
946 * // Async call completed
947 * if (ctx->async_success == FALSE)
953 #define SILC_FSM_SEMA_TIMEDWAIT(sema, seconds, useconds, ret_to) \
955 if (silc_fsm_sema_timedwait(sema, fsm, seconds, useconds, ret_to) == 0) \
956 return SILC_FSM_WAIT; \
959 /****f* silcutil/SilcFSMAPI/SILC_FSM_SEMA_POST
963 * SILC_FSM_SEMA_POST(semaphore)
967 * Increases the semaphore counter and awakens everybody that are
968 * waiting for this semaphore. This macro never blocks. It can be
969 * safely called at any place in state function and in asynchronous
970 * callbacks or other functions.
974 * SILC_FSM_STATE(silc_foo_async_completion)
978 * // Notify all waiters
979 * ctx->async_success = TRUE;
980 * SILC_FSM_SEMA_POST(ctx->async_sema);
985 #define SILC_FSM_SEMA_POST(sema) \
987 silc_fsm_sema_post(sema); \
990 #include "silcfsm_i.h"
992 #endif /* SILCFSM_H */