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 all commits
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
12 changes: 6 additions & 6 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.write(
[&]()
{
if (!_paused && f && f.is< rs2::motion_frame >()
Expand Down Expand Up @@ -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.read<std::vector<float>>([&]() { return _x_history; });
auto y_hist = shared_data.read<std::vector<float>>([&]() { return _y_history; });
auto z_hist = shared_data.read<std::vector<float>>([&]() { return _z_history; });

std::vector<float> n_hist;
if(_show_n_value)
n_hist = read_shared_data<std::vector<float>>([&]() { return _n_history; });
n_hist = shared_data.read<std::vector<float>>([&]() { return _n_history; });

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.write(
[&]()
{
_x_history.clear();
Expand Down
20 changes: 5 additions & 15 deletions common/graph-model.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
#include <GLFW/glfw3.h>
#include <rsutils/time/periodic-timer.h>
#include "rect.h"

#include <librealsense2/rs.hpp>
#include <rsutils/concurrency/shared-data-access.h>

namespace rs2
{
Expand All @@ -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();
}
Expand All @@ -33,19 +34,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 All @@ -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;

Expand Down
6 changes: 3 additions & 3 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.write([&](){
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 @@ -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<std::deque<int>>([&](){ return drops_history; });
auto hist = shared_data.read<std::deque<int>>([&](){ return drops_history; });
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.write([&](){
stream_to_time.clear();
last_time = 0;
*total = 0;
Expand Down
19 changes: 4 additions & 15 deletions common/output-model.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <imgui.h>

#include <rsutils/concurrency/concurrency.h>
#include <rsutils/concurrency/shared-data-access.h>

namespace rs2
{
Expand All @@ -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;
Expand All @@ -50,24 +51,12 @@ 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);

rsutils::concurrency::shared_data_access shared_data;

private:
void thread_function()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <mutex>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please add all needed includes this file use from std.
I see for example
std::function

Copy link
Author

Choose a reason for hiding this comment

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

You're right I added it, thanks

#include <functional>

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<void()>& action)
{
std::lock_guard<std::mutex> lock(m_mutex);
action();
}

template<class T>
T read(const std::function<T()>& action) const
{
std::lock_guard<std::mutex> lock(m_mutex);
return action();
}

private:
std::mutex& m_mutex;
};

}
}
Loading