Changeset 2845
- Timestamp:
- 12/01/2011 00:09:16 (17 months ago)
- Location:
- box/trunk/lib/common
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
box/trunk/lib/common/BoxTime.cpp
r2482 r2845 95 95 } 96 96 97 // -------------------------------------------------------------------------- 98 // 99 // Function 100 // Name: ShortSleep(box_time_t duration) 101 // Purpose: Sleeps for the specified duration as accurately 102 // and efficiently as possible. 103 // Created: 2011/01/11 104 // 105 // -------------------------------------------------------------------------- 106 107 void ShortSleep(box_time_t duration, bool logDuration) 108 { 109 if(logDuration) 110 { 111 BOX_TRACE("Sleeping for " << BoxTimeToMicroSeconds(duration) << 112 " microseconds"); 113 } 114 115 #ifdef WIN32 116 Sleep(BoxTimeToMilliSeconds(duration)); 117 #else 118 struct timespec ts; 119 memset(&ts, 0, sizeof(ts)); 120 ts.tv_sec = duration / MICRO_SEC_IN_SEC; 121 ts.tv_nsec = duration % MICRO_SEC_IN_SEC; 122 123 while (nanosleep(&ts, &ts) == -1 && errno == EINTR) 124 { 125 // FIXME evil hack for OSX, where ts.tv_sec contains 126 // a negative number interpreted as unsigned 32-bit 127 // when nanosleep() returns later than expected. 128 129 int32_t secs = (int32_t) ts.tv_sec; 130 int64_t remain_ns = (secs * 1000000000) + ts.tv_nsec; 131 132 if (remain_ns < 0) 133 { 134 BOX_WARNING("nanosleep interrupted " << 135 ((float)(0 - remain_ns) / 1000000000) << 136 " secs late"); 137 return; 138 } 139 140 BOX_TRACE("nanosleep interrupted with " << 141 (remain_ns / 1000000000) << " secs remaining, " 142 "sleeping again"); 143 } 144 #endif 145 } 146 -
box/trunk/lib/common/BoxTime.h
r2482 r2845 19 19 #define MICRO_SEC_IN_SEC (1000000) 20 20 #define MICRO_SEC_IN_SEC_LL (1000000LL) 21 #define MILLI_SEC_IN_ NANO_SEC (1000)22 #define MILLI_SEC_IN_ NANO_SEC_LL (1000LL)21 #define MILLI_SEC_IN_SEC (1000) 22 #define MILLI_SEC_IN_SEC_LL (1000LL) 23 23 24 24 box_time_t GetCurrentBoxTime(); … … 34 34 inline uint64_t BoxTimeToMilliSeconds(box_time_t Time) 35 35 { 36 return Time / MILLI_SEC_IN_ NANO_SEC_LL;36 return Time / MILLI_SEC_IN_SEC_LL; 37 37 } 38 38 inline uint64_t BoxTimeToMicroSeconds(box_time_t Time) … … 44 44 bool showMicros = false); 45 45 46 void ShortSleep(box_time_t duration, bool logDuration); 47 46 48 #endif // BOXTIME__H -
box/trunk/lib/common/Test.cpp
r2677 r2845 22 22 #endif 23 23 24 #include "BoxTime.h" 24 25 #include "Test.h" 25 26 … … 452 453 void safe_sleep(int seconds) 453 454 { 454 BOX_TRACE("sleeping for " << seconds << " seconds"); 455 456 #ifdef WIN32 457 Sleep(seconds * 1000); 458 #else 459 struct timespec ts; 460 memset(&ts, 0, sizeof(ts)); 461 ts.tv_sec = seconds; 462 ts.tv_nsec = 0; 463 while (nanosleep(&ts, &ts) == -1 && errno == EINTR) 464 { 465 // FIXME evil hack for OSX, where ts.tv_sec contains 466 // a negative number interpreted as unsigned 32-bit 467 // when nanosleep() returns later than expected. 468 469 int32_t secs = (int32_t) ts.tv_sec; 470 int64_t remain_ns = (secs * 1000000000) + ts.tv_nsec; 471 472 if (remain_ns < 0) 473 { 474 BOX_WARNING("nanosleep interrupted " << 475 ((float)(0 - remain_ns) / 1000000000) << 476 " secs late"); 477 return; 478 } 479 480 BOX_TRACE("nanosleep interrupted with " << 481 (remain_ns / 1000000000) << " secs remaining, " 482 "sleeping again"); 483 } 484 #endif 485 } 486 455 ShortSleep(SecondsToBoxTime(seconds), true); 456 } 457
Note: See TracChangeset
for help on using the changeset viewer.
