///////////////////////////////////////////////////////////////////////////////
//
// File      : Asura_PS3_Mutex.h
// Author    : Philip Hall
// Created   : 19/10/2007
//
// Copyright (C) 2007 Rebellion. All Rights Reserved
//
///////////////////////////////////////////////////////////////////////////////

#if !defined __ASURA_PS3_MUTEX_H__
#define __ASURA_PS3_MUTEX_H__

// Asura headers
#include "Debugging/Asura_Assert.h"
#include "Debugging/Asura_LogFile.h"

#include <sys/synchronization.h>


////PGH/////////////////////////////////////////
// Mutex wrapper
////////////////////////////////////////////////									  
class Asura_PS3_Mutex
{
public:
	Asura_PS3_Mutex()
	{
		sys_mutex_attribute_t mutexAttr;
		sys_mutex_attribute_initialize(mutexAttr);
		int iResult = sys_mutex_create(&m_uMutex, &mutexAttr);
		if (iResult < CELL_OK)
		{
			Asura_Assert(false, "[Asura_PS3_WaitLock] - Unable to initialise mutex (this is probably fatal)\n");
		}
	}

	~Asura_PS3_Mutex()
	{
		Destroy();
	}

	// Destroy this mutex object
	void Destroy()
	{
		if ( m_uMutex != 0 )
		{
			sys_mutex_destroy(m_uMutex);
			m_uMutex = 0;
		}
	}

	// Lock this mutex with timeout
	bool Lock(u_int uTimeoutUS=0)
	{
		int iResult = sys_mutex_lock(m_uMutex, uTimeoutUS);
		return (iResult == CELL_OK);
	}

	// Unlock this mutex
	bool Unlock()
	{
		int iResult = sys_mutex_unlock(m_uMutex);
		return (iResult == CELL_OK);
	}

protected:
	sys_mutex_t	m_uMutex;
};




#endif






