Changeset e2b38c968064a42fc31f82e7a0bb9a6d1d79d531
- 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
| rd1d3dc1 |
re2b38c9 |
|
| 145 | 145 | } |
|---|
| 146 | 146 | |
|---|
| 147 | | /** |
|---|
| 148 | | * Return a value that is no bigger than the clock precision |
|---|
| 149 | | * (possibly zero). |
|---|
| 150 | | */ |
|---|
| 151 | | static inline unsigned mprec( void ) |
|---|
| 152 | | { |
|---|
| 153 | 147 | #if defined (HAVE_CLOCK_NANOSLEEP) |
|---|
| | 148 | static unsigned prec = 0; |
|---|
| | 149 | |
|---|
| | 150 | static void mprec_once( void ) |
|---|
| | 151 | { |
|---|
| 154 | 152 | struct timespec ts; |
|---|
| 155 | 153 | if( clock_getres( CLOCK_MONOTONIC, &ts )) |
|---|
| 156 | 154 | clock_getres( CLOCK_REALTIME, &ts ); |
|---|
| 157 | 155 | |
|---|
| 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 | */ |
|---|
| | 164 | static 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 |
|---|
| 160 | 171 | return 0; |
|---|
| 161 | | } |
|---|
| 162 | | |
|---|
| 163 | | static unsigned prec = 0; |
|---|
| | 172 | #endif |
|---|
| | 173 | } |
|---|
| | 174 | |
|---|
| 164 | 175 | static volatile mtime_t cached_time = 0; |
|---|
| 165 | 176 | |
|---|
| … | … | |
| 316 | 327 | void mwait( mtime_t date ) |
|---|
| 317 | 328 | { |
|---|
| 318 | | if( prec == 0 ) |
|---|
| 319 | | prec = mprec(); |
|---|
| 320 | | |
|---|
| 321 | 329 | /* If the deadline is already elapsed, or within the clock precision, |
|---|
| 322 | 330 | * 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 |
|---|
| 324 | 332 | return; |
|---|
| 325 | 333 | |
|---|