implemented deque for work stealing
[IRC.git] / Robust / src / Runtime / thread.c
index e76b0d9d2b2da6a88d1cf068e08c9537127de2b6..c46b100039a8e9544853c44d5adcb9729673b9fa 100644 (file)
@@ -1,5 +1,6 @@
 #include "runtime.h"
 #include <sys/types.h>
+#include <sys/mman.h>
 #include <unistd.h>
 #include <errno.h>
 #include <stdlib.h>
@@ -24,6 +25,9 @@
 #include "tm.h"
 #endif
 #include <execinfo.h>
+#ifdef EVENTMONITOR
+#include "monitor.h"
+#endif
 
 
 int threadcount;
@@ -42,7 +46,7 @@ pthread_mutex_t threadnotifylock;
 pthread_cond_t threadnotifycond;
 pthread_key_t oidval;
 
-#if defined(THREADS) || defined(DSTM) || defined(STM)
+#if defined(THREADS) || defined(DSTM) || defined(STM)||defined(MLP)
 #ifndef MAC
 extern __thread struct listitem litem;
 #else
@@ -148,7 +152,7 @@ void threadhandler(int sig, struct sigcontext ctx) {
   threadexit();
 }
 
-struct primitivelist *pl;
+#define downpage(x) ((void *)(((INTPTR)x)&~((INTPTR)4095)))
 
 void initializethreads() {
   struct sigaction sig;
@@ -169,10 +173,14 @@ void initializethreads() {
 #endif
   processOptions();
   initializeexithandler();
+#ifdef AFFINITY
+  set_affinity();
+#endif
 
   //deprecated use of sighandler, but apparently still works
 #ifdef SANDBOX
   sig.sa_handler=(void *)errorhandler;
+  abortenabled=0;
 #else
   sig.sa_handler=(void *)threadhandler;
 #endif
@@ -196,7 +204,24 @@ void initializethreads() {
   dc_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
   ptrstack.count=0;
   primstack.count=0;
-  pl=&primstack;
+  branchstack.count=0;
+#if defined(STMARRAY)&&!defined(DUALVIEW)
+  arraystack.count=0;
+#endif
+  int a=mprotect((downpage(&ptrstack.buffer[1024])), 4096, PROT_NONE);
+  if (a==-1)
+    perror("ptrstack");
+  a=mprotect(downpage(&primstack.array[MAXVALUES]), 4096, PROT_NONE);
+  if (a==-1)
+    perror("primstack");
+  a=mprotect(downpage(&branchstack.array[MAXBRANCHES]), 4096, PROT_NONE);
+  if (a==-1)
+    perror("branchstack");
+#if defined(STMARRAY)&&!defined(DUALVIEW)
+  a=mprotect(downpage(&arraystack.index[MAXARRAY]), 4096, PROT_NONE);
+  if (a==-1)
+    perror("arraystack");
+#endif
 #endif
 #ifdef STMSTATS
   trec=calloc(1, sizeof(threadrec_t));
@@ -207,7 +232,9 @@ void initializethreads() {
   { 
     int i;
     for(i=0; i<TOTALNUMCLASSANDARRAY; i++) {
-      typesCausingAbort[i] = 0;
+      typesCausingAbort[i].numaccess = 0;
+      typesCausingAbort[i].numabort = 0;
+      typesCausingAbort[i].numtrans = 0;
     }
   }
 #endif
@@ -228,10 +255,31 @@ void initializethreads() {
     list->prev=&litem;
   list=&litem;
 #endif
+#ifdef EVENTMONITOR
+  createmonitor();
+#endif
 }
 
 #if defined(THREADS)||defined(STM)
 void initthread(struct ___Thread___ * ___this___) {
+#ifdef AFFINITY
+  set_affinity();
+#endif
+#ifdef EVENTMONITOR
+  createmonitor();
+#endif
+#ifdef SANDBOX
+  struct sigaction sig;
+  abortenabled=0;
+  sig.sa_handler=(void *)errorhandler;
+  sig.sa_flags=SA_RESTART;
+  sigemptyset(&sig.sa_mask);
+
+  /* Catch bus errors, segmentation faults, and floating point exceptions*/
+  sigaction(SIGBUS,&sig,0);
+  sigaction(SIGSEGV,&sig,0);
+  sigaction(SIGFPE,&sig,0);
+#endif
 #ifdef PRECISE_GC
   INTPTR p[]={1, (INTPTR) NULL, (INTPTR) ___this___};
   //Add our litem to list of threads
@@ -266,6 +314,24 @@ void initthread(struct ___Thread___ * ___this___) {
   dc_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
   ptrstack.count=0;
   primstack.count=0;
+  branchstack.count=0;
+#if defined(STMARRAY)&&!defined(DUALVIEW)
+  arraystack.count=0;
+#endif
+  int a=mprotect(downpage(&ptrstack.buffer[1024]), 4096, PROT_NONE);
+  if (a==-1)
+    perror("ptrstack");
+  a=mprotect(downpage(&primstack.array[MAXVALUES]), 4096, PROT_NONE);
+  if (a==-1)
+    perror("primstack");
+  a=mprotect(downpage(&branchstack.array[MAXBRANCHES]), 4096, PROT_NONE);
+  if (a==-1)
+    perror("branchstack");
+#if defined(STMARRAY)&!defined(DUALVIEW)
+  a=mprotect(downpage(&arraystack.index[MAXARRAY]), 4096, PROT_NONE);
+  if (a==-1)
+    perror("arraystack");
+#endif
 #endif
  ___Thread____NNR____staticStart____L___Thread___((struct ___Thread____NNR____staticStart____L___Thread____params *)p);
  objstrDelete(t_cache);
@@ -323,6 +389,14 @@ void CALL00(___Thread______yield____) {
 }
 #endif
 
+#ifdef D___Thread______abort____
+void CALL00(___Thread______abort____) {
+#ifdef SANDBOX
+  _longjmp(aborttrans,1);
+#endif
+}
+#endif
+
 #ifdef DSTM
 #ifdef RECOVERY
 // return if the machine is dead
@@ -516,6 +590,8 @@ void startDSMthread(int oid, int objType) {
   int retval;
   pthread_attr_t nattr;
 
+//  printf("%s -> oid : %u\n",__func__,oid);
+
   pthread_mutex_lock(&gclistlock);
   threadcount++;
   pthread_mutex_unlock(&gclistlock);