diff options
| author | Stanislaw Halik <sthalik@misaki.pl> | 2017-06-18 18:19:17 +0200 | 
|---|---|---|
| committer | Stanislaw Halik <sthalik@misaki.pl> | 2017-06-18 18:48:42 +0200 | 
| commit | e88c7b29ea9ec9fcd6ac6b15c965085152100d2e (patch) | |
| tree | 6747fe7fa797e024b986ba624d05e6f59032f0ac | |
| parent | 646530b5f9ca5debe7a9b4840192e32e43f919bf (diff) | |
get rid of "volatile" abuse
We heavily used "volatile bool" to check if the thread
loop should stop. But this functionality is already
provided by Qt5's QThread::requestInterruption.
In other cases, "volatile" is wonderfully
underspecified so it's better to ditch its usage in
favor of std::atomic<t>. At the time we don't appear to
be using the "volatile" keyword except when calling
win32's Interlocked*() family of functions as
necessary.
In freetrackclient's header the "volatile" qualifier
was used as part of a typedef. This doesn't work. Use
it as part of data declaration.
| -rw-r--r-- | dinput/keybinding-worker.cpp | 11 | ||||
| -rw-r--r-- | dinput/keybinding-worker.hpp | 1 | ||||
| -rw-r--r-- | freetrackclient/fttypes.h | 63 | ||||
| -rw-r--r-- | logic/tracker.cpp | 5 | ||||
| -rw-r--r-- | logic/tracker.h | 1 | ||||
| -rw-r--r-- | proto-simconnect/ftnoir_protocol_sc.cpp | 13 | ||||
| -rw-r--r-- | proto-simconnect/ftnoir_protocol_sc.h | 18 | ||||
| -rw-r--r-- | tracker-aruco/ftnoir_tracker_aruco.cpp | 5 | ||||
| -rw-r--r-- | tracker-aruco/ftnoir_tracker_aruco.h | 1 | ||||
| -rw-r--r-- | tracker-freepie-udp/ftnoir_tracker_freepie-udp.cpp | 7 | ||||
| -rw-r--r-- | tracker-freepie-udp/ftnoir_tracker_freepie-udp.h | 1 | ||||
| -rw-r--r-- | tracker-hatire/ftnoir_tracker_hat.h | 4 | ||||
| -rw-r--r-- | tracker-hydra/ftnoir_tracker_hydra.cpp | 2 | ||||
| -rw-r--r-- | tracker-hydra/ftnoir_tracker_hydra.h | 1 | ||||
| -rw-r--r-- | tracker-pt/ftnoir_tracker_pt.h | 8 | ||||
| -rw-r--r-- | tracker-pt/point_tracker.cpp | 2 | ||||
| -rw-r--r-- | tracker-tobii-eyex/tobii-eyex.hpp | 3 | ||||
| -rw-r--r-- | tracker-udp/ftnoir_tracker_udp.cpp | 10 | ||||
| -rw-r--r-- | tracker-udp/ftnoir_tracker_udp.h | 1 | 
19 files changed, 76 insertions, 81 deletions
| diff --git a/dinput/keybinding-worker.cpp b/dinput/keybinding-worker.cpp index 0357752c..41fa3486 100644 --- a/dinput/keybinding-worker.cpp +++ b/dinput/keybinding-worker.cpp @@ -28,7 +28,7 @@ KeybindingWorker::~KeybindingWorker()  {      qDebug() << "exit: keybinding worker"; -    should_quit = true; +    requestInterruption();      wait();      if (dinkeyboard) {          dinkeyboard->Unacquire(); @@ -74,11 +74,10 @@ bool KeybindingWorker::init()      return true;  } -KeybindingWorker::KeybindingWorker() : dinkeyboard(nullptr), din(dinput_handle::make_di()), should_quit(false) +KeybindingWorker::KeybindingWorker() : dinkeyboard(nullptr), din(dinput_handle::make_di())  { -    should_quit = !init(); - -    start(); +    if (init()) +        start();  }  KeybindingWorker& KeybindingWorker::make() @@ -92,7 +91,7 @@ void KeybindingWorker::run()      unsigned char keystate[256] = {0};      unsigned char old_keystate[256] = {0}; -    while (!should_quit) +    while (!isInterruptionRequested())      {          {              QMutexLocker l(&mtx); diff --git a/dinput/keybinding-worker.hpp b/dinput/keybinding-worker.hpp index 553a314c..6ed277cd 100644 --- a/dinput/keybinding-worker.hpp +++ b/dinput/keybinding-worker.hpp @@ -48,7 +48,6 @@ private:      QMutex mtx;      QMainWindow fake_main_window;      dinput_handle::di_t din; -    volatile bool should_quit;      void run() override;      bool init(); diff --git a/freetrackclient/fttypes.h b/freetrackclient/fttypes.h index 050ffc02..4989a570 100644 --- a/freetrackclient/fttypes.h +++ b/freetrackclient/fttypes.h @@ -19,51 +19,46 @@  #pragma once -#ifndef _MSC_VER -#   include <inttypes.h> -#else -typedef unsigned __int32 uint32_t; -typedef __int32 int32_t; -#endif +#include <inttypes.h>  #define FREETRACK_HEAP "FT_SharedMem"  #define FREETRACK_MUTEX "FT_Mutext"  /* only 6 headpose floats and the data id are filled -sh */ -typedef struct __FTData { -    volatile unsigned int DataID; -    volatile int CamWidth; -    volatile int CamHeight; +typedef struct FTData__ { +    int32_t DataID; +    int32_t CamWidth; +    int32_t CamHeight;      /* virtual pose */ -    volatile float Yaw;   /* positive yaw to the left */ -    volatile float Pitch; /* positive pitch up */ -    volatile float Roll;  /* positive roll to the left */ -    volatile float X; -    volatile float Y; -    volatile float Z; +    float  Yaw;   /* positive yaw to the left */ +    float  Pitch; /* positive pitch up */ +    float  Roll;  /* positive roll to the left */ +    float  X; +    float  Y; +    float  Z;      /* raw pose with no smoothing, sensitivity, response curve etc. */ -    volatile float RawYaw; -    volatile float RawPitch; -    volatile float RawRoll; -    volatile float RawX; -    volatile float RawY; -    volatile float RawZ; +    float  RawYaw; +    float  RawPitch; +    float  RawRoll; +    float  RawX; +    float  RawY; +    float  RawZ;      /* raw points, sorted by Y, origin top left corner */ -    volatile float X1; -    volatile float Y1; -    volatile float X2; -    volatile float Y2; -    volatile float X3; -    volatile float Y3; -    volatile float X4; -    volatile float Y4; +    float  X1; +    float  Y1; +    float  X2; +    float  Y2; +    float  X3; +    float  Y3; +    float  X4; +    float  Y4;  } FTData;  /* we add some shit at the end for other legacy proto, sadly */ -typedef struct __FTHeap { +typedef struct FTHeap__ {      FTData data; -    volatile int32_t GameID; -    volatile unsigned char table[8]; -    volatile int32_t GameID2; +    int32_t GameID; +    unsigned char table[8]; +    int32_t GameID2;  } FTHeap; diff --git a/logic/tracker.cpp b/logic/tracker.cpp index a5d4b86f..533a6f54 100644 --- a/logic/tracker.cpp +++ b/logic/tracker.cpp @@ -44,7 +44,7 @@ Tracker::Tracker(Mappings& m, SelectedLibraries& libs, TrackLogger& logger) :  Tracker::~Tracker()  { -    set(f_should_quit, true); +    requestInterruption();      wait();  } @@ -384,7 +384,7 @@ void Tracker::run()      t.start(); -    while (!get(f_should_quit)) +    while (!isInterruptionRequested())      {          logic(); @@ -491,5 +491,4 @@ bits::bits() : b(0u)      set(f_enabled_p, true);      set(f_enabled_h, true);      set(f_zero, false); -    set(f_should_quit, false);  } diff --git a/logic/tracker.h b/logic/tracker.h index 2c4c5610..b1ed4042 100644 --- a/logic/tracker.h +++ b/logic/tracker.h @@ -40,7 +40,6 @@ struct bits          f_enabled_h      = 1 << 1,          f_enabled_p      = 1 << 2,          f_zero           = 1 << 3, -        f_should_quit    = 1 << 4,      };      std::atomic<unsigned> b; diff --git a/proto-simconnect/ftnoir_protocol_sc.cpp b/proto-simconnect/ftnoir_protocol_sc.cpp index 9c0f1a5b..be0c23c4 100644 --- a/proto-simconnect/ftnoir_protocol_sc.cpp +++ b/proto-simconnect/ftnoir_protocol_sc.cpp @@ -14,13 +14,13 @@  #include "opentrack-library-path.h"  #include "compat/timer.hpp" -simconnect::simconnect() : hSimConnect(nullptr), should_stop(false), should_reconnect(false) +simconnect::simconnect() : hSimConnect(nullptr), should_reconnect(false)  {  }  simconnect::~simconnect()  { -    should_stop = true; +    requestInterruption();      wait();  } @@ -34,7 +34,7 @@ void simconnect::run()          return;      } -    while (!should_stop) +    while (!isInterruptionRequested())      {          HRESULT hr; @@ -52,7 +52,7 @@ void simconnect::run()              Timer tm;              int idle_seconds = 0; -            while (!should_stop) +            while (!isInterruptionRequested())              {                  if (should_reconnect)                  { @@ -88,14 +88,15 @@ void simconnect::run()              qDebug() << "simconnect: can't open handle:" << hr;          } -        if (!should_stop) +        if (!isInterruptionRequested())              Sleep(100);      }      CloseHandle(event);  } -void simconnect::pose( const double *headpose ) { +void simconnect::pose( const double *headpose ) +{      // euler degrees      virtSCRotX = float(-headpose[Pitch]);      virtSCRotY = float(headpose[Yaw]); diff --git a/proto-simconnect/ftnoir_protocol_sc.h b/proto-simconnect/ftnoir_protocol_sc.h index 270d922d..93376885 100644 --- a/proto-simconnect/ftnoir_protocol_sc.h +++ b/proto-simconnect/ftnoir_protocol_sc.h @@ -11,8 +11,10 @@   */  #pragma once  #include "api/plugin-api.hpp" -  #include "ui_ftnoir_sccontrols.h" + +#include <atomic> +  #include <QThread>  #include <QMessageBox>  #include <QSettings> @@ -70,12 +72,12 @@ private:      void run() override; -    volatile float virtSCPosX; -    volatile float virtSCPosY; -    volatile float virtSCPosZ; -    volatile float virtSCRotX; -    volatile float virtSCRotY; -    volatile float virtSCRotZ; +    std::atomic<float> virtSCPosX; +    std::atomic<float> virtSCPosY; +    std::atomic<float> virtSCPosZ; +    std::atomic<float> virtSCRotX; +    std::atomic<float> virtSCRotY; +    std::atomic<float> virtSCRotZ;      importSimConnect_Open simconnect_open;      importSimConnect_Close simconnect_close; @@ -84,7 +86,7 @@ private:      importSimConnect_SubscribeToSystemEvent simconnect_subscribetosystemevent;      HANDLE hSimConnect; -    volatile bool should_stop, should_reconnect; +    std::atomic<bool> should_reconnect;      static void CALLBACK processNextSimconnectEvent(SIMCONNECT_RECV* pData, DWORD cbData, void *pContext);      settings s;      QLibrary SCClientLib; diff --git a/tracker-aruco/ftnoir_tracker_aruco.cpp b/tracker-aruco/ftnoir_tracker_aruco.cpp index 7a19b0d3..745ce50d 100644 --- a/tracker-aruco/ftnoir_tracker_aruco.cpp +++ b/tracker-aruco/ftnoir_tracker_aruco.cpp @@ -52,7 +52,6 @@ aruco_tracker::aruco_tracker() :      roi_points(4),      last_roi(65535, 65535, 0, 0),      adaptive_size_pos(0), -    stop(false),      use_otsu(false)  {      // param 2 ignored for Otsu thresholding. it's required to use our fork of Aruco. @@ -61,7 +60,7 @@ aruco_tracker::aruco_tracker() :  aruco_tracker::~aruco_tracker()  { -    stop = true; +    requestInterruption();      wait();      // fast start/stop causes breakage      portable::sleep(1000); @@ -368,7 +367,7 @@ void aruco_tracker::run()      fps_timer.start();      last_detection_timer.start(); -    while (!stop) +    while (!isInterruptionRequested())      {          {              QMutexLocker l(&camera_mtx); diff --git a/tracker-aruco/ftnoir_tracker_aruco.h b/tracker-aruco/ftnoir_tracker_aruco.h index 02b7e0a7..8531bef9 100644 --- a/tracker-aruco/ftnoir_tracker_aruco.h +++ b/tracker-aruco/ftnoir_tracker_aruco.h @@ -117,7 +117,6 @@ private:      cv::Rect last_roi;      Timer fps_timer, last_detection_timer;      unsigned adaptive_size_pos; -    volatile bool stop;      bool use_otsu;      struct resolution_tuple diff --git a/tracker-freepie-udp/ftnoir_tracker_freepie-udp.cpp b/tracker-freepie-udp/ftnoir_tracker_freepie-udp.cpp index 4c5d14ef..e5667ebb 100644 --- a/tracker-freepie-udp/ftnoir_tracker_freepie-udp.cpp +++ b/tracker-freepie-udp/ftnoir_tracker_freepie-udp.cpp @@ -6,13 +6,13 @@  #include <cmath> -tracker_freepie::tracker_freepie() : pose { 0,0,0, 0,0,0 }, should_quit(false) +tracker_freepie::tracker_freepie() : pose { 0,0,0, 0,0,0 }  {  }  tracker_freepie::~tracker_freepie()  { -    should_quit = true; +    requestInterruption();      wait();  } @@ -42,7 +42,8 @@ void tracker_freepie::run() {      sock.bind(QHostAddress::Any, (unsigned short) s.port, QUdpSocket::ShareAddress | QUdpSocket::ReuseAddressHint); -    while (!should_quit) { +    while (!isInterruptionRequested()) +    {          int order[] = {              bound<int>(s.idx_x, 0, 2),              bound<int>(s.idx_y, 0, 2), diff --git a/tracker-freepie-udp/ftnoir_tracker_freepie-udp.h b/tracker-freepie-udp/ftnoir_tracker_freepie-udp.h index 5a98e194..51a710f8 100644 --- a/tracker-freepie-udp/ftnoir_tracker_freepie-udp.h +++ b/tracker-freepie-udp/ftnoir_tracker_freepie-udp.h @@ -42,7 +42,6 @@ private:      QUdpSocket sock;      settings s;      QMutex mtx; -    volatile bool should_quit;  };  class dialog_freepie : public ITrackerDialog diff --git a/tracker-hatire/ftnoir_tracker_hat.h b/tracker-hatire/ftnoir_tracker_hat.h index 01b2afff..47f66812 100644 --- a/tracker-hatire/ftnoir_tracker_hat.h +++ b/tracker-hatire/ftnoir_tracker_hat.h @@ -5,6 +5,8 @@  #include "ftnoir_tracker_hat_settings.h"  #include "ftnoir_arduino_type.h" +#include <atomic> +  #include <QObject>  #include <QPalette>  #include <QtGui> @@ -41,7 +43,7 @@ private:      int frame_cnt; -    volatile int CptError; +    std::atomic<int> CptError;      static inline QByteArray to_latin1(const QString& str) { return str.toLatin1(); }  }; diff --git a/tracker-hydra/ftnoir_tracker_hydra.cpp b/tracker-hydra/ftnoir_tracker_hydra.cpp index 77e62243..9c0d85f8 100644 --- a/tracker-hydra/ftnoir_tracker_hydra.cpp +++ b/tracker-hydra/ftnoir_tracker_hydra.cpp @@ -23,7 +23,7 @@  #endif  #include <sixense.h> -Hydra_Tracker::Hydra_Tracker() : should_quit(false) {} +Hydra_Tracker::Hydra_Tracker() {}  #include <sixense_math.hpp> diff --git a/tracker-hydra/ftnoir_tracker_hydra.h b/tracker-hydra/ftnoir_tracker_hydra.h index 31a994d5..15016e4e 100644 --- a/tracker-hydra/ftnoir_tracker_hydra.h +++ b/tracker-hydra/ftnoir_tracker_hydra.h @@ -18,7 +18,6 @@ public:      ~Hydra_Tracker();      void start_tracker(QFrame *) override;      void data(double *data) override; -    volatile bool should_quit;  private:      settings s;      QMutex mutex; diff --git a/tracker-pt/ftnoir_tracker_pt.h b/tracker-pt/ftnoir_tracker_pt.h index b7b0019a..65e367f5 100644 --- a/tracker-pt/ftnoir_tracker_pt.h +++ b/tracker-pt/ftnoir_tracker_pt.h @@ -11,6 +11,8 @@  #include "api/plugin-api.hpp"  #include "ftnoir_tracker_pt_settings.h" +#include <atomic> +  #include "numeric.hpp"  #include "camera.h" @@ -79,9 +81,9 @@ private:      QSize preview_size; -    volatile unsigned point_count; -    volatile unsigned char commands; -    volatile bool ever_success; +    std::atomic<unsigned> point_count; +    std::atomic<unsigned char> commands; +    std::atomic<bool> ever_success;      static constexpr f rad2deg = f(180/M_PI);      //static constexpr float deg2rad = float(M_PI/180); diff --git a/tracker-pt/point_tracker.cpp b/tracker-pt/point_tracker.cpp index 8e0c28de..4022cbb2 100644 --- a/tracker-pt/point_tracker.cpp +++ b/tracker-pt/point_tracker.cpp @@ -37,7 +37,7 @@ PointModel::PointModel(settings_pt& s)      set_model(s);      // calculate u      u = M01.cross(M02); -    u /= norm(u); +    u = cv::normalize(u);      // calculate projection matrix on M01,M02 plane      f s11 = M01.dot(M01); diff --git a/tracker-tobii-eyex/tobii-eyex.hpp b/tracker-tobii-eyex/tobii-eyex.hpp index 23173f22..62b0d733 100644 --- a/tracker-tobii-eyex/tobii-eyex.hpp +++ b/tracker-tobii-eyex/tobii-eyex.hpp @@ -19,6 +19,7 @@ using namespace options;  #include <functional>  #include <atomic> +  #include <QObject>  #include <QMutex> @@ -77,7 +78,7 @@ private:      } dev_state;      double yaw, pitch; -    volatile bool do_center; +    std::atomic<bool> do_center;  };  class tobii_eyex_metadata : public Metadata diff --git a/tracker-udp/ftnoir_tracker_udp.cpp b/tracker-udp/ftnoir_tracker_udp.cpp index 5f95ab5a..e2d6fc93 100644 --- a/tracker-udp/ftnoir_tracker_udp.cpp +++ b/tracker-udp/ftnoir_tracker_udp.cpp @@ -13,13 +13,12 @@  udp::udp() :      last_recv_pose { 0,0,0, 0,0,0 }, -    last_recv_pose2 { 0,0,0, 0,0,0 }, -    should_quit(false) +    last_recv_pose2 { 0,0,0, 0,0,0 }  {}  udp::~udp()  { -    should_quit = true; +    requestInterruption();      wait();  } @@ -28,9 +27,10 @@ void udp::run()      QByteArray datagram;      datagram.resize(sizeof(last_recv_pose)); -    should_quit = !sock.bind(QHostAddress::Any, quint16(s.port), QUdpSocket::ShareAddress | QUdpSocket::ReuseAddressHint); +    if (!sock.bind(QHostAddress::Any, quint16(s.port), QUdpSocket::ShareAddress | QUdpSocket::ReuseAddressHint)) +        return; -    while (!should_quit) +    while (!isInterruptionRequested())      {          if (sock.hasPendingDatagrams())          { diff --git a/tracker-udp/ftnoir_tracker_udp.h b/tracker-udp/ftnoir_tracker_udp.h index b6e0aefd..0167a005 100644 --- a/tracker-udp/ftnoir_tracker_udp.h +++ b/tracker-udp/ftnoir_tracker_udp.h @@ -33,7 +33,6 @@ private:      double last_recv_pose[6], last_recv_pose2[6];      QMutex mutex;      settings s; -    volatile bool should_quit;  };  class dialog_udp: public ITrackerDialog | 
