From 7b6380a0379c0bee83fae2e344de47448ddd23f2 Mon Sep 17 00:00:00 2001 From: noy zini Date: Mon, 14 Apr 2025 16:37:28 +0300 Subject: [PATCH 1/2] combine shared data function --- common/graph-model.cpp | 16 ++++++++-------- common/graph-model.h | 18 ++---------------- common/output-model.cpp | 10 +++++----- common/output-model.h | 35 ++++++++++++++++++++--------------- 4 files changed, 35 insertions(+), 44 deletions(-) diff --git a/common/graph-model.cpp b/common/graph-model.cpp index f25b2e4b1d..737a7df5b5 100644 --- a/common/graph-model.cpp +++ b/common/graph-model.cpp @@ -7,7 +7,7 @@ using namespace rs2; void graph_model::process_frame(rs2::frame f) { - write_shared_data( + shared_data_access::write_shared_data( [&]() { if (!_paused && f && f.is< rs2::motion_frame >() @@ -42,7 +42,7 @@ void graph_model::process_frame(rs2::frame f) } } - }); + }, _m); } void graph_model::draw(rect stream_rect) @@ -56,13 +56,13 @@ void graph_model::draw(rect stream_rect) time_index[i] = static_cast(i); // Read shared history data - auto x_hist = read_shared_data>([&]() { return _x_history; }); - auto y_hist = read_shared_data>([&]() { return _y_history; }); - auto z_hist = read_shared_data>([&]() { return _z_history; }); + auto x_hist = shared_data_access::read_shared_data>([&]() { return _x_history; }, _m); + auto y_hist = shared_data_access::read_shared_data>([&]() { return _y_history; }, _m); + auto z_hist = shared_data_access::read_shared_data>([&]() { return _z_history; }, _m); std::vector n_hist; if(_show_n_value) - n_hist = read_shared_data>([&]() { return _n_history; }); + n_hist = shared_data_access::read_shared_data>([&]() { return _n_history; }, _m); ImGui::BeginChild(_name.c_str(), ImVec2(stream_rect.w + 2, stream_rect.h)); @@ -83,7 +83,7 @@ void graph_model::draw(rect stream_rect) void graph_model::clear() { - write_shared_data( + shared_data_access::write_shared_data( [&]() { _x_history.clear(); @@ -99,7 +99,7 @@ void graph_model::clear() if(_show_n_value) _n_history.push_back(0); } - }); + }, _m); } void graph_model::pause() diff --git a/common/graph-model.h b/common/graph-model.h index 04e5ff99bb..8c6aa66e36 100644 --- a/common/graph-model.h +++ b/common/graph-model.h @@ -7,11 +7,9 @@ #include #include #include -#include #include -#include "rect.h" -#include +#include "output-model.h" namespace rs2 { @@ -33,19 +31,7 @@ namespace rs2 void pause(); void resume(); bool is_paused(); - protected: - template - T read_shared_data(std::function action) - { - std::lock_guard lock(_m); - T res = action(); - return res; - } - void write_shared_data(std::function action) - { - std::lock_guard lock(_m); - action(); - } + private: float _x_value = 0.0f, _y_value = 0.0f, _z_value = 0.0f, _n_value = 0.0f; bool _show_n_value; diff --git a/common/output-model.cpp b/common/output-model.cpp index 228be4a98d..7871b32937 100644 --- a/common/output-model.cpp +++ b/common/output-model.cpp @@ -1204,7 +1204,7 @@ void stream_dashboard::draw_dashboard(ux_window& win, rect& r) void frame_drops_dashboard::process_frame(rs2::frame f) { - write_shared_data([&](){ + shared_data_access::write_shared_data([&](){ double ts = glfwGetTime(); if (method == 1) ts = f.get_timestamp() / 1000.f; auto it = stream_to_time.find(f.get_profile().unique_id()); @@ -1236,12 +1236,12 @@ void frame_drops_dashboard::process_frame(rs2::frame f) } stream_to_time[f.get_profile().unique_id()] = ts; - }); + }, m ); } void frame_drops_dashboard::draw(ux_window& win, rect r) { - auto hist = read_shared_data>([&](){ return drops_history; }); + auto hist = shared_data_access::read_shared_data>([&](){ return drops_history; }, m); for (int i = 0; i < hist.size(); i++) { add_point((float)i, (float)hist[i]); @@ -1277,7 +1277,7 @@ int frame_drops_dashboard::get_height() const void frame_drops_dashboard::clear(bool full) { - write_shared_data([&](){ + shared_data_access::write_shared_data([&](){ stream_to_time.clear(); last_time = 0; *total = 0; @@ -1288,5 +1288,5 @@ void frame_drops_dashboard::clear(bool full) for (int i = 0; i < 100; i++) drops_history.push_back(0); } - }); + }, m); } diff --git a/common/output-model.h b/common/output-model.h index 5989710264..fb373d9027 100644 --- a/common/output-model.h +++ b/common/output-model.h @@ -24,6 +24,25 @@ namespace rs2 class device_model; class ux_window; + class shared_data_access + { + public: + shared_data_access() = delete; + + static void write_shared_data( const std::function< void() > & action, std::mutex & m ) + { + std::lock_guard< std::mutex > lock( m ); + action(); + } + + template< class T > + static T read_shared_data( const std::function< T() > & action, std::mutex & m ) + { + std::lock_guard< std::mutex > lock( m ); + return action(); + } + }; + class stream_dashboard { public: @@ -50,24 +69,11 @@ namespace rs2 protected: virtual void process_frame(rs2::frame f) = 0; - void write_shared_data(std::function action) - { - std::lock_guard lock(m); - action(); - } - - template - T read_shared_data(std::function action) - { - std::lock_guard lock(m); - T res = action(); - return res; - } - void add_point(float x, float y) { xy.push_back(std::make_pair(x, y)); } void draw_dashboard(ux_window& win, rect& r); + std::mutex m; private: void thread_function() { @@ -80,7 +86,6 @@ namespace rs2 } std::string name; rs2::frame_queue q; - std::mutex m; std::atomic stop { false }; std::thread t; std::vector> xy; From 771bf0e1d69be6cb26d2d92b8921d424a23e131e Mon Sep 17 00:00:00 2001 From: noy zini Date: Sun, 27 Apr 2025 12:04:05 +0300 Subject: [PATCH 2/2] create shared_data_access class --- common/graph-model.cpp | 16 ++++----- common/graph-model.h | 10 ++++-- common/output-model.cpp | 10 +++--- common/output-model.h | 26 +++----------- .../rsutils/concurrency/shared-data-access.h | 35 +++++++++++++++++++ 5 files changed, 60 insertions(+), 37 deletions(-) create mode 100644 third-party/rsutils/include/rsutils/concurrency/shared-data-access.h diff --git a/common/graph-model.cpp b/common/graph-model.cpp index 737a7df5b5..b77ad10b7c 100644 --- a/common/graph-model.cpp +++ b/common/graph-model.cpp @@ -7,7 +7,7 @@ using namespace rs2; void graph_model::process_frame(rs2::frame f) { - shared_data_access::write_shared_data( + shared_data.write( [&]() { if (!_paused && f && f.is< rs2::motion_frame >() @@ -42,7 +42,7 @@ void graph_model::process_frame(rs2::frame f) } } - }, _m); + }); } void graph_model::draw(rect stream_rect) @@ -56,13 +56,13 @@ void graph_model::draw(rect stream_rect) time_index[i] = static_cast(i); // Read shared history data - auto x_hist = shared_data_access::read_shared_data>([&]() { return _x_history; }, _m); - auto y_hist = shared_data_access::read_shared_data>([&]() { return _y_history; }, _m); - auto z_hist = shared_data_access::read_shared_data>([&]() { return _z_history; }, _m); + auto x_hist = shared_data.read>([&]() { return _x_history; }); + auto y_hist = shared_data.read>([&]() { return _y_history; }); + auto z_hist = shared_data.read>([&]() { return _z_history; }); std::vector n_hist; if(_show_n_value) - n_hist = shared_data_access::read_shared_data>([&]() { return _n_history; }, _m); + n_hist = shared_data.read>([&]() { return _n_history; }); ImGui::BeginChild(_name.c_str(), ImVec2(stream_rect.w + 2, stream_rect.h)); @@ -83,7 +83,7 @@ void graph_model::draw(rect stream_rect) void graph_model::clear() { - shared_data_access::write_shared_data( + shared_data.write( [&]() { _x_history.clear(); @@ -99,7 +99,7 @@ void graph_model::clear() if(_show_n_value) _n_history.push_back(0); } - }, _m); + }); } void graph_model::pause() diff --git a/common/graph-model.h b/common/graph-model.h index 8c6aa66e36..515f211e01 100644 --- a/common/graph-model.h +++ b/common/graph-model.h @@ -7,9 +7,11 @@ #include #include #include +#include #include - -#include "output-model.h" +#include "rect.h" +#include +#include namespace rs2 { @@ -20,7 +22,8 @@ namespace rs2 : _name(name), _stream_type(stream), _show_n_value(show_n_value), - _update_timer{ std::chrono::milliseconds(_update_rate) } + _update_timer{ std::chrono::milliseconds(_update_rate) }, + shared_data(_m) { clear(); } @@ -42,6 +45,7 @@ namespace rs2 const int VECTOR_SIZE = 300; std::vector< float > _x_history, _y_history, _z_history, _n_history; + rsutils::concurrency::shared_data_access shared_data; std::mutex _m; std::string _name; diff --git a/common/output-model.cpp b/common/output-model.cpp index 7871b32937..9a2be825d6 100644 --- a/common/output-model.cpp +++ b/common/output-model.cpp @@ -1204,7 +1204,7 @@ void stream_dashboard::draw_dashboard(ux_window& win, rect& r) void frame_drops_dashboard::process_frame(rs2::frame f) { - shared_data_access::write_shared_data([&](){ + shared_data.write([&](){ double ts = glfwGetTime(); if (method == 1) ts = f.get_timestamp() / 1000.f; auto it = stream_to_time.find(f.get_profile().unique_id()); @@ -1236,12 +1236,12 @@ void frame_drops_dashboard::process_frame(rs2::frame f) } stream_to_time[f.get_profile().unique_id()] = ts; - }, m ); + }); } void frame_drops_dashboard::draw(ux_window& win, rect r) { - auto hist = shared_data_access::read_shared_data>([&](){ return drops_history; }, m); + auto hist = shared_data.read>([&](){ return drops_history; }); for (int i = 0; i < hist.size(); i++) { add_point((float)i, (float)hist[i]); @@ -1277,7 +1277,7 @@ int frame_drops_dashboard::get_height() const void frame_drops_dashboard::clear(bool full) { - shared_data_access::write_shared_data([&](){ + shared_data.write([&](){ stream_to_time.clear(); last_time = 0; *total = 0; @@ -1288,5 +1288,5 @@ void frame_drops_dashboard::clear(bool full) for (int i = 0; i < 100; i++) drops_history.push_back(0); } - }, m); + }); } diff --git a/common/output-model.h b/common/output-model.h index fb373d9027..73d346e34c 100644 --- a/common/output-model.h +++ b/common/output-model.h @@ -18,35 +18,17 @@ #include #include +#include namespace rs2 { class device_model; class ux_window; - class shared_data_access - { - public: - shared_data_access() = delete; - - static void write_shared_data( const std::function< void() > & action, std::mutex & m ) - { - std::lock_guard< std::mutex > lock( m ); - action(); - } - - template< class T > - static T read_shared_data( const std::function< T() > & action, std::mutex & m ) - { - std::lock_guard< std::mutex > lock( m ); - return action(); - } - }; - class stream_dashboard { public: - stream_dashboard(std::string name, int size) : q(size), name(name), t([this](){ thread_function(); }) {} + stream_dashboard(std::string name, int size) : q(size), name(name), shared_data(m), t([this](){ thread_function(); }) {} virtual ~stream_dashboard() { stop = true; @@ -73,7 +55,8 @@ namespace rs2 void draw_dashboard(ux_window& win, rect& r); - std::mutex m; + rsutils::concurrency::shared_data_access shared_data; + private: void thread_function() { @@ -86,6 +69,7 @@ namespace rs2 } std::string name; rs2::frame_queue q; + std::mutex m; std::atomic stop { false }; std::thread t; std::vector> xy; diff --git a/third-party/rsutils/include/rsutils/concurrency/shared-data-access.h b/third-party/rsutils/include/rsutils/concurrency/shared-data-access.h new file mode 100644 index 0000000000..93ef1b928e --- /dev/null +++ b/third-party/rsutils/include/rsutils/concurrency/shared-data-access.h @@ -0,0 +1,35 @@ +// License: Apache 2.0. See LICENSE file in root directory. +// Copyright(c) 2025 Intel Corporation. All Rights Reserved. +#pragma once +#include +#include + +namespace rsutils { + namespace concurrency { + + class shared_data_access + { + public: + shared_data_access(std::mutex& mutex_ref) + : m_mutex(mutex_ref) { + } + + void write(const std::function& action) + { + std::lock_guard lock(m_mutex); + action(); + } + + template + T read(const std::function& action) const + { + std::lock_guard lock(m_mutex); + return action(); + } + + private: + std::mutex& m_mutex; + }; + + } +}