From 315d907ef15c0711ec943a4383a394ff3ca0357d Mon Sep 17 00:00:00 2001 From: Steve Checkoway Date: Thu, 15 Dec 2005 08:32:29 +0000 Subject: [PATCH] Add CreateThisThread(). Use it instead of the static object to name the main thread (and the GUI thread on OS X). --- stepmania/PBProject/SMMain.mm | 6 +++++ stepmania/src/RageThreads.cpp | 43 +++++++++++++++++++++++++---------- stepmania/src/RageThreads.h | 1 + stepmania/src/StepMania.cpp | 4 ++++ 4 files changed, 42 insertions(+), 12 deletions(-) diff --git a/stepmania/PBProject/SMMain.mm b/stepmania/PBProject/SMMain.mm index 2e7d30acd8..6d135b5352 100755 --- a/stepmania/PBProject/SMMain.mm +++ b/stepmania/PBProject/SMMain.mm @@ -1,5 +1,6 @@ #include "global.h" #include "RageUtil.h" +#include "RageThreads.h" #import #include "ProductInfo.h" @@ -109,6 +110,11 @@ static void setupMenus( void ) int main( int argc, char **argv ) { + RageThread guiThread; + + guiThread.SetName( "GUIThread" ); + guiThread.CreateThisThread(); + [SMApplication poseAsClass:[NSApplication class]]; NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; diff --git a/stepmania/src/RageThreads.cpp b/stepmania/src/RageThreads.cpp index 85c5617ff6..03ca7bce15 100644 --- a/stepmania/src/RageThreads.cpp +++ b/stepmania/src/RageThreads.cpp @@ -244,21 +244,35 @@ void RageThread::Create( int (*fn)(void *), void *data ) m_pSlot->pImpl = MakeThread( fn, data, &m_pSlot->id ); } -/* On startup, register the main thread's slot. We do this in a static constructor, - * and not InitThreads(), to guarantee it happens in the main thread. */ -static struct SetupMainThread +void RageThread::CreateThisThread() { - SetupMainThread() + ASSERT( m_pSlot == NULL ); + + InitThreads(); + + LockMut( g_ThreadSlotsLock ); + + int slotno = FindEmptyThreadSlot(); + + m_pSlot = &g_ThreadSlots[slotno]; + + if( name == "" ) { - LockMut(g_ThreadSlotsLock); - int slot = FindEmptyThreadSlot(); - strcpy( g_ThreadSlots[slot].name, "MainThread" ); - sprintf( g_ThreadSlots[slot].ThreadFormattedOutput, "Thread: %s", g_ThreadSlots[slot].name ); - g_ThreadSlots[slot].id = RageThread::GetCurrentThreadID(); - g_ThreadSlots[slot].pImpl = MakeThisThread(); + if( LOG ) + LOG->Warn( "Created a thread without naming it first." ); + + /* If you don't name it, I will: */ + strcpy( m_pSlot->name, "Jon" ); } -} SetupMainThreadObj; + else + { + strcpy( m_pSlot->name, name.c_str() ); + } + sprintf( m_pSlot->ThreadFormattedOutput, "Thread: %s", name.c_str() ); + m_pSlot->id = GetThisThreadId(); + m_pSlot->pImpl = MakeThisThread(); +} const char *RageThread::GetCurThreadName() { @@ -294,7 +308,12 @@ int RageThread::Wait() { ASSERT( m_pSlot != NULL ); ASSERT( m_pSlot->pImpl != NULL ); - int ret = m_pSlot->pImpl->Wait(); + int ret; + + if( m_pSlot->id == GetThisThreadId() ) + ret = 0; + else + ret = m_pSlot->pImpl->Wait(); LockMut(g_ThreadSlotsLock); diff --git a/stepmania/src/RageThreads.h b/stepmania/src/RageThreads.h index 27e7c80caa..dc3b63b289 100644 --- a/stepmania/src/RageThreads.h +++ b/stepmania/src/RageThreads.h @@ -19,6 +19,7 @@ public: void SetName( const CString &n ) { name = n; } CString GetName() const { return name; } void Create( int (*fn)(void *), void *data ); + void CreateThisThread(); /* For crash handlers: kill or suspend all threads (except for * the running one) immediately. */ diff --git a/stepmania/src/StepMania.cpp b/stepmania/src/StepMania.cpp index 60bc83b305..e54f18e8b7 100644 --- a/stepmania/src/StepMania.cpp +++ b/stepmania/src/StepMania.cpp @@ -926,6 +926,10 @@ int main(int argc, char* argv[]) int argc = 1; char *argv[] = {"default.xbe"}; #endif + RageThread mainThread; + + mainThread.SetName( "MainThread" ); + mainThread.CreateThisThread(); SetCommandlineArguments( argc, argv );