Show
Ignore:
Timestamp:
04/09/08 15:37:35 (9 months ago)
Author:
aturner
Message:

switch to timespec (nanosecond precision) for sleeping. OS X's AbsoluteTime?
methods are really damn accurate! refs #41

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • features/performance/src/sleep.h

    r1970 r1971  
    5757 
    5858static inline void 
    59 nanosleep_sleep(struct timeval nap) 
     59nanosleep_sleep(struct timespec nap) 
    6060{ 
    61     struct timespec ts; 
    62     TIMEVAL_TO_TIMESPEC(&nap, &ts); 
    63     nanosleep(&ts, NULL); 
     61    nanosleep(&nap, NULL); 
    6462} 
    6563 
     
    7068 */ 
    7169static inline void 
    72 gettimeofday_sleep(struct timeval nap) 
     70gettimeofday_sleep(struct timespec nap) 
    7371{ 
    74     struct timeval now, sleep_until; 
     72    struct timeval now, sleep_until, nap_for; 
    7573    gettimeofday(&now, NULL); 
    76     timeradd(&now, &nap, &sleep_until); 
     74    TIMESPEC_TO_TIMEVAL(&nap_for, &nap); 
     75    timeradd(&now, &nap_for, &sleep_until); 
    7776     
    7877    do { 
     
    8786 
    8887 
     88/*  
     89 * Not sure if this is any better then a gettimeofday() loop, but  
     90 * in order to find out, nap needs to be a timespec (nanosec precision) 
     91 */ 
    8992static inline void 
    90 absolute_time_sleep(struct timeval nap) 
     93absolute_time_sleep(struct timespec nap) 
    9194{ 
    9295    AbsoluteTime sleep_until, naptime, time_left; 
     96    Nanoseconds nanosec; 
    9397 
    94     naptime = DurationToAbsolute(TIMEVAL_TO_MICROSEC(&nap)); 
     98    nanosec = UInt64ToUnsignedWide(TIMESPEC_TO_NANOSEC(&nap)); 
     99    naptime = NanosecondsToAbsolute(nanosec); 
    95100 
    96101    sleep_until = AddAbsoluteToAbsolute(UpTime(), naptime); 
    97      
     102 
    98103    do { 
    99104        time_left = SubAbsoluteFromAbsolute(sleep_until, UpTime()); 
     
    112117 */ 
    113118static inline void  
    114 select_sleep(const struct timeval nap) 
     119select_sleep(const struct timespec nap) 
    115120{ 
    116121    struct timeval timeout; 
    117122 
    118     timeout.tv_sec = nap.tv_sec; 
    119     timeout.tv_usec = nap.tv_usec; 
     123    TIMESPEC_TO_TIMEVAL(&timeout, &nap); 
    120124 
    121125    if (select(0, NULL, NULL, NULL, &timeout) < 0) 
     
    137141 
    138142static inline void  
    139 ioport_sleep(const struct timeval nap)  
     143ioport_sleep(const struct timespec nap)  
    140144{ 
     145    struct timeval nap_for; 
    141146    u_int32_t usec; 
    142147    time_t i; 
     148     
     149    TIMESPEC_TO_TIMEVAL(&nap_for, &nap); 
    143150     
    144151    /*  
     
    146153     * use slower 64bit integers or worry about integer overflows. 
    147154     */ 
    148     for (i = 0; i < nap.tv_sec; i ++) { 
    149         usec = SEC_TO_MICROSEC(nap.tv_sec); 
     155    for (i = 0; i < nap_for.tv_sec; i ++) { 
     156        usec = SEC_TO_MICROSEC(nap_for.tv_sec); 
    150157        while (usec > 0) { 
    151158            usec --; 
     
    155162     
    156163    /* process the usec */ 
    157     usec = nap.tv_usec; 
     164    usec = nap.tv_nsec / 1000; 
    158165    usec --; /* fudge factor for all the above */ 
    159166    while (usec > 0) {