Sophie

Sophie

distrib > PLD > ra > i686 > by-pkgid > 0ddb6ae038b55f1cc86ef23d80c93347 > files > 18

phoenix-devel-020415-2.i686.rpm

/*
    Mohawk Software Framework by Mohawk Software
    Copyright (C) 1998-2001 Mark L. Woodward
 
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.
 
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.
 
    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the Free
    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
    MA 02111-1307, USA
 
    If you want support or to commercially license this library, the author
    can be reached at info@mohawksoft.com
*/ 
#ifndef _MMUTEX_
#define _MMUTEX_

/** a class for encapsulatng a mutex */
class MMutex
{
	protected:
#ifdef WIN32
	HANDLE m_event;
	HANDLE m_mutex;
	DWORD  m_dwresult;
#else
	pthread_mutex_t	m_mutex;
	pthread_cond_t	m_condition;
#endif
	int		m_stat;

	public:
	MMutex(void);
	~MMutex();
	int stat(void) { return m_stat; }
	void wait(int sec=1, int usec=0);
	void signal(Boolean fAll);
	void lock(void);
	void unlock(void);
	int inc(int *var);
	int dec(int *var);
};

/** a class to lock and unlock a mutex within a function */
class AutoMutex
{
	MMutex *m_mtx;
	public:
	AutoMutex(MMutex *mtx)
	{
		m_mtx = mtx;
		m_mtx->lock();
	}
	~AutoMutex()
	{
		m_mtx->unlock();
	}
};

class MRWMutex
{
	protected:
	MMutex	m_mtxWrite;
	MMutex	m_mtxGateway;
	int	m_readers;
	int	m_writers;
	int	m_wrwait;

	public:
	MRWMutex();
	void LockRead();
	void UnlockRead();
	Boolean LockWrite(int wait = 30);
	void UnlockWrite();
	void UnlockWriteLockRead();
};

class AutoRMutex
{
	protected:
	MRWMutex *m_rwmtx;
	AutoRMutex(MRWMutex * rwmtx)
	{
		m_rwmtx = rwmtx;
		m_rwmtx->LockRead();
	}
	~AutoRMutex()
	{
		m_rwmtx->UnlockRead();
	}
};

#ifdef THREAD_SAFE
#define THREAD_SAFE_MTX		MMutex	_mtx_;
#define THREAD_SAFE_LOCK	AutoMutex amtx( &_mtx_)
#else
#define THREAD_SAFE_MTX	
#define THREAD_SAFE_LOCK
#endif

#endif