Created SILC Runtime Toolkit git repository Part II.
[runtime.git] / lib / silcutil / silcfsm.c
index 3cef6d969506a8f374feb54fdcb5e3a89029b0cf..bbd297a04fcce2c78b6188aefa387e7f356ee66c 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 2005 - 2007 Pekka Riikonen
+  Copyright (C) 2005 - 2008 Pekka Riikonen
 
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
@@ -17,7 +17,7 @@
 
 */
 
-#include "silc.h"
+#include "silcruntime.h"
 
 SILC_TASK_CALLBACK(silc_fsm_run);
 SILC_TASK_CALLBACK(silc_fsm_finish_fsm);
@@ -59,8 +59,11 @@ SilcBool silc_fsm_init(SilcFSM fsm,
                       SilcSchedule schedule)
 {
   if (!schedule) {
-    silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
-    return FALSE;
+    schedule = silc_schedule_get_global();
+    if (!schedule) {
+      silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
+      return FALSE;
+    }
   }
 
   fsm->fsm_context = fsm_context;
@@ -108,7 +111,7 @@ void silc_fsm_thread_init(SilcFSMThread thread,
   SILC_LOG_DEBUG(("Initializing new thread %p (%s)",
                  thread, real_thread ? "real" : "FSM"));
 
-  SILC_ASSERT(!fsm->thread);
+  SILC_VERIFY(!fsm->thread);
 
   thread->fsm_context = thread_context;
   thread->state_context = NULL;
@@ -317,7 +320,7 @@ void silc_fsm_finish(void *fsm)
 {
   SilcFSM f = fsm;
 
-  SILC_ASSERT(!f->finished);
+  SILC_VERIFY(!f->finished);
 
   f->started = FALSE;
   f->finished = TRUE;
@@ -356,7 +359,7 @@ SilcSchedule silc_fsm_get_schedule(void *fsm)
 
 SilcFSM silc_fsm_get_machine(SilcFSMThread thread)
 {
-  SILC_ASSERT(thread->thread);
+  SILC_VERIFY(thread->thread);
   return (SilcFSM)thread->u.t.fsm;
 }
 
@@ -406,7 +409,7 @@ SilcBool silc_fsm_thread_wait(void *fsm, void *thread)
 {
   SilcFSM t = thread;
 
-  SILC_ASSERT(t->thread);
+  SILC_VERIFY(t->thread);
 
   t->u.t.event = silc_fsm_event_alloc(t->u.t.fsm);
   if (!t->u.t.event)
@@ -517,7 +520,7 @@ SilcFSMEvent silc_fsm_event_alloc(SilcFSM fsm)
 void silc_fsm_event_init(SilcFSMEvent event, SilcFSM fsm)
 {
   SILC_LOG_DEBUG(("Initializing event %p", event));
-  SILC_ASSERT(!fsm->thread);
+  SILC_VERIFY(!fsm->thread);
   memset(event, 0, sizeof(*event));
   event->fsm = fsm;
   event->refcnt = 0;
@@ -767,12 +770,15 @@ 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, silc_schedule_get_stack(old));
+  fsm->schedule = silc_schedule_init(0, old, silc_schedule_get_stack(old), old);
   if (silc_unlikely(!fsm->schedule)) {
     fsm->schedule = old;
     return NULL;
   }
 
+  /* The new scheduler is a global scheduler in this thread */
+  silc_schedule_set_global(fsm->schedule);
+
   /* Start the FSM thread */
   if (silc_unlikely(!silc_schedule_task_add_timeout(fsm->schedule,
                                                    silc_fsm_run, fsm, 0, 0))) {
@@ -784,6 +790,9 @@ void *silc_fsm_thread(void *context)
   /* Run the scheduler */
   silc_schedule(fsm->schedule);
 
+  /* Reset global scheduler */
+  silc_schedule_set_global(NULL);
+
   /* Free resources */
   silc_schedule_uninit(fsm->schedule);