-
Notifications
You must be signed in to change notification settings - Fork 0
/
Timer.cs
263 lines (216 loc) · 7.96 KB
/
Timer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/* Timer */
//using Vanara.PInvoke;
namespace SharpDune;
enum TimerType
{
TIMER_GUI = 1, /*!< The identifier for GUI timer. */
TIMER_GAME = 2 /*!< The identifier for Game timer. */
}
class TimerNode
{
internal uint usec_left;
internal uint usec_delay;
internal Action callback;
internal bool callonce;
}
static class Timer
{
internal static volatile uint g_timerGUI; /*!< Tick counter. Increases with 1 every tick when Timer 1 is enabled. Used for GUI. */
internal static volatile uint g_timerGame; /*!< Tick counter. Increases with 1 every tick when Timer 2 is enabled. Used for game timing (units, . . .). */
internal static volatile uint g_timerInput; /*!< Tick counter. Increases with 1 every tick. Used for input timing. */
internal static volatile uint g_timerSleep; /*!< Tick counter. Increases with 1 every tick. Used for sleeping. */
internal static volatile uint g_timerTimeout; /*!< Tick counter. Decreases with 1 every tick when non-zero. Used to timeout. */
static volatile int s_timer_count;
static ushort s_timersActive;
static TimerNode[] s_timerNodes;
static int s_timerNodeCount;
static int s_timerNodeSize;
static int s_timerTime;
static uint s_timerLastTime;
const uint s_timerSpeed = 1000000 / 120; /* Our timer runs at 120Hz */
static System.Threading.Timer s_timer;
//static /*HANDLE*/nint s_timerMainThread = nint.Zero;
//static /*HANDLE*/Kernel32.TimerQueueTimerHandle s_timerThread = Kernel32.TimerQueueTimerHandle.NULL;
//readonly static Kernel32.WaitOrTimerCallback Timer_InterruptWindows_Del = Timer_InterruptWindows;
/*
* Sleep for an amount of ticks.
* @param ticks The amount of ticks to sleep.
*/
internal static void Timer_Sleep(ushort ticks)
{
var tick = g_timerSleep + ticks;
while (tick >= g_timerSleep) SleepIdle();
}
/*
* Set timers on and off.
*
* @param timer The timer to switch.
* @param set True sets the timer on, false sets it off.
* @return True if timer was set, false if it was not set.
*/
internal static bool Timer_SetTimer(TimerType timer, bool set)
{
byte t;
bool ret;
t = (byte)(1 << ((byte)timer - 1));
ret = (s_timersActive & t) != 0;
if (set)
{
s_timersActive |= t;
}
else
{
s_timersActive &= (ushort)~t;
}
return ret;
}
/*
* Add a timer.
* @param callback the callback for the timer.
* @param usec_delay The interval of the timer.
*/
internal static void Timer_Add(Action callback, uint usec_delay, bool callonce)
{
TimerNode node;
if (s_timerNodeCount == s_timerNodeSize)
{
s_timerNodeSize += 2;
Array.Resize(ref s_timerNodes, s_timerNodeSize); //s_timerNodes = (TimerNode *)realloc(s_timerNodes, s_timerNodeSize * sizeof(TimerNode));
s_timerNodes[s_timerNodeSize - 2] = new TimerNode();
s_timerNodes[s_timerNodeSize - 1] = new TimerNode();
}
node = s_timerNodes[s_timerNodeCount++];
node.usec_left = usec_delay;
node.usec_delay = usec_delay;
node.callback = callback;
node.callonce = callonce;
}
internal static uint Timer_GetTime() =>
(uint)Environment.TickCount;
/*
* Uninitialize the timer.
*/
internal static void Timer_Uninit()
{
Timer_InterruptSuspend();
//Kernel32.CloseHandle(s_timerMainThread);
s_timerNodes = null; //free(s_timerNodes);
s_timerNodeCount = 0;
s_timerNodeSize = 0;
}
/*
* Suspend the timer interrupt handling.
*/
static void Timer_InterruptSuspend()
{
s_timer?.Change(Timeout.Infinite, Timeout.Infinite);
s_timer?.Dispose();
s_timer = null;
//if (s_timerThread != nint.Zero) Kernel32.DeleteTimerQueueTimer(Kernel32.TimerQueueHandle.NULL, s_timerThread, Kernel32.SafeEventHandle.Null);
//s_timerThread = nint.Zero;
}
/*
* Initialize the timer.
*/
internal static void Timer_Init()
{
s_timerLastTime = Timer_GetTime();
s_timerTime = (int)(s_timerSpeed / 1000);
//Kernel32.DuplicateHandle(Kernel32.GetCurrentProcess(), (nint)Kernel32.GetCurrentThread(), Kernel32.GetCurrentProcess(), out s_timerMainThread, 0, false, Kernel32.DUPLICATE_HANDLE_OPTIONS.DUPLICATE_SAME_ACCESS);
Timer_InterruptResume();
}
/*
* Resume the timer interrupt handling.
*/
static void Timer_InterruptResume() =>
s_timer = new System.Threading.Timer(_ => s_timer_count++, null, s_timerTime, s_timerTime);
//Kernel32.CreateTimerQueueTimer(out s_timerThread, Kernel32.TimerQueueHandle.NULL, Timer_InterruptWindows_Del, nint.Zero, (uint)s_timerTime, (uint)s_timerTime, Kernel32.WT.WT_EXECUTEINTIMERTHREAD);
//static void Timer_InterruptWindows(nint arg, bool TimerOrWaitFired)
//{
// //Kernel32.SuspendThread(s_timerMainThread);
// s_timer_count++;
// //Kernel32.ResumeThread(s_timerMainThread);
//}
internal static void SleepAndProcessBackgroundTasks()
{
while (s_timer_count == 0)
{
Thread.Sleep(2); /* TODO : use a semaphore */
}
/* timer signal SIGALRM has been triggered */
if (s_timer_count > 1)
{
Trace.WriteLine($"WARNING: s_timer_count = {s_timer_count}");
}
s_timer_count = 0;
Timer_InterruptRun(0);
if (s_timer_count > 0)
{
/* one more iteration if SIGALRM has been triggered
* during Timer_InterruptRun() */
s_timer_count = 0;
Timer_InterruptRun(1); /* don't run "callonce" timers */
}
}
static bool timerLock;
/*
* Run the timer interrupt handler.
*/
static void Timer_InterruptRun(int arg)
{
TimerNode node;
uint new_time, usec_delta, delta;
int i;
/* Lock the timer, to avoid double-calls */
if (timerLock) return;
timerLock = true;
/* Calculate the time between calls */
new_time = Timer_GetTime();
usec_delta = (new_time - s_timerLastTime) * 1000;
s_timerLastTime = new_time;
/* Walk all our timers, see which (and how often) it should be triggered */
for (i = 0; i < s_timerNodeCount; i++)
{
node = s_timerNodes[i];
delta = usec_delta;
/* No delay means: as often as possible, but don't worry about it */
if (node.usec_delay == 0)
{
node.callback?.Invoke();
continue;
}
if (node.callonce)
{
if (node.usec_left <= delta)
{
delta -= node.usec_left;
node.usec_left = node.usec_delay;
if (arg == 0) node.callback?.Invoke();
while (node.usec_left <= delta) delta -= node.usec_left;
}
}
else
{
while (node.usec_left <= delta)
{
delta -= node.usec_left;
node.usec_left = node.usec_delay;
node.callback?.Invoke();
}
}
node.usec_left -= delta;
}
timerLock = false;
}
/*
* Handle game timers.
*/
internal static void Timer_Tick()
{
if ((s_timersActive & (ushort)TimerType.TIMER_GUI) != 0) g_timerGUI++;
if ((s_timersActive & (ushort)TimerType.TIMER_GAME) != 0) g_timerGame++;
g_timerInput++;
g_timerSleep++;
if (g_timerTimeout != 0) g_timerTimeout--;
}
}