// Queue.cpp: implementation of the CQueue class.
//
//////////////////////////////////////////////////////////////////////

#include "Queue.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CQueue::CQueue()
{
	m_pHead = m_pTail = NULL;
}

CQueue::~CQueue()
{

}

void CQueue::Enqueue(PCQUEUE_GEN_HEADER	pObject)
{
	HALT(!pObject);

	m_SpinLock.Lock();
	pObject->pNext = NULL;
	if(m_pTail == NULL)
	{
		m_pHead = m_pTail = pObject;
	}
	else	
	{
		m_pTail->pNext = pObject;
		m_pTail = pObject;
	}
	m_SpinLock.Release();
}

PCQUEUE_GEN_HEADER	CQueue::Dequeue(void)
{
	PCQUEUE_GEN_HEADER	pcurr = m_pHead;
	m_SpinLock.Lock();
	if(m_pHead != NULL) 
	{
		if(m_pHead == m_pTail)
			m_pHead = m_pTail = NULL;
		else
			m_pHead = m_pHead->pNext;
	}
	m_SpinLock.Release();
	return pcurr;
}

PCQUEUE_GEN_HEADER	CQueue::GetHead(void) 
{
	return m_pHead;
}

int	CQueue::IsQueueEmpty(void) 
{ 
	return (m_pHead == NULL); 
}

int	CQueue::Size(void) 
{
	int	size;
	PCQUEUE_GEN_HEADER	pcurr;
	
	for(size=0,pcurr=m_pHead;
		pcurr;
		pcurr=pcurr->pNext,size++) ;
		
	return size;
}