///////////////////////////////////////////////////////////////////////////////
/// @file deThread.hpp
///
/// @brief 
///
/// @author trajar
///
/// This file is the intellectual property of Novus Delta, LLC.. Usage of the
/// contents of this file is subject to the Destiny3D Member License which
/// can be found at http://www.destiny3d.com.  Any other usage is prohibited.
///
/// This file is distributed "AS IS" without warranty of any kind.  Novus
/// Delta, LLC. does not guarantee the fitness of the contents of this file
/// for any particular purpose.
///
/// Copyright (C) 2001-2003 Novus Delta, LLC. All Rights Reserved.
///
/// <hr>
///                                 Change History
/// <hr>
///
/// @date Jun 2002
/// @author trajar
/// @remarks Creation
///
///////////////////////////////////////////////////////////////////////////////

#ifndef _DETHREAD_HPP
#define _DETHREAD_HPP

#ifndef		_DENET_HPP
 #include	"deNet.hpp"
#endif

#ifndef		_DESYNCHOBJECT_HPP
 #include	"deSynchObject.hpp"
#endif

#include <WINDOWS.H>

class deThreadPool;

class DENET_API deThread
{
public:

		//--------------------------------------------------------------------
		// enum eStatus - Thread statues
		//--------------------------------------------------------------------
		enum eStatus
		{
			STATUS_UNKNOWN	= -3,
			STATUS_NONE		= -2,
			STATUS_SLEEP	= -1,
			STATUS_RUN		= 0,
			STATUS_WAKEUP,			
			STATUS_STOP,
			STATUS_DEAD,
			STATUS_MAX_NUMBER,
		};

		typedef DWORD (* ActiveProcess)( void * );

		deThread();
		virtual ~deThread();

		bool		Start ( void );
		DWORD		Stop  ( bool bForceKill = false );
		void		AttachProcess( ActiveProcess ptr, void * param );
		void		DetachProcess(void);
		bool		Sleep ( void );
		bool		Wakeup( void );

		DWORD		GetExitCode();
		eStatus		GetStatus(void) const;
		
protected:

		//--------------------------------------------------------------------
		// DWORD Run( void * )
		//--------------------------------------------------------------------
		static DWORD WINAPI Run( LPVOID param );
	
protected:

		//--------------------------------------------------------------------
		// struct ThreadContext
		//--------------------------------------------------------------------
		class ThreadContext
		{
			friend class deThread;
		public:

			ThreadContext(void)	: m_pParent(NULL), m_ProcessParam(NULL), m_AttachedProcess(NULL)			{ }
			virtual ~ThreadContext(void)	{ }

		protected:

			HANDLE			m_hThread;						//	The Thread Handle
			DWORD			m_dwTID;						//	The Thread ID
			deThread *		m_pParent;						//	The this pointer of the parent CThread object
			DWORD			m_dwExitCode;					//	The Exit Code of the thread
			HANDLE			m_Events[STATUS_MAX_NUMBER];
			HANDLE			m_ProcessChangeEvent;
			void	 *		m_ProcessParam;
			ActiveProcess 	m_AttachedProcess;
		};

		static deSynchObject	m_SynchObject;
		eStatus					m_Status;
		ThreadContext			m_ThreadCtx;	//	The Thread Context member
		LPTHREAD_START_ROUTINE	m_pThreadFunc;	//	The Worker Thread Function Pointer
};

#endif