Index: trunk/kernel/kern/thread.h
===================================================================
--- trunk/kernel/kern/thread.h	(revision 690)
+++ trunk/kernel/kern/thread.h	(revision 1)
@@ -1,7 +1,8 @@
 /*
  * thread.h -  Thread and related operations definition.
- *
+ * 
  * Author  Ghassan Almaless (2008,2009,2010,2011,2012)
- *         Alain Greiner    (2016,2017,2018,2019,2020)
+ *         Mohamed Lamine Karaoui (2015)
+ *         Alain Greiner (2016)
  *
  * Copyright (c) UPMC Sorbonne Universites
@@ -26,16 +27,11 @@
 #define _THREAD_H_
 
-#include <hal_kernel_types.h>
-#include <shared_syscalls.h>
-#include <shared_almos.h>
+#include <hal_types.h>
 #include <hal_special.h>
-#include <hal_kentry.h>
 #include <xlist.h>
 #include <list.h>
 #include <hal_context.h>
-#include <remote_busylock.h>
+#include <spinlock.h>
 #include <core.h>
-#include <chdev.h>
-#include <alarm.h>
 #include <cluster.h>
 #include <process.h>
@@ -44,14 +40,32 @@
 #include <dev_txt.h>
 #include <dev_mmc.h>
-#include <dev_dma.h>
-
-/***************************************************************************************
- * These macros are used to compose or decompose the 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 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.
+ **************************************************************************************/
+ 
+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)     */
+} 
+pthread_attr_t;
+
 
 /***************************************************************************************
@@ -60,75 +74,74 @@
 
 typedef enum
-{
-	THREAD_USER    = 0,          /*! user thread (pthread)                            */
+{   
+	THREAD_USER    = 0,          /*! user thread (pthread)                            */ 
 	THREAD_RPC     = 1,          /*! kernel thread executing pending RPCs             */
 	THREAD_DEV     = 2,          /*! kernel thread executing I/O device commands      */
-	THREAD_IDLE    = 3,          /*! kernel idle thread                               */
+	THREAD_KERNEL  = 3,          /*! other kernel thread                              */
+	THREAD_IDLE    = 4,          /*! kernel idle thread                               */
+	THREAD_TYPES_NR
+} 
+thread_type_t;
+
+/***************************************************************************************
+ * This defines the masks associated to the thread flags.
+ **************************************************************************************/
+
+#define THREAD_FLAG_LOADABLE     0x0001  /*! This thread has not been executed yet    */
+#define THREAD_FLAG_DETACHED     0x0002  /*! This user thread is detached from parent */
+
+/***************************************************************************************
+ * This defines the masks associated to the thread signals.
+ **************************************************************************************/
+
+#define THREAD_SIG_KILL          0x0001  /*! This thread must be destroyed ASAP       */
+
+/***************************************************************************************
+ * This defines the masks associated to the blocking causes.
+ **************************************************************************************/
+
+#define THREAD_BLOCKED_GLOBAL    0x0001  /*! thread global blocking                   */ 
+#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_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_IDLE      0x1000  /*! thread RPC wait activation               */
+#define THREAD_BLOCKED_DEV_QUEUE 0x2000  /*! thread DEV wait queue                    */
+#define THREAD_BLOCKED_DEV_ISR   0x4000  /*! thread DEV wait ISR                      */
+
+/***************************************************************************************
+ * This structure defines thread instrumentation informations.
+ **************************************************************************************/
+
+typedef struct thread_info_s
+{
+	uint32_t              pgfault_nr;    /*! cumulated number of page fault           */
+	uint32_t              sched_nr;      /*! TODO ???  [AG]                           */
+	uint32_t              u_err_nr;      /*! TODO ???  [AG]                           */
+	uint32_t              m_err_nr;      /*! TODO ???  [AG]                           */
+	uint32_t              tm_tmp;        /*! temp date to compute execution duration  */
+	uint32_t              tm_exec;       /*! TODO ???  [AG]                           */
+	uint32_t              tm_create;     /*! date of the creation                     */
+	uint32_t              tm_born;       /*! date of the thread loading               */
+	uint32_t              tm_dead;       /*! date of the death                        */
+	clock_t               tm_sleep;      /*! TODO ???  [AG]                           */
+	clock_t               tm_wait;       /*! TODO ???  [AG]                           */
+	clock_t               tm_usr;        /*! user execution duration                  */
+	clock_t               tm_sys;        /*! system execution duration                */
 }
-thread_type_t;
-
-/***************************************************************************************
- * This defines the thread flags bit-vector.
- **************************************************************************************/
-
-#define THREAD_FLAG_DETACHED     0x0001  /*! This thread is detached from parent      */
-#define THREAD_FLAG_JOIN_DONE    0x0002  /*! Parent thread made a join request        */
-#define THREAD_FLAG_KILL_DONE    0x0004  /*! This thread received a kill request      */
-#define THREAD_FLAG_REQ_ACK      0x0010  /*! Acknowledge required from scheduler      */
-#define THREAD_FLAG_REQ_DELETE   0x0020  /*! Destruction required from scheduler      */
-
-/***************************************************************************************
- * This defines the thread blocking causes bit-vector.
- **************************************************************************************/
-
-#define THREAD_BLOCKED_GLOBAL    0x0001  /*! ANY : deactivated / wait activation      */
-#define THREAD_BLOCKED_IO        0x0002  /*! USR : wait IO operation completion       */
-#define THREAD_BLOCKED_MAPPER    0x0004  /*! ??? : wait mapper                        */
-#define THREAD_BLOCKED_EXIT      0x0008  /*! USR : blocked in join / wait exit        */
-#define THREAD_BLOCKED_JOIN      0x0010  /*! USR : blocked in exit / wait join        */
-#define THREAD_BLOCKED_SEM       0x0020  /*! USR : wait semaphore                     */
-#define THREAD_BLOCKED_PAGE      0x0040  /*! ??? : wait page access                   */
-#define THREAD_BLOCKED_IDLE      0x0080  /*! RPC : wait RPC_FIFO non empty            */
-#define THREAD_BLOCKED_USERSYNC  0x0100  /*! USR : wait cond / mutex / barrier        */
-#define THREAD_BLOCKED_RPC       0x0200  /*! ANY : wait RPC completion                */
-#define THREAD_BLOCKED_ISR       0x0400  /*! DEV : wait hardware IRQ                  */
-#define THREAD_BLOCKED_WAIT      0x0800  /*! USR : wait child process termination     */
-#define THREAD_BLOCKED_LOCK      0x1000  /*! ANY : wait queuelock or rwlock           */
-#define THREAD_BLOCKED_CLIENT    0x2000  /*! DEV : wait clients queue non empty       */
-#define THREAD_BLOCKED_SLEEP     0x4000  /*! ANY : wait a timer based alarm           */
+thread_info_t;
 
 /***************************************************************************************
  * This structure defines a thread descriptor.
  * It is used for both the user threads and the kernel threads.
- * In a process, a user thread is identified by a unique TRDID (thread identifier):
+ * In a process, an user thread is identified by a unique TRDID (thread identifier),
+ * 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 CXY of cluster containing the thread.
- * For the main thread the LTID value is always 0, in the owner cluster. 
- * The LTID is used to index the th_tbl[] array in the local process descriptor.
+ * - The TRDID 16 MSB bits contain the owner cluster CXY.
+ * - 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
- * thread is registered in the local copy of the process descriptor.
- *
- * Implementation notes:
- *
- * (1) Don't modify the first 4 fields order, as this order is used by the
- *     hal_kentry assembly code for the TSAR architectures.
- *
- * (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".
- *
- * (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. 
- *
- * (4) The thread_info_t structure is defined in the shared_almos.h file in the
- *     /kernel/syscall/shared_include directory.
+ * thread is registerd in the local copy of the process descriptor.
  **************************************************************************************/
 
@@ -137,13 +150,5 @@
 typedef struct thread_s
 {
-	void              * cpu_context;     /*! CPU context used by sched_yield          */
-	void              * fpu_context;     /*! FPU context used by sched_yield          */
-    void              * uzone_current;   /*! used by hal_do_syscall & hal_do_except   */
-    void              * uzone_previous;  /*! used by hal_do_syscall & hal_do_except   */
-
-	intptr_t            k_stack_base;    /*! kernel stack base address                */
-	uint32_t            k_stack_size;    /*! kernel stack size (bytes)                */
-
-	uint32_t            trdid;           /*! thread index (cxy.ltid)                  */
+	uint32_t            trdid;           /*! thread index (in THTBL)                  */
 	thread_type_t       type;            /*! thread type                              */
 	uint32_t            quantum;         /*! number of clock ticks given to thread    */
@@ -152,57 +157,58 @@
 	core_t            * core;            /*! pointer to the owner core                */
 	process_t         * process;         /*! pointer on local process descriptor      */
-    xptr_t              parent;          /*! extended pointer on parent thread        */
-
-    remote_busylock_t   join_lock;       /*! lock protecting the join/exit            */
-    xptr_t              join_xp;         /*! joining/killer thread extended pointer   */
-    void              * exit_status;     /*! status returned to joiniy thread        */
-
-    uint32_t          * ack_rsp_count;   /*! pointer on acknowledge response counter  */
-
-	vseg_t            * user_stack_vseg; /*! local pointer on user stack vseg         */
-
-    void              * entry_func;      /*! pointer on thread entry function         */
-    void              * entry_args;      /*! pthread     : pointer on arguments       */
+	page_t            * page;            /*! pointer on page desc. containing thread  */ 
+
+	uint32_t            local_locks;	 /*! number of local locks owned by thread    */
+    list_entry_t        locks_root;      /*! root of local locks list                 */
+
+	uint32_t            remote_locks;	 /*! number of local locks owned by thread    */
+    xlist_entry_t       xlocks_root;     /*! root of remote locks list                */
+
+	intptr_t            u_stack_base;    /*! user stack base address                  */
+	uint32_t            u_stack_size;    /*! user stack size (bytes)                  */
+	intptr_t            k_stack_base;    /*! kernel stack base address                */
+	uint32_t            k_stack_size;    /*! kernel stack size (bytes)                */
+
+    void              * entry_func;      /*! pointer on entry function                */
+    void              * entry_args;      /*! pointer on entry function arguments      */
 
     uint32_t            flags;           /*! bit vector of flags                      */
     uint32_t            blocked;         /*! bit vector of blocking causes            */
-
-	error_t             errno;           /*! errno value set by last system call      */
-    uint32_t            utls;            /*! user thread local storage                */
+    uint32_t            signals;         /*! bit vector of signals                    */
+
+	error_t             errno;           /*! errno value of last system call          */
 
     bool_t              fork_user;       /*! user defined placement for next fork()   */
     cxy_t               fork_cxy;        /*! target cluster  for next fork()          */
 
+	xlist_entry_t       children_root;   /*! root of list of attached children        */
+    uint32_t            children_nr;     /*! number of attached children threads      */
+    remote_spinlock_t * children_lock;   /*! lock protecting the children list        */
+
+    xlist_entry_t       brothers_list;   /*! member of threads with same parent       */
+
 	list_entry_t        sched_list;      /*! member of threads attached to same core  */
 
-    chdev_t           * chdev;           /*! chdev pointer (for a DEV thread only)    */
-
-    alarm_t             alarm;           /*! embedded timer based alarm               */
-
-    reg_t               save_sr;         /*! used by sched_yield() function           */
-
-    ioc_command_t       ioc_cmd;         /*! IOC device generic command               */
-    txt_command_t       txt_cmd;         /*! TXT device generic command               */
-    nic_command_t       nic_cmd;         /*! NIC device generic command               */
-    mmc_command_t       mmc_cmd;         /*! MMC device generic command               */
-    dma_command_t       dma_cmd;         /*! DMA device generic command               */
-    fbf_command_t       fbf_cmd;         /*! FBF device generic command               */
-
-  	xptr_t              rpc_client_xp;   /*! client thread (for a RPC thread only)    */
-
-    list_entry_t        wait_list;       /*! member of a local waiting queue          */
-    xlist_entry_t       wait_xlist;      /*! member of a trans-cluster waiting queue  */
-    xlist_entry_t       tmp_xlist;       /*! member of a trans-cluster kleenex queue  */
-
-	uint32_t            busylocks;	     /*! number of taken busylocks                */
-
-#if DEBUG_BUSYLOCK_TYPE
-    xlist_entry_t       busylocks_root;  /*! root of xlist of taken busylocks         */
-#endif
+    uint32_t            dev_channel;     /*! device channel for a DEV thread          */
+    union                                /*! embedded command for a DEV thread        */
+    {
+        ioc_command_t   ioc;             /*! IOC device generic command               */
+        txt_command_t   txt;             /*! TXT device generic command               */
+        nic_command_t   nic;             /*! NIC device generic command               */
+        mmc_command_t   mmc;             /*! NIC device generic command               */
+    }  
+    dev;
+    
+	cxy_t               rpc_client_cxy;  /*! client cluster index (for a RPC thread)  */
+
+    xlist_entry_t       wait_list;       /*! member of threads blocked on same cond   */
+
+	void              * fpu_context;     /*! pointer on FPU context                   */
+	void              * cpu_context;     /*! pointer on CPU context                   */
 
 	thread_info_t       info;            /*! embedded thread_info_t                   */
 
 	uint32_t            signature;       /*! for kernel stack overflow detection      */
-}
+} 
 thread_t;
 
@@ -214,80 +220,50 @@
 
 /***************************************************************************************
- * This function returns a printable string for a thread type.
- ***************************************************************************************
- * @ type    : thread type.
- * returns pointer on string.
- **************************************************************************************/
-const char * thread_type_str( thread_type_t type );
-
-/***************************************************************************************
- * This function is used by the pthread_create() system call to create a "new" thread
- * in an existing process. It allocates memory for an user thread descriptor in the
- * local cluster, and initializes it from information contained in the arguments.
- * The CPU context is initialized from scratch. 
- * It is registered in the local process descriptor specified by the <pid> argument.
- * The THREAD_BLOCKED_GLOBAL bit is set => the thread must be activated by the caller
- * to start at the next scheduling point.
- ***************************************************************************************
- * @ pid          : process identifier.
- * @ start_func   : pointer on entry function.
- * @ start_args   : pointer on function argument (can be NULL).
+ * 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".
+ * 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.  
  * @ attr         : pointer on pthread attributes descriptor.
- * @ new_thread   : [out] address of buffer for new thread descriptor pointer.
+ * @ u_stack_base : actual user stack size.
+ * @ u_stack_size : actual user stack base.
  * @ returns 0 if success / returns ENOMEM if error.
  **************************************************************************************/
-error_t thread_user_create( pid_t             pid,
-                            void            * start_func,
-                            void            * start_arg,
+error_t thread_user_create( thread_t       ** new_thread,
                             pthread_attr_t  * attr,
-                            thread_t       ** new_thread );
-
-/***************************************************************************************
- * This function is used by the sys_fork() syscall to create the "child" main thread
- * in the local cluster. It is called, generally through the RPC_PROCESS_MAKE_FORK,
- * by the process_make_fork() function. It allocates memory from the local cluster
- * for a "child" thread descriptor, and initializes it from the "parent" thread 
- * descriptor defined by the <parent_thread_xp> argument.
- * The new thread is attached to the core that has the lowest load in local cluster.
- * It is registered in the "child" process defined by the <child_process> argument.
- * This new thread inherits its user stack from the parent thread, as it uses the
- * Copy-On-Write mechanism to get a private stack when required.
- * The content of the parent kernel stack is copied into the child kernel stack, as 
- * the Copy-On-Write mechanism cannot be used for kernel segments (because kernel 
- * uses physical addressing on some architectures).
- * The CPU and FPU execution contexts are created and linked to the new thread.
- * but the actual context copy is NOT done, and is done by the sys_fork() function. 
- * The THREAD_BLOCKED_GLOBAL bit is set => the thread must be activated to start.
- ***************************************************************************************
- * @ parent_thread_xp  : extended pointer on parent thread descriptor.
- * @ child_process     : local pointer on child process descriptor.
- * @ child_thread      : [out] address of buffer for child thread descriptor pointer.
- * @ returns 0 if success / returns -1 if error.
- **************************************************************************************/
-error_t thread_user_fork( xptr_t      parent_thread_xp,
-                          process_t * child_process,
-                          thread_t ** child_thread );
-
-/***************************************************************************************
- * This function is called by the process_make_exec() function to re-initialise the
- * calling thread descriptor, that will become the new process main thread, from
- * the process descriptor, and from the <args_nr> & <args_base> arguments.
- * - The calling thread TRDID is not modified.
- * - The kernel stack (currently in use) is not modified.  
- * - It calls the hal_cpu_context_init() to re-initialize the thread CPU context.
- * - It calls the hal_do_cpu_restore() to force execution of the new user code.
- ***************************************************************************************
- * @ argc   : actual number of main thread arguments.
- * @ argv   : user space pointer on array of pointers on arguments.
- **************************************************************************************/
-void thread_user_exec( uint32_t  argc,
-                       intptr_t  argv );
+                            intptr_t          u_stack_base,
+                            uint32_t          u_stack_size );
+
+/***************************************************************************************
+ * This function allocates memory for an user thread descriptor in the local cluster,
+ * and initialises it from informations contained in the calling thread descriptor.
+ * It is used by the fork() system call to create the child process main thread.
+ * The new thread is attached to the core with the lowest load. 
+ * It is registered in the process descriptor defined by the "process" argument.
+ * This new thread inherits its execution context from the calling thread,
+ * and the "loadable" field is NOT set.
+ * 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.
+ * @ 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 );
 
 /***************************************************************************************
  * This function allocates memory for a kernel thread descriptor in the local cluster,
- * and initializes it from arguments values. 
- * It is called by kernel_init() to statically create all DEV server threads
- * It is also called to dynamically create RPC threads when required.
- * The THREAD_BLOCKED_GLOBAL bit is set, and the thread must be activated to start.
+ * and initialise it from arguments values.
+ * The THREAD_BLOCKED_GLOBAL bit is set, and the thread must be activated to start.   
  ***************************************************************************************
  * @ new_thread   : address of buffer for new thread pointer.
@@ -300,177 +276,141 @@
 error_t thread_kernel_create( thread_t     ** new_thread,
                               thread_type_t   type,
-                              void          * func,
+                              void          * func, 
                               void          * args,
                               lid_t           core_lid );
 
 /***************************************************************************************
- * This function is called by the kernel_init() function to initialize the IDLE thread
- * descriptor from arguments values.
- * The THREAD_BLOCKED_GLOBAL bit is set, and the thread must be activated to start.
- * It returns a kernel panic if failure.
- ***************************************************************************************
- * @ thread   : pointer on existing thread descriptor.
- * @ type     : kernel thread type.
- * @ func     : pointer on function.
- * @ args     : function arguments.
- * @ core_lid : local core index.
- **************************************************************************************/
-void thread_idle_init( thread_t      * thread,
-                       thread_type_t   type,
-                       void          * func,
-                       void          * args,
-                       lid_t           core_lid );
-
-/***************************************************************************************
- * This low-level function is called by the sched_handle_signals() function when a
- * thread is marked for delete. It removes the thread identified by the <thread>
- * argument from the process th_tbl[], and releases all physical memory allocated for 
- * this. This includes the thread descriptor itself, the associated CPU and FPU context,
- * and the physical memory allocated for an user thread stack.
- * This function does not remove the thread from the scheduler, as this is done by
- * the scheduler itself.
- ***************************************************************************************
- * @ thread  : pointer on the thread descriptor to release.
- * @ return the number of threads registered in the process th_tbl[] before deletion.
- **************************************************************************************/
-uint32_t thread_destroy( thread_t * thread );
-
-/***************************************************************************************
- * This function defines the code of the thread executed by all cores after kernel_init,
- * or when no other thread is runnable for a given core.
- * It enter and infinite loop in wich:
- * - it unmask the IRQs
- * - it optionally calls the hal_core_sleep() function to reduce the power consumption
- *   (this behavior is controlled by the CONFIG_THREAD_IDLE_MODE_SLEEP flag).
- * - it call the sched_yield() function to find another runnable thread.
- *
- * TODO: In the TSAR architecture the hal_core_sleep() function forces the core to
- * low-power mode. Any IRQ will force the core to exit this low-power mode, but no ISR
- * is executed. We must analyse if we have the same behaviour for I86 architectures...
- **************************************************************************************/
-void thread_idle_func( void );
-
-/***************************************************************************************
- * This function is used by a "blocker" thread running in the same cluster as a "target"
- * thread to request the scheduler of the target thread to acknowledge that the target
- * thread is blocked and not running, at the next context switch.
- * This function executes atomically the following actions :
- * - it set the request_pending boolean in the target scheduler descriptor.
- * - it set the REQ_ACK flag in the "flags" field of the target thread descriptor.
- * - It registers the responses counter pointer in the target thread descriptor.
- * The request_pending flag is handled as a set/reset flip-flop by the "blocker" thread
- * and by the "target" scheduler. 
- ***************************************************************************************
- * @ target        : local pointer on target thread.
- * @ ack_rsp_count : local pointer on responses counter.
- **************************************************************************************/
-void thread_set_req_ack( thread_t * target,
-                         uint32_t * ack_rsp_count );
-
-/***************************************************************************************
- * This function is used by the sched_handle_signal() function executed by the 
- * scheduler of a "target" thread to reset a "blocked not running" acknowledge request
- * in both the target thread descriptor, and in the target  thread scheduler.
- ***************************************************************************************
- * @ target    : local pointer on target thread.
- **************************************************************************************/
-void thread_reset_req_ack( thread_t * target );
-
-/***************************************************************************************
- * This function is used by the four sys_thread_cancel(), sys_thread_exit(),
- * sys_kill() and sys_exit() system calls to mark for delete a given thread. 
- * It set the THREAD_BLOCKED_GLOBAL bit and set the THREAD_FLAG_REQ_DELETE bit in the
- * thread descriptor identified by the <thread_xp> argument, to ask the scheduler 
- * to asynchronously delete the target thread, at the next scheduling point. 
- * The calling thread can run in any cluster, as it uses remote accesses.
- * This function makes a kernel panic if the target thread is the main thread,
- * because the main thread deletion will cause the process deletion, and a process
- * must be deleted by the parent process, running the wait function.
- * If the target thread is running in "attached" mode, and the <is_forced> argument
- * is false, this function implements the required sychronisation with the joining 
- * thread, blocking the killer thread until the pthread_join() syscall is executed
- * by the joining thread.
- ***************************************************************************************
- * @ thread_xp   : extended pointer on the target thread.
- * @ is_forced   : the deletion does not depends on the attached mode. 
- **************************************************************************************/
-void thread_delete_request( xptr_t  thread_xp,
-                            bool_t  is_forced );
-
-/***************************************************************************************
- * This function registers a blocking cause defined by the <cause> argument 
- * in a remote thread descriptor identified by the <thread_xp> argument.
- * We need an extended pointer, because this function can be called by another thread
- * than the target thread, executing the sys_kill() function.
- * WARNING : this function does not deschedule the target thread, and the descheduling
- * must be explicitely forced by a sched_yield().
- ***************************************************************************************
- * @ thread_xp   : extended pointer on remote thread descriptor.
- * @ cause       : mask defining the cause (one hot).
- **************************************************************************************/
-void thread_block( xptr_t   thread_xp,
-                   uint32_t cause );
-
-/***************************************************************************************
- * This function resets the bit identified by the <cause> argument in a remote
- * thread descriptor identified by the <thread_xp> argument.
+ * This function releases the physical memory allocated for a thread descriptor 
+ * in the local cluster. It can be used for both an user and a kernel thread.
+ * The physical memory dynamically allocated in the HEAP or MMAP zones by an user 
+ * thread will be released when the process is killed, and the page table flushed.
+ ***************************************************************************************
+ * @ thread  : pointer on the thread descriptor to release.  
+ **************************************************************************************/
+void thread_destroy( thread_t * thread );
+
+/***************************************************************************************
+ * This function defines the code of the thread executed when no other thread is 
+ * runnable for a given core. It try to force the core to the low-power state.
+ **************************************************************************************/
+void * thread_idle_func();
+
+/***************************************************************************************
+ * This function registers a child thread in the global list of attached 
+ * children threads of a parent thread.
+ * It does NOT take a lock, as this function is allways called by the parent thread.
+ ***************************************************************************************
+ * @ xp_parent : extended pointer on the parent thread descriptor.
+ * @ xp_child  : extended pointer on the child thread descriptor.
+ **************************************************************************************/
+void thread_child_parent_link( xptr_t  xp_parent,
+                               xptr_t  xp_child );
+
+/***************************************************************************************
+ * 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.
+ * @ xp_child  : extended pointer on the child thread descriptor.
+ **************************************************************************************/
+void thread_child_parent_unlink( xptr_t xp_parent,
+                                 xptr_t xp_child );
+
+
+/***************************************************************************************
+ * This function atomically set a signal in a thread descriptor.
+ ***************************************************************************************
+ * @ thread    : local pointer on target thread.
+ * @ mask      : mask on selected signal.
+ **************************************************************************************/
+inline void thread_set_signal( thread_t * thread,
+                               uint32_t   mask );
+
+/***************************************************************************************
+ * This function reset a signal in a thread descriptor.
+ ***************************************************************************************
+ * @ thread    : local pointer on target thread.
+ * @ mask      : mask on selected signal.
+ **************************************************************************************/
+inline void thread_reset_signal( thread_t * thread,
+                                 uint32_t   mask );
+
+/***************************************************************************************
+ * This function returns true if the calling thread is attached to its parent thread.
+ **************************************************************************************/
+inline bool_t thread_is_joinable();
+
+/***************************************************************************************
+ * This function returns true if the calling thread is not blocked.
+ **************************************************************************************/
+inline bool_t thread_is_runnable();
+
+/***************************************************************************************
+ * This function cheks if the calling thread can deschedule.
+ ***************************************************************************************
+ * @ returns true if no locks taken.
+ **************************************************************************************/
+inline bool_t thread_can_yield();
+
+/***************************************************************************************
+ * This function checks if the calling thread must be descheduled. 
+ ***************************************************************************************
+ * @ returns true if no locks taken, and elapsed time.
+ **************************************************************************************/
+bool_t thread_check_sched();
+
+/***************************************************************************************
+ * This function can be used by the calling thread to suicide.
+ * All locks must be previouly released.
+ * The scenario depends on the attached/detached flag :
+ * - if detached, it set the SIG_KILL signal in the "signals" bit_vector, registers
+ *   the BLOCKED_EXIT bit in the "blocked" bit_vector, and deschedule.
+ * - if attached, it simply set the BLOCKED_EXIT bit in the "blocked" bit vector
+ *   and deschedule. The SIG_KILL signal will be set by the parent thread when
+ *   executing the pthread_join().
+ ***************************************************************************************
+ * @ returns 0 if success / returns EINVAL if locks_count is not zero.
+ **************************************************************************************/
+error_t thread_exit();
+
+/***************************************************************************************  
+ * This function registers a blocking cause in the target thread "blocked" bit vector.
+ * Warning : this function does not deschedule the calling thread.
+ * The descheduling can be forced by a sched_yield(). 
+ ***************************************************************************************
+ * @ thread   : local pointer on target thread.
+ * @ cause    : mask defining the cause (one hot).
+ **************************************************************************************/
+void thread_block( thread_t * thread,
+                   uint32_t   cause );
+
+/***************************************************************************************  
+ * This function reset the bit identified by the cause argument in the "blocked"
+ * bit vector of a remote thread descriptor.
  * We need an extended pointer, because the client thread of an I/O operation on a
- * given device is generally not in the same cluster as the associated server thread.
- * WARNING : this function does not reschedule the remote thread.
+ * given device is not in the same cluster as the associated device descriptor.
+ * 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.
  ***************************************************************************************
- * @ thread_xp   : extended pointer the remote thread.
- * @ cause       : mask defining the cause (one hot).
- * @ return non zero if the bit-vector was actually modified / return 0 otherwise
- **************************************************************************************/
-uint32_t thread_unblock( xptr_t   thread_xp,
-                         uint32_t cause );
-
-/***************************************************************************************
- * This function updates the calling thread user_time or kernel_time counters. 
- ***************************************************************************************
- * @ thread   : local pointer on target thread.
- * @ is_user  : update user time if true / update kernel time if false
- **************************************************************************************/
-void thread_time_update( thread_t * thread,
-                         bool_t     is_user );
-
-/***************************************************************************************
- * 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 );
-
-/***************************************************************************************
- * 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 (local or remote) 
- * currently owned by a the thread identified by the <thead_xp> argument.
- * The <string> argument is printed in header (can be the calling function name). 
- * WARNING : the DEBUG_BUSYLOCK parameter must be set in the kernel_config.h file.
- ***************************************************************************************
- * @ thread_xp  : extended pointer on target thread.
- * @ string     : defines the calling context.
- **************************************************************************************/
-void thread_display_busylocks( xptr_t       thread_xp,
-                               const char * string );
-
-
+ * @ thread   : extended pointer on the remote thread.
+ * @ cause    : mask defining the cause (one hot).
+ **************************************************************************************/
+void thread_unblock( xptr_t   thread,
+                     uint32_t cause );
+
+/***************************************************************************************  
+ * This function kills a target thread, identified by its local pointer.
+ * It is generally called by the local process_destroy() function.
+ * - it forces the global blocked bit in target thread descriptor.
+ * - it set the SIG_KILL signal in target thread.
+ * - it send an IPI_SCHED_REQUEST to the target thread core.
+ ***************************************************************************************
+ * @ thread   : local pointer on the target thread.
+ * @ returns 0 if success / returns EINVAL if locks_count is not zero.
+ **************************************************************************************/
+void thread_kill( thread_t * thread );
 
 #endif	/* _THREAD_H_ */
