Index: trunk/kernel/kern/process.c
===================================================================
--- trunk/kernel/kern/process.c	(revision 581)
+++ trunk/kernel/kern/process.c	(revision 583)
@@ -68,5 +68,5 @@
 //////////////////////////////////////////////////////////////////////////////////////////
 
-///////////////////////////
+/////////////////////////////////
 process_t * process_alloc( void )
 {
@@ -463,13 +463,14 @@
 }  // end process_destroy()
 
-/////////////////////////////////////////////////
+///////////////////////////////////////////////////////////////////
 const char * process_action_str( process_sigactions_t action_type )
 {
-  switch ( action_type ) {
-  case BLOCK_ALL_THREADS:   return "BLOCK";
-  case UNBLOCK_ALL_THREADS: return "UNBLOCK";
-  case DELETE_ALL_THREADS:  return "DELETE";
-  default:                  return "undefined";
-  }
+    switch ( action_type )
+    {
+        case BLOCK_ALL_THREADS:   return "BLOCK";
+        case UNBLOCK_ALL_THREADS: return "UNBLOCK";
+        case DELETE_ALL_THREADS:  return "DELETE";
+        default:                  return "undefined";
+    }
 }
 
@@ -499,13 +500,12 @@
     remote_nr = 0;
 
-// check calling thread can yield
-assert( (client->busylocks == 0),
-"cannot yield : busylocks = %d\n", client->busylocks );
+    // check calling thread can yield
+    thread_assert_can_yield( client , __FUNCTION__ );
 
 #if DEBUG_PROCESS_SIGACTION
 uint32_t cycle = (uint32_t)hal_get_cycles();
 if( DEBUG_PROCESS_SIGACTION < cycle )
-printk("\n[DBG] %s : thread %x in process %x enter to %s process %x / cycle %d\n",
-__FUNCTION__ , client->trdid, client->process->pid,
+printk("\n[DBG] %s : thread[%x,%x] enter to %s process %x / cycle %d\n",
+__FUNCTION__ , client->process->pid, client->trdid,
 process_action_str( type ) , pid , cycle );
 #endif
@@ -522,10 +522,9 @@
     lock_xp   = XPTR( owner_cxy , &cluster->pmgr.copies_lock[lpid] );
 
-    // check action type
-    assert( ((type == DELETE_ALL_THREADS ) ||
-             (type == BLOCK_ALL_THREADS )  ||
-             (type == UNBLOCK_ALL_THREADS )), "illegal action type" );
+// check action type
+assert( ((type == DELETE_ALL_THREADS ) ||
+         (type == BLOCK_ALL_THREADS )  ||
+         (type == UNBLOCK_ALL_THREADS )), "illegal action type" );
              
-
     // The client thread send parallel RPCs to all remote clusters containing
     // target process copies, wait all responses, and then handles directly 
@@ -576,6 +575,6 @@
 #if DEBUG_PROCESS_SIGACTION 
 if( DEBUG_PROCESS_SIGACTION < cycle )
-printk("\n[DBG] %s : thread %x in process %x handles remote process %x in cluster %x\n",
-__FUNCTION__, client->trdid, client->process->pid, pid , process_cxy );
+printk("\n[DBG] %s : thread[%x,%x] send RPC to cluster %x for process %x\n",
+__FUNCTION__, client->process->pid, client->trdid, process_cxy, pid );
 #endif
             // call RPC in target cluster
@@ -608,9 +607,9 @@
 #if DEBUG_PROCESS_SIGACTION 
 if( DEBUG_PROCESS_SIGACTION < cycle )
-printk("\n[DBG] %s : thread %x in process %x handles local process %x in cluster %x\n",
-__FUNCTION__, client->trdid, client->process->pid, pid , local_cxy );
+printk("\n[DBG] %s : thread[%x,%x] handles local process %x in cluster %x\n",
+__FUNCTION__, client->process->pid, client->trdid, pid , local_cxy );
 #endif
         if     (type == DELETE_ALL_THREADS  ) process_delete_threads ( local , client_xp ); 
-        else if(type == BLOCK_ALL_THREADS   ) process_block_threads  ( local , client_xp ); 
+        else if(type == BLOCK_ALL_THREADS   ) process_block_threads  ( local ); 
         else if(type == UNBLOCK_ALL_THREADS ) process_unblock_threads( local );
     }
@@ -619,6 +618,6 @@
 cycle = (uint32_t)hal_get_cycles();
 if( DEBUG_PROCESS_SIGACTION < cycle )
-printk("\n[DBG] %s : thread %x in process %x exit after %s process %x / cycle %d\n",
-__FUNCTION__, client->trdid, client->process->pid,
+printk("\n[DBG] %s : thread[%x,%x] exit after %s process %x / cycle %d\n",
+__FUNCTION__, client->process->pid, client->trdid,
 process_action_str( type ), pid, cycle );
 #endif
@@ -627,6 +626,5 @@
 
 /////////////////////////////////////////////////
-void process_block_threads( process_t * process,
-                            xptr_t      client_xp )
+void process_block_threads( process_t * process )
 {
     thread_t          * target;         // pointer on target thread 
@@ -644,13 +642,12 @@
 uint32_t cycle = (uint32_t)hal_get_cycles();
 if( DEBUG_PROCESS_SIGACTION < cycle )
-printk("\n[DBG] %s : thread %x in process %x enter for process %x in cluster %x / cycle %d\n",
-__FUNCTION__, this->trdid, this->process->pid, pid, local_cxy , cycle );
+printk("\n[DBG] %s : thread[%x,%x] enter for process %x in cluster %x / cycle %d\n",
+__FUNCTION__, this->process->pid, this->trdid, pid, local_cxy , cycle );
 #endif
 
 // check target process is an user process
-assert( ( process->pid != 0 ),
-"target process must be an user process" );
-
-    // get target process owner cluster
+assert( (LPID_FROM_PID( process->pid ) != 0 ), "target process must be an user process" );
+
+    // get target process cluster
     owner_cxy = CXY_FROM_PID( process->pid );
 
@@ -668,28 +665,23 @@
             count++;
 
-            // main thread and client thread should not be blocked
-            if( ((ltid != 0) || (owner_cxy != local_cxy)) &&         // not main thread
-                (client_xp) != XPTR( local_cxy , target ) )          // not client thread
+            // set the global blocked bit in target thread descriptor.
+            thread_block( XPTR( local_cxy , target ) , THREAD_BLOCKED_GLOBAL );
+  
+            // - if the calling thread and the target thread are running on the same core,
+            //   we don't need confirmation from scheduler,
+            // - if the calling thread and the target thread are not running on the same
+            //   core, we ask the target scheduler to acknowlege the blocking
+            //   to be sure that the target thread is not running.
+            
+            if( this->core->lid != target->core->lid )
             {
-                // set the global blocked bit in target thread descriptor.
-                thread_block( XPTR( local_cxy , target ) , THREAD_BLOCKED_GLOBAL );
-  
-                // - if the calling thread and the target thread are on the same core,
-                //   we don't need confirmation from scheduler,
-                // - if the calling thread and the target thread are not running on the same
-                //   core, we ask the target scheduler to acknowlege the blocking
-                //   to be sure that the target thread is not running.
-            
-                if( this->core->lid != target->core->lid )
-                {
-                    // increment responses counter 
-                    hal_atomic_add( (void*)&ack_count , 1 );
-
-                    // set FLAG_REQ_ACK and &ack_rsp_count in target descriptor
-                    thread_set_req_ack( target , (uint32_t *)&ack_count );
-
-                    // force scheduling on target thread
-                    dev_pic_send_ipi( local_cxy , target->core->lid );
-                }
+                // increment responses counter 
+                hal_atomic_add( (void*)&ack_count , 1 );
+
+                // set FLAG_REQ_ACK and &ack_rsp_count in target descriptor
+                thread_set_req_ack( target , (uint32_t *)&ack_count );
+
+                // force scheduling on target thread
+                dev_pic_send_ipi( local_cxy , target->core->lid );
             }
         }
@@ -713,5 +705,5 @@
 cycle = (uint32_t)hal_get_cycles();
 if( DEBUG_PROCESS_SIGACTION < cycle )
-printk("\n[DBG] %s : thread %x in process %x exit for process %x in cluster %x / cycle %d\n",
+printk("\n[DBG] %s : thread[%x,%x] exit for process %x in cluster %x / cycle %d\n",
 __FUNCTION__, this, this->process->pid, pid, local_cxy , cycle );
 #endif
@@ -740,14 +732,14 @@
 uint32_t cycle = (uint32_t)hal_get_cycles();
 if( DEBUG_PROCESS_SIGACTION < cycle )
-printk("\n[DBG] %s : thread %x n process %x enter for process %x in cluster %x / cycle %d\n",
-__FUNCTION__, this->trdid, this->process->pid, pid, local_cxy , cycle );
+printk("\n[DBG] %s : thread[%x,%x] enter in cluster %x for process %x / cycle %d\n",
+__FUNCTION__, this->process->pid, this->trdid, local_cxy, process->pid, cycle );
 #endif
 
 // check target process is an user process
-assert( ( process->pid != 0 ),
+assert( ( LPID_FROM_PID( process->pid ) != 0 ),
 "target process must be an user process" );
 
     // get lock protecting process th_tbl[]
-    rwlock_rd_acquire( &process->th_lock );
+    rwlock_wr_acquire( &process->th_lock );
 
     // loop on target process local threads                       
@@ -773,11 +765,11 @@
 
     // release lock protecting process th_tbl[]
-    rwlock_rd_release( &process->th_lock );
+    rwlock_wr_release( &process->th_lock );
 
 #if DEBUG_PROCESS_SIGACTION
 cycle = (uint32_t)hal_get_cycles();
 if( DEBUG_PROCESS_SIGACTION < cycle )
-printk("\n[DBG] %s : thread %x in process %x exit for process %x in cluster %x / cycle %d\n",
-__FUNCTION__, this->trdid, this->process->pid, pid, local_cxy , cycle );
+printk("\n[DBG] %s : thread[%x,%x] exit for process %x in cluster %x / cycle %d\n",
+__FUNCTION__, this->process->pid, this->trdid, pid, local_cxy , cycle );
 #endif
 
@@ -799,6 +791,6 @@
 uint32_t cycle = (uint32_t)hal_get_cycles();
 if( DEBUG_PROCESS_SIGACTION < cycle )
-printk("\n[DBG] %s : thread %x in process %x enter for process %x in cluster %x / cycle %d\n",
-__FUNCTION__, this->trdid, this->process->pid, pid, local_cxy , cycle );
+printk("\n[DBG] %s : thread[%x,%x] enter for process %x in cluster %x / cycle %d\n",
+__FUNCTION__, this->process->pid, this->trdid, pid, local_cxy , cycle );
 #endif
 
@@ -831,6 +823,6 @@
 cycle = (uint32_t)hal_get_cycles();
 if( DEBUG_PROCESS_SIGACTION < cycle )
-printk("\n[DBG] %s : thread %x in process %x exit for process %x in cluster %x / cycle %d\n",
-__FUNCTION__, this->trdid, this->process->pid, pid, local_cxy, cycle );
+printk("\n[DBG] %s : thread[%x,%x] exit for process %x in cluster %x / cycle %d\n",
+__FUNCTION__, this->process->pid, this->trdid, pid, local_cxy, cycle );
 #endif
 
@@ -850,6 +842,6 @@
 uint32_t cycle = (uint32_t)hal_get_cycles();
 if( DEBUG_PROCESS_GET_LOCAL_COPY < cycle )
-printk("\n[DBG] %s : thread %x in cluster %x enter for process %x in cluster %x / cycle %d\n",
-__FUNCTION__, this->trdid, this->process->pid, pid, local_cxy, cycle );
+printk("\n[DBG] %s : thread[%x,%x] enter for process %x in cluster %x / cycle %d\n",
+__FUNCTION__, this->process->pid, this->trdid, pid, local_cxy, cycle );
 #endif
 
@@ -897,6 +889,6 @@
 cycle = (uint32_t)hal_get_cycles();
 if( DEBUG_PROCESS_GET_LOCAL_COPY < cycle )
-printk("\n[DBG] %s : thread %x in cluster %x exit in cluster %x / process %x / cycle %d\n",
-__FUNCTION__, this->trdid, this->process->pid, local_cxy, process_ptr, cycle );
+printk("\n[DBG] %s : thread[%x,%x] exit in cluster %x / process %x / cycle %d\n",
+__FUNCTION__, this->process->pid, this->trdid, local_cxy, process_ptr, cycle );
 #endif
 
@@ -1109,5 +1101,5 @@
     if( thread->type != THREAD_IDLE ) rwlock_wr_acquire( &process->th_lock );
 
-    // scan kth_tbl
+    // scan th_tbl
     for( ltid = 0 ; ltid < CONFIG_THREADS_MAX_PER_CLUSTER ; ltid++ )
     {
@@ -1127,8 +1119,12 @@
         // returns trdid
         *trdid = TRDID( local_cxy , ltid );
-    }
-
-    // get the lock protecting th_tbl for all threads
-    // but the idle thread executing kernel_init (cannot yield)
+
+// if( LPID_FROM_PID( process->pid ) == 0 )
+// printk("\n@@@ %s : allocate ltid %d for a thread %s in cluster %x\n",
+// __FUNCTION__, ltid, thread_type_str( thread->type), local_cxy );
+
+    }
+
+    // release the lock protecting th_tbl
     if( thread->type != THREAD_IDLE ) rwlock_wr_release( &process->th_lock );
 
@@ -1141,7 +1137,4 @@
 {
     uint32_t count;  // number of threads in local process descriptor
-
-// check argument
-assert( (thread != NULL) , "thread argument is NULL" );
 
     process_t * process = thread->process;
@@ -1150,15 +1143,15 @@
     ltid_t  ltid = LTID_FROM_TRDID( thread->trdid );
     
-    // the lock depends on thread user/kernel type, because we cannot
-    // use a descheduling policy for the lock protecting the kth_tbl
-
     // get the lock protecting th_tbl[]
     rwlock_wr_acquire( &process->th_lock );
 
-    // get number of kernel threads
+    // get number of threads
     count = process->th_nr;
 
+// check thread
+assert( (thread != NULL) , "thread argument is NULL" );
+
 // check th_nr value
-assert( (count > 0) , "process kth_nr cannot be 0\n" );
+assert( (count > 0) , "process th_nr cannot be 0\n" );
 
     // remove thread from th_tbl[]
@@ -1166,5 +1159,9 @@
     process->th_nr = count-1;
 
-    // release lock protecting kth_tbl
+// if( LPID_FROM_PID( process->pid ) == 0 )
+// printk("\n@@@ %s : release ltid %d for a thread %s in cluster %x\n",
+// __FUNCTION__, ltid, thread_type_str( thread->type), local_cxy );
+
+    // release lock protecting th_tbl
     rwlock_wr_release( &process->th_lock );
 
@@ -1203,8 +1200,11 @@
 
 #if DEBUG_PROCESS_MAKE_FORK
-uint32_t cycle = (uint32_t)hal_get_cycles();
+uint32_t cycle   = (uint32_t)hal_get_cycles();
+thread_t * this  = CURRENT_THREAD;
+trdid_t    trdid = this->trdid;
+pid_t      pid   = this->process->pid;
 if( DEBUG_PROCESS_MAKE_FORK < cycle )
-printk("\n[DBG] %s : thread %x in process %x enter / cluster %x / cycle %d\n",
-__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, local_cxy, cycle );
+printk("\n[DBG] %s : thread[%x,%x] enter / cluster %x / cycle %d\n",
+__FUNCTION__, pid, trdid, local_cxy, cycle );
 #endif
 
@@ -1231,6 +1231,6 @@
 cycle = (uint32_t)hal_get_cycles();
 if( DEBUG_PROCESS_MAKE_FORK < cycle )
-printk("\n[DBG] %s : thread %x in process %x allocated process %x / cycle %d\n",
-__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, new_pid, cycle );
+printk("\n[DBG] %s : thread[%x,%x] allocated process %x / cycle %d\n",
+__FUNCTION__, pid, trdid, new_pid, cycle );
 #endif
 
@@ -1243,6 +1243,6 @@
 cycle = (uint32_t)hal_get_cycles();
 if( DEBUG_PROCESS_MAKE_FORK < cycle )
-printk("\n[DBG] %s : thread %x in process %x initialized child_process %x / cycle %d\n",
-__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, new_pid, cycle );
+printk("\n[DBG] %s : thread[%x,%x] initialized child_process %x / cycle %d\n",
+__FUNCTION__, pid, trdid, new_pid, cycle );
 #endif
 
@@ -1263,7 +1263,6 @@
 cycle = (uint32_t)hal_get_cycles();
 if( DEBUG_PROCESS_MAKE_FORK < cycle )
-printk("\n[DBG] %s : thread %x in process %x copied VMM from parent %x to child %x / cycle %d\n",
-__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, 
-parent_pid, new_pid, cycle );
+printk("\n[DBG] %s : thread[%x,%x] copied VMM from parent to child / cycle %d\n",
+__FUNCTION__, pid, trdid, cycle );
 #endif
 
@@ -1277,6 +1276,6 @@
 cycle = (uint32_t)hal_get_cycles();
 if( DEBUG_PROCESS_MAKE_EXEC < cycle )
-printk("\n[DBG] %s : thread %x in process %x / child takes TXT ownership / cycle %d\n",
-__FUNCTION__ , CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, cycle );
+printk("\n[DBG] %s : thread[%x,%x] / child takes TXT ownership / cycle %d\n",
+__FUNCTION__ , pid, trdid, cycle );
 #endif
 
@@ -1306,6 +1305,6 @@
 cycle = (uint32_t)hal_get_cycles();
 if( DEBUG_PROCESS_MAKE_FORK < cycle )
-printk("\n[DBG] %s : thread %x in process %x created main thread %x / cycle %d\n", 
-__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, thread, cycle );
+printk("\n[DBG] %s : thread[%x,%x] created main thread %x / cycle %d\n", 
+__FUNCTION__, pid, trdid, thread, cycle );
 #endif
 
@@ -1328,6 +1327,6 @@
 cycle = (uint32_t)hal_get_cycles();
 if( DEBUG_PROCESS_MAKE_FORK < cycle )
-printk("\n[DBG] %s : thread %x in process %x set COW in parent and child / cycle %d\n",
-__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, cycle );
+printk("\n[DBG] %s : thread[%x,%x] set COW in parent and child / cycle %d\n",
+__FUNCTION__, pid, trdid, cycle );
 #endif
 
@@ -1350,6 +1349,6 @@
 cycle = (uint32_t)hal_get_cycles();
 if( DEBUG_PROCESS_MAKE_FORK < cycle )
-printk("\n[DBG] %s : thread %x in process %x exit / created process %x / cycle %d\n",
-__FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, new_pid, cycle );
+printk("\n[DBG] %s : thread[%x,%x] exit / created process %x / cycle %d\n",
+__FUNCTION__, pid, trdid, new_pid, cycle );
 #endif
 
@@ -1384,6 +1383,6 @@
 uint32_t cycle = (uint32_t)hal_get_cycles();
 if( DEBUG_PROCESS_MAKE_EXEC < cycle )
-printk("\n[DBG] %s : thread %x in process %x enters / path %s / cycle %d\n",
-__FUNCTION__, thread->trdid, pid, path, cycle );
+printk("\n[DBG] %s : thread[%x,%x] enters for %s / cycle %d\n",
+__FUNCTION__, pid, thread->trdid, path, cycle );
 #endif
 
@@ -1406,6 +1405,6 @@
 cycle = (uint32_t)hal_get_cycles();
 if( DEBUG_PROCESS_MAKE_EXEC < cycle )
-printk("\n[DBG] %s : thread %x in process %x opened file <%s> / cycle %d\n",
-__FUNCTION__, thread->trdid, pid, path, cycle );
+printk("\n[DBG] %s : thread[%x,%x] opened file <%s> / cycle %d\n",
+__FUNCTION__, pid, thread->trdid, path, cycle );
 #endif
 
@@ -1416,6 +1415,6 @@
 cycle = (uint32_t)hal_get_cycles();
 if( DEBUG_PROCESS_MAKE_EXEC < cycle )
-printk("\n[DBG] %s : thread %x in process %x deleted all threads / cycle %d\n",
-__FUNCTION__, thread->trdid, pid, cycle );
+printk("\n[DBG] %s : thread[%x,%x] deleted all threads / cycle %d\n",
+__FUNCTION__, pid, thread->trdid, cycle );
 #endif
 
@@ -1426,6 +1425,6 @@
 cycle = (uint32_t)hal_get_cycles();
 if( DEBUG_PROCESS_MAKE_EXEC < cycle )
-printk("\n[DBG] %s : thread %x in process %x reset VMM / cycle %d\n",
-__FUNCTION__, thread->trdid, pid, cycle );
+printk("\n[DBG] %s : thread[%x,%x] reset VMM / cycle %d\n",
+__FUNCTION__, pid, thread->trdid, cycle );
 #endif
 
@@ -1443,6 +1442,6 @@
 cycle = (uint32_t)hal_get_cycles();
 if( DEBUG_PROCESS_MAKE_EXEC < cycle )
-printk("\n[DBG] %s : thread %x in process %x / kentry/args/envs vsegs registered / cycle %d\n",
-__FUNCTION__, thread->trdid, pid, cycle );
+printk("\n[DBG] %s : thread[%x,%x] / kentry/args/envs vsegs registered / cycle %d\n",
+__FUNCTION__, pid, thread->trdid, cycle );
 #endif
 
@@ -1461,6 +1460,6 @@
 cycle = (uint32_t)hal_get_cycles();
 if( DEBUG_PROCESS_MAKE_EXEC < cycle )
-printk("\n[DBG] %s : thread %x in process %x / code/data vsegs registered / cycle %d\n",
-__FUNCTION__, thread->trdid, pid, cycle );
+printk("\n[DBG] %s : thread[%x,%x] / code/data vsegs registered / cycle %d\n",
+__FUNCTION__, pid, thread->trdid, cycle );
 #endif
 
