Skip to content

combine shared data function #13941

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions common/graph-model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 >()
Expand Down Expand Up @@ -42,7 +42,7 @@ void graph_model::process_frame(rs2::frame f)

}
}
});
}, _m);
}

void graph_model::draw(rect stream_rect)
Expand All @@ -56,13 +56,13 @@ void graph_model::draw(rect stream_rect)
time_index[i] = static_cast<float>(i);

// Read shared history data
auto x_hist = read_shared_data<std::vector<float>>([&]() { return _x_history; });
auto y_hist = read_shared_data<std::vector<float>>([&]() { return _y_history; });
auto z_hist = read_shared_data<std::vector<float>>([&]() { return _z_history; });
auto x_hist = shared_data_access::read_shared_data<std::vector<float>>([&]() { return _x_history; }, _m);
auto y_hist = shared_data_access::read_shared_data<std::vector<float>>([&]() { return _y_history; }, _m);
auto z_hist = shared_data_access::read_shared_data<std::vector<float>>([&]() { return _z_history; }, _m);

std::vector<float> n_hist;
if(_show_n_value)
n_hist = read_shared_data<std::vector<float>>([&]() { return _n_history; });
n_hist = shared_data_access::read_shared_data<std::vector<float>>([&]() { return _n_history; }, _m);

ImGui::BeginChild(_name.c_str(), ImVec2(stream_rect.w + 2, stream_rect.h));

Expand All @@ -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();
Expand All @@ -99,7 +99,7 @@ void graph_model::clear()
if(_show_n_value)
_n_history.push_back(0);
}
});
}, _m);
}

void graph_model::pause()
Expand Down
18 changes: 2 additions & 16 deletions common/graph-model.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
#include <implot.h>
#include <mutex>
#include <vector>
#include <GLFW/glfw3.h>
#include <rsutils/time/periodic-timer.h>
#include "rect.h"

#include <librealsense2/rs.hpp>
#include "output-model.h"

namespace rs2
{
Expand All @@ -33,19 +31,7 @@ namespace rs2
void pause();
void resume();
bool is_paused();
protected:
template<class T>
T read_shared_data(std::function<T()> action)
{
std::lock_guard<std::mutex> lock(_m);
T res = action();
return res;
}
void write_shared_data(std::function<void()> action)
{
std::lock_guard<std::mutex> 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;
Expand Down
10 changes: 5 additions & 5 deletions common/output-model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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([&](){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not rs::utils it?
With some UT maybe :)
another question can't we set the mute in the class construction and not send it on each action?

Copy link
Author

@Noy-Zini Noy-Zini Apr 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added under rsutils/concurrency/shared-data-access and changed the functionality to set the mutex in the class construction.

double ts = glfwGetTime();
if (method == 1) ts = f.get_timestamp() / 1000.f;
auto it = stream_to_time.find(f.get_profile().unique_id());
Expand Down Expand Up @@ -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<std::deque<int>>([&](){ return drops_history; });
auto hist = shared_data_access::read_shared_data<std::deque<int>>([&](){ return drops_history; }, m);
for (int i = 0; i < hist.size(); i++)
{
add_point((float)i, (float)hist[i]);
Expand Down Expand Up @@ -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;
Expand All @@ -1288,5 +1288,5 @@ void frame_drops_dashboard::clear(bool full)
for (int i = 0; i < 100; i++)
drops_history.push_back(0);
}
});
}, m);
}
35 changes: 20 additions & 15 deletions common/output-model.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -50,24 +69,11 @@ namespace rs2
protected:
virtual void process_frame(rs2::frame f) = 0;

void write_shared_data(std::function<void()> action)
{
std::lock_guard<std::mutex> lock(m);
action();
}

template<class T>
T read_shared_data(std::function<T()> action)
{
std::lock_guard<std::mutex> 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()
{
Expand All @@ -80,7 +86,6 @@ namespace rs2
}
std::string name;
rs2::frame_queue q;
std::mutex m;
std::atomic<int> stop { false };
std::thread t;
std::vector<std::pair<float, float>> xy;
Expand Down
Loading