Index: trunk/kernel/kern/chdev.c
===================================================================
--- trunk/kernel/kern/chdev.c	(revision 619)
+++ trunk/kernel/kern/chdev.c	(revision 625)
@@ -138,5 +138,4 @@
     uint32_t   server_lid;    // core running the server thread local index
     xptr_t     lock_xp;       // extended pointer on lock protecting the chdev state
-    uint32_t   save_sr;       // for critical section
 
 #if (DEBUG_SYS_READ & 1)
@@ -177,5 +176,5 @@
 uint32_t rx_cycle = (uint32_t)hal_get_cycles();
 if( (is_rx) && (DEBUG_CHDEV_CMD_RX < rx_cycle) )
-printk("\n[%s] client[%x,%x] enter for RX / server[%x,%x] / cycle %d\n",
+printk("\n[%s] client thread[%x,%x] enter for RX / server[%x,%x] / cycle %d\n",
 __FUNCTION__, this->process->pid, this->trdid, server_pid, server_trdid, rx_cycle );
 #endif
@@ -184,5 +183,5 @@
 uint32_t tx_cycle = (uint32_t)hal_get_cycles();
 if( (is_rx == 0) && (DEBUG_CHDEV_CMD_TX < tx_cycle) )
-printk("\n[%s] client[%x,%x] enter for TX / server[%x,%x] / cycle %d\n",
+printk("\n[%s] client thread[%x,%x] enter for TX / server[%x,%x] / cycle %d\n",
 __FUNCTION__, this->process->pid, this->trdid, server_pid, server_trdid, tx_cycle );
 #endif
@@ -194,62 +193,19 @@
     xptr_t  root_xp    = XPTR( chdev_cxy , &chdev_ptr->wait_root );
 
-    // build extended pointer on server thread blocked state
-    xptr_t  blocked_xp = XPTR( chdev_cxy , &server_ptr->blocked );
-
     // build extended pointer on lock protecting chdev waiting queue
     lock_xp            = XPTR( chdev_cxy , &chdev_ptr->wait_lock );
 
-    // TODO the hal_disable_irq() / hal_restore_irq() 
-    // in the sequence below is probably useless, as it is 
-    // already done by the busylock_acquire() / busylock_release()
-    // => remove it [AG] october 2018
-
-    // critical section for the following sequence: 
+    // The following actions execute in critical section,
+    // because the lock_acquire / lock_release : 
     // (1) take the lock protecting the chdev state
-    // (2) block the client thread
-    // (3) unblock the server thread if required
-    // (4) register client thread in server queue 
-    // (5) send IPI to force server scheduling
-    // (6) release the lock protecting waiting queue
-    // (7) deschedule
-
-    // enter critical section
-    hal_disable_irq( &save_sr );
-
-    // take the lock protecting chdev queue
+    // (2) register client thread in server queue 
+    // (3) unblock the server thread and block client thread
+    // (4) send IPI to force server scheduling
+    // (5) release the lock protecting waiting queue
+
+    // 1. take the lock protecting chdev queue
     remote_busylock_acquire( lock_xp );
 
-    // block current thread
-    thread_block( XPTR( local_cxy , CURRENT_THREAD ) , THREAD_BLOCKED_IO );
-
-#if (DEBUG_CHDEV_CMD_TX & 1)
-if( (is_rx == 0) && (DEBUG_CHDEV_CMD_TX < tx_cycle) )
-printk("\n[%s] client thread[%x,%x] blocked\n",
-__FUNCTION__, this->process->pid, this->trdid );
-#endif
-
-#if (DEBUG_CHDEV_CMD_RX & 1)
-if( (is_rx) && (DEBUG_CHDEV_CMD_RX < rx_cycle) )
-printk("\n[%s] client thread[%x,%x] blocked\n",
-__FUNCTION__, this->process_pid, this->trdid );
-#endif
-
-    // unblock server thread if required
-    if( hal_remote_l32( blocked_xp ) & THREAD_BLOCKED_IDLE )
-    thread_unblock( server_xp , THREAD_BLOCKED_IDLE );
-
-#if (DEBUG_CHDEV_CMD_TX & 1)
-if( (is_rx == 0) && (DEBUG_CHDEV_CMD_TX < tx_cycle) )
-printk("\n[%s] TX server thread[%x,%x] unblocked\n",
-__FUNCTION__, server_pid, server_trdid );
-#endif
-
-#if (DEBUG_CHDEV_CMD_RX & 1)
-if( (is_rx) && (DEBUG_CHDEV_CMD_RX < rx_cycle) )
-printk("\n[%s] RX server thread[%x,%x] unblocked\n",
-__FUNCTION__, server_pid, server_trdid );
-#endif
-
-    // register client thread in waiting queue 
+    // 2. register client thread in waiting queue 
     xlist_add_last( root_xp , list_xp );
 
@@ -266,5 +222,21 @@
 #endif
  
-    // send IPI to core running the server thread when server core != client core
+    // 3. client thread unblocks server thread and blocks itself
+    thread_unblock( server_xp , THREAD_BLOCKED_IDLE );
+    thread_block( XPTR( local_cxy , CURRENT_THREAD ) , THREAD_BLOCKED_IO );
+
+#if (DEBUG_CHDEV_CMD_TX & 1)
+if( (is_rx == 0) && (DEBUG_CHDEV_CMD_TX < tx_cycle) )
+printk("\n[%s] client thread[%x,%x] unblock server thread[%x,%x] and block itsef\n",
+__FUNCTION__, this->process->pid, this->trdid, server_pid, server_trdid );
+#endif
+
+#if (DEBUG_CHDEV_CMD_RX & 1)
+if( (is_rx) && (DEBUG_CHDEV_CMD_RX < rx_cycle) )
+printk("\n[%s] client thread[%x,%x] unblock server thread[%x,%x] and block itsef\n",
+__FUNCTION__, this->process->pid, this->trdid, server_pid, server_trdid );
+#endif
+
+    // 4. send IPI to core running the server thread when server core != client core
     if( (server_lid != this->core->lid) || (local_cxy != chdev_cxy) )
     {
@@ -285,17 +257,14 @@
     }
  
-    // release lock protecting chdev queue
+    // 5. release lock protecting chdev queue
     remote_busylock_release( lock_xp );
 
     // deschedule
     sched_yield("blocked on I/O");
-
-    // exit critical section
-    hal_restore_irq( save_sr );
 
 #if DEBUG_CHDEV_CMD_RX
 rx_cycle = (uint32_t)hal_get_cycles();
 if( (is_rx) && (DEBUG_CHDEV_CMD_RX < rx_cycle) )
-printk("\n[%s] client_thread[%x,%x] exit for RX / cycle %d\n",
+printk("\n[%s] client thread[%x,%x] exit for RX / cycle %d\n",
 __FUNCTION__, this->process->pid, this->trdid, rx_cycle );
 #endif
@@ -304,5 +273,5 @@
 tx_cycle = (uint32_t)hal_get_cycles();
 if( (is_rx == 0) && (DEBUG_CHDEV_CMD_TX < tx_cycle) )
-printk("\n[%s] client_thread[%x,%x] exit for TX / cycle %d\n",
+printk("\n[%s] client thread[%x,%x] exit for TX / cycle %d\n",
 __FUNCTION__, this->process->pid, this->trdid, tx_cycle );
 #endif
@@ -344,5 +313,5 @@
 uint32_t rx_cycle = (uint32_t)hal_get_cycles();
 if( (chdev->is_rx) && (DEBUG_CHDEV_SERVER_RX < rx_cycle) )
-printk("\n[%s] DEV thread[%x,%x] check TXT_RX channel %d / cycle %d\n",
+printk("\n[%s] server thread[%x,%x] check TXT_RX channel %d / cycle %d\n",
 __FUNCTION__ , server->process->pid, server->trdid, chdev->channel, rx_cycle );
 #endif
@@ -370,5 +339,5 @@
 rx_cycle = (uint32_t)hal_get_cycles();
 if( (chdev->is_rx) && (DEBUG_CHDEV_SERVER_RX < rx_cycle) )
-printk("\n[%s] thread[%x,%x] found RX queue empty => blocks / cycle %d\n",
+printk("\n[%s] server thread[%x,%x] found RX queue empty => blocks / cycle %d\n",
 __FUNCTION__ , server->process->pid, server->trdid, rx_cycle );
 #endif
@@ -377,5 +346,5 @@
 tx_cycle = (uint32_t)hal_get_cycles();
 if( (chdev->is_rx == 0) && (DEBUG_CHDEV_SERVER_TX < tx_cycle) )
-printk("\n[%s] thread[%x,%x] found TX queue empty => blocks / cycle %d\n",
+printk("\n[%s] server thread[%x,%x] found TX queue empty => blocks / cycle %d\n",
 __FUNCTION__ , server->process->pid, server->trdid, tx_cycle );
 #endif
@@ -407,5 +376,5 @@
 rx_cycle = (uint32_t)hal_get_cycles();
 if( (chdev->is_rx) && (DEBUG_CHDEV_SERVER_RX < rx_cycle) )
-printk("\n[%s] thread[%x,%x] for RX get client thread[%x,%x] / cycle %d\n",
+printk("\n[%s] server thread[%x,%x] get command from client thread[%x,%x] / cycle %d\n",
 __FUNCTION__, server->process->pid, server->trdid, client_pid, client_trdid, rx_cycle );
 #endif
@@ -414,5 +383,5 @@
 tx_cycle = (uint32_t)hal_get_cycles();
 if( (chdev->is_rx == 0) && (DEBUG_CHDEV_SERVER_TX < tx_cycle) )
-printk("\n[%s] thread[%x,%x] for TX get client thread[%x,%x] / cycle %d\n",
+printk("\n[%s] server thread[%x,%x] get command from client thread[%x,%x] / cycle %d\n",
 __FUNCTION__, server->process->pid, server->trdid, client_pid, client_trdid, tx_cycle );
 #endif
@@ -445,5 +414,5 @@
 rx_cycle = (uint32_t)hal_get_cycles();
 if( (chdev->is_rx) && (DEBUG_CHDEV_SERVER_RX < rx_cycle) )
-printk("\n[%s] thread[%x,%x] completes RX for client thread[%x,%x] / cycle %d\n",
+printk("\n[%s] thread[%x,%x] completes command for client thread[%x,%x] / cycle %d\n",
 __FUNCTION__, server->process->pid, server->trdid, client_pid, client_trdid, rx_cycle );
 #endif
@@ -452,5 +421,5 @@
 tx_cycle = (uint32_t)hal_get_cycles();
 if( (chdev->is_rx == 0) && (DEBUG_CHDEV_SERVER_TX < tx_cycle) )
-printk("\n[%s] thread[%x,%x] completes TX for client thread[%x,%x] / cycle %d\n",
+printk("\n[%s] thread[%x,%x] completes command for client thread[%x,%x] / cycle %d\n",
 __FUNCTION__, server->process->pid, server->trdid, client_pid, client_trdid, tX_cycle );
 #endif
