amxmodx/dlls/mysqlx/thread/ThreadWorker.cpp

246 lines
4.3 KiB
C++
Raw Normal View History

2006-04-23 05:10:06 +04:00
#include "ThreadWorker.h"
ThreadWorker::ThreadWorker() :
m_Threader(NULL),
m_QueueLock(NULL),
m_StateLock(NULL),
m_PauseSignal(NULL),
m_AddSignal(NULL),
me(NULL),
m_think_time(DEFAULT_THINK_TIME_MS)
{
m_state = Worker_Invalid;
}
ThreadWorker::ThreadWorker(IThreader *pThreader, unsigned int thinktime) :
m_Threader(pThreader),
m_QueueLock(NULL),
m_StateLock(NULL),
m_PauseSignal(NULL),
m_AddSignal(NULL),
me(NULL),
m_think_time(thinktime)
{
if (m_Threader)
{
m_state = Worker_Stopped;
} else {
m_state = Worker_Invalid;
}
}
ThreadWorker::~ThreadWorker()
{
if (m_state != Worker_Stopped || m_state != Worker_Invalid)
Stop(true);
if (m_ThreadQueue.size())
Flush(true);
}
void ThreadWorker::OnTerminate(IThreadHandle *pHandle, bool cancel)
{
//we don't particularly care
return;
}
void ThreadWorker::RunThread(IThreadHandle *pHandle)
{
WorkerState this_state = Worker_Running;
2006-04-23 05:10:06 +04:00
size_t num;
while (true)
{
/**
* Check number of items in the queue
*/
if (this_state != Worker_Stopped)
2006-04-23 05:10:06 +04:00
{
m_QueueLock->Lock();
num = m_ThreadQueue.size();
if (!num)
{
/**
* if none, wait for an item
*/
m_Waiting = true;
m_QueueLock->Unlock();
m_AddSignal->Wait();
m_Waiting = false;
} else {
m_QueueLock->Unlock();
}
2006-04-23 05:10:06 +04:00
}
m_StateLock->Lock();
this_state = m_state;
m_StateLock->Unlock();
if (this_state != Worker_Running)
{
if (this_state == Worker_Paused || this_state == Worker_Stopped)
2006-04-23 05:10:06 +04:00
{
//wait until the lock is cleared.
if (this_state == Worker_Paused)
m_PauseSignal->Wait();
if (this_state == Worker_Stopped)
2006-04-23 05:10:06 +04:00
{
//if we're supposed to flush cleanrly,
// run all of the remaining frames first.
if (!m_FlushType)
{
while (m_ThreadQueue.size())
RunFrame();
}
break;
2006-04-23 05:10:06 +04:00
}
}
}
/**
* Run the frame.
*/
RunFrame();
/**
* wait in between threads if specified
*/
if (m_think_time)
m_Threader->ThreadSleep(m_think_time);
}
}
SWThreadHandle *ThreadWorker::PopThreadFromQueue()
{
if (m_state <= Worker_Stopped && !m_QueueLock)
return NULL;
SWThreadHandle *swt;
m_QueueLock->Lock();
swt = BaseWorker::PopThreadFromQueue();
m_QueueLock->Unlock();
return swt;
}
void ThreadWorker::AddThreadToQueue(SWThreadHandle *pHandle)
{
if (m_state <= Worker_Stopped)
return;
m_QueueLock->Lock();
2006-04-23 08:24:46 +04:00
BaseWorker::AddThreadToQueue(pHandle);
2006-04-23 05:10:06 +04:00
if (m_Waiting)
{
m_AddSignal->Signal();
}
m_QueueLock->Unlock();
}
WorkerState ThreadWorker::GetStatus(unsigned int *threads)
{
WorkerState state;
m_StateLock->Lock();
state = BaseWorker::GetStatus(threads);
m_StateLock->Unlock();
return state;
}
bool ThreadWorker::Start()
{
if (m_state == Worker_Invalid)
{
if (m_Threader == NULL)
return false;
} else if (m_state != Worker_Stopped) {
return false;
}
m_Waiting = false;
m_QueueLock = m_Threader->MakeMutex();
m_StateLock = m_Threader->MakeMutex();
m_PauseSignal = m_Threader->MakeEventSignal();
m_AddSignal = m_Threader->MakeEventSignal();
m_state = Worker_Running;
ThreadParams pt;
pt.flags = Thread_Default;
pt.prio = ThreadPrio_Normal;
me = m_Threader->MakeThread(this, &pt);
return true;
}
bool ThreadWorker::Stop(bool flush_cancel)
{
if (m_state == Worker_Invalid || m_state == Worker_Stopped)
return false;
WorkerState oldstate;
2006-04-23 05:10:06 +04:00
//set new state
m_StateLock->Lock();
oldstate = m_state;
2006-04-23 05:10:06 +04:00
m_state = Worker_Stopped;
m_FlushType = flush_cancel;
m_StateLock->Unlock();
2006-04-23 05:10:06 +04:00
if (oldstate == Worker_Paused)
2006-04-23 05:10:06 +04:00
{
Unpause();
} else {
2006-04-23 05:10:06 +04:00
m_AddSignal->Signal();
Pause();
Unpause();
2006-04-23 05:10:06 +04:00
}
2006-04-23 05:10:06 +04:00
me->WaitForThread();
//destroy it
me->DestroyThis();
//flush all remaining events
Flush(true);
//free mutex locks
m_QueueLock->DestroyThis();
m_StateLock->DestroyThis();
m_PauseSignal->DestroyThis();
m_AddSignal->DestroyThis();
//invalidizzle
m_QueueLock = NULL;
m_StateLock = NULL;
m_PauseSignal = NULL;
m_AddSignal = NULL;
me = NULL;
return true;
}
bool ThreadWorker::Pause()
{
if (m_state != Worker_Running)
return false;
m_StateLock->Lock();
m_state = Worker_Paused;
m_StateLock->Unlock();
return true;
}
bool ThreadWorker::Unpause()
{
if (m_state != Worker_Paused)
return false;
m_StateLock->Lock();
m_state = Worker_Running;
m_StateLock->Unlock();
m_PauseSignal->Signal();
if (m_Waiting)
{
m_AddSignal->Signal();
}
return true;
}