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