Index: trunk/kernel/kern/thread.h
===================================================================
--- trunk/kernel/kern/thread.h	(revision 16)
+++ trunk/kernel/kern/thread.h	(revision 23)
@@ -3,5 +3,4 @@
  * 
  * Author  Ghassan Almaless (2008,2009,2010,2011,2012)
- *         Mohamed Lamine Karaoui (2015)
  *         Alain Greiner (2016)
  *
@@ -43,30 +42,38 @@
 
 /***************************************************************************************
- * This defines the various pthread_attr_t flags.
- **************************************************************************************/
-
-#define PT_FLAG_DETACH                0x001    // user defined not joinable
-#define PT_FLAG_CLUSTER_DEFINED       0x002    // user defined target cluster
-#define PT_FLAG_CORE_DEFINED          0x004    // user defined core cluster
-
-/***************************************************************************************
- * This structure defines the input argument of the pthread_create() system call.
- * It contains all informations required to initialize an user thread, and is passed
- * as input argument to the thread_user_create() function.
- * It is partly set by the kernel, partly set by the user application itself,
- * using the pthread_attr_***() functions.
+ * 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 defines the various pthread_attr_t attributes bit-vector.
+ **************************************************************************************/
+
+/***************************************************************************************
+ * This opaque structure contains the user defined attributes for an user thread.
+ * It is passed as input argument to the thread_user_create() function.
+ * It is set by the user application itself, using the pthread_attr_***() functions.
+ * The currently supported attributes are defined below.
  **************************************************************************************/
  
 typedef struct pthread_attr_s
 {
-    pid_t       pid;             /*! owner process identifier                         */
-	void      * entry_func;      /*! pointer on entry function                        */
-	void      * entry_args;      /*! pointer on entry function arguments              */
-	uint32_t    flags;           /*! pthread flags        (entirely user specified)   */
-	cxy_t       cxy;             /*! target cluster       (can be user specified)     */
-	lid_t       lid;             /*! target core          (can be user specified)     */
+	uint32_t    attributes;      /*! user defined attributes bit vector               */
+	cxy_t       cxy;             /*! target cluster identifier                        */
+	lid_t       lid;             /*! target core index                                */
 } 
 pthread_attr_t;
 
+typedef enum
+{
+    PT_ATTR_DETACH          = 0x0001,  /*! user defined not joinable                  */
+    PT_ATTR_CLUSTER_DEFINED = 0x0002,  /*! user defined target cluster                */
+    PT_ATTR_CORE_DEFINED    = 0x0004,  /*! user defined core index in cluster         */
+}
+pt_attributes_t;
 
 /***************************************************************************************
@@ -86,9 +93,11 @@
 
 /***************************************************************************************
- * This defines the masks associated to the thread flags.
+ * This defines the thread flags bit-vector.
  **************************************************************************************/
 
 #define THREAD_FLAG_LOADABLE     0x0001  /*! This thread has not been executed yet    */
-#define THREAD_FLAG_DETACHED     0x0002  /*! This user thread is detached from parent */
+#define THREAD_FLAG_DETACHED     0x0002  /*! This thread is detached from parent      */
+#define THREAD_FLAG_JOIN         0x0004  /*! Parent thread made a join                */
+#define THREAD_FLAG_EXIT         0x0008  /*! This thread made an exit                 */
 
 /***************************************************************************************
@@ -102,11 +111,14 @@
  **************************************************************************************/
 
-#define THREAD_BLOCKED_GLOBAL    0x0001  /*! thread global blocking                   */ 
+#define THREAD_BLOCKED_GLOBAL    0x0001  /*! thread desactivated / wait activation    */ 
 #define THREAD_BLOCKED_IO        0x0002  /*! thread wait IO operation completion      */
 #define THREAD_BLOCKED_MAPPER    0x0004  /*! thread wait mapper                       */
-#define THREAD_BLOCKED_EXIT      0x0010  /*! thread requested exit                    */
+#define THREAD_BLOCKED_JOIN      0x0008  /*! thread blocked in join / wait exit       */
+#define THREAD_BLOCKED_EXIT      0x0010  /*! thread blocked in exit / wait join i     */
 #define THREAD_BLOCKED_KILL      0x0020  /*! thread received kill signal              */
 #define THREAD_BLOCKED_SEM       0x0040  /*! thread wait semaphore                    */
 #define THREAD_BLOCKED_PAGE      0x0080  /*! thread wait page access                  */
+#define THREAD_BLOCKED_USERSYNC  0x0100  /*! thread wait POSIX (cond/mutex/barrier)   */
+
 #define THREAD_BLOCKED_IDLE      0x1000  /*! thread RPC wait activation               */
 #define THREAD_BLOCKED_DEV_QUEUE 0x2000  /*! thread DEV wait queue                    */
@@ -141,5 +153,5 @@
  * that is returned by the kernel to the user:
  * - The TRDID 16 LSB bits contain the LTID (Local Thread Index).
- * - The TRDID 16 MSB bits contain the owner cluster CXY.
+ * - The TRDID 16 MSB bits contain the CXY of cluster containing the thread.
  * - The LTID is used to index the th_tbl[] array in the local process descriptor.
  * This TRDID is computed by the process_register_thread() function, when the user
@@ -155,5 +167,5 @@
 	void              * fpu_context;     /*! used for dynamic FPU allocation          */
 
-	uint32_t            trdid;           /*! thread index (in THTBL)                  */
+	uint32_t            trdid;           /*! thread index (cxy.ltid)                  */
 	thread_type_t       type;            /*! thread type                              */
 	uint32_t            quantum;         /*! number of clock ticks given to thread    */
@@ -162,7 +174,12 @@
 	core_t            * core;            /*! pointer to the owner core                */
 	process_t         * process;         /*! pointer on local process descriptor      */
-
+    xptr_t              parent;          /*! extended pointer on parent thread        */       
+
+    void              * exit_value;      /*! exit_value used in case of join          */
+    
 	uint32_t            local_locks;	 /*! number of local locks owned by thread    */
     list_entry_t        locks_root;      /*! root of local locks list                 */
+
+    remote_spinlock_t * flags_lock;      /*! lock protecting the flags                */
 
 	uint32_t            remote_locks;	 /*! number of local locks owned by thread    */
@@ -182,4 +199,5 @@
 
 	error_t             errno;           /*! errno value set by last system call      */
+    uint32_t            utls;            /*! user thread local storage                */
 
     bool_t              fork_user;       /*! user defined placement for next fork()   */
@@ -231,23 +249,25 @@
 /***************************************************************************************
  * This function allocates memory for an user thread descriptor in the local cluster,
- * and initializes it from information contained in the "attr" argument.
- * It is used by the pthread_create system call, the CPU context is initialised from
- * scratch, and the "loadable" field is set.
- * The new thread is attached to the core specified in the "attr" argument.
- * It is registered in the local process descriptor for the process specified in "attr".
+ * and initializes it from information contained in the arguments.
+ * It is used by the "pthread_create" system call. 
+ * The CPU context is initialised from scratch, and the "loadable" field is set.
+ * The new thread is attached to the core specified in the <attr> argument.
+ * It is registered in the local process descriptor specified by the <pid> argument.
  * The thread descriptor pointer is returned to allow the parent thread to register it
  * in its children list.
- * The THREAD_BLOCKED_GLOBAL bit is set, and the thread must be activated to start.   
- ***************************************************************************************
- * @ new_thread   : address of buffer for new thread descriptor pointer.  
+ * The THREAD_BLOCKED_GLOBAL bit is set => the thread must be activated to start.   
+ ***************************************************************************************
+ * @ pid          : process identifier.
+ * @ start_func   : pointer on entry function.
+ * @ start_args   : pointer on function argument (can be NULL).
  * @ attr         : pointer on pthread attributes descriptor.
- * @ u_stack_base : actual user stack size.
- * @ u_stack_size : actual user stack base.
+ * @ new_thread   : [out] address of buffer for new thread descriptor pointer.  
  * @ returns 0 if success / returns ENOMEM if error.
  **************************************************************************************/
-error_t thread_user_create( thread_t       ** new_thread,
+error_t thread_user_create( pid_t             pid,
+                            void            * start_func,
+                            void            * start_arg,
                             pthread_attr_t  * attr,
-                            intptr_t          u_stack_base,
-                            uint32_t          u_stack_size );
+                            thread_t       ** new_thread );
 
 /***************************************************************************************
@@ -261,14 +281,10 @@
  * The THREAD_BLOCKED_GLOBAL bit is set, and the thread must be activated to start.   
  ***************************************************************************************
- * @ new_thread   : address of buffer for new thread descriptor pointer.  
  * @ process      : local pointer on owner process descriptor.
- * @ u_stack_base : actual user stack size.
- * @ u_stack_size : actual user stack base.
+ * @ new_thread   : [out] address of buffer for new thread descriptor pointer.  
  * @ returns 0 if success / returns ENOMEM if error.
  **************************************************************************************/
-error_t thread_user_fork( thread_t ** new_thread,
-                          process_t * process,
-                          intptr_t    u_stack_base,
-                          uint32_t    u_stack_size );
+error_t thread_user_fork( process_t * process,
+                          thread_t ** new_thread );
 
 /***************************************************************************************
@@ -343,7 +359,4 @@
  * This function removes an user thread from the parent thread global list 
  * of attached children threads. 
- * It does take the lock, as this function can be called by the child thread.
- * In this case, it uses the extended pointer on the parent thread contained 
- * in the pthread_attr_t embedded in the child thread descriptor.
  ***************************************************************************************
  * @ xp_parent : extended pointer on the parent thread descriptor.
@@ -352,5 +365,4 @@
 void thread_child_parent_unlink( xptr_t xp_parent,
                                  xptr_t xp_child );
-
 
 /***************************************************************************************
@@ -475,4 +487,15 @@
 void thread_signals_handle( thread_t * thread );
 
+/***************************************************************************************
+ * This function returns the extended pointer on a thread descriptor identified
+ * by its thread identifier, and process identifier.
+ * It can be called by any thread running in any cluster.
+ ***************************************************************************************
+ * @ pid     : process identifier.
+ * @ trdid   : thread identifier.
+ * @ return the extended pointer if thread found / return XPTR_NULL if not found. 
+ **************************************************************************************/
+xptr_t thread_get_xptr( pid_t    pid,
+                        trdid_t  trdid );
 
 
