Changeset e2b38c968064a42fc31f82e7a0bb9a6d1d79d531

Show
Ignore:
Timestamp:
19/05/08 19:39:16 (7 months ago)
Author:
Rémi Denis-Courmont <rem@videolan.org>
git-committer:
Rémi Denis-Courmont <rem@videolan.org> 1211218756 +0300
git-parent:

[f2312da8a85f4646ac97ce46bc98a08dd3d2de4b]

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

mprec: thread-safety

Files:

Legend:

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

    rd1d3dc1 re2b38c9  
    145145} 
    146146 
    147 /** 
    148  * Return a value that is no bigger than the clock precision 
    149  * (possibly zero). 
    150  */ 
    151 static inline unsigned mprec( void ) 
    152 { 
    153147#if defined (HAVE_CLOCK_NANOSLEEP) 
     148static unsigned prec = 0; 
     149 
     150static void mprec_once( void ) 
     151{ 
    154152    struct timespec ts; 
    155153    if( clock_getres( CLOCK_MONOTONIC, &ts )) 
    156154        clock_getres( CLOCK_REALTIME, &ts ); 
    157155 
    158     return ts.tv_nsec / 1000; 
    159 #endif 
     156    prec = ts.tv_nsec / 1000; 
     157
     158#endif 
     159 
     160/** 
     161 * Return a value that is no bigger than the clock precision 
     162 * (possibly zero). 
     163 */ 
     164static inline unsigned mprec( void ) 
     165
     166#if defined (HAVE_CLOCK_NANOSLEEP) 
     167    static pthread_once_t once = PTHREAD_ONCE_INIT; 
     168    pthread_once( &once, mprec_once ); 
     169    return prec; 
     170#else 
    160171    return 0; 
    161 
    162  
    163 static unsigned prec = 0; 
     172#endif 
     173
     174 
    164175static volatile mtime_t cached_time = 0; 
    165176 
     
    316327void mwait( mtime_t date ) 
    317328{ 
    318     if( prec == 0 ) 
    319         prec = mprec(); 
    320  
    321329    /* If the deadline is already elapsed, or within the clock precision, 
    322330     * do not even bother the clock. */ 
    323     if( ( date - cached_time ) < (mtime_t)prec ) // OK: mtime_t is signed 
     331    if( ( date - cached_time ) < (mtime_t)mprec() ) // OK: mtime_t is signed 
    324332        return; 
    325333