Skip to content

Commit 58931ae

Browse files
authored
PR #13941 from Noy-Zini: combine shared data function
2 parents 684cf10 + 771bf0e commit 58931ae

File tree

5 files changed

+53
-39
lines changed

5 files changed

+53
-39
lines changed

common/graph-model.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ using namespace rs2;
77

88
void graph_model::process_frame(rs2::frame f)
99
{
10-
write_shared_data(
10+
shared_data.write(
1111
[&]()
1212
{
1313
if (!_paused && f && f.is< rs2::motion_frame >()
@@ -56,13 +56,13 @@ void graph_model::draw(rect stream_rect)
5656
time_index[i] = static_cast<float>(i);
5757

5858
// Read shared history data
59-
auto x_hist = read_shared_data<std::vector<float>>([&]() { return _x_history; });
60-
auto y_hist = read_shared_data<std::vector<float>>([&]() { return _y_history; });
61-
auto z_hist = read_shared_data<std::vector<float>>([&]() { return _z_history; });
59+
auto x_hist = shared_data.read<std::vector<float>>([&]() { return _x_history; });
60+
auto y_hist = shared_data.read<std::vector<float>>([&]() { return _y_history; });
61+
auto z_hist = shared_data.read<std::vector<float>>([&]() { return _z_history; });
6262

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

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

@@ -83,7 +83,7 @@ void graph_model::draw(rect stream_rect)
8383

8484
void graph_model::clear()
8585
{
86-
write_shared_data(
86+
shared_data.write(
8787
[&]()
8888
{
8989
_x_history.clear();

common/graph-model.h

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
#include <GLFW/glfw3.h>
1111
#include <rsutils/time/periodic-timer.h>
1212
#include "rect.h"
13-
1413
#include <librealsense2/rs.hpp>
14+
#include <rsutils/concurrency/shared-data-access.h>
1515

1616
namespace rs2
1717
{
@@ -22,7 +22,8 @@ namespace rs2
2222
: _name(name),
2323
_stream_type(stream),
2424
_show_n_value(show_n_value),
25-
_update_timer{ std::chrono::milliseconds(_update_rate) }
25+
_update_timer{ std::chrono::milliseconds(_update_rate) },
26+
shared_data(_m)
2627
{
2728
clear();
2829
}
@@ -33,19 +34,7 @@ namespace rs2
3334
void pause();
3435
void resume();
3536
bool is_paused();
36-
protected:
37-
template<class T>
38-
T read_shared_data(std::function<T()> action)
39-
{
40-
std::lock_guard<std::mutex> lock(_m);
41-
T res = action();
42-
return res;
43-
}
44-
void write_shared_data(std::function<void()> action)
45-
{
46-
std::lock_guard<std::mutex> lock(_m);
47-
action();
48-
}
37+
4938
private:
5039
float _x_value = 0.0f, _y_value = 0.0f, _z_value = 0.0f, _n_value = 0.0f;
5140
bool _show_n_value;
@@ -56,6 +45,7 @@ namespace rs2
5645
const int VECTOR_SIZE = 300;
5746
std::vector< float > _x_history, _y_history, _z_history, _n_history;
5847

48+
rsutils::concurrency::shared_data_access shared_data;
5949
std::mutex _m;
6050
std::string _name;
6151

common/output-model.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,7 +1204,7 @@ void stream_dashboard::draw_dashboard(ux_window& win, rect& r)
12041204

12051205
void frame_drops_dashboard::process_frame(rs2::frame f)
12061206
{
1207-
write_shared_data([&](){
1207+
shared_data.write([&](){
12081208
double ts = glfwGetTime();
12091209
if (method == 1) ts = f.get_timestamp() / 1000.f;
12101210
auto it = stream_to_time.find(f.get_profile().unique_id());
@@ -1241,7 +1241,7 @@ void frame_drops_dashboard::process_frame(rs2::frame f)
12411241

12421242
void frame_drops_dashboard::draw(ux_window& win, rect r)
12431243
{
1244-
auto hist = read_shared_data<std::deque<int>>([&](){ return drops_history; });
1244+
auto hist = shared_data.read<std::deque<int>>([&](){ return drops_history; });
12451245
for (int i = 0; i < hist.size(); i++)
12461246
{
12471247
add_point((float)i, (float)hist[i]);
@@ -1277,7 +1277,7 @@ int frame_drops_dashboard::get_height() const
12771277

12781278
void frame_drops_dashboard::clear(bool full)
12791279
{
1280-
write_shared_data([&](){
1280+
shared_data.write([&](){
12811281
stream_to_time.clear();
12821282
last_time = 0;
12831283
*total = 0;

common/output-model.h

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <imgui.h>
1919

2020
#include <rsutils/concurrency/concurrency.h>
21+
#include <rsutils/concurrency/shared-data-access.h>
2122

2223
namespace rs2
2324
{
@@ -27,7 +28,7 @@ namespace rs2
2728
class stream_dashboard
2829
{
2930
public:
30-
stream_dashboard(std::string name, int size) : q(size), name(name), t([this](){ thread_function(); }) {}
31+
stream_dashboard(std::string name, int size) : q(size), name(name), shared_data(m), t([this](){ thread_function(); }) {}
3132
virtual ~stream_dashboard()
3233
{
3334
stop = true;
@@ -50,24 +51,12 @@ namespace rs2
5051
protected:
5152
virtual void process_frame(rs2::frame f) = 0;
5253

53-
void write_shared_data(std::function<void()> action)
54-
{
55-
std::lock_guard<std::mutex> lock(m);
56-
action();
57-
}
58-
59-
template<class T>
60-
T read_shared_data(std::function<T()> action)
61-
{
62-
std::lock_guard<std::mutex> lock(m);
63-
T res = action();
64-
return res;
65-
}
66-
6754
void add_point(float x, float y) { xy.push_back(std::make_pair(x, y)); }
6855

6956
void draw_dashboard(ux_window& win, rect& r);
7057

58+
rsutils::concurrency::shared_data_access shared_data;
59+
7160
private:
7261
void thread_function()
7362
{
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// License: Apache 2.0. See LICENSE file in root directory.
2+
// Copyright(c) 2025 Intel Corporation. All Rights Reserved.
3+
#pragma once
4+
#include <mutex>
5+
#include <functional>
6+
7+
namespace rsutils {
8+
namespace concurrency {
9+
10+
class shared_data_access
11+
{
12+
public:
13+
shared_data_access(std::mutex& mutex_ref)
14+
: m_mutex(mutex_ref) {
15+
}
16+
17+
void write(const std::function<void()>& action)
18+
{
19+
std::lock_guard<std::mutex> lock(m_mutex);
20+
action();
21+
}
22+
23+
template<class T>
24+
T read(const std::function<T()>& action) const
25+
{
26+
std::lock_guard<std::mutex> lock(m_mutex);
27+
return action();
28+
}
29+
30+
private:
31+
std::mutex& m_mutex;
32+
};
33+
34+
}
35+
}

0 commit comments

Comments
 (0)