Added.
[silc.git] / lib / silcutil / silcfsm.c
index 5139c0cd7f7be2f2c0f7b45f1e76dd1d7752da8d..d87e0406cd5d83b14f4c34c8d0e8104899dfa04e 100644 (file)
@@ -151,6 +151,9 @@ SILC_TASK_CALLBACK(silc_fsm_free_final)
   if (f->thread && f->u.t.event)
     silc_fsm_event_free(f->u.t.event);
 
+  if (!f->thread)
+    silc_atomic_uninit32(&f->u.m.threads);
+
   silc_free(f);
 }
 
@@ -314,10 +317,6 @@ void silc_fsm_finish(void *fsm)
 
   SILC_ASSERT(!f->finished);
 
-  /* Machine must not have active threads */
-  if (!f->thread && silc_atomic_get_int32(&f->u.m.threads))
-    assert(silc_atomic_get_int32(&f->u.m.threads) == 0);
-
   f->started = FALSE;
   f->finished = TRUE;
 
@@ -481,11 +480,13 @@ SILC_TASK_CALLBACK(silc_fsm_finish_fsm)
       fsm->destructor(fsm, fsm->fsm_context, fsm->destructor_context);
 
   } else {
+    /* Machine must not have active threads */
+    assert(silc_atomic_get_int32(&fsm->u.m.threads) == 0);
+
     if (fsm->u.m.lock) {
       silc_mutex_free(fsm->u.m.lock);
       fsm->u.m.lock = NULL;
     }
-    silc_atomic_uninit32(&fsm->u.m.threads);
 
     /* Call the destructor callback. */
     if (fsm->destructor)
@@ -764,14 +765,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);