os_win32_time.cpp
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 // This file is part of FZIs ic_workspace.
5 //
6 // This program is free software licensed under the LGPL
7 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
8 // You can find a copy of this license in LICENSE folder in the top
9 // directory of the source code.
10 //
11 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
12 //
13 // -- END LICENSE BLOCK ------------------------------------------------
14 
15 //----------------------------------------------------------------------
22 //----------------------------------------------------------------------
23 #include <sys/types.h>
24 #include <sys/timeb.h>
25 
26 #include "icl_core/TimeSpan.h"
27 #include "icl_core/TimeStamp.h"
28 #include "icl_core/os_win32_time.h"
29 
30 namespace icl_core {
31 namespace os {
32 namespace hidden_win32 {
33 
34 void gettimeofday(struct timespec *time)
35 {
36  struct _timeb tod;
37  _ftime_s(&tod);
38  //Care: _ftime resolves in millisec
39  time->tv_sec = time_t(tod.time);
40  time->tv_nsec = tod.millitm*1000000;
41 }
42 
43 int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
44 {
45  icl_core::TimeSpan wait_time(*rqtp);
47 
48  DWORD sleeptime_ms = DWORD(1000*rqtp->tv_sec + rqtp->tv_nsec/1000000);
49  Sleep(sleeptime_ms);
50  icl_core::TimeSpan sleep_time = ::icl_core::TimeStamp() - start_time;
51 
52  // 1ms deviation is ok!
53  if (sleep_time + icl_core::TimeSpan(0, 1000000) >= wait_time)
54  {
55  if (rmtp != 0)
56  {
57  rmtp->tv_sec = 0;
58  rmtp->tv_nsec = 0;
59  }
60  return 0;
61  }
62  else
63  {
64  if (rmtp != 0)
65  {
66  icl_core::TimeSpan remaining_sleep_time = wait_time - sleep_time;
67  rmtp->tv_sec = remaining_sleep_time.tsSec();
68  rmtp->tv_nsec = remaining_sleep_time.tsNSec();
69  }
70  return -1;
71  }
72 }
73 
74 unsigned int sleep(unsigned int seconds)
75 {
77 
78  ::Sleep(DWORD(1000*seconds));
79  ::icl_core::TimeSpan sleep_time = ::icl_core::TimeStamp() - start_time;
80 
81  if (sleep_time.tsSec() >= seconds)
82  {
83  return 0;
84  }
85  else
86  {
87  return static_cast<unsigned int>(sleep_time.tsSec() - seconds);
88  }
89 }
90 
91 int usleep(unsigned long useconds)
92 {
93  ::icl_core::TimeStamp start_time;
94 
95  ::Sleep(DWORD(useconds/1000));
96  ::icl_core::TimeSpan sleep_time = ::icl_core::TimeStamp() - start_time;
97 
98  // 1ms deviation is ok!
99  if (sleep_time.toUSec() + 1000 >= useconds)
100  {
101  return 0;
102  }
103  else
104  {
105  return -1;
106  }
107 }
108 
109 
110 }
111 }
112 }
Represents absolute times.
Definition: TimeStamp.h:61
unsigned int sleep(unsigned int seconds)
time_t tv_sec
Seconds.
Definition: os_win32_time.h:36
Win32 implementation of the global functions for time manipulation,.
int32_t tsNSec() const
Definition: TimeSpan.cpp:153
long tv_nsec
Nanoseconds.
Definition: os_win32_time.h:38
int usleep(unsigned long useconds)
int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
Repesents absolute times.
Definition: TimeSpan.h:46
int64_t tsSec() const
Definition: TimeSpan.cpp:148
void gettimeofday(struct timespec *time)
Contains TimeStamp.
int64_t toUSec() const
May result in an overflow if seconds are too large.
Definition: TimeSpan.cpp:168


fzi_icl_core
Author(s):
autogenerated on Mon Jun 10 2019 13:17:58