summaryrefslogtreecommitdiff
path: root/src/util/thread.h
diff options
context:
space:
mode:
authorest31 <MTest31@outlook.com>2015-06-29 11:06:03 +0200
committerest31 <MTest31@outlook.com>2015-06-29 15:00:19 +0200
commit29dda9f356042c403b3b7da1d717d32b45c9b6de (patch)
treeac063171e654443370691468175502bd221ac979 /src/util/thread.h
parent4e28c8d3c8af51b728a049c28806e0f740ac0106 (diff)
downloadminetest-29dda9f356042c403b3b7da1d717d32b45c9b6de.tar.gz
minetest-29dda9f356042c403b3b7da1d717d32b45c9b6de.tar.bz2
minetest-29dda9f356042c403b3b7da1d717d32b45c9b6de.zip
Add UpdateThread and use it for minimap and mesh threads
Diffstat (limited to 'src/util/thread.h')
-rw-r--r--src/util/thread.h61
1 files changed, 61 insertions, 0 deletions
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<typename T>
class MutexedVariable
@@ -208,5 +209,65 @@ private:
MutexedQueue< GetRequest<Key, T, Caller, CallerData> > 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