[kaffe] unix-jthreads timeout overflow fix

Timothy Stack stack at cs.utah.edu
Fri May 31 10:19:19 PDT 2002


hi,

Attached is a fix for unix-jthreads that should better handle large
timeout values.  For example:

  synchronized( this )
  {
    this.wait(Long.MAX_VALUE);
  }

eventually reaches this code:

  jtid->time = timeout + currentTime();

which overflows and gives a bogus value.

tim
-------------- next part --------------
Index: jthread.c
===================================================================
RCS file: /cvs/kaffe/kaffe/kaffe/kaffevm/systems/unix-jthreads/jthread.c,v
retrieving revision 1.86
diff -u -r1.86 jthread.c
--- jthread.c	29 May 2002 19:46:24 -0000	1.86
+++ jthread.c	31 May 2002 17:11:56 -0000
@@ -255,26 +255,34 @@
 addToAlarmQ(jthread* jtid, jlong timeout)
 {
 	jthread** tidp;
+	jlong ct;
 
 	assert(intsDisabled());
 
-	jtid->flags |= THREAD_FLAGS_ALARM;
-
-	/* Get absolute time */
-	jtid->time = timeout + currentTime();
-
-	/* Find place in alarm list and insert it */
-	for (tidp = &alarmList; (*tidp) != 0; tidp = &(*tidp)->nextalarm) {
-		if ((*tidp)->time > jtid->time) {
-			break;
+	ct = currentTime();
+	if( (timeout + ct) > ct ) {
+		jtid->flags |= THREAD_FLAGS_ALARM;
+		
+		/* Get absolute time */
+		jtid->time = timeout + ct;
+		
+		/* Find place in alarm list and insert it */
+		for (tidp = &alarmList;
+		     (*tidp) != 0;
+		     tidp = &(*tidp)->nextalarm) {
+			if ((*tidp)->time > jtid->time) {
+				break;
+			}
 		}
-	}
-	jtid->nextalarm = *tidp;
-	*tidp = jtid;
-
-	/* If I'm head of alarm list, restart alarm */
-	if (tidp == &alarmList) {
-		MALARM(timeout);
+		jtid->nextalarm = *tidp;
+		*tidp = jtid;
+		
+		/* If I'm head of alarm list, restart alarm */
+		if (tidp == &alarmList) {
+			MALARM(timeout);
+		}
+	} else {
+		/* Huge timeout value, ignore it. */
 	}
 }
 
@@ -2216,8 +2224,13 @@
  * various building blocks for timeout system call functions
  */
 #define SET_DEADLINE(deadline, timeout) 		\
-	if (timeout != NOTIMEOUT) {			\
-		deadline = timeout + currentTime();	\
+	if (timeout != NOTIMEOUT) { 			\
+		jlong ct = currentTime();		\
+		deadline = timeout + ct;		\
+		if( deadline < ct ) {			\
+			deadline = 0;			\
+			timeout = NOTIMEOUT;		\
+		}					\
 	}
 
 #define BREAK_IF_LATE(deadline, timeout)		\


More information about the kaffe mailing list