]> git.stg.codes - stg.git/blob - projects/stargazer/stg_timer.cpp
Change some usleep() to nanosleep() `cause usleep() is obsolete since
[stg.git] / projects / stargazer / stg_timer.cpp
1 #include <pthread.h>
2
3 #include <ctime>
4 #include <cstring>
5
6 #include "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 #endif
58
59 nonstop = 1;
60 isTimerRunning = true;
61 while (nonstop)
62     {
63     #ifdef STG_TIMER_DEBUG
64     struct timespec ts = {0, 1000000000 / TIME_SPEED};
65     nanosleep(&ts);
66     //usleep(1000000 / TIME_SPEED);
67     stgTime++;
68     #else
69     struct timespec ts = {0, 500000000};
70     nanosleep(&ts);
71     //usleep(500000);
72     stgTime = time(NULL);
73     #endif
74     }
75 isTimerRunning = false;
76
77 return NULL;
78 }
79 //-----------------------------------------------------------------------------
80 int RunStgTimer()
81 {
82 static int a = 0;
83 isTimerRunning = false;
84
85 if (a == 0)
86     if (pthread_create(&thrStgTimer, NULL, StgTimer, NULL))
87         {
88         isTimerRunning = false;
89         return -1;
90         }
91
92 a = 1;
93 return 0;
94 }
95 //-----------------------------------------------------------------------------
96 void StopStgTimer()
97 {
98 nonstop = 0;
99 pthread_join(thrStgTimer, NULL); // Cleanup thread resources
100 printfd(__FILE__, "STG_TIMER stopped\n");
101 }
102 //-----------------------------------------------------------------------------
103 bool IsStgTimerRunning()
104 {
105 return isTimerRunning;
106 }
107 //-----------------------------------------------------------------------------
108 int stgUsleep(unsigned long t)
109 {
110 #ifdef STG_TIMER_DEBUG
111 struct timespec ts = {(t / TIME_SPEED) / 1000000, ((t / TIME_SPEED) % 1000000) * 1000};
112 return nanosleep(&ts);
113 //return usleep(t / TIME_SPEED);
114 #else
115 struct timespec ts = {t / 1000000, (t % 1000000) * 1000};
116 return nanosleep(&ts);
117 //return usleep(t);
118 #endif
119 }
120 //-----------------------------------------------------------------------------
121 void WaitTimer()
122 {
123     for (int i = 0; i < 5 && !isTimerRunning; i++)
124         stgUsleep(200000);
125 }
126 //-----------------------------------------------------------------------------