functions_win.cpp
Go to the documentation of this file.
00001 
00008 /*****************************************************************************
00009 ** Includes
00010 *****************************************************************************/
00011 
00012 #include <ecl/config/windows.hpp>
00013 #include <ecl/config/portable_types.hpp>
00014 #include "../../include/ecl/time_lite/functions_win.hpp"
00015 
00016 /*****************************************************************************
00017 ** Macros
00018 *****************************************************************************/
00019 
00020 #if defined(ECL_HAS_WIN_TIMERS)
00021 
00022 /*****************************************************************************
00023 ** Namespaces
00024 *****************************************************************************/
00025 
00026 namespace ecl {
00027 
00028 /*****************************************************************************
00029 ** Notes
00030 *****************************************************************************/
00031 
00032 // This is defined by windows and looks like this:
00033 //typedef union _LARGE_INTEGER {
00034 //      struct {
00035 //              DWORD LowPart;
00036 //              LONG HighPart;
00037 //      };
00038 //      LONGLONG QuadPart;
00039 //} LARGE_INTEGER;
00040 //typedef LARGE_INTEGER TimeStructure; /**< @brief The underlying type used to store time structures. **/
00041 
00042 /*****************************************************************************
00043 ** Private functions
00044 *****************************************************************************/
00045 
00046 static double cpu_frequency() {
00047         static double frequency = 0.0;
00048 
00049         if ( frequency == 0.0 ) { // need to initialise
00050                 LARGE_INTEGER freq;
00051                 QueryPerformanceFrequency(&freq);
00052                 frequency = static_cast<double>(freq.QuadPart);
00053         }
00054         return frequency;
00055 }
00056 
00057 /*****************************************************************************
00058 ** Public functions
00059 *****************************************************************************/
00060 
00061 TimeError epoch_time(TimeStructure &time) {
00062         // Get time
00063     LARGE_INTEGER stamp;
00064     QueryPerformanceCounter(&stamp);
00065         double t = static_cast<double>(stamp.QuadPart)/cpu_frequency();
00066         time.tv_sec = static_cast<long>(t);
00067         time.tv_nsec = ((t - static_cast<double>(time.tv_sec))*1000000000.0);
00068         return TimeError(NoError);
00069 }
00070 
00071 TimeError sleep_until(const TimeStructure &time) {
00072         TimeStructure current_time, sleep_time;
00073 
00074         TimeError error = epoch_time(current_time);
00075         if ( error.flag() != NoError ) { return error; }
00076 
00077         /*********************
00078         ** current > time?
00079         **********************/
00080         if ( current_time.tv_sec > time.tv_sec ) {
00081                 return TimeError(NoError); // return immediately
00082         } else if ( current_time.tv_sec == time.tv_sec ) {
00083                 if ( current_time.tv_nsec > time.tv_nsec ) {
00084                   return TimeError(NoError);
00085                 }
00086         }
00087         sleep_time.tv_sec = time.tv_sec - current_time.tv_sec;
00088         if ( current_time.tv_nsec <= time.tv_nsec ) {
00089                 sleep_time.tv_nsec = time.tv_nsec - current_time.tv_nsec;
00090         } else {
00091                 sleep_time.tv_sec -= 1;
00092                 sleep_time.tv_nsec = 1000000000L - current_time.tv_nsec + time.tv_nsec;
00093         }
00094         return ecl::sleep(sleep_time);
00095 }
00096 
00097 TimeError sleep(const TimeStructure &time) {
00098     HANDLE timer = NULL;
00099     LARGE_INTEGER sleep_time;
00100 
00101     sleep_time.QuadPart = -
00102             static_cast<ecl::uint64>(time.tv_sec)*10000000LL -
00103             static_cast<ecl::uint64>(time.tv_nsec) / 100LL;
00104 
00105       if ( (timer = CreateWaitableTimer(NULL, TRUE, NULL)) == NULL ) {
00106           return TimeError(UnknownError);
00107       }
00108       if (!SetWaitableTimer (timer, &sleep_time, 0, NULL, NULL, 0)) {
00109           return TimeError(UnknownError);
00110       }
00111 
00112       if (WaitForSingleObject (timer, INFINITE) != WAIT_OBJECT_0) {
00113           return TimeError(UnknownError);
00114       }
00115       return TimeError(NoError);
00116 }
00117 
00118 
00119 } // namespace ecl
00120 
00121 #endif


ecl_time_lite
Author(s): Daniel Stonier
autogenerated on Sun Oct 5 2014 23:35:28