Newer
Older
Import / projects / Gameloft / glwebtools / source / glwebtools / glwebtools_mutex_glf.cpp
#include <glwebtools/internal/glwebtools_default_config.h>

#if GLWEBTOOLS_USE_GLF_MUTEX

#include <glwebtools/os/glwebtools_mutex.h>
#include <glwebtools/internal/glwebtools_memory.h>
#include <glwebtools/internal/glwebtools_macro.h>

#include <glf/core/mutex.h>

namespace glwebtools
{
	Mutex::Mutex()
	{
		m_handler = static_cast<void*>(GLWEBTOOLS_NEW glf::Mutex());
#ifdef GLWEBTOOLS_DEBUG
		if(!m_handler)
		{
			GLWEBTOOLS_ASSERT(m_handler);
			GLWEBTOOLS_LOG_MAJOR_ERROR("%s", "Could not allocate mutex, operation may not be thread safe");
		}
#endif
	}

	Mutex::~Mutex()
	{
		if(m_handler)
		{
			glf::Mutex* mutex = static_cast<glf::Mutex*>(m_handler);
			GLWEBTOOLS_DELETE(mutex);
			m_handler = 0;
		}
	}
	
	void Mutex::Lock()
	{
		if(m_handler)
		{
			glf::Mutex* mutex = static_cast<glf::Mutex*>(m_handler);
			mutex->Lock();
		}
#ifdef GLWEBTOOLS_DEBUG
		else
		{
			GLWEBTOOLS_LOG_MAJOR_ERROR("%s", "no mutex to lock, operation may not be thread safe");
		}
#endif
	}

	void Mutex::Unlock()
	{
		if(m_handler)
		{
			glf::Mutex* mutex = static_cast<glf::Mutex*>(m_handler);
			return mutex->Unlock();
		}
	}
	
	bool Mutex::TryLock()
	{
		if(m_handler)
		{
			glf::Mutex* mutex = static_cast<glf::Mutex*>(m_handler);
			return mutex->TryLock();
		}
		else
		{
#ifdef GLWEBTOOLS_DEBUG
			GLWEBTOOLS_LOG_MAJOR_ERROR("%s", "no mutex to trylock, operation may not be thread safe");
#endif
			return true; //must return true or we'll get stuck indefinetly
		}
	}
}

#endif //GLWEBTOOLS_USE_GLF_MUTEX