SObjectizer 5.8
Loading...
Searching...
No Matches
std_controller.cpp
Go to the documentation of this file.
1/*
2 * SObjectizer-5
3 */
4
5/*!
6 * \file
7 * \since
8 * v.5.5.4
9 *
10 * \brief A standard implementation of controller for run-time monitoring.
11 */
12
13#include <so_5/stats/impl/std_controller.hpp>
14
15#include <so_5/stats/messages.hpp>
16
17#include <so_5/send_functions.hpp>
18
19namespace so_5
20{
21
22namespace stats
23{
24
25namespace impl
26{
27
28//
29// std_controller_t
30//
31
32std_controller_t::std_controller_t(
33 mbox_t mbox )
34 : m_mbox( std::move( mbox ) )
35 {}
36
37const mbox_t &
38std_controller_t::mbox() const
39 {
40 return m_mbox;
41 }
42
43void
44std_controller_t::turn_on()
45 {
46 std::lock_guard< std::mutex > lock{ m_start_stop_lock };
47
49 {
50 // Distribution thread must be started.
53 new std::thread( [this] { body(); } ) );
54 }
55 }
56
57void
58std_controller_t::turn_off()
59 {
60 std::lock_guard< std::mutex > lock{ m_start_stop_lock };
61
63 {
64 {
65 // Send shutdown signal to work thread.
66 std::lock_guard< std::mutex > cond_lock{ m_data_lock };
68
69 m_wake_up_cond.notify_one();
70 }
71
72 // Wait for work thread termination.
74
75 // Pointer to work thread must be dropped.
76 // This allows to start new working thread.
78 }
79 }
80
81std::chrono::steady_clock::duration
82std_controller_t::set_distribution_period(
83 std::chrono::steady_clock::duration period )
84 {
85 std::lock_guard< std::mutex > lock{ m_data_lock };
86
87 auto ret_value = m_distribution_period;
88
89 m_distribution_period = period;
90
91 return ret_value;
92 }
93
94void
95std_controller_t::add( source_t & what )
96 {
97 std::lock_guard< std::mutex > lock{ m_data_lock };
98
100 }
101
102void
103std_controller_t::remove( source_t & what ) noexcept
104 {
105 std::lock_guard< std::mutex > lock{ m_data_lock };
106
108 }
109
110void
111std_controller_t::body()
112 {
113 while( true )
114 {
115 std::unique_lock< std::mutex > lock{ m_data_lock };
116
118 return;
119
120 const auto actual_duration = distribute_current_data();
121
122 if( actual_duration < m_distribution_period )
123 // There is some time to sleep.
124 m_wake_up_cond.wait_for(
125 lock,
126 m_distribution_period - actual_duration );
127 }
128 }
129
130std::chrono::steady_clock::duration
131std_controller_t::distribute_current_data()
132 {
133 auto started_at = std::chrono::steady_clock::now();
134
136
137 source_t * s = m_head;
138 while( s )
139 {
141
142 s = source_list_next( *s );
143 }
144
146
147 return std::chrono::steady_clock::now() - started_at;
148 }
149
150} /* namespace impl */
151
152} /* namespace stats */
153
154} /* namespace so_5 */
intrusive_ptr_t(intrusive_ptr_t &&o) noexcept
Move constructor.
std::condition_variable m_wake_up_cond
Condition for wake-up data-distribution thread.
const mbox_t m_mbox
Mbox for sending monitoring data.
std::chrono::steady_clock::duration m_distribution_period
Data-distribution period.
void body()
Main body of data distribution thread.
source_t * m_tail
Tail of data sources list.
std::chrono::steady_clock::duration distribute_current_data()
Initiates distribution of current values for all data sources.
std::mutex m_data_lock
Object lock for data-related operations.
std::mutex m_start_stop_lock
Object lock for start/stop operations.
virtual void add(source_t &what) override
Registration of new data source.
virtual const mbox_t & mbox() const override
Get the mbox for receiving monitoring information.
virtual void remove(source_t &what) noexcept override
Deregistration of previously registered data source.
bool m_shutdown_initiated
Shutdown signal.
virtual void turn_on() override
Turn the monitoring on.
virtual void turn_off() override
Turn the monitoring off.
std::unique_ptr< std::thread > m_distribution_thread
Main data-distribution thread.
source_t * m_head
Head of data sources list.
virtual std::chrono::steady_clock::duration set_distribution_period(std::chrono::steady_clock::duration period) override
Set distribution period.
static source_t * source_list_next(const source_t &what) noexcept
Helper method for accessing next data source in the list.
static void source_list_add(source_t &what, source_t *&head, source_t *&tail) noexcept
Helper method for adding data source to existing list.
static void source_list_remove(source_t &what, source_t *&head, source_t *&tail) noexcept
Helper method for removing data source from existing list.
An interface of data source.
virtual void distribute(const mbox_t &)=0
Send appropriate notification about the current value.
Internal implementation of run-time monitoring and statistics related stuff.
Declarations of messages used by run-time monitoring and statistics.
Definition messages.hpp:36
All stuff related to run-time monitoring and statistics.
Private part of message limit implementation.
Definition agent.cpp:33
void send(Target &&to, Args &&... args)
A utility function for creating and delivering a message or a signal.
Notification about finish of stats distribution.
Definition messages.hpp:100
Notification about start of new stats distribution.
Definition messages.hpp:90