Index: trunk/kernel/kern/scheduler.c
===================================================================
--- trunk/kernel/kern/scheduler.c	(revision 440)
+++ trunk/kernel/kern/scheduler.c	(revision 443)
@@ -34,10 +34,14 @@
 #include <scheduler.h>
 
+
 ///////////////////////////////////////////////////////////////////////////////////////////
 // Extern global variables
 ///////////////////////////////////////////////////////////////////////////////////////////
 
-extern chdev_directory_t    chdev_dir;            // allocated in kernel_init.c file
-extern uint32_t             switch_save_sr[];     // allocated in kernel_init.c file
+uint32_t   idle_thread_count;
+uint32_t   idle_thread_count_active;
+
+extern chdev_directory_t    chdev_dir;          // allocated in kernel_init.c file
+extern uint32_t             switch_save_sr[];   // allocated in kernel_init.c file
 
 ////////////////////////////////
@@ -50,7 +54,7 @@
 
     sched->current        = CURRENT_THREAD;
-    sched->idle           = NULL;             // initialized in kernel_init()
-    sched->u_last         = NULL;             // initialized in sched_register_thread()
-    sched->k_last         = NULL;             // initialized in sched_register_thread()
+    sched->idle           = NULL;               // initialized in kernel_init()
+    sched->u_last         = NULL;               // initialized in sched_register_thread()
+    sched->k_last         = NULL;               // initialized in sched_register_thread()
 
     // initialise threads lists
@@ -58,5 +62,6 @@
     list_root_init( &sched->k_root );
 
-    sched->req_ack_pending = false;           // no pending request
+    sched->req_ack_pending = false;             // no pending request
+    sched->trace           = false;             // context switches trace desactivated
 
 }  // end sched_init()
@@ -179,4 +184,5 @@
     thread_t     * thread;
     process_t    * process;
+    bool_t         last_thread;
 
     // get pointer on scheduler
@@ -237,14 +243,14 @@
 
             // delete thread
-            thread_destroy( thread );
+            last_thread = thread_destroy( thread );
 
 #if DEBUG_SCHED_HANDLE_SIGNALS
 uint32_t cycle = (uint32_t)hal_get_cycles();
 if( DEBUG_SCHED_HANDLE_SIGNALS < cycle )
-printk("\n[DBG] %s : thread %x in proces %x (%x) deleted / cycle %d\n",
-__FUNCTION__ , thread , process->pid , process , cycle );
+printk("\n[DBG] %s : thread %x in proces %x on core[%x,%d] deleted / cycle %d\n",
+__FUNCTION__ , thread->trdid , process->pid , local_cxy , thread->core->lid , cycle );
 #endif
             // destroy process descriptor if no more threads
-            if( process->th_nr == 0 ) 
+            if( last_thread ) 
             {
                 // delete process    
@@ -254,6 +260,6 @@
 cycle = (uint32_t)hal_get_cycles();
 if( DEBUG_SCHED_HANDLE_SIGNALS < cycle )
-printk("\n[DBG] %s : process %x has been deleted / cycle %d\n",
-__FUNCTION__ , process->pid , cycle );
+printk("\n[DBG] %s : process %x in cluster %x deleted / cycle %d\n",
+__FUNCTION__ , process->pid , local_cxy , cycle );
 #endif
 
@@ -277,5 +283,5 @@
  
 #if (DEBUG_SCHED_YIELD & 0x1)
-if( DEBUG_SCHED_YIELD < (uint32_t)hal_get_cycles() )
+if( sched->trace )
 sched_display( core->lid );
 #endif
@@ -295,10 +301,10 @@
 
     // check next thread kernel_stack overflow
-    assert( (next->signature == THREAD_SIGNATURE),
-    __FUNCTION__ , "kernel stack overflow for thread %x\n", next );
+    assert( (next->signature == THREAD_SIGNATURE), __FUNCTION__ , 
+    "kernel stack overflow for thread %x on core[%x,%d] \n", next, local_cxy, core->lid );
 
     // check next thread attached to same core as the calling thread
-    assert( (next->core == current->core),
-    __FUNCTION__ , "next core %x != current core %x\n", next->core, current->core );
+    assert( (next->core == current->core), __FUNCTION__ , 
+    "next core %x != current core %x\n", next->core, current->core );
 
     // check next thread not blocked when type != IDLE
@@ -311,18 +317,11 @@
     {
 
-if( (local_cxy == 0X1) && (core->lid == 1) && ((uint32_t)current == 0xcc000) )
-printk("\n@@@@@ cc000 exit at cycle %d\n", (uint32_t)hal_get_cycles() );
-
-if( (local_cxy == 0X1) && (core->lid == 1) && ((uint32_t)next == 0xcc000) )
-printk("\n@@@@@ cc000 enter at cycle %d\n", (uint32_t)hal_get_cycles() );
-
 #if DEBUG_SCHED_YIELD
-uint32_t cycle = (uint32_t)hal_get_cycles();
-if( DEBUG_SCHED_YIELD < cycle )
+if( sched->trace )
 printk("\n[DBG] %s : core[%x,%d] / cause = %s\n"
 "      thread %x (%s) (%x,%x) => thread %x (%s) (%x,%x) / cycle %d\n",
 __FUNCTION__, local_cxy, core->lid, cause, 
-current, thread_type_str(current->type), current->process->pid, current->trdid,
-next , thread_type_str(next->type) , next->process->pid , next->trdid , cycle );
+current, thread_type_str(current->type), current->process->pid, current->trdid,next ,
+thread_type_str(next->type) , next->process->pid , next->trdid , (uint32_t)hal_get_cycles() );
 #endif
 
@@ -345,11 +344,10 @@
     {
 
-#if (DEBUG_SCHED_YIELD & 1)
-uint32_t cycle = (uint32_t)hal_get_cycles();
-if( DEBUG_SCHED_YIELD < cycle )
+#if DEBUG_SCHED_YIELD 
+if( sched->trace )
 printk("\n[DBG] %s : core[%x,%d] / cause = %s\n"
 "      thread %x (%s) (%x,%x) continue / cycle %d\n",
-__FUNCTION__, local_cxy, core->lid, cause,
-current, thread_type_str(current->type), current->process->pid, current->trdid, cycle );
+__FUNCTION__, local_cxy, core->lid, cause, current, thread_type_str(current->type),
+current->process->pid, current->trdid, (uint32_t)hal_get_cycles() );
 #endif
 
@@ -389,5 +387,5 @@
 
     nolock_printk("\n***** threads on core[%x,%d] / current %x / cycle %d\n",
-            local_cxy , core->lid, sched->current, (uint32_t)hal_get_cycles() );
+    local_cxy , core->lid, sched->current, (uint32_t)hal_get_cycles() );
 
     // display kernel threads
