diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2017-04-06 04:22:12 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2017-04-06 04:22:12 +0200 |
commit | f9160866df2a045146910ab555af718cde676694 (patch) | |
tree | e401a2aca39f28cbd4a484071b2529bac41dc47e /pose-widget | |
parent | e1f1ab62f1aaa5ee9924f1d124dc4a1b66ea9893 (diff) |
pose-widget: avoid condition variable overhead
It's busy-looping on Windows.
Diffstat (limited to 'pose-widget')
-rw-r--r-- | pose-widget/pose-widget.cpp | 26 | ||||
-rw-r--r-- | pose-widget/pose-widget.hpp | 4 |
2 files changed, 17 insertions, 13 deletions
diff --git a/pose-widget/pose-widget.cpp b/pose-widget/pose-widget.cpp index e711a617..8ccec988 100644 --- a/pose-widget/pose-widget.cpp +++ b/pose-widget/pose-widget.cpp @@ -8,6 +8,7 @@ #include "pose-widget.hpp" #include "compat/util.hpp" #include "compat/timer.hpp" +#include "compat/sleep.hpp" #include <cmath> #include <algorithm> #include <QPainter> @@ -21,10 +22,9 @@ pose_transform::pose_transform(QWidget* dst) : dst(dst), image(w, h, QImage::Format_ARGB32), image2(w, h, QImage::Format_ARGB32), - width(w), height(h) + width(w), height(h), + fresh(false) { - fresh.clear(std::memory_order_seq_cst); - front = QImage(QString(":/images/side1.png")); back = QImage(QString(":/images/side6.png")); @@ -57,15 +57,18 @@ void pose_transform::run() { lock_guard l(mtx); - const cv_status st = cvar.wait_for(l, std::chrono::milliseconds(2000)); - if (st == cv_status::timeout) - continue; + + if (fresh) + goto end; rotation = rotation_; translation = translation_; } project_quad_texture(); + +end: + portable::sleep(9); } } @@ -80,9 +83,12 @@ pose_widget::~pose_widget() void pose_widget::rotate_async(double xAngle, double yAngle, double zAngle, double x, double y, double z) { - if (!xform.fresh.test_and_set(std::memory_order_seq_cst)) + bool expected = true; + if (xform.fresh.compare_exchange_weak(expected, false)) + { update(); - xform.rotate_async(xAngle, yAngle, zAngle, x, y, z); + xform.rotate_async(xAngle, yAngle, zAngle, x, y, z); + } } template<typename F> @@ -114,7 +120,7 @@ void pose_widget::rotate_sync(double xAngle, double yAngle, double zAngle, doubl void pose_transform::rotate_async(double xAngle, double yAngle, double zAngle, double x, double y, double z) { - with_rotate([this]() { cvar.notify_one(); }, xAngle, yAngle, zAngle, x, y, z); + with_rotate([this]() {}, xAngle, yAngle, zAngle, x, y, z); } void pose_transform::rotate_sync(double xAngle, double yAngle, double zAngle, double x, double y, double z) @@ -338,7 +344,7 @@ void pose_transform::project_quad_texture() lock_guard l2(mtx2); image2.fill(Qt::transparent); std::swap(image, image2); - fresh.clear(); + fresh = true; } } diff --git a/pose-widget/pose-widget.hpp b/pose-widget/pose-widget.hpp index 7befe72d..3e98624c 100644 --- a/pose-widget/pose-widget.hpp +++ b/pose-widget/pose-widget.hpp @@ -15,7 +15,6 @@ #include "compat/euler.hpp" #include <mutex> -#include <condition_variable> #include <atomic> #ifdef BUILD_POSE_WIDGET @@ -67,7 +66,6 @@ class pose_transform final : private QThread rmat rotation, rotation_; vec3 translation, translation_; - std::condition_variable cvar; std::mutex mtx, mtx2; QWidget* dst; @@ -77,7 +75,7 @@ class pose_transform final : private QThread int width, height; - std::atomic_flag fresh; + std::atomic<bool> fresh; static constexpr int w = 320, h = 240; }; |