#include "worker_thread.h"

ZCWorkThread::ZCWorkThread(const char *threadName)
	:m_strName(threadName),
	m_nStatus(ZCThread_Status_Init)
{
	m_threadEntry = new std::thread(cbThreadProcess, this);
}
ZCWorkThread::~ZCWorkThread()
{
	
}

void ZCWorkThread::StartThread()
{
	if (m_nStatus == ZCThread_Status_Init || m_nStatus == ZCThread_Status_Init)
	{
		m_nStatus = ZCThread_Status_Running;
		m_workSignal.Send();
	}
}
void ZCWorkThread::PauseThread()
{
	if (m_nStatus == ZCThread_Status_Running)
	{
		m_nStatus = ZCThread_Status_Pause;
		m_workSignal.Send();  //启动后 线程处于 m_workSignal.Wait() 状态
		m_pauseSignal.Wait(); //线程
	}
}
void ZCWorkThread::StopThread()
{
	if (m_nStatus != ZCThread_Status_Stopped)
	{
		m_nStatus = ZCThread_Status_Stopped;
		m_workSignal.Send();
		m_threadEntry->join();
		delete m_threadEntry;
		m_threadEntry = nullptr;
	}
}
void *ZCWorkThread::threadProcess()
{
	while (true)
	{
		if (m_nStatus == ZCThread_Status_Init)
		{
			m_workSignal.Wait();
		}
		else if (m_nStatus == ZCThread_Status_Pause)
		{
			m_pauseSignal.Send();
			m_workSignal.Wait();
		}
		else if (m_nStatus == ZCThread_Status_Running)
		{
			DoRunning();
		}
		else if (m_nStatus == ZCThread_Status_Stopped)
		{
			break;
		}
	}

	return 0;
}