From 25fc3cd5d6249f4cb86a5d2b062adc7046f955b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20=E2=80=98Touky=E2=80=99=20Huet?= Date: Mon, 26 May 2014 03:26:20 +0000 Subject: [PATCH] Added Generic thread manager --- src/lol/sys/thread.h | 40 ++++++++++++++++++++++++++++++++-------- src/sys/thread.cpp | 19 +++++++++---------- 2 files changed, 41 insertions(+), 18 deletions(-) diff --git a/src/lol/sys/thread.h b/src/lol/sys/thread.h index b93d1051..7ce86169 100644 --- a/src/lol/sys/thread.h +++ b/src/lol/sys/thread.h @@ -85,7 +85,7 @@ struct ThreadJobType class ThreadJob { - friend class ThreadManager; + friend class BaseThreadManager; public: ThreadJob() { m_type = ThreadJobType::NONE; } @@ -99,11 +99,14 @@ protected: ThreadJobType m_type; }; -class ThreadManager : public Entity +//Base class for thread manager +class BaseThreadManager : public Entity { public: - ThreadManager(int thread_count); - ~ThreadManager(); + BaseThreadManager(int thread_count); + ~BaseThreadManager(); + + char const *GetName() { return ""; } //Initialize, Ticker::Ref and start the thread bool Start(); @@ -111,15 +114,14 @@ public: bool Stop(); //Work stuff bool AddWork(ThreadJob* job); +protected: bool FetchResult(Array& results); //Base thread work function static void *BaseThreadWork(void* data); virtual void TickGame(float seconds); - virtual void TreatResult(ThreadJob* result) { } + //Default behaviour : delete the job result + virtual void TreatResult(ThreadJob* result) { delete(result); } - char const *GetName() { return ""; } - -protected: /* Worker threads */ int m_thread_count; Array m_threads; @@ -128,6 +130,28 @@ protected: Queue m_resultqueue; Array m_job_dispatch; }; + +//Generic class for thread manager, executes work and store results, for you to use +class GenericThreadManager : public BaseThreadManager +{ +public: + GenericThreadManager(int thread_count) + : BaseThreadManager(thread_count) { } + + char const *GetName() { return ""; } + + bool GetWorkResult(Array& results) + { + results += m_job_result; + m_job_result.Empty(); + return results.Count() > 0; + } + +protected: + virtual void TreatResult(ThreadJob* result) { m_job_result << result; } + + Array m_job_result; +}; #endif } /* namespace lol */ diff --git a/src/sys/thread.cpp b/src/sys/thread.cpp index 3621b3cc..dab6512d 100644 --- a/src/sys/thread.cpp +++ b/src/sys/thread.cpp @@ -19,17 +19,17 @@ namespace lol { #if LOL_FEATURE_THREADS -ThreadManager::ThreadManager(int thread_count) +BaseThreadManager::BaseThreadManager(int thread_count) { m_thread_count = thread_count; } -ThreadManager::~ThreadManager() +BaseThreadManager::~BaseThreadManager() { Stop(); } //Initialize, Ticker::Ref and start the thread -bool ThreadManager::Start() +bool BaseThreadManager::Start() { if (m_threads.Count() > 0) return false; @@ -45,7 +45,7 @@ bool ThreadManager::Start() } //Stop the threads -bool ThreadManager::Stop() +bool BaseThreadManager::Stop() { if (m_threads.Count() <= 0) return false; @@ -62,13 +62,13 @@ bool ThreadManager::Stop() } //Work stuff -bool ThreadManager::AddWork(ThreadJob* job) +bool BaseThreadManager::AddWork(ThreadJob* job) { if (m_jobqueue.TryPush(job)) return true; return false; } -bool ThreadManager::FetchResult(Array& results) +bool BaseThreadManager::FetchResult(Array& results) { ThreadJob* result; while (m_resultqueue.TryPop(result)) @@ -77,9 +77,9 @@ bool ThreadManager::FetchResult(Array& results) } //Base thread work function -void *ThreadManager::BaseThreadWork(void* data) +void *BaseThreadManager::BaseThreadWork(void* data) { - ThreadManager *that = (ThreadManager *)data; + BaseThreadManager *that = (BaseThreadManager *)data; that->m_spawnqueue.Push(ThreadStatus::THREAD_STARTED); for ( ; ; ) { @@ -99,7 +99,7 @@ void *ThreadManager::BaseThreadWork(void* data) return NULL; } -void ThreadManager::TickGame(float seconds) +void BaseThreadManager::TickGame(float seconds) { //Start if needed Start(); @@ -117,7 +117,6 @@ void ThreadManager::TickGame(float seconds) ThreadJob* job = result[i]; if (job->GetJobType() == ThreadJobType::WORK_DONE) TreatResult(job); - delete(job); } } }