Sun Mar 11 15:22:42 CET 2007 Jochen Eisinger <coffee@silcnet.org>
[crypto.git] / lib / silcutil / silcfsm.h
1 /*
2
3   silcfsm.h
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2005, 2006, 2007 Pekka Riikonen
8
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.
12
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.
17
18 */
19
20 /****h* silcutil/SILC Finite State Machine
21  *
22  * DESCRIPTION
23  *
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
29  * threads.
30  *
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.
35  *
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
40  * is not required.
41  *
42  ***/
43
44 #ifndef SILCFSM_H
45 #define SILCFSM_H
46
47 /****s* silcutil/SilcFSMAPI/SilcFSM
48  *
49  * NAME
50  *
51  *    typedef struct SilcFSMObject *SilcFSM;
52  *
53  * DESCRIPTION
54  *
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.
59  *
60  ***/
61 typedef struct SilcFSMObject *SilcFSM;
62
63 /****s* silcutil/SilcFSMAPI/SilcFSMStruct
64  *
65  * NAME
66  *
67  *    typedef struct SilcFSMObject SilcFSMStruct;
68  *
69  * DESCRIPTION
70  *
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.
74  *
75  ***/
76 typedef struct SilcFSMObject SilcFSMStruct;
77
78 /****s* silcutil/SilcFSMAPI/SilcFSMThread
79  *
80  * NAME
81  *
82  *    typedef struct SilcFSMObject *SilcFSMThread;
83  *
84  * DESCRIPTION
85  *
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.
93  *
94  ***/
95 typedef struct SilcFSMObject *SilcFSMThread;
96
97 /****s* silcutil/SilcFSMAPI/SilcFSMThreadStruct
98  *
99  * NAME
100  *
101  *    typedef struct SilcFSMObject SilcFSMThreadStruct;
102  *
103  * DESCRIPTION
104  *
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.
108  *
109  ***/
110 typedef struct SilcFSMObject SilcFSMThreadStruct;
111
112 /****d* silcutil/SilcFSMAPI/SILC_FSM_CONTINUE
113  *
114  * NAME
115  *
116  *    #define SILC_FSM_CONTINUE ...
117  *
118  * DESCRIPTION
119  *
120  *    Moves to next state synchronously.  This type is used is returned
121  *    from state functions to immediately move to next state.
122  *
123  * EXAMPLE
124  *
125  *    SILC_FSM_STATE(silc_foo_state)
126  *    {
127  *      ...
128  *
129  *      // Move to next state now
130  *      silc_fsm_next(fsm, silc_foo_next_state);
131  *      return SILC_FSM_CONTINUE;
132  *    }
133  *
134  ***/
135 #if defined(SILC_DEBUG)
136 #define SILC_FSM_CONTINUE \
137   fsm->next_state(fsm, fsm->fsm_context, fsm->state_context);
138 #else
139 #define SILC_FSM_CONTINUE SILC_FSM_ST_CONTINUE;
140 #endif /* SILC_DEBUG */
141
142 /****d* silcutil/SilcFSMAPI/SILC_FSM_YIELD
143  *
144  * NAME
145  *
146  *    #define SILC_FSM_YIELD ...
147  *
148  * DESCRIPTION
149  *
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.
154  *
155  ***/
156 #define SILC_FSM_YIELD SILC_FSM_ST_YIELD;
157
158 /****d* silcutil/SilcFSMAPI/SILC_FSM_WAIT
159  *
160  * NAME
161  *
162  *    #define SILC_FSM_WAIT ...
163  *
164  * DESCRIPTION
165  *
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.
169  *
170  ***/
171 #define SILC_FSM_WAIT SILC_FSM_ST_WAIT
172
173 /****d* silcutil/SilcFSMAPI/SILC_FSM_FINISH
174  *
175  * NAME
176  *
177  *    #define SILC_FSM_FINISH ...
178  *
179  * DESCRIPTION
180  *
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.
185  *
186  ***/
187 #define SILC_FSM_FINISH SILC_FSM_ST_FINISH
188
189 /****f* silcutil/SilcFSMAPI/SilcFSMDestructor
190  *
191  * SYNOPSIS
192  *
193  *    typedef void (*SilcFSMDestructor)(SilcFSM fsm, void *fsm_context,
194  *                                      void *destructor_context);
195  *
196  * DESCRIPTION
197  *
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.
204  *
205  ***/
206 typedef void (*SilcFSMDestructor)(SilcFSM fsm, void *fsm_context,
207                                   void *destructor_context);
208
209 /****f* silcutil/SilcFSMAPI/SilcFSMThreadDestructor
210  *
211  * SYNOPSIS
212  *
213  *    typedef void (*SilcFSMThreadDestructor)(SilcFSMThread thread,
214  *                                            void *thread_context,
215  *                                            void *destructor_context);
216  *
217  * DESCRIPTION
218  *
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.
225  *
226  * NOTES
227  *
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
230  *    thread.
231  *
232  ***/
233 typedef void (*SilcFSMThreadDestructor)(SilcFSMThread thread,
234                                         void *thread_context,
235                                         void *destructor_context);
236
237 /****d* silcutil/SilcFSMAPI/SILC_FSM_STATE
238  *
239  * NAME
240  *
241  *    #define SILC_FSM_STATE(name)
242  *
243  * DESCRIPTION
244  *
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.
250  *
251  * SOURCE
252  */
253 #define SILC_FSM_STATE(name)                                            \
254 int name(struct SilcFSMObject *fsm, void *fsm_context, void *state_context)
255 /***/
256
257 /* State function callback */
258 typedef int (*SilcFSMStateCallback)(struct SilcFSMObject *fsm,
259                                     void *fsm_context,
260                                     void *state_context);
261
262 /****d* silcutil/SilcFSMAPI/SILC_FSM_CALL
263  *
264  * NAME
265  *
266  *    SILC_FSM_CALL(function)
267  *
268  * DESCRIPTION
269  *
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.
275  *
276  * NOTES
277  *
278  *    The state function returns in this macro.
279  *
280  * EXAMPLE
281  *
282  *    // Simple example
283  *    silc_fsm_next(fsm, some_next_state);
284  *    SILC_FSM_CALL(silc_some_async_call(server, some_callback, context));
285  *
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)));
290  *
291  ***/
292 #define SILC_FSM_CALL(function)                 \
293 do {                                            \
294   assert(!silc_fsm_set_call(fsm, TRUE));        \
295   function;                                     \
296   if (!silc_fsm_set_call(fsm, FALSE))           \
297     return SILC_FSM_CONTINUE;                   \
298   return SILC_FSM_WAIT;                         \
299 } while(0)
300
301 /****d* silcutil/SilcFSMAPI/SILC_FSM_CALL_CONTINUE
302  *
303  * NAME
304  *
305  *    SILC_FSM_CALL_CONTINUE(fsm)
306  *
307  * DESCRIPTION
308  *
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.
311  *
312  * EXAMPLE
313  *
314  *    void some_callback(void *context) {
315  *      SilcFSM fsm = context;
316  *      ...
317  *      // Continue to the next state
318  *      SILC_FSM_CALL_CONTINUE(fsm);
319  *    }
320  *
321  ***/
322 #define SILC_FSM_CALL_CONTINUE(fsm)             \
323 do {                                            \
324   if (!silc_fsm_set_call(fsm, FALSE))           \
325     silc_fsm_continue(fsm);                     \
326 } while(0)
327
328 /****d* silcutil/SilcFSMAPI/SILC_FSM_CALL_CONTINUE_SYNC
329  *
330  * NAME
331  *
332  *    SILC_FSM_CALL_CONTINUE_SYNC(fsm)
333  *
334  * DESCRIPTION
335  *
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
339  *    scheduler.
340  *
341  * EXAMPLE
342  *
343  *    void some_callback(void *context) {
344  *      SilcFSM fsm = context;
345  *      ...
346  *      // Continue to the next state immediately
347  *      SILC_FSM_CALL_CONTINUE_SYNC(fsm);
348  *    }
349  *
350  ***/
351 #define SILC_FSM_CALL_CONTINUE_SYNC(fsm)        \
352 do {                                            \
353   if (!silc_fsm_set_call(fsm, FALSE))           \
354     silc_fsm_continue_sync(fsm);                \
355 } while(0)
356
357 /****d* silcutil/SilcFSMAPI/SILC_FSM_THREAD_WAIT
358  *
359  * NAME
360  *
361  *    SILC_FSM_THREAD_WAIT(thread)
362  *
363  * DESCRIPTION
364  *
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.
368  *
369  * NOTES
370  *
371  *    The state function returns in this macro.
372  *
373  *    This macro is the only way to safely make sure that the thread has
374  *    terminated by the time FSM continues from the waiting state.  Using
375  *    FSM events to signal from the thread before SILC_FSM_FINISH is returned
376  *    works with normal FSM threads, but especially with real system threads
377  *    it does not guarantee that the FSM won't continue before the thread has
378  *    actually terminated.  Usually this is not a problem, but it can be a
379  *    problem if the FSM is waiting to be freed.  In this case using this
380  *    macro is strongly recommended.
381  *
382  ***/
383 #define SILC_FSM_THREAD_WAIT(thread)            \
384 do {                                            \
385   silc_fsm_thread_wait(fsm, thread);            \
386   return SILC_FSM_WAIT;                         \
387 } while(0)
388
389 /****f* silcutil/SilcFSMAPI/silc_fsm_alloc
390  *
391  * SYNOPSIS
392  *
393  *    SilcFSM silc_fsm_alloc(void *fsm_context,
394  *                           SilcFSMDestructor destructor,
395  *                           void *destructor_context,
396  *                           SilcSchedule schedule);
397  *
398  * DESCRIPTION
399  *
400  *    Allocates SILC Finite State Machine context.  The `destructor' with
401  *    `destructor_context' will be called when the machines finishes.  The
402  *    caller must free the returned context with silc_fsm_free.  The
403  *    `fsm_context' is delivered to every FSM state function.  The `schedule'
404  *    is the caller's scheduler and the FSM will be run in the scheduler.
405  *
406  * EXAMPLE
407  *
408  *    SilcAsyncOperation silc_async_call(Callback callback, void *cb_context)
409  *    {
410  *      SilcAsyncOperation op;
411  *      SilcFSM fsm;
412  *      ...
413  *
414  *      // Allocate async operation so that caller can control us, like abort
415  *      op = silc_async_alloc(silc_async_call_abort, NULL, ourcontext);
416  *
417  *      // Start FSM
418  *      fsm = silc_fsm_alloc(ourcontext, fsm_destructor, ourcontext,
419  *                           schedule);
420  *      silc_fsm_start(fsm, first_state);
421  *      ...
422  *
423  *      // Return async operation for upper layer
424  *      return op;
425  *    }
426  *
427  ***/
428 SilcFSM silc_fsm_alloc(void *fsm_context,
429                        SilcFSMDestructor destructor,
430                        void *destructor_context,
431                        SilcSchedule schedule);
432
433 /****f* silcutil/SilcFSMAPI/silc_fsm_init
434  *
435  * SYNOPSIS
436  *
437  *    SilcBool silc_fsm_init(SilcFSM fsm,
438  *                           void *fsm_context,
439  *                           SilcFSMDestructor destructor,
440  *                           void *destructor_context,
441  *                           SilcSchedule schedule);
442  *
443  * DESCRIPTION
444  *
445  *    Initializes a pre-allocated SilcFSM context.  This call is equivalent
446  *    to silc_fsm_alloc except that this takes the pre-allocated context
447  *    as argument.  The silc_fsm_free must not be called if this was called.
448  *    Returns TRUE if the initialization is Ok or FALSE if error occurred.
449  *    This function does not allocate any memory.  The `schedule' is the
450  *    caller's scheduler and the FSM will be run in the scheduler.
451  *
452  * EXAMPLE
453  *
454  *    SilcFSMStruct fsm;
455  *
456  *    silc_fsm_init(&fsm, application, fsm_destructor, application, schedule);
457  *    silc_fsm_start(&fsm, first_state);
458  *
459  ***/
460 SilcBool silc_fsm_init(SilcFSM fsm,
461                        void *fsm_context,
462                        SilcFSMDestructor destructor,
463                        void *destructor_context,
464                        SilcSchedule schedule);
465
466 /****f* silcutil/SilcFSMAPI/silc_fsm_thread_alloc
467  *
468  * SYNOPSIS
469  *
470  *    SilcFSMThread silc_fsm_thread_alloc(SilcFSM fsm,
471  *                                        void *thread_context,
472  *                                        SilcFSMThreadDestructor destructor,
473  *                                        void *destructor_context,
474  *                                        SilcBool real_thread);
475  *
476  * DESCRIPTION
477  *
478  *    Allocates FSM thread context.  The thread will be executed in the
479  *    FSM machine indicated by `fsm'.  The caller must free the returned
480  *    thread context with silc_fsm_free.  If the 'real_thread' is TRUE
481  *    then the thread will actually be executed in real thread, if platform
482  *    supports them.  The `thread_context' is delivered to every state
483  *    function in the thread.
484  *
485  * NOTES
486  *
487  *    If the system does not support threads, then this function will revert
488  *    back to normal FSM threads.
489  *
490  *    If the `real_thread' is TRUE then FSM will allocate new SilcSchedule
491  *    for the FSM thread. If you need scheduler in the real thread it is
492  *    strongly recommended that you use the SilcSchedule that is allocated
493  *    for the thread.  You can retrieve the SilcSchedule from the thread
494  *    using silc_fsm_get_schedule function.  Note that, the allocated
495  *    SilcSchedule will become invalid after the thread finishes.
496  *
497  *    If `real_thread' is FALSE the silc_fsm_get_schedule will return
498  *    the SilcSchedule that was originally given to silc_fsm_alloc or
499  *    silc_fsm_init.
500  *
501  * EXAMPLE
502  *
503  *    SILC_FSM_STATE(silc_foo_state)
504  *    {
505  *      SilcFSMThread thread;
506  *      ...
507  *
508  *      // Execute the route lookup in thread
509  *      thread = silc_fsm_thread_alloc(fsm, fsm_context, NULL, NULL, FALSE);
510  *      silc_fsm_start(thread, silc_route_lookup_start);
511  *
512  *      // Wait here for the thread to terminate. Set the state where to go
513  *      // after the thread has terminated.
514  *      silc_fsm_next(fsm, silc_foo_route_lookup_finished);
515  *      SILC_FSM_THREAD_WAIT(thread);
516  *    }
517  *
518  ***/
519 SilcFSMThread silc_fsm_thread_alloc(SilcFSM fsm,
520                                     void *thread_context,
521                                     SilcFSMThreadDestructor destructor,
522                                     void *destructor_context,
523                                     SilcBool real_thread);
524
525 /****f* silcutil/SilcFSMAPI/silc_fsm_thread_init
526  *
527  * SYNOPSIS
528  *
529  *    void silc_fsm_thread_init(SilcFSMThread thread,
530  *                              SilcFSM fsm,
531  *                              void *thread_context,
532  *                              SilcFSMThreadDestructor destructor,
533  *                              void *destructor_context,
534  *                              SilcBool real_thread);
535  *
536  * DESCRIPTION
537  *
538  *    Initializes a pre-allocated SilcFSMThread context.  This call is
539  *    equivalent to silc_fsm_thread_alloc except that this takes the
540  *    pre-allocated context as argument.  The silc_fsm_free must not be
541  *    called if this was called.  If the `real_thread' is TRUE then the
542  *    thread will actually be executed in real thread, if platform supports
543  *    them.
544  *
545  * NOTES
546  *
547  *    See the notes from the silc_fsm_thread_alloc.
548  *
549  * EXAMPLE
550  *
551  *    SilcFSMThreadStruct thread;
552  *
553  *    silc_fsm_thread_init(&thread, fsm, application, NULL, NULL, FALSE);
554  *    silc_fsm_start(&thread, first_state);
555  *
556  ***/
557 void silc_fsm_thread_init(SilcFSMThread thread,
558                           SilcFSM fsm,
559                           void *thread_context,
560                           SilcFSMThreadDestructor destructor,
561                           void *destructor_context,
562                           SilcBool real_thread);
563
564 /****f* silcutil/SilcFSMAPI/silc_fsm_free
565  *
566  * SYNOPSIS
567  *
568  *    void silc_fsm_free(void *fsm);
569  *
570  * DESCRIPTION
571  *
572  *    Free the SILC FSM context that was allocated with silc_fsm_alloc,
573  *    or free the SILC FSM thread context that was allocated with
574  *    silc_fsm_thread_alloc.  This function is used with both SilcFSM
575  *    and SilcFSMThread contexts.
576  *
577  * NOTES
578  *
579  *    When freeing FSM, it must not have any active threads.
580  *
581  ***/
582 void silc_fsm_free(void *fsm);
583
584 /****f* silcutil/SilcFSMAPI/silc_fsm_start
585  *
586  * SYNOPSIS
587  *
588  *    void silc_fsm_start(void *fsm, SilcFSMStateCallback start_state);
589  *
590  * DESCRIPTION
591  *
592  *    This function must be called after the SILC FSM context was created.
593  *    This actually starts the state machine.  Note that, the machine is
594  *    started later after this function returns.  The `start_state' is the
595  *    state where the machine or thread is started.  This function is used
596  *    with both SilcFSM and SilcFSMThread contexts.
597  *
598  * EXAMPLE
599  *
600  *    SilcFSM fsm;
601  *
602  *    fsm = silc_fsm_alloc(context, destructor, context, schedule);
603  *    silc_fsm_start(fsm, first_state);
604  *
605  ***/
606 void silc_fsm_start(void *fsm, SilcFSMStateCallback start_state);
607
608 /****f* silcutil/SilcFSMAPI/silc_fsm_start_sync
609  *
610  * SYNOPSIS
611  *
612  *    void silc_fsm_start_sync(void *fsm, SilcFSMStateCallback start_state);
613  *
614  * DESCRIPTION
615  *
616  *    This function is same as silc_fsm_start, except that the FSM will
617  *    be started immediately inside this function.  After this function
618  *    returns the `start_state' has already been executed.  If the machine
619  *    is completely synchronous (no waiting used in the machine) then
620  *    the machine will have finished once this function returns.  Also
621  *    note that if the machine is completely synchronous the destructor
622  *    will also be called from inside this function.  This function is used
623  *    with both SilcFSM and SilcFSMThread contexts.
624  *
625  ***/
626 void silc_fsm_start_sync(void *fsm, SilcFSMStateCallback start_state);
627
628 /****f* silcutil/SilcFSMAPI/silc_fsm_next
629  *
630  * SYNOPSIS
631  *
632  *    void silc_fsm_next(void *fsm, SilcFSMStateCallback next_state);
633  *
634  * DESCRIPTION
635  *
636  *    Set the next state to be executed.  If the state function that
637  *    call this function returns SILC_FSM_CONTINUE, the `next_state'
638  *    will be executed immediately.  If it returns SILC_FSM_YIELD it
639  *    yields the thread and the `next_state' will be run after other
640  *    threads have run first.  This function must always be used to set
641  *    the next state in the machine or thread.  This function is used
642  *    with both SilcFSM and SilcFSMThread contexts.
643  *
644  * EXAMPLE
645  *
646  *    // Move to next state
647  *    silc_fsm_next(fsm, next_state);
648  *    return SILC_FSM_CONTINUE;
649  *
650  ***/
651 void silc_fsm_next(void *fsm, SilcFSMStateCallback next_state);
652
653 /****f* silcutil/SilcFSMAPI/silc_fsm_next_later
654  *
655  * SYNOPSIS
656  *
657  *    void silc_fsm_next_later(void *fsm, SilcFSMStateCallback next_state,
658  *                             SilcUInt32 seconds, SilcUInt32 useconds);
659  *
660  * DESCRIPTION
661  *
662  *    Set the next state to be executed later, at the specified time.
663  *    The SILC_FSM_WAIT must be returned in the state function if this
664  *    function is called.  If any other state is returned machine operation
665  *    is undefined.  The machine or thread will move to `next_state' after
666  *    the specified timeout.  This function is used with both SilcFSM and
667  *    SilcFSMThread contexts.
668  *
669  * NOTES
670  *
671  *    If both `seconds' and `useconds' are 0, the effect is same as calling
672  *    silc_fsm_next function, and SILC_FSM_CONTINUE must be returned.
673  *
674  *    If silc_fsm_continue or silc_fsm_continue_sync is called while the
675  *    machine or thread is in SILC_FSM_WAIT state the timeout is automatically
676  *    canceled and the state moves to the next state.
677  *
678  * EXAMPLE
679  *
680  *    // Move to next state after 10 seconds
681  *    silc_fsm_next_later(fsm, next_state, 10, 0);
682  *    return SILC_FSM_WAIT;
683  *
684  ***/
685 void silc_fsm_next_later(void *fsm, SilcFSMStateCallback next_state,
686                          SilcUInt32 seconds, SilcUInt32 useconds);
687
688 /****f* silcutil/SilcFSMAPI/silc_fsm_continue
689  *
690  * SYNOPSIS
691  *
692  *    void silc_fsm_continue(void *fsm);
693  *
694  * DESCRIPTION
695  *
696  *    Continues in the state machine from a SILC_FSM_WAIT state.  This can
697  *    be called from outside waiting FSM to continue to the next state.
698  *    This function can be used instead of SILC_FSM_CALL_CONTINUE macro
699  *    in case the SILC_FSM_CALL was not used.  This must not be used if
700  *    SILC_FSM_CALL was used.  This function is used with both SilcFSM and
701  *    SilcFSMThread contexts.
702  *
703  ***/
704 void silc_fsm_continue(void *fsm);
705
706 /****f* silcutil/SilcFSMAPI/silc_fsm_continue_sync
707  *
708  * SYNOPSIS
709  *
710  *    void silc_fsm_continue_sync(void *fsm);
711  *
712  * DESCRIPTION
713  *
714  *    Continues immediately in the state machine from a SILC_FSM_WAIT state.
715  *    This can be called from outside waiting FSM to immediately continue to
716  *    the next state.  This function can be used instead of the
717  *    SILC_FSM_CALL_CONTINUE_SYNC macro in case the SILC_FSM_CALL was not used.
718  *    This must not be used if SILC_FSM_CALL was used.  This function is used
719  *    with both SilcFSM and SilcFSMThread contexts.
720  *
721  ***/
722 void silc_fsm_continue_sync(void *fsm);
723
724 /****f* silcutil/SilcFSMAPI/silc_fsm_finish
725  *
726  * SYNOPSIS
727  *
728  *    void silc_fsm_finish(void *fsm);
729  *
730  * DESCRIPTION
731  *
732  *    Finishes the `fsm'.  This function may be used in case the FSM
733  *    needs to be finished outside FSM states.  Usually FSM is finished
734  *    by returning SILC_FSM_FINISH from the state, but if this is not
735  *    possible this function may be called.  This function is used with
736  *    both SilcFSM and SilcFSMThread contexts.
737  *
738  *    If the `fsm' is a machine and it has running threads, the machine
739  *    will fatally fail.  The caller must first finish the threads and
740  *    then the machine.
741  *
742  ***/
743 void silc_fsm_finish(void *fsm);
744
745 /****f* silcutil/SilcFSMAPI/silc_fsm_set_context
746  *
747  * SYNOPSIS
748  *
749  *    void silc_fsm_set_context(void *fsm, void *fsm_context);
750  *
751  * DESCRIPTION
752  *
753  *    Set new context for the `fsm'.  This function can be used to change
754  *    the context inside the `fsm', if needed.  This function is used with
755  *    both SilcFSM and SilcFSMThread contexts.  The context is the
756  *    `fsm_context' in the state function (SILC_FSM_STATE).
757  *
758  ***/
759 void silc_fsm_set_context(void *fsm, void *fsm_context);
760
761 /****f* silcutil/SilcFSMAPI/silc_fsm_get_context
762  *
763  * SYNOPSIS
764  *
765  *    void *silc_fsm_get_context(void *fsm);
766  *
767  * DESCRIPTION
768  *
769  *    Returns the context associated with the `fsm'.  It is the context that
770  *    was given to silc_fsm_alloc, silc_fsm_init, silc_fsm_thread_alloc or
771  *    silc_fsm_thread_init.  This function is used with both SilcFSM and
772  *    SilcFSMThread contexts.
773  *
774  ***/
775 void *silc_fsm_get_context(void *fsm);
776
777 /****f* silcutil/SilcFSMAPI/silc_fsm_set_state_context
778  *
779  * SYNOPSIS
780  *
781  *    void silc_fsm_set_state_context(void *fsm, void *state_context);
782  *
783  * DESCRIPTION
784  *
785  *    Set's a state specific context for the `fsm'.  This function can be
786  *    used to change the state context inside the `fsm', if needed.  This
787  *    function is used with both SilcFSM and SilcFSMThread contexts.  The
788  *    context is the `state_context' in the state function (SILC_FSM_STATE).
789  *
790  ***/
791 void silc_fsm_set_state_context(void *fsm, void *state_context);
792
793 /****f* silcutil/SilcFSMAPI/silc_fsm_get_state_context
794  *
795  * SYNOPSIS
796  *
797  *    void *silc_fsm_get_state_context(void *fsm);
798  *
799  * DESCRIPTION
800  *
801  *    Returns the state context associated with the `fsm'.  It is the context
802  *    that was set with silc_fsm_set_state_context function.  This function
803  *    is used with both SilcFSM and SilcFSMThread contexts.
804  *
805  ***/
806 void *silc_fsm_get_state_context(void *fsm);
807
808 /****f* silcutil/SilcFSMAPI/silc_fsm_get_schedule
809  *
810  * SYNOPSIS
811  *
812  *    SilcSchedule silc_fsm_get_schedule(void *fsm);
813  *
814  * DESCRIPTION
815  *
816  *    Returns the SilcSchedule that has been associated with the `fsm'.
817  *    If caller needs scheduler it may retrieve it with this function.  This
818  *    function is used with both SilcFSM and SilcFSMThread contexts.
819  *
820  *    If the `fsm' is thread and real system threads are being used, and this
821  *    is called from the thread, it will return the SilcSchedule that was
822  *    allocated by the FSM for the thread.  It is strongly recommended to
823  *    use this SilcSchedule if you are using real threads, and you need
824  *    scheduler in the thread.  Note that, once the thread finishes the
825  *    returned SilcSchedule becomes invalid.
826  *
827  *    In other times this returns the SilcSchedule pointer that was given
828  *    to silc_fsm_alloc or silc_fsm_init.
829  *
830  ***/
831 SilcSchedule silc_fsm_get_schedule(void *fsm);
832
833 /****f* silcutil/SilcFSMAPI/silc_fsm_get_machine
834  *
835  * SYNOPSIS
836  *
837  *    SilcFSM silc_fsm_get_machine(SilcFSMThread thread);
838  *
839  * DESCRIPTION
840  *
841  *    Returns the machine from the FSM thread indicated by `thread'.
842  *
843  ***/
844 SilcFSM silc_fsm_get_machine(SilcFSMThread thread);
845
846 /****f* silcutil/SilcFSMAPI/silc_fsm_is_started
847  *
848  * SYNOPSIS
849  *
850  *    SilcBool silc_fsm_is_started(void *fsm);
851  *
852  * DESCRIPTION
853  *
854  *    Returns TRUE if the machine or thread `fsm' has been started and has
855  *    not been finished yet.  This function is used with both SilcFSM and
856  *    SilcFSMThread contexts.
857  *
858  ***/
859 SilcBool silc_fsm_is_started(void *fsm);
860
861 /* FSM Events */
862
863 /****s* silcutil/SilcFSMAPI/SilcFSMEvent
864  *
865  * NAME
866  *
867  *    typedef struct SilcFSMEventObject *SilcFSMEvent;
868  *
869  * DESCRIPTION
870  *
871  *    The FSM event context allocated with silc_fsm_event_alloc.  The
872  *    caller must free it with silc_fsm_event_free.  It is also possible
873  *    to use pre-allocated SilcFSMEventStruct instead of SilcFSMEvent context.
874  *
875  ***/
876 typedef struct SilcFSMEventObject *SilcFSMEvent;
877
878 /****s* silcutil/SilcFSMAPI/SilcFSMEventStruct
879  *
880  * NAME
881  *
882  *    typedef struct SilcFSMEventObject SilcFSMEventStruct;
883  *
884  * DESCRIPTION
885  *
886  *    The FSM event context that can be used as pre-allocated context.
887  *    It is initialized with silc_fsm_event_init.  It need not be
888  *    uninitialized.
889  *
890  ***/
891 typedef struct SilcFSMEventObject SilcFSMEventStruct;
892
893 /****f* silcutil/SilcFSMAPI/silc_fsm_event_alloc
894  *
895  * SYNOPSIS
896  *
897  *    SilcFSMEvent silc_fsm_event_alloc(SilcFSM fsm);
898  *
899  * DESCRIPTION
900  *
901  *    Allocates asynchronous FSM event.  FSM events are asynchronous events
902  *    that can be waited and signalled.  They can be used as condition
903  *    variables and signallers.  They can be used for example to wait that
904  *    some event happens, some thread moves to a specific state or similar.
905  *    The FSM Events may also be used in FSM threads that are executed in
906  *    real system threads.  It is safe to wait and signal the event from
907  *    threads.
908  *
909  *    Use the macros SILC_FSM_EVENT_WAIT and SILC_FSM_EVENT_TIMEDWAIT to wait
910  *    for the event.  Use the SILC_FSM_EVENT_SIGNAL macro to signal all the
911  *    waiters.
912  *
913  ***/
914 SilcFSMEvent silc_fsm_event_alloc(SilcFSM fsm);
915
916 /****f* silcutil/SilcFSMAPI/silc_fsm_event_init
917  *
918  * SYNOPSIS
919  *
920  *    void silc_fsm_event_init(SilcFSMEvent event, SilcFSM fsm);
921  *
922  * DESCRIPTION
923  *
924  *    Initializes a pre-allocates FSM event context.  This call is
925  *    equivalent to silc_fsm_event_alloc except this use the pre-allocated
926  *    context.  This fuction does not allocate any memory.
927  *
928  ***/
929 void silc_fsm_event_init(SilcFSMEvent event, SilcFSM fsm);
930
931 /****f* silcutil/SilcFSMAPI/silc_fsm_event_free
932  *
933  * SYNOPSIS
934  *
935  *    void silc_fsm_event_free(SilcFSMEvent event);
936  *
937  * DESCRIPTION
938  *
939  *    Free the event allocated by silc_fsm_event_alloc function.
940  *
941  ***/
942 void silc_fsm_event_free(SilcFSMEvent event);
943
944 /****d* silcutil/SilcFSMAPI/SILC_FSM_EVENT_WAIT
945  *
946  * NAME
947  *
948  *    SILC_FSM_EVENT_WAIT(event)
949  *
950  * DESCRIPTION
951  *
952  *    Macro used to wait for the `event' to be signalled.  The machine
953  *    or thread will be suspended while it is waiting for the event.
954  *    This macro can only be used in FSM state functions.  When the
955  *    event is signalled the FSM will re-enter the current state (or
956  *    state that was set with silc_fsm_next before waiting).
957  *
958  * EXAMPLE
959  *
960  *    // Signalling example
961  *    ctx->async_event = silc_fsm_event_alloc(fsm);
962  *    ...
963  *
964  *    SILC_FSM_STATE(silc_foo_state)
965  *    {
966  *      ...
967  *
968  *      // Wait here for async call to complete
969  *      SILC_FSM_EVENT_WAIT(ctx->async_event);
970  *
971  *      // Async call completed
972  *      if (ctx->async_success == FALSE)
973  *        fatal(error);
974  *      ...
975  *    }
976  *
977  ***/
978 #define SILC_FSM_EVENT_WAIT(event)              \
979 do {                                            \
980   if (silc_fsm_event_wait(event, fsm) == 0)     \
981     return SILC_FSM_WAIT;                       \
982 } while(0)
983
984 /****d* silcutil/SilcFSMAPI/SILC_FSM_EVENT_TIMEDWAIT
985  *
986  * NAME
987  *
988  *    SILC_FSM_EVENT_TIMEDWAIT(event, seconds, useconds, timedout)
989  *
990  * DESCRIPTION
991  *
992  *    Macro used to wait for the `event' to be signalled, or until
993  *    the timeout specified by `seconds' and `useconds' has elapsed.  If
994  *    the timeout occurs before the event is signalled, the machine
995  *    will wakeup.  The `timedout' is SilcBool pointer and if it is
996  *    non-NULL indication of whether timeout occurred or not is saved to
997  *    the pointer.  This macro can only be used in FSM state functions.
998  *    When the event is signalled or timedout the FSM will re-enter
999  *    the current state (or state that was set with silc_fsm_next before
1000  *    waiting).
1001  *
1002  * EXAMPLE
1003  *
1004  *    SILC_FSM_STATE(silc_foo_state)
1005  *    {
1006  *      SilcBool timedout;
1007  *      ...
1008  *
1009  *      // Wait here for async call to complete, or 10 seconds for timeout
1010  *      SILC_FSM_EVENT_TIMEDWAIT(ctx->async_event, 10, 0, &timedout);
1011  *
1012  *      // See if timeout occurred
1013  *      if (timedout == TRUE)
1014  *        fatal(error);
1015  *
1016  *      // Async call completed
1017  *      if (ctx->async_success == FALSE)
1018  *        fatal(error);
1019  *      ...
1020  *    }
1021  *
1022  ***/
1023 #define SILC_FSM_EVENT_TIMEDWAIT(event, seconds, useconds, ret_to)      \
1024 do {                                                                    \
1025   if (silc_fsm_event_timedwait(event, fsm, seconds, useconds, ret_to) == 0) \
1026     return SILC_FSM_WAIT;                                               \
1027 } while(0)
1028
1029 /****f* silcutil/SilcFSMAPI/SILC_FSM_EVENT_SIGNAL
1030  *
1031  * SYNOPSIS
1032  *
1033  *    SILC_FSM_EVENT_SIGNAL(event)
1034  *
1035  * DESCRIPTION
1036  *
1037  *    Signals the `event' and awakens everybody that are waiting for this
1038  *    event.  This macro never blocks.  It can be safely called at any place
1039  *    in state function and in asynchronous callbacks or other functions.
1040  *
1041  * EXAMPLE
1042  *
1043  *    SILC_FSM_STATE(silc_foo_async_completion)
1044  *    {
1045  *      ...
1046  *
1047  *      // Notify all waiters
1048  *      ctx->async_success = TRUE;
1049  *      SILC_FSM_EVENT_SIGNAL(ctx->async_event);
1050  *      ...
1051  *    }
1052  *
1053  ***/
1054 #define SILC_FSM_EVENT_SIGNAL(event)            \
1055 do {                                            \
1056   silc_fsm_event_signal(event);                 \
1057 } while(0)
1058
1059 #include "silcfsm_i.h"
1060
1061 #endif /* SILCFSM_H */