/****************************************************************************************************

		MCSMPSafeCriticalSections.h
		Copyright: (c) 2002 Eovia Corp. All rights reserved.

		Author:	Arnaud
		Date:	7/28/2007

****************************************************************************************************/

#ifndef __MCSMPSafeCriticalSections__
#define __MCSMPSafeCriticalSections__

#if CP_PRAGMA_ONCE
#pragma once
#endif

#ifdef qIsInExtension
#include "IShThreadUtilities.h"
#include "COMSafeUtilities.h"
#endif
#include "MCThread.h" // Needed in any case for kUndefinedThreadID



///This class is intended to protect cooperative threads (and preemptive ones)
///on the pc cooperative threads are not affected by critical sections, 
///on the mac they deadlock. Both cases are bad.
///For now this class is to protect a single function. 
///When we'll need the same thread to enter twice in the section, we'll need to store the cooperative thread id.
///
/// \deprecated use TMCSafeCriticalSection instead
class TMCSafeSection : public TMCObject
{
public:
	TMCSafeSection	()	{ fMainThreadInside= false;		fCS = NewCS(); }
	~TMCSafeSection ()	{ MCAssert(!fMainThreadInside); DeleteCS(fCS); }

	inline TMCCriticalSection*	GetCS() { return fCS; }

	inline boolean		IsMainThreadInside	() const			{ return fMainThreadInside; }
	inline void			SetMainThreadInside	(boolean value)		{ fMainThreadInside= value; }

	inline void			Enter				()					{ EnterCS(fCS); }
	inline void			Leave				()					{ LeaveCS(fCS); }

protected:
	TMCCriticalSection*	fCS;
	boolean				fMainThreadInside;
};


///
/// \deprectaed use CWhileInSafeCS2 instead
class CWhileInSafeCS : public TMCObject
{
public:
	CWhileInSafeCS	(TMCSafeSection& theCS) : fCS(theCS), fIsInMainSMPProcess(IsInMainSMPProcess())
	{		
		if (MCVerify(&fCS && fCS.GetCS()))
		{
			if (fIsInMainSMPProcess)
			{
				while(fCS.IsMainThreadInside())
				{
					//we need to wait
#ifdef qIsInExtension
					gShellThreadUtilities->YieldProcesses(0);
#else
					TMCThreadUtilities::YieldThreadsNow();
#endif
				}

				fCS.SetMainThreadInside(true);
				fCS.Enter();
			}
			else
			{
				fCS.Enter();
			}
		}
	}

	~CWhileInSafeCS()
	{ 
		if (MCVerify(fCS.GetCS()))
		{
			if (fIsInMainSMPProcess)
			{
				fCS.SetMainThreadInside(false);
				fCS.Leave();
			}
			else
			{
				fCS.Leave();
			}
		}
	}

protected:
	TMCSafeSection& fCS;
	boolean			fIsInMainSMPProcess;
};



///This class is intended to protect cooperative threads (and preemptive ones)
///on the pc cooperative threads are not affected by critical sections, 
///on the mac they deadlock. Both cases are bad.
class TMCSafeCriticalSection : public TMCObject
{
public:
	TMCSafeCriticalSection	()	{ fThreadID = kUndefinedThreadID;		fCS = NewCS(); }
	~TMCSafeCriticalSection ()	{ MCAssert(fThreadID == kUndefinedThreadID); DeleteCS(fCS); }

	inline TMCCriticalSection*	GetCS() { return fCS; }

	int32			Enter(boolean reentrant);
	void			Leave(int32 reentrantThread);

protected:
	TMCCriticalSection*	fCS;
	int32				fThreadID;
};


class CWhileInSafeCS2 : public TMCObject
{
public:
	CWhileInSafeCS2	(TMCSafeCriticalSection& theCS, boolean reentrant) : fCS(theCS)
	{		
		if (MCVerify(&fCS))
		{
			fReentrantThreadId = fCS.Enter(reentrant);
		}
	}

	~CWhileInSafeCS2()
	{ 
		if (MCVerify(&fCS))
		{
			fCS.Leave(fReentrantThreadId);
		}
	}

protected:
	TMCSafeCriticalSection& fCS;
	int32					fReentrantThreadId;
};




#endif

