Skip to content
  • Keith Packard's avatar
    os: Clean up WaitFor.c · 0b2f3083
    Keith Packard authored and Adam Jackson's avatar Adam Jackson committed
    
    
    Do all timer stuff before blocking, avoiding a bunch of duplicate code
    and merge common code in WaitForSomething.
    
    The WaitForSomething changes need a bit of explanation to show that
    the new code is effectively equivalent to the old. Eliding error
    checking and trivial bits we've got:
    
    Before:
    
    	if (ready clients)
    		timeout = 0
    	else
    		compute timeout
    	i = poll
    	if (i <= 0) {
    		if (ready clients)
    			return TRUE;
    		if (input)
    			return FALSE;
    		if (any ready timers) {
    			run timers
    			return FALSE;
    		}
    	} else {
    		if (input)
    			return FALSE;
    		if (any ready timers) {
    			run timers
    			return FALSE;
    		}
    		if (ready clients)
    			return TRUE;
    	}
    
    After:
    
    	if (ready clients)
    		timeout = 0;
    	else
    		compute timeout
    		run_timers
    	poll
    
    	if (input)
    		return FALSE;
    
    	if (ready clients)
    		return TRUE;
    
    The old code would return TRUE if there were ready clients and input
    pending. Dispatch would then schedule that ready client, but before
    processing any requests, it would notice that there was input pending
    and go process it. The new code just checks for input first, which is
    effectively the same.
    
    If the poll timed out and there weren't clients ready, then timers
    would get run.
    
    If the poll didn't time out, then timers would get run, even if there
    were clients now ready. Now, if the timeout interval was zero, that
    means that the timers must have been ready *before* poll was
    invoked. In this case, we should simply run the timers before calling
    poll -- no sense calling poll just to discard any data that it
    generates.
    
    If the timeout interval was non-zero, and poll didn't timeout, then
    either there aren't any timers to run, or we got a surprise and hit a
    timer exactly as a client became ready to run. This is the one case
    where the new code is different from the old; the new code delays the
    timer call until the next time WaitForSomething is called.
    
    Signed-off-by: default avatarKeith Packard <keithp@keithp.com>
    Reviewed-by: default avatarAdam Jackson <ajax@redhat.com>
    0b2f3083