iFun Engine API  1.0.0-b6053
Great Technology for Great Games
monotonic_clock.h
Go to the documentation of this file.
1 // Copyright (C) 2013-2020 iFunFactory Inc. All Rights Reserved.
2 //
3 // This work is confidential and proprietary to iFunFactory Inc. and
4 // must not be used, disclosed, copied, or distributed without the prior
5 // consent of iFunFactory Inc.
6 
9 #ifndef INCLUDE_FUNAPI_TIME_MONOTONIC_CLOCK_H_
10 #define INCLUDE_FUNAPI_TIME_MONOTONIC_CLOCK_H_
11 
12 #ifndef _WIN32
13 
14 #include <stdint.h>
15 #include <time.h>
16 
17 #include <funapi/types.h>
18 
19 
20 namespace fun {
21 
24 
25 class FUNAPI_DLL_VISIBILITY MonotonicClock {
26  public:
28  typedef int64_t Value;
29 
31  typedef int64_t Duration;
32 
34  static void ToTimespec(const Value &value, struct timespec *ret) {
35  ret->tv_sec = value / (1000 * 1000);
36  ret->tv_nsec = value % (1000 * 1000) * 1000;
37  }
38 
40  static Duration FromSec(int64_t sec) {
41  return sec * 1000 * 1000;
42  }
43 
45  static Duration FromMsec(int64_t msec) {
46  return msec * 1000;
47  }
48 
50  static Duration FromUsec(int64_t usec) {
51  return usec;
52  }
53 
57  static Value Now() {
58  struct timespec now;
59  clock_gettime(CLOCK_MONOTONIC, &now);
60  return now.tv_sec * 1000 * 1000 + now.tv_nsec / 1000;
61  }
62 
63  private:
64  MonotonicClock() { }
65 };
66 
67 } // namespace fun
68 
69 #else // _WIN32
70 
71 #include <funapi/types.h>
72 
73 #include <cstddef>
74 #include <cstdint>
75 #include <ctime>
76 
77 
78 namespace fun {
79 
80 class FUNAPI_DLL_VISIBILITY MonotonicClock {
81  public:
83  typedef LONGLONG Value;
84 
86  typedef LONGLONG Duration;
87 
89  static void ToTimespec(const Value &value, struct timespec *ret) {
90  ret->tv_sec = value / (1000 * 1000);
91  ret->tv_nsec = value % (1000 * 1000) * 1000;
92  }
93 
95  static Duration FromSec(int64_t sec) {
96  return sec * 1000 * 1000;
97  }
98 
100  static Duration FromMsec(int64_t msec) {
101  return msec * 1000;
102  }
103 
105  static Duration FromUsec(int64_t usec) {
106  return usec;
107  }
108 
112  static Value Now() {
113  static bool init = false;
114  static LARGE_INTEGER frequency; // frequency.QuadPart 는 ticks per second 를 포함한다.
115  static LARGE_INTEGER epoch;
116 
117  if (!init) {
118  init = true;
119  QueryPerformanceFrequency(&frequency);
120  QueryPerformanceCounter(&epoch);
121  }
122 
123  LARGE_INTEGER now;
124  QueryPerformanceCounter(&now);
125 
126  LARGE_INTEGER diff;
127  diff.QuadPart = now.QuadPart - epoch.QuadPart;
128 
129  // 아래 작업은 tick 에 곱하는 작업을 먼저하고 tick 을 sec 으로 바꾸는 것을 나중에 한다.
130  // 이는 precision 을 잃지 않기 위해서이다.
131  diff.QuadPart *= 1000 * 1000; // usec 으로 바꾸기 위해서 tick 값을 조정한다.
132  diff.QuadPart /= frequency.QuadPart;
133 
134  return diff.QuadPart;
135  }
136 
137  private:
138  MonotonicClock() { }
139 };
140 
141 } // namespace fun
142 
143 #endif // _WIN32
144 
145 #endif // INCLUDE_FUNAPI_TIME_MONOTONIC_CLOCK_H_
static Duration FromSec(int64_t sec)
Converts sec value into Duration.
Definition: monotonic_clock.h:40
static Duration FromMsec(int64_t msec)
Converts msec value into Duration.
Definition: monotonic_clock.h:45
static Duration FromUsec(int64_t usec)
Converts usec value into Duration.
Definition: monotonic_clock.h:50
static void ToTimespec(const Value &value, struct timespec *ret)
Converts the given Monotonic clock value into timespec.
Definition: monotonic_clock.h:34
Definition: json.h:18
Funapi uses MonotonicClock to be overcome clock drift.
Definition: monotonic_clock.h:25
int64_t Duration
duration value in usec.
Definition: monotonic_clock.h:31
static Value Now()
Returns the current Monotonic value.
Definition: monotonic_clock.h:57
int64_t Value
clock value in usec.
Definition: monotonic_clock.h:28