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