Changeset 320e6b5eaa2ea265c86b12c93f0ec57dec270fd4

Show
Ignore:
Timestamp:
01/03/07 21:16:49 (2 years ago)
Author:
Rémi Denis-Courmont <rem@videolan.org>
git-committer:
Rémi Denis-Courmont <rem@videolan.org> 1172780209 +0000
git-parent:

[c13d67b82c674fffe5d65656aff7484c227a74a1]

git-author:
Rémi Denis-Courmont <rem@videolan.org> 1172780209 +0000
Message:

Cache system clock in userland to save a few avoidable sleeps.
Also restore (albeit not hard-coded this time) the old Linux custom
of not waiting for a delay shorter than the scheduler precision

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • src/misc/mtime.c

    r220ff7f r320e6b5  
    111111} 
    112112 
     113/** 
     114 * Return a value that is no bigger than the clock precision 
     115 * (possibly zero). 
     116 */ 
     117static inline unsigned mprec( void ) 
     118{ 
     119#if defined (HAVE_CLOCK_NANOSLEEP) 
     120    struct timespec ts; 
     121    if( clock_getres( CLOCK_MONOTONIC, &ts )) 
     122        clock_getres( CLOCK_REALTIME, &ts ); 
     123 
     124    return ts.tv_nsec / 1000; 
     125#endif 
     126    return 0; 
     127} 
     128 
     129static unsigned prec = 0; 
     130static volatile mtime_t cached_time = 0; 
    113131 
    114132/** 
     
    120138mtime_t mdate( void ) 
    121139{ 
     140    mtime_t res; 
     141 
    122142#if defined (HAVE_CLOCK_NANOSLEEP) 
    123143    struct timespec ts; 
     
    130150        (void)clock_gettime( CLOCK_REALTIME, &ts ); 
    131151 
    132     return ((mtime_t)ts.tv_sec * (mtime_t)1000000) 
     152    res = ((mtime_t)ts.tv_sec * (mtime_t)1000000) 
    133153           + (mtime_t)(ts.tv_nsec / 1000); 
    134154 
    135155#elif defined( HAVE_KERNEL_OS_H ) 
    136     return( real_time_clock_usecs() ); 
     156    res = real_time_clock_usecs(); 
    137157 
    138158#elif defined( WIN32 ) || defined( UNDER_CE ) 
     
    176196        lldiv_t d = lldiv (counter.QuadPart, freq); 
    177197 
    178         return (d.quot * 1000000) 
    179              + ((d.rem * 1000000) / freq); 
     198        res = (d.quot * 1000000) + ((d.rem * 1000000) / freq); 
    180199    } 
    181200    else 
     
    189208        static mtime_t i_previous_time = I64C(-1); 
    190209        static int i_wrap_counts = -1; 
    191         mtime_t usec_time; 
    192210 
    193211        if( i_wrap_counts == -1 ) 
     
    200218 
    201219        EnterCriticalSection( &date_lock ); 
    202         usec_time = I64C(1000) * 
     220        res = I64C(1000) * 
    203221            (i_wrap_counts * I64C(0x100000000) + GetTickCount()); 
    204         if( i_previous_time > usec_time
     222        if( i_previous_time > res
    205223        { 
    206224            /* Counter wrapped */ 
     
    210228        i_previous_time = usec_time; 
    211229        LeaveCriticalSection( &date_lock ); 
    212  
    213         return usec_time; 
    214230    } 
    215231#else 
     
    218234    /* gettimeofday() cannot fail given &tv_date is a valid address */ 
    219235    (void)gettimeofday( &tv_date, NULL ); 
    220     return( (mtime_t) tv_date.tv_sec * 1000000 + (mtime_t) tv_date.tv_usec ); 
    221 #endif 
     236    res = (mtime_t) tv_date.tv_sec * 1000000 + (mtime_t) tv_date.tv_usec; 
     237#endif 
     238 
     239    return cached_time = res; 
    222240} 
    223241 
     
    232250void mwait( mtime_t date ) 
    233251{ 
     252    if( prec == 0 ) 
     253        prec = mprec(); 
     254 
     255    /* If the deadline is already elapsed, or within the clock precision, 
     256     * do not even bother the clock. */ 
     257    if( ( date - cached_time ) < (mtime_t)prec ) // OK: mtime_t is signed 
     258        return; 
     259 
    234260#if 0 && defined (HAVE_CLOCK_NANOSLEEP) 
    235261    lldiv_t d = lldiv( date, 1000000 ); 
     
    257283void msleep( mtime_t delay ) 
    258284{ 
     285    mtime_t earlier = cached_time; 
     286 
    259287#if defined( HAVE_CLOCK_NANOSLEEP )  
    260288    lldiv_t d = lldiv( delay, 1000000 ); 
     
    298326    select( 0, NULL, NULL, NULL, &tv_delay ); 
    299327#endif 
     328 
     329    earlier += delay; 
     330    if( cached_time < earlier ) 
     331        cached_time = earlier; 
    300332} 
    301333