multithreading tests from 152 lab 5
[riscv-tests.git] / mt / common / util.h
diff --git a/mt/common/util.h b/mt/common/util.h
new file mode 100755 (executable)
index 0000000..83b2b6c
--- /dev/null
@@ -0,0 +1,32 @@
+// helpful utility and synch functions 
+
+// relies on defining "ncores" before including this file...
+
+#ifndef __UTIL_H
+#define __UTIL_H
+
+#define rdcycle() ({ unsigned long _c; asm volatile ("rdcycle %0" : "=r"(_c) :: "memory"); _c; })
+#define rdinstret() ({ unsigned long _c; asm volatile ("rdinstret %0" : "=r"(_c) :: "memory"); _c; })
+                            
+void __attribute__((noinline)) barrier()
+{
+  static volatile int sense;
+  static volatile int count;
+  static __thread int threadsense;
+
+  __sync_synchronize();
+
+  threadsense = !threadsense;
+  if (__sync_fetch_and_add(&count, 1) == ncores-1)
+  {
+    count = 0;
+    sense = threadsense;
+  }
+  else while(sense != threadsense)
+    ;
+
+  __sync_synchronize();
+}
+   
+#endif //__UTIL_H
+