Index: trunk/kernel/kern/process.h
===================================================================
--- trunk/kernel/kern/process.h	(revision 14)
+++ trunk/kernel/kern/process.h	(revision 23)
@@ -4,5 +4,5 @@
  * Authors  Ghassan Almaless (2008,2009,2010,2011,2012)
  *          Mohamed Lamine Karaoui (2015)
- *          Alain Greiner (2016)
+ *          Alain Greiner (2016,2017)
  *
  * Copyright (c) UPMC Sorbonne Universites
@@ -54,13 +54,4 @@
 
 /*********************************************************************************************
- * These macros are used to compose or decompose global thread identifier (TRDID)
- * to or from cluster identifier / local thread index (CXY , LTID)
- ********************************************************************************************/
-
-#define LTID_FROM_TRDID( trdid )   (ltid_t)(trdid & 0x0000FFFF) 
-#define CXY_FROM_TRDID( trdid )    (cxy_t)(trdid >> 16)
-#define TRDID( cxy , ltid )        (trdid_t)((cxy << 16) | ltid )
-
-/*********************************************************************************************
  * This structure defines an array of extended pointers on the open file descriptors
  * for a given process. We use an extended pointer because the open file descriptor
@@ -68,5 +59,6 @@
  * 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) must be done by the reference cluster, and reported
+ * All modifications (open/close) in this structure must be done by the reference cluster,
+ * and reported in process copies.
  ********************************************************************************************/
 
@@ -75,5 +67,4 @@
 	remote_spinlock_t lock;             /*! lock protecting fd_array[] change.              */
     uint32_t          current;          /*! current number of open files                    */
-    uint32_t          max;              /*! max number of open files (can be increased)     */
 	xptr_t            array[CONFIG_PROCESS_FILE_MAX_NR];  
 }
@@ -88,46 +79,57 @@
  * allocated to this cluster.
  * The process descriptor for a PID process is replicated in all clusters containing
- * at least one thread of the PID process, with some restrictions:
- * - The list of registered vsegs, the page table (contained in vmm), and the fd_array[]
- *   are only complete in the reference process descriptor, other copies are read-only caches.
- * - The following fields are NOT defined in copies other than the reference:
- *   children_root , children_list , children_nr , sig_mgr
+ * at least one thread of the PID process, with the following rules :
+ *
+ * 1) The <pid>, <ppid>, <ref_xp>, <vfs_root_xp>, <vfs_bin_xp>  fields are defined
+ *    in all process descriptor copies.
+ * 2) The <vfs_cwd_xp> and associated <cwd_lock>, that can be dynamically modified,
+ *    are only defined in the reference process descriptor.
+ * 2) The <vmm>, containing the list of registered vsegs, and the page table, are only
+ *    complete in the reference process cluster, other copies are read-only caches.
+ * 3) the <fd_array>, containing extended pointers on the open file descriptors, is only
+ *    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.
+ * 5) The <children_root>, and <children_nr> fields are only defined in the reference
+ *    cluster, and are undefined in other clusters.
+ * 6) The <brothers_list>, <local_list>, <copies_list>, <th_tbl>, <th_nr>, <th_lock> fields
+ *    are defined in all process descriptors copies.
+ * 7) The <sig_mgr> field is only defined in the reference cluster. TODO 
  ********************************************************************************************/
 
 typedef struct process_s
 {
-	vmm_t            vmm;               /*! embedded virtual memory manager                 */
-
-	fd_array_t       fd_array;          /*! embedded file descriptors array                 */
-
-	xptr_t           vfs_root_xp;       /*! extende pointer on current file system root     */
-	xptr_t           vfs_cwd_xp;        /*! extended pointer on current working directory   */
-	xptr_t           vfs_bin_xp;        /*! extended pointer on associate .elf file         */
-
-	spinlock_t       cd_lock;           /*! lock protecting working directory changes       */
+	vmm_t             vmm;              /*! embedded virtual memory manager                 */
+
+	fd_array_t        fd_array;         /*! embedded open file descriptors array            */
+
+	xptr_t            vfs_root_xp;      /*! extende pointer on current VFS root inode       */
+	xptr_t            vfs_bin_xp;       /*! extended pointer on .elf file inode             */
+	pid_t             pid;              /*! process identifier                              */
+	pid_t             ppid;             /*! parent process identifier                       */
+    xptr_t            ref_xp;           /*! extended pointer on reference process           */
+
+	xptr_t            vfs_cwd_xp;       /*! extended pointer on current working dir inode   */
+	remote_rwlock_t   cwd_lock;         /*! lock protecting working directory changes       */
  
-	pid_t            pid;               /*! process identifier                              */
-	pid_t            ppid;              /*! parent process identifier                       */
-	bool_t           is_ref;            /*! this is the reference process if true           */
-    xptr_t           ref_xp;            /*! extended pointer on reference process           */
-
-	xlist_entry_t    children_root;     /*! root of the children process xlist              */
-    uint32_t         children_nr;       /*! number of children processes                    */
-
-	xlist_entry_t    brothers_list;     /*! member of list of children of same parent       */
-
-    list_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        */
-
-	spinlock_t       th_lock;           /*! lock protecting th_tbl[] concurrent access      */
-	uint32_t         th_nr;             /*! number of threads in this cluster               */
-
+	xlist_entry_t     children_root;    /*! root of the children process xlist              */
+    uint32_t          children_nr;      /*! number of children processes                    */
+
+	xlist_entry_t     brothers_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        */
+
+	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 semaphores list             */
-    uint32_t         sem_nr;            /*! number of semaphores                            */
-
-	sig_mgr_t        sig_mgr;           /*! embedded signal manager TODO [AG]               */
+    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 */
+
+	sig_mgr_t         sig_mgr;          /*! embedded signal manager TODO [AG]               */
 }
 process_t;
@@ -149,5 +151,5 @@
     xptr_t             vfs_bin_xp;     /*! extended pointer on process .elf file            */
 
-    char               path[256];      /*! pathname for  .elf file                          */
+    char               path[CONFIG_VFS_MAX_PATH_LENGTH];   /*!  .elf file path              */
 
     char            ** args_pointers;  /*! physical base address of array of pointers       */
@@ -162,10 +164,4 @@
 exec_info_t;
 
-/*********************************************************************************************
- * This macro returns a pointer on the process descriptor for the calling thread.
- ********************************************************************************************/
-
-#define CURRENT_PROCESS (hal_get_current_thread()->process)
-
 /***************   Process Descriptor Operations    *****************************************/
 
@@ -204,5 +200,5 @@
  * The PID value must have been defined previously by the owner cluster manager.
  * The reference cluster can be different from the owner cluster. 
- * It set the pid / ppid / is_ref / pref / fields.
+ * It set the pid / ppid / ref_xp fields.
  * It registers this process descriptor in three lists:
  * - the children_list in the parent process descriptor.
@@ -232,5 +228,5 @@
 /********************************************************************************************* 
  * This function releases all memory allocated for a process descriptor in the local cluster,
- * after releasing memory allocated for embedded substructures (fd_array, vmm, etc).
+ * including memory allocated for embedded substructures (fd_array, vmm, etc).
  * The local th_tbl[] array must be empty.
  ********************************************************************************************* 
@@ -255,5 +251,5 @@
  * If this local copy does not exist yet, it is dynamically created, from the reference 
  * process descriptor, registered in the global copies_list, and registered in the local_list.
- * This function is used by both thread_user_create() and thread_user_copy() functions.
+ * This function is used by the thread_user_create() function.
  ********************************************************************************************* 
  * @ pid     : searched process identifier.
@@ -288,5 +284,5 @@
 
 /*********************************************************************************************
- * This function reset the file descriptor array for a given process: no open file.
+ * This function initialises all entries of the local fd_array as empty.
  ********************************************************************************************* 
  * @ process  : pointer on the local process descriptor.
@@ -295,52 +291,51 @@
 
 /*********************************************************************************************
- * This function closes all open files and releases all file descriptors.
+ * 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 <file_id> argument.
+ * This function must be executed by a thread running reference cluster, that contain
+ * the complete list of process descriptors copies.
  ********************************************************************************************* 
  * @ process  : pointer on the local process descriptor.
- ********************************************************************************************/
-void process_fd_destroy( process_t * process );
+ * @ file_id  : file descriptor index in the fd_array.
+ ********************************************************************************************/
+void process_fd_remove( process_t * process,
+                        uint32_t    file_id );
+
+/*********************************************************************************************
+ * 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 access 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 exist in reference cluster.
+ * The file descriptor refcount is not incremented.
+ ********************************************************************************************* 
+ * @ process  : pointer on the local process descriptor.
+ * @ file_id  : 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    file_id );
 
 /*********************************************************************************************
  * This function checks the number of open files for a given process. 
- ********************************************************************************************* 
- * @ process  : pointer on the local process descriptor.
+ * 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( process_t * process );
-
-/*********************************************************************************************
- * These functions allocates a slot in the fd_array for a given process,
- * and returns the file descriptor index.
- ********************************************************************************************* 
- * @ process  : pointer on the local process descriptor.
- * @ file     : extended pointer on the file descriptor.
- * @ fd       : [out] file descriptor index
+bool_t process_fd_array_full();
+
+/*********************************************************************************************
+ * 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.
+ ********************************************************************************************* 
+ * @ file_xp  : extended pointer on the file descriptor to be registered.
+ * @ file_id  : [out] buffer for fd_array slot index.
  * @ return 0 if success / return EMFILE if array full.
  ********************************************************************************************/
-error_t process_fd_allocate( process_t * process, 
-                             xptr_t      file_xp, 
-                             uint32_t  * fd );
-
-/*********************************************************************************************
- * This function releases an existing file descriptor from the process fd_array. 
- ********************************************************************************************* 
- * @ process  : pointer on the local process descriptor.
- * @ return 0 if success / return EBADF if illegal file descriptor.
- ********************************************************************************************/
-error_t process_fd_release( process_t * process,
-                            uint32_t    fd );
-
-/*********************************************************************************************
- * This function copies all non-zero entries from a local <src> fd_array,
- * embedded in a process descriptor, to another local <dst_xp> fd_array, embedded
- * in another process descriptor. 
- * It takes the remote lock protecting the <src> fd_array during the copy.
- * For each involved file descriptor, the refcount is incremented. 
- ********************************************************************************************* 
- * @ dst   : pointer on the destination fd_array_t.
- * @ src   : pointer on the source fd_array_t.
- ********************************************************************************************/
-void process_fd_local_copy( fd_array_t * dst,
-                            fd_array_t * src );
+error_t process_fd_register( xptr_t      file_xp, 
+                             uint32_t  * file_id );
 
 /*********************************************************************************************
@@ -358,4 +353,5 @@
 
 
+
 /********************   Thread Related Operations   *****************************************/ 
 
