#include "dfixer_lib.h"


Lock preAcqLock;
set<Pair> preAcqSet;

Lock hwCondLock;
set<Pair> hwCondSet;

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

/**	set preAcquisition condition */
void setPreAcqTaken(Lock * lock)
{
	Pair key = Pair(TID, (uint) lock);
	
	if(preAcqSet.find(key) == preAcqSet.end() )
	{
		acq( &preAcqLock);
		preAcqSet.insert(key);
		rel( &preAcqLock);
	}
}

/**	get preAcquisition condition */
bool preAcqTaken(Lock* lock)
{
	Pair key = Pair(TID, (uint) lock);
	return preAcqSet.find(key) != preAcqSet.end();
}

/**	clear preAcquisition condition */
void clearPreAcq(Lock* lock)
{
	Pair key = Pair(TID, (uint) lock);
	set<Pair>::iterator it = preAcqSet.find(key);
	if(it != preAcqSet.end())
	{
		acq( &preAcqLock);
		preAcqSet.erase( it );
		rel( &preAcqLock);
	}
}


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

/**	set v_hw condition */
void setHWCondOn(Lock* hLock, Lock* wLock)
{
	Pair key = Pair((uint)hLock, (uint) wLock);
	
	if( hwCondSet.find(key) == hwCondSet.end() )
	{
		acq( &hwCondLock);
		hwCondSet.insert(key);
		rel( &hwCondLock);
	}
}


/**	get v_hw condition */
bool getHWCond(Lock* hLock, Lock* wLock)
{
	Pair key = Pair((uint)hLock, (uint) wLock);
	return hwCondSet.find(key) != hwCondSet.end();
}

/**	clear v_hw condition */
void setHWCondOff(Lock* hLock, Lock* wLock)
{
	Pair key = Pair((uint)hLock, (uint) wLock);
	set<Pair>::iterator it = hwCondSet.find(key);
	
	if( it != hwCondSet.end())
	{
		acq( &hwCondLock);
		hwCondSet.erase(key);
		rel( &hwCondLock);
	}
}

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

/**	preAcquisition */
bool preAcq_pthread_mutex_lock (Lock* hLock, Lock* wLock)
{
	while( tryAcq(hLock) || tryAcq(wLock) )
	{
		rel(hLock);
		rel(wLock);
	}
	return true;
}


