📄 platformmutex.h
字号:
//-----------------------------------------------------------------------------
// Torque Game Engine
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------
#ifndef _PLATFORMMUTEX_H_
#define _PLATFORMMUTEX_H_
#ifndef _PLATFORMASSERT_H_
#include "platform/platformAssert.h"
#endif
// TODO: change this to a proper class.
struct Mutex
{
static void* createMutex( void );
static void destroyMutex( void* );
static bool lockMutex( void *mutex, bool block = true);
static void unlockMutex( void* );
};
#ifdef TGE_RPG
class MutexInstance
{
public:
void* m_pMutex;
MutexInstance() {m_pMutex = Mutex::createMutex();}
~MutexInstance() {Mutex::destroyMutex(m_pMutex);}
void* getMutex();
bool lock(bool blocking=false);
void unlock();
};
inline void* MutexInstance::getMutex()
{
return m_pMutex;
}
inline bool MutexInstance::lock(bool blocking)
{
return Mutex::lockMutex(m_pMutex, blocking);
}
inline void MutexInstance::unlock()
{
Mutex::unlockMutex(m_pMutex);
}
#endif
/// Helper for simplifying mutex locking code.
///
/// This class will automatically unlock a mutex that you've
/// locked through it, saving you from managing a lot of complex
/// exit cases. For instance:
///
/// @code
/// MutexHandle handle;
/// handle.lock(myMutex);
///
/// if(error1)
/// return; // Auto-unlocked by handle if we leave here - normally would
/// // leave the mutex locked, causing much pain later.
///
/// handle.unlock();
/// @endcode
class MutexHandle
{
private:
void *mMutexPtr;
public:
MutexHandle()
: mMutexPtr(NULL)
{
}
#ifdef TGE_RPG
MutexHandle(void *mutex, bool blocking=false)
: mMutexPtr(NULL)
{
lock(mutex, blocking);
}
MutexHandle(MutexInstance& inst, bool blocking=false)
: mMutexPtr(NULL)
{
lock(inst.getMutex(), blocking);
}
#endif
~MutexHandle()
{
if(mMutexPtr)
unlock();
}
bool lock(void *mutex, bool blocking=false)
{
AssertFatal(!mMutexPtr, "MutexHandle::lock - shouldn't be locking things twice!");
bool ret = Mutex::lockMutex(mutex, blocking);
if(ret)
{
// We succeeded, do book-keeping.
mMutexPtr = mutex;
}
return ret;
}
void unlock()
{
AssertFatal(mMutexPtr, "MutexHandle::unlock - didn't have a mutex to unlock!");
Mutex::unlockMutex(mMutexPtr);
mMutexPtr = NULL;
}
};
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -