diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2018-12-20 18:23:14 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2018-12-24 19:31:24 +0100 |
commit | e81df263f4123a39fe6d4d50fb21f47dd242e796 (patch) | |
tree | 4b8cd13da31ac3fb3a2d2695b65595d7c5570439 | |
parent | 2613beb8028ecac53548d311b27ff38559763f6c (diff) |
remove const correctness violations
This is possibly related to a livelock where several threads do const
removal in their respective code paths.
Use the `mutable' specifier for the mutexes and spline's cached data.
Now using the `mutable' specifier, get rid of <optional> in
compat/mutex.
-rw-r--r-- | compat/copyable-mutex.cpp | 22 | ||||
-rw-r--r-- | compat/copyable-mutex.hpp | 14 | ||||
-rw-r--r-- | logic/pipeline.cpp | 3 | ||||
-rw-r--r-- | logic/pipeline.hpp | 2 | ||||
-rw-r--r-- | options/bundle.cpp | 12 | ||||
-rw-r--r-- | options/bundle.hpp | 15 | ||||
-rw-r--r-- | spline/spline.cpp | 19 | ||||
-rw-r--r-- | spline/spline.hpp | 29 | ||||
-rw-r--r-- | tracker-pt/pt-api.hpp | 5 |
9 files changed, 41 insertions, 80 deletions
diff --git a/compat/copyable-mutex.cpp b/compat/copyable-mutex.cpp index dde84c83..17b5aa34 100644 --- a/compat/copyable-mutex.cpp +++ b/compat/copyable-mutex.cpp @@ -1,18 +1,19 @@ #include "copyable-mutex.hpp" +#include <cstdlib> -mutex& mutex::operator=(const mutex& datum) +mutex& mutex::operator=(const mutex& rhs) { - inner.emplace(datum->isRecursive() ? QMutex::Recursive : QMutex::NonRecursive); + if (rhs->isRecursive() != inner.isRecursive()) + std::abort(); + return *this; } -mutex::mutex(const mutex& datum) +mutex::mutex(const mutex& datum) : mutex{datum.inner.isRecursive() ? Recursive : NonRecursive} { - *this = datum; } -mutex::mutex(mutex::mode m) : - inner { std::in_place, static_cast<QMutex::RecursionMode>(int(m)) } +mutex::mutex(RecursionMode m) : inner{m} { } @@ -21,13 +22,12 @@ QMutex* mutex::operator&() const return *this; } -QMutex* mutex::operator->() const +mutex::operator QMutex*() const { - return *this; + return &inner; } -mutex::operator QMutex*() const +QMutex* mutex::operator->() const { - return const_cast<QMutex*>(&inner.value()); + return *this; } - diff --git a/compat/copyable-mutex.hpp b/compat/copyable-mutex.hpp index 46c6c88c..37e6802b 100644 --- a/compat/copyable-mutex.hpp +++ b/compat/copyable-mutex.hpp @@ -1,25 +1,21 @@ #pragma once -#include <optional> - #include <QMutex> #include "export.hpp" class OTR_COMPAT_EXPORT mutex { - std::optional<QMutex> inner; + mutable QMutex inner; public: - enum mode - { - recursive = QMutex::Recursive, - normal = QMutex::NonRecursive, - }; + using RecursionMode = QMutex::RecursionMode; + static constexpr inline RecursionMode Recursive = RecursionMode::Recursive; + static constexpr inline RecursionMode NonRecursive = RecursionMode::NonRecursive; mutex& operator=(const mutex& datum); mutex(const mutex& datum); - explicit mutex(mode m = normal); + explicit mutex(RecursionMode m); QMutex* operator&() const; operator QMutex*() const; diff --git a/logic/pipeline.cpp b/logic/pipeline.cpp index 3bdbc4ff..219ccf62 100644 --- a/logic/pipeline.cpp +++ b/logic/pipeline.cpp @@ -357,7 +357,6 @@ Pose pipeline::maybe_apply_filter(const Pose& value) const Pose pipeline::apply_zero_pos(Pose value) const { - // custom zero position for (int i = 0; i < 6; i++) value(i) += m(i).opts.zero * (m(i).opts.invert ? -1 : 1); @@ -579,7 +578,7 @@ void pipeline::run() void pipeline::raw_and_mapped_pose(double* mapped, double* raw) const { - QMutexLocker foo(&const_cast<pipeline&>(*this).mtx); + QMutexLocker foo(&mtx); for (int i = 0; i < 6; i++) { diff --git a/logic/pipeline.hpp b/logic/pipeline.hpp index 9fdd6c32..a4dcb6b8 100644 --- a/logic/pipeline.hpp +++ b/logic/pipeline.hpp @@ -81,7 +81,7 @@ class OTR_LOGIC_EXPORT pipeline : private QThread { Q_OBJECT - QMutex mtx; + mutable QMutex mtx; main_settings s; Mappings& m; event_handler& ev; diff --git a/options/bundle.cpp b/options/bundle.cpp index 8db4f906..938639b3 100644 --- a/options/bundle.cpp +++ b/options/bundle.cpp @@ -20,15 +20,7 @@ using namespace options::globals; namespace options::detail { -mutex::mutex(QMutex::RecursionMode mode) : QMutex(mode) {} - -mutex::operator QMutex*() const -{ - return const_cast<QMutex*>(static_cast<const QMutex*>(this)); -} - bundle::bundle(const QString& group_name) : - mtx(QMutex::Recursive), group_name(group_name), saved(group_name), transient(saved) @@ -82,13 +74,13 @@ void bundle::store_kv(const QString& name, const QVariant& new_value) QVariant bundle::get_variant(const QString& name) const { - QMutexLocker l(mtx); + QMutexLocker l(&mtx); return transient.get_variant(name); } bool bundle::contains(const QString &name) const { - QMutexLocker l(mtx); + QMutexLocker l(&mtx); return transient.contains(name); } diff --git a/options/bundle.hpp b/options/bundle.hpp index 9ab7f74c..4c2b9781 100644 --- a/options/bundle.hpp +++ b/options/bundle.hpp @@ -29,13 +29,6 @@ #include "export.hpp" namespace options::detail { - class OTR_OPTIONS_EXPORT mutex final : public QMutex - { - public: - explicit mutex(QMutex::RecursionMode mode); - cc_noinline operator QMutex*() const; // NOLINT - }; - class bundle; } // ns options::detail @@ -51,7 +44,7 @@ class OTR_OPTIONS_EXPORT bundle final : public QObject, public connector { Q_OBJECT - mutex mtx; + mutable QMutex mtx { QMutex::Recursive }; const QString group_name; group saved; group transient; @@ -65,7 +58,7 @@ public: bundle(const bundle&) = delete; bundle& operator=(const bundle&) = delete; - QMutex* get_mtx() const override { return mtx; } + QMutex* get_mtx() const override { return &mtx; } QString name() const { return group_name; } explicit bundle(const QString& group_name); @@ -100,8 +93,8 @@ private: friend OTR_OPTIONS_EXPORT std::shared_ptr<v> options::make_bundle(const QString& name); - [[nodiscard]] std::shared_ptr<v> make_bundle_(const k& key); - [[nodiscard]] static bundler& bundler_singleton(); + std::shared_ptr<v> make_bundle_(const k& key); + static bundler& bundler_singleton(); bundler(); ~bundler(); diff --git a/spline/spline.cpp b/spline/spline.cpp index 752e85d1..9b0147a8 100644 --- a/spline/spline.cpp +++ b/spline/spline.cpp @@ -75,11 +75,6 @@ float spline::get_value(double x) float spline::get_value_no_save(double x) const { - return const_cast<spline&>(*this).get_value_no_save_internal(x); -} - -float spline::get_value_no_save_internal(double x) -{ QMutexLocker foo(&_mutex); float q = float(x * bucket_size_coefficient(points)); @@ -98,7 +93,7 @@ bool spline::get_last_value(QPointF& point) return activep; } -float spline::get_value_internal(int x) +float spline::get_value_internal(int x) const { if (!validp) { @@ -112,12 +107,6 @@ float spline::get_value_internal(int x) return sign * clamp(ret_, 0, 1000); } -void spline::add_lone_point() -{ - points = { QPointF(s->opts.clamp_x_, s->opts.clamp_y_) }; - s->points = points; -} - QPointF spline::ensure_in_bounds(const QList<QPointF>& points, int i) { const int sz = points.size(); @@ -148,7 +137,7 @@ bool spline::sort_fn(const QPointF& one, const QPointF& two) return one.x() < two.x(); } -void spline::update_interp_data() +void spline::update_interp_data() const { points_t list = points; ensure_valid(list); @@ -208,7 +197,7 @@ void spline::update_interp_data() }; // multiplier helps fill in all the x's needed - const unsigned end = int(c_interp * (p2_x - p1_x)) + 1; + const unsigned end{int(c_interp * (p2_x - p1_x)) + 1u}; for (unsigned k = 0; k <= end; k++) { @@ -363,7 +352,7 @@ double spline::max_output() const return std::fabs(clamp.to<double>()); } -void spline::ensure_valid(points_t& list) +void spline::ensure_valid(points_t& list) const { QMutexLocker foo(&_mutex); diff --git a/spline/spline.hpp b/spline/spline.hpp index 3d2d6e57..acac2ad7 100644 --- a/spline/spline.hpp +++ b/spline/spline.hpp @@ -8,12 +8,10 @@ #pragma once -#include "compat/copyable-mutex.hpp" #include "options/options.hpp" - #include "axis-opts.hpp" - #include "export.hpp" +#include "compat/copyable-mutex.hpp" #include <cstddef> #include <vector> @@ -95,10 +93,8 @@ struct OTR_SPLINE_EXPORT base_spline : base_spline_, spline_modify_mixin, spline class OTR_SPLINE_EXPORT spline : public base_spline { double bucket_size_coefficient(const QList<QPointF>& points) const; - void update_interp_data(); - float get_value_internal(int x); - void add_lone_point(); - float get_value_no_save_internal(double x); + void update_interp_data() const; + float get_value_internal(int x) const; static bool sort_fn(const QPointF& one, const QPointF& two); static QPointF ensure_in_bounds(const QList<QPointF>& points, int i); @@ -106,21 +102,20 @@ class OTR_SPLINE_EXPORT spline : public base_spline void disconnect_signals(); + mutex _mutex { mutex::Recursive }; std::shared_ptr<spline_detail::settings> s; QMetaObject::Connection conn_changed, conn_maxx, conn_maxy; + mutable std::vector<float> data = std::vector<float>(value_count, float(-16)); + mutable QPointF last_input_value; + mutable bool activep = false; + mutable bool validp = false; - static constexpr inline std::size_t value_count = 4096; - - std::vector<float> data = std::vector<float>(value_count, float(-16)); - - mutex _mutex { mutex::recursive }; - QPointF last_input_value; std::shared_ptr<QObject> ctx { std::make_shared<QObject>() }; - bool activep = false; - bool validp = false; + // cached s->points + mutable points_t points; - points_t points; + static constexpr inline std::size_t value_count = 4096; public: void invalidate_settings(); @@ -153,7 +148,7 @@ public: void set_tracking_active(bool value) override; bundle get_bundle(); - void ensure_valid(points_t& in_out); + void ensure_valid(points_t& in_out) const; std::shared_ptr<spline_detail::base_settings> get_settings() override; std::shared_ptr<const spline_detail::base_settings> get_settings() const override; diff --git a/tracker-pt/pt-api.hpp b/tracker-pt/pt-api.hpp index 12085560..6c36ebaf 100644 --- a/tracker-pt/pt-api.hpp +++ b/tracker-pt/pt-api.hpp @@ -42,16 +42,13 @@ struct pt_frame : pt_pixel_pos_mixin template<typename t> t* as() & { - using u = remove_cvref_t<t>; - static_assert(std::is_convertible_v<u*, pt_frame*>, "must be derived from pt_frame"); - return static_cast<t*>(this); } template<typename t> t const* as_const() const& { - return const_cast<pt_frame*>(this)->as<const t>(); + return static_cast<t const*>(this); } }; |