Index: trunk/kernel/kern/chdev.c
===================================================================
--- trunk/kernel/kern/chdev.c	(revision 438)
+++ trunk/kernel/kern/chdev.c	(revision 440)
@@ -124,4 +124,5 @@
 {
     thread_t * server_ptr;    // local pointer on server thread associated to chdev
+    xptr_t     server_xp;     // extended pointer on server thread
     core_t   * core_ptr;      // local pointer on core running the server thread
     uint32_t   lid;           // core running the server thread local index
@@ -140,7 +141,17 @@
     thread_t * this = CURRENT_THREAD;
 
-    // get device descriptor cluster and local pointer
+    // get chdev cluster and local pointer
     cxy_t     chdev_cxy = GET_CXY( chdev_xp );
-    chdev_t * chdev_ptr = (chdev_t *)GET_PTR( chdev_xp );
+    chdev_t * chdev_ptr = GET_PTR( chdev_xp );
+
+    // get local and extended pointers on server thread
+    server_ptr = (thread_t *)hal_remote_lpt( XPTR( chdev_cxy , &chdev_ptr->server) );
+    server_xp  = XPTR( chdev_cxy , server_ptr );
+
+    // get local pointer on core running the server thread
+    core_ptr   = (core_t *)hal_remote_lpt( XPTR( chdev_cxy , &server_ptr->core ) );
+
+    // get server core local index
+    lid = hal_remote_lw( XPTR( chdev_cxy , &core_ptr->lid ) );
 
 #if (DEBUG_CHDEV_CMD_RX || DEBUG_CHDEV_CMD_TX)
@@ -162,42 +173,48 @@
 #endif
 
-    // build extended pointers on client thread xlist and device root
-    xptr_t  list_xp = XPTR( local_cxy , &this->wait_list );
-    xptr_t  root_xp = XPTR( chdev_cxy , &chdev_ptr->wait_root );
-
-    // get local pointer on server thread
-    server_ptr = (thread_t *)hal_remote_lpt( XPTR( chdev_cxy , &chdev_ptr->server) );
-
-    // build extended pointer on chdev lock protecting queue
+    // build extended pointer on client thread xlist
+    xptr_t  list_xp    = XPTR( local_cxy , &this->wait_list );
+
+    // build extended pointer on chdev waiting queue root
+    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 );
 
-    // get local pointer on core running the server thread
-    core_ptr = (core_t *)hal_remote_lpt( XPTR( chdev_cxy , &server_ptr->core ) );
-
-    // get core local index
-    lid = hal_remote_lw( XPTR( chdev_cxy , &core_ptr->lid ) );
-
-    // compute server core != thread core
-    different = (lid != this->core->lid) || (local_cxy != chdev_cxy);
-
-    // enter critical section to make atomic : 
-    // (1) client blocking
-    // (2) client registration in server queue 
-    // (3) IPI to force server scheduling
-    // (4) descheduling 
+    // critical section for the following sequence:
+    // (1) take the lock protecting waiting queue 
+    // (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
     // ... in this order
+
+    // enter critical section
     hal_disable_irq( &save_sr );
+
+    // take the lock
+    remote_spinlock_lock( lock_xp );
 
     // block current thread
     thread_block( XPTR( local_cxy , CURRENT_THREAD ) , THREAD_BLOCKED_IO );
 
+    if( hal_remote_lw( blocked_xp ) & THREAD_BLOCKED_IDLE )
+    thread_unblock( server_xp , THREAD_BLOCKED_IDLE );
+
     // register client thread in waiting queue 
-    remote_spinlock_lock( lock_xp );
     xlist_add_last( root_xp , list_xp );
-    remote_spinlock_unlock( lock_xp );
-
-    // send IPI to core running the server thread if required
+
+    // send IPI to core running the server thread when server != client
+    different = (lid != this->core->lid) || (local_cxy != chdev_cxy);
     if( different ) dev_pic_send_ipi( chdev_cxy , lid ); 
     
+    // release lock
+    remote_spinlock_unlock( lock_xp );
+
     // deschedule
     assert( thread_can_yield( this ) , __FUNCTION__ , "illegal sched_yield\n" );
@@ -260,18 +277,25 @@
             remote_spinlock_unlock( lock_xp );
 
+            // block 
+            thread_block( XPTR( local_cxy , server ) , THREAD_BLOCKED_IDLE ); 
+
             // deschedule
+            assert( thread_can_yield( server ) , __FUNCTION__ , "illegal sched_yield\n" );
             sched_yield("I/O queue empty");
         } 
         else                            // waiting queue not empty
         {
+            // get extended pointer on first client thread
+            client_xp = XLIST_FIRST_ELEMENT( root_xp , thread_t , wait_list );
+
+            // get client thread cluster and local pointer
+            client_cxy = GET_CXY( client_xp );
+            client_ptr = GET_PTR( client_xp );
+
+            // remove this first client thread from waiting queue
+            xlist_unlink( XPTR( client_cxy , &client_ptr->wait_list ) );
+
             // release lock 
             remote_spinlock_unlock( lock_xp );
-
-            // get extended pointer on first client thread
-            client_xp = XLIST_FIRST_ELEMENT( root_xp , thread_t , wait_list );
-
-            // get client thread cluster, local pointer, and identifier 
-            client_cxy = GET_CXY( client_xp );
-            client_ptr = (thread_t *)GET_PTR( client_xp );
 
 #if DEBUG_CHDEV_SERVER_RX
@@ -300,9 +324,4 @@
             chdev->cmd( client_xp );
         
-            // remove the client thread from waiting queue
-            remote_spinlock_lock( lock_xp );
-            xlist_unlink( XPTR( client_cxy , &client_ptr->wait_list ) );
-            remote_spinlock_unlock( lock_xp );
-
             // unblock client thread
             thread_unblock( client_xp , THREAD_BLOCKED_IO );
@@ -343,4 +362,7 @@
     chdev_t     * chdev_ptr;
 
+    assert( (file_xp != XPTR_NULL) , __FUNCTION__,
+    "file_xp == XPTR_NULL\n" );
+
     // get cluster and local pointer on remote file descriptor
     // associated inode and chdev are stored in same cluster as the file desc.
@@ -353,5 +375,5 @@
 
     assert( (inode_type == INODE_TYPE_DEV) , __FUNCTION__ ,
-    "inode type %d is not INODE_TYPE_DEV", inode_type );
+    "inode type %d is not INODE_TYPE_DEV\n", inode_type );
 
     // get chdev local pointer from inode extension
