]> git.stg.codes - stg.git/blob - stargazer/stg_timer.cpp
Port to CMake, get rid of os_int.h.
[stg.git] / stargazer / stg_timer.cpp
1 #include "stg_timer.h"
2
3 #include "stg/common.h"
4
5 #include <ctime>
6 #include <cstring>
7 #include <csignal>
8
9 #include <pthread.h>
10
11 void * StgTimer(void *);
12
13 static int nonstop;
14 static pthread_t thrStgTimer;
15 static bool isTimerRunning = false;
16 volatile time_t stgTime;
17
18 #ifdef STG_TIMER_DEBUG
19 const int TIME_SPEED = 1;
20 /*
21  1  - 1x  speed
22  2  - 2x  speed
23  5  - 5x  speed
24  10 - 10x speed
25  */
26
27 const int START_TIME = 2;
28 /*
29  0 - as is
30  1 - start before new day (3 min before)   29.11.2005 23:57:00
31  2 - start before new month (3 min before) 30.11.2005 23:57:00
32  */
33 #endif
34
35 //-----------------------------------------------------------------------------
36 void * StgTimer(void *)
37 {
38 #ifdef STG_TIMER_DEBUG
39 struct tm lt;
40 memset(&lt, 0, sizeof(lt));
41
42 lt.tm_year = 2016 - 1900; // 2005
43 lt.tm_mon  = 7 - 1;      // Nov
44 lt.tm_hour = 23;          // 23 h
45 lt.tm_min = 57;           // 50 min
46 lt.tm_sec = 0;            // 00 sec
47 lt.tm_isdst = -1;
48
49 switch (START_TIME)
50     {
51     case 0:
52         stgTime = time(NULL);
53         break;
54
55     case 1:
56         lt.tm_mday = 29;
57         stgTime = mktime(&lt);
58         break;
59
60     case 2:
61         lt.tm_mday = 31;
62         stgTime = mktime(&lt);
63         break;
64     }
65 #else
66 stgTime = time(NULL);
67 #endif
68
69 sigset_t signalSet;
70 sigfillset(&signalSet);
71 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
72
73 nonstop = 1;
74 isTimerRunning = true;
75 while (nonstop)
76     {
77     #ifdef STG_TIMER_DEBUG
78     struct timespec ts;
79     if (TIME_SPEED == 1)
80         {
81         ts.tv_sec = 1;
82         ts.tv_nsec = 0;
83         }
84     else
85         {
86         ts.tv_sec = 0;
87         ts.tv_nsec = 1000000000 / TIME_SPEED;
88         }
89     nanosleep(&ts, NULL);
90     stgTime++;
91     #else
92     struct timespec ts = {0, 500000000};
93     nanosleep(&ts, NULL);
94     stgTime = time(NULL);
95     #endif
96     }
97 isTimerRunning = false;
98
99 return NULL;
100 }
101 //-----------------------------------------------------------------------------
102 int RunStgTimer()
103 {
104 static int a = 0;
105 isTimerRunning = false;
106
107 if (a == 0)
108     if (pthread_create(&thrStgTimer, NULL, &StgTimer, NULL))
109         {
110         isTimerRunning = false;
111         return -1;
112         }
113
114 a = 1;
115 return 0;
116 }
117 //-----------------------------------------------------------------------------
118 void StopStgTimer()
119 {
120 nonstop = 0;
121 pthread_join(thrStgTimer, NULL); // Cleanup thread resources
122 printfd(__FILE__, "STG_TIMER stopped\n");
123 }
124 //-----------------------------------------------------------------------------
125 bool IsStgTimerRunning()
126 {
127 return isTimerRunning;
128 }
129 //-----------------------------------------------------------------------------
130 int stgUsleep(unsigned long t)
131 {
132 #ifdef STG_TIMER_DEBUG
133 struct timespec ts = {static_cast<time_t>((t / TIME_SPEED) / 1000000), static_cast<long>(((t / TIME_SPEED) % 1000000) * 1000)};
134 return nanosleep(&ts, NULL);
135 #else
136 struct timespec ts = {static_cast<time_t>(t / 1000000), static_cast<long>((t % 1000000) * 1000)};
137 return nanosleep(&ts, NULL);
138 #endif
139 }
140 //-----------------------------------------------------------------------------
141 void WaitTimer()
142 {
143     for (int i = 0; i < 5 && !isTimerRunning; i++)
144         stgUsleep(200000);
145 }
146 //-----------------------------------------------------------------------------