From 2a02c4bb2ab611952ac783eea9b93a1d44790dd7 Mon Sep 17 00:00:00 2001 From: Patrik Gornicz Date: Sat, 22 Nov 2008 00:02:32 -0500 Subject: [PATCH] added some thread safty ... much more still needed --- src/debug.cpp | 22 ++++++++++++++++- src/debug.h | 6 +++++ src/main.cpp | 77 +++++++++++++++++++++++++++++++++++++++++++++++------------ 3 files changed, 89 insertions(+), 16 deletions(-) diff --git a/src/debug.cpp b/src/debug.cpp index c155785..909a54f 100644 --- a/src/debug.cpp +++ b/src/debug.cpp @@ -22,11 +22,31 @@ using std::cerr; using std::cout; using std::endl; +#include + /// ***** Public Methods ***** +SDL_mutex* muDPF = NULL; + void DPF(int level, const char* pstr) { - cout << pstr << endl; + // lock + SDL_mutexP( muDPF ); + + cout << pstr << endl; + + //unlock + SDL_mutexV( muDPF ); +} + +void debug::init() +{ + muDPF = SDL_CreateMutex(); +} + +void debug::clean() +{ + SDL_DestroyMutex( muDPF ); } /// ***** Private Methods ***** diff --git a/src/debug.h b/src/debug.h index 517c0c6..c9c5e7a 100644 --- a/src/debug.h +++ b/src/debug.h @@ -25,6 +25,12 @@ using std::endl; void DPF(int level, const char* pstr); +namespace debug +{ + void init(); + void clean(); +} + // comment out when not debugging #define DEBUGGING diff --git a/src/main.cpp b/src/main.cpp index 1309824..f94fac6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -31,7 +31,14 @@ /// ***** Private Method Headers ***** -void init(); +void mainInit(); +void mainClean(); + +void updatesInit(); +void updatesClean(); + +void drawInit(); +void drawClean(); void sighandler( int sig ); @@ -44,6 +51,9 @@ void handleInput(); void update(float); void draw(); +int startUpdateThread(void*); +int startDrawThread(void*); + /// ***** Private Variables ***** // variable used to determine if it is time to shutdown @@ -69,26 +79,36 @@ const int min_wait_milli = 20; const int min_wait_micro = min_wait_milli * 1000; const float game_step = 10; -int startUpdateThread(void*); -int startDrawThread(void*); +bool updateInitialized = false; +bool drawInitialized = false; /// ***** MAIN Method ***** int main(int argc, char** args) { - init(); + mainInit(); run(); - clean(); + mainClean(); return 0; } /// ***** Initializers/Cleaners ***** -void init() +void mainInit() { installSignal(); - graphics::init(); - DPF(0, "Graphics initialized"); + debug::init(); +} + +void mainClean() +{ + debug::clean(); +} + +void updatesInit() +{ + while(!drawInitialized) + SDL_Delay(100); game::init(); DPF(0, "Game initialized"); @@ -100,33 +120,52 @@ void init() DPF(0, "Configs initialized"); DPF(0, "Initialization Complete"); + + updateInitialized = true; } -void clean() +void updatesClean() { - DPF(0, "Cleaning up"); + DPF(0, "Update Thread Cleaning"); cfg::clean(); input::clean(); game::clean(); +} + +void drawInit() +{ + graphics::init(); + DPF(0, "Graphics initialized"); + + drawInitialized = true; + + while(!updateInitialized) + SDL_Delay(100); +} + +void drawClean() +{ + DPF(0, "Draw Thread Cleaning"); graphics::clean(); } /// ***** Private Methods ***** +SDL_Thread* updatesThread = NULL; +SDL_Thread* drawThread = NULL; void run() { is_Running = true; - SDL_Thread* updates = SDL_CreateThread(startUpdateThread, NULL); + updatesThread = SDL_CreateThread(startUpdateThread, NULL); + drawThread = SDL_CreateThread(startDrawThread, NULL); - // for some reason this needs to be on the main thread ...? - startDrawThread(NULL); - - SDL_WaitThread(updates, NULL); + SDL_WaitThread(updatesThread, NULL); + SDL_WaitThread(drawThread, NULL); } void updateFPSCounters() @@ -179,6 +218,8 @@ void draw() int startUpdateThread(void*) { + updatesInit(); + last_Second = tickCountMicro(); while(is_Running) @@ -200,15 +241,21 @@ int startUpdateThread(void*) } } + updatesClean(); + return 0; } int startDrawThread(void*) { + drawInit(); + while(is_Running) { draw(); } + drawClean(); + return 0; } -- 2.10.2