]> git.stg.codes - stg.git/blob - projects/stargazer/stg_timer.cpp
Merge remote-tracking branch 'origin/stg-2.409' into ticket37
[stg.git] / projects / 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
48 switch (START_TIME)
49     {
50     case 0:
51         stgTime = time(NULL);
52         break;
53
54     case 1:
55         lt.tm_mday = 29;
56         stgTime = mktime(&lt);
57         break;
58
59     case 2:
60         lt.tm_mday = 31;
61         stgTime = mktime(&lt);
62         break;
63     }
64 #else
65 stgTime = time(NULL);
66 #endif
67
68 sigset_t signalSet;
69 sigfillset(&signalSet);
70 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
71
72 nonstop = 1;
73 isTimerRunning = true;
74 while (nonstop)
75     {
76     #ifdef STG_TIMER_DEBUG
77     struct timespec ts;
78     if (TIME_SPEED == 1)
79         {
80         ts.tv_sec = 1;
81         ts.tv_nsec = 0;
82         }
83     else
84         {
85         ts.tv_sec = 0;
86         ts.tv_nsec = 1000000000 / TIME_SPEED;
87         }
88     nanosleep(&ts, NULL);
89     stgTime++;
90     #else
91     struct timespec ts = {0, 500000000};
92     nanosleep(&ts, NULL);
93     stgTime = time(NULL);
94     #endif
95     }
96 isTimerRunning = false;
97
98 return NULL;
99 }
100 //-----------------------------------------------------------------------------
101 int RunStgTimer()
102 {
103 static int a = 0;
104 isTimerRunning = false;
105
106 if (a == 0)
107     if (pthread_create(&thrStgTimer, NULL, &StgTimer, NULL))
108         {
109         isTimerRunning = false;
110         return -1;
111         }
112
113 a = 1;
114 return 0;
115 }
116 //-----------------------------------------------------------------------------
117 void StopStgTimer()
118 {
119 nonstop = 0;
120 pthread_join(thrStgTimer, NULL); // Cleanup thread resources
121 printfd(__FILE__, "STG_TIMER stopped\n");
122 }
123 //-----------------------------------------------------------------------------
124 bool IsStgTimerRunning()
125 {
126 return isTimerRunning;
127 }
128 //-----------------------------------------------------------------------------
129 int stgUsleep(unsigned long t)
130 {
131 #ifdef STG_TIMER_DEBUG
132 struct timespec ts = {static_cast<time_t>((t / TIME_SPEED) / 1000000), static_cast<long>(((t / TIME_SPEED) % 1000000) * 1000)};
133 return nanosleep(&ts, NULL);
134 #else
135 struct timespec ts = {static_cast<time_t>(t / 1000000), static_cast<long>((t % 1000000) * 1000)};
136 return nanosleep(&ts, NULL);
137 #endif
138 }
139 //-----------------------------------------------------------------------------
140 void WaitTimer()
141 {
142     for (int i = 0; i < 5 && !isTimerRunning; i++)
143         stgUsleep(200000);
144 }
145 //-----------------------------------------------------------------------------