Changeset 2845


Ignore:
Timestamp:
12/01/2011 00:09:16 (17 months ago)
Author:
chris
Message:

Move accurate sleep code from Test.cpp to BoxTime?, allow requesting times
in microseconds with ShortSleep?(), make safe_sleep() use it.

Rename MILLI_SEC_IN_NANO_SEC to MILLI_SEC_IN_SEC which is what it actually is.

Location:
box/trunk/lib/common
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • box/trunk/lib/common/BoxTime.cpp

    r2482 r2845  
    9595} 
    9696 
     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 
     107void 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  
    1919#define MICRO_SEC_IN_SEC        (1000000) 
    2020#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) 
    2323 
    2424box_time_t GetCurrentBoxTime(); 
     
    3434inline uint64_t BoxTimeToMilliSeconds(box_time_t Time) 
    3535{ 
    36         return Time / MILLI_SEC_IN_NANO_SEC_LL; 
     36        return Time / MILLI_SEC_IN_SEC_LL; 
    3737} 
    3838inline uint64_t BoxTimeToMicroSeconds(box_time_t Time) 
     
    4444        bool showMicros = false); 
    4545 
     46void ShortSleep(box_time_t duration, bool logDuration); 
     47 
    4648#endif // BOXTIME__H 
  • box/trunk/lib/common/Test.cpp

    r2677 r2845  
    2222#endif 
    2323 
     24#include "BoxTime.h" 
    2425#include "Test.h" 
    2526 
     
    452453void safe_sleep(int seconds) 
    453454{ 
    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.