Changeset 320e6b5eaa2ea265c86b12c93f0ec57dec270fd4
- 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
| r220ff7f |
r320e6b5 |
|
| 111 | 111 | } |
|---|
| 112 | 112 | |
|---|
| | 113 | /** |
|---|
| | 114 | * Return a value that is no bigger than the clock precision |
|---|
| | 115 | * (possibly zero). |
|---|
| | 116 | */ |
|---|
| | 117 | static 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 | |
|---|
| | 129 | static unsigned prec = 0; |
|---|
| | 130 | static volatile mtime_t cached_time = 0; |
|---|
| 113 | 131 | |
|---|
| 114 | 132 | /** |
|---|
| … | … | |
| 120 | 138 | mtime_t mdate( void ) |
|---|
| 121 | 139 | { |
|---|
| | 140 | mtime_t res; |
|---|
| | 141 | |
|---|
| 122 | 142 | #if defined (HAVE_CLOCK_NANOSLEEP) |
|---|
| 123 | 143 | struct timespec ts; |
|---|
| … | … | |
| 130 | 150 | (void)clock_gettime( CLOCK_REALTIME, &ts ); |
|---|
| 131 | 151 | |
|---|
| 132 | | return ((mtime_t)ts.tv_sec * (mtime_t)1000000) |
|---|
| | 152 | res = ((mtime_t)ts.tv_sec * (mtime_t)1000000) |
|---|
| 133 | 153 | + (mtime_t)(ts.tv_nsec / 1000); |
|---|
| 134 | 154 | |
|---|
| 135 | 155 | #elif defined( HAVE_KERNEL_OS_H ) |
|---|
| 136 | | return( real_time_clock_usecs() ); |
|---|
| | 156 | res = real_time_clock_usecs(); |
|---|
| 137 | 157 | |
|---|
| 138 | 158 | #elif defined( WIN32 ) || defined( UNDER_CE ) |
|---|
| … | … | |
| 176 | 196 | lldiv_t d = lldiv (counter.QuadPart, freq); |
|---|
| 177 | 197 | |
|---|
| 178 | | return (d.quot * 1000000) |
|---|
| 179 | | + ((d.rem * 1000000) / freq); |
|---|
| | 198 | res = (d.quot * 1000000) + ((d.rem * 1000000) / freq); |
|---|
| 180 | 199 | } |
|---|
| 181 | 200 | else |
|---|
| … | … | |
| 189 | 208 | static mtime_t i_previous_time = I64C(-1); |
|---|
| 190 | 209 | static int i_wrap_counts = -1; |
|---|
| 191 | | mtime_t usec_time; |
|---|
| 192 | 210 | |
|---|
| 193 | 211 | if( i_wrap_counts == -1 ) |
|---|
| … | … | |
| 200 | 218 | |
|---|
| 201 | 219 | EnterCriticalSection( &date_lock ); |
|---|
| 202 | | usec_time = I64C(1000) * |
|---|
| | 220 | res = I64C(1000) * |
|---|
| 203 | 221 | (i_wrap_counts * I64C(0x100000000) + GetTickCount()); |
|---|
| 204 | | if( i_previous_time > usec_time ) |
|---|
| | 222 | if( i_previous_time > res ) |
|---|
| 205 | 223 | { |
|---|
| 206 | 224 | /* Counter wrapped */ |
|---|
| … | … | |
| 210 | 228 | i_previous_time = usec_time; |
|---|
| 211 | 229 | LeaveCriticalSection( &date_lock ); |
|---|
| 212 | | |
|---|
| 213 | | return usec_time; |
|---|
| 214 | 230 | } |
|---|
| 215 | 231 | #else |
|---|
| … | … | |
| 218 | 234 | /* gettimeofday() cannot fail given &tv_date is a valid address */ |
|---|
| 219 | 235 | (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; |
|---|
| 222 | 240 | } |
|---|
| 223 | 241 | |
|---|
| … | … | |
| 232 | 250 | void mwait( mtime_t date ) |
|---|
| 233 | 251 | { |
|---|
| | 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 | |
|---|
| 234 | 260 | #if 0 && defined (HAVE_CLOCK_NANOSLEEP) |
|---|
| 235 | 261 | lldiv_t d = lldiv( date, 1000000 ); |
|---|
| … | … | |
| 257 | 283 | void msleep( mtime_t delay ) |
|---|
| 258 | 284 | { |
|---|
| | 285 | mtime_t earlier = cached_time; |
|---|
| | 286 | |
|---|
| 259 | 287 | #if defined( HAVE_CLOCK_NANOSLEEP ) |
|---|
| 260 | 288 | lldiv_t d = lldiv( delay, 1000000 ); |
|---|
| … | … | |
| 298 | 326 | select( 0, NULL, NULL, NULL, &tv_delay ); |
|---|
| 299 | 327 | #endif |
|---|
| | 328 | |
|---|
| | 329 | earlier += delay; |
|---|
| | 330 | if( cached_time < earlier ) |
|---|
| | 331 | cached_time = earlier; |
|---|
| 300 | 332 | } |
|---|
| 301 | 333 | |
|---|