about to make lots of changes to system, just committing this stable compilation...
[IRC.git] / Robust / src / Runtime / runtime.c
index 1df79aa044cb431e2d1f1b66b78acf82b80998bb..c175242faf4de4d6beb6b940a4b3cfcf725f36b2 100644 (file)
@@ -6,13 +6,28 @@
 #include <errno.h>
 #include <stdio.h>
 #include "option.h"
+#include "methodheaders.h"
 #ifdef DSTM
 #include "dstm.h"
 #include "prelookup.h"
 #include "prefetch.h"
 #endif
+#ifdef STM
+#include "tm.h"
+#include <pthread.h>
+#endif
+
+#if defined(THREADS)||defined(STM)
+/* Global barrier for STM */
+pthread_barrier_t barrier;
+pthread_barrierattr_t attr;
+#endif
+
+#include <string.h>
 
 extern int classsize[];
+extern int typearray[];
+extern int typearray2[];
 jmp_buf error_handler;
 int instructioncount;
 
@@ -30,6 +45,24 @@ int instaccum=0;
 #include "dmalloc.h"
 #endif
 
+int instanceof(struct ___Object___ *ptr, int type) {
+  int i=ptr->type;
+  do {
+    if (i==type)
+      return 1;
+    i=typearray[i];
+  } while(i!=-1);
+  i=ptr->type;
+  if (i>NUMCLASSES) {
+    do {
+      if (i==type)
+       return 1;
+      i=typearray2[i-NUMCLASSES];
+    } while(i!=-1);
+  }
+  return 0;
+}
+
 void exithandler(int sig, siginfo_t *info, void * uap) {
   exit(0);
 }
@@ -76,15 +109,69 @@ void injectinstructionfailure() {
 #endif
 }
 
+#ifdef D___Double______nativeparsedouble____L___String___
+double CALL01(___Double______nativeparsedouble____L___String___,struct ___String___ * ___str___) {
+  int length=VAR(___str___)->___count___;
+  int maxlength=(length>60)?60:length;
+  char str[maxlength+1];
+  struct ArrayObject * chararray=VAR(___str___)->___value___;
+  int i;
+  int offset=VAR(___str___)->___offset___;
+  for(i=0; i<maxlength; i++) {
+    str[i]=((short *)(((char *)&chararray->___length___)+sizeof(int)))[i+offset];
+  }
+  str[i]=0;
+  double d=atof(str);
+  return d;
+}
+#endif
+
+#ifdef D___String______convertdoubletochar____D__AR_C
+int CALL12(___String______convertdoubletochar____D__AR_C, double ___val___, double ___val___, struct ArrayObject ___chararray___) {
+  int length=VAR(___chararray___)->___length___;
+  char str[length];
+  int i;
+  int num=snprintf(str, length, "%f",___val___);
+  if (num>=length)
+    num=length-1;
+  for(i=0; i<length; i++) {
+    ((short *)(((char *)&VAR(___chararray___)->___length___)+sizeof(int)))[i]=(short)str[i];
+  }
+  return num;
+}
+#endif
+
 void CALL11(___System______exit____I,int ___status___, int ___status___) {
+#ifdef TRANSSTATS
+  printf("numTransCommit = %d\n", numTransCommit);
+  printf("numTransAbort = %d\n", numTransAbort);
+  printf("nSoftAbort = %d\n", nSoftAbort);
+#ifdef STM
+  printf("nSoftAbortCommit = %d\n", nSoftAbortCommit);
+  printf("nSoftAbortAbort = %d\n", nSoftAbortAbort);
+#ifdef STMSTATS
+  int i;
+  for(i=0; i<TOTALNUMCLASSANDARRAY; i++) {
+    printf("typesCausingAbort[%d]= %d\n", i, typesCausingAbort[i]);
+  }
+#endif
+#endif
+#endif
   exit(___status___);
 }
 
+#ifdef D___Vector______removeElement_____AR_L___Object____I_I
+void CALL23(___Vector______removeElement_____AR_L___Object____I_I, int ___index___, int ___size___, struct ArrayObject * ___array___, int ___index___, int ___size___) {
+  char* offset=((char *)(&VAR(___array___)->___length___))+sizeof(unsigned int)+sizeof(void *)*___index___;
+  memmove(offset, offset+sizeof(void *),(___size___-___index___-1)*sizeof(void *));
+}
+#endif
+
 void CALL11(___System______printI____I,int ___status___, int ___status___) {
   printf("%d\n",___status___);
 }
 
-long CALL00(___System______currentTimeMillis____) {
+long long CALL00(___System______currentTimeMillis____) {
   struct timeval tv; long long retval;
   gettimeofday(&tv, NULL);
   retval = tv.tv_sec; /* seconds */
@@ -108,15 +195,18 @@ void CALL00(___System______clearPrefetchCache____) {
   prehashClear();
 }
 
+#ifdef RANGEPREFETCH
 void CALL02(___System______rangePrefetch____L___Object_____AR_S, struct ___Object___ * ___o___, struct ArrayObject * ___offsets___) {
   /* Manual Prefetches to be inserted */
   //printf("DEBUG-> %s() ___Object___ * ___o___ = %x\n", __func__, VAR(___o___));
   //printf("DEBUG-> %s() ArrayObject * = %x\n", __func__, VAR(___offsets___));
   int numoffset=VAR(___offsets___)->___length___;
   int i;
-  short offArry[numoffset];
-  for(i = 0; i<numoffset; i++) {
-    offArry[i] = *((short *)(((char *)&VAR(___offsets___)->___length___) + sizeof(int) + i * sizeof(short)));
+  short offArry[numoffset+2];
+  offArry[0] = 0;
+  offArry[1] = 0;
+  for(i = 2; i<(numoffset+2); i++) {
+    offArry[i] = *((short *)(((char *)&VAR(___offsets___)->___length___) + sizeof(int) + (i-2) * sizeof(short)));
     //printf("DEBUG-> offArry[%d] = %d\n", i, offArry[i]);
   }
   unsigned int oid;
@@ -125,15 +215,51 @@ void CALL02(___System______rangePrefetch____L___Object_____AR_S, struct ___Objec
   } else { //even
     oid = (unsigned int) COMPOID(VAR(___o___)); //inside transaction therefore a pointer to oid
   }
-  rangePrefetch(oid, (short)numoffset, offArry);
+  rangePrefetch(oid, (short)(numoffset+2), offArry);
+}
+#else
+void CALL02(___System______rangePrefetch____L___Object_____AR_S, struct ___Object___ * ___o___, struct ArrayObject * ___offsets___) {
+  return;
+}
+#endif
+
+#endif
+
+/* STM Barrier constructs */
+#ifdef D___Barrier______setBarrier____I
+void CALL11(___Barrier______setBarrier____I, int nthreads, int nthreads) {
+  // Barrier initialization
+  int ret;
+  if((ret = pthread_barrier_init(&barrier, NULL, nthreads)) != 0) {
+    printf("%s() Could not create a barrier: numthreads = 0 in %s\n", __func__, __FILE__);
+    exit(-1);
+  }
+}
+#endif
+
+#ifdef D___Barrier______enterBarrier____
+void CALL00(___Barrier______enterBarrier____) {
+  // Synchronization point
+  int ret;
+#ifdef PRECISE_GC
+  stopforgc((struct garbagelist *)___params___);
+#endif
+  ret = pthread_barrier_wait(&barrier);
+#ifdef PRECISE_GC
+  restartaftergc();
+#endif
+  if(ret != 0 && ret != PTHREAD_BARRIER_SERIAL_THREAD) {
+    printf("%s() Could not wait on barrier: error %d in %s\n", __func__, errno, __FILE__);
+    exit(-1);
+  }
 }
 #endif
 
 /* Object allocation function */
 
 #ifdef DSTM
-__attribute__((malloc)) void * allocate_newglobal(transrecord_t *trans, int type) {
-  struct ___Object___ * v=(struct ___Object___ *) transCreateObj(trans, classsize[type]);
+__attribute__((malloc)) void * allocate_newglobal(int type) {
+  struct ___Object___ * v=(struct ___Object___ *) transCreateObj(classsize[type]);
   v->type=type;
 #ifdef THREADS
   v->tid=0;
@@ -145,8 +271,8 @@ __attribute__((malloc)) void * allocate_newglobal(transrecord_t *trans, int type
 
 /* Array allocation function */
 
-__attribute__((malloc)) struct ArrayObject * allocate_newarrayglobal(transrecord_t *trans, int type, int length) {
-  struct ArrayObject * v=(struct ArrayObject *)transCreateObj(trans, sizeof(struct ArrayObject)+length*classsize[type]);
+__attribute__((malloc)) struct ArrayObject * allocate_newarrayglobal(int type, int length) {
+  struct ArrayObject * v=(struct ArrayObject *)transCreateObj(sizeof(struct ArrayObject)+length*classsize[type]);
   if (length<0) {
     printf("ERROR: negative array\n");
     return NULL;
@@ -163,7 +289,59 @@ __attribute__((malloc)) struct ArrayObject * allocate_newarrayglobal(transrecord
 #endif
 
 
-#ifdef PRECISE_GC
+#ifdef STM
+// STM Versions of allocation functions
+
+/* Object allocation function */
+__attribute__((malloc)) void * allocate_newtrans(void * ptr, int type) {
+  struct ___Object___ * v=(struct ___Object___ *) transCreateObj(ptr, classsize[type]);
+  v->type=type;
+  v->___objlocation___=v;
+  return v;
+}
+
+/* Array allocation function */
+__attribute__((malloc)) struct ArrayObject * allocate_newarraytrans(void * ptr, int type, int length) {
+  struct ArrayObject * v=(struct ArrayObject *)transCreateObj(ptr, sizeof(struct ArrayObject)+length*classsize[type]);
+  if (length<0) {
+    printf("ERROR: negative array\n");
+    return NULL;
+  }
+  v->___objlocation___=(struct ___Object___*)v;
+  v->type=type;
+  v->___length___=length;
+  return v;
+}
+__attribute__((malloc)) void * allocate_new(void * ptr, int type) {
+  objheader_t *tmp=mygcmalloc((struct garbagelist *) ptr, classsize[type]+sizeof(objheader_t));
+  struct ___Object___ * v=(struct ___Object___ *) &tmp[1];
+  initdsmlocks(&tmp->lock);
+  tmp->version = 1;
+  v->___objlocation___=v;
+  v->type = type;
+  return v;
+}
+
+/* Array allocation function */
+
+__attribute__((malloc)) struct ArrayObject * allocate_newarray(void * ptr, int type, int length) {
+  objheader_t *tmp=mygcmalloc((struct garbagelist *) ptr, sizeof(struct ArrayObject)+length*classsize[type]+sizeof(objheader_t));
+  struct ArrayObject * v=(struct ArrayObject *) &tmp[1];
+  initdsmlocks(&tmp->lock);
+  tmp->version=1;
+  v->type=type;
+  if (length<0) {
+    printf("ERROR: negative array %d\n", length);
+    return NULL;
+  }
+  v->___objlocation___=(struct ___Object___ *)v;
+  v->___length___=length;
+  return v;
+}
+#endif
+
+#ifndef STM
+#if defined(PRECISE_GC)
 __attribute__((malloc)) void * allocate_new(void * ptr, int type) {
   struct ___Object___ * v=(struct ___Object___ *) mygcmalloc((struct garbagelist *) ptr, classsize[type]);
   v->type=type;
@@ -221,18 +399,18 @@ __attribute__((malloc)) struct ArrayObject * allocate_newarray(int type, int len
   return v;
 }
 #endif
-
+#endif
 
 /* Converts C character arrays into Java strings */
 #ifdef PRECISE_GC
 __attribute__((malloc)) struct ___String___ * NewString(void * ptr, const char *str,int length) {
 #else
-  __attribute__((malloc)) struct ___String___ * NewString(const char *str,int length) {
+__attribute__((malloc)) struct ___String___ * NewString(const char *str,int length) {
 #endif
   int i;
 #ifdef PRECISE_GC
   struct ArrayObject * chararray=allocate_newarray((struct garbagelist *)ptr, CHARARRAYTYPE, length);
-  int ptrarray[]={1, (int) ptr, (int) chararray};
+  INTPTR ptrarray[]={1, (INTPTR) ptr, (INTPTR) chararray};
   struct ___String___ * strobj=allocate_new((struct garbagelist *) &ptrarray, STRINGTYPE);
   chararray=(struct ArrayObject *) ptrarray[2];
 #else