Index: soft/giet_vm/applications/sort/main.c
===================================================================
--- soft/giet_vm/applications/sort/main.c	(revision 515)
+++ soft/giet_vm/applications/sort/main.c	(revision 669)
@@ -3,50 +3,40 @@
 // Date   :  November 2013
 // Author :  Cesar Fuguet Tortolero <cesar.fuguet-tortolero@lip6.fr>
+///////////////////////////////////////////////////////////////////////////////
+// This multi-threaded application implement a multi-stage sort application.
+// The various stages are separated by synchronisation barriers.
+// There is one thread per physical processors. Computation is organised as
+// a binary tree: All threads contribute to the first stage of parallel sort
+// but, the number of participating threads is divided by 2 at each next stage.
+//       Number_of_stages = number of barriers = log2(Number_of_threads)
 //
-// Description :
-//
-//      Sort application using the GIET-VM OS. This application uses the
-//      barrier routines to apply a sort algorithm in several stages.
-//
-//      Constraints :
-//
-//      - It supports up to 1024 processors and the number of processors
-//        must be a power of 2.
-//
-//      _ The array of values to be sorted (ARRAY_LENGTH) must be power of 2 
-//        larger than the number of processors.
-//
-//      - This application must be executed on a cache coherent architecture. 
-//
+// Constraints :
+// - It supports up to 1024 processors and the number of processors
+//   must be a power of 2.
+// _ The array of values to be sorted (ARRAY_LENGTH) must be power of 2 
+//   larger than the number of processors.
+// - This application uses a private TTY terminal, shared by all threads,
+//   that is protectted by an user-level SQT lock.
 ///////////////////////////////////////////////////////////////////////////////
 
 #include "stdio.h"
 #include "mapping_info.h"
-#include "hard_config.h"
 #include "user_barrier.h"
+#include "user_lock.h"
 
 #define ARRAY_LENGTH    0x1000
 #define IPT             (ARRAY_LENGTH / threads) // ITEMS PER THREAD
 
-////////////////////////////////////////////////////////////////////////////////
-// Processors other than 0 display algorithm state if VERBOSE non zero
-
-#define VERBOSE         1
-
-////////////////////////////////////////////////////////////////////////////////
-// Define printf according to verbosity option and number of available TTY
-
-#if (VERBOSE == 1)
-#   define printf(...)     giet_shr_printf(__VA_ARGS__)
-#else      
-#   define printf(...)
-#endif
-
-#define task0_printf(...) if(thread_id == 0) giet_shr_printf(__VA_ARGS__)
-
-int array0[ARRAY_LENGTH];
-int array1[ARRAY_LENGTH];
-
-volatile int init_ok = 0;
+// macro to use a shared TTY
+#define printf(...)     lock_acquire( &tty_lock ); \
+                        giet_tty_printf(__VA_ARGS__);  \
+                        lock_release( &tty_lock )
+
+int              array0[ARRAY_LENGTH];
+int              array1[ARRAY_LENGTH];
+
+volatile int     init_ok = 0;
+giet_barrier_t   barrier[10];
+user_lock_t      tty_lock;   
 
 void bubbleSort(
@@ -63,22 +53,11 @@
         int init_pos_result);
 
-///////////////////////////////////////////////////////
-// This application supports at most 1024 processors
-// Number of barriers = log2(threads)
-
-giet_barrier_t barrier[10];
-
 //////////////////////////////////////////
-__attribute__ ((constructor)) void sort()
+__attribute__ ((constructor)) void main()
+//////////////////////////////////////////
 {
-    int thread_id = giet_thread_id();
     int * src_array = NULL;
     int * dst_array = NULL;
     int i;
-    
-    unsigned int time_start = giet_proctime();
-    unsigned int time_end;   
-
-    // compute number of threads (one thread per proc)
     unsigned int x_size;
     unsigned int y_size;
@@ -86,7 +65,41 @@
     unsigned int threads;
 
+    // each thread gets its thread_id
+    int thread_id = giet_thread_id();
+    
+    unsigned int time_start = giet_proctime();
+    unsigned int time_end;   
+
+    // each thread compute number of threads (one thread per proc)
     giet_procs_number( &x_size , &y_size , &nprocs );
     threads = x_size * y_size * nprocs;
 
+    // thread 0 makes TTY and barrier initialisations
+    // other threads wait initialisation completion.
+    if ( thread_id == 0 )
+    {
+        // request a shared TTY used by all threads
+        giet_tty_alloc(1);
+        
+        // TTY lock initialisation
+        lock_init( &tty_lock );
+
+        printf("\n[ SORT T0 ] Starting sort application with %d threads "
+                 "at cycle %d\n", threads, time_start);
+
+        // Barriers Initialization
+        for (i = 0; i < __builtin_ctz( threads ); i++)
+        {
+            barrier_init( &barrier[i], threads >> i );
+        }
+
+        init_ok = 1;
+    }
+    else
+    {
+        while( !init_ok );
+    }
+
+    // each thread checks number of tasks
     if ( (threads != 1)   && (threads != 2)   && (threads != 4)   && 
          (threads != 8)   && (threads != 16 ) && (threads != 32)  && 
@@ -94,33 +107,9 @@
          (threads != 512) && (threads != 1024) )
     {
-        task0_printf("[SORT ERROR] Number of processors must be power of 2\n"
-                     "  x_size = %d / y_size = %d / nprocs = %d\n",
-                     x_size , y_size , nprocs );
-        giet_exit("error");
-    }
-
-    task0_printf("\n[ Thread 0 ] Starting sort application with %d threads "
-                 "at cycle %d\n", threads, time_start);
-
-    ///////////////////////////
-    // Barriers Initialization
-
-    if (thread_id == 0)
-    {
-        for (i = 0; i < __builtin_ctz( threads ); i++)
-        {
-            barrier_init(&barrier[i], threads >> i);
-        }
-
-        init_ok = 1;
-    }
-    else
-    {
-        while(!init_ok);
-    }
-
-    ////////////////////////
-    // Array Initialization
-
+        giet_exit("error : number of processors must be power of 2");
+    }
+
+
+    // Each thread contribute to Array Initialization
     for (i = IPT * thread_id; i < IPT * (thread_id + 1); i++)
     {
@@ -128,24 +117,23 @@
     }
 
-    ///////////////////////////////////
-    // Parallel sort of array elements
-
-    printf("[ Thread %d ] Stage 0: Sorting...\n\r", thread_id);
+    // all threads contribute to the first stage of parallel sort
+    printf("[ SORT T%d ] Stage 0: Sorting...\n\r", thread_id);
 
     bubbleSort(array0, IPT, IPT * thread_id);
 
-    printf("[ Thread %d ] Finishing Stage 0\n\r", thread_id);
-
+    printf("[ SORT T%d ] Finishing Stage 0\n\r", thread_id);
+
+    // the number of threads is divided by 2 at each next stage
     for (i = 0; i < __builtin_ctz( threads ); i++)
     {
-        barrier_wait(&barrier[i]);
+        barrier_wait( &barrier[i] );
 
         if((thread_id % (2 << i)) != 0)
         {
-            printf("[ Thread %d ] Quit\n\r", thread_id );
+            printf("[ SORT T%d ] Quit\n\r", thread_id );
             giet_exit("Completed");
         }
 
-        printf("[ Thread %d ] Stage %d: Sorting...\n\r", thread_id, i+1);
+        printf("[ SORT T%d ] Stage %d: Sorting...\n\r", thread_id, i+1);
 
         if((i % 2) == 0)
@@ -167,5 +155,5 @@
                 );
 
-        printf("[ Thread %d ] Finishing Stage %d\n\r", thread_id, i + 1);
+        printf("[ SORT T%d ] Finishing Stage %d\n\r", thread_id, i + 1);
     }
 
@@ -173,7 +161,5 @@
     int failure_index;
 
-    //////////////////////////////
     // Verify the resulting array
-    
     if(thread_id != 0)
     {
@@ -186,5 +172,4 @@
         if(dst_array[i] > dst_array[i+1])
         {
-
             success = 0;
             failure_index = i;
@@ -195,6 +180,6 @@
     time_end = giet_proctime();
 
-    printf("[ Thread 0 ] Finishing sort application at cycle %d\n"
-           "[ Thread 0 ] Time elapsed = %d\n",
+    printf("[ SORT T0 ] Finishing sort application at cycle %d\n"
+           "[ SORT T0 ] Time elapsed = %d\n",
             time_end, (time_end - time_start) );
 
@@ -205,5 +190,5 @@
     else
     {
-        printf("[ Thread 0 ] Failure!! Incorrect element: %d\n\r", 
+        printf("[ SORT T0 ] Failure!! Incorrect element: %d\n\r", 
                failure_index);
         for(i=0; i<ARRAY_LENGTH; i++)
