]> git.stg.codes - stg.git/blob - projects/stargazer/stg_timer.cpp
Another set of minor fixes.
[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 = 0;
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 = 2007 - 1900; // 2005
43 lt.tm_mon  = 11 - 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 = 30;
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 = {0, 1000000000 / TIME_SPEED};
78     nanosleep(&ts, NULL);
79     stgTime++;
80     #else
81     struct timespec ts = {0, 500000000};
82     nanosleep(&ts, NULL);
83     stgTime = time(NULL);
84     #endif
85     }
86 isTimerRunning = false;
87
88 return NULL;
89 }
90 //-----------------------------------------------------------------------------
91 int RunStgTimer()
92 {
93 static int a = 0;
94 isTimerRunning = false;
95
96 if (a == 0)
97     if (pthread_create(&thrStgTimer, NULL, &StgTimer, NULL))
98         {
99         isTimerRunning = false;
100         return -1;
101         }
102
103 a = 1;
104 return 0;
105 }
106 //-----------------------------------------------------------------------------
107 void StopStgTimer()
108 {
109 nonstop = 0;
110 pthread_join(thrStgTimer, NULL); // Cleanup thread resources
111 printfd(__FILE__, "STG_TIMER stopped\n");
112 }
113 //-----------------------------------------------------------------------------
114 bool IsStgTimerRunning()
115 {
116 return isTimerRunning;
117 }
118 //-----------------------------------------------------------------------------
119 int stgUsleep(unsigned long t)
120 {
121 #ifdef STG_TIMER_DEBUG
122 struct timespec ts = {static_cast<time_t>((t / TIME_SPEED) / 1000000), static_cast<long>(((t / TIME_SPEED) % 1000000) * 1000)};
123 return nanosleep(&ts, NULL);
124 #else
125 struct timespec ts = {static_cast<time_t>(t / 1000000), static_cast<long>((t % 1000000) * 1000)};
126 return nanosleep(&ts, NULL);
127 #endif
128 }
129 //-----------------------------------------------------------------------------
130 void WaitTimer()
131 {
132     for (int i = 0; i < 5 && !isTimerRunning; i++)
133         stgUsleep(200000);
134 }
135 //-----------------------------------------------------------------------------