diff --git a/base/thread.cpp b/base/thread.cpp index 98177d3..b9ea634 100644 --- a/base/thread.cpp +++ b/base/thread.cpp @@ -3,14 +3,15 @@ namespace base { -Thread g_main_thread; pthread_key_t Thread::s_key_; Thread::Thread(SocketServer *ss) : start_routine_(NULL), start_pv_(NULL), MessageQueue(ss) { - if (&g_main_thread == this) { + static bool s_first_thread; + if (!s_first_thread) { pthread_key_create(&s_key_, NULL); pthread_setspecific(s_key_, this); + s_first_thread = true; } } diff --git a/game/ht.h b/game/ht.h index b853e36..e63fb80 100644 --- a/game/ht.h +++ b/game/ht.h @@ -117,6 +117,7 @@ class CommandQueueViewer; #include "game/missionlist.h" #include "mpshared/netmessage.h" #include "game/dragrect.h" +#include "base/thread.h" //#include "yajl/wrapper/jsontypes.h" namespace wi { @@ -8800,6 +8801,9 @@ void HostGetUserName(char *pszBuff, int cbMax) secHost; SoundDevice *HostOpenSoundDevice() secSoundDevice; bool HostSoundServiceProc() secSoundDevice; void HostSleep(dword ct) secHost; +void HostSetGameThread(base::Thread *thread); +base::Thread& HostGetGameThread(); +base::Thread *HostGetGameThreadPointer(); const int knKeyboardAskDefault = 0; const int knKeyboardAskURL = 1; diff --git a/game/sdl/host.cpp b/game/sdl/host.cpp index ab7b560..e4d3f03 100644 --- a/game/sdl/host.cpp +++ b/game/sdl/host.cpp @@ -18,6 +18,7 @@ SdlPackFileReader gpakr; HttpPackManager *gppackm; HttpPackInfoManager *gppim; CompleteManager *gpcptm; +base::Thread *gpgt; char *gpszUdid; @@ -511,4 +512,16 @@ bool HostEnumAddonFiles(Enum *penm, char *pszAddonDir, int cbDir, return false; } +void HostSetGameThread(base::Thread *thread) { + gpgt = thread; +} + +base::Thread& HostGetGameThread() { + return *gpgt; +} + +base::Thread *HostGetGameThreadPointer() { + return gpgt; +} + } // namespace wi diff --git a/game/sdl/main.cpp b/game/sdl/main.cpp index fa3450d..3174a86 100644 --- a/game/sdl/main.cpp +++ b/game/sdl/main.cpp @@ -22,11 +22,18 @@ static void quit(int rc) #ifdef __cplusplus extern "C" #endif + int SDL_main(int argc, char *argv[]) { + // Create the main thread + base::Thread *main_thread = new base::Thread(); + // Set up the main thread as the SDL event thread. base::Thread::current().set_ss(new wi::SdlEventServer()); + // Let the host have a pointer to the thread + wi::HostSetGameThread(main_thread); + // TODO(darrinm): pass args through wi::GameMain((char *)""); return 0; diff --git a/server/main.cpp b/server/main.cpp index d04ee03..a19852a 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -16,6 +16,9 @@ #include #include +// Main server thread +base::Thread main_thread; + // These are defaults that can be overridden const dword kcRoomsMax = 200; const dword kcGamesPerRoomMax = 100;