Files
itgmania212121/stepmania/src/RageThreads.cpp
T

806 lines
20 KiB
C++
Raw Normal View History

/*
* If you're going to use threads, remember this:
*
* Threads suck.
*
* If there's any way to avoid them, take it! Threaded code an order of
* magnitude more complicated, harder to debug and harder to make robust.
*
2004-06-14 05:46:49 +00:00
* That said, here are a few helpers for when they're unavoidable.
*/
2003-02-16 04:01:45 +00:00
#include "global.h"
#include "RageThreads.h"
2002-12-21 05:13:45 +00:00
#include "RageTimer.h"
#include "RageLog.h"
#include "RageUtil.h"
2003-07-27 03:08:02 +00:00
2004-04-05 05:22:32 +00:00
#include <cerrno>
2004-03-21 08:36:43 +00:00
#include <set>
2003-07-27 03:08:02 +00:00
2004-06-11 19:24:05 +00:00
#include "arch/Threads/Threads.h"
#include "arch/Dialog/Dialog.h"
2004-03-20 22:01:40 +00:00
2004-06-14 11:01:10 +00:00
#if defined(CRASH_HANDLER)
2004-06-15 06:24:58 +00:00
#if defined(_WINDOWS)
#include "archutils/Win32/crash.h"
#elif defined(LINUX) || defined(MACOSX)
2004-06-11 19:24:05 +00:00
#include "archutils/Unix/CrashHandler.h"
2004-03-28 06:04:37 +00:00
#endif
2004-06-11 19:29:03 +00:00
#endif
2004-03-28 06:04:37 +00:00
2004-09-09 02:28:05 +00:00
/* Assume TLS doesn't work until told otherwise. It's ArchHooks's job to set this. */
bool RageThread::s_bSystemSupportsTLS = false;
bool RageThread::s_bIsShowingDialog = false;
2004-09-09 02:28:05 +00:00
2003-07-27 03:08:02 +00:00
#define MAX_THREADS 128
//static vector<RageMutex*> *g_MutexList = NULL; /* watch out for static initialization order problems */
2003-07-27 03:08:02 +00:00
struct ThreadSlot
{
2006-03-29 04:59:28 +00:00
mutable char m_szName[1024]; /* mutable so we can force nul-termination */
2003-07-27 03:08:02 +00:00
/* Format this beforehand, since it's easier to do that than to do it under crash conditions. */
2006-03-29 04:59:28 +00:00
char m_szThreadFormattedOutput[1024];
2006-03-29 04:59:28 +00:00
bool m_bUsed;
uint64_t m_iID;
2003-07-27 03:08:02 +00:00
2006-03-29 04:59:28 +00:00
ThreadImpl *m_pImpl;
2004-03-28 06:04:37 +00:00
#undef CHECKPOINT_COUNT
2003-08-08 09:03:30 +00:00
#define CHECKPOINT_COUNT 5
struct ThreadCheckpoint
{
2006-03-29 04:59:28 +00:00
const char *m_szFile, *m_szMessage;
int m_iLine;
char m_szFormattedBuf[1024];
ThreadCheckpoint() { Set( NULL, 0, NULL ); }
2006-03-29 04:59:28 +00:00
void Set( const char *szFile, int iLine, const char *szMessage = NULL );
const char *GetFormattedCheckpoint();
};
2006-03-29 04:59:28 +00:00
ThreadCheckpoint m_Checkpoints[CHECKPOINT_COUNT];
int m_iCurCheckpoint, m_iNumCheckpoints;
const char *GetFormattedCheckpoint( int lineno );
ThreadSlot() { Init(); }
void Init()
2003-07-27 03:08:02 +00:00
{
2006-03-29 04:59:28 +00:00
m_iID = GetInvalidThreadId();
m_iCurCheckpoint = m_iNumCheckpoints = 0;
m_pImpl = NULL;
2004-06-13 07:55:27 +00:00
/* Reset used last; otherwise, a thread creation might pick up the slot. */
2006-03-29 04:59:28 +00:00
m_bUsed = false;
2003-07-27 03:08:02 +00:00
}
2005-12-19 02:18:09 +00:00
void Release()
{
2006-03-29 04:59:28 +00:00
SAFE_DELETE( m_pImpl );
2005-12-19 02:18:09 +00:00
Init();
}
2004-03-20 22:01:40 +00:00
const char *GetThreadName() const;
2003-07-27 03:08:02 +00:00
};
2004-06-11 19:24:05 +00:00
2006-03-29 04:59:28 +00:00
void ThreadSlot::ThreadCheckpoint::Set( const char *szFile, int iLine, const char *szMessage )
{
2006-03-29 04:59:28 +00:00
m_szFile = szFile;
m_iLine = iLine;
m_szMessage = szMessage;
2006-03-29 05:19:13 +00:00
/* Skip any path components. */
if( m_szFile != NULL )
{
const char *p = strrchr( m_szFile, '/' );
if( p == NULL )
p = strrchr( m_szFile, '\\' );
if( p != NULL && p[1] != '\0' )
m_szFile = p+1;
}
2006-08-09 22:07:53 +00:00
snprintf( m_szFormattedBuf, sizeof(m_szFormattedBuf), " %s:%i %s", m_szFile, m_iLine, m_szMessage? m_szMessage:"" );
}
const char *ThreadSlot::ThreadCheckpoint::GetFormattedCheckpoint()
{
2006-03-29 04:59:28 +00:00
if( m_szFile == NULL )
return NULL;
/* Make sure it's terminated: */
2006-03-29 04:59:28 +00:00
m_szFormattedBuf[ sizeof(m_szFormattedBuf)-1 ] = 0;
2006-03-29 04:59:28 +00:00
return m_szFormattedBuf;
}
const char *ThreadSlot::GetFormattedCheckpoint( int lineno )
{
2006-03-29 04:59:28 +00:00
if( lineno >= CHECKPOINT_COUNT || lineno >= m_iNumCheckpoints )
return NULL;
2006-03-29 04:59:28 +00:00
if( m_iNumCheckpoints == CHECKPOINT_COUNT )
{
2006-03-29 04:59:28 +00:00
lineno += m_iCurCheckpoint;
lineno %= CHECKPOINT_COUNT;
}
2006-03-29 04:59:28 +00:00
return m_Checkpoints[lineno].GetFormattedCheckpoint();
}
2003-07-27 03:08:02 +00:00
static ThreadSlot g_ThreadSlots[MAX_THREADS];
2004-06-11 19:24:05 +00:00
struct ThreadSlot *g_pUnknownThreadSlot = NULL;
2004-06-13 07:09:41 +00:00
2006-03-29 04:59:28 +00:00
/* Lock this mutex before using or modifying m_pImpl. Other values are just identifiers,
2004-06-13 07:55:27 +00:00
* so possibly racing over them is harmless (simply using a stale thread ID, etc). */
static RageMutex &GetThreadSlotsLock()
{
static RageMutex *pLock = new RageMutex( "ThreadSlots" );
return *pLock;
}
2003-07-27 03:08:02 +00:00
static int FindEmptyThreadSlot()
{
LockMut( GetThreadSlotsLock() );
2003-07-27 03:08:02 +00:00
for( int entry = 0; entry < MAX_THREADS; ++entry )
{
2006-03-29 04:59:28 +00:00
if( g_ThreadSlots[entry].m_bUsed )
2003-07-27 03:08:02 +00:00
continue;
2006-03-29 04:59:28 +00:00
g_ThreadSlots[entry].m_bUsed = true;
2003-07-27 03:08:02 +00:00
return entry;
}
2005-12-15 08:59:27 +00:00
RageException::Throw( "Out of thread slots!" );
2003-07-27 03:08:02 +00:00
}
2004-06-16 01:14:43 +00:00
static void InitThreads()
{
2005-04-25 09:44:59 +00:00
/* We don't have to worry about two threads calling this at once, since it's
* called when we create a thread. */
2004-06-16 01:14:43 +00:00
static bool bInitialized = false;
if( bInitialized )
return;
2007-02-23 09:01:11 +00:00
LockMut( GetThreadSlotsLock() );
2004-06-16 01:14:43 +00:00
/* Libraries might start threads on their own, which might call user callbacks,
* which could come back here. Make sure we don't accidentally initialize twice. */
if( bInitialized )
return;
bInitialized = true;
/* Register the "unknown thread" slot. */
int slot = FindEmptyThreadSlot();
2006-03-29 04:59:28 +00:00
strcpy( g_ThreadSlots[slot].m_szName, "Unknown thread" );
g_ThreadSlots[slot].m_iID = GetInvalidThreadId();
sprintf( g_ThreadSlots[slot].m_szThreadFormattedOutput, "Unknown thread" );
2004-06-16 01:14:43 +00:00
g_pUnknownThreadSlot = &g_ThreadSlots[slot];
}
2004-06-11 19:24:05 +00:00
static ThreadSlot *GetThreadSlotFromID( uint64_t iID )
2003-07-27 03:08:02 +00:00
{
2004-06-16 01:14:43 +00:00
InitThreads();
2003-07-27 03:08:02 +00:00
for( int entry = 0; entry < MAX_THREADS; ++entry )
{
2006-03-29 04:59:28 +00:00
if( !g_ThreadSlots[entry].m_bUsed )
2003-07-27 03:08:02 +00:00
continue;
2006-03-29 04:59:28 +00:00
if( g_ThreadSlots[entry].m_iID == iID )
2004-06-11 19:24:05 +00:00
return &g_ThreadSlots[entry];
2003-07-27 03:08:02 +00:00
}
2004-06-11 19:24:05 +00:00
return NULL;
2003-07-27 03:08:02 +00:00
}
2004-06-11 19:24:05 +00:00
static ThreadSlot *GetCurThreadSlot()
2004-04-20 03:44:38 +00:00
{
return GetThreadSlotFromID( RageThread::GetCurrentThreadID() );
}
2004-06-11 19:24:05 +00:00
static ThreadSlot *GetUnknownThreadSlot()
2004-02-20 23:42:40 +00:00
{
2004-06-11 19:24:05 +00:00
return g_pUnknownThreadSlot;
2004-02-20 23:42:40 +00:00
}
2003-07-27 03:08:02 +00:00
2005-12-19 02:08:06 +00:00
RageThread::RageThread()
2003-07-27 03:08:02 +00:00
{
2004-06-11 19:24:05 +00:00
m_pSlot = NULL;
2006-11-01 05:42:15 +00:00
m_sName = "unnamed";
2003-07-27 03:08:02 +00:00
}
RageThread::RageThread( const RageThread &cpy )
{
/* Copying a thread does not start the copy. */
m_pSlot = NULL;
m_sName = cpy.m_sName;
}
2003-07-27 03:08:02 +00:00
RageThread::~RageThread()
{
if( m_pSlot != NULL )
Wait();
2003-07-27 03:08:02 +00:00
}
2004-03-20 22:01:40 +00:00
const char *ThreadSlot::GetThreadName() const
{
/* This function may be called in crash conditions, so guarantee the string
* is null-terminated. */
2006-03-29 04:59:28 +00:00
m_szName[sizeof(m_szName)-1] = 0;
2004-03-20 22:01:40 +00:00
2006-03-29 04:59:28 +00:00
return m_szName;
2004-03-20 22:01:40 +00:00
}
2003-07-27 03:08:02 +00:00
void RageThread::Create( int (*fn)(void *), void *data )
{
/* Don't create a thread that's already running: */
2004-06-11 19:24:05 +00:00
ASSERT( m_pSlot == NULL );
2003-07-27 03:08:02 +00:00
2004-06-16 01:14:43 +00:00
InitThreads();
2004-06-13 07:09:41 +00:00
/* Lock unused slots, so nothing else uses our slot before we mark it used. */
LockMut(GetThreadSlotsLock());
2004-06-13 07:09:41 +00:00
2003-07-27 03:08:02 +00:00
int slotno = FindEmptyThreadSlot();
2004-06-11 19:24:05 +00:00
m_pSlot = &g_ThreadSlots[slotno];
2003-07-27 03:08:02 +00:00
2006-11-01 05:42:15 +00:00
strcpy( m_pSlot->m_szName, m_sName.c_str() );
2003-07-27 03:08:02 +00:00
2004-08-14 23:28:33 +00:00
if( LOG )
2006-11-01 05:42:15 +00:00
LOG->Trace( "Starting thread: %s", m_sName.c_str() );
sprintf( m_pSlot->m_szThreadFormattedOutput, "Thread: %s", m_sName.c_str() );
2004-06-11 19:24:05 +00:00
2004-06-16 01:53:27 +00:00
/* Start a thread using our own startup function. We pass the id to fill in,
* to make sure it's set before the thread actually starts. (Otherwise, early
* checkpoints might not have a completely set-up thread slot.) */
2006-03-29 04:59:28 +00:00
m_pSlot->m_pImpl = MakeThread( fn, data, &m_pSlot->m_iID );
2003-07-27 03:08:02 +00:00
}
2006-01-21 23:01:06 +00:00
RageThreadRegister::RageThreadRegister( const RString &sName )
{
InitThreads();
LockMut( GetThreadSlotsLock() );
int iSlot = FindEmptyThreadSlot();
m_pSlot = &g_ThreadSlots[iSlot];
2006-03-29 04:59:28 +00:00
strcpy( m_pSlot->m_szName, sName );
sprintf( m_pSlot->m_szThreadFormattedOutput, "Thread: %s", sName.c_str() );
2006-03-29 04:59:28 +00:00
m_pSlot->m_iID = GetThisThreadId();
m_pSlot->m_pImpl = MakeThisThread();
}
RageThreadRegister::~RageThreadRegister()
{
LockMut( GetThreadSlotsLock() );
2005-12-19 02:18:09 +00:00
m_pSlot->Release();
m_pSlot = NULL;
}
2003-07-27 03:08:02 +00:00
const char *RageThread::GetCurThreadName()
{
2004-04-20 03:44:38 +00:00
return GetThreadNameByID( GetCurrentThreadID() );
}
2004-06-11 19:24:05 +00:00
const char *RageThread::GetThreadNameByID( uint64_t iID )
2004-04-20 03:44:38 +00:00
{
2004-06-11 19:24:05 +00:00
ThreadSlot *slot = GetThreadSlotFromID( iID );
if( slot == NULL )
2004-04-20 03:44:38 +00:00
return "???";
2004-06-11 19:24:05 +00:00
return slot->GetThreadName();
2003-07-27 03:08:02 +00:00
}
2004-09-07 02:58:59 +00:00
bool RageThread::EnumThreadIDs( int n, uint64_t &iID )
{
if( n >= MAX_THREADS )
return false;
LockMut(GetThreadSlotsLock());
2004-09-07 02:58:59 +00:00
const ThreadSlot *slot = &g_ThreadSlots[n];
2006-03-29 04:59:28 +00:00
if( slot->m_bUsed )
iID = slot->m_iID;
2004-09-07 02:58:59 +00:00
else
iID = GetInvalidThreadId();
return true;
}
2003-07-27 03:08:02 +00:00
int RageThread::Wait()
{
2004-06-11 19:24:05 +00:00
ASSERT( m_pSlot != NULL );
2006-03-29 04:59:28 +00:00
ASSERT( m_pSlot->m_pImpl != NULL );
int ret = m_pSlot->m_pImpl->Wait();
2003-07-27 03:08:02 +00:00
LockMut( GetThreadSlotsLock() );
2004-06-13 07:55:27 +00:00
2005-12-19 02:18:09 +00:00
m_pSlot->Release();
2004-06-11 19:24:05 +00:00
m_pSlot = NULL;
2003-07-27 03:08:02 +00:00
2004-06-11 19:24:05 +00:00
return ret;
2004-03-28 06:04:37 +00:00
}
2003-07-27 07:00:13 +00:00
void RageThread::HaltAllThreads( bool Kill )
2003-07-27 03:08:02 +00:00
{
2004-06-11 19:24:05 +00:00
const uint64_t ThisThreadID = GetThisThreadId();
2003-10-01 08:20:03 +00:00
for( int entry = 0; entry < MAX_THREADS; ++entry )
{
2006-03-29 04:59:28 +00:00
if( !g_ThreadSlots[entry].m_bUsed )
2003-10-01 08:20:03 +00:00
continue;
2006-03-29 04:59:28 +00:00
if( ThisThreadID == g_ThreadSlots[entry].m_iID || g_ThreadSlots[entry].m_pImpl == NULL )
2004-03-28 06:04:37 +00:00
continue;
2006-03-29 04:59:28 +00:00
g_ThreadSlots[entry].m_pImpl->Halt( Kill );
2004-03-28 06:04:37 +00:00
}
2004-01-12 01:36:25 +00:00
}
void RageThread::ResumeAllThreads()
{
2004-06-11 19:24:05 +00:00
const uint64_t ThisThreadID = GetThisThreadId();
2004-01-12 01:36:25 +00:00
for( int entry = 0; entry < MAX_THREADS; ++entry )
{
2006-03-29 04:59:28 +00:00
if( !g_ThreadSlots[entry].m_bUsed )
2004-01-12 01:36:25 +00:00
continue;
2006-03-29 04:59:28 +00:00
if( ThisThreadID == g_ThreadSlots[entry].m_iID || g_ThreadSlots[entry].m_pImpl == NULL )
2004-01-12 01:36:25 +00:00
continue;
2006-03-29 04:59:28 +00:00
g_ThreadSlots[entry].m_pImpl->Resume();
2003-10-01 08:20:03 +00:00
}
2003-07-27 03:08:02 +00:00
}
2004-06-11 19:24:05 +00:00
uint64_t RageThread::GetCurrentThreadID()
2004-04-07 03:38:15 +00:00
{
2004-06-11 19:24:05 +00:00
return GetThisThreadId();
2004-04-07 03:38:15 +00:00
}
uint64_t RageThread::GetInvalidThreadID()
{
return GetInvalidThreadId();
}
2004-04-07 03:38:15 +00:00
/* Normally, checkpoints are only seen in crash logs. It's occasionally useful
* to see them in logs, but this outputs a huge amount of text. */
static bool g_LogCheckpoints = false;
void Checkpoints::LogCheckpoints( bool on )
{
g_LogCheckpoints = on;
}
void Checkpoints::SetCheckpoint( const char *file, int line, const char *message )
{
2004-06-11 19:24:05 +00:00
ThreadSlot *slot = GetCurThreadSlot();
if( slot == NULL )
slot = GetUnknownThreadSlot();
/* We can't ASSERT here, since that uses checkpoints. */
2004-06-11 19:24:05 +00:00
if( slot == NULL )
2004-06-16 00:29:52 +00:00
sm_crash( "GetUnknownThreadSlot() returned NULL" );
/* Ignore everything up to and including the first "src/". */
const char *temp = strstr( file, "src/" );
if( temp )
file = temp + 4;
2006-03-29 04:59:28 +00:00
slot->m_Checkpoints[slot->m_iCurCheckpoint].Set( file, line, message );
if( g_LogCheckpoints )
2006-03-29 04:59:28 +00:00
LOG->Trace( "%s", slot->m_Checkpoints[slot->m_iCurCheckpoint].m_szFormattedBuf );
2006-03-29 04:59:28 +00:00
++slot->m_iCurCheckpoint;
slot->m_iNumCheckpoints = max( slot->m_iNumCheckpoints, slot->m_iCurCheckpoint );
slot->m_iCurCheckpoint %= CHECKPOINT_COUNT;
}
/* This is called under crash conditions. Be careful. */
static const char *GetCheckpointLog( int slotno, int lineno )
{
ThreadSlot &slot = g_ThreadSlots[slotno];
2006-03-29 04:59:28 +00:00
if( !slot.m_bUsed )
return NULL;
2004-02-20 23:42:40 +00:00
/* Only show the "Unknown thread" entry if it has at least one checkpoint. */
2005-12-15 08:59:27 +00:00
if( &slot == g_pUnknownThreadSlot && slot.GetFormattedCheckpoint(0) == NULL )
2004-02-20 23:42:40 +00:00
return NULL;
if( lineno != 0 )
return slot.GetFormattedCheckpoint( lineno-1 );
2006-03-29 04:59:28 +00:00
slot.m_szThreadFormattedOutput[sizeof(slot.m_szThreadFormattedOutput)-1] = 0;
return slot.m_szThreadFormattedOutput;
}
2004-10-27 19:49:08 +00:00
/* XXX: iSize check unimplemented */
void Checkpoints::GetLogs( char *pBuf, int iSize, const char *delim )
{
2004-10-27 19:49:08 +00:00
pBuf[0] = 0;
for( int slotno = 0; slotno < MAX_THREADS; ++slotno )
{
const char *buf = GetCheckpointLog( slotno, 0 );
if( buf == NULL )
2004-02-20 23:42:40 +00:00
continue;
2004-10-27 19:49:08 +00:00
strcat( pBuf, buf );
strcat( pBuf, delim );
2005-12-15 08:59:27 +00:00
for( int line = 1; (buf = GetCheckpointLog(slotno, line)) != NULL; ++line )
{
2004-10-27 19:49:08 +00:00
strcat( pBuf, buf );
strcat( pBuf, delim );
}
}
}
/*
* "Safe" mutexes: locking the same mutex more than once from the same thread
* is refcounted and does not deadlock.
*
* Only actually lock the mutex once; when we do so, remember which thread locked it.
* Then, when we lock in the future, only increment a counter, with no locks.
*
* We must be holding the real mutex to write to LockedBy and LockCnt. However,
* we can look at LockedBy to see if it's us that owns it (in which case, we already
* hold the mutex).
*
* In Windows, this helps smooth out performance: for some reason, Windows likes
* to yank the scheduler away from a thread that locks a mutex that it already owns.
*/
#if 0
2004-03-21 08:36:43 +00:00
static const int MAX_MUTEXES = 256;
/* g_MutexesBefore[n] is a list of mutex IDs which must be locked before n (if at all).
* The array g_MutexesBefore[n] is locked for writing by locking mutex n, so lock that
* mutex *before* calling MarkLockedMutex(). */
bool g_MutexesBefore[MAX_MUTEXES][MAX_MUTEXES];
void RageMutex::MarkLockedMutex()
{
/* This only makes locking take about 25% longer, and we generally don't lock in
* inner loops, so this is enabled by default for now. */
// if( !g_bEnableMutexOrderChecking )
// return;
const int ID = this->m_UniqueID;
ASSERT( ID < MAX_MUTEXES );
/* This is a queue of all mutexes that must be locked before ID, if at all. */
vector<const RageMutex *> before;
/* Iterate over all locked mutexes that are locked by this thread. */
2004-09-21 07:53:39 +00:00
for( unsigned i = 0; i < g_MutexList->size(); ++i )
2004-03-21 08:36:43 +00:00
{
const RageMutex *mutex = (*g_MutexList)[i];
if( mutex->m_UniqueID == this->m_UniqueID )
continue;
if( !mutex->IsLockedByThisThread() )
continue;
/* mutex must be locked before this. If we've previously marked the opposite,
* then we have an inconsistent lock order. */
if( g_MutexesBefore[mutex->m_UniqueID][this->m_UniqueID] )
{
2004-03-21 09:30:15 +00:00
LOG->Warn( "Mutex lock inconsistency: mutex \"%s\" must be locked before \"%s\"",
this->GetName().c_str(), mutex->GetName().c_str() );
2004-03-21 08:36:43 +00:00
break;
}
/* Optimization: don't add it to the queue if it's already been done. */
if( !g_MutexesBefore[this->m_UniqueID][mutex->m_UniqueID] )
before.push_back( mutex );
}
while( before.size() )
{
const RageMutex *mutex = before.back();
before.pop_back();
g_MutexesBefore[this->m_UniqueID][mutex->m_UniqueID] = 1;
/* All IDs which must be locked before mutex must also be locked before
* this. That is, if A < mutex, because mutex < this, mark A < this. */
for( i = 0; i < g_MutexList->size(); ++i )
{
const RageMutex *mutex2 = (*g_MutexList)[i];
if( g_MutexesBefore[mutex->m_UniqueID][mutex2->m_UniqueID] )
before.push_back( mutex2 );
}
}
}
2004-03-21 08:36:43 +00:00
/* XXX: How can g_FreeMutexIDs and g_MutexList be threadsafed? */
static set<int> *g_FreeMutexIDs = NULL;
#endif
2006-01-21 23:01:06 +00:00
RageMutex::RageMutex( const RString &name ):
m_sName( name )
{
2004-06-11 19:24:05 +00:00
m_pMutex = MakeMutex( this );
m_LockedBy = GetInvalidThreadId();
m_LockCnt = 0;
2004-03-21 08:36:43 +00:00
/* if( g_FreeMutexIDs == NULL )
2004-03-21 08:36:43 +00:00
{
g_FreeMutexIDs = new set<int>;
for( int i = 0; i < MAX_MUTEXES; ++i )
g_FreeMutexIDs->insert( i );
}
2004-04-20 01:08:49 +00:00
if( g_FreeMutexIDs->empty() )
{
ASSERT_M( g_MutexList, "!g_FreeMutexIDs but !g_MutexList?" ); // doesn't make sense to be out of mutexes yet never created any
2006-01-21 23:01:06 +00:00
RString s;
2004-04-20 01:08:49 +00:00
for( unsigned i = 0; i < g_MutexList->size(); ++i )
{
if( i )
s += ", ";
s += ssprintf( "\"%s\"", (*g_MutexList)[i]->GetName().c_str() );
}
LOG->Trace( "%s", s.c_str() );
FAIL_M( ssprintf("MAX_MUTEXES exceeded creating \"%s\"", name.c_str() ) );
}
2004-03-21 08:36:43 +00:00
m_UniqueID = *g_FreeMutexIDs->begin();
2004-03-21 08:36:43 +00:00
g_FreeMutexIDs->erase( g_FreeMutexIDs->begin() );
if( g_MutexList == NULL )
g_MutexList = new vector<RageMutex*>;
g_MutexList->push_back( this );
*/
}
RageMutex::~RageMutex()
{
2004-06-11 19:24:05 +00:00
delete m_pMutex;
/*
2004-03-21 08:36:43 +00:00
vector<RageMutex*>::iterator it = find( g_MutexList->begin(), g_MutexList->end(), this );
ASSERT( it != g_MutexList->end() );
g_MutexList->erase( it );
if( g_MutexList->empty() )
{
delete g_MutexList;
g_MutexList = NULL;
}
2004-06-11 19:24:05 +00:00
delete m_pMutex;
2004-03-21 08:36:43 +00:00
g_FreeMutexIDs->insert( m_UniqueID );
*/
}
void RageMutex::Lock()
{
2005-12-15 08:59:27 +00:00
uint64_t iThisThreadId = GetThisThreadId();
2005-08-24 02:26:36 +00:00
if( m_LockedBy == iThisThreadId )
2004-06-11 19:24:05 +00:00
{
++m_LockCnt;
return;
}
2004-03-21 09:30:15 +00:00
2004-06-11 19:24:05 +00:00
if( !m_pMutex->Lock() )
{
const ThreadSlot *ThisSlot = GetThreadSlotFromID( GetThisThreadId() );
const ThreadSlot *OtherSlot = GetThreadSlotFromID( m_LockedBy );
2006-01-21 23:01:06 +00:00
RString ThisSlotName = "(???" ")"; // stupid trigraph warnings
RString OtherSlotName = "(???" ")"; // stupid trigraph warnings
if( ThisSlot )
2006-03-29 04:59:28 +00:00
ThisSlotName = ssprintf( "%s (%i)", ThisSlot->GetThreadName(), (int) ThisSlot->m_iID );
if( OtherSlot )
2006-03-29 04:59:28 +00:00
OtherSlotName = ssprintf( "%s (%i)", OtherSlot->GetThreadName(), (int) OtherSlot->m_iID );
2006-01-21 23:01:06 +00:00
const RString sReason = ssprintf( "Thread deadlock on mutex %s between %s and %s",
GetName().c_str(), ThisSlotName.c_str(), OtherSlotName.c_str() );
2004-06-11 19:24:05 +00:00
#if defined(CRASH_HANDLER)
/* Don't leave GetThreadSlotsLock() locked when we call ForceCrashHandlerDeadlock. */
GetThreadSlotsLock().Lock();
2006-03-29 04:59:28 +00:00
uint64_t CrashHandle = OtherSlot? OtherSlot->m_iID:0;
GetThreadSlotsLock().Unlock();
2004-06-13 07:55:27 +00:00
2004-06-11 19:24:05 +00:00
/* Pass the crash handle of the other thread, so it can backtrace that thread. */
2005-12-31 03:56:25 +00:00
CrashHandler::ForceDeadlock( sReason, CrashHandle );
2004-06-11 19:29:03 +00:00
#else
FAIL_M( sReason );
2004-06-11 19:29:03 +00:00
#endif
2004-06-11 19:24:05 +00:00
}
2005-08-24 02:26:36 +00:00
m_LockedBy = iThisThreadId;
2004-03-21 09:30:15 +00:00
2004-05-05 20:37:14 +00:00
/* This has internal thread safety issues itself (eg. one thread may delete
* a mutex while another locks one); disable for now. */
2004-06-11 19:24:05 +00:00
// MarkLockedMutex();
}
2004-06-14 05:29:12 +00:00
bool RageMutex::TryLock()
{
2005-12-15 08:59:27 +00:00
if( m_LockedBy == GetThisThreadId() )
2004-06-14 05:29:12 +00:00
{
++m_LockCnt;
return true;
}
if( !m_pMutex->TryLock() )
return false;
m_LockedBy = GetThisThreadId();
return true;
}
void RageMutex::Unlock()
{
2004-06-11 19:24:05 +00:00
if( m_LockCnt )
{
--m_LockCnt;
return;
}
m_LockedBy = GetInvalidThreadId();
m_pMutex->Unlock();
}
2004-03-21 08:36:43 +00:00
bool RageMutex::IsLockedByThisThread() const
{
2004-06-11 19:24:05 +00:00
return m_LockedBy == GetThisThreadId();
2004-03-21 08:36:43 +00:00
}
2005-12-15 08:59:27 +00:00
LockMutex::LockMutex( RageMutex &pMutex, const char *file_, int line_ ):
mutex( pMutex ),
file( file_ ),
line( line_ ),
locked_at( RageTimer::GetTimeSinceStart() )
2002-12-21 05:13:45 +00:00
{
mutex.Lock();
2002-12-27 22:11:55 +00:00
locked = true;
2002-12-21 05:13:45 +00:00
}
LockMutex::~LockMutex()
{
2005-12-15 08:59:27 +00:00
if( locked )
2002-12-27 22:11:55 +00:00
mutex.Unlock();
2002-12-21 05:13:45 +00:00
}
2002-12-27 22:11:55 +00:00
void LockMutex::Unlock()
{
ASSERT( locked );
2002-12-27 22:11:55 +00:00
locked = false;
mutex.Unlock();
2002-12-27 23:01:45 +00:00
if( file && locked_at != -1 )
2002-12-27 23:01:45 +00:00
{
const float dur = RageTimer::GetTimeSinceStart() - locked_at;
if( dur > 0.015f )
LOG->Trace( "Lock at %s:%i took %f", file, line, dur );
2002-12-27 23:01:45 +00:00
}
2002-12-27 22:11:55 +00:00
}
2006-01-21 23:01:06 +00:00
RageEvent::RageEvent( RString name ):
2005-12-15 08:59:27 +00:00
RageMutex( name )
2004-11-07 23:21:45 +00:00
{
m_pEvent = MakeEvent( m_pMutex );
}
RageEvent::~RageEvent()
{
delete m_pEvent;
}
2004-11-10 22:10:52 +00:00
/* For each of these calls, the mutex must be locked, and must not be locked recursively. */
2005-01-26 20:59:41 +00:00
bool RageEvent::Wait( RageTimer *pTimeout )
2004-11-07 23:21:45 +00:00
{
ASSERT( IsLockedByThisThread() );
2004-11-10 22:10:52 +00:00
ASSERT( m_LockCnt == 0 );
2005-01-26 20:59:41 +00:00
/* A zero RageTimer also means no timeout. */
if( pTimeout != NULL && pTimeout->IsZero() )
pTimeout = NULL;
bool bRet = m_pEvent->Wait( pTimeout );
2004-11-10 22:10:52 +00:00
m_LockedBy = GetThisThreadId();
2005-01-26 20:59:41 +00:00
return bRet;
2004-11-07 23:21:45 +00:00
}
void RageEvent::Signal()
{
ASSERT( IsLockedByThisThread() );
2004-11-10 22:10:52 +00:00
ASSERT( m_LockCnt == 0 );
2004-11-07 23:21:45 +00:00
m_pEvent->Signal();
}
void RageEvent::Broadcast()
{
ASSERT( IsLockedByThisThread() );
2004-11-10 22:10:52 +00:00
ASSERT( m_LockCnt == 0 );
2004-11-07 23:21:45 +00:00
m_pEvent->Broadcast();
}
bool RageEvent::WaitTimeoutSupported() const
{
return m_pEvent->WaitTimeoutSupported();
}
2006-01-21 23:01:06 +00:00
RageSemaphore::RageSemaphore( RString sName, int iInitialValue ):
2004-06-14 05:29:12 +00:00
m_sName( sName )
{
m_pSema = MakeSemaphore( iInitialValue );
}
RageSemaphore::~RageSemaphore()
{
delete m_pSema;
}
int RageSemaphore::GetValue() const
{
return m_pSema->GetValue();
}
void RageSemaphore::Post()
{
m_pSema->Post();
}
2004-08-29 22:51:09 +00:00
void RageSemaphore::Wait( bool bFailOnTimeout )
2004-06-14 05:29:12 +00:00
{
retry:
2004-06-14 05:29:12 +00:00
if( m_pSema->Wait() )
return;
if( !bFailOnTimeout || RageThread::GetIsShowingDialog() )
goto retry;
2004-06-14 05:29:12 +00:00
/* We waited too long. We're probably deadlocked, though unlike mutexes, we can't
* tell which thread we're stuck on. */
const ThreadSlot *ThisSlot = GetThreadSlotFromID( GetThisThreadId() );
2006-01-21 23:01:06 +00:00
const RString sReason = ssprintf( "Semaphore timeout on mutex %s on thread %s",
2004-06-14 05:29:12 +00:00
GetName().c_str(), ThisSlot? ThisSlot->GetThreadName(): "(???" ")" ); // stupid trigraph warnings
2004-06-14 10:57:21 +00:00
#if defined(CRASH_HANDLER)
2005-12-31 03:56:25 +00:00
CrashHandler::ForceDeadlock( sReason, GetInvalidThreadId() );
2004-06-14 05:29:12 +00:00
#else
RageException::Throw( "%s", sReason.c_str() );
#endif
}
bool RageSemaphore::TryWait()
{
return m_pSema->TryWait();
}
/*
2004-05-06 02:40:33 +00:00
* Copyright (c) 2001-2004 Glenn Maynard
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/