forked from albertz/openlierox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainLoop.cpp
496 lines (424 loc) · 12.2 KB
/
MainLoop.cpp
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
#include <SDL.h>
#include <setjmp.h>
#include "MainLoop.h"
#include "Mutex.h"
#include "ReadWriteLock.h"
#include "AuxLib.h"
#include "EventQueue.h"
#include "Options.h"
#include "LieroX.h"
#include "Cache.h"
#include "OLXCommand.h"
#include "InputEvents.h"
#include "TaskManager.h"
#include "Timer.h"
#include "DeprecatedGUI/Menu.h"
#include "SkinnedGUI/CGuiSkin.h"
#include "CServer.h"
#include "IRC.h"
#include "CrashHandler.h"
#include "Clipboard.h"
/*
Notes:
The following things need to be run on the main thread,
because of implementation details on SDL & the underlying OS,
so we cannot do much about it:
- OpenGL stuff
- SDL_Render*
- SDL events
So, this is exactly what we do in the main thread, and not much more.
Check via isMainThread(), whether you are in the main thread.
Then, there is the main game thread, where we run the game logic
itself, and also do much of the software pixel drawing, and
most other things.
We call it the gameloop thread, because the game gameloop runs in it.
Check via isGameloopThread(), whether you are in the gameloop thread.
To be able to do the software drawing in the mainloop thread,
and in parallel do the system screen drawing, we need to have two
main screen surface, which are handled by the VideoPostProcessor.
Then gameloop thread draws to the VideoPostProcessor::videoSurface().
The main thread draws the VideoPostProcessor::m_videoBufferSurface
to the screen.
*/
struct VideoHandler {
SDL_mutex* mutex;
SDL_cond* sign;
int framesInQueue;
bool videoModeReady;
VideoHandler() : mutex(NULL), sign(NULL), framesInQueue(0), videoModeReady(true) {
mutex = SDL_CreateMutex();
sign = SDL_CreateCond();
}
~VideoHandler() {
SDL_DestroyMutex(mutex); mutex = NULL;
SDL_DestroyCond(sign); sign = NULL;
}
// Runs on the main thread.
void frame() {
{
ScopedLock lock(mutex);
if(!videoModeReady) return;
SDL_CondBroadcast(sign);
if(framesInQueue > 0) framesInQueue--;
else return;
// This must be done while locked because we don't want a flipBuffers meanwhile.
VideoPostProcessor::process();
}
// This can be done unlocked, because we only access it through the main thread.
VideoPostProcessor::render();
}
// Runs on the main thread.
void doSingleDirectFrame() {
assert(isMainThread());
{
ScopedLock lock(mutex);
VideoPostProcessor::flipBuffers();
VideoPostProcessor::process();
}
VideoPostProcessor::render();
}
// Runs on the main thread.
void setVideoMode() {
bool makeFrame = false;
{
ScopedLock lock(mutex);
SetVideoMode();
videoModeReady = true;
SDL_CondBroadcast(sign);
if(framesInQueue > 0) {
framesInQueue = 0;
makeFrame = true;
// This must be done while locked because we don't want a flipBuffers meanwhile.
VideoPostProcessor::process();
}
}
if(makeFrame)
// This can be done unlocked, because we only access it through the main thread.
VideoPostProcessor::render();
}
// This must not be run on the main thread.
void pushFrame() {
assert(!isMainThread());
// wait for current drawing
ScopedLock lock(mutex);
while(framesInQueue > 0)
SDL_CondWait(sign, mutex);
SDL_Event ev;
ev.type = SDL_USEREVENT;
ev.user.code = UE_DoVideoFrame;
if(SDL_PushEvent(&ev) != 0) {
framesInQueue++;
VideoPostProcessor::flipBuffers();
} else
warnings << "failed to push videoframeevent" << endl;
}
void requestSetVideoMode() {
ScopedLock lock(mutex);
SDL_Event ev;
ev.type = SDL_USEREVENT;
ev.user.code = UE_DoSetVideoMode;
if(SDL_PushEvent(&ev) != 0) {
videoModeReady = false;
while(!videoModeReady)
SDL_CondWait(sign, mutex);
}
else
warnings << "failed to push setvideomode event" << endl;
}
};
static VideoHandler videoHandler;
#ifndef WIN32
sigjmp_buf longJumpBuffer;
#endif
static SDL_Event QuitEventThreadEvent() {
SDL_Event ev;
ev.type = SDL_USEREVENT;
ev.user.code = UE_QuitEventThread;
return ev;
}
struct CoutPrint : PrintOutFct {
void print(const std::string& str) const {
printf("%s", str.c_str());
}
};
void startMainLockDetector() {
if(!tLXOptions->bUseMainLockDetector) return;
// When debugging, DumpAllThreadsCallstack doesn't work that well in many cases
// because the debugger catches SIGUSR2. Also, when debugging, the developer
// maybe want to pause manually. So we don't use the automatic main lock detector.
if(AmIBeingDebugged()) return;
// This checks the game loop thread, if it is working sanely.
struct MainLockDetector : Action {
bool wait(Uint32 time) {
if(!tLX) return false;
AbsTime oldTime = tLX->currentTime;
for(Uint32 t = 0; t < time; t += 100) {
if(!tLX) return false;
if(game.state == Game::S_Quit) return false;
if(oldTime != tLX->currentTime) return true;
SDL_Delay(100);
}
return true;
}
Result handle() {
// We should always have tLX!=NULL here as we uninit it after the thread-shutdown now.
while(game.state != Game::S_Quit) {
AbsTime oldTime = tLX->currentTime;
if(!wait(1000)) return true;
if(!tLX) return true;
if(game.state == Game::S_Quit) return true;
if(IsWaitingForEvent()) continue;
// HINT: Comment that out and you'll find a lot of things in OLX which could be improved.
// WARNING: This is the only code here which could lead to very rare crashes.
//if(!cClient || cClient->getStatus() != NET_PLAYING) continue;
// check if the mainthread is hanging
if(oldTime == tLX->currentTime) {
warnings << "possible lock of game thread detected" << endl;
notes << "current game state: " << game.state << endl;
//OlxWriteCoreDump("mainlock");
//RaiseDebugger();
if(!wait(5*1000)) return true;
if(tLX && game.state != Game::S_Quit && oldTime == tLX->currentTime) {
hints << "Still locked after 5 seconds. Current threads:" << endl;
threadPool->dumpState(stdoutCLI());
DumpAllThreadsCallstack(CoutPrint());
hints << "Free system memory: " << (GetFreeSysMemory() / 1024) << " KB" << endl;
hints << "Cache size: " << (cCache.GetCacheSize() / 1024) << " KB" << endl;
}
else continue;
// pause for a while, don't be so hard
if(!wait(25*1000)) return true;
if(tLX && game.state != Game::S_Quit && oldTime == tLX->currentTime) {
warnings << "we still are locked after 30 seconds" << endl;
DumpAllThreadsCallstack(CoutPrint());
if(tLXOptions && tLXOptions->bFullscreen) {
notes << "we are in fullscreen, going to window mode now" << endl;
tLXOptions->bFullscreen = false;
doSetVideoModeInMainThread();
notes << "setting window mode sucessfull" << endl;
}
}
else continue;
// pause for a while, don't be so hard
if(!wait(25*1000)) return true;
if(tLX && game.state != Game::S_Quit && oldTime == tLX->currentTime) {
errors << "we still are locked after 60 seconds" << endl;
DumpAllThreadsCallstack(CoutPrint());
if(!AmIBeingDebugged()) {
errors << "aborting now" << endl;
abort();
}
}
}
}
return true;
}
};
threadPool->start(new MainLockDetector(), "main lock detector", true);
}
struct MainLoopTask : LoopTask {
enum State {
State_Startup,
State_Loop,
State_Quit
};
State state;
MainLoopTask() : state(State_Startup) {}
Result handle_Startup();
Result handle_Loop();
Result handle_Quit();
Result handleFrame();
};
// Runs on the main thread.
// Returns false on quit.
static bool handleSDLEvent(SDL_Event& ev) {
if(ev.type == SDL_USEREVENT) {
switch(ev.user.code) {
case UE_QuitEventThread:
return false;
case UE_DoVideoFrame:
videoHandler.frame();
return true;
case UE_DoSetVideoMode:
videoHandler.setVideoMode();
return true;
case UE_DoActionInMainThread:
((Action*)ev.user.data1)->handle();
delete (Action*)ev.user.data1;
return true;
case UE_NopWakeup:
// do nothing, it's just a wakeup
return true;
}
warnings << "handleSDLEvent: got unknown user event " << (int)ev.user.code << endl;
return true;
}
if( ev.type == SDL_SYSWMEVENT ) {
// At the moment: Callback for clipboard on X11, should be called every time new event arrived
// Must be called in the main thread.
// TODO: Move to SDL_CLIPBOARDUPDATE, and SDL for clipboard?
Clipboard_handleSysWmEvent(ev);
return true;
}
// mainQueue could be unset at very early or late calls here
if(mainQueue)
mainQueue->push(ev);
return true;
}
// Get all SDL events and push them to our event queue.
// We have to do that in the same thread where we inited the video because of SDL.
bool handleSDLEvents(bool wait) {
assert(isMainThread());
SDL_Event ev;
memset( &ev, 0, sizeof(ev) );
if(wait) {
if( SDL_WaitEvent(&ev) ) {
if(!handleSDLEvent(ev))
return false;
}
else {
notes << "error while waiting for next event" << endl;
SDL_Delay(200);
return true;
}
}
while( SDL_PollEvent(&ev) ) {
if(!handleSDLEvent(ev))
return false;
}
return true;
}
void doMainLoop() {
#ifdef SINGLETHREADED
MainLoopTask mainLoop;
while(true) {
// we handle SDL events in doVideoFrameInMainThread
if(NegResult r = mainLoop.handleFrame())
break;
}
#else
ThreadPoolItem* mainLoopThread = threadPool->start(new MainLoopTask(), "main loop", false);
startMainLockDetector();
if(!bDedicated) {
while(true) {
if(!handleSDLEvents(true))
goto quit;
}
}
quit:
threadPool->wait(mainLoopThread, NULL);
mainLoopThread = NULL;
#endif
}
// note: important to have const char* here because std::string is too dynamic, could be screwed up when returning
void SetCrashHandlerReturnPoint(const char* name) {
#ifndef WIN32
if(sigsetjmp(longJumpBuffer, true) != 0) {
hints << "returned from sigsetjmp in " << name << endl;
if(!tLXOptions) {
notes << "we already have tLXOptions uninitialised, exiting now" << endl;
exit(10);
return;
}
if(tLXOptions->bFullscreen) {
notes << "we are in fullscreen, going to window mode now" << endl;
tLXOptions->bFullscreen = false;
doSetVideoModeInMainThread();
notes << "back in window mode" << endl;
}
}
#endif
}
void doVideoFrameInMainThread() {
if(bDedicated) return;
if(isMainThread())
videoHandler.doSingleDirectFrame();
else
videoHandler.pushFrame();
}
void doSetVideoModeInMainThread() {
if(bDedicated) return;
if(isMainThread())
videoHandler.setVideoMode();
else
videoHandler.requestSetVideoMode();
}
void doVppOperation(Action* act) {
{
ScopedLock lock(videoHandler.mutex);
act->handle();
}
delete act;
}
void doActionInMainThread(Action* act) {
if(bDedicated) {
warnings << "doActionInMainThread cannot work correctly in dedicated mode" << endl;
// we will just put it to the main queue instead
mainQueue->push(act);
return;
}
if(isMainThread()) {
act->handle();
delete act;
}
else {
SDL_Event ev;
ev.type = SDL_USEREVENT;
ev.user.code = UE_DoActionInMainThread;
ev.user.data1 = act;
if(SDL_PushEvent(&ev) == 0) {
errors << "failed to push custom action event" << endl;
}
}
}
Result MainLoopTask::handle_Startup() {
gameloopThreadId = getCurrentThreadId();
assert(gameloopThreadId != (ThreadId)-1);
setCurThreadPriority(0.5f);
// NOTE: This code is really old and outdated.
// We might just merge that with the new code.
// Otherwise, it will never work like this because of
// missing stuff, e.g. no game.init call (which does
// also init the old menu, so we cannot simply call it
// like this right now).
if( tLXOptions->bNewSkinnedGUI )
{
// Just for test - it's not real handler yet
SkinnedGUI::cMainSkin->Load("default");
SkinnedGUI::cMainSkin->OpenLayout("test.skn");
while (game.state != Game::S_Quit) {
tLX->fDeltaTime = GetTime() - tLX->currentTime;
tLX->currentTime = GetTime();
ProcessEvents();
SkinnedGUI::cMainSkin->Frame();
}
return "quit";
}
state = State_Loop;
return true;
}
Result MainLoopTask::handle_Loop() {
if(game.state == Game::S_Quit) {
state = State_Quit;
return true;
}
game.frame();
return true;
}
Result MainLoopTask::handle_Quit() {
if(!isMainThread()) {
SDL_Event quitEv = QuitEventThreadEvent();
if(!bDedicated)
while(SDL_PushEvent(&quitEv) == 0) {}
}
gameloopThreadId = (ThreadId)-1;
return "quit";
}
Result MainLoopTask::handleFrame() {
switch(state) {
case State_Startup: return handle_Startup();
case State_Loop: return handle_Loop();
case State_Quit: return handle_Quit();
}
return "invalid state";
}