Added SILC_FSM_YIELD.
[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 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 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
29  * threads.
30  *
31  * The FSM provides semaphores because of their versatility.  The FSM
32  * semaphores can be used as mutual exclusion locks to protect critical
33  * sections, and as conditional variables and signallers.  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.
38  *
39  ***/
40
41 #ifndef SILCFSM_H
42 #define SILCFSM_H
43
44 /****s* silcutil/SilcFSMAPI/SilcFSM
45  *
46  * NAME
47  *
48  *    typedef struct SilcFSMObject *SilcFSM;
49  *
50  * DESCRIPTION
51  *
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.
56  *
57  ***/
58 typedef struct SilcFSMObject *SilcFSM;
59
60 /****s* silcutil/SilcFSMAPI/SilcFSMStruct
61  *
62  * NAME
63  *
64  *    typedef struct SilcFSMObject SilcFSMStruct;
65  *
66  * DESCRIPTION
67  *
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.
71  *
72  ***/
73 typedef struct SilcFSMObject SilcFSMStruct;
74
75 /****s* silcutil/SilcFSMAPI/SilcFSMThread
76  *
77  * NAME
78  *
79  *    typedef struct SilcFSMObject *SilcFSMThread;
80  *
81  * DESCRIPTION
82  *
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.
90  *
91  ***/
92 typedef struct SilcFSMObject *SilcFSMThread;
93
94 /****s* silcutil/SilcFSMAPI/SilcFSM
95  *
96  * NAME
97  *
98  *    typedef struct SilcFSMObject SilcFSMThreadStruct;
99  *
100  * DESCRIPTION
101  *
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.
105  *
106  ***/
107 typedef struct SilcFSMObject SilcFSMThreadStruct;
108
109 /****d* silcutil/SilcFSMAPI/SilcFSMStatus
110  *
111  * NAME
112  *
113  *    typedef enum { ... } SilcFSMStatus;
114  *
115  * DESCRIPTION
116  *
117  *    Status values that the FSM state functions return.  They dicatate
118  *    how the machine will behave after returning from the state function.
119  *
120  * SOURCE
121  */
122 typedef enum {
123   SILC_FSM_CONTINUE,         /* Continue immediately to next state */
124   SILC_FSM_YIELD,            /* Continue to next state through scheduler */
125   SILC_FSM_WAIT,             /* Wait for some async call or timeout */
126   SILC_FSM_FINISH,           /* Finish state machine and call destructor
127                                 through scheduler */
128 } SilcFSMStatus;
129 /***/
130
131 /****f* silcutil/SilcFSMAPI/SilcFSMDestructor
132  *
133  * SYNOPSIS
134  *
135  *    typedef void (*SilcFSMDestructor)(SilcFSM fsm, void *fsm_context,
136  *                                      void *destructor_context);
137  *
138  * DESCRIPTION
139  *
140  *    The destructor callback that was set in silc_fsm_alloc or in
141  *    silc_fsm_init function.  It will be called when a state function
142  *    returns SILC_FSM_FINISH.  This function will be called through
143  *    the scheduler; it will not be called immediately after the state
144  *    function returns SILC_FSM_FINISH, but will be called later.
145  *    The `fsm' may be freed in this function.
146  *
147  ***/
148 typedef void (*SilcFSMDestructor)(SilcFSM fsm, void *fsm_context,
149                                   void *destructor_context);
150
151 /****f* silcutil/SilcFSMAPI/SilcFSMThreadDestructor
152  *
153  * SYNOPSIS
154  *
155  *    typedef void (*SilcFSMThreadDestructor)(SilcFSMThread thread,
156  *                                            void *thread_context,
157  *                                            void *destructor_context);
158  *
159  * DESCRIPTION
160  *
161  *    The destructor callback that was set in silc_fsm_thread_alloc or in
162  *    silc_fsm_thread_init function.  It will be called when a state function
163  *    returns SILC_FSM_FINISH.  This function will be called through
164  *    the scheduler; it will not be called immediately after the state
165  *    function returns SILC_FSM_FINISH, but will be called later.  The
166  *    `thread' may be freed in this function.
167  *
168  * NOTES
169  *
170  *    Even if the `thread' was executed in real system thread, this callback
171  *    is always received in the main machine thread, not in the created
172  *    thread.
173  *
174  ***/
175 typedef void (*SilcFSMThreadDestructor)(SilcFSMThread thread,
176                                         void *thread_context,
177                                         void *destructor_context);
178
179 /****d* silcutil/SilcFSMAPI/SILC_FSM_STATE
180  *
181  * NAME
182  *
183  *    #define SILC_FSM_STATE(name)
184  *
185  * DESCRIPTION
186  *
187  *    This macro is used to declare a FSM state function.  The `fsm' is
188  *    the SilcFSM or SilcFSMThread context, the `fsm_context' is the context
189  *    given as argument to silc_fsm_alloc, silc_fsm_init, silc_fsm_thread_init,
190  *    or silc_fsm_thread_alloc function.  The `state_context' is the optional
191  *    state specific context set with silc_fsm_set_state_context function.
192  *
193  * SOURCE
194  */
195 #define SILC_FSM_STATE(name)                                            \
196 SilcFSMStatus name(struct SilcFSMObject *fsm, void *fsm_context,        \
197                    void *state_context)
198 /***/
199
200 /* State function callback */
201 typedef SilcFSMStatus (*SilcFSMStateCallback)(struct SilcFSMObject *fsm,
202                                               void *fsm_context,
203                                               void *state_context);
204
205 /****d* silcutil/SilcFSMAPI/SILC_FSM_CALL
206  *
207  * NAME
208  *
209  *    SILC_FSM_CALL(function)
210  *
211  * DESCRIPTION
212  *
213  *    Macro used to call asynchronous calls from state function.  If the
214  *    call is not really asynchronous then this will cause the machine to
215  *    directly proceed to next state.  If the call is truly asynchronous
216  *    then this will set the machine to wait state.  The silc_fsm_next
217  *    must be called before this macro, so that the next state is set.
218  *
219  * NOTES
220  *
221  *    The state function returns in this macro.
222  *
223  * EXAMPLE
224  *
225  *    // Simple example
226  *    silc_fsm_next(fsm, some_next_state);
227  *    SILC_FSM_CALL(silc_some_async_call(server, some_callback, context));
228  *
229  *    // More complex example
230  *    silc_fsm_next(fsm, some_next_state);
231  *    SILC_FSM_CALL((some_context->operation =
232  *                   silc_some_async_call(server, some_callback, context)));
233  *
234  ***/
235 #define SILC_FSM_CALL(function)                 \
236 do {                                            \
237   assert(!silc_fsm_set_call(fsm, TRUE));        \
238   function;                                     \
239   if (!silc_fsm_set_call(fsm, FALSE))           \
240     return SILC_FSM_CONTINUE;                   \
241   return SILC_FSM_WAIT;                         \
242 } while(0)
243
244 /****d* silcutil/SilcFSMAPI/SILC_FSM_CALL_CONTINUE
245  *
246  * NAME
247  *
248  *    SILC_FSM_CALL_CONTINUE(fsm)
249  *
250  * DESCRIPTION
251  *
252  *    Macro used to proceed after asynchornous call.  This is called in the
253  *    callback of the asynchronous call to continue in the state machine.
254  *
255  * EXAMPLE
256  *
257  *    void some_callback(void *context) {
258  *      SilcFSM fsm = context;
259  *      ...
260  *      // Continue to the next state
261  *      SILC_FSM_CALL_CONTINUE(fsm);
262  *    }
263  *
264  ***/
265 #define SILC_FSM_CALL_CONTINUE(fsm)             \
266 do {                                            \
267   if (!silc_fsm_set_call(fsm, FALSE))           \
268     silc_fsm_continue(fsm);                     \
269 } while(0)
270
271 /****d* silcutil/SilcFSMAPI/SILC_FSM_CALL_CONTINUE_SYNC
272  *
273  * NAME
274  *
275  *    SILC_FSM_CALL_CONTINUE_SYNC(fsm)
276  *
277  * DESCRIPTION
278  *
279  *    Macro used to proceed after asynchornous call.  This is called in the
280  *    callback of the asynchronous call to continue in the state machine.
281  *    This continues to the next state synchronously, not through the
282  *    scheduler.
283  *
284  * EXAMPLE
285  *
286  *    void some_callback(void *context) {
287  *      SilcFSM fsm = context;
288  *      ...
289  *      // Continue to the next state immediately
290  *      SILC_FSM_CALL_CONTINUE_SYNC(fsm);
291  *    }
292  *
293  ***/
294 #define SILC_FSM_CALL_CONTINUE_SYNC(fsm)        \
295 do {                                            \
296   if (!silc_fsm_set_call(fsm, FALSE))           \
297     silc_fsm_continue_sync(fsm);                \
298 } while(0)
299
300 /****d* silcutil/SilcFSMAPI/SILC_FSM_THREAD_WAIT
301  *
302  * NAME
303  *
304  *    SILC_FSM_THREAD_WAIT(thread)
305  *
306  * DESCRIPTION
307  *
308  *    Macro used to wait for the `thread' to terminate.  The machine or
309  *    thread will be suspended while it is waiting for the thread to
310  *    terminate.
311  *
312  * NOTES
313  *
314  *    The state function returns in this macro.
315  *
316  *    This macro is the only way to safely make sure that the thread has
317  *    terminated by the time FSM continues from the waiting state.  Using
318  *    semaphores to signal from the thread before SILC_FSM_FINISH is returned
319  *    works with normal FSM threads, but especially with real system threads
320  *    it does not guarantee that the FSM won't continue before the thread has
321  *    actually terminated.  Usually this is not a problem, but it can be a
322  *    problem if the FSM is waiting to be freed.  In this case using this
323  *    macro is strongly recommended.
324  *
325  ***/
326 #define SILC_FSM_THREAD_WAIT(thread)            \
327 do {                                            \
328   silc_fsm_thread_wait(fsm, thread);            \
329   return SILC_FSM_WAIT;                         \
330 } while(0)
331
332 /****f* silcutil/SilcFSMAPI/silc_fsm_alloc
333  *
334  * SYNOPSIS
335  *
336  *    SilcFSM silc_fsm_alloc(void *fsm_context,
337  *                           SilcFSMDestructor destructor,
338  *                           void *destructor_context,
339  *                           SilcSchedule schedule);
340  *
341  * DESCRIPTION
342  *
343  *    Allocates SILC Finite State Machine context.  The `destructor' with
344  *    `destructor_context' will be called when the machines finishes.  The
345  *    caller must free the returned context with silc_fsm_free.  The
346  *    `fsm_context' is delivered to every FSM state function.  The `schedule'
347  *    is the caller's scheduler and the FSM will be run in the scheduler.
348  *
349  * EXAMPLE
350  *
351  *    SilcAsyncOperation silc_async_call(Callback callback, void *cb_context)
352  *    {
353  *      SilcAsyncOperation op;
354  *      SilcFSM fsm;
355  *      ...
356  *
357  *      // Allocate async operation so that caller can control us, like abort
358  *      op = silc_async_alloc(silc_async_call_abort, NULL, ourcontext);
359  *
360  *      // Start FSM
361  *      fsm = silc_fsm_alloc(ourcontext, fsm_destructor, ourcontext,
362  *                           schedule);
363  *      silc_fsm_start(fsm, first_state);
364  *      ...
365  *
366  *      // Return async operation for upper layer
367  *      return op;
368  *    }
369  *
370  ***/
371 SilcFSM silc_fsm_alloc(void *fsm_context,
372                        SilcFSMDestructor destructor,
373                        void *destructor_context,
374                        SilcSchedule schedule);
375
376 /****f* silcutil/SilcFSMAPI/silc_fsm_init
377  *
378  * SYNOPSIS
379  *
380  *    SilcBool silc_fsm_init(SilcFSM fsm,
381  *                           void *fsm_context,
382  *                           SilcFSMDestructor destructor,
383  *                           void *destructor_context,
384  *                           SilcSchedule schedule);
385  *
386  * DESCRIPTION
387  *
388  *    Initializes a pre-allocated SilcFSM context.  This call is equivalent
389  *    to silc_fsm_alloc except that this takes the pre-allocated context
390  *    as argument.  The silc_fsm_free must not be called if this was called.
391  *    Returns TRUE if the initialization is Ok or FALSE if error occurred.
392  *    This function does not allocate any memory.  The `schedule' is the
393  *    caller's scheduler and the FSM will be run in the scheduler.
394  *
395  * EXAMPLE
396  *
397  *    SilcFSMStruct fsm;
398  *
399  *    silc_fsm_init(&fsm, application, fsm_destructor, application, schedule);
400  *    silc_fsm_start(&fsm, first_state);
401  *
402  ***/
403 SilcBool silc_fsm_init(SilcFSM fsm,
404                        void *fsm_context,
405                        SilcFSMDestructor destructor,
406                        void *destructor_context,
407                        SilcSchedule schedule);
408
409 /****f* silcutil/SilcFSMAPI/silc_fsm_thread_alloc
410  *
411  * SYNOPSIS
412  *
413  *    SilcFSMThread silc_fsm_thread_alloc(SilcFSM fsm,
414  *                                        void *thread_context,
415  *                                        SilcFSMThreadDestructor destructor,
416  *                                        void *destructor_context,
417  *                                        SilcBool real_thread);
418  *
419  * DESCRIPTION
420  *
421  *    Allocates FSM thread context.  The thread will be executed in the
422  *    FSM machine indicated by `fsm'.  The caller must free the returned
423  *    thread context with silc_fsm_free.  If the 'real_thread' is TRUE
424  *    then the thread will actually be executed in real thread, if platform
425  *    supports them.  The `thread_context' is delivered to every state
426  *    function in the thread.
427  *
428  * NOTES
429  *
430  *    Note the limitations on using `real_thread' boolean to indicate running
431  *    the FSM thread in a real system thread:
432  *
433  *    If the system does not support threads, then this function will revert
434  *    back to normal FSM threads.
435  *
436  *    If the `real_thread' is TRUE then FSM will allocate new SilcSchedule
437  *    for the FSM thread.  This is done because the SilcSchedule that the
438  *    `fsm' use cannot be used in the thread.  This is limitation in the
439  *    SilcSchedule implementation.  If you need scheduler in the real thread
440  *    it is strongly recommended that you use the SilcSchedule that is
441  *    allocated for the thread.  You can retrieve the SilcSchedule from the
442  *    thread using silc_fsm_get_schedule function.  Note that, the allocated
443  *    SilcSchedule will become invalid after the thread finishes.
444  *
445  *    You may still however use the original SilcSchedule if you wish.  In
446  *    this case note its limitation: you may only add and/or remove tasks,
447  *    tasks cannot be executed in the thread.  You will need to deliver the
448  *    original SilcSchedule to the thread in the `thread_context' if you wish
449  *    to use it.
450  *
451  *    If `real_thread' is FALSE then no limitations on what can be run in
452  *    the thread exist.  In this case silc_fsm_get_schedule will return
453  *    the SilcSchedule that was originally given to silc_fsm_alloc or
454  *    silc_fsm_init.
455  *
456  * EXAMPLE
457  *
458  *    SILC_FSM_STATE(silc_foo_state)
459  *    {
460  *      SilcFSMThread thread;
461  *      ...
462  *
463  *      // Execute the route lookup in thread
464  *      thread = silc_fsm_thread_alloc(fsm, fsm_context, NULL, NULL, FALSE);
465  *      silc_fsm_start(thread, silc_route_lookup_start);
466  *
467  *      // Wait here for the thread to terminate. Set the state where to go
468  *      // after the thread has terminated.
469  *      silc_fsm_next(fsm, silc_foo_route_lookup_finished);
470  *      SILC_FSM_THREAD_WAIT(thread);
471  *    }
472  *
473  ***/
474 SilcFSMThread silc_fsm_thread_alloc(SilcFSM fsm,
475                                     void *thread_context,
476                                     SilcFSMThreadDestructor destructor,
477                                     void *destructor_context,
478                                     SilcBool real_thread);
479
480 /****f* silcutil/SilcFSMAPI/silc_fsm_thread_init
481  *
482  * SYNOPSIS
483  *
484  *    void silc_fsm_thread_init(SilcFSMThread thread,
485  *                              SilcFSM fsm,
486  *                              void *thread_context,
487  *                              SilcFSMThreadDestructor destructor,
488  *                              void *destructor_context,
489  *                              SilcBool real_thread);
490  *
491  * DESCRIPTION
492  *
493  *    Initializes a pre-allocated SilcFSMThread context.  This call is
494  *    equivalent to silc_fsm_thread_alloc except that this takes the
495  *    pre-allocated context as argument.  The silc_fsm_free must not be
496  *    called if this was called.  If the `real_thread' is TRUE then the
497  *    thread will actually be executed in real thread, if platform supports
498  *    them.
499  *
500  * NOTES
501  *
502  *    See the notes from the silc_fsm_thread_alloc.
503  *
504  * EXAMPLE
505  *
506  *    SilcFSMThreadStruct thread;
507  *
508  *    silc_fsm_thread_init(&thread, fsm, application, NULL, NULL, FALSE);
509  *    silc_fsm_start(&thread, first_state);
510  *
511  ***/
512 void silc_fsm_thread_init(SilcFSMThread thread,
513                           SilcFSM fsm,
514                           void *thread_context,
515                           SilcFSMThreadDestructor destructor,
516                           void *destructor_context,
517                           SilcBool real_thread);
518
519 /****f* silcutil/SilcFSMAPI/silc_fsm_free
520  *
521  * SYNOPSIS
522  *
523  *    void silc_fsm_free(void *fsm);
524  *
525  * DESCRIPTION
526  *
527  *    Free the SILC FSM context that was allocated with silc_fsm_alloc,
528  *    or free the SILC FSM thread context that was allocated with
529  *    silc_fsm_thread_alloc.  This function is used with both SilcFSM
530  *    and SilcFSMThread contexts.
531  *
532  * NOTES
533  *
534  *    When freeing FSM, it must not have any active threads.
535  *
536  ***/
537 void silc_fsm_free(void *fsm);
538
539 /****f* silcutil/SilcFSMAPI/silc_fsm_start
540  *
541  * SYNOPSIS
542  *
543  *    void silc_fsm_start(void *fsm, SilcFSMStateCallback start_state);
544  *
545  * DESCRIPTION
546  *
547  *    This function must be called after the SILC FSM context was created.
548  *    This actually starts the state machine.  Note that, the machine is
549  *    started later after this function returns.  The `start_state' is the
550  *    state where the machine or thread is started.  This function is used
551  *    with both SilcFSM and SilcFSMThread contexts.
552  *
553  * EXAMPLE
554  *
555  *    SilcFSM fsm;
556  *
557  *    fsm = silc_fsm_alloc(context, destructor, context, schedule);
558  *    silc_fsm_start(fsm, first_state);
559  *
560  ***/
561 void silc_fsm_start(void *fsm, SilcFSMStateCallback start_state);
562
563 /****f* silcutil/SilcFSMAPI/silc_fsm_start_sync
564  *
565  * SYNOPSIS
566  *
567  *    void silc_fsm_start_sync(void *fsm, SilcFSMStateCallback start_state);
568  *
569  * DESCRIPTION
570  *
571  *    This function is same as silc_fsm_start, except that the FSM will
572  *    be started immediately inside this function.  After this function
573  *    returns the `start_state' has already been executed.  If the machine
574  *    is completely synchronous (no waiting used in the machine) then
575  *    the machine will have finished once this function returns.  Also
576  *    note that if the machine is completely synchronous the destructor
577  *    will also be called from inside this function.  This function is used
578  *    with both SilcFSM and SilcFSMThread contexts.
579  *
580  ***/
581 void silc_fsm_start_sync(void *fsm, SilcFSMStateCallback start_state);
582
583 /****f* silcutil/SilcFSMAPI/silc_fsm_next
584  *
585  * SYNOPSIS
586  *
587  *    void silc_fsm_next(void *fsm, SilcFSMStateCallback next_state);
588  *
589  * DESCRIPTION
590  *
591  *    Set the next state to be executed.  If the state function that
592  *    call this function returns SILC_FSM_CONTINUE, the `next_state'
593  *    will be executed immediately.  If it returns SILC_FSM_YIELD it
594  *    yields the thread and the `next_state' will be run after other
595  *    threads have run first.  This function must always be used to set
596  *    the next state in the machine or thread.  This function is used
597  *    with both SilcFSM and SilcFSMThread contexts.
598  *
599  * EXAMPLE
600  *
601  *    // Move to next state
602  *    silc_fsm_next(fsm, next_state);
603  *    return SILC_FSM_CONTINUE;
604  *
605  ***/
606 void silc_fsm_next(void *fsm, SilcFSMStateCallback next_state);
607
608 /****f* silcutil/SilcFSMAPI/silc_fsm_next_later
609  *
610  * SYNOPSIS
611  *
612  *    void silc_fsm_next_later(void *fsm, SilcFSMStateCallback next_state,
613  *                             SilcUInt32 seconds, SilcUInt32 useconds);
614  *
615  * DESCRIPTION
616  *
617  *    Set the next state to be executed later, at the specified time.
618  *    The SILC_FSM_WAIT must be returned in the state function if this
619  *    function is called.  If any other state is returned machine operation
620  *    is undefined.  The machine or thread will move to `next_state' after
621  *    the specified timeout.  This function is used with both SilcFSM and
622  *    SilcFSMThread contexts.
623  *
624  * NOTES
625  *
626  *    If both `seconds' and `useconds' are 0, the effect is same as calling
627  *    silc_fsm_next function, and SILC_FSM_CONTINUE must be returned.
628  *
629  * EXAMPLE
630  *
631  *    // Move to next state after 10 seconds
632  *    silc_fsm_next_later(fsm, next_state, 10, 0);
633  *    return SILC_FSM_WAIT;
634  *
635  ***/
636 void silc_fsm_next_later(void *fsm, SilcFSMStateCallback next_state,
637                          SilcUInt32 seconds, SilcUInt32 useconds);
638
639 /****f* silcutil/SilcFSMAPI/silc_fsm_continue
640  *
641  * SYNOPSIS
642  *
643  *    void silc_fsm_continue(void *fsm);
644  *
645  * DESCRIPTION
646  *
647  *    Continues in the state machine from a SILC_FSM_WAIT state.  This can
648  *    be called from outside waiting FSM to continue to the next state.
649  *    This function can be used instead of SILC_FSM_CALL_CONTINUE macro
650  *    in case the SILC_FSM_CALL was not used.  This must not be used if
651  *    SILC_FSM_CALL was used.  This function is used with both SilcFSM and
652  *    SilcFSMThread contexts.
653  *
654  ***/
655 void silc_fsm_continue(void *fsm);
656
657 /****f* silcutil/SilcFSMAPI/silc_fsm_continue_sync
658  *
659  * SYNOPSIS
660  *
661  *    void silc_fsm_continue_sync(void *fsm);
662  *
663  * DESCRIPTION
664  *
665  *    Continues immediately in the state machine from a SILC_FSM_WAIT state.
666  *    This can be called from outside waiting FSM to immediately continue to
667  *    the next state.  This function can be used instead of the
668  *    SILC_FSM_CALL_CONTINUE_SYNC macro in case the SILC_FSM_CALL was not used.
669  *    This must not be used if SILC_FSM_CALL was used.  This function is used
670  *    with both SilcFSM and SilcFSMThread contexts.
671  *
672  ***/
673 void silc_fsm_continue_sync(void *fsm);
674
675 /****f* silcutil/SilcFSMAPI/silc_fsm_set_context
676  *
677  * SYNOPSIS
678  *
679  *    void silc_fsm_set_context(void *fsm, void *fsm_context);
680  *
681  * DESCRIPTION
682  *
683  *    Set new context for the `fsm'.  This function can be used to change
684  *    the context inside the `fsm', if needed.  This function is used with
685  *    both SilcFSM and SilcFSMThread contexts.  The context is the
686  *    `fsm_context' in the state function (SILC_FSM_STATE).
687  *
688  ***/
689 void silc_fsm_set_context(void *fsm, void *fsm_context);
690
691 /****f* silcutil/SilcFSMAPI/silc_fsm_get_context
692  *
693  * SYNOPSIS
694  *
695  *    void *silc_fsm_get_context(void *fsm);
696  *
697  * DESCRIPTION
698  *
699  *    Returns the context associated with the `fsm'.  It is the context that
700  *    was given to silc_fsm_alloc, silc_fsm_init, silc_fsm_thread_alloc or
701  *    silc_fsm_thread_init.  This function is used with both SilcFSM and
702  *    SilcFSMThread contexts.
703  *
704  ***/
705 void *silc_fsm_get_context(void *fsm);
706
707 /****f* silcutil/SilcFSMAPI/silc_fsm_set_state_context
708  *
709  * SYNOPSIS
710  *
711  *    void silc_fsm_set_state_context(void *fsm, void *state_context);
712  *
713  * DESCRIPTION
714  *
715  *    Set's a state specific context for the `fsm'.  This function can be
716  *    used to change the state context inside the `fsm', if needed.  This
717  *    function is used with both SilcFSM and SilcFSMThread contexts.  The
718  *    context is the `state_context' in the state function (SILC_FSM_STATE).
719  *
720  ***/
721 void silc_fsm_set_state_context(void *fsm, void *state_context);
722
723 /****f* silcutil/SilcFSMAPI/silc_fsm_get_state_context
724  *
725  * SYNOPSIS
726  *
727  *    void *silc_fsm_get_state_context(void *fsm);
728  *
729  * DESCRIPTION
730  *
731  *    Returns the state context associated with the `fsm'.  It is the context
732  *    that was set with silc_fsm_set_state_context function.  This function
733  *    is used with both SilcFSM and SilcFSMThread contexts.
734  *
735  ***/
736 void *silc_fsm_get_state_context(void *fsm);
737
738 /****f* silcutil/SilcFSMAPI/silc_fsm_get_schedule
739  *
740  * SYNOPSIS
741  *
742  *    SilcSchedule silc_fsm_get_schedule(void *fsm);
743  *
744  * DESCRIPTION
745  *
746  *    Returns the SilcSchedule that has been associated with the `fsm'.
747  *    If caller needs scheduler it may retrieve it with this function.  This
748  *    function is used with both SilcFSM and SilcFSMThread contexts.
749  *
750  *    If the `fsm' is thread and real system threads are being used, and this
751  *    is called from the thread, it will return the SilcSchedule that was
752  *    allocated by the FSM for the thread.  It is strongly recommended to
753  *    use this SilcSchedule if you are using real threads, and you need
754  *    scheduler in the thread.  Note that, once the thread finishes the
755  *    returned SilcSchedule becomes invalid.
756  *
757  *    In other times this returns the SilcSchedule pointer that was given
758  *    to silc_fsm_alloc or silc_fsm_init.
759  *
760  ***/
761 SilcSchedule silc_fsm_get_schedule(void *fsm);
762
763 /****f* silcutil/SilcFSMAPI/silc_fsm_get_machine
764  *
765  * SYNOPSIS
766  *
767  *    SilcFSM silc_fsm_get_machine(SilcFSMThread thread);
768  *
769  * DESCRIPTION
770  *
771  *    Returns the machine from the FSM thread indicated by `thread'.
772  *
773  ***/
774 SilcFSM silc_fsm_get_machine(SilcFSMThread thread);
775
776
777 /* FSM Semaphores */
778
779 /****s* silcutil/SilcFSMAPI/SilcFSMSema
780  *
781  * NAME
782  *
783  *    typedef struct SilcFSMSemaObject *SilcFSMSema;
784  *
785  * DESCRIPTION
786  *
787  *    The FSM semaphore context allocated with silc_fsm_sema_alloc.  The
788  *    caller must free it with silc_fsm_sema_free.  It is also possible
789  *    to use pre-allocated SilcFSMSemaStruct instead of SilcFSMSema context.
790  *
791  ***/
792 typedef struct SilcFSMSemaObject *SilcFSMSema;
793
794 /****s* silcutil/SilcFSMAPI/SilcFSMSemaStruct
795  *
796  * NAME
797  *
798  *    typedef struct SilcFSMSemaObject SilcFSMSemaStruct;
799  *
800  * DESCRIPTION
801  *
802  *    The FSM semaphore context that can be used as pre-allocated context.
803  *    It is initialized with silc_fsm_sema_init.  It need not be
804  *    uninitialized.
805  *
806  ***/
807 typedef struct SilcFSMSemaObject SilcFSMSemaStruct;
808
809 /****f* silcutil/SilcFSMAPI/silc_fsm_sema_alloc
810  *
811  * SYNOPSIS
812  *
813  *    SilcFSMSema silc_fsm_sema_alloc(SilcFSM fsm, SilcUInt32 value);
814  *
815  * DESCRIPTION
816  *
817  *    Allocates FSM semaphore with initial value of `value'.  Semaphores are
818  *    counters for resources shared between machine and threads.  Semaphores
819  *    can be waited until the semaphore value is non-zero.  The FSM will be
820  *    suspended when waiting for semaphore.  When the semaphore is incremented
821  *    all that are waiting for the semaphore will be signalled and awaken.
822  *
823  *    Semaphores can be used to wait for example when thread terminates, or
824  *    when thread moves into a specific state, or to protect critical
825  *    sections.  The FSM semaphores can be used also in FSM threads that are
826  *    executed in real system threads.
827  *
828  *    Use the macros SILC_FSM_SEMA_WAIT and SILC_FSM_SEMA_TIMEDWAIT to wait
829  *    for semaphore.  Use the SILC_FSM_SEMA_POST macro to increment the
830  *    counter and wake up all waiters.
831  *
832  ***/
833 SilcFSMSema silc_fsm_sema_alloc(SilcFSM fsm, SilcUInt32 value);
834
835 /****f* silcutil/SilcFSMAPI/silc_fsm_sema_init
836  *
837  * SYNOPSIS
838  *
839  *    void silc_fsm_sema_init(SilcFSMSema sema, SilcFSM fsm, SilcUInt32 value);
840  *
841  * DESCRIPTION
842  *
843  *    Initializes a pre-allocates semaphore context.  This call is
844  *    equivalent to silc_fsm_sema_alloc except this use the pre-allocated
845  *    context.  This fuction does not allocate any memory.
846  *
847  ***/
848 void silc_fsm_sema_init(SilcFSMSema sema, SilcFSM fsm, SilcUInt32 value);
849
850 /****f* silcutil/SilcFSMAPI/silc_fsm_sema_free
851  *
852  * SYNOPSIS
853  *
854  *    void silc_fsm_sema_free(SilcFSMSema sema);
855  *
856  * DESCRIPTION
857  *
858  *    Free the semaphore allocated by silc_fsm_sema_alloc function.
859  *
860  ***/
861 void silc_fsm_sema_free(SilcFSMSema sema);
862
863 /****d* silcutil/SilcFSMAPI/SILC_FSM_SEMA_WAIT
864  *
865  * NAME
866  *
867  *    SILC_FSM_SEMA_WAIT(semaphore)
868  *
869  * DESCRIPTION
870  *
871  *    Macro used to wait for the `semaphore' to become non-zero.  The
872  *    machine will be suspended while it is waiting for the semaphore.
873  *    This macro can only be used in FSM state functions.  When the
874  *    semaphore is signalled the FSM will re-enter the current state (or
875  *    state that was set with silc_fsm_next before waiting).
876  *
877  * EXAMPLE
878  *
879  *    // Signalling example
880  *    ctx->async_sema = silc_fsm_sema_alloc(fsm, 0);
881  *    ...
882  *
883  *    SILC_FSM_STATE(silc_foo_state)
884  *    {
885  *      ...
886  *
887  *      // Wait here for async call to complete
888  *      SILC_FSM_SEMA_WAIT(ctx->async_sema);
889  *
890  *      // Async call completed
891  *      if (ctx->async_success == FALSE)
892  *        fatal(error);
893  *      ...
894  *    }
895  *
896  *    // Mutual exclusion example
897  *    ctx->lock = silc_fsm_sema_alloc(fsm, 1);
898  *    ...
899  *
900  *    SILC_FSM_STATE(silc_foo_state)
901  *    {
902  *      ...
903  *      SILC_FSM_SEMA_WAIT(ctx->lock);
904  *      very critical stuff...
905  *      SILC_FSM_SEMA_POST(ctx->lock);
906  *      ...
907  *    }
908  *
909  ***/
910 #define SILC_FSM_SEMA_WAIT(sema)                \
911 do {                                            \
912   if (silc_fsm_sema_wait(sema, fsm) == 0)       \
913     return SILC_FSM_WAIT;                       \
914 } while(0)
915
916 /****d* silcutil/SilcFSMAPI/SILC_FSM_SEMA_TIMEDWAIT
917  *
918  * NAME
919  *
920  *    SILC_FSM_SEMA_TIMEDWAIT(semaphore, seconds, useconds, timedout)
921  *
922  * DESCRIPTION
923  *
924  *    Macro used to wait for the `semaphore' to become non-zero, or until
925  *    the timeout specified by `seconds' and `useconds' has elapsed.  If
926  *    the timeout occurs before the semaphore becomes non-zero, the machine
927  *    will wakeup.  The `timedout' is SilcBool pointer and if it is
928  *    non-NULL indication of whether timeout occurred or not is saved to
929  *    the pointer.  This macro can only be used in FSM state functions.
930  *    When the semaphore is signalled or timedout the FSM will re-enter
931  *    the current state (or state that was set with silc_fsm_next before
932  *    waiting).
933  *
934  * EXAMPLE
935  *
936  *    SILC_FSM_STATE(silc_foo_state)
937  *    {
938  *      SilcBool timedout;
939  *      ...
940  *
941  *      // Wait here for async call to complete, or 10 seconds for timeout
942  *      SILC_FSM_SEMA_TIMEDWAIT(ctx->async_sema, 10, 0, &timedout);
943  *
944  *      // See if timeout occurred
945  *      if (timedout == TRUE)
946  *        fatal(error);
947  *
948  *      // Async call completed
949  *      if (ctx->async_success == FALSE)
950  *        fatal(error);
951  *      ...
952  *    }
953  *
954  ***/
955 #define SILC_FSM_SEMA_TIMEDWAIT(sema, seconds, useconds, ret_to)          \
956 do {                                                                      \
957   if (silc_fsm_sema_timedwait(sema, fsm, seconds, useconds, ret_to) == 0) \
958     return SILC_FSM_WAIT;                                                 \
959 } while(0)
960
961 /****f* silcutil/SilcFSMAPI/SILC_FSM_SEMA_POST
962  *
963  * SYNOPSIS
964  *
965  *    SILC_FSM_SEMA_POST(semaphore)
966  *
967  * DESCRIPTION
968  *
969  *    Increases the semaphore counter and awakens everybody that are
970  *    waiting for this semaphore.  This macro never blocks.  It can be
971  *    safely called at any place in state function and in asynchronous
972  *    callbacks or other functions.
973  *
974  * EXAMPLE
975  *
976  *    SILC_FSM_STATE(silc_foo_async_completion)
977  *    {
978  *      ...
979  *
980  *      // Notify all waiters
981  *      ctx->async_success = TRUE;
982  *      SILC_FSM_SEMA_POST(ctx->async_sema);
983  *      ...
984  *    }
985  *
986  ***/
987 #define SILC_FSM_SEMA_POST(sema)                \
988 do {                                            \
989   silc_fsm_sema_post(sema);                     \
990 } while(0)
991
992 #include "silcfsm_i.h"
993
994 #endif /* SILCFSM_H */