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