SILC_FSM_THREAD_WAIT continues to next state if already terminated.
authorPekka Riikonen <priikone@silcnet.org>
Sat, 21 Oct 2006 13:45:45 +0000 (13:45 +0000)
committerPekka Riikonen <priikone@silcnet.org>
Sat, 21 Oct 2006 13:45:45 +0000 (13:45 +0000)
lib/silcutil/silcfsm.c
lib/silcutil/silcfsm.h

index 7a0bf4ac100bcb2a3585d0a68679080114fd3c31..c94425d0647e4bdfe09d4c02356e038f07936f8e 100644 (file)
@@ -318,12 +318,18 @@ void *silc_fsm_get_state_context(void *fsm)
 SilcBool silc_fsm_thread_wait(void *fsm, void *thread)
 {
   SilcFSM t = thread;
+
 #if defined(SILC_DEBUG)
   assert(t->thread);
 #endif /* SILC_DEBUG */
+
+  if (t->finished)
+    return FALSE;
   t->u.t.sema = silc_fsm_sema_alloc(t->u.t.fsm, 0);
   if (!t->u.t.sema)
     return FALSE;
+
+  SILC_LOG_DEBUG(("Waiting for thread %p to terminate", thread));
   silc_fsm_sema_wait(t->u.t.sema, fsm);
   return TRUE;
 }
@@ -649,7 +655,7 @@ static void silc_fsm_thread_termination_post(SilcFSMSema sema)
   SilcFSM fsm;
   SilcMutex lock = sema->fsm->u.m.lock;
 
-  SILC_LOG_DEBUG(("Post thread termination semaphore %p", sema));
+  SILC_LOG_DEBUG(("Post thread terminate semaphore %p", sema));
 
   silc_mutex_lock(lock);
 
index 0eb707c8bd769dc0edf07bdde3a6cb427144bb45..8468c4fd2e2bda1018fc7490b9440e838484cae0 100644 (file)
@@ -307,7 +307,9 @@ do {                                                \
  *
  *    Macro used to wait for the `thread' to terminate.  The machine or
  *    thread will be suspended while it is waiting for the thread to
- *    terminate.
+ *    terminate.  If the thread to be waited has already terminated (but
+ *    the context has not been freed yet), this will continue immediately
+ *    to the following state without waiting.
  *
  * NOTES
  *
@@ -325,8 +327,9 @@ do {                                                \
  ***/
 #define SILC_FSM_THREAD_WAIT(thread)           \
 do {                                           \
-  silc_fsm_thread_wait(fsm, thread);           \
-  return SILC_FSM_WAIT;                                \
+  if (silc_fsm_thread_wait(fsm, thread))       \
+    return SILC_FSM_WAIT;                      \
+  return SILC_FSM_CONTINUE;                    \
 } while(0)
 
 /****f* silcutil/SilcFSMAPI/silc_fsm_alloc