Added SILC errno API. Added SilcResult, generic error code and
[silc.git] / lib / silcutil / silcfsm.c
index 3382713ed50276c90c1c84975727f901dc113a31..3cef6d969506a8f374feb54fdcb5e3a89029b0cf 100644 (file)
@@ -58,8 +58,10 @@ SilcBool silc_fsm_init(SilcFSM fsm,
                       void *destructor_context,
                       SilcSchedule schedule)
 {
-  if (!schedule)
+  if (!schedule) {
+    silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
     return FALSE;
+  }
 
   fsm->fsm_context = fsm_context;
   fsm->state_context = NULL;
@@ -481,7 +483,7 @@ SILC_TASK_CALLBACK(silc_fsm_finish_fsm)
 
   } else {
     /* Machine must not have active threads */
-    assert(silc_atomic_get_int32(&fsm->u.m.threads) == 0);
+    SILC_VERIFY(silc_atomic_get_int32(&fsm->u.m.threads) == 0);
 
     if (fsm->u.m.lock) {
       silc_mutex_free(fsm->u.m.lock);
@@ -765,14 +767,19 @@ void *silc_fsm_thread(void *context)
   /* We allocate new SilcSchedule for the FSM, as the old SilcSchedule
      cannot be used in this thread.  Application may still use it if it
      wants but we use our own. */
-  fsm->schedule = silc_schedule_init(0, old);
-  if (silc_unlikely(!fsm->schedule))
+  fsm->schedule = silc_schedule_init(0, old, silc_schedule_get_stack(old));
+  if (silc_unlikely(!fsm->schedule)) {
+    fsm->schedule = old;
     return NULL;
+  }
 
   /* Start the FSM thread */
   if (silc_unlikely(!silc_schedule_task_add_timeout(fsm->schedule,
-                                                   silc_fsm_run, fsm, 0, 0)))
+                                                   silc_fsm_run, fsm, 0, 0))) {
+    silc_schedule_uninit(fsm->schedule);
+    fsm->schedule = old;
     return NULL;
+  }
 
   /* Run the scheduler */
   silc_schedule(fsm->schedule);