00001 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*- 00002 00003 // -- BEGIN LICENSE BLOCK ---------------------------------------------- 00004 // This file is part of FZIs ic_workspace. 00005 // 00006 // This program is free software licensed under the LGPL 00007 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3). 00008 // You can find a copy of this license in LICENSE folder in the top 00009 // directory of the source code. 00010 // 00011 // © Copyright 2014 FZI Forschungszentrum Informatik, Karlsruhe, Germany 00012 // 00013 // -- END LICENSE BLOCK ------------------------------------------------ 00014 00015 //---------------------------------------------------------------------- 00022 //---------------------------------------------------------------------- 00023 #include <sys/types.h> 00024 #include <sys/timeb.h> 00025 00026 #include "icl_core/TimeSpan.h" 00027 #include "icl_core/TimeStamp.h" 00028 #include "icl_core/os_win32_time.h" 00029 00030 namespace icl_core { 00031 namespace os { 00032 namespace hidden_win32 { 00033 00034 void gettimeofday(struct timespec *time) 00035 { 00036 struct _timeb tod; 00037 _ftime_s(&tod); 00038 //Care: _ftime resolves in millisec 00039 time->tv_sec = time_t(tod.time); 00040 time->tv_nsec = tod.millitm*1000000; 00041 } 00042 00043 int nanosleep(const struct timespec *rqtp, struct timespec *rmtp) 00044 { 00045 icl_core::TimeSpan wait_time(*rqtp); 00046 icl_core::TimeStamp start_time = icl_core::TimeStamp(); 00047 00048 DWORD sleeptime_ms = DWORD(1000*rqtp->tv_sec + rqtp->tv_nsec/1000000); 00049 Sleep(sleeptime_ms); 00050 icl_core::TimeSpan sleep_time = ::icl_core::TimeStamp() - start_time; 00051 00052 // 1ms deviation is ok! 00053 if (sleep_time + icl_core::TimeSpan(0, 1000000) >= wait_time) 00054 { 00055 if (rmtp != 0) 00056 { 00057 rmtp->tv_sec = 0; 00058 rmtp->tv_nsec = 0; 00059 } 00060 return 0; 00061 } 00062 else 00063 { 00064 if (rmtp != 0) 00065 { 00066 icl_core::TimeSpan remaining_sleep_time = wait_time - sleep_time; 00067 rmtp->tv_sec = remaining_sleep_time.tsSec(); 00068 rmtp->tv_nsec = remaining_sleep_time.tsNSec(); 00069 } 00070 return -1; 00071 } 00072 } 00073 00074 unsigned int sleep(unsigned int seconds) 00075 { 00076 ::icl_core::TimeStamp start_time = icl_core::TimeStamp(); 00077 00078 ::Sleep(DWORD(1000*seconds)); 00079 ::icl_core::TimeSpan sleep_time = ::icl_core::TimeStamp() - start_time; 00080 00081 if (sleep_time.tsSec() >= seconds) 00082 { 00083 return 0; 00084 } 00085 else 00086 { 00087 return static_cast<unsigned int>(sleep_time.tsSec() - seconds); 00088 } 00089 } 00090 00091 int usleep(unsigned long useconds) 00092 { 00093 ::icl_core::TimeStamp start_time; 00094 00095 ::Sleep(DWORD(useconds/1000)); 00096 ::icl_core::TimeSpan sleep_time = ::icl_core::TimeStamp() - start_time; 00097 00098 // 1ms deviation is ok! 00099 if (sleep_time.toUSec() + 1000 >= useconds) 00100 { 00101 return 0; 00102 } 00103 else 00104 { 00105 return -1; 00106 } 00107 } 00108 00109 00110 } 00111 } 00112 }