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
|
#if HS_BUILD_FOR_WIN32
|
||||||
HANDLE fSemaH;
|
HANDLE fSemaH;
|
||||||
#elif HS_BUILD_FOR_UNIX
|
#elif HS_BUILD_FOR_UNIX
|
||||||
@ -122,8 +158,8 @@ class hsSemaphore {
|
|||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
public:
|
public:
|
||||||
hsSemaphore(int initialValue=0, const char* name=nullptr);
|
hsGlobalSemaphore(int initialValue = 0, const char* name = nullptr);
|
||||||
~hsSemaphore();
|
~hsGlobalSemaphore();
|
||||||
|
|
||||||
#ifdef HS_BUILD_FOR_WIN32
|
#ifdef HS_BUILD_FOR_WIN32
|
||||||
HANDLE GetHandle() const { return fSemaH; }
|
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);
|
OSStatus status = MPCreateSemaphore(kPosInfinity32, initialValue, &fSemaId);
|
||||||
hsThrowIfOSErr(status);
|
hsThrowIfOSErr(status);
|
||||||
}
|
}
|
||||||
|
|
||||||
hsSemaphore::~hsSemaphore()
|
hsGlobalSemaphore::~hsGlobalSemaphore()
|
||||||
{
|
{
|
||||||
OSStatus status = MPDeleteSemaphore(fSemaId);
|
OSStatus status = MPDeleteSemaphore(fSemaId);
|
||||||
hsThrowIfOSErr(status);
|
hsThrowIfOSErr(status);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool hsSemaphore::Wait(hsMilliseconds timeToWait)
|
bool hsGlobalSemaphore::Wait(hsMilliseconds timeToWait)
|
||||||
{
|
{
|
||||||
Duration duration;
|
Duration duration;
|
||||||
|
|
||||||
@ -145,7 +145,7 @@ bool hsSemaphore::Wait(hsMilliseconds timeToWait)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void hsSemaphore::Signal()
|
void hsGlobalSemaphore::Signal()
|
||||||
{
|
{
|
||||||
OSStatus status = MPSignalSemaphore(fSemaId);
|
OSStatus status = MPSignalSemaphore(fSemaId);
|
||||||
hsThrowIfOSErr(status);
|
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
|
#ifdef USE_SEMA
|
||||||
fPSema = nil;
|
fPSema = nil;
|
||||||
@ -222,7 +222,7 @@ hsSemaphore::hsSemaphore(int initialValue, const char* name)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
hsSemaphore::~hsSemaphore()
|
hsGlobalSemaphore::~hsGlobalSemaphore()
|
||||||
{
|
{
|
||||||
#ifdef USE_SEMA
|
#ifdef USE_SEMA
|
||||||
int status = 0;
|
int status = 0;
|
||||||
@ -242,7 +242,7 @@ hsSemaphore::~hsSemaphore()
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
bool hsSemaphore::Wait(hsMilliseconds timeToWait)
|
bool hsGlobalSemaphore::Wait(hsMilliseconds timeToWait)
|
||||||
{
|
{
|
||||||
#ifdef USE_SEMA // SHOULDN'T THIS USE timeToWait??!?!? -rje
|
#ifdef USE_SEMA // SHOULDN'T THIS USE timeToWait??!?!? -rje
|
||||||
// shouldn't this use sem_timedwait? -dpogue (2012-03-04)
|
// shouldn't this use sem_timedwait? -dpogue (2012-03-04)
|
||||||
@ -294,7 +294,7 @@ EXIT:
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void hsSemaphore::Signal()
|
void hsGlobalSemaphore::Signal()
|
||||||
{
|
{
|
||||||
#ifdef USE_SEMA
|
#ifdef USE_SEMA
|
||||||
int status = sem_post(fPSema);
|
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);
|
fSemaH = ::CreateSemaphore(nil, initialValue, kPosInfinity32, name);
|
||||||
if (fSemaH == nil)
|
if (fSemaH == nil)
|
||||||
throw hsOSException(-1);
|
throw hsOSException(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
hsSemaphore::~hsSemaphore()
|
hsGlobalSemaphore::~hsGlobalSemaphore()
|
||||||
{
|
{
|
||||||
::CloseHandle(fSemaH);
|
::CloseHandle(fSemaH);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool hsSemaphore::Wait(hsMilliseconds timeToWait)
|
bool hsGlobalSemaphore::Wait(hsMilliseconds timeToWait)
|
||||||
{
|
{
|
||||||
if (timeToWait == kPosInfinity32)
|
if (timeToWait == kPosInfinity32)
|
||||||
timeToWait = INFINITE;
|
timeToWait = INFINITE;
|
||||||
@ -162,7 +162,7 @@ bool hsSemaphore::Wait(hsMilliseconds timeToWait)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void hsSemaphore::Signal()
|
void hsGlobalSemaphore::Signal()
|
||||||
{
|
{
|
||||||
::ReleaseSemaphore(fSemaH, 1, nil);
|
::ReleaseSemaphore(fSemaH, 1, nil);
|
||||||
}
|
}
|
||||||
|
@ -53,8 +53,8 @@ void plCrashBase::IInit(const char* file)
|
|||||||
{
|
{
|
||||||
char sema[128];
|
char sema[128];
|
||||||
snprintf(sema, arrsize(sema), "%s-%s", file, CRASH_NOTIFY_SUFFIX);
|
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);
|
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
|
class plCrashBase
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
hsSemaphore* fCrashed;
|
hsGlobalSemaphore* fCrashed;
|
||||||
hsSemaphore* fHandled;
|
hsGlobalSemaphore* fHandled;
|
||||||
|
|
||||||
~plCrashBase();
|
~plCrashBase();
|
||||||
void IInit(const char* file);
|
void IInit(const char* file);
|
||||||
|
@ -277,14 +277,14 @@ plStatusLog::plStatusLog( uint8_t numDisplayLines, const plFileName &filename, u
|
|||||||
if (filename.IsValid())
|
if (filename.IsValid())
|
||||||
{
|
{
|
||||||
fFilename = filename;
|
fFilename = filename;
|
||||||
fSema = new hsSemaphore(1, fFilename.AsString().c_str());
|
fSema = new hsGlobalSemaphore(1, fFilename.AsString().c_str());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
fFilename = "";
|
fFilename = "";
|
||||||
flags |= kDontWriteFile;
|
flags |= kDontWriteFile;
|
||||||
|
|
||||||
fSema = new hsSemaphore(1);
|
fSema = new hsGlobalSemaphore(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
fOrigFlags = fFlags = flags;
|
fOrigFlags = fFlags = flags;
|
||||||
|
@ -86,7 +86,7 @@ class plStatusLog
|
|||||||
plFileName fFilename;
|
plFileName fFilename;
|
||||||
char** fLines;
|
char** fLines;
|
||||||
uint32_t* fColors;
|
uint32_t* fColors;
|
||||||
hsSemaphore* fSema;
|
hsGlobalSemaphore* fSema;
|
||||||
FILE* fFileHandle;
|
FILE* fFileHandle;
|
||||||
uint32_t fSize;
|
uint32_t fSize;
|
||||||
bool fForceLog;
|
bool fForceLog;
|
||||||
|
Reference in New Issue
Block a user