Skip to content

Commit 0d5c8c4

Browse files
committed
create shared_data_access class
1 parent 7b6380a commit 0d5c8c4

File tree

5 files changed

+59
-37
lines changed

5 files changed

+59
-37
lines changed

common/graph-model.cpp

Lines changed: 8 additions & 8 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-
shared_data_access::write_shared_data(
10+
shared_data.write(
1111
[&]()
1212
{
1313
if (!_paused && f && f.is< rs2::motion_frame >()
@@ -42,7 +42,7 @@ void graph_model::process_frame(rs2::frame f)
4242

4343
}
4444
}
45-
}, _m);
45+
});
4646
}
4747

4848
void graph_model::draw(rect stream_rect)
@@ -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 = shared_data_access::read_shared_data<std::vector<float>>([&]() { return _x_history; }, _m);
60-
auto y_hist = shared_data_access::read_shared_data<std::vector<float>>([&]() { return _y_history; }, _m);
61-
auto z_hist = shared_data_access::read_shared_data<std::vector<float>>([&]() { return _z_history; }, _m);
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 = shared_data_access::read_shared_data<std::vector<float>>([&]() { return _n_history; }, _m);
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-
shared_data_access::write_shared_data(
86+
shared_data.write(
8787
[&]()
8888
{
8989
_x_history.clear();
@@ -99,7 +99,7 @@ void graph_model::clear()
9999
if(_show_n_value)
100100
_n_history.push_back(0);
101101
}
102-
}, _m);
102+
});
103103
}
104104

105105
void graph_model::pause()

common/graph-model.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
#include <implot.h>
88
#include <mutex>
99
#include <vector>
10+
#include <GLFW/glfw3.h>
1011
#include <rsutils/time/periodic-timer.h>
11-
12-
#include "output-model.h"
12+
#include "rect.h"
13+
#include <librealsense2/rs.hpp>
14+
#include <rsutils/concurrency/shared-data-access.h>
1315

1416
namespace rs2
1517
{
@@ -20,7 +22,8 @@ namespace rs2
2022
: _name(name),
2123
_stream_type(stream),
2224
_show_n_value(show_n_value),
23-
_update_timer{ std::chrono::milliseconds(_update_rate) }
25+
_update_timer{ std::chrono::milliseconds(_update_rate) },
26+
shared_data(_m)
2427
{
2528
clear();
2629
}
@@ -42,6 +45,7 @@ namespace rs2
4245
const int VECTOR_SIZE = 300;
4346
std::vector< float > _x_history, _y_history, _z_history, _n_history;
4447

48+
rsutils::concurrency::shared_data_access shared_data;
4549
std::mutex _m;
4650
std::string _name;
4751

common/output-model.cpp

Lines changed: 5 additions & 5 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-
shared_data_access::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());
@@ -1236,12 +1236,12 @@ void frame_drops_dashboard::process_frame(rs2::frame f)
12361236
}
12371237

12381238
stream_to_time[f.get_profile().unique_id()] = ts;
1239-
}, m );
1239+
});
12401240
}
12411241

12421242
void frame_drops_dashboard::draw(ux_window& win, rect r)
12431243
{
1244-
auto hist = shared_data_access::read_shared_data<std::deque<int>>([&](){ return drops_history; }, m);
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-
shared_data_access::write_shared_data([&](){
1280+
shared_data.write([&](){
12811281
stream_to_time.clear();
12821282
last_time = 0;
12831283
*total = 0;
@@ -1288,5 +1288,5 @@ void frame_drops_dashboard::clear(bool full)
12881288
for (int i = 0; i < 100; i++)
12891289
drops_history.push_back(0);
12901290
}
1291-
}, m);
1291+
});
12921292
}

common/output-model.h

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,17 @@
1818
#include <imgui.h>
1919

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

2223
namespace rs2
2324
{
2425
class device_model;
2526
class ux_window;
2627

27-
class shared_data_access
28-
{
29-
public:
30-
shared_data_access() = delete;
31-
32-
static void write_shared_data( const std::function< void() > & action, std::mutex & m )
33-
{
34-
std::lock_guard< std::mutex > lock( m );
35-
action();
36-
}
37-
38-
template< class T >
39-
static T read_shared_data( const std::function< T() > & action, std::mutex & m )
40-
{
41-
std::lock_guard< std::mutex > lock( m );
42-
return action();
43-
}
44-
};
45-
4628
class stream_dashboard
4729
{
4830
public:
49-
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(); }) {}
5032
virtual ~stream_dashboard()
5133
{
5234
stop = true;
@@ -73,7 +55,8 @@ namespace rs2
7355

7456
void draw_dashboard(ux_window& win, rect& r);
7557

76-
std::mutex m;
58+
rsutils::concurrency::shared_data_access shared_data;
59+
7760
private:
7861
void thread_function()
7962
{
@@ -86,6 +69,7 @@ namespace rs2
8669
}
8770
std::string name;
8871
rs2::frame_queue q;
72+
std::mutex m;
8973
std::atomic<int> stop { false };
9074
std::thread t;
9175
std::vector<std::pair<float, float>> xy;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
6+
namespace rsutils {
7+
namespace concurrency {
8+
9+
class shared_data_access
10+
{
11+
public:
12+
shared_data_access(std::mutex& mutex_ref)
13+
: m_mutex(mutex_ref) {
14+
}
15+
16+
void write(const std::function<void()>& action)
17+
{
18+
std::lock_guard<std::mutex> lock(m_mutex);
19+
action();
20+
}
21+
22+
template<class T>
23+
T read(const std::function<T()>& action) const
24+
{
25+
std::lock_guard<std::mutex> lock(m_mutex);
26+
return action();
27+
}
28+
29+
private:
30+
std::mutex& m_mutex;
31+
};
32+
33+
}
34+
}

0 commit comments

Comments
 (0)