small differences
[IRC.git] / Robust / src / Runtime / thread.c
index 8582a0165f6d9988436bba512ab545390852455b..47704013a92fada38f2c742de698590a999c76b8 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>
@@ -8,9 +9,14 @@
 #include <signal.h>
 
 #ifdef DSTM
+#ifdef RECOVERY
+#include <DSTM/interface_recovery/dstm.h>
+#include <DSTM/interface_recovery/llookup.h>
+#else
 #include <DSTM/interface/dstm.h>
 #include <DSTM/interface/llookup.h>
 #endif
+#endif
 
 #ifndef RAW
 #include <stdio.h>
@@ -125,7 +131,12 @@ void threadhandler(int sig, struct sigcontext ctx) {
   printf("signal\n");
   printf("To get stack trace, set breakpoint in threadhandler in gdb\n");
   nptrs = backtrace(buffer, 100);
+#ifdef BIT64
+  buffer[1]=(void *)ctx.rip;
+#else
   buffer[1]=(void *)ctx.eip;
+#endif
+
   strings = backtrace_symbols(buffer, nptrs);
   if (strings == NULL) {
     perror("backtrace_symbols");
@@ -138,7 +149,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;
@@ -161,7 +172,12 @@ void initializethreads() {
   initializeexithandler();
 
   //deprecated use of sighandler, but apparently still works
+#ifdef SANDBOX
+  sig.sa_handler=(void *)errorhandler;
+  abortenabled=0;
+#else
   sig.sa_handler=(void *)threadhandler;
+#endif
   sig.sa_flags=SA_RESTART;
   sigemptyset(&sig.sa_mask);
 
@@ -175,11 +191,31 @@ void initializethreads() {
   t_cache = objstrCreate(1048576);
   t_reserve=NULL;
   t_chashCreate(CHASH_SIZE, CLOADFACTOR);
+#ifdef READSET
+  rd_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
+#endif
 #ifdef DELAYCOMP
   dc_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
   ptrstack.count=0;
   primstack.count=0;
-  pl=&primstack;
+  branchstack.count=0;
+#ifdef STMARRAY
+  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");
+#ifdef STMARRAY
+  a=mprotect(downpage(&arraystack.index[MAXARRAY]), 4096, PROT_NONE);
+  if (a==-1)
+    perror("arraystack");
+#endif
 #endif
 #ifdef STMSTATS
   trec=calloc(1, sizeof(threadrec_t));
@@ -190,7 +226,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
@@ -215,6 +253,18 @@ void initializethreads() {
 
 #if defined(THREADS)||defined(STM)
 void initthread(struct ___Thread___ * ___this___) {
+#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
@@ -242,10 +292,31 @@ void initthread(struct ___Thread___ * ___this___) {
   t_cache = objstrCreate(1048576);
   t_reserve=NULL;
   t_chashCreate(CHASH_SIZE, CLOADFACTOR);
+#ifdef READSET
+  rd_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
+#endif
 #ifdef DELAYCOMP
   dc_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
   ptrstack.count=0;
   primstack.count=0;
+  branchstack.count=0;
+#ifdef STMARRAY
+  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");
+#ifdef STMARRAY
+  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);
@@ -303,6 +374,26 @@ 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
+int CALL12(___Thread______nativeGetStatus____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
+  return getStatus(___mid___);
+}
+#else 
+int CALL12(___Thread______nativeGetStatus____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
+  return 0;
+}
+#endif
+#endif
 #ifdef DSTM
 /* Add thread join capability */
 void CALL01(___Thread______join____, struct ___Thread___ * ___this___) {
@@ -323,8 +414,15 @@ transstart:
 #endif
     transAbort();
     return;
-  } else {
-
+  }
+#ifdef RECOVERY
+  else if( checkiftheMachineDead(p->___mid___) == 0) {
+    printf("Thread oid = %x is dead\n", (unsigned int) VAR(___this___));
+    transAbort();
+    return;
+  }
+#endif
+  else {
     version = (ptr-1)->version;
     if((oidarray = calloc(1, sizeof(unsigned int))) == NULL) {
       printf("Calloc error %s, %d\n", __FILE__, __LINE__);
@@ -343,7 +441,12 @@ transstart:
 #ifdef PRECISE_GC
     stopforgc((struct garbagelist *)___params___);
 #endif
+
+#ifdef RECOVERY
+    reqNotify(oidarray, versionarray, 1,p->___mid___);
+#else
     reqNotify(oidarray, versionarray, 1);
+#endif
 #ifdef PRECISE_GC
     restartaftergc();
 #endif
@@ -381,7 +484,9 @@ void CALL01(___Thread______nativeCreate____, struct ___Thread___ * ___this___) {
   pthread_mutex_unlock(&gclistlock);
   pthread_attr_init(&nattr);
   pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
-
+  INTPTR stacksize;
+  pthread_attr_getstacksize(&nattr, &stacksize);
+  printf("STACKSIZE=%u\n",stacksize);
   do {
     retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initthread, VAR(___this___));
     if (retval!=0)
@@ -479,6 +584,7 @@ void startDSMthread(int oid, int objType) {
   ptr[0]=oid;
   ptr[1]=objType;
   pthread_key_create(&oidval, globalDestructor);
+  
   do {
     retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initDSMthread,  ptr);
     if (retval!=0)