Index: trunk/kernel/syscalls/sys_thread_create.c
===================================================================
--- trunk/kernel/syscalls/sys_thread_create.c	(revision 690)
+++ trunk/kernel/syscalls/sys_thread_create.c	(revision 1)
@@ -1,6 +1,8 @@
 /*
  * sys_thread_create.c - creates a new user thread
- *
- * Author     Alain Greiner (2016,2017,2018,2019)
+ * 
+ * Author  Ghassan Almaless (2008,2009,2010,2011,2012)
+ *         Mohamed lamine Karaoui (2015)
+ *         Alain Greiner (2016)
  *
  * Copyright (c) UPMC Sorbonne Universites
@@ -22,8 +24,6 @@
  */
 
-#include <kernel_config.h>
-#include <hal_kernel_types.h>
-#include <hal_uspace.h>
-#include <hal_vmm.h>
+#include <almos_config.h>
+#include <hal_types.h>
 #include <printk.h>
 #include <errno.h>
@@ -36,193 +36,122 @@
 #include <kmem.h>
 #include <process.h>
+#include <spinlock.h>
 #include <dqdt.h>
-#include <rpc.h>
 
-#include <syscalls.h>
 
-/////////////////////////////////////////////////////////
-int sys_thread_create( trdid_t               * trdid_ptr,
-                       struct pthread_attr_s * user_attr,
-                       void                  * start_func,
-                       void                  * start_args )
+//////////////////////////////////////////////////////////////////////////////////////////
+// This function implements the pthread_create system call
+//////////////////////////////////////////////////////////////////////////////////////////
+int sys_thread_create ( thread_t       * new_thread,    // [out] argument
+                        pthread_attr_t * user_attr,     // [in] argument
+                        void           * start_func,    // [in] argument
+                        void           * start_args )   // [in] argument
 {
-    pthread_attr_t   kern_attr;        // copy of pthread attributes in kernel space
-    thread_t       * parent;           // pointer on thread executing the pthread_create
-    cxy_t            child_cxy;        // created child thread cluster identifier
-    thread_t       * child_ptr;        // pointer on created child thread
-    xptr_t           child_xp;         // extended pointer on created thread
+	pthread_attr_t   attr;             // copy of pthread attributes in kernel space
+	thread_t       * parent;           // pointer on thread executing the pthread_create
+    xptr_t           xp_parent;        // extended pointer on created thread
+    thread_t       * child;            // pointer on created child thread
+    xptr_t           xp_child;         // extended pointer on created thread
     trdid_t          trdid;            // created thread identifier
     process_t      * process;          // pointer on local process descriptor
-    vseg_t         * vseg;             // required for user space checking
     error_t          error;
 
-    // get parent thead pointer, extended pointer, and process 
-    parent     = CURRENT_THREAD;
-    process    = parent->process;
+	uint32_t         tm_start;
+	uint32_t         tm_end;
 
-#if (DEBUG_SYS_THREAD_CREATE || CONFIG_INSTRUMENTATION_SYSCALLS)
-uint64_t     tm_start = hal_get_cycles();
-#endif
+	tm_start = hal_time_stamp();
 
-#if DEBUG_SYS_THREAD_CREATE
-if( DEBUG_SYS_THREAD_CREATE < tm_start )
-printk("\n[%s] thread[%x,%x] enter / cycle %d\n",
-__FUNCTION__, process->pid, parent->trdid, (uint32_t)tm_start );
-#endif
+    // get parent thead pointer, extended pointer, and process pointer
+	parent     = CURRENT_THREAD;
+    xp_parent  = XPTR( local_cxy , parent );    
+	process    = parent->process;
 
-    // check trdid buffer in user space
-    if( vmm_get_vseg( process , (intptr_t)trdid_ptr , &vseg ) )
+    // check user_attr & start_func arguments
+	if( user_attr == NULL )
+	{
+		printk("\n[ERROR] in %s : user_attr is NULL\n", __FUNCTION__ );
+		return EINVAL;
+	}
+	if( start_func== NULL )
+	{
+		printk("\n[ERROR] in %s : start_func is NULL\n", __FUNCTION__ );
+		return EINVAL;
+	}
+
+    // copy user_attr structure to kernel space
+	hal_copy_from_uspace( &attr , user_attr , sizeof(pthread_attr_t) );
+
+    // check/set "cxy" attribute
+	if( attr.flags & PT_FLAG_CLUSTER_DEFINED ) 
     {
-
-#if DEBUG_SYSCALLS_ERROR
-printk("\n[ERROR] in %s : thread[%x,%x] / trdid buffer %x unmapped %x\n",
-__FUNCTION__, process->pid, parent->trdid, (intptr_t)trdid_ptr );
-#endif
-		parent->errno = EINVAL;
-		return -1;
+        if( cluster_is_undefined( attr.cxy ) )
+        {
+		    printk("\n[ERROR] in %s : illegal target cluster = %x\n",
+                   __FUNCTION__ , attr.cxy );
+		    return = EINVAL;
+        } 
+    }
+    else
+    {
+        attr.cxy = dqdt_get_cluster_for_process();
     }
 
-	// check user_attr buffer in user space & copy to kernel space
-    if( user_attr != NULL )
+    // set "pid", "start_func" & "start_args" attributes
+    attr.pid        = process->pid;
+    attr.start_func = start_func;
+    attr.start_args = start_args;
+
+    // create the thread, using a RPC if required
+    // this returns "error", "child", and "xp_child" 
+    if( attr.cxy == local_cxy )                 // target cluster is local 
     {
-	    if( vmm_get_vseg( process , (intptr_t)user_attr , &vseg ) )
-	    {
+        // allocate a stack from local VMM 
+        vseg_t * vseg = vmm_create_vseg( process, 
+                                         0,
+                                         0,
+                                         VSEG_TYPE_STACK,
+                                         local_cxy );
+        if( vseg == NULL )
+        {
+		    printk("\n[ERROR] in %s for : cannot create stack vseg\n", __FUNCTION__ );
+		    return = EINVAL;
+        } 
 
-#if DEBUG_SYSCALLS_ERROR
-printk("\n[ERROR] in %s : thread[%x,%x] / user_attr buffer unmapped %x\n",
-__FUNCTION__, process->pid, parent->trdid, (intptr_t)user_attr );
-#endif
-		    parent->errno = EINVAL;
-		    return -1;
-	    }
-        
-	    hal_copy_from_uspace( XPTR( local_cxy , &kern_attr ),
-                              user_attr,
-                              sizeof(pthread_attr_t) );
+        // create thread in local cluster
+        error = thread_user_create( &child,
+                                    &attr,
+                                    vseg->min,
+                                    vseg->max - vseg->min );
+
+        xp_child = XPTR( local_cxy , thread );
+    }
+    else                                                 // target cluster is remote
+    {
+        rpc_thread_user_create( attr.cxy , &attr , &error , &xp_child );
+        child = (thread_t *)GET_PTR( xp_child );
     }
 
-	// check start_func in user space
-	if( vmm_get_vseg( process , (intptr_t)start_func , &vseg ) )
+    // check successful thread creation
+    if( error ) 
     {
+		printk("\n[ERROR] in %s : cannot create thread\n", __FUNCTION__ );
+        return ENOMEM;
+    }
 
-#if DEBUG_SYSCALLS_ERROR
-printk("\n[ERROR] in %s : thread[%x,%x] / start_func unmapped %x\n",
-__FUNCTION__, process->pid, parent->trdid, (intptr_t)start_func );
-#endif
-        parent->errno = EINVAL;
-	    return -1;
-	}
+    // returns trdid to user space 
+    trdid = hal_remote_lw( XPTR( attr.cxy , &child->trdid ) );   
+	hal_copy_to_uspace( new_thread , &trdid , sizeof(pthread_t) );
+    
+    // register new-thread in parent-thread children list if required
+    if( attr.flags & PT_FLAG_DETACH == 0 ) thread_child_parent_link( xp_parent , xp_child );
 
-	// check start_args buffer in user space
-	if( start_args != NULL )
-    {
-        if( vmm_get_vseg( process , (intptr_t)start_args , &vseg ) )
-	    {
+	tm_end = hal_time_stamp();
 
-#if DEBUG_SYSCALLS_ERROR
-printk("\n[ERROR] in %s : thread[%x,%x] / start_args buffer unmapped %x\n",
-__FUNCTION__, process->pid, parent->trdid, (intptr_t)start_args );
-#endif
-		    parent->errno = EINVAL;
-		    return -1;
-        }
-	}
-
-    // define attributes and child_cxy
-    if( user_attr != NULL )                      // user defined attributes
-    {
-	    // check / get child_cxy
-	    if( kern_attr.attributes & PT_ATTR_CLUSTER_DEFINED )
-	    {
-		    if( cluster_is_active( kern_attr.cxy ) == false )
-		    {
-
-#if DEBUG_SYSCALLS_ERROR
-printk("\n[ERROR] in %s : thread[%x,%x] / illegal target cluster %x\n",
-__FUNCTION__, process->pid, parent->trdid, kern_attr.cxy );
-#endif
-			    parent->errno = EINVAL;
-			    return -1;
-            }
-            child_cxy = kern_attr.cxy;
-		}
-        else
-        {
-            child_cxy = dqdt_get_cluster_for_thread( LOCAL_CLUSTER->dqdt_root_xp );
-        }
-	}
-	else                                        // set default attributes
-	{
-        kern_attr.attributes = PT_ATTR_CLUSTER_DEFINED;
-        child_cxy = dqdt_get_cluster_for_thread( LOCAL_CLUSTER->dqdt_root_xp );
-	}
-
-	// create the thread, using a RPC if required
-	// this returns "error", "child_ptr", and "child_xp"
-
-	if( child_cxy == local_cxy )                         // target cluster is local
-	{
-		// create thread in local cluster
-		error = thread_user_create( process->pid,
-		                            start_func,
-		                            start_args,
-		                            &kern_attr,
-		                            &child_ptr );
-
-		child_xp = XPTR( local_cxy , child_ptr );
-	}
-	else                                                 // target cluster is remote
-	{
-		rpc_thread_user_create_client( child_cxy,
-		                               process->pid,
-		                               start_func,
-		                               start_args,
-		                               &kern_attr,
-		                               &child_xp,
-		                               &error );
-
-		child_ptr = (thread_t *)GET_PTR( child_xp );
-	}
-
-	// check successful thread creation
-	if( error )
-	{
-
-#if DEBUG_SYSCALLS_ERROR
-printk("\n[ERROR] in %s : thread[%x,%x] cannot create new thread\n",
-__FUNCTION__ , process->pid, parent->trdid );
-#endif
-		parent->errno = ENOMEM;
-		return -1;
-	}
-
-	// returns trdid to user space
-	trdid = hal_remote_l32( XPTR( child_cxy , &child_ptr->trdid ) );
-	hal_copy_to_uspace( trdid_ptr,
-                        XPTR( local_cxy , &trdid ),
-                        sizeof(pthread_t) );
-
-    // activate new thread
-	thread_unblock( child_xp , THREAD_BLOCKED_GLOBAL );
-
-    hal_fence();
-
-#if (DEBUG_SYS_THREAD_CREATE || CONFIG_INSTRUMENTATION_SYSCALLS)
-uint64_t     tm_end = hal_get_cycles();
-#endif
-
-#if DEBUG_SYS_THREAD_CREATE
-if( DEBUG_SYS_THREAD_CREATE < tm_end )
-printk("\n[%s] thread[%x,%x] created thread %x / cycle %d\n",
-__FUNCTION__, process->pid, parent->trdid, child_ptr->trdid, (uint32_t)tm_end );
-#endif
-
-#if CONFIG_INSTRUMENTATION_SYSCALLS
-hal_atomic_add( &syscalls_cumul_cost[SYS_THREAD_CREATE] , tm_end - tm_start );
-hal_atomic_add( &syscalls_occurences[SYS_THREAD_CREATE] , 1 );
-#endif
-
+	thread_dmsg("\n[INFO] %s created thread %x for process %x in cluster %x\n"
+                "  start_cycle = %d / end_cycle = %d\n",
+                   trdid , process->pid , attr.cxy , tm_start , tm_end );
 	return 0;
 
 }  // end sys_thread_create()
 
+
