[kaffe] unix-jthreads timeout overflow fix

Jim Pick jim@kaffe.org
31 May 2002 20:33:09 -0700


Looks good, I put it in.

Cheers,

 - Jim

On Fri, 2002-05-31 at 10:19, Timothy Stack wrote:
> 
> 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
> ----
> 

> 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)		\