]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/util/thread.h
Revert "Fix short 180 degree rotation when using set_bone_position (#10405)" (#10534)
[dragonfireclient.git] / src / util / thread.h
index b3a5e68a2179ca20d70ca63bbcf771eeee8a0b93..73e9beb806f44d8effc6dd8e3a3bec3f82adf23f 100644 (file)
@@ -17,46 +17,39 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
-#ifndef UTIL_THREAD_HEADER
-#define UTIL_THREAD_HEADER
+#pragma once
 
-#include "../irrlichttypes.h"
-#include "../jthread/jthread.h"
-#include "../jthread/jmutex.h"
-#include "../jthread/jmutexautolock.h"
+#include "irrlichttypes.h"
+#include "threading/thread.h"
+#include "threading/mutex_auto_lock.h"
 #include "porting.h"
 #include "log.h"
+#include "container.h"
 
 template<typename T>
-class MutexedVariable {
+class MutexedVariable
+{
 public:
-       MutexedVariable(value):
+       MutexedVariable(const T &value):
                m_value(value)
        {}
 
        T get()
        {
-               JMutexAutoLock lock(m_mutex);
+               MutexAutoLock lock(m_mutex);
                return m_value;
        }
 
-       void set(value)
+       void set(const T &value)
        {
-               JMutexAutoLock lock(m_mutex);
+               MutexAutoLock lock(m_mutex);
                m_value = value;
        }
 
-       // You'll want to grab this in a SharedPtr
-       JMutexAutoLock *getLock()
-       {
-               return new JMutexAutoLock(m_mutex);
-       }
-
        // You pretty surely want to grab the lock when accessing this
        T m_value;
-
 private:
-       JMutex m_mutex;
+       std::mutex m_mutex;
 };
 
 /*
@@ -85,11 +78,11 @@ class CallerInfo {
 template<typename Key, typename T, typename Caller, typename CallerData>
 class GetRequest {
 public:
-       GetRequest() {}
-       ~GetRequest() {}
+       GetRequest() = default;
+       ~GetRequest() = default;
 
-       GetRequest(Key a_key) {
-               key = a_key;
+       GetRequest(const Key &a_key): key(a_key)
+       {
        }
 
        Key key;
@@ -111,14 +104,14 @@ class RequestQueue {
                return m_queue.empty();
        }
 
-       void add(Key key, Caller caller, CallerData callerdata,
+       void add(const Key &key, Caller caller, CallerData callerdata,
                ResultQueue<Key, T, Caller, CallerData> *dest)
        {
                typename std::deque<GetRequest<Key, T, Caller, CallerData> >::iterator i;
                typename std::list<CallerInfo<Caller, CallerData, Key, T> >::iterator j;
 
                {
-                       JMutexAutoLock lock(m_queue.getMutex());
+                       MutexAutoLock lock(m_queue.getMutex());
 
                        /*
                                If the caller is already on the list, only update CallerData
@@ -192,60 +185,44 @@ class RequestQueue {
        MutexedQueue<GetRequest<Key, T, Caller, CallerData> > m_queue;
 };
 
-class UpdateThread : public JThread {
+class UpdateThread : public Thread
+{
 public:
-       UpdateThread() {}
-       virtual ~UpdateThread() {}
+       UpdateThread(const std::string &name) : Thread(name + "Update") {}
+       ~UpdateThread() = default;
 
-       void deferUpdate()
-       {
-               m_update_sem.Post();
-       }
+       void deferUpdate() { m_update_sem.post(); }
 
-       void Stop()
+       void stop()
        {
-               JThread::Stop();
+               Thread::stop();
 
                // give us a nudge
-               m_update_sem.Post();
+               m_update_sem.post();
        }
 
-       void *Thread()
+       void *run()
        {
-               ThreadStarted();
-
-               const char *thread_name = getName();
-               log_register_thread(thread_name);
-               porting::setThreadName(thread_name);
-
-               DSTACK(__FUNCTION_NAME);
                BEGIN_DEBUG_EXCEPTION_HANDLER
 
-               while (!StopRequested()) {
-                       m_update_sem.Wait();
+               while (!stopRequested()) {
+                       m_update_sem.wait();
+                       // Set semaphore to 0
+                       while (m_update_sem.wait(0));
 
-                       // Empty the queue, just in case doUpdate() is expensive
-                       while (m_update_sem.GetValue())
-                               m_update_sem.Wait();
-
-                       if (StopRequested())
-                               break;
+                       if (stopRequested()) break;
 
                        doUpdate();
                }
 
-               END_DEBUG_EXCEPTION_HANDLER(errorstream)
+               END_DEBUG_EXCEPTION_HANDLER
 
                return NULL;
        }
 
 protected:
        virtual void doUpdate() = 0;
-       virtual const char *getName() = 0;
 
 private:
-       JSemaphore m_update_sem;
+       Semaphore m_update_sem;
 };
-
-#endif
-