📄 platformsemaphore.h
字号:
//-----------------------------------------------------------------------------
// Torque Game Engine
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------
#ifndef _PLATFORMSEMAPHORE_H_
#define _PLATFORMSEMAPHORE_H_
#ifndef _TORQUE_TYPES_H_
#include "platform/types.h"
#endif
struct Semaphore
{
static void * createSemaphore(U32 initialCount = 1);
static void destroySemaphore(void * semaphore);
static bool acquireSemaphore(void * semaphore, bool block = true);
#ifdef TGE_RPG
static void releaseSemaphore(void * semaphore,U32 uCount=1);
#else
static void releaseSemaphore(void * semaphore);
#endif
inline static bool P(void * semaphore, bool block = true) {return(acquireSemaphore(semaphore, block));}
inline static void V(void * semaphore) {releaseSemaphore(semaphore);}
};
//#ifdef TGE_RPG
/// Platform independent semaphore class.
///
/// The semaphore class wraps OS specific semaphore functionality for thread synchronization.
class SemaphoreInstance
{
void* mSemaphore;
public:
/// Semaphore constructor - initialCount specifies how many wait calls
/// will be let through before an increment is required.
SemaphoreInstance(U32 uInitCount=0)
{
mSemaphore = Semaphore::createSemaphore(uInitCount);
}
~SemaphoreInstance()
{
Semaphore::destroySemaphore(mSemaphore);
mSemaphore = 0;
//increment();
}
/// Thread calling wait will block as long as the semaphore's count
/// is zero. If the semaphore is incremented, one of the waiting threads
/// will be awakened and the semaphore will decrement.
bool wait(bool bBlock);
/// Increments the semaphore's internal count. This will wake
/// count threads that are waiting on this semaphore.
void increment(U32 count = 1);
};
inline bool SemaphoreInstance::wait(bool bBlock)
{
if(mSemaphore == 0)
return false;
bool ret = Semaphore::acquireSemaphore(mSemaphore, bBlock);
return ret;
}
inline void SemaphoreInstance::increment(U32 count)
{
if(mSemaphore == 0)
return;
//AssertFatal(mSemaphore, "SemaphoreInstance::increment - didn't have a mutex to unlock!");
Semaphore::releaseSemaphore(mSemaphore, count);
}
//#endif//#ifdef TGE_RPG
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -