Index: trunk/kernel/kern/process.h
===================================================================
--- trunk/kernel/kern/process.h	(revision 527)
+++ trunk/kernel/kern/process.h	(revision 564)
@@ -1,4 +1,4 @@
 /*
- * process.h - process related management functions
+ * process.h - process related functions definition.
  *
  * Authors  Ghassan Almaless (2008,2009,2010,2011,2012)
@@ -33,5 +33,8 @@
 #include <xlist.h>
 #include <bits.h>
-#include <spinlock.h>
+#include <busylock.h>
+#include <queuelock.h>
+#include <remote_queuelock.h>
+#include <remote_rwlock.h>
 #include <hal_atomic.h>
 #include <vmm.h>
@@ -69,13 +72,19 @@
  * A free entry in this array contains the XPTR_NULL value.
  * The array size is defined by a the CONFIG_PROCESS_FILE_MAX_NR parameter.
- * All modifications (open/close) in this structure must be done by the reference cluster,
- * and reported in process copies.
+ *
+ * NOTE: - Only the fd_array[] in the reference process contains a complete list of open
+ *         files, and is protected by the lock against concurrent access.
+ *       - the fd_array[] in a process copy is simply a cache containing a subset of the
+ *         open files to speed the fdid to xptr translation, but the "lock" and "current
+ *         fields should not be used.
+ *       - all modifications made by the process_fd_remove() are done in reference cluster
+ *         and reported in all process_copies.
  ********************************************************************************************/
 
 typedef struct fd_array_s
 {
-	remote_spinlock_t lock;                               /*! lock protecting fd_array      */
-    uint32_t          current;                            /*! current number of open files  */
-	xptr_t            array[CONFIG_PROCESS_FILE_MAX_NR];  /*! xptr on open file descriptors */
+	remote_queuelock_t lock;                              /*! lock protecting fd_array      */
+    uint32_t           current;                           /*! current number of open files  */
+	xptr_t             array[CONFIG_PROCESS_FILE_MAX_NR]; /*! open file descriptors         */
 }
 fd_array_t;
@@ -100,50 +109,49 @@
  *    complete in the reference process cluster, other copies are read-only caches.
  * 4) The <sem_root>, <mutex_root>, <barrier_root>, <condvar_root>, and the associated
- *    <sync_lock>, that are dynamically allocated, are only defined in the reference cluster.
+ *    <sync_lock>, dynamically allocated, are only defined in the reference cluster.
  * 5) The <children_root>, <children_nr>, <children_list>, and <txt_list> fields are only
  *    defined in the reference cluster, and are undefined in other clusters.
- * 6) The <local_list>, <copies_list>, <th_tbl>, <th_nr>, <th_lock> fields
- *    are defined in all process descriptors copies.
+ * 6) The <local_list>, <copies_list>, <th_tbl>, <th_nr>, <u_th_lock> or <k_th_lock> fields
+ *    are specific n each cluster, and are defined in all process descriptors copies.
  * 7) The termination <flags> and <exit_status> are only defined in the reference cluster.
- *    The term_state format is defined in the shared_syscalls.h file.
+ *    (The term_state format is defined in the shared_syscalls.h file ).
  ********************************************************************************************/
 
 typedef struct process_s
 {
-	vmm_t             vmm;              /*! embedded virtual memory manager                 */
-
-	fd_array_t        fd_array;         /*! embedded open file descriptors array            */
-
-	xptr_t            vfs_root_xp;      /*! extended pointer on current VFS root inode      */
-	xptr_t            vfs_bin_xp;       /*! extended pointer on .elf file descriptor        */
-	pid_t             pid;              /*! process identifier                              */
-    xptr_t            ref_xp;           /*! extended pointer on reference process           */
-    xptr_t            owner_xp;         /*! extended pointer on owner process               */
-    xptr_t            parent_xp;        /*! extended pointer on parent process              */
-
-	xptr_t            vfs_cwd_xp;       /*! extended pointer on current working dir inode   */
-	remote_rwlock_t   cwd_lock;         /*! lock protecting working directory changes       */
-
-	xlist_entry_t     children_root;    /*! root of the children process xlist              */
-    remote_spinlock_t children_lock;    /*! lock protecting children process xlist          */
-    uint32_t          children_nr;      /*! number of children processes                    */
-
-	xlist_entry_t     children_list;    /*! member of list of children of same parent       */
-    xlist_entry_t     local_list;       /*! member of list of process in same cluster       */
-    xlist_entry_t     copies_list;      /*! member of list of copies of same process        */
-    xlist_entry_t     txt_list;         /*! member of list of processes sharing same TXT    */
-
-	spinlock_t        th_lock;          /*! lock protecting th_tbl[] concurrent access      */
-	uint32_t          th_nr;            /*! number of threads in this cluster               */
-
-	struct thread_s * th_tbl[CONFIG_THREAD_MAX_PER_CLUSTER]; /*! pointers on local threads  */
-
-    xlist_entry_t     sem_root;         /*! root of the process semaphore list              */
-    xlist_entry_t     mutex_root;       /*! root of the process mutex list                  */
-    xlist_entry_t     barrier_root;     /*! root of the process barrier list                */
-    xlist_entry_t     condvar_root;     /*! root of the process condvar list                */
-    remote_spinlock_t sync_lock;        /*! lock protecting sem,mutex,barrier,condvar lists */
-
-    uint32_t          term_state;       /*! termination status (flags & exit status)        */
+	vmm_t              vmm;              /*! embedded virtual memory manager                 */
+
+	fd_array_t         fd_array;         /*! embedded open file descriptors array            */
+
+	xptr_t             vfs_root_xp;      /*! extended pointer on current VFS root inode      */
+	xptr_t             vfs_bin_xp;       /*! extended pointer on .elf file descriptor        */
+	pid_t              pid;              /*! process identifier                              */
+    xptr_t             ref_xp;           /*! extended pointer on reference process           */
+    xptr_t             owner_xp;         /*! extended pointer on owner process               */
+    xptr_t             parent_xp;        /*! extended pointer on parent process              */
+
+	xptr_t             vfs_cwd_xp;       /*! extended pointer on current working dir inode   */
+	remote_rwlock_t    cwd_lock;         /*! lock protecting working directory changes       */
+
+	xlist_entry_t      children_root;    /*! root of the children process xlist              */
+    remote_queuelock_t children_lock;    /*! lock protecting children process xlist          */
+    uint32_t           children_nr;      /*! number of children processes                    */
+
+	xlist_entry_t      children_list;    /*! member of list of children of same parent       */
+    xlist_entry_t      local_list;       /*! member of list of process in same cluster       */
+    xlist_entry_t      copies_list;      /*! member of list of copies of same process        */
+    xlist_entry_t      txt_list;         /*! member of list of processes sharing same TXT    */
+
+	struct thread_s  * th_tbl[CONFIG_THREADS_MAX_PER_CLUSTER];       /*! local threads       */
+	uint32_t           th_nr;            /*! number of threads in this cluster               */
+    rwlock_t           th_lock;          /*! lock protecting th_tbl[]  i                     */ 
+
+    xlist_entry_t      sem_root;         /*! root of the user definedsemaphore list          */
+    xlist_entry_t      mutex_root;       /*! root of the user defined mutex list             */
+    xlist_entry_t      barrier_root;     /*! root of the user defined barrier list           */
+    xlist_entry_t      condvar_root;     /*! root of the user defined condvar list           */
+    remote_queuelock_t sync_lock;        /*! lock protecting user defined synchro lists      */
+
+    uint32_t           term_state;       /*! termination status (flags & exit status)        */
 }
 process_t;
@@ -210,7 +218,7 @@
 
 /*********************************************************************************************
- * This function initializes a local, reference, user process descriptor from another process
- * descriptor, defined by the <parent_xp> argument. The <process> and <pid> arguments must
- * be previously allocated by the caller. This function can be called by two functions:
+ * This function initializes a reference, user process descriptor from another process
+ * descriptor, defined by the <parent_xp> argument. The <process> and <pid> arguments 
+ * are previously allocated by the caller. This function can be called by two functions:
  * 1) process_init_create() : process is the INIT process; parent is process-zero.
  * 2) process_make_fork() : the parent process descriptor is generally remote.
@@ -411,44 +419,9 @@
 
 /*********************************************************************************************
- * This function uses as many remote accesses as required, to reset an entry in fd_array[],
- * in all clusters containing a copy. The entry is identified by the <fdid> argument.
- * This function must be executed by a thread running reference cluster, that contains
- * the complete list of process descriptors copies.
- *********************************************************************************************
- * @ process  : pointer on the local process descriptor.
- * @ fdid     : file descriptor index in the fd_array.
- ********************************************************************************************/
-void process_fd_remove( process_t * process,
-                        uint32_t    fdid );
-
-/*********************************************************************************************
- * This function returns an extended pointer on a file descriptor identified by its index
- * in fd_array. It can be called by any thread running in any cluster.
- * It accesses first the local process descriptor. In case of local miss, it uses remote
- * access to access the reference process descriptor.
- * It updates the local fd_array when the file descriptor exists in reference cluster.
- * The file descriptor refcount is not incremented.
- *********************************************************************************************
- * @ process  : pointer on the local process descriptor.
- * @ fdid     : file descriptor index in the fd_array.
- * @ return extended pointer on file descriptor if success / return XPTR_NULL if not found.
- ********************************************************************************************/
-xptr_t process_fd_get_xptr( process_t * process,
-                            uint32_t    fdid );
-
-/*********************************************************************************************
- * This function checks the number of open files for a given process.
- * It can be called by any thread in any cluster, because it uses portable remote access
- * primitives to access the reference process descriptor.
- *********************************************************************************************
- * @ returns true if file descriptor array full.
- ********************************************************************************************/
-bool_t process_fd_array_full( void );
-
-/*********************************************************************************************
  * This function allocates a free slot in the fd_array of the reference process,
  * register the <file_xp> argument in the allocated slot, and return the slot index.
  * It can be called by any thread in any cluster, because it uses portable remote access
  * primitives to access the reference process descriptor.
+ * It takes the lock protecting the reference fd_array against concurrent accesses.
  *********************************************************************************************
  * @ file_xp  : extended pointer on the file descriptor to be registered.
@@ -459,4 +432,34 @@
                              xptr_t      file_xp,
                              uint32_t  * fdid );
+
+/*********************************************************************************************
+ * This function uses as many remote accesses as required, to reset an entry in fd_array[],
+ * in all clusters containing a copy. The entry is identified by the <fdid> argument.
+ * This function must be executed by a thread running in reference cluster, that contains
+ * the complete list of process descriptors copies.
+ * It takes the lock protecting the reference fd_array against concurrent accesses.
+ * TODO this function is not implemented yet.
+ *********************************************************************************************
+ * @ process  : pointer on the local process descriptor.
+ * @ fdid     : file descriptor index in the fd_array.
+ ********************************************************************************************/
+void process_fd_remove( process_t * process,
+                        uint32_t    fdid );
+
+/*********************************************************************************************
+ * This function returns an extended pointer on a file descriptor identified by its index
+ * in fd_array. It can be called by any thread running in any cluster.
+ * It accesses first the local process descriptor. In case of local miss, it takes
+ * the lock protecting the reference fd_array, and access the reference process descriptor.
+ * It updates the local fd_array when the file descriptor exists in reference cluster.
+ * It takes the lock protecting the reference fd_array against concurrent accesses.
+ * The file descriptor refcount is not incremented.
+ *********************************************************************************************
+ * @ process  : pointer on the local process descriptor.
+ * @ fdid     : file descriptor index in the fd_array.
+ * @ return extended pointer on file descriptor if success / return XPTR_NULL if not found.
+ ********************************************************************************************/
+xptr_t process_fd_get_xptr( process_t * process,
+                            uint32_t    fdid );
 
 /*********************************************************************************************
@@ -465,5 +468,5 @@
  * <dst_xp> fd_array, embedded in another process descriptor. 
  * The calling thread can be running in any cluster.
- * It takes the remote lock protecting the <src_xp> fd_array during the copy.
+ * It takes the lock protecting the reference fd_array against concurrent accesses.
  * For each involved file descriptor, the refcount is incremented.
  *********************************************************************************************
@@ -474,4 +477,14 @@
                              xptr_t src_xp );
 
+/*********************************************************************************************
+ * This function checks the current number of open files for a given process.
+ * It can be called by any thread in any cluster, because it uses portable remote access
+ * primitives to access the reference process descriptor.
+ * It does not take the lock protecting the reference fd_array.
+ *********************************************************************************************
+ * @ returns true if file descriptor array full.
+ ********************************************************************************************/
+bool_t process_fd_array_full( void );
+
 
 
@@ -479,8 +492,7 @@
 
 /*********************************************************************************************
- * This function registers a new thread in the local process descriptor.
- * It checks that there is an available slot in the local th_tbl[] array,
- * allocates a new LTID, and registers the new thread in the th_tbl[].
- * It takes the lock protecting exclusive access to the th_tbl[].
+ * This function atomically registers a new thread in the local process descriptor.
+ * It checks that there is an available slot in the local th_tbl[] array, and allocates
+ * a new LTID using the relevant lock depending on the kernel/user type.
  *********************************************************************************************
  * @ process  : pointer on the local process descriptor.
@@ -494,6 +506,6 @@
 
 /*********************************************************************************************
- * This function removes a thread registration from the local process descriptor.
- * It takes the lock protecting exclusive access to the th_tbl[].
+ * This function atomically removes a thread registration from the local process descriptor
+ * th_tbl[] array, using the relevant lock, depending on the kernel/user type.
  *********************************************************************************************
  * @ thread   : local pointer on thread to be removed.
@@ -541,5 +553,6 @@
 
 /*********************************************************************************************
- * This function gives the TXT ownership to a process identified by the <process_xp> argument.
+ * This function gives a process identified by the <process_xp> argument the exclusive
+ * ownership of its attached TXT_RX terminal (i.e. put the process in foreground).
  * It can be called by a thread running in any cluster, but the <process_xp> must be the
  * owner cluster process descriptor. 
@@ -568,7 +581,7 @@
  * process_xp must be the owner cluster process descriptor. 
  *********************************************************************************************
- * @ return a non-zero value if target process is TXT owner.
- ********************************************************************************************/
-uint32_t process_txt_is_owner( xptr_t process_xp );
+ * @ returns true if target process is TXT owner.
+ ********************************************************************************************/
+bool_t process_txt_is_owner( xptr_t process_xp );
 
 /*********************************************************************************************
