Index: trunk/kernel/kern/scheduler.c
===================================================================
--- trunk/kernel/kern/scheduler.c	(revision 279)
+++ trunk/kernel/kern/scheduler.c	(revision 296)
@@ -30,5 +30,12 @@
 #include <core.h>
 #include <thread.h>
+#include <chdev.h>
 #include <scheduler.h>
+
+///////////////////////////////////////////////////////////////////////////////////////////
+// Extern global variables
+///////////////////////////////////////////////////////////////////////////////////////////
+
+extern chdev_directory_t    chdev_dir;            // allocated in kernel_init.c file
 
 
@@ -144,7 +151,10 @@
 thread_t * sched_select( core_t * core )
 {
-    thread_t * thread;
+    thread_t    * thread;
 
     scheduler_t * sched = &core->scheduler;
+
+    sched_dmsg("\n[INFO] %s : enter core[%x,%d] / cycle %d\n",
+    __FUNCTION__ , local_cxy , core->lid , hal_time_stamp() );
 
     // take lock protecting sheduler lists
@@ -154,6 +164,5 @@
     list_entry_t * last;
 
-    // first : scan the kernel threads list,
-    // only if this list is not empty 
+    // first : scan the kernel threads list if not empty 
     if( list_is_empty( &sched->k_root ) == false )
     {
@@ -171,9 +180,13 @@
             thread = LIST_ELEMENT( current , thread_t , sched_list );
 
-            // return thread if runnable
-            if( thread->blocked == 0 ) 
+            // return thread if not idle_thread and runnable 
+            if( (thread->type != THREAD_IDLE) && (thread->blocked == 0) ) 
             {
                 // release lock 
                 spinlock_unlock( &sched->lock );
+
+                sched_dmsg("\n[INFO] %s : exit core[%x,%d] / k_thread = %x / cycle %d\n",
+                __FUNCTION__ , local_cxy , core->lid , thread->trdid , hal_time_stamp() );
+
                 return thread;
             }
@@ -182,6 +195,5 @@
     }
 
-    // second : scan the user threads list,
-    // only if this list is not empty 
+    // second : scan the user threads list if not empty 
     if( list_is_empty( &sched->u_root ) == false )
     {
@@ -204,4 +216,7 @@
                 // release lock 
                 spinlock_unlock( &sched->lock );
+
+                sched_dmsg("\n[INFO] %s : exit core[%x,%d] / u_thread = %x / cycle %d\n",
+                __FUNCTION__ , local_cxy , core->lid , thread->trdid , hal_time_stamp() );
                 return thread;
             }
@@ -213,8 +228,11 @@
     spinlock_unlock( &sched->lock );
 
+    sched_dmsg("\n[INFO] %s : exit core[%x,%d] / idle = %x / cycle %d\n",
+    __FUNCTION__ , local_cxy , core->lid , sched->idle->trdid , hal_time_stamp() );
+
     // third : return idle thread if no runnable thread
     return sched->idle;
 
-}  // end sched_elect()
+}  // end sched_select()
 
 //////////////////////////////////////////
@@ -223,6 +241,8 @@
     list_entry_t * iter;
     thread_t     * thread;
-
     scheduler_t  * sched = &core->scheduler;
+
+    sched_dmsg("\n[INFO] %s : enter / thread %x on core[%x,%d]\n",
+    __FUNCTION__, CURRENT_THREAD->trdid , local_cxy , core->lid );
 
     // take lock protecting threads lists
@@ -246,11 +266,13 @@
     spinlock_unlock( &sched->lock );
 
+    sched_dmsg("\n[INFO] %s : exit / thread %x on core[%x,%d]\n",
+    __FUNCTION__, CURRENT_THREAD->trdid , local_cxy , core->lid );
+
 } // end sched_handle_signals()
 
-//////////////////
-void sched_yield()
+///////////////////////////////////
+void sched_yield( thread_t * next )
 {
     reg_t         sr_save;
-    thread_t    * next;
 
     thread_t    * current = CURRENT_THREAD;
@@ -258,131 +280,109 @@
     scheduler_t * sched   = &core->scheduler;
 
-    if( thread_can_yield() == false )
-    {
-        printk("\n[PANIC] in %s : thread %x for process %x on core_gid %x"
-               " has not released all locks at cycle %d\n",
-               __FUNCTION__, current->trdid, current->process->pid, 
-               local_cxy , core->lid , hal_get_cycles() );
-        hal_core_sleep();
-    }
-
-    // desactivate IRQs
-    hal_disable_irq( &sr_save );
+    sched_dmsg("\n[INFO] %s : thread %x on core[%x,%d] enter / cycle %d\n",
+    __FUNCTION__, current->trdid, local_cxy, core->lid, hal_time_stamp() );
+
+    // check calling thread released all locks
+    assert( (thread_can_yield() == true), __FUNCTION__, "locks not released\n");
 
     // first loop on all threads to handle pending signals
     sched_handle_signals( core );
 
-    // second loop on threads to select next thread
-    next = sched_select( core );
-
-    // check stack overflow for selected thread
-    if( next->signature != THREAD_SIGNATURE )
-    {
-        printk("\n[PANIC] in %s : detected stack overflow for thread %x of process %x"
-               " on core [%x][%d]\n",
-               __FUNCTION__, next->trdid, next->process->pid, local_cxy , core->lid );
-        hal_core_sleep();
-	}
-	
-	sched_dmsg("\n[INFO] %s on core %d in cluster %x / old thread = %x / new thread = %x\n",
-               __FUNCTION__, core->lid, local_cxy, current->trdid, next->trdid );
-
-    // switch contexts and update scheduler state if new thread
-	if( next != current )  
-	{
-        hal_cpu_context_save( current );
-        hal_cpu_context_restore( next );
-
+    // second loop on threads to select next thread if required
+    if( next == NULL ) next = sched_select( core );
+
+    // check next thread attached to same core as the calling thread
+    assert( (next->core == current->core), __FUNCTION__ , "next core != current core\n");
+
+    // check next thread not blocked
+    assert( (next->blocked == 0), __FUNCTION__ , "next thread is blocked\n");
+
+    // switch contexts and update scheduler state if next != current
+	if( next != current )
+    {
+        sched_dmsg("\n[INFO] %s : trd %x (%s) on core[%x,%d] => trd %x (%s) / cycle %d\n",
+        __FUNCTION__, current->trdid, thread_type_str(current->type), local_cxy, core->lid, 
+        next->trdid, thread_type_str(next->type), hal_time_stamp() );
+
+        // calling thread desactivate IRQs
+        hal_disable_irq( &sr_save );
+
+        // update scheduler 
         if( current->type == THREAD_USER ) sched->u_last = &current->sched_list;
         else                               sched->k_last = &current->sched_list;
-
         sched->current = next;
-	}
-
-    // restore IRQs
-    hal_restore_irq( sr_save );
-
-	if( current->type != THREAD_USER ) return;
-
-	if( next == core->fpu_owner ) hal_fpu_enable();
-	else                          hal_fpu_disable();
-
+
+        // handle FPU
+	    if( next->type == THREAD_USER ) return;
+        {
+	        if( next == core->fpu_owner )  hal_fpu_enable();
+	        else                           hal_fpu_disable();
+        }
+
+        // switch contexts 
+        hal_cpu_context_save( current->cpu_context );
+        hal_cpu_context_restore( next->cpu_context );
+
+        // restore IRQs when calling thread resume
+        hal_restore_irq( sr_save );
+
+        sched_dmsg("\n[INFO] %s : thread %x on core[%x,%d] / cycle %d\n",
+        __FUNCTION__, current->trdid, local_cxy, core->lid, hal_time_stamp() );
+    }
+    else
+    {
+        sched_dmsg("\n[INFO] %s : thread %x on core[%x,%d] continue / cycle %d\n",
+        __FUNCTION__, current->trdid, local_cxy, core->lid, hal_time_stamp() );
+    }
 }  // end sched_yield()
 
-//////////////////////////////////////
-void sched_switch_to( thread_t * new )
-{
-    reg_t         sr_save;
-
-    thread_t    * current = CURRENT_THREAD;
-    core_t      * core    = current->core;
-    process_t   * process = current->process;
-
-    // check calling thread released all locks
-    if( thread_can_yield() == false )
-    {
-        printk("\n[PANIC] in %s : thread %x for process %x on core %d in cluster %x"
-               " has not released all locks\n",
-               __FUNCTION__, current->trdid, process->pid, core->lid, local_cxy );
-        hal_core_sleep();
-    }
-
-    // check new thread attached to same core as the calling thread
-    if( new->core != current->core )
-    {
-        printk("\n[PANIC] in %s : new thread %x is attached to core %d"
-               " different from core %d of current thread\n",
-               __FUNCTION__, new->trdid, new->core->lid, core->lid , current->trdid );
-        hal_core_sleep();
-    }
-
-    // check new thread not blocked
-    if( new->blocked == 0 )
-    {
-        printk("\n[PANIC] in %s for thread %x of process %x on core %d in cluster %x"
-               " : new thread %x is blocked\n",
-               __FUNCTION__, current->trdid, process->pid , core->lid, local_cxy , new->trdid );
-        hal_core_sleep();
-    }
-
-    // check stack overflow for new thread
-    if( new->signature != THREAD_SIGNATURE )
-    {
-        printk("\n[PANIC] in %s : stack overflow for new thread %x of process %x"
-               " on core %d in cluster %x\n",
-               __FUNCTION__, new->trdid, process->pid , core->lid , local_cxy );
-        hal_core_sleep();
-	}
-
-    // desactivate IRQs
-    hal_disable_irq( &sr_save );
-
-    // loop on all threads to handle pending signals
-    sched_handle_signals( core );
-
-    // check stack overflow for new thread
-    if( new->signature != THREAD_SIGNATURE )
-    {
-        printk("PANIC %s detected stack overflow for thread %x of process %x"
-               " on core %d in cluster %x\n",
-               __FUNCTION__, new->trdid, new->process->pid, core->lid, local_cxy);
-        hal_core_sleep();
-	}
-	
-	sched_dmsg("INFO : %s on core %d in cluster %x / old thread = %x / new thread = %x\n",
-               __FUNCTION__, core->lid, local_cxy, current->trdid, new->trdid );
-
-    // switch contexts if new thread
-    hal_cpu_context_save( current );
-    hal_cpu_context_restore( new );
-
-    // restore IRQs
-    hal_restore_irq( sr_save );
-
-	if( current->type != THREAD_USER ) return;
-
-	if( current == core->fpu_owner )  hal_fpu_enable();
-	else                              hal_fpu_disable();
-
-}  // end sched_switch_to()
-
+////////////////////
+void sched_display()
+{
+    list_entry_t * iter;
+    thread_t     * thread;
+    uint32_t       save_sr;
+
+    thread_t     * current = CURRENT_THREAD;
+    core_t       * core    = current->core;
+    scheduler_t  * sched   = &core->scheduler;
+    
+    // get pointers on TXT0 chdev
+    xptr_t    txt0_xp  = chdev_dir.txt[0];
+    cxy_t     txt0_cxy = GET_CXY( txt0_xp );
+    chdev_t * txt0_ptr = GET_PTR( txt0_xp );
+
+    // get extended pointer on remote TXT0 chdev lock
+    xptr_t  lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock );
+
+    // get TXT0 lock in busy waiting mode
+    remote_spinlock_lock_busy( lock_xp , &save_sr );
+
+    nolock_printk("\n********** scheduler state for core[%x,%d] **********************\n"
+           "kernel_threads = %d / user_threads = %d / current = %x\n",
+            local_cxy , core->lid, 
+            sched->k_threads_nr, sched->u_threads_nr, sched->current->trdid );
+
+    // display kernel threads
+    LIST_FOREACH( &sched->k_root , iter )
+    {
+        thread = LIST_ELEMENT( iter , thread_t , sched_list );
+        nolock_printk(" - type = %s / trdid = %x / pid = %x / func = %x / blocked_vect = %x\n",
+        thread_type_str( thread->type ), thread->trdid, thread->process->pid,
+        thread->entry_func, thread->blocked );
+    }
+
+    // display user threads
+    LIST_FOREACH( &sched->u_root , iter )
+    {
+        thread = LIST_ELEMENT( iter , thread_t , sched_list );
+        nolock_printk(" - type = %s / trdid = %x / pid = %x / func = %x / blocked_vect = %x\n",
+        thread_type_str( thread->type ), thread->trdid, thread->process->pid,
+        thread->entry_func, thread->blocked );
+    }
+
+    // release TXT0 lock
+    remote_spinlock_unlock_busy( lock_xp , save_sr );
+
+}  // end sched_display()
+
