Pol  Revision:cb584c9
polclock.cpp
Go to the documentation of this file.
1 
7 #include "polclock.h"
8 
9 #include <atomic>
10 
11 #ifdef _WIN32
12 #define WIN32_LEAN_AND_MEAN
13 #include <windows.h>
14 #else
15 #include <sys/time.h>
16 #include <unistd.h>
17 #endif
18 
19 #include "globals/state.h"
20 
21 namespace Pol
22 {
23 namespace Core
24 {
25 #ifdef _WIN32
26 typedef clock_t polclock_base_type;
27 #else
28 typedef int polclock_base_type;
29 #endif
30 static std::atomic<polclock_base_type> polclock_base( 0 );
31 static std::atomic<time_t> poltime_base( 0 );
32 static std::atomic<time_t> poltime_paused_at( 0 );
33 
34 polclock_base_type getCurrentPolClockSeconds()
35 {
36 #ifdef _WIN32
37  return clock();
38 #else
39  struct timeval tmv;
40  struct timezone tz;
41  gettimeofday( &tmv, &tz );
42  return tmv.tv_sec;
43 #endif
44 }
45 
46 void pol_sleep_ms( unsigned int millis )
47 {
48 #ifdef _WIN32
49  Sleep( millis );
50 #else
51  usleep( millis * 1000L );
52 #endif
53 }
54 
56 {
58 }
59 
61 {
63 }
64 
66 {
67  polclock_base_type polclock_diff = getCurrentPolClockSeconds() - stateManager.polclock_paused_at;
68  polclock_base += polclock_diff;
70 }
71 
73 {
74 #ifdef _WIN32
75 #ifndef POLCLOCK_STRETCH
76  return ( clock() - polclock_base ) / POLCLOCK_DIV;
77 #else
78  return ( clock() - polclock_base ) / ( POLCLOCK_DIV * POLCLOCK_STRETCH );
79 #endif
80 #else
81  struct timeval tmv;
82  struct timezone tz;
83  gettimeofday( &tmv, &tz );
84  return ( tmv.tv_sec - polclock_base ) * 100 + tmv.tv_usec / ( 1000L * 10L );
85 #endif
86 }
87 
89 {
90  poltime_base = time( nullptr );
91 }
93 {
94  poltime_paused_at = time( nullptr );
95 }
97 {
98  time_t poltime_diff = time( nullptr ) - poltime_paused_at;
99  poltime_base += poltime_diff;
100 }
101 
102 time_t poltime()
103 {
104 #ifndef POLCLOCK_STRETCH
105  return time( nullptr ) - poltime_base;
106 #else
107  return ( time( nullptr ) - poltime_base ) / POLCLOCK_STRETCH;
108 #endif
109 }
110 
112 {
113  static bool inited = false;
114  if ( !inited )
115  {
116  inited = true;
117  start_polclock();
118  start_poltime();
119  }
120 }
122 {
123  pause_polclock();
124  pause_poltime();
125 }
127 {
129  restart_poltime();
130 }
131 }
132 }
void pol_sleep_ms(unsigned int millis)
Definition: polclock.cpp:46
void start_polclock()
Definition: polclock.cpp:55
const unsigned POLCLOCK_DIV
Definition: polclock.h:30
void pause_poltime()
Definition: polclock.cpp:92
polclock_t polclock()
Definition: polclock.cpp:72
static std::atomic< polclock_base_type > polclock_base(0)
void restart_poltime()
Definition: polclock.cpp:96
void start_pol_clocks()
Definition: polclock.cpp:111
void start_poltime()
Definition: polclock.cpp:88
int polclock_base_type
Definition: polclock.cpp:28
void restart_polclock()
Definition: polclock.cpp:65
static std::atomic< time_t > poltime_base(0)
int polclock_t
Definition: polclock.h:26
time_t poltime()
Definition: polclock.cpp:102
std::atomic< clock_t > polclock_paused_at
Definition: state.h:42
StateManager stateManager
Definition: state.cpp:8
void pause_polclock()
Definition: polclock.cpp:60
static std::atomic< time_t > poltime_paused_at(0)
Definition: berror.cpp:12
void restart_pol_clocks()
Definition: polclock.cpp:126
void pause_pol_clocks()
Definition: polclock.cpp:121
polclock_base_type getCurrentPolClockSeconds()
Definition: polclock.cpp:34