summary refs log tree commit diff
path: root/src/Compute/Queue.hpp
diff options
context:
space:
mode:
authorMel <einebeere@gmail.com>2023-06-12 23:07:36 +0200
committerMel <einebeere@gmail.com>2023-06-12 23:07:36 +0200
commit92ac46df6afa8ee76f972cceb681cf32658f84a2 (patch)
tree6ff89b9d6e8e0568f346b5f7b8b9e7ba3e74211e /src/Compute/Queue.hpp
parentd0de60dc33df75fbcacb53a09568b14d0fd48cb9 (diff)
downloadmeowcraft-92ac46df6afa8ee76f972cceb681cf32658f84a2.tar.zst
meowcraft-92ac46df6afa8ee76f972cceb681cf32658f84a2.zip
Start generation from player position
Diffstat (limited to 'src/Compute/Queue.hpp')
-rw-r--r--src/Compute/Queue.hpp18
1 files changed, 12 insertions, 6 deletions
diff --git a/src/Compute/Queue.hpp b/src/Compute/Queue.hpp
index 6aba1ab..53acc2d 100644
--- a/src/Compute/Queue.hpp
+++ b/src/Compute/Queue.hpp
@@ -5,7 +5,7 @@
 #include <mutex>
 #include <utility>
 #include <vector>
-#include <iostream>
+#include <queue>
 #include <thread>
 
 namespace MC::Compute {
@@ -20,9 +20,9 @@ public:
         }
     };
 
-    void add(I id, std::function<X()> execute) {
+    void add(I id, float priority, std::function<X()> execute) {
         std::scoped_lock work_lock(m_control->work.mutex);
-        m_control->work.jobs.push_back({id, std::move(execute)});
+        m_control->work.jobs.emplace(id, priority, execute);
     }
 
     struct Result {
@@ -43,13 +43,19 @@ public:
 
 private:
     struct Job {
+        Job() = default;
+        Job(I id, float priority, std::function<X()> execute) : id(id), priority(priority), execute(execute) {}
+
         I id;
+        float priority;
         std::function<X()> execute;
+
+        bool operator>(const Job& other) const { return priority > other.priority; }
     };
 
     struct Work {
         std::mutex mutex;
-        std::vector<Job> jobs;
+        std::priority_queue<Job, std::vector<Job>, std::greater<Job>> jobs;
     };
 
     struct Results {
@@ -71,8 +77,8 @@ private:
             {
                 std::scoped_lock work_lock(control->work.mutex);
                 if (!control->work.jobs.empty()) {
-                    job = control->work.jobs.back();
-                    control->work.jobs.pop_back();
+                    job = control->work.jobs.top();
+                    control->work.jobs.pop();
                     nothing_to_do = false;
                 }
             }