mirror of
https://foundry.openuru.org/gitblit/r/CWE-ou-minkata.git
synced 2025-07-17 18:59:09 +00:00
Move the old IPC-based semaphore to hsGlobalSemaphore, and provide a
"normal" (inter-thread only) semaphore class.
This commit is contained in:
@ -107,8 +107,44 @@ public:
|
||||
};
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
class hsSemaphore
|
||||
{
|
||||
std::mutex fMutex;
|
||||
std::condition_variable fCondition;
|
||||
unsigned fValue;
|
||||
|
||||
class hsSemaphore {
|
||||
public:
|
||||
hsSemaphore(unsigned initial = 0) : fValue(initial) { }
|
||||
|
||||
inline void Wait()
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(fMutex);
|
||||
fCondition.wait(lock, [this]() { return fValue > 0; });
|
||||
--fValue;
|
||||
}
|
||||
|
||||
template <class _Rep, class _Period>
|
||||
inline bool Wait(const std::chrono::duration<_Rep, _Period> &duration)
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(fMutex);
|
||||
|
||||
bool result = fCondition.wait_for(lock, duration, [this]() { return fValue > 0; });
|
||||
if (result)
|
||||
--fValue;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
inline void Signal()
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(fMutex);
|
||||
++fValue;
|
||||
fCondition.notify_one();
|
||||
}
|
||||
};
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
class hsGlobalSemaphore {
|
||||
#if HS_BUILD_FOR_WIN32
|
||||
HANDLE fSemaH;
|
||||
#elif HS_BUILD_FOR_UNIX
|
||||
@ -122,8 +158,8 @@ class hsSemaphore {
|
||||
#endif
|
||||
#endif
|
||||
public:
|
||||
hsSemaphore(int initialValue=0, const char* name=nullptr);
|
||||
~hsSemaphore();
|
||||
hsGlobalSemaphore(int initialValue = 0, const char* name = nullptr);
|
||||
~hsGlobalSemaphore();
|
||||
|
||||
#ifdef HS_BUILD_FOR_WIN32
|
||||
HANDLE GetHandle() const { return fSemaH; }
|
||||
|
@ -115,19 +115,19 @@ void hsThread::ThreadYield()
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
hsSemaphore::hsSemaphore(int initialValue)
|
||||
hsGlobalSemaphore::hsGlobalSemaphore(int initialValue)
|
||||
{
|
||||
OSStatus status = MPCreateSemaphore(kPosInfinity32, initialValue, &fSemaId);
|
||||
hsThrowIfOSErr(status);
|
||||
}
|
||||
|
||||
hsSemaphore::~hsSemaphore()
|
||||
hsGlobalSemaphore::~hsGlobalSemaphore()
|
||||
{
|
||||
OSStatus status = MPDeleteSemaphore(fSemaId);
|
||||
hsThrowIfOSErr(status);
|
||||
}
|
||||
|
||||
bool hsSemaphore::Wait(hsMilliseconds timeToWait)
|
||||
bool hsGlobalSemaphore::Wait(hsMilliseconds timeToWait)
|
||||
{
|
||||
Duration duration;
|
||||
|
||||
@ -145,7 +145,7 @@ bool hsSemaphore::Wait(hsMilliseconds timeToWait)
|
||||
return true;
|
||||
}
|
||||
|
||||
void hsSemaphore::Signal()
|
||||
void hsGlobalSemaphore::Signal()
|
||||
{
|
||||
OSStatus status = MPSignalSemaphore(fSemaId);
|
||||
hsThrowIfOSErr(status);
|
||||
|
@ -192,7 +192,7 @@ static void InitEventLoggingFile()
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
hsSemaphore::hsSemaphore(int initialValue, const char* name)
|
||||
hsGlobalSemaphore::hsGlobalSemaphore(int initialValue, const char* name)
|
||||
{
|
||||
#ifdef USE_SEMA
|
||||
fPSema = nil;
|
||||
@ -222,7 +222,7 @@ hsSemaphore::hsSemaphore(int initialValue, const char* name)
|
||||
#endif
|
||||
}
|
||||
|
||||
hsSemaphore::~hsSemaphore()
|
||||
hsGlobalSemaphore::~hsGlobalSemaphore()
|
||||
{
|
||||
#ifdef USE_SEMA
|
||||
int status = 0;
|
||||
@ -242,7 +242,7 @@ hsSemaphore::~hsSemaphore()
|
||||
#endif
|
||||
}
|
||||
|
||||
bool hsSemaphore::Wait(hsMilliseconds timeToWait)
|
||||
bool hsGlobalSemaphore::Wait(hsMilliseconds timeToWait)
|
||||
{
|
||||
#ifdef USE_SEMA // SHOULDN'T THIS USE timeToWait??!?!? -rje
|
||||
// shouldn't this use sem_timedwait? -dpogue (2012-03-04)
|
||||
@ -294,7 +294,7 @@ EXIT:
|
||||
#endif
|
||||
}
|
||||
|
||||
void hsSemaphore::Signal()
|
||||
void hsGlobalSemaphore::Signal()
|
||||
{
|
||||
#ifdef USE_SEMA
|
||||
int status = sem_post(fPSema);
|
||||
|
@ -135,19 +135,19 @@ void hsThread::ThreadYield()
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
hsSemaphore::hsSemaphore(int initialValue, const char *name)
|
||||
hsGlobalSemaphore::hsGlobalSemaphore(int initialValue, const char *name)
|
||||
{
|
||||
fSemaH = ::CreateSemaphore(nil, initialValue, kPosInfinity32, name);
|
||||
if (fSemaH == nil)
|
||||
throw hsOSException(-1);
|
||||
}
|
||||
|
||||
hsSemaphore::~hsSemaphore()
|
||||
hsGlobalSemaphore::~hsGlobalSemaphore()
|
||||
{
|
||||
::CloseHandle(fSemaH);
|
||||
}
|
||||
|
||||
bool hsSemaphore::Wait(hsMilliseconds timeToWait)
|
||||
bool hsGlobalSemaphore::Wait(hsMilliseconds timeToWait)
|
||||
{
|
||||
if (timeToWait == kPosInfinity32)
|
||||
timeToWait = INFINITE;
|
||||
@ -162,7 +162,7 @@ bool hsSemaphore::Wait(hsMilliseconds timeToWait)
|
||||
}
|
||||
}
|
||||
|
||||
void hsSemaphore::Signal()
|
||||
void hsGlobalSemaphore::Signal()
|
||||
{
|
||||
::ReleaseSemaphore(fSemaH, 1, nil);
|
||||
}
|
||||
|
@ -53,8 +53,8 @@ void plCrashBase::IInit(const char* file)
|
||||
{
|
||||
char sema[128];
|
||||
snprintf(sema, arrsize(sema), "%s-%s", file, CRASH_NOTIFY_SUFFIX);
|
||||
fCrashed = new hsSemaphore(0, sema);
|
||||
fCrashed = new hsGlobalSemaphore(0, sema);
|
||||
|
||||
snprintf(sema, arrsize(sema), "%s-%s", file, CRASH_HANDLE_SUFFIX);
|
||||
fHandled = new hsSemaphore(0, sema);
|
||||
fHandled = new hsGlobalSemaphore(0, sema);
|
||||
}
|
||||
|
@ -48,8 +48,8 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com
|
||||
class plCrashBase
|
||||
{
|
||||
protected:
|
||||
hsSemaphore* fCrashed;
|
||||
hsSemaphore* fHandled;
|
||||
hsGlobalSemaphore* fCrashed;
|
||||
hsGlobalSemaphore* fHandled;
|
||||
|
||||
~plCrashBase();
|
||||
void IInit(const char* file);
|
||||
|
@ -277,14 +277,14 @@ plStatusLog::plStatusLog( uint8_t numDisplayLines, const plFileName &filename, u
|
||||
if (filename.IsValid())
|
||||
{
|
||||
fFilename = filename;
|
||||
fSema = new hsSemaphore(1, fFilename.AsString().c_str());
|
||||
fSema = new hsGlobalSemaphore(1, fFilename.AsString().c_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
fFilename = "";
|
||||
flags |= kDontWriteFile;
|
||||
|
||||
fSema = new hsSemaphore(1);
|
||||
fSema = new hsGlobalSemaphore(1);
|
||||
}
|
||||
|
||||
fOrigFlags = fFlags = flags;
|
||||
|
@ -86,7 +86,7 @@ class plStatusLog
|
||||
plFileName fFilename;
|
||||
char** fLines;
|
||||
uint32_t* fColors;
|
||||
hsSemaphore* fSema;
|
||||
hsGlobalSemaphore* fSema;
|
||||
FILE* fFileHandle;
|
||||
uint32_t fSize;
|
||||
bool fForceLog;
|
||||
|
Reference in New Issue
Block a user