lottie/vector: added taskqueue class in preparation for multithreaded backend 28/184428/4
authorsubhransu mohanty <sub.mohanty@samsung.com>
Wed, 18 Jul 2018 03:27:53 +0000 (12:27 +0900)
committerHermet Park <chuneon.park@samsung.com>
Thu, 19 Jul 2018 04:09:26 +0000 (04:09 +0000)
Change-Id: I0b5e5cab84b383bc48f846f99807870178672a1f

src/vector/vtaskqueue.h [new file with mode: 0644]

diff --git a/src/vector/vtaskqueue.h b/src/vector/vtaskqueue.h
new file mode 100644 (file)
index 0000000..5318f0e
--- /dev/null
@@ -0,0 +1,60 @@
+#ifndef VTASKQUEUE_H
+#define VTASKQUEUE_H
+
+#include<deque>
+
+template <typename Task>
+class TaskQueue {
+    using lock_t = std::unique_lock<std::mutex>;
+    std::deque<Task *> _q;
+    bool _done{false};
+    std::mutex _mutex;
+    std::condition_variable _ready;
+
+public:
+    bool try_pop(Task *&task) {
+        lock_t lock{_mutex, std::try_to_lock};
+        if (!lock || _q.empty()) return false;
+        task = std::move(_q.front());
+        _q.pop_front();
+        return true;
+    }
+
+    bool try_push(Task *task) {
+        {
+            lock_t lock{_mutex, std::try_to_lock};
+            if (!lock) return false;
+            _q.push_back(task);
+        }
+        _ready.notify_one();
+        return true;
+    }
+
+    void done() {
+        {
+            lock_t lock{_mutex};
+            _done = true;
+        }
+        _ready.notify_all();
+    }
+
+    bool pop(Task *&task) {
+        lock_t lock{_mutex};
+        while (_q.empty() && !_done)
+            _ready.wait(lock);
+        if (_q.empty()) return false;
+        task = std::move(_q.front());
+        _q.pop_front();
+        return true;
+    }
+
+    void push(Task *task) {
+        {
+            lock_t lock{_mutex};
+            _q.push_back(task);
+        }
+        _ready.notify_one();
+    }
+};
+
+#endif // VTASKQUEUE_H