- added callback functionality to IThreadWorker

- added exposure of IThreader to sample sdk

--HG--
extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%401096
This commit is contained in:
David Anderson
2007-07-12 16:57:26 +00:00
parent 8a8e6ef594
commit 529933739d
12 changed files with 119 additions and 17 deletions
+25 -2
View File
@@ -14,9 +14,10 @@
#include "BaseWorker.h"
BaseWorker::BaseWorker() :
BaseWorker::BaseWorker(IThreadWorkerCallbacks *hooks) :
m_perFrame(SM_DEFAULT_THREADS_PER_FRAME),
m_state(Worker_Stopped)
m_state(Worker_Stopped),
m_pHooks(hooks)
{
}
@@ -129,14 +130,18 @@ unsigned int BaseWorker::RunFrame()
while (done < max)
{
if ((swt=PopThreadFromQueue()) == NULL)
{
break;
}
pThread = swt->pThread;
swt->m_state = Thread_Running;
pThread->RunThread(swt);
swt->m_state = Thread_Done;
pThread->OnTerminate(swt, false);
if (swt->m_params.flags & Thread_AutoRelease)
{
delete swt;
}
done++;
}
@@ -157,30 +162,46 @@ bool BaseWorker::Start()
m_state = Worker_Running;
if (m_pHooks)
{
m_pHooks->OnWorkerStart(this);
}
return true;
}
bool BaseWorker::Stop(bool flush_cancel)
{
if (m_state == Worker_Invalid || m_state == Worker_Stopped)
{
return false;
}
if (m_state == Worker_Paused)
{
if (!Unpause())
{
return false;
}
}
m_state = Worker_Stopped;
Flush(flush_cancel);
if (m_pHooks)
{
m_pHooks->OnWorkerStop(this);
}
return true;
}
bool BaseWorker::Pause()
{
if (m_state != Worker_Running)
{
return false;
}
m_state = Worker_Paused;
@@ -191,7 +212,9 @@ bool BaseWorker::Pause()
bool BaseWorker::Unpause()
{
if (m_state != Worker_Paused)
{
return false;
}
m_state = Worker_Running;
+2 -1
View File
@@ -54,7 +54,7 @@ private:
class BaseWorker : public IThreadWorker
{
public:
BaseWorker();
BaseWorker(IThreadWorkerCallbacks *hooks);
virtual ~BaseWorker();
public: //IWorker
virtual unsigned int RunFrame();
@@ -81,6 +81,7 @@ protected:
SourceHook::List<SWThreadHandle *> m_ThreadQueue;
unsigned int m_perFrame;
volatile WorkerState m_state;
IThreadWorkerCallbacks *m_pHooks;
};
#endif //_INCLUDE_SOURCEMOD_BASEWORKER_H
+3 -3
View File
@@ -25,13 +25,13 @@
#include "PosixThreads.h"
#include "ThreadWorker.h"
IThreadWorker *PosixThreader::MakeWorker(bool threaded)
IThreadWorker *PosixThreader::MakeWorker(IThreadWorkerCallbacks *hooks, bool threaded)
{
if (threaded)
{
return new ThreadWorker(this, DEFAULT_THINK_TIME_MS);
return new ThreadWorker(hooks, this, DEFAULT_THINK_TIME_MS);
} else {
return new BaseWorker();
return new BaseWorker(hooks);
}
}
+1 -1
View File
@@ -95,7 +95,7 @@ public:
void GetPriorityBounds(ThreadPriority &max, ThreadPriority &min);
void ThreadSleep(unsigned int ms);
IEventSignal *MakeEventSignal();
IThreadWorker *MakeWorker(bool threaded);
IThreadWorker *MakeWorker(IThreadWorkerCallbacks *hooks, bool threaded);
void DestroyWorker(IThreadWorker *pWorker);
};
+33 -2
View File
@@ -14,7 +14,7 @@
#include "ThreadWorker.h"
ThreadWorker::ThreadWorker() :
ThreadWorker::ThreadWorker(IThreadWorkerCallbacks *hooks) : BaseWorker(hooks),
m_Threader(NULL),
m_QueueLock(NULL),
m_StateLock(NULL),
@@ -26,7 +26,8 @@ ThreadWorker::ThreadWorker() :
m_state = Worker_Invalid;
}
ThreadWorker::ThreadWorker(IThreader *pThreader, unsigned int thinktime) :
ThreadWorker::ThreadWorker(IThreadWorkerCallbacks *hooks, IThreader *pThreader, unsigned int thinktime) :
BaseWorker(hooks),
m_Threader(pThreader),
m_QueueLock(NULL),
m_StateLock(NULL),
@@ -46,10 +47,14 @@ ThreadWorker::ThreadWorker(IThreader *pThreader, unsigned int thinktime) :
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)
@@ -62,6 +67,11 @@ void ThreadWorker::RunThread(IThreadHandle *pHandle)
{
WorkerState this_state = Worker_Running;
size_t num;
if (m_pHooks)
{
m_pHooks->OnWorkerStart(this);
}
while (true)
{
@@ -112,7 +122,9 @@ void ThreadWorker::RunThread(IThreadHandle *pHandle)
if (!m_FlushType)
{
while (m_ThreadQueue.size())
{
RunFrame();
}
}
break;
}
@@ -127,14 +139,23 @@ void ThreadWorker::RunThread(IThreadHandle *pHandle)
* wait in between threads if specified
*/
if (m_think_time)
{
m_Threader->ThreadSleep(m_think_time);
}
}
if (m_pHooks)
{
m_pHooks->OnWorkerStop(this);
}
}
SWThreadHandle *ThreadWorker::PopThreadFromQueue()
{
if (m_state <= Worker_Stopped && !m_QueueLock)
{
return NULL;
}
SWThreadHandle *swt;
m_QueueLock->Lock();
@@ -147,7 +168,9 @@ SWThreadHandle *ThreadWorker::PopThreadFromQueue()
void ThreadWorker::AddThreadToQueue(SWThreadHandle *pHandle)
{
if (m_state <= Worker_Stopped)
{
return;
}
m_QueueLock->Lock();
BaseWorker::AddThreadToQueue(pHandle);
@@ -174,7 +197,9 @@ bool ThreadWorker::Start()
if (m_state == Worker_Invalid)
{
if (m_Threader == NULL)
{
return false;
}
} else if (m_state != Worker_Stopped) {
return false;
}
@@ -196,7 +221,9 @@ bool ThreadWorker::Start()
bool ThreadWorker::Stop(bool flush_cancel)
{
if (m_state == Worker_Invalid || m_state == Worker_Stopped)
{
return false;
}
WorkerState oldstate;
@@ -244,7 +271,9 @@ bool ThreadWorker::Stop(bool flush_cancel)
bool ThreadWorker::Pause()
{
if (m_state != Worker_Running)
{
return false;
}
m_StateLock->Lock();
m_state = Worker_Paused;
@@ -257,7 +286,9 @@ bool ThreadWorker::Pause()
bool ThreadWorker::Unpause()
{
if (m_state != Worker_Paused)
{
return false;
}
m_StateLock->Lock();
m_state = Worker_Running;
+2 -2
View File
@@ -22,8 +22,8 @@
class ThreadWorker : public BaseWorker, public IThread
{
public:
ThreadWorker();
ThreadWorker(IThreader *pThreader, unsigned int thinktime=DEFAULT_THINK_TIME_MS);
ThreadWorker(IThreadWorkerCallbacks *hooks);
ThreadWorker(IThreadWorkerCallbacks *hooks, IThreader *pThreader, unsigned int thinktime=DEFAULT_THINK_TIME_MS);
virtual ~ThreadWorker();
public: //IThread
virtual void OnTerminate(IThreadHandle *pHandle, bool cancel);
+3 -3
View File
@@ -16,13 +16,13 @@
#include "WinThreads.h"
#include "ThreadWorker.h"
IThreadWorker *WinThreader::MakeWorker(bool threaded)
IThreadWorker *WinThreader::MakeWorker(IThreadWorkerCallbacks *hooks, bool threaded)
{
if (threaded)
{
return new ThreadWorker(this, DEFAULT_THINK_TIME_MS);
return new ThreadWorker(hooks, this, DEFAULT_THINK_TIME_MS);
} else {
return new BaseWorker();
return new BaseWorker(hooks);
}
}
+1 -1
View File
@@ -84,7 +84,7 @@ public:
void GetPriorityBounds(ThreadPriority &max, ThreadPriority &min);
void ThreadSleep(unsigned int ms);
IEventSignal *MakeEventSignal();
IThreadWorker *MakeWorker(bool threaded);
IThreadWorker *MakeWorker(IThreadWorkerCallbacks *hooks, bool threaded);
void DestroyWorker(IThreadWorker *pWorker);
};