Index: trunk/kernel/syscalls/sys_thread_create.c
===================================================================
--- trunk/kernel/syscalls/sys_thread_create.c	(revision 406)
+++ trunk/kernel/syscalls/sys_thread_create.c	(revision 407)
@@ -40,13 +40,11 @@
 
 
-//////////////////////////////////////////////////////////////////////////////////////////
-// 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_arg )    // [in] argument
+///////////////////////////////////////////////////
+int sys_thread_create ( pthread_t      * trdid_ptr,
+                        pthread_attr_t * user_attr,
+                        void           * start_func,
+                        void           * start_arg )
 {
-	pthread_attr_t   k_attr;           // copy of pthread attributes in kernel space
+	pthread_attr_t   kern_attr;        // copy of pthread attributes in kernel space
 	thread_t       * parent;           // pointer on thread executing the pthread_create
 	xptr_t           parent_xp;        // extended pointer on created thread
@@ -56,4 +54,5 @@
 	process_t      * process;          // pointer on local process descriptor
 	paddr_t          paddr;            // unused, required by vmm_v2p_translate()
+    cxy_t            target_cxy;       // target cluster identifier
 	error_t          error;
 
@@ -63,18 +62,23 @@
 	tm_start = hal_get_cycles();
 
-	// get parent thead pointer, extended pointer, and process pointer
+	// get parent thead pointer, extended pointer, and process 
 	parent     = CURRENT_THREAD;
 	parent_xp  = XPTR( local_cxy , parent );
 	process    = parent->process;
 
-	// check user_attr in user space
-	error = vmm_v2p_translate( false , user_attr , &paddr );
+	// check user_attr in user space & copy to kernel space
+    if( user_attr != NULL )
+    {
+	    error = vmm_v2p_translate( false , user_attr , &paddr );
 
-	if( error )
-	{
-		printk("\n[ERROR] in %s : user_attr unmapped\n", __FUNCTION__ );
-		parent->errno = EINVAL;
-		return -1;
-	}
+	    if( error )
+	    {
+		    printk("\n[ERROR] in %s : user_attr unmapped\n", __FUNCTION__ );
+		    parent->errno = EINVAL;
+		    return -1;
+	    }
+        
+	    hal_copy_from_uspace( &kern_attr , user_attr , sizeof(pthread_attr_t) );
+    }
 
 	// check start_func in user space
@@ -98,27 +102,34 @@
 	}
 
-	// copy user_attr structure from user space to kernel space
-	hal_copy_from_uspace( &k_attr , user_attr , sizeof(pthread_attr_t) );
-
-	// check/set "cxy" attribute
-	if( k_attr.attributes & PT_ATTR_CLUSTER_DEFINED )
+	// check / define attributes an target_cxy
+    if( user_attr != NULL )                      // user defined attributes
+    {
+	    // check / get target_cxy
+	    if( kern_attr.attributes & PT_ATTR_CLUSTER_DEFINED )
+	    {
+		    if( cluster_is_undefined( kern_attr.cxy ) )
+		    {
+			    printk("\n[ERROR] in %s : illegal target cluster = %x\n",
+			    __FUNCTION__ , kern_attr.cxy );
+			    parent->errno = EINVAL;
+			    return -1;
+            }
+            target_cxy = kern_attr.cxy;
+		}
+        else
+        {
+            target_cxy = dqdt_get_cluster_for_process();
+        }
+	}
+	else                                        // set default attributes
 	{
-		if( cluster_is_undefined( k_attr.cxy ) )
-		{
-			printk("\n[ERROR] in %s : illegal target cluster attribute = %x\n",
-			       __FUNCTION__ , k_attr.cxy );
-			parent->errno = EINVAL;
-			return -1;
-		}
-	}
-	else
-	{
-		k_attr.cxy = dqdt_get_cluster_for_process();
+        kern_attr.attributes = PT_ATTR_DETACH | PT_ATTR_CLUSTER_DEFINED;
+        target_cxy           = dqdt_get_cluster_for_process();
 	}
 
 	// create the thread, using a RPC if required
-	// this returns "error", "child", and "child_xp"
+	// this returns "error", "child_ptr", and "child_xp"
 
-	if( k_attr.cxy == local_cxy )                         // target cluster is local
+	if( target_cxy == local_cxy )                         // target cluster is local
 	{
 		// create thread in local cluster
@@ -126,5 +137,5 @@
 		                            start_func,
 		                            start_arg,
-		                            &k_attr,
+		                            &kern_attr,
 		                            &child_ptr );
 
@@ -133,9 +144,9 @@
 	else                                                 // target cluster is remote
 	{
-		rpc_thread_user_create_client( k_attr.cxy,
+		rpc_thread_user_create_client( target_cxy,
 		                               process->pid,
 		                               start_func,
 		                               start_arg,
-		                               &k_attr,
+		                               &kern_attr,
 		                               &child_xp,
 		                               &error );
@@ -152,17 +163,28 @@
 
 	// returns trdid to user space
-	trdid = hal_remote_lw( XPTR( k_attr.cxy , &child_ptr->trdid ) );
-	hal_copy_to_uspace( new_thread , &trdid , sizeof(pthread_t) );
+	trdid = hal_remote_lw( XPTR( target_cxy , &child_ptr->trdid ) );
+	hal_copy_to_uspace( trdid_ptr , &trdid , sizeof(pthread_t) );
 
-	// register new-thread in parent-thread children list if required
-	if( (k_attr.attributes & PT_ATTR_DETACH) == 0 )
-	    thread_child_parent_link( parent_xp , child_xp );
+    // register child in parent if required
+    if( user_attr != NULL )
+    {
+	    if( (kern_attr.attributes & PT_ATTR_DETACH) == 0 )
+	        thread_child_parent_link( parent_xp , child_xp );
+    }
+
+    // activate new thread
+	thread_unblock( child_xp , THREAD_BLOCKED_GLOBAL );
+
+    hal_fence();
 
 	tm_end = hal_get_cycles();
 
-	thread_dmsg("\n[DMSG] %s created thread %x for process %x in cluster %x\n"
-	            "  start_cycle = %d / end_cycle = %d\n",
-	               trdid , process->pid , k_attr.cxy , tm_start , tm_end );
+syscall_dmsg("\n[DBG] %s : core[%x,%d] created thread %x for process %x / cycle %d\n"
+"      cluster %x / cost = %d cycles\n",
+__FUNCTION__ , local_cxy , parent->core->lid , trdid , process->pid , tm_end ,
+target_cxy , tm_end - tm_start );
+
 	return 0;
-}
 
+}  // end sys_thread_create()
+
