diff --git a/common/graph-model.cpp b/common/graph-model.cpp index f25b2e4b1d..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) { - write_shared_data( + shared_data.write( [&]() { if (!_paused && f && f.is< rs2::motion_frame >() @@ -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.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 = read_shared_data>([&]() { return _n_history; }); + 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() { - write_shared_data( + shared_data.write( [&]() { _x_history.clear(); diff --git a/common/graph-model.h b/common/graph-model.h index 04e5ff99bb..515f211e01 100644 --- a/common/graph-model.h +++ b/common/graph-model.h @@ -10,8 +10,8 @@ #include #include #include "rect.h" - #include +#include namespace rs2 { @@ -22,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(); } @@ -33,19 +34,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; @@ -56,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 228be4a98d..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) { - 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()); @@ -1241,7 +1241,7 @@ void frame_drops_dashboard::process_frame(rs2::frame f) void frame_drops_dashboard::draw(ux_window& win, rect r) { - auto hist = read_shared_data>([&](){ return drops_history; }); + 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) { - write_shared_data([&](){ + shared_data.write([&](){ stream_to_time.clear(); last_time = 0; *total = 0; diff --git a/common/output-model.h b/common/output-model.h index 5989710264..73d346e34c 100644 --- a/common/output-model.h +++ b/common/output-model.h @@ -18,6 +18,7 @@ #include #include +#include namespace rs2 { @@ -27,7 +28,7 @@ namespace rs2 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; @@ -50,24 +51,12 @@ 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); + rsutils::concurrency::shared_data_access shared_data; + private: void thread_function() { 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; + }; + + } +}