#include "qsthread.h"

QSThread::QSThread():QThread()
{
    m_bStop = false;
    m_bRunning = false;

    m_nDefaultMicroSleep = 1000;
    m_nCurrentState = 0;
}

QSThread::~QSThread()
{
    m_bStop = true;
    while(m_bRunning==true)
    {
        usleep(1000*30);
    }


}

void QSThread::ExitThread()
{
    m_bStop = true;
    while(m_bRunning==true)
    {
        usleep(1000*30);
    }
}

void QSThread::TerminateThread()
{
    terminate();
    m_bRunning = false;
}

void QSThread::ChangeState(int nState)
{
    m_nCurrentState = nState;
}

int QSThread::GetCurrentState()
{
    return m_nCurrentState;
}

void QSThread::IdleFuction()
{
    //1ms sleep
    usleep(m_nDefaultMicroSleep);
}

void QSThread::run()
{
    m_bRunning = true;

    Init();
    qDebug()<<"From worker thread: "<<currentThreadId();
    usleep(1000*1000);

    while (m_bStop==false && m_nCurrentState!=1000)
    {
        InnerRun();
    }

    m_bRunning = false;
}

void QSThread::InnerRun()
{
    if(m_nCurrentState==0)
    {
        IdleFuction();
    }
}

void QSThread::Init()
{

}

