Index: /soft/giet_vm/sort/main.c
===================================================================
--- /soft/giet_vm/sort/main.c	(revision 256)
+++ /soft/giet_vm/sort/main.c	(revision 257)
@@ -1,39 +1,83 @@
-//=================================================================================================
-// File       : main.c
-//
-// Date       : 11/2013
-//
-// Author     : Cesar Fuguet Tortolero
-//            :<cesar.fuguet-tortolero@lip6.fr>
-//
-// Description: Sort application using the GIET-VM OS.
-//              This application uses the barrier routines to apply a sort algorithm
-//              in several stages.
-//=================================================================================================
+///////////////////////////////////////////////////////////////////////////////
+// File :
+//      
+//      main.c
+//
+// Date :
+//      
+//      November 2013
+//
+// Author :
+//
+//      Cesar Fuguet Tortolero <cesar.fuguet-tortolero@lip6.fr>
+//
+// Description :
+//
+//      Sort application using the GIET-VM OS. This application uses the
+//      barrier routines to apply a sort algorithm in several stages.
+//
+//      Considerations :
+//
+//          - It supports up to 256 processors and the number of processors
+//            must be a power of 2.
+//
+//          - If there is only one TTY available, this application uses a spin
+//            lock to avoid several threads writting at the same time.
+//
+//          - This application must be executed on a cache coherent
+//            architecture. Otherwise some modifications must be applied
+//
+//          - The processors executing this application must have a contiguous
+//            processor id and the first processor must have id 0.
+//
+//      TODO: Replace processor id based identification mechanism by one based
+//            on thread id
+//
+///////////////////////////////////////////////////////////////////////////////
 
 #include "stdio.h"
 #include "hard_config.h"
 #include "barrier.h"
-
-#define NPROCS          8
-#define ARRAY_LENGTH    (NPROCS * 50)
+#include "spin_lock.h"
+
+//////////////////////////////////////////////////////////////////////////
+// The NPROCS constant must be modified depending on the desired number of
+// threads
+
+#define NPROCS          16
+#define ARRAY_LENGTH    (NPROCS * 128)
 #define IPP             (ARRAY_LENGTH / NPROCS) // ITEMS PER PROCESSOR
 
-// Other processors than 0 display algorithm state 
+////////////////////////////////////////////////////////////////////////////////
+// Processors other than 0 display algorithm state
+// The processor 0 always displays some information so this does not affect him 
+
 #define VERBOSE         1
 
+///////////////////////////////////////////////////////////////////////
+// Define printf according to verbosity option and number of available
+// TTY
+
 #if (VERBOSE == 1)
-#define printf(...)     giet_tty_printf(__VA_ARGS__)
-#define puts(...)       giet_tty_puts(__VA_ARGS__)
-#else
-#define printf(...)
-#define puts(...)
+#   if (NB_TTY_CHANNELS > 1)
+#       define printf(...)     giet_tty_printf(__VA_ARGS__)
+#       define puts(...)       giet_tty_puts(__VA_ARGS__)
+
+#   else    // NB_TTY_CHANNELS == 0
+#       define printf(...) \
+        lock_acquire(&tty_lock); \
+        giet_tty_printf(__VA_ARGS__); \
+        lock_release(&tty_lock);  
+#   endif
+#else       // VERBOSE == 0
+#   define printf(...)
+#   define puts(...)
 #endif
+
+#define task0_printf(...) if(procid() == 0) giet_tty_printf(__VA_ARGS__)
 
 #define exit    giet_exit
 #define procid  giet_procid
 #define rand    giet_rand
-
-#define task0_printf(...) if(procid() == 0) giet_tty_printf(__VA_ARGS__)
 
 int array0[ARRAY_LENGTH];
@@ -55,7 +99,10 @@
         int init_pos_result);
 
-// this application support at most 256 processors
-// number of barriers = log2(NPROCS)
+///////////////////////////////////////////////////
+// This application support at most 256 processors
+// Number of barriers = log2(NPROCS)
+
 giet_barrier_t barrier[8];
+giet_lock_t tty_lock;
 
 __attribute__ ((constructor)) void sort()
@@ -66,11 +113,8 @@
     int i;
 
-    /**************************************************************************/
-    /* Hello World */
-
     task0_printf("Starting SORT application\n");
 
-    /**************************************************************************/
-    /* Array Inititialitatin */
+    ////////////////////////
+    // Array Initialization
 
     for (i = IPP * proc_id; i < IPP * (proc_id + 1); i++)
@@ -79,6 +123,6 @@
     }
 
-    /**************************************************************************/
-    /* Barriers Inititialitatin */
+    ///////////////////////////
+    // Barriers Initialization
 
     while((proc_id != 0) && (init_ok == 0));
@@ -88,5 +132,5 @@
         for (i = 0; i < __builtin_ctz(NPROCS); i++)
         {
-            printf("Initializing barrier %d with %d\n", i, NPROCS >> i);
+            task0_printf("Initializing barrier %d with %d\n", i, NPROCS >> i);
             barrier_init(&barrier[i], NPROCS >> i);
         }
@@ -99,10 +143,12 @@
     barrier_wait(&barrier[0]);
 
-    /**************************************************************************/
-    /* Parallel sorting of array pieces */
-
-    task0_printf("Stage 0: Processor Sorting...\n\r");
+    ///////////////////////////////////
+    // Parallel sort of array elements
+
+    printf("Proc %d Stage 0: Processor Sorting...\n\r", proc_id);
+
     bubbleSort(array0, IPP, IPP * proc_id);
-    task0_printf("Finishing Stage 0...\n\r");
+
+    printf("Proc %d Finishing Stage 0...\n\r", proc_id);
 
     for (i = 0; i < __builtin_ctz(NPROCS); i++)
@@ -110,5 +156,6 @@
         asm volatile ("sync");
         barrier_wait(&barrier[i]);
-        task0_printf("Stage %d: Starting...\n\r", i+1);
+
+        printf("Proc %d Stage %d: Starting...\n\r", proc_id, i+1);
 
         if((proc_id % (2 << i)) != 0) exit();
@@ -132,9 +179,12 @@
                 );
 
-        task0_printf("Finishing Stage %d...\n\r", i + 1);
+        printf("Proc %d Finishing Stage %d...\n\r", proc_id, i + 1);
     }
 
     int success;
     int failure_index;
+
+    //////////////////////////////
+    // Verify the resulting array
 
     if(proc_id == 0)
