Index: trunk/kernel/syscalls/sys_thread_join.c
===================================================================
--- trunk/kernel/syscalls/sys_thread_join.c	(revision 506)
+++ trunk/kernel/syscalls/sys_thread_join.c	(revision 566)
@@ -31,5 +31,5 @@
 #include <errno.h>
 #include <printk.h>
-#include <remote_spinlock.h>
+#include <remote_busylock.h>
 
 #include <syscalls.h>
@@ -67,10 +67,10 @@
 tm_start = hal_get_cycles();
 if( DEBUG_SYS_THREAD_JOIN < tm_start )
-printk("\n[DBG] %s : parent thread %x enter / process %x / target trdid %x / cycle %d\n",
-__FUNCTION__ , joining_ptr , process->pid , trdid , (uint32_t)tm_start );
+printk("\n[DBG] %s : thread %x in process %x enter / target thread %x / cycle %d\n",
+__FUNCTION__ , joining_ptr->trdid, process->pid, trdid , (uint32_t)tm_start );
 #endif
 
     // check trdid argument
-	if( (target_ltid >= CONFIG_THREAD_MAX_PER_CLUSTER) || cluster_is_undefined( target_cxy ) )  
+	if( (target_ltid >= CONFIG_THREADS_MAX_PER_CLUSTER) || cluster_is_undefined( target_cxy ) )  
 	{
 
@@ -126,5 +126,5 @@
 
     // check target thread joinable
-    if( (hal_remote_lw( target_flags_xp ) & THREAD_FLAG_DETACHED) == 0 )
+    if( (hal_remote_l32( target_flags_xp ) & THREAD_FLAG_DETACHED) != 0 )
     {
 
@@ -140,11 +140,17 @@
 
     // get the lock protecting the join in target thread
-    remote_spinlock_lock( target_join_lock_xp );
+    remote_busylock_acquire( target_join_lock_xp );
 
     // test the kill_done flag from the target thread
-    if( hal_remote_lw( target_flags_xp ) & THREAD_FLAG_KILL_DONE )  // killer thread is first
-    {
+    if( hal_remote_l32( target_flags_xp ) & THREAD_FLAG_KILL_DONE )  // killer thread is first
+    {
+
+#if (DEBUG_SYS_THREAD_JOIN & 1)
+if( DEBUG_SYS_THREAD_JOIN < tm_start )
+printk("\n[DBG] %s : thread %x in process %x / killer thread arrived first\n",
+__FUNCTION__ , joining_ptr->trdid, process->pid );
+#endif
         // get pointers on killer thread
-        killer_xp  = (xptr_t)hal_remote_lwd( target_join_xp_xp );
+        killer_xp  = (xptr_t)hal_remote_l64( target_join_xp_xp );
 
         // reset the kill_done flag in target thread
@@ -155,5 +161,5 @@
 
         // release the lock protecting join      
-        remote_spinlock_unlock( target_join_lock_xp );
+        remote_busylock_release( target_join_lock_xp );
 
         // restore IRQs
@@ -162,4 +168,10 @@
     else                                                          // joining thread is first
     {
+
+#if (DEBUG_SYS_THREAD_JOIN & 1)
+if( DEBUG_SYS_THREAD_JOIN < tm_start )
+printk("\n[DBG] %s : thread %x in process %x / joining thread arrived first\n",
+__FUNCTION__ , joining_ptr->trdid, process->pid );
+#endif
         // set the join_done flag in target thread
         hal_remote_atomic_or( target_flags_xp , THREAD_FLAG_JOIN_DONE );
@@ -169,12 +181,22 @@
 
         // register the joining thread extended pointer in target thread
-        hal_remote_swd( target_join_xp_xp , joining_xp );
+        hal_remote_s64( target_join_xp_xp , joining_xp );
 
         // release the lock protecting the join      
-        remote_spinlock_unlock( target_join_lock_xp );
-
+        remote_busylock_release( target_join_lock_xp );
+
+#if (DEBUG_SYS_THREAD_JOIN & 1)
+if( DEBUG_SYS_THREAD_JOIN < tm_start )
+printk("\n[DBG] %s : thread %x in process %x / joining thread deschedule\n",
+__FUNCTION__ , joining_ptr->trdid, process->pid );
+#endif
         // deschedule
         sched_yield( "joining thread waiting killer thread" );
     
+#if (DEBUG_SYS_THREAD_JOIN & 1)
+if( DEBUG_SYS_THREAD_JOIN < tm_start )
+printk("\n[DBG] %s : thread %x in process %x / joining thread resume\n",
+__FUNCTION__ , joining_ptr->trdid, process->pid );
+#endif
         // restore IRQs
         hal_restore_irq( save_sr );
@@ -184,6 +206,6 @@
 tm_end = hal_get_cycles();
 if( DEBUG_SYS_THREAD_JOIN < tm_end )
-printk("\n[DBG] %s : parent thread %x exit / process %x / target trdid %x / cycle %d\n",
-__FUNCTION__, joining_ptr, process->pid, trdid, (uint32_t)tm_end );
+printk("\n[DBG] %s : thread %x in process %x exit / target thread %x / cycle %d\n",
+__FUNCTION__, joining_ptr->trdid, process->pid, (uint32_t)tm_end );
 #endif
 
