/** SGX

The   sss    gggg x   x   
     s      g      x x    Engine
      sss   g  gg   x
         s  g   g  x x          Copyright 1995-2009.
     ssss    gggg x   x         Steven Goodwin. All Rights Reserved.
    
This version has been released under the GNU Public License v2.0.

Please see the license files for details.

File: sgx/include/sgx/core/threading/thread.hpp

*/

#ifndef SGX_CORE_THREADING_THREAD_HPP
#define SGX_CORE_THREADING_THREAD_HPP	1

class CSGXThread;
typedef tMEM_SIZE (*cbThreadFunction)(CSGXThread *);

#define SGX_THREAD_STATE_INITIALIZING	0
#define SGX_THREAD_STATE_RUNNING			1
#define SGX_THREAD_STATE_PAUSED			2

class sgxCoreThreadCallbackHandler {
public:
	virtual tBOOL	onCoreThreadStarted(const CSGXThread *pThread);
	virtual tBOOL	onCoreThreadSuspended(const CSGXThread *pThread);
	virtual tBOOL	onCoreThreadResumed(const CSGXThread *pThread);
	virtual tBOOL	onCoreThreadFinished(const CSGXThread *pThread, const tMEM_SIZE result);
};

/**
You can derive from this class, to eliminate the (rather ugly) hangover
of callback functions from version 8.x. You are obliged to honour the m_State
member, and only process the thread code when it equals SGX_THREAD_STATE_RUNNING.

Every thread is created by CSGXThreadManager::create(pThread)
Once it has been added to the system, the m_State is will be to
SGX_THREAD_STATE_INITIALIZING and its start method will be invoked. 
You must call

  pThread->resume();

To begin processing.

*/
class CSGXThread : public sgxCoreThreadCallbackHandler
{ 
public:
	CSGXThread(const sgxCPU *pOnCPU=NULL);
	virtual tMEM_SIZE						start();
	virtual void 							suspend();
	virtual void 							resume();
	virtual void 							end(const tMEM_SIZE result);

	//
	sgxCoreThreadCallbackHandler *	m_pHandler;
	volatile tUINT32				m_State;
	void *							m_pUserData;
	
	// Do not change the values below, unless you know what you're doing!
	const sgxCPU *					m_pCPU;
	cbThreadFunction				m_CallbackToStartThread;
	tMEM_SIZE						m_SystemID;

};

#endif /* SGX_CORE_THREADING_THREAD_HPP */

