/*<<<***************************************************************************
The MIT License (MIT)

Copyright(C) 2013 - 2015 Marc Mirti Mancinelli, Christophe Lardau and Philippe Pamart (xdcube.org)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
////////////////////////////////////////////////////////////////////////////////
#Title: xbODthreadLow
#Project: xdcBase
////////////////////////////////////////////////////////////////////////////////
#Author: Marc MIRTI MANCINELLI
#History:
<17/08/2013: creation.
12/02/2014: this notice inserted.
***************************************************************************>>>*/
#include "stdafx.h"

#ifdef xbOS_MSWIN
#include "xbODthreadLow.h"
#include <process.h> 

xbThreadLowBase::xbThreadLowBase()
{
	m_hThread = NULL;
	m_nThreadID = 0;
}

xbThreadLowBase::~xbThreadLowBase()
{
	eProtectTrace(Destroy(););
}
void xbThreadLowBase::Destroy()
{
	// free thread object
	if (m_hThread == NULL)
		return;
	if (!CloseHandle(m_hThread))
		xbThrowEsys(xbHR_LAST,"xbThreadLowBase::Destroy() failure");
	m_hThread = NULL;
}

UINT APIENTRY xbThreadLowBaseEntry(void* pParam)
{
	eProtectBegin();
	xbThreadLowBase* pThread = (xbThreadLowBase*)pParam;
	xbRdbgAssert(pThread != NULL);
	return pThread->Run();
	eProtectEndTrace();
	return -1;
}
bool xbThreadLowBase::CreateThread(DWORD dwCreateFlags, UINT nStackSize,
	LPSECURITY_ATTRIBUTES lpSecurityAttrs)
{
	xbRdbgAssert(m_hThread == NULL);
	// create the thread (it may or may not start to run)
	m_hThread = (HANDLE)(ULONG_PTR)_beginthreadex(lpSecurityAttrs, nStackSize,  
		&xbThreadLowBaseEntry, this, dwCreateFlags, (UINT*)&m_nThreadID);
	if (m_hThread == NULL)
		return false;
	return true;
}

#endif
