Added silc_fsm_finish.
authorPekka Riikonen <priikone@silcnet.org>
Fri, 24 Nov 2006 14:44:58 +0000 (14:44 +0000)
committerPekka Riikonen <priikone@silcnet.org>
Fri, 24 Nov 2006 14:44:58 +0000 (14:44 +0000)
lib/silcutil/silcfsm.c
lib/silcutil/silcfsm.h

index d1b0dff965212ca6996f7d7ef45998978f7ba812..466063d6fe9383160fb530cd05e6747c4386499b 100644 (file)
@@ -20,7 +20,7 @@
 #include "silc.h"
 
 SILC_TASK_CALLBACK(silc_fsm_run);
-SILC_TASK_CALLBACK(silc_fsm_finish);
+SILC_TASK_CALLBACK(silc_fsm_finish_fsm);
 SILC_TASK_CALLBACK(silc_fsm_sema_timedout);
 SILC_TASK_CALLBACK(silc_fsm_start_real_thread);
 static void *silc_fsm_thread(void *context);
@@ -273,6 +273,32 @@ void silc_fsm_continue_sync(void *fsm)
   silc_fsm_run(f->schedule, silc_schedule_get_context(f->schedule), 0, 0, f);
 }
 
+/* Finish FSM */
+
+void silc_fsm_finish(void *fsm)
+{
+  SilcFSM f = fsm;
+
+  SILC_ASSERT(!f->finished);
+  f->finished = TRUE;
+
+  /* If we are thread and using real threads, the FSM thread will finish
+     after the real thread has finished, in the main thread. */
+  if (f->thread && f->real_thread) {
+    /* Stop the real thread's scheduler to finish the thread */
+    silc_schedule_stop(f->schedule);
+    silc_schedule_wakeup(f->schedule);
+    return;
+  }
+
+  /* Normal FSM operation */
+  if (f->synchronous)
+    silc_fsm_finish_fsm(f->schedule, silc_schedule_get_context(f->schedule),
+                       0, 0, fsm);
+  else
+    silc_schedule_task_add_timeout(f->schedule, silc_fsm_finish_fsm, f, 0, 1);
+}
+
 /* Return associated scheduler */
 
 SilcSchedule silc_fsm_get_schedule(void *fsm)
@@ -369,24 +395,7 @@ SILC_TASK_CALLBACK(silc_fsm_run)
   case SILC_FSM_FINISH:
     /* Finish the state machine */
     SILC_LOG_DEBUG(("State finish %p", fsm));
-#if defined(SILC_DEBUG)
-    SILC_ASSERT(!fsm->finished);
-#endif /* SILC_DEBUG */
-    fsm->finished = TRUE;
-
-    /* If we are thread and using real threads, the FSM thread will finish
-       after the real thread has finished, in the main thread. */
-    if (fsm->thread && fsm->real_thread) {
-      silc_schedule_stop(fsm->schedule);
-      break;
-    }
-
-    /* Normal FSM operation */
-    if (fsm->synchronous)
-      silc_fsm_finish(fsm->schedule, app_context, 0, 0, fsm);
-    else
-      silc_schedule_task_add_timeout(fsm->schedule, silc_fsm_finish,
-                                    fsm, 0, 1);
+    silc_fsm_finish(fsm);
     break;
 
   default:
@@ -397,7 +406,7 @@ SILC_TASK_CALLBACK(silc_fsm_run)
 /* Finishes the FSM.  This is always executed in the main thread, even
    for FSM threads that were run in real threads. */
 
-SILC_TASK_CALLBACK(silc_fsm_finish)
+SILC_TASK_CALLBACK(silc_fsm_finish_fsm)
 {
   SilcFSM fsm = context;
 
@@ -454,9 +463,7 @@ SilcFSMSema silc_fsm_sema_alloc(SilcFSM fsm, SilcUInt32 value)
 void silc_fsm_sema_init(SilcFSMSema sema, SilcFSM fsm, SilcUInt32 value)
 {
   SILC_LOG_DEBUG(("Initializing semaphore %p", sema));
-#if defined(SILC_DEBUG)
   SILC_ASSERT(!fsm->thread);
-#endif /* SILC_DEBUG */
   memset(sema, 0, sizeof(*sema));
   sema->fsm = fsm;
   sema->refcnt = 0;
@@ -704,7 +711,8 @@ static void *silc_fsm_thread(void *context)
 
   /* Finish the FSM thread in the main thread */
   SILC_ASSERT(fsm->finished);
-  silc_schedule_task_add_timeout(fsm->schedule, silc_fsm_finish, fsm, 0, 1);
+  silc_schedule_task_add_timeout(fsm->schedule, silc_fsm_finish_fsm,
+                                fsm, 0, 1);
   silc_schedule_wakeup(fsm->schedule);
 
   return NULL;
index 0c7a9679dac458bf62ab201ad91e06b2b491d20d..b7413afee2ca67d152524cf824bbdc5a477d5060 100644 (file)
@@ -679,6 +679,23 @@ void silc_fsm_continue(void *fsm);
  ***/
 void silc_fsm_continue_sync(void *fsm);
 
+/****f* silcutil/SilcFSMAPI/silc_fsm_finish
+ *
+ * SYNOPSIS
+ *
+ *    void silc_fsm_finish(void *fsm);
+ *
+ * DESCRIPTION
+ *
+ *    Finishes the `fsm'.  This function may be used in case the FSM
+ *    needs to be finished outside FSM states.  Usually FSM is finished
+ *    by returning SILC_FSM_FINISH from the state, but if this is not
+ *    possible this function may be called.  This function is used with
+ *    both SilcFSM and SilcFSMThread contexts.
+ *
+ ***/
+void silc_fsm_finish(void *fsm);
+
 /****f* silcutil/SilcFSMAPI/silc_fsm_set_context
  *
  * SYNOPSIS