/*
	ZMutex.hpp
	Author: James Russell <jcrussell@762studios.com>

	Purpose: RAII Wrapper for libsst-concurrency SST_Mutex objects.

	Changelog
	2011/11/27 - creation (jcrussell)
*/

#pragma once

#ifndef _ZMUTEX_H
#define _ZMUTEX_H

#include <ZUtil/ZUtilBuild.hpp>

#include <SST/SST_Mutex.h>

/*
ZMutex class.  Used to allocate and deallocate a mutex within the scope of a class.
Cannot be copied or assigned, which prevents it from being used in stl-like containers.
*/
class ZMutex
{
private:
	DISABLE_COPY_AND_ASSIGN(ZMutex);

	//libsst Mutex
	SST_Mutex Mutex;

public:
	/*
	Constructor.  Creates a mutex from libsst-concurrency.
	*/
	ZMutex();

	/*
	Destructor.  Frees our mutex from libsst-concurrency.
	*/
	~ZMutex();

	/*
	public ZLock::Acquire
	
	Acquires this lock, which locks the contained mutex.
	
	@return (void)
	@context (all)
	*/
	void Acquire();

	/*
	public ZLock::Release
	
	Releases this lock, which unlocks the contained mutex.
	
	@return (void)
	@context (all)
	*/
	void Release();
};

/*
Scoped lock.  Acquires the provided lock so long as it remains in scope.  Releases the lock when
it goes out of scope.
*/
class ZLock
{
private:
	DISABLE_COPY_AND_ASSIGN(ZLock);

	//The Mutex
	ZMutex& Mutex;

public:
	/*
	Constructor.  Acquires the mutex during construction.

	@param _mutex - the mutex we are to acquire
	*/
	ZLock(ZMutex& _lock);

	/*
	Destructor.  Releases the mutex when the object is destructed (goes out of scope).
	*/
	~ZLock();
};

#endif

