Index: trunk/kernel/libk/rwlock.h
===================================================================
--- trunk/kernel/libk/rwlock.h	(revision 457)
+++ trunk/kernel/libk/rwlock.h	(revision 563)
@@ -1,4 +1,4 @@
 /*
- * rwlock.h - kernel read/write lock definition.
+ * rwlock.h - kernel local read/write lock definition.
  * 
  * Author   Alain Greiner    (2016,2017,2018)
@@ -27,41 +27,36 @@
 #include <kernel_config.h>
 #include <hal_kernel_types.h>
+#include <busylock.h>
 #include <list.h>
 
-/*******************************************************************************************
- * This structure defines a local rwlock, that supports several simultaneous read 
- * accesses, but only one write access. It implements a ticket based allocation policy.
- * Both readers and writers must take a ticket before doing anything else, and access
- * are done in same order as requests (for both read an write ).
- * - A reader take the lock to atomically increments the registered readers count.
- *   Then it release the lock and access the protected structure. It atomically decrement
- *   the readers count without taking the lock when access is completed.
- * - A writer take the lock and keep it, but must wait completion of all current read 
- *   accesses before starting its own access.
- * As this local lock is only accessed by the local threads, if the lock is taken, 
- * the new-comers use a busy waiting policy with a delay between retry.
- * TODO : Introduce the rwlocks in the list of locks taken by a given thread for debug.
+/******************************************************************************************n
+ * This structure defines a kernel, local, read/write lock, supporting several simultaneous
+ * read accesses, but only one write access to a given locally shared object in a cluster.
+ * Both readers and writers take the associated busylock before accessing or updating
+ * the rwlock state, and releases the busylock after rwlock state update.
+ * - when a reader try to access the object, it increments the readers "count" when the 
+ *   lock is not "taken" by a writer. It registers in the "rd_root" waiting queue, blocks,
+ *   and deschedules when the lock is taken.
+ * - when a writer try to take the rwlock, it check the "taken" field. If the lock is already
+ *   taken, or if the number of readers is non zero, it registers in the "wr_root" waiting
+ *   queue, blocks, and deschedules. It set "taken" otherwise.
+ * - when a reader completes its access, it decrement the readers "count", unblock the
+ *   the first waiting writer if there is no other readers, and unblock all waiting
+ *   readers if there no write request.
+ * - when a  writer completes its access, it reset the "taken" field, releases the first
+ *   waiting writer if queue non empty, or releases all waiting readers if no writer.
  ******************************************************************************************/
-
-/****     Forward declarations    ****/
-
-struct thread_s;
 
 /*******************************************************************************************
  * This structure defines a local rwlock.
- * The "owner" and "list" fields are used for debug.
  ******************************************************************************************/
 
 typedef struct rwlock_s
 {
-	uint32_t            ticket;           /*! first free ticket index                     */
-    uint32_t            current;          /*! ticket index of current owner               */
-    uint32_t            count;            /*! number of simultaneous readers threads      */
-
-#if DEBUG_RWLOCKS
-	struct thread_s   * owner;            /*! pointer on curent writer thread             */
-    list_entry_t        list;             /*! member of list of locks taken by owner      */
-#endif
-
+    busylock_t          lock;        /*! busylock protecting the rwlock state             */
+	volatile uint32_t   taken;       /*! lock taken by an exclusive writer if non zero    */
+    volatile uint32_t   count;       /*! current number of simultaneous readers threads   */
+    list_entry_t        rd_root;     /*! root of list of waiting readers                  */
+    list_entry_t        wr_root;     /*! root of list of waiting writers                  */
 }
 rwlock_t;
@@ -69,8 +64,12 @@
 /*******************************************************************************************
  * This function initializes a local rwlock.
+ * The <type> argument defines the lock usage and is only used for debug.
+ * This type is actually stored in the associated busylock descriptor.
  *******************************************************************************************
- * @ lock       : pointer on rwlock
+ * @ lock       : pointer on rwlock.
+ * @ type       : lock usage for debug.
  ******************************************************************************************/
-void rwlock_init( rwlock_t * lock );
+void rwlock_init( rwlock_t * lock,
+                  uint32_t   type );
 
 /*******************************************************************************************
@@ -79,5 +78,5 @@
  * @ lock       : pointer on rwlock
  ******************************************************************************************/
-void rwlock_rd_lock( rwlock_t * lock );
+void rwlock_rd_acquire( rwlock_t * lock );
 
 /*******************************************************************************************
@@ -86,5 +85,5 @@
  * @ lock       : pointer on rwlock
  ******************************************************************************************/
-void rwlock_wr_lock( rwlock_t * lock );
+void rwlock_wr_acquire( rwlock_t * lock );
 
 /*******************************************************************************************
@@ -93,5 +92,5 @@
  * @ lock       : pointer on rwlock
  ******************************************************************************************/
-void rwlock_rd_unlock( rwlock_t * lock );
+void rwlock_rd_release( rwlock_t * lock );
 
 /*******************************************************************************************
@@ -100,5 +99,5 @@
  * @ lock       : pointer on rwlock
  ******************************************************************************************/
-void rwlock_wr_unlock( rwlock_t * lock );
+void rwlock_wr_release( rwlock_t * lock );
 
 
