Index: trunk/kernel/kern/thread.h
===================================================================
--- trunk/kernel/kern/thread.h	(revision 527)
+++ trunk/kernel/kern/thread.h	(revision 564)
@@ -3,5 +3,5 @@
  *
  * Author  Ghassan Almaless (2008,2009,2010,2011,2012)
- *         Alain Greiner (2016)
+ *         Alain Greiner (2016,2017,2018)
  *
  * Copyright (c) UPMC Sorbonne Universites
@@ -32,5 +32,5 @@
 #include <list.h>
 #include <hal_context.h>
-#include <spinlock.h>
+#include <remote_busylock.h>
 #include <core.h>
 #include <chdev.h>
@@ -92,4 +92,5 @@
 #define THREAD_BLOCKED_ISR       0x0400  /*! thread DEV wait ISR                      */
 #define THREAD_BLOCKED_WAIT      0x0800  /*! thread wait child process termination    */
+#define THREAD_BLOCKED_LOCK      0x1000  /*! thread wait queuelock or rwlock          */
 
 /***************************************************************************************
@@ -119,6 +120,22 @@
  * This TRDID is computed by the process_register_thread() function, when the user
  * thread is registered in the local copy of the process descriptor.
- * WARNING : Don't modify the first 4 fields order, as this order is used by the
- * hal_kentry assembly code for the TSAR architecture.
+ *
+ * WARNING (1) Don't modify the first 4 fields order, as this order is used by the
+ *             hal_kentry assembly code for some architectures (TSAR).
+ *
+ * WARNING (2) Most of the thread state is private and accessed only by this thread,
+ *             but some fields are shared, and can be modified by other threads.
+ *             - the "blocked" bit_vector can be modified by another thread
+ *               running in another cluster (using atomic instructions),
+ *               to change this thread scheduling status.
+ *             - the "flags" bit_vector can be modified by another thread
+ *               running in another cluster (using atomic instructions),
+ *               to register requests such as ACK or DELETE.
+ *             - the "join_xp" field can be modified by the joining thread,
+ *               and this rendez-vous is protected by the dedicated "join_lock".
+ *
+ * WARNING (3) When this thread is blocked on a shared resource (queuelock, condvar,
+ *             or chdev), it registers in the associated waiting queue, using the
+ *             "wait_list" (local list) or "wait_xlist" (trans-cluster list) fields. 
  **************************************************************************************/
 
@@ -144,5 +161,5 @@
     xptr_t              parent;          /*! extended pointer on parent thread        */
 
-    remote_spinlock_t   join_lock;       /*! lock protecting the join/exit            */
+    remote_busylock_t   join_lock;       /*! lock protecting the join/exit            */
     xptr_t              join_xp;         /*! joining/killer thread extended pointer   */
 
@@ -180,10 +197,12 @@
 	cxy_t               rpc_client_cxy;  /*! client cluster index (for a RPC thread)  */
 
-    xlist_entry_t       wait_list;       /*! member of threads blocked on same cond   */
-
-    list_entry_t        locks_root;      /*! root of list of locks taken              */
-    xlist_entry_t       xlocks_root;     /*! root of xlist of remote locks taken      */
-	uint32_t            local_locks;	 /*! number of local locks owned by thread    */
-	uint32_t            remote_locks;	 /*! number of remote locks owned by thread   */
+    list_entry_t        wait_list;       /*! member of a local waiting queue          */
+    xlist_entry_t       wait_xlist;      /*! member of a trans-cluster waiting queue  */
+
+	uint32_t            busylocks;	     /*! number of taken busylocks                */
+
+#if DEBUG_BUSYLOCK
+    xlist_entry_t       busylocks_root;  /*! root of xlist of taken busylocks         */
+#endif
 
 	thread_info_t       info;            /*! embedded thread_info_t                   */
@@ -311,5 +330,5 @@
 
 /***************************************************************************************
- * This function is called by the sched_handle_signals() function to releases 
+ * This low-level function is called by the sched_handle_signals() function to releases 
  * the physical memory allocated for a thread in a given cluster, when this thread 
  * is marked for delete. This include the thread descriptor itself, the associated 
@@ -363,18 +382,4 @@
  **************************************************************************************/
 void thread_reset_req_ack( thread_t * target );
-
-/***************************************************************************************
- * This function checks if the calling thread can deschedule.
- ***************************************************************************************
- * @ returns true if no locks taken.
- **************************************************************************************/
-inline bool_t thread_can_yield( void );
-
-/***************************************************************************************
- * This function implements the delayed descheduling mechanism : It is called  by 
- * all lock release functions, and calls the sched_yield() function when all locks 
- * have beeen released and the calling thread THREAD_FLAG_SCHED flag is set. 
- **************************************************************************************/
-void thread_check_sched( void );
 
 /***************************************************************************************
@@ -417,5 +422,5 @@
  * thread descriptor identified by the <thread_xp> argument.
  * We need an extended pointer, because the client thread of an I/O operation on a
- * given device is not in the same cluster as the associated device descriptor.
+ * given device is generally not in the same cluster as the associated server thread.
  * WARNING : this function does not reschedule the remote thread.
  * The scheduling can be forced by sending an IPI to the core running the remote thread.
@@ -432,8 +437,8 @@
  ***************************************************************************************
  * @ thread   : local pointer on target thread.
- * @ is_user  : update user time if non zero / update kernel time if zero
+ * @ is_user  : update user time if true / update kernel time if false
  **************************************************************************************/
 void thread_time_update( thread_t * thread,
-                         uint32_t   is_user );
+                         bool_t     is_user );
 
 /***************************************************************************************
@@ -449,4 +454,29 @@
                         trdid_t  trdid );
 
+/***************************************************************************************
+ * This function checks that the thread identified by the <thread> argument does hold
+ * any busylock (local or remote).
+ * If the xlist of taken busylocks is not empty, it displays the set of taken locks,
+ * and makes a kernel panic.  
+ ***************************************************************************************
+ * @ thread    : local pointer on target thread.
+ * @ func_str  : faulty function name.
+ **************************************************************************************/
+void thread_assert_can_yield( thread_t    * thread,
+                              const char  * func_str );
+
+/***************************************************************************************
+ * This debug function display the list of busylocks currently owned by a thread
+ * identified by the DEBUG_BUSYLOCK_THREAD_XP parameter.
+ * It is called each time the target thread acquire or release a busylock
+ * (local or remote). It is never called when DEBUG_BUSYLOCK_THEAD_CP == 0.
+ ***************************************************************************************
+ * @ lock_type  : type of acquired / released busylock.
+ * @ is_acquire : change is an acquire when true / change is a release when false.
+ **************************************************************************************/
+void thread_display_busylocks( uint32_t lock_type,
+                               bool_t   is_acquire );
+
+
 
 #endif	/* _THREAD_H_ */
