// mutex.h :
//

#if !defined(AGD_MUTEX_H__307A751D_EF2D_45D6_BBA8_2EB4B79B548D__INCLUDED_)
#define AGD_MUTEX_H__307A751D_EF2D_45D6_BBA8_2EB4B79B548D__INCLUDED_

#include <inttypes.h>
#include <stdlib.h>
#include <pthread.h>
#include "uuid.h"

/////////////////////////////////////////////////////////////////////////////
// mutex.h - Declarations:

#ifdef _WIN32
#define _DEF_MUTEX_DIR						"."
#endif	//	_WIN32

#ifdef __linux__
#define _DEF_MUTEX_DIR						"/tmp"
#endif	//	__linux__

/////////////////////////////////////////////////////////////////////////////

class CGlobalMutex
{
public:
	CGlobalMutex(void);
	virtual ~CGlobalMutex(void);

	int Create(const uuid_t &ruuid, const char *pszDir = _DEF_MUTEX_DIR);
	long Release(void);

	bool Lock(void);
	bool TryLock(void);
	bool Unlock(void);

private:
	int m_nShmID;
	pthread_mutexattr_t m_mutexAttr;
	pthread_mutex_t *m_pMutex;
};

/////////////////////////////////////////////////////////////////////////////

class CLocalMutex
{
public:
	CLocalMutex(void);
	virtual ~CLocalMutex(void);

	bool Create(bool bRecursive = false);
	void Release(void);

	bool Lock(void);
	bool TryLock(void);
	bool Unlock(void);

private:
	bool m_bInit;
	pthread_mutexattr_t m_mutexAttr;
	pthread_mutex_t m_mutex;
};

/////////////////////////////////////////////////////////////////////////////
#endif	//	!defined(AGD_MUTEX_H__307A751D_EF2D_45D6_BBA8_2EB4B79B548D__INCLUDED_)
