Index: trunk/kernel/kern/scheduler.c
===================================================================
--- trunk/kernel/kern/scheduler.c	(revision 416)
+++ trunk/kernel/kern/scheduler.c	(revision 428)
@@ -86,4 +86,5 @@
 
     // release lock 
+    hal_fence();
     spinlock_unlock( &sched->lock );
 
@@ -181,7 +182,5 @@
     list_entry_t * iter;
     thread_t     * thread;
-
-// printk("\n@@@ %s : current thread %x enter at cycle %d\n",
-// __FUNCTION__ , CURRENT_THREAD , hal_time_stamp() );
+    process_t    * process;
 
     scheduler_t  * sched = &core->scheduler;
@@ -212,7 +211,6 @@
         if( thread->flags & THREAD_FLAG_REQ_DELETE )
         {
-
-sched_dmsg("\n[DBG] %s : current thread %x delete thread %x at cycle %d\n",
-__FUNCTION__ , CURRENT_THREAD , thread , hal_time_stamp() );
+            // get thread process descriptor
+            process = thread->process;
 
 	        // release FPU if required
@@ -223,25 +221,33 @@
             thread_child_parent_unlink( thread->parent , XPTR( local_cxy , thread ) );
 
-            // detach thread from process
-            process_remove_thread( thread );
-
-            // remove thread from scheduler
+            // remove thread from scheduler (scheduler lock already taken)
+            uint32_t threads_nr = sched->u_threads_nr;
+            assert( (threads_nr != 0) , __FUNCTION__ , "u_threads_nr cannot be 0\n" );
+            sched->u_threads_nr = threads_nr - 1;
             list_unlink( &thread->sched_list );
-            sched->u_threads_nr--;
-            if( sched->u_threads_nr == 0 ) sched->u_last = NULL;
-
-            // release memory allocated to thread
+            if( threads_nr == 1 ) sched->u_last = NULL;
+
+            // delete thread
             thread_destroy( thread );
 
+sched_dmsg("\n[DBG] %s : thread %x deleted thread %x / cycle %d\n",
+__FUNCTION__ , CURRENT_THREAD , thread , (uint32_t)hal_get_cycles() );
+
             // destroy process descriptor if no more threads
-            if (thread->process->th_nr == 0) process_destroy( thread->process );
+            if( process->th_nr == 0 ) 
+            {
+                // delete process    
+                process_destroy( process );
+
+sched_dmsg("\n[DBG] %s : thread %x deleted process %x / cycle %d\n",
+__FUNCTION__ , CURRENT_THREAD , process , (uint32_t)hal_get_cycles() );
+
+            }
         }
     }
 
     // release lock 
+    hal_fence();
     spinlock_unlock( &sched->lock );
-
-// printk("\n@@@ %s : current thread %x exit at cycle %d\n",
-// __FUNCTION__ , CURRENT_THREAD , hal_time_stamp() );
 
 } // end sched_handle_requests()
@@ -277,5 +283,5 @@
 
     // check next thread not blocked when type != IDLE
-    assert( (next->blocked == 0) || (next->type = THREAD_IDLE) , __FUNCTION__ ,
+    assert( ((next->blocked == 0) || (next->type == THREAD_IDLE)) , __FUNCTION__ ,
     "next thread %x (%s) is blocked on core[%x,%d]\n", 
     next->trdid , thread_type_str(next->type) , local_cxy , core->lid );
@@ -310,9 +316,12 @@
     {
 
-sched_dmsg("\n[DBG] %s : core[%x,%d] / cause = %s\n" 
+#if( CONFIG_SCHED_DEBUG & 0x1 )
+if( hal_time_stamp() > CONFIG_SCHED_DEBUG ) 
+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, 
 (uint32_t)hal_get_cycles() );
+#endif
 
     }
@@ -355,8 +364,6 @@
     remote_spinlock_lock_busy( lock_xp , &save_sr );
 
-    nolock_printk("\n***** scheduler state for core[%x,%d] at cycle %d\n"
-           "kernel_threads = %d / user_threads = %d / current = (%x,%x)\n",
-            local_cxy , core->lid, hal_time_stamp(),
-            sched->k_threads_nr, sched->u_threads_nr,
+    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 );
 
