From f9160866df2a045146910ab555af718cde676694 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Thu, 6 Apr 2017 04:22:12 +0200 Subject: pose-widget: avoid condition variable overhead It's busy-looping on Windows. --- pose-widget/pose-widget.cpp | 26 ++++++++++++++++---------- pose-widget/pose-widget.hpp | 4 +--- 2 files changed, 17 insertions(+), 13 deletions(-) (limited to 'pose-widget') 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 #include #include @@ -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 @@ -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 -#include #include #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 fresh; static constexpr int w = 320, h = 240; }; -- cgit v1.2.3