366edf704406b3cd48033f8d833279f7b1fdda1c
[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 returned from state
121  *    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   SILC_VERIFY(!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.  The machine or thread will continue once the waited
368  *    thread has terminated.
369  *
370  * NOTES
371  *
372  *    The state function returns in this macro.
373  *
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.
382  *
383  ***/
384 #define SILC_FSM_THREAD_WAIT(thread)            \
385 do {                                            \
386   silc_fsm_thread_wait(fsm, thread);            \
387   return SILC_FSM_WAIT;                         \
388 } while(0)
389
390 /****f* silcutil/SilcFSMAPI/silc_fsm_alloc
391  *
392  * SYNOPSIS
393  *
394  *    SilcFSM silc_fsm_alloc(void *fsm_context,
395  *                           SilcFSMDestructor destructor,
396  *                           void *destructor_context,
397  *                           SilcSchedule schedule);
398  *
399  * DESCRIPTION
400  *
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.
406  *    If `schedule' is NULL this will call silc_schedule_get_global to try
407  *    get global scheduler.  Returns NULL on error or if system is out of
408  *    memory.
409  *
410  * EXAMPLE
411  *
412  *    SilcAsyncOperation silc_async_call(Callback callback, void *cb_context)
413  *    {
414  *      SilcAsyncOperation op;
415  *      SilcFSM fsm;
416  *      ...
417  *
418  *      // Allocate async operation so that caller can control us, like abort
419  *      op = silc_async_alloc(silc_async_call_abort, NULL, ourcontext);
420  *
421  *      // Start FSM
422  *      fsm = silc_fsm_alloc(ourcontext, fsm_destructor, ourcontext,
423  *                           schedule);
424  *      silc_fsm_start(fsm, first_state);
425  *      ...
426  *
427  *      // Return async operation for upper layer
428  *      return op;
429  *    }
430  *
431  ***/
432 SilcFSM silc_fsm_alloc(void *fsm_context,
433                        SilcFSMDestructor destructor,
434                        void *destructor_context,
435                        SilcSchedule schedule);
436
437 /****f* silcutil/SilcFSMAPI/silc_fsm_init
438  *
439  * SYNOPSIS
440  *
441  *    SilcBool silc_fsm_init(SilcFSM fsm,
442  *                           void *fsm_context,
443  *                           SilcFSMDestructor destructor,
444  *                           void *destructor_context,
445  *                           SilcSchedule schedule);
446  *
447  * DESCRIPTION
448  *
449  *    Initializes a pre-allocated SilcFSM context.  This call is equivalent
450  *    to silc_fsm_alloc except that this takes the pre-allocated context
451  *    as argument.  The silc_fsm_free must not be called if this was called.
452  *    Returns TRUE if the initialization is Ok or FALSE if error occurred.
453  *    This function does not allocate any memory.  The `schedule' is the
454  *    caller's scheduler and the FSM will be run in the scheduler.  If
455  *    `schedule' is NULL this will call silc_schedule_get_global to try to
456  *    get global scheduler.
457  *
458  * EXAMPLE
459  *
460  *    SilcFSMStruct fsm;
461  *
462  *    silc_fsm_init(&fsm, application, fsm_destructor, application, schedule);
463  *    silc_fsm_start(&fsm, first_state);
464  *
465  ***/
466 SilcBool silc_fsm_init(SilcFSM fsm,
467                        void *fsm_context,
468                        SilcFSMDestructor destructor,
469                        void *destructor_context,
470                        SilcSchedule schedule);
471
472 /****f* silcutil/SilcFSMAPI/silc_fsm_thread_alloc
473  *
474  * SYNOPSIS
475  *
476  *    SilcFSMThread silc_fsm_thread_alloc(SilcFSM fsm,
477  *                                        void *thread_context,
478  *                                        SilcFSMThreadDestructor destructor,
479  *                                        void *destructor_context,
480  *                                        SilcBool real_thread);
481  *
482  * DESCRIPTION
483  *
484  *    Allocates FSM thread context.  The thread will be executed in the
485  *    FSM machine indicated by `fsm'.  The caller must free the returned
486  *    thread context with silc_fsm_free.  If the 'real_thread' is TRUE
487  *    then the thread will actually be executed in real thread, if platform
488  *    supports them.  The `thread_context' is delivered to every state
489  *    function in the thread.  Returns NULL on error or if the system is out
490  *    of memory.
491  *
492  * NOTES
493  *
494  *    If the system does not support threads, then this function will revert
495  *    back to normal FSM threads.
496  *
497  *    If the `real_thread' is TRUE then FSM will allocate new SilcSchedule
498  *    for the FSM thread. If you need scheduler in the real thread it is
499  *    strongly recommended that you use the SilcSchedule that is allocated
500  *    for the thread.  You can retrieve the SilcSchedule from the thread
501  *    using silc_fsm_get_schedule function.  Note that, the allocated
502  *    SilcSchedule will become invalid after the thread finishes.
503  *
504  *    If `real_thread' is FALSE the silc_fsm_get_schedule will return
505  *    the SilcSchedule that was originally given to silc_fsm_alloc or
506  *    silc_fsm_init.
507  *
508  * EXAMPLE
509  *
510  *    SILC_FSM_STATE(silc_foo_state)
511  *    {
512  *      SilcFSMThread thread;
513  *      ...
514  *
515  *      // Execute the route lookup in thread
516  *      thread = silc_fsm_thread_alloc(fsm, fsm_context, NULL, NULL, FALSE);
517  *      silc_fsm_start(thread, silc_route_lookup_start);
518  *
519  *      // Wait here for the thread to terminate. Set the state where to go
520  *      // after the thread has terminated.
521  *      silc_fsm_next(fsm, silc_foo_route_lookup_finished);
522  *      SILC_FSM_THREAD_WAIT(thread);
523  *    }
524  *
525  ***/
526 SilcFSMThread silc_fsm_thread_alloc(SilcFSM fsm,
527                                     void *thread_context,
528                                     SilcFSMThreadDestructor destructor,
529                                     void *destructor_context,
530                                     SilcBool real_thread);
531
532 /****f* silcutil/SilcFSMAPI/silc_fsm_thread_init
533  *
534  * SYNOPSIS
535  *
536  *    void silc_fsm_thread_init(SilcFSMThread thread,
537  *                              SilcFSM fsm,
538  *                              void *thread_context,
539  *                              SilcFSMThreadDestructor destructor,
540  *                              void *destructor_context,
541  *                              SilcBool real_thread);
542  *
543  * DESCRIPTION
544  *
545  *    Initializes a pre-allocated SilcFSMThread context.  This call is
546  *    equivalent to silc_fsm_thread_alloc except that this takes the
547  *    pre-allocated context as argument.  The silc_fsm_free must not be
548  *    called if this was called.  If the `real_thread' is TRUE then the
549  *    thread will actually be executed in real thread, if platform supports
550  *    them.
551  *
552  * NOTES
553  *
554  *    See the notes from the silc_fsm_thread_alloc.
555  *
556  * EXAMPLE
557  *
558  *    SilcFSMThreadStruct thread;
559  *
560  *    silc_fsm_thread_init(&thread, fsm, application, NULL, NULL, FALSE);
561  *    silc_fsm_start(&thread, first_state);
562  *
563  ***/
564 void silc_fsm_thread_init(SilcFSMThread thread,
565                           SilcFSM fsm,
566                           void *thread_context,
567                           SilcFSMThreadDestructor destructor,
568                           void *destructor_context,
569                           SilcBool real_thread);
570
571 /****f* silcutil/SilcFSMAPI/silc_fsm_free
572  *
573  * SYNOPSIS
574  *
575  *    void silc_fsm_free(void *fsm);
576  *
577  * DESCRIPTION
578  *
579  *    Free the SILC FSM context that was allocated with silc_fsm_alloc,
580  *    or free the SILC FSM thread context that was allocated with
581  *    silc_fsm_thread_alloc.  This function is used with both SilcFSM
582  *    and SilcFSMThread contexts.
583  *
584  * NOTES
585  *
586  *    When freeing FSM, it must not have any active threads.
587  *
588  ***/
589 void silc_fsm_free(void *fsm);
590
591 /****f* silcutil/SilcFSMAPI/silc_fsm_start
592  *
593  * SYNOPSIS
594  *
595  *    void silc_fsm_start(void *fsm, SilcFSMStateCallback start_state);
596  *
597  * DESCRIPTION
598  *
599  *    This function must be called after the SILC FSM context was created.
600  *    This actually starts the state machine.  Note that, the machine is
601  *    started later after this function returns.  The `start_state' is the
602  *    state where the machine or thread is started.  This function is used
603  *    with both SilcFSM and SilcFSMThread contexts.
604  *
605  * EXAMPLE
606  *
607  *    SilcFSM fsm;
608  *
609  *    fsm = silc_fsm_alloc(context, destructor, context, schedule);
610  *    silc_fsm_start(fsm, first_state);
611  *
612  ***/
613 void silc_fsm_start(void *fsm, SilcFSMStateCallback start_state);
614
615 /****f* silcutil/SilcFSMAPI/silc_fsm_start_sync
616  *
617  * SYNOPSIS
618  *
619  *    void silc_fsm_start_sync(void *fsm, SilcFSMStateCallback start_state);
620  *
621  * DESCRIPTION
622  *
623  *    This function is same as silc_fsm_start, except that the FSM will
624  *    be started immediately inside this function.  After this function
625  *    returns the `start_state' has already been executed.  If the machine
626  *    is completely synchronous (no waiting used in the machine) then
627  *    the machine will have finished once this function returns.  Also
628  *    note that if the machine is completely synchronous the destructor
629  *    will also be called from inside this function.  This function is used
630  *    with both SilcFSM and SilcFSMThread contexts.
631  *
632  ***/
633 void silc_fsm_start_sync(void *fsm, SilcFSMStateCallback start_state);
634
635 /****f* silcutil/SilcFSMAPI/silc_fsm_next
636  *
637  * SYNOPSIS
638  *
639  *    void silc_fsm_next(void *fsm, SilcFSMStateCallback next_state);
640  *
641  * DESCRIPTION
642  *
643  *    Set the next state to be executed.  If the state function that
644  *    call this function returns SILC_FSM_CONTINUE, the `next_state'
645  *    will be executed immediately.  If it returns SILC_FSM_YIELD it
646  *    yields the thread and the `next_state' will be run after other
647  *    threads have run first.  This function must always be used to set
648  *    the next state in the machine or thread.  This function is used
649  *    with both SilcFSM and SilcFSMThread contexts.
650  *
651  * EXAMPLE
652  *
653  *    // Move to next state
654  *    silc_fsm_next(fsm, next_state);
655  *    return SILC_FSM_CONTINUE;
656  *
657  ***/
658 void silc_fsm_next(void *fsm, SilcFSMStateCallback next_state);
659
660 /****f* silcutil/SilcFSMAPI/silc_fsm_next_later
661  *
662  * SYNOPSIS
663  *
664  *    void silc_fsm_next_later(void *fsm, SilcFSMStateCallback next_state,
665  *                             SilcUInt32 seconds, SilcUInt32 useconds);
666  *
667  * DESCRIPTION
668  *
669  *    Set the next state to be executed later, at the specified time.
670  *    The SILC_FSM_WAIT must be returned in the state function if this
671  *    function is called.  If any other state is returned machine operation
672  *    is undefined.  The machine or thread will move to `next_state' after
673  *    the specified timeout.  This function is used with both SilcFSM and
674  *    SilcFSMThread contexts.
675  *
676  * NOTES
677  *
678  *    If both `seconds' and `useconds' are 0, the effect is same as calling
679  *    silc_fsm_next function, and SILC_FSM_CONTINUE must be returned.
680  *
681  *    If silc_fsm_continue or silc_fsm_continue_sync is called while the
682  *    machine or thread is in SILC_FSM_WAIT state the timeout is automatically
683  *    canceled and the state moves to the next state.
684  *
685  * EXAMPLE
686  *
687  *    // Move to next state after 10 seconds
688  *    silc_fsm_next_later(fsm, next_state, 10, 0);
689  *    return SILC_FSM_WAIT;
690  *
691  ***/
692 void silc_fsm_next_later(void *fsm, SilcFSMStateCallback next_state,
693                          SilcUInt32 seconds, SilcUInt32 useconds);
694
695 /****f* silcutil/SilcFSMAPI/silc_fsm_continue
696  *
697  * SYNOPSIS
698  *
699  *    void silc_fsm_continue(void *fsm);
700  *
701  * DESCRIPTION
702  *
703  *    Continues in the state machine from a SILC_FSM_WAIT state.  This can
704  *    be called from outside waiting FSM to continue to the next state.
705  *    This function can be used instead of SILC_FSM_CALL_CONTINUE macro
706  *    in case the SILC_FSM_CALL was not used.  This must not be used if
707  *    SILC_FSM_CALL was used.  This function is used with both SilcFSM and
708  *    SilcFSMThread contexts.
709  *
710  ***/
711 void silc_fsm_continue(void *fsm);
712
713 /****f* silcutil/SilcFSMAPI/silc_fsm_continue_sync
714  *
715  * SYNOPSIS
716  *
717  *    void silc_fsm_continue_sync(void *fsm);
718  *
719  * DESCRIPTION
720  *
721  *    Continues immediately in the state machine from a SILC_FSM_WAIT state.
722  *    This can be called from outside waiting FSM to immediately continue to
723  *    the next state.  This function can be used instead of the
724  *    SILC_FSM_CALL_CONTINUE_SYNC macro in case the SILC_FSM_CALL was not used.
725  *    This must not be used if SILC_FSM_CALL was used.  This function is used
726  *    with both SilcFSM and SilcFSMThread contexts.
727  *
728  ***/
729 void silc_fsm_continue_sync(void *fsm);
730
731 /****f* silcutil/SilcFSMAPI/silc_fsm_finish
732  *
733  * SYNOPSIS
734  *
735  *    void silc_fsm_finish(void *fsm);
736  *
737  * DESCRIPTION
738  *
739  *    Finishes the `fsm'.  This function may be used in case the FSM
740  *    needs to be finished outside FSM states.  Usually FSM is finished
741  *    by returning SILC_FSM_FINISH from the state, but if this is not
742  *    possible this function may be called.  This function is used with
743  *    both SilcFSM and SilcFSMThread contexts.
744  *
745  *    If the `fsm' is a machine and it has running threads, the machine
746  *    will fatally fail.  The caller must first finish the threads and
747  *    then the machine.
748  *
749  ***/
750 void silc_fsm_finish(void *fsm);
751
752 /****f* silcutil/SilcFSMAPI/silc_fsm_set_context
753  *
754  * SYNOPSIS
755  *
756  *    void silc_fsm_set_context(void *fsm, void *fsm_context);
757  *
758  * DESCRIPTION
759  *
760  *    Set new context for the `fsm'.  This function can be used to change
761  *    the context inside the `fsm', if needed.  This function is used with
762  *    both SilcFSM and SilcFSMThread contexts.  The context is the
763  *    `fsm_context' in the state function (SILC_FSM_STATE).
764  *
765  ***/
766 void silc_fsm_set_context(void *fsm, void *fsm_context);
767
768 /****f* silcutil/SilcFSMAPI/silc_fsm_get_context
769  *
770  * SYNOPSIS
771  *
772  *    void *silc_fsm_get_context(void *fsm);
773  *
774  * DESCRIPTION
775  *
776  *    Returns the context associated with the `fsm'.  It is the context that
777  *    was given to silc_fsm_alloc, silc_fsm_init, silc_fsm_thread_alloc or
778  *    silc_fsm_thread_init.  This function is used with both SilcFSM and
779  *    SilcFSMThread contexts.
780  *
781  ***/
782 void *silc_fsm_get_context(void *fsm);
783
784 /****f* silcutil/SilcFSMAPI/silc_fsm_set_state_context
785  *
786  * SYNOPSIS
787  *
788  *    void silc_fsm_set_state_context(void *fsm, void *state_context);
789  *
790  * DESCRIPTION
791  *
792  *    Set's a state specific context for the `fsm'.  This function can be
793  *    used to change the state context inside the `fsm', if needed.  This
794  *    function is used with both SilcFSM and SilcFSMThread contexts.  The
795  *    context is the `state_context' in the state function (SILC_FSM_STATE).
796  *
797  ***/
798 void silc_fsm_set_state_context(void *fsm, void *state_context);
799
800 /****f* silcutil/SilcFSMAPI/silc_fsm_get_state_context
801  *
802  * SYNOPSIS
803  *
804  *    void *silc_fsm_get_state_context(void *fsm);
805  *
806  * DESCRIPTION
807  *
808  *    Returns the state context associated with the `fsm'.  It is the context
809  *    that was set with silc_fsm_set_state_context function.  This function
810  *    is used with both SilcFSM and SilcFSMThread contexts.
811  *
812  ***/
813 void *silc_fsm_get_state_context(void *fsm);
814
815 /****f* silcutil/SilcFSMAPI/silc_fsm_get_schedule
816  *
817  * SYNOPSIS
818  *
819  *    SilcSchedule silc_fsm_get_schedule(void *fsm);
820  *
821  * DESCRIPTION
822  *
823  *    Returns the SilcSchedule that has been associated with the `fsm'.
824  *    If caller needs scheduler it may retrieve it with this function.  This
825  *    function is used with both SilcFSM and SilcFSMThread contexts.
826  *
827  *    If the `fsm' is thread and real system threads are being used, and this
828  *    is called from the thread, it will return the SilcSchedule that was
829  *    allocated by the FSM for the thread.  It is strongly recommended to
830  *    use this SilcSchedule if you are using real threads, and you need
831  *    scheduler in the thread.  Note that, once the thread finishes the
832  *    returned SilcSchedule becomes invalid.
833  *
834  *    In other times this returns the SilcSchedule pointer that was given
835  *    to silc_fsm_alloc or silc_fsm_init.
836  *
837  ***/
838 SilcSchedule silc_fsm_get_schedule(void *fsm);
839
840 /****f* silcutil/SilcFSMAPI/silc_fsm_get_machine
841  *
842  * SYNOPSIS
843  *
844  *    SilcFSM silc_fsm_get_machine(SilcFSMThread thread);
845  *
846  * DESCRIPTION
847  *
848  *    Returns the machine from the FSM thread indicated by `thread'.
849  *
850  ***/
851 SilcFSM silc_fsm_get_machine(SilcFSMThread thread);
852
853 /****f* silcutil/SilcFSMAPI/silc_fsm_is_started
854  *
855  * SYNOPSIS
856  *
857  *    SilcBool silc_fsm_is_started(void *fsm);
858  *
859  * DESCRIPTION
860  *
861  *    Returns TRUE if the machine or thread `fsm' has been started and has
862  *    not been finished yet.  This function is used with both SilcFSM and
863  *    SilcFSMThread contexts.
864  *
865  ***/
866 SilcBool silc_fsm_is_started(void *fsm);
867
868 /* FSM Events */
869
870 /****s* silcutil/SilcFSMAPI/SilcFSMEvent
871  *
872  * NAME
873  *
874  *    typedef struct SilcFSMEventObject *SilcFSMEvent;
875  *
876  * DESCRIPTION
877  *
878  *    The FSM event context allocated with silc_fsm_event_alloc.  The
879  *    caller must free it with silc_fsm_event_free.  It is also possible
880  *    to use pre-allocated SilcFSMEventStruct instead of SilcFSMEvent context.
881  *
882  ***/
883 typedef struct SilcFSMEventObject *SilcFSMEvent;
884
885 /****s* silcutil/SilcFSMAPI/SilcFSMEventStruct
886  *
887  * NAME
888  *
889  *    typedef struct SilcFSMEventObject SilcFSMEventStruct;
890  *
891  * DESCRIPTION
892  *
893  *    The FSM event context that can be used as pre-allocated context.
894  *    It is initialized with silc_fsm_event_init.  It need not be
895  *    uninitialized.
896  *
897  ***/
898 typedef struct SilcFSMEventObject SilcFSMEventStruct;
899
900 /****f* silcutil/SilcFSMAPI/silc_fsm_event_alloc
901  *
902  * SYNOPSIS
903  *
904  *    SilcFSMEvent silc_fsm_event_alloc(SilcFSM fsm);
905  *
906  * DESCRIPTION
907  *
908  *    Allocates asynchronous FSM event.  FSM events are asynchronous events
909  *    that can be waited and signalled.  They can be used as condition
910  *    variables and signallers.  They can be used for example to wait that
911  *    some event happens, some thread moves to a specific state or similar.
912  *    The FSM Events may also be used in FSM threads that are executed in
913  *    real system threads.  It is safe to wait and signal the event from
914  *    threads.  The `fsm' must be the machine, not a thread.  Returns NULL
915  *    if system is out of memory or `fsm' is not FSM machine.
916  *
917  *    Use the macros SILC_FSM_EVENT_WAIT and SILC_FSM_EVENT_TIMEDWAIT to wait
918  *    for the event.  Use the SILC_FSM_EVENT_SIGNAL macro to signal all the
919  *    waiters.
920  *
921  ***/
922 SilcFSMEvent silc_fsm_event_alloc(SilcFSM fsm);
923
924 /****f* silcutil/SilcFSMAPI/silc_fsm_event_init
925  *
926  * SYNOPSIS
927  *
928  *    void silc_fsm_event_init(SilcFSMEvent event, SilcFSM fsm);
929  *
930  * DESCRIPTION
931  *
932  *    Initializes a pre-allocates FSM event context.  This call is
933  *    equivalent to silc_fsm_event_alloc except this use the pre-allocated
934  *    context.  This fuction does not allocate any memory.  The `fsm'
935  *    must be the machine, not a thread.
936  *
937  ***/
938 void silc_fsm_event_init(SilcFSMEvent event, SilcFSM fsm);
939
940 /****f* silcutil/SilcFSMAPI/silc_fsm_event_free
941  *
942  * SYNOPSIS
943  *
944  *    void silc_fsm_event_free(SilcFSMEvent event);
945  *
946  * DESCRIPTION
947  *
948  *    Free the event allocated by silc_fsm_event_alloc function.
949  *
950  ***/
951 void silc_fsm_event_free(SilcFSMEvent event);
952
953 /****d* silcutil/SilcFSMAPI/SILC_FSM_EVENT_WAIT
954  *
955  * NAME
956  *
957  *    SILC_FSM_EVENT_WAIT(event)
958  *
959  * DESCRIPTION
960  *
961  *    Macro used to wait for the `event' to be signalled.  The machine
962  *    or thread will be suspended while it is waiting for the event.
963  *    This macro can only be used in FSM state functions.  When the
964  *    event is signalled the FSM will re-enter the current state (or
965  *    state that was set with silc_fsm_next before waiting).
966  *
967  * EXAMPLE
968  *
969  *    // Signalling example
970  *    ctx->async_event = silc_fsm_event_alloc(fsm);
971  *    ...
972  *
973  *    SILC_FSM_STATE(silc_foo_state)
974  *    {
975  *      ...
976  *
977  *      // Wait here for async call to complete
978  *      SILC_FSM_EVENT_WAIT(ctx->async_event);
979  *
980  *      // Async call completed
981  *      if (ctx->async_success == FALSE)
982  *        fatal(error);
983  *      ...
984  *    }
985  *
986  ***/
987 #define SILC_FSM_EVENT_WAIT(event)              \
988 do {                                            \
989   if (silc_fsm_event_wait(event, fsm) == 0)     \
990     return SILC_FSM_WAIT;                       \
991 } while(0)
992
993 /****d* silcutil/SilcFSMAPI/SILC_FSM_EVENT_TIMEDWAIT
994  *
995  * NAME
996  *
997  *    SILC_FSM_EVENT_TIMEDWAIT(event, seconds, useconds, timedout)
998  *
999  * DESCRIPTION
1000  *
1001  *    Macro used to wait for the `event' to be signalled, or until
1002  *    the timeout specified by `seconds' and `useconds' has elapsed.  If
1003  *    the timeout occurs before the event is signalled, the machine
1004  *    will wakeup.  The `timedout' is SilcBool pointer and if it is
1005  *    non-NULL indication of whether timeout occurred or not is saved to
1006  *    the pointer.  This macro can only be used in FSM state functions.
1007  *    When the event is signalled or timedout the FSM will re-enter
1008  *    the current state (or state that was set with silc_fsm_next before
1009  *    waiting).
1010  *
1011  * EXAMPLE
1012  *
1013  *    SILC_FSM_STATE(silc_foo_state)
1014  *    {
1015  *      SilcBool timedout;
1016  *      ...
1017  *
1018  *      // Wait here for async call to complete, or 10 seconds for timeout
1019  *      SILC_FSM_EVENT_TIMEDWAIT(ctx->async_event, 10, 0, &timedout);
1020  *
1021  *      // See if timeout occurred
1022  *      if (timedout == TRUE)
1023  *        fatal(error);
1024  *
1025  *      // Async call completed
1026  *      if (ctx->async_success == FALSE)
1027  *        fatal(error);
1028  *      ...
1029  *    }
1030  *
1031  ***/
1032 #define SILC_FSM_EVENT_TIMEDWAIT(event, seconds, useconds, ret_to)      \
1033 do {                                                                    \
1034   if (silc_fsm_event_timedwait(event, fsm, seconds, useconds, ret_to) == 0) \
1035     return SILC_FSM_WAIT;                                               \
1036 } while(0)
1037
1038 /****f* silcutil/SilcFSMAPI/SILC_FSM_EVENT_SIGNAL
1039  *
1040  * SYNOPSIS
1041  *
1042  *    SILC_FSM_EVENT_SIGNAL(event)
1043  *
1044  * DESCRIPTION
1045  *
1046  *    Signals the `event' and awakens everybody that are waiting for this
1047  *    event.  This macro never blocks.  It can be safely called at any place
1048  *    in state function and in asynchronous callbacks or other functions.
1049  *
1050  * EXAMPLE
1051  *
1052  *    SILC_FSM_STATE(silc_foo_async_completion)
1053  *    {
1054  *      ...
1055  *
1056  *      // Notify all waiters
1057  *      ctx->async_success = TRUE;
1058  *      SILC_FSM_EVENT_SIGNAL(ctx->async_event);
1059  *      ...
1060  *    }
1061  *
1062  ***/
1063 #define SILC_FSM_EVENT_SIGNAL(event)            \
1064 do {                                            \
1065   silc_fsm_event_signal(event);                 \
1066 } while(0)
1067
1068 #include "silcfsm_i.h"
1069
1070 #endif /* SILCFSM_H */