Index: trunk/libs/libpthread/pthread.c
===================================================================
--- trunk/libs/libpthread/pthread.c	(revision 477)
+++ trunk/libs/libpthread/pthread.c	(revision 573)
@@ -31,6 +31,5 @@
 #include <syscalls_numbers.h>
 
-#define PTHREAD_MUTEX_DEBUG     0
-#define PTHREAD_BARRIER_DEBUG   1
+#define PTHREAD_BARRIER_DEBUG   0
 
 ////////////////////////////////////////////////////////////////////////////////////////////
@@ -307,8 +306,4 @@
         if ( node->parent != NULL )  sqt_barrier_decrement( node->parent );
 
-        // reset the current node
-        node->sense = expected;
-        node->count = node->arity;
-
 #if PTHREAD_BARRIER_DEBUG
 printf("\n[BARRIER] %s : core[%x,%d] reset SQT barrier node %x :\n"
@@ -317,4 +312,8 @@
 node->level , node->arity , node->sense , node->count );
 #endif
+        // reset the current node
+        node->sense = expected;
+        node->count = node->arity;
+
         return;
     }
@@ -374,16 +373,17 @@
     }
 
-    mutex->current = 0;
-    mutex->free    = 0;
-
-#if PTHEAD_MUTEX_DEBUG
-unsigned int cxy;
-unsigned int lid;
-get_core( &cxy , &lid );
-printf("\n[MUTEX DEBUG] %s : core[%x,%d] initializes mutex %x\n",
-__FUNCTION__, cxy, lid, mutex );
-#endif
-
-    return 0;
+    return hal_user_syscall( SYS_MUTEX,
+                             (reg_t)mutex,
+                             MUTEX_INIT,
+                             0, 0 );
+}
+
+////////////////////////////////////////////////////
+int pthread_mutex_destroy( pthread_mutex_t * mutex )
+{
+    return hal_user_syscall( SYS_MUTEX,
+                             (reg_t)mutex,
+                             MUTEX_DESTROY,
+                             0, 0 );
 }
 
@@ -391,30 +391,8 @@
 int pthread_mutex_lock( pthread_mutex_t * mutex ) 
 {
-    unsigned int          ticket;
-
-    // get next free ticket
-    ticket = (unsigned int)hal_user_atomic_add( (int *)&mutex->free, 1 );
-
-#if PTHREAD_MUTEX_DEBUG
-unsigned int cxy;
-unsigned int lid;
-get_core( &cxy , &lid );
-printf("\n[MUTEX DEBUG] %s : core[%x,%d] get ticket %d\n",
-" / mutex = %x / current = %d / free = %d\n",
-__FUNCTION__, cxy, lid, ticket, mutex, mutex->current, mutex->free );
-#endif
-
-    // poll the current index
-    while( 1 )
-    {
-        if( mutex->current == ticket) break;
-    }
-               
-#if PTHREAD_MUTEX_DEBUG
-printf("\n[MUTEX DEBUG] %s : core[%x,%d] get mutex %x / current = %d / free = %d\n",
-__FUNCTION__, cxy, lid, mutex, mutex->current, mutex->free );
-#endif
-
-    return 0;
+    return hal_user_syscall( SYS_MUTEX,
+                             (reg_t)mutex,
+                             MUTEX_LOCK,
+                             0, 0 );
 }
 
@@ -422,21 +400,8 @@
 int pthread_mutex_trylock( pthread_mutex_t * mutex )
 {
-    unsigned int          ticket;
-
-    // get next free ticket
-    ticket = (unsigned int)hal_user_atomic_add( (int *)&mutex->free, 1 );
-
-#if PTHREAD_MUTEX_DEBUG
-unsigned int    cxy;
-unsigned int    lid;
-get_core( &cxy, &lid );
-printf("\n[MUTEX DEBUG] %s : core[%x,%d] get ticket = %d"
-" / mutex = %x / current = %d / free = %d\n",
-__FUNCTION__, cxy, lid, ticket, mutex, mutex->current, mutex->free );
-#endif
-
-    // test ticket
-    if( ticket == mutex->current ) return 0;       // success
-    else                           return -1;      // failure
+    return hal_user_syscall( SYS_MUTEX,
+                             (reg_t)mutex,
+                             MUTEX_TRYLOCK,
+                             0, 0 );
 }
     
@@ -444,19 +409,71 @@
 int pthread_mutex_unlock( pthread_mutex_t * mutex )
 {
-    hal_user_fence();
-
-    mutex->current = mutex->current + 1;
-
-#if PTHREAD_MUTEX_DEBUG
-unsigned int    cxy;
-unsigned int    lid;
-get_core( &cxy , &lid );
-printf("\n[MUTEX_DEBUG] %s : core[%x,%d] releases mutex %x"
-" / current = %d / free = %d\n",
-__FUNCTION__, cxy, lid, mutex, mutex->current, mutex->free );
-#endif
-
-    return 0;
-}
+    return hal_user_syscall( SYS_MUTEX,
+                             (reg_t)mutex,
+                             MUTEX_UNLOCK,
+                             0, 0 );
+}
+
+////////////////////////////////////////////////////////////////////////////////////////////
+//                               Condition variable
+////////////////////////////////////////////////////////////////////////////////////////////
+
+///////////////////////////////////////////////
+int pthread_cond_init( pthread_cond_t     * cond,
+                       pthread_condattr_t * attr )
+{
+    if( attr )
+    {
+        printf("[ERROR] in %s ; <attr> argument must be NULL\n", __FUNCTION__ );
+        return -1;
+    }
+
+   return hal_user_syscall( SYS_CONDVAR,
+                             (reg_t)cond,
+                             CONDVAR_INIT,
+                             0, 0 );
+}
+
+/////////////////////////////////////////////////
+int pthread_cond_destroy( pthread_cond_t * cond )
+{
+    return hal_user_syscall( SYS_CONDVAR,
+                             (reg_t)cond,
+                             CONDVAR_DESTROY,
+                             0, 0 );
+}
+
+//////////////////////////////////////////////
+int pthread_cond_wait( pthread_cond_t  * cond,
+                       pthread_mutex_t * mutex )
+{
+    return hal_user_syscall( SYS_CONDVAR,
+                             (reg_t)cond,
+                             CONDVAR_WAIT,
+                             (reg_t)mutex,
+                             0 );
+}
+
+////////////////////////////////////////////////
+int pthread_cond_signal( pthread_cond_t * cond )
+{
+    return hal_user_syscall( SYS_CONDVAR,
+                             (reg_t)cond,
+                             CONDVAR_SIGNAL,
+                             0, 0 );
+}
+
+///////////////////////////////////////////////////
+int pthread_cond_broadcast( pthread_cond_t * cond )
+{
+    return hal_user_syscall( SYS_CONDVAR,
+                             (reg_t)cond,
+                             CONDVAR_BROADCAST,
+                             0, 0 );
+}
+
+
+
+
 
 
