From 29dda9f356042c403b3b7da1d717d32b45c9b6de Mon Sep 17 00:00:00 2001 From: est31 Date: Mon, 29 Jun 2015 11:06:03 +0200 Subject: Add UpdateThread and use it for minimap and mesh threads --- src/util/thread.h | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) (limited to 'src/util') diff --git a/src/util/thread.h b/src/util/thread.h index eda9c0ca2..faa5869ca 100644 --- a/src/util/thread.h +++ b/src/util/thread.h @@ -25,6 +25,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "../jthread/jmutex.h" #include "../jthread/jmutexautolock.h" #include "porting.h" +#include "log.h" template class MutexedVariable @@ -208,5 +209,65 @@ private: MutexedQueue< GetRequest > m_queue; }; +class UpdateThread : public JThread +{ +private: + JSemaphore m_update_sem; + +protected: + virtual void doUpdate() = 0; + virtual const char *getName() = 0; + +public: + UpdateThread() + { + } + ~UpdateThread() + {} + + void deferUpdate() + { + m_update_sem.Post(); + } + + void Stop() + { + JThread::Stop(); + + // give us a nudge + m_update_sem.Post(); + } + + void *Thread() + { + ThreadStarted(); + + const char *thread_name = getName(); + + log_register_thread(thread_name); + + DSTACK(__FUNCTION_NAME); + + BEGIN_DEBUG_EXCEPTION_HANDLER + + porting::setThreadName(thread_name); + + while (!StopRequested()) { + + m_update_sem.Wait(); + + // Empty the queue, just in case doUpdate() is expensive + while (m_update_sem.GetValue()) m_update_sem.Wait(); + + if (StopRequested()) break; + + doUpdate(); + } + END_DEBUG_EXCEPTION_HANDLER(errorstream) + + return NULL; + } +}; + #endif -- cgit v1.2.3