From cb7db70967814b92d2c2012958afacada606f300 Mon Sep 17 00:00:00 2001
From: bdemsky <bdemsky>
Date: Wed, 14 Mar 2007 02:12:20 +0000
Subject: [PATCH] random bug fixes & sleep call

---
 Robust/src/ClassLibrary/HashMap.java      | 24 ++++++++++++-----------
 Robust/src/ClassLibrary/Object.java       | 12 +++++++++++-
 Robust/src/ClassLibrary/ObjectJava.java   | 12 +++++++++++-
 Robust/src/ClassLibrary/StringBuffer.java | 21 ++++++++------------
 Robust/src/ClassLibrary/Thread.java       |  2 ++
 Robust/src/Runtime/object.c               |  2 +-
 Robust/src/Runtime/object.h               |  3 +--
 Robust/src/Runtime/socket.c               | 11 +++++++++++
 Robust/src/Runtime/thread.c               | 14 +++++++++++++
 9 files changed, 72 insertions(+), 29 deletions(-)

diff --git a/Robust/src/ClassLibrary/HashMap.java b/Robust/src/ClassLibrary/HashMap.java
index 11dfdf3f..db3d5694 100644
--- a/Robust/src/ClassLibrary/HashMap.java
+++ b/Robust/src/ClassLibrary/HashMap.java
@@ -63,19 +63,21 @@ public class HashMap {
     Object remove(Object key) {
 	int bin=hash(key);
 	HashEntry ptr=table[bin];
-	if (ptr.key.equals(key)) {
-	    table[bin]=ptr.next;
-	    numItems--;
-	    return ptr.value;
-	}
-	while(ptr.next!=null) {
-	    if (ptr.next.key.equals(key)) {
-		Object oldvalue=ptr.value;
-		ptr.next=ptr.next.next;
+	if (ptr!=null) {
+	    if (ptr.key.equals(key)) {
+		table[bin]=ptr.next;
 		numItems--;
-		return oldvalue;
+		return ptr.value;
+	    }
+	    while(ptr.next!=null) {
+		if (ptr.next.key.equals(key)) {
+		    Object oldvalue=ptr.value;
+		    ptr.next=ptr.next.next;
+		    numItems--;
+		    return oldvalue;
+		}
+		ptr=ptr.next;
 	    }
-	    ptr=ptr.next;
 	}
 	return null;
     }
diff --git a/Robust/src/ClassLibrary/Object.java b/Robust/src/ClassLibrary/Object.java
index dfaa7d3f..a972bfd2 100644
--- a/Robust/src/ClassLibrary/Object.java
+++ b/Robust/src/ClassLibrary/Object.java
@@ -1,5 +1,15 @@
 public class Object {
-    public native int hashCode();
+    public native int nativehashCode();
+    private int cachedCode;
+    private boolean cachedHash;
+
+    public int hashCode() {
+	if (!cachedHash) {
+	    cachedCode=nativehashCode();
+	    cachedHash=true;
+	}
+	return cachedCode;
+    }
 
     /* DON'T USE THIS METHOD UNLESS NECESSARY */
     /* WE WILL DEPRECATE IT AS SOON AS INSTANCEOF WORKS */
diff --git a/Robust/src/ClassLibrary/ObjectJava.java b/Robust/src/ClassLibrary/ObjectJava.java
index 274d6490..82b1ef68 100644
--- a/Robust/src/ClassLibrary/ObjectJava.java
+++ b/Robust/src/ClassLibrary/ObjectJava.java
@@ -1,8 +1,18 @@
 public class Object {
-    public native int hashCode();
+    public int cachedCode;
+    public boolean cachedHash;
+
+    public native int nativehashCode();
     private Object nextlockobject;
     private Object prevlockobject;
 
+    public int hashCode() {
+	if (!cachedHash) {
+	    cachedCode=nativehashCode();
+	    cachedHash=true;
+	}
+	return cachedCode;
+    }
 
     /* DON'T USE THIS METHOD UNLESS NECESSARY */
     /* WE WILL DEPRECATE IT AS SOON AS INSTANCEOF WORKS */
diff --git a/Robust/src/ClassLibrary/StringBuffer.java b/Robust/src/ClassLibrary/StringBuffer.java
index 02bb8a36..84e771e8 100644
--- a/Robust/src/ClassLibrary/StringBuffer.java
+++ b/Robust/src/ClassLibrary/StringBuffer.java
@@ -1,13 +1,11 @@
 public class StringBuffer {
     char value[];
     int count;
-    int offset;
     //    private static final int DEFAULTSIZE=16;
 
     public StringBuffer(String str) {
 	value=new char[str.count+16];//16 is DEFAULTSIZE
 	count=str.count;
-	offset=0;
 	for(int i=0;i<count;i++)
 	    value[i]=str.value[i+str.offset];
     }
@@ -15,7 +13,6 @@ public class StringBuffer {
     public StringBuffer() {
 	value=new char[16];//16 is DEFAULTSIZE
 	count=0;
-	offset=0;
     }
 
     public int length() {
@@ -27,42 +24,40 @@ public class StringBuffer {
     }
 
     public char charAt(int x) {
-	return value[x+offset];
+	return value[x];
     }
 
     public void append(String s) {
-	if ((s.count+count+offset)>value.length) {
+	if ((s.count+count)>value.length) {
 	    // Need to allocate
 	    char newvalue[]=new char[s.count+count+16]; //16 is DEFAULTSIZE
 	    for(int i=0;i<count;i++)
-		newvalue[i]=value[i+offset];
+		newvalue[i]=value[i];
 	    for(int i=0;i<s.count;i++)
 		newvalue[i+count]=s.value[i+s.offset];
 	    value=newvalue;
 	    count+=s.count;
-	    offset=0;
 	} else {
 	    for(int i=0;i<s.count;i++) {
-		value[i+count+offset]=s.value[i+s.offset];
+		value[i+count]=s.value[i+s.offset];
 	    }
 	    count+=s.count;
 	}
     }
 
     public void append(StringBuffer s) {
-	if ((s.count+count+offset)>value.length) {
+	if ((s.count+count)>value.length) {
 	    // Need to allocate
 	    char newvalue[]=new char[s.count+count+16]; //16 is DEFAULTSIZE
 	    for(int i=0;i<count;i++)
-		newvalue[i]=value[i+offset];
+		newvalue[i]=value[i];
 	    for(int i=0;i<s.count;i++)
-		newvalue[i+count]=s.value[i+s.offset];
+		newvalue[i+count]=s.value[i];
 	    value=newvalue;
 	    count+=s.count;
-	    offset=0;
 	} else {
 	    for(int i=0;i<s.count;i++) {
-		value[i+count+offset]=s.value[i+s.offset];
+		value[i+count]=s.value[i];
 	    }
 	    count+=s.count;
 	}
diff --git a/Robust/src/ClassLibrary/Thread.java b/Robust/src/ClassLibrary/Thread.java
index b9f2a822..3008a067 100644
--- a/Robust/src/ClassLibrary/Thread.java
+++ b/Robust/src/ClassLibrary/Thread.java
@@ -6,6 +6,8 @@ public class Thread {
     private static void staticStart(Thread t) {
 	t.run();
     }
+
+    public native static void sleep(long millis);
     
     public void run() {}
 
diff --git a/Robust/src/Runtime/object.c b/Robust/src/Runtime/object.c
index 4e7f7f96..ab986ecb 100644
--- a/Robust/src/Runtime/object.c
+++ b/Robust/src/Runtime/object.c
@@ -6,7 +6,7 @@
 #include "thread.h"
 #endif
 
-int CALL01(___Object______hashCode____, struct ___Object___ * ___this___) {
+int CALL01(___Object______nativehashCode____, struct ___Object___ * ___this___) {
   return (int) VAR(___this___);
 }
 
diff --git a/Robust/src/Runtime/object.h b/Robust/src/Runtime/object.h
index 73e201d8..f0d95d57 100644
--- a/Robust/src/Runtime/object.h
+++ b/Robust/src/Runtime/object.h
@@ -3,8 +3,7 @@
 #include "runtime.h"
 #include "structdefs.h"
 
-int CALL01(___Object______hashCode____, struct ___Object___ * ___this___);
-int CALL01(___Object______getType____, struct ___Object___ * ___this___);
+int CALL01(___Object______nativehashCode____, struct ___Object___ * ___this___);int CALL01(___Object______getType____, struct ___Object___ * ___this___);
 #ifdef THREADS
 int CALL01(___Object______MonitorEnter____, struct ___Object___ * ___this___);
 int CALL01(___Object______MonitorExit____, struct ___Object___ * ___this___);
diff --git a/Robust/src/Runtime/socket.c b/Robust/src/Runtime/socket.c
index 360db132..542f2dee 100644
--- a/Robust/src/Runtime/socket.c
+++ b/Robust/src/Runtime/socket.c
@@ -19,9 +19,20 @@ int CALL24(___Socket______nativeConnect____I__AR_B_I, int ___fd___, int ___port_
   sin.sin_family= AF_INET;
   sin.sin_port=htons(___port___);
   sin.sin_addr.s_addr=htonl(*(int *)(((char *)&VAR(___address___)->___length___)+sizeof(int)));
+#ifdef THREADS
+#ifdef PRECISE_GC
+  struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
+#endif
+#endif
   do {
     rc = connect(___fd___, (struct sockaddr *) &sin, sizeof(sin));
   } while (rc<0 && errno==EINTR); /* repeat if interrupted */
+#ifdef THREADS
+#ifdef PRECISE_GC
+  restartaftergc(tmp);
+#endif
+#endif
+
   
   if (rc<0) goto error;
 
diff --git a/Robust/src/Runtime/thread.c b/Robust/src/Runtime/thread.c
index 7c22e069..5f34adf6 100644
--- a/Robust/src/Runtime/thread.c
+++ b/Robust/src/Runtime/thread.c
@@ -78,6 +78,20 @@ void initthread(struct ___Thread___ * ___this___) {
   pthread_mutex_unlock(&gclistlock);
 }
 
+void CALL11(___Thread______sleep____J, long long ___millis___, long long ___millis___) {
+#ifdef THREADS
+#ifdef PRECISE_GC
+  struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
+#endif
+#endif
+  usleep(___millis___);  
+#ifdef THREADS
+#ifdef PRECISE_GC
+  restartaftergc(tmp);
+#endif
+#endif
+}
+
 void CALL01(___Thread______nativeCreate____, struct ___Thread___ * ___this___) {
   pthread_t thread;
   int retval;
-- 
2.34.1