From 0fe08538e9b54abc95ccad708890523b2e284340 Mon Sep 17 00:00:00 2001
From: Cedric Roux <cedric.roux@eurecom.fr>
Date: Mon, 21 Mar 2016 16:44:12 +0100
Subject: [PATCH] add __synch_synchronize for the T_cache it probably makes
 things zillion times slower but it seems to be necessary

---
 T.c           | 13 ++++++++++---
 T.h           |  6 ++++--
 tracer/main.c | 10 +++++++++-
 3 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/T.c b/T.c
index a33d1e90b6..13bf25b324 100644
--- a/T.c
+++ b/T.c
@@ -54,9 +54,16 @@ static void *T_send_thread(void *_)
 {
   while (1) {
     usleep(5000);
+    __sync_synchronize();
     while (T_cache[T_busylist_head].busy) {
-      char *b = T_cache[T_busylist_head].buffer;
-      int l = T_cache[T_busylist_head].length;
+      char *b;
+      int l;
+      /* TODO: be sure about those memory barriers - in doubt one is
+       * put here too
+       */
+      __sync_synchronize();
+      b = T_cache[T_busylist_head].buffer;
+      l = T_cache[T_busylist_head].length;
       while (l) {
         int done = write(T_socket, b, l);
         if (done <= 0) {
@@ -123,7 +130,7 @@ void T_connect_to_tracer(char *addr, int port)
 
 #ifdef T_USE_SHARED_MEMORY
   /* setup shared memory */
-  T_shm_fd = shm_open(T_SHM_FILENAME, O_RDWR, 0666);
+  T_shm_fd = shm_open(T_SHM_FILENAME, O_RDWR /*| O_SYNC*/, 0666);
   shm_unlink(T_SHM_FILENAME);
   if (T_shm_fd == -1) { perror(T_SHM_FILENAME); abort(); }
   T_cache = mmap(NULL, T_CACHE_SIZE * sizeof(T_cache_t),
diff --git a/T.h b/T.h
index 405594f2d4..6797213d31 100644
--- a/T.h
+++ b/T.h
@@ -118,16 +118,18 @@ extern T_cache_t *T_cache;
 #ifdef T_USE_SHARED_MEMORY
 
 #define T_SEND() \
-  T_cache[T_LOCAL_slot].busy = 1; \
   T_cache[T_LOCAL_slot].length = T_LOCAL_size; \
+  __sync_synchronize(); \
+  T_cache[T_LOCAL_slot].busy = 1; \
   T_send(T_LOCAL_buf, T_LOCAL_size)
 
 #else /* T_USE_SHARED_MEMORY */
 
 /* when not using shared memory, wait for send to finish */
 #define T_SEND() \
-  T_cache[T_LOCAL_slot].busy = 1; \
   T_cache[T_LOCAL_slot].length = T_LOCAL_size; \
+  __sync_synchronize(); \
+  T_cache[T_LOCAL_slot].busy = 1; \
   T_send(T_LOCAL_buf, T_LOCAL_size); \
   while (T_cache[T_LOCAL_slot].busy) usleep(1*1000)
 
diff --git a/tracer/main.c b/tracer/main.c
index 18ed8087cc..cf8f8aac85 100644
--- a/tracer/main.c
+++ b/tracer/main.c
@@ -356,7 +356,8 @@ void wait_message(void)
 
 void init_shm(void)
 {
-  int s = shm_open(T_SHM_FILENAME, O_RDWR | O_CREAT, 0666);
+  int i;
+  int s = shm_open(T_SHM_FILENAME, O_RDWR | O_CREAT /*| O_SYNC*/, 0666);
   if (s == -1) { perror(T_SHM_FILENAME); abort(); }
   if (ftruncate(s, T_CACHE_SIZE * sizeof(T_cache_t)))
     { perror(T_SHM_FILENAME); abort(); }
@@ -365,6 +366,12 @@ void init_shm(void)
   if (T_cache == NULL)
     { perror(T_SHM_FILENAME); abort(); }
   close(s);
+
+  /* let's garbage the memory to catch some potential problems
+   * (think multiprocessor sync issues, barriers, etc.)
+   */
+  memset(T_cache, 0x55, T_CACHE_SIZE * sizeof(T_cache_t));
+  for (i = 0; i < T_CACHE_SIZE; i++) T_cache[i].busy = 0;
 }
 
 #endif /* T_USE_SHARED_MEMORY */
@@ -555,6 +562,7 @@ no_init_message:
   while (1) {
 #ifdef T_USE_SHARED_MEMORY
     wait_message();
+    __sync_synchronize();
 #endif
 
 #ifdef T_USE_SHARED_MEMORY
-- 
GitLab