RageSemaphore

This commit is contained in:
Glenn Maynard
2004-06-14 05:29:12 +00:00
parent 1f0ac3792d
commit 22fe9d6841
2 changed files with 81 additions and 0 deletions
+61
View File
@@ -547,6 +547,22 @@ void RageMutex::Lock()
// MarkLockedMutex();
}
bool RageMutex::TryLock()
{
if( m_LockedBy == (uint64_t) GetThisThreadId() )
{
++m_LockCnt;
return true;
}
if( !m_pMutex->TryLock() )
return false;
m_LockedBy = GetThisThreadId();
return true;
}
void RageMutex::Unlock()
{
if( m_LockCnt )
@@ -596,6 +612,51 @@ void LockMutex::Unlock()
}
}
RageSemaphore::RageSemaphore( CString sName, int iInitialValue ):
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();
}
void RageSemaphore::Wait()
{
if( m_pSema->Wait() )
return;
/* We waited too long. We're probably deadlocked, though unlike mutexes, we can't
* tell which thread we're stuck on. */
#if defined(CRASH_HANDLER)
const ThreadSlot *ThisSlot = GetThreadSlotFromID( GetThisThreadId() );
const CString sReason = ssprintf( "Semaphore timeout on mutex %s on thread %s",
GetName().c_str(), ThisSlot? ThisSlot->GetThreadName(): "(???" ")" ); // stupid trigraph warnings
ForceCrashHandler( sReason );
#else
RageException::Throw( "%s", sReason.c_str() );
#endif
}
bool RageSemaphore::TryWait()
{
return m_pSema->TryWait();
}
/*
* Copyright (c) 2001-2004 Glenn Maynard
* All rights reserved.
+20
View File
@@ -61,6 +61,7 @@ public:
CString GetName() const { return m_sName; }
void SetName( const CString &s ) { m_sName = s; }
void Lock();
bool TryLock();
void Unlock();
bool IsLockedByThisThread() const;
@@ -90,6 +91,7 @@ public:
void Unlock();
};
/* Double-abstracting __LINE__ lets us append it to other text, to generate
* locally unique variable names. (Otherwise we get "LocalLock__LINE__".) I'm
* not sure why this works, but it does, in both VC and GCC. */
@@ -117,6 +119,24 @@ public:
#define LockMut(m) LockMutex LocalLock(m, __FUNCTION__, __LINE__)
#endif
class SemaImpl;
class RageSemaphore
{
public:
RageSemaphore( CString sName, int iInitialValue = 0 );
~RageSemaphore();
CString GetName() const { return m_sName; }
int GetValue() const;
void Post();
void Wait();
bool TryWait();
private:
SemaImpl *m_pSema;
CString m_sName;
};
#endif
/*