Index: trunk/kernel/kern/scheduler.c
===================================================================
--- trunk/kernel/kern/scheduler.c	(revision 436)
+++ trunk/kernel/kern/scheduler.c	(revision 437)
@@ -97,20 +97,28 @@
     list_entry_t * current;
     list_entry_t * last;
+    list_entry_t * root;
+    bool_t         done;
 
     // take lock protecting sheduler lists
     spinlock_lock( &sched->lock );
 
-    // first loop : scan the kernel threads list if not empty 
+    // first : scan the kernel threads list if not empty 
     if( list_is_empty( &sched->k_root ) == false )
     {
+        root    = &sched->k_root;
         last    = sched->k_last;
-        current = sched->k_last;
-        do
+        current = last;
+        done    = false;
+
+        while( done == false )
         {
             // get next entry in kernel list
-            current = list_next( &sched->k_root , current );
+            current = current->next;
+
+            // check exit condition
+            if( current == last ) done = true;
 
             // skip the root that does not contain a thread
-            if( current == NULL ) current = sched->k_root.next;
+            if( current == root ) continue;
 
             // get thread pointer for this entry
@@ -120,8 +128,5 @@
             switch( thread->type )
             {
-                case THREAD_IDLE: // skip IDLE thread
-                break;
-
-                case THREAD_RPC:  // RPC thread if non blocked and FIFO non-empty
+                case THREAD_RPC:  // if non blocked and RPC FIFO non-empty
                 if( (thread->blocked == 0) && 
                     (local_fifo_is_empty( &LOCAL_CLUSTER->rpc_fifo ) == 0) )
@@ -132,5 +137,5 @@
                 break;
 
-                default:          // DEV thread if non blocked and waiting queue non empty
+                case THREAD_DEV:  // if non blocked and waiting queue non empty
                 if( (thread->blocked == 0) &&
                     (xlist_is_empty( XPTR( local_cxy , &thread->chdev->wait_root)) == 0) ) 
@@ -140,21 +145,29 @@
                 }
                 break;
-            }  // end switch type
-        }
-        while( current != last );
-    }
-
-    // second loop : scan the user threads list if not empty 
+
+                default:
+                break;
+            }
+        } // end loop on kernel threads
+    } // end if kernel threads
+
+    // second : scan the user threads list if not empty 
     if( list_is_empty( &sched->u_root ) == false )
     {
+        root    = &sched->u_root;
         last    = sched->u_last;
-        current = sched->u_last;
-        do
+        current = last;
+        done    = false;
+
+        while( done == false )
         {
             // get next entry in user list
-            current = list_next( &sched->u_root , current );
+            current = current->next;
+
+            // check exit condition
+            if( current == last ) done = true;
 
             // skip the root that does not contain a thread
-            if( current == NULL ) current = sched->u_root.next;
+            if( current == root ) continue;
 
             // get thread pointer for this entry
@@ -167,9 +180,8 @@
                 return thread;
             }
-        }
-        while( current != last );
-    }
-
-    // third : return idle thread if no runnable thread
+        } // end loop on user threads
+    } // end if user threads
+
+    // third : return idle thread if no other runnable thread
     spinlock_unlock( &sched->lock );
     return sched->idle;
@@ -180,4 +192,5 @@
 void sched_handle_signals( core_t * core )
 {
+
     list_entry_t * iter;
     thread_t     * thread;
@@ -214,4 +227,10 @@
             process = thread->process;
 
+#if CONFIG_DEBUG_SCHED_HANDLE_SIGNALS
+uint32_t cycle = (uint32_t)hal_get_cycles();
+if( CONFIG_DEBUG_SCHED_HANDLE_SIGNALS < cycle )
+printk("\n[DBG] %s : thread %x in proces %x must be deleted / cycle %d\n",
+__FUNCTION__ , thread , process->pid , cycle );
+#endif
 	        // release FPU if required
 	        if( thread->core->fpu_owner == thread )  thread->core->fpu_owner = NULL;
@@ -232,8 +251,8 @@
 
 #if CONFIG_DEBUG_SCHED_HANDLE_SIGNALS
-uint32_t cycle = (uint32_t)hal_get_cycles();
+cycle = (uint32_t)hal_get_cycles();
 if( CONFIG_DEBUG_SCHED_HANDLE_SIGNALS < cycle )
-printk("\n[DBG] %s : thread %x deleted thread %x / cycle %d\n",
-__FUNCTION__ , CURRENT_THREAD , thread , cycle );
+printk("\n[DBG] %s : thread %x in process %x has been deleted / cycle %d\n",
+__FUNCTION__ , thread , process->pid , cycle );
 #endif
             // destroy process descriptor if no more threads
@@ -246,6 +265,6 @@
 cycle = (uint32_t)hal_get_cycles();
 if( CONFIG_DEBUG_SCHED_HANDLE_SIGNALS < cycle )
-printk("\n[DBG] %s : thread %x deleted process %x / cycle %d\n",
-__FUNCTION__ , CURRENT_THREAD , process , cycle );
+printk("\n[DBG] %s : process %x has been deleted / cycle %d\n",
+__FUNCTION__ , process->pid , cycle );
 #endif
 
@@ -374,7 +393,6 @@
     remote_spinlock_lock_busy( lock_xp , &save_sr );
 
-    nolock_printk("\n***** scheduler state for core[%x,%d] / cycle %d / current = (%x,%x)\n",
-            local_cxy , core->lid, (uint32_t)hal_get_cycles(),
-            sched->current->process->pid , sched->current->trdid );
+    nolock_printk("\n***** threads on core[%x,%d] / current %x / cycle %d\n",
+            local_cxy , core->lid, sched->current, (uint32_t)hal_get_cycles() );
 
     // display kernel threads
@@ -390,7 +408,7 @@
         else
         {
-            nolock_printk(" - %s / pid %X / trdid %X / desc %X / block %X / flags %X \n",
+            nolock_printk(" - %s / pid %X / trdid %X / desc %X / block %X / flags %X\n",
             thread_type_str( thread->type ), thread->process->pid, thread->trdid,
-            thread, thread->blocked, thread->flags  );
+            thread, thread->blocked, thread->flags );
         }
     }
