Skip to content

Commit

Permalink
Improve observer_set class (#3924)
Browse files Browse the repository at this point in the history
* Improve observer_set

* Move observer_set class to its own file
  • Loading branch information
dsiganos committed Aug 27, 2022
1 parent 441ab5e commit 7221c2f
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 50 deletions.
1 change: 1 addition & 0 deletions nano/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ add_library(
memory.cpp
numbers.hpp
numbers.cpp
observer_set.hpp
optional_ptr.hpp
rate_limiting.hpp
rate_limiting.cpp
Expand Down
55 changes: 55 additions & 0 deletions nano/lib/observer_set.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#pragma once

#include <nano/lib/locks.hpp>
#include <nano/lib/utility.hpp>

#include <functional>
#include <vector>

namespace nano
{
template <typename... T>
class observer_set final
{
public:
void add (std::function<void (T...)> const & observer_a)
{
nano::lock_guard<nano::mutex> lock (mutex);
observers.push_back (observer_a);
}

void notify (T... args) const
{
nano::unique_lock<nano::mutex> lock (mutex);
auto observers_copy = observers;
lock.unlock ();

for (auto & i : observers_copy)
{
i (args...);
}
}

bool empty () const
{
nano::lock_guard<nano::mutex> lock (mutex);
return observers.empty ();
}

std::unique_ptr<container_info_component> collect_container_info (std::string const & name) const
{
nano::unique_lock<nano::mutex> lock (mutex);
auto count = observers.size ();
lock.unlock ();
auto sizeof_element = sizeof (typename decltype (observers)::value_type);
auto composite = std::make_unique<container_info_composite> (name);
composite->add_component (std::make_unique<container_info_leaf> (container_info{ "observers", count, sizeof_element }));
return composite;
}

private:
mutable nano::mutex mutex{ mutex_identifier (mutexes::observer_set) };
std::vector<std::function<void (T...)>> observers;
};

}
2 changes: 1 addition & 1 deletion nano/lib/stats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ void nano::stat::update (uint32_t key_a, uint64_t value)
entry->samples.push_back (entry->sample_current);
entry->sample_current.set_value (0);

if (!entry->sample_observers.observers.empty ())
if (!entry->sample_observers.empty ())
{
auto snapshot (entry->samples);
entry->sample_observers.notify (snapshot);
Expand Down
1 change: 1 addition & 0 deletions nano/lib/stats.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <nano/lib/errors.hpp>
#include <nano/lib/observer_set.hpp>
#include <nano/lib/utility.hpp>

#include <boost/circular_buffer.hpp>
Expand Down
39 changes: 0 additions & 39 deletions nano/lib/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,45 +147,6 @@ std::string generate_stacktrace ();
std::size_t get_file_descriptor_limit ();
void set_file_descriptor_limit (std::size_t limit);

template <typename... T>
class observer_set final
{
public:
void add (std::function<void (T...)> const & observer_a)
{
nano::lock_guard<nano::mutex> lock (mutex);
observers.push_back (observer_a);
}
void notify (T... args)
{
nano::unique_lock<nano::mutex> lock (mutex);
auto observers_copy = observers;
lock.unlock ();

for (auto & i : observers_copy)
{
i (args...);
}
}
nano::mutex mutex{ mutex_identifier (mutexes::observer_set) };
std::vector<std::function<void (T...)>> observers;
};

template <typename... T>
std::unique_ptr<container_info_component> collect_container_info (observer_set<T...> & observer_set, std::string const & name)
{
size_t count = 0;
{
nano::lock_guard<nano::mutex> lock (observer_set.mutex);
count = observer_set.observers.size ();
}

auto sizeof_element = sizeof (typename decltype (observer_set.observers)::value_type);
auto composite = std::make_unique<container_info_composite> (name);
composite->add_component (std::make_unique<container_info_leaf> (container_info{ "observers", count, sizeof_element }));
return composite;
}

void remove_all_files_in_dir (boost::filesystem::path const & dir);
void move_all_files_to_dir (boost::filesystem::path const & from, boost::filesystem::path const & to);

Expand Down
2 changes: 1 addition & 1 deletion nano/lib/work.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,6 @@ std::unique_ptr<nano::container_info_component> nano::collect_container_info (wo
auto sizeof_element = sizeof (decltype (work_pool.pending)::value_type);
auto composite = std::make_unique<container_info_composite> (name);
composite->add_component (std::make_unique<container_info_leaf> (container_info{ "pending", count, sizeof_element }));
composite->add_component (collect_container_info (work_pool.work_observers, "work_observers"));
composite->add_component (work_pool.work_observers.collect_container_info ("work_observers"));
return composite;
}
1 change: 1 addition & 0 deletions nano/lib/work.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <nano/lib/config.hpp>
#include <nano/lib/locks.hpp>
#include <nano/lib/numbers.hpp>
#include <nano/lib/observer_set.hpp>
#include <nano/lib/utility.hpp>

#include <boost/optional.hpp>
Expand Down
18 changes: 9 additions & 9 deletions nano/node/node_observers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
std::unique_ptr<nano::container_info_component> nano::collect_container_info (nano::node_observers & node_observers, std::string const & name)
{
auto composite = std::make_unique<nano::container_info_composite> (name);
composite->add_component (collect_container_info (node_observers.blocks, "blocks"));
composite->add_component (collect_container_info (node_observers.wallet, "wallet"));
composite->add_component (collect_container_info (node_observers.vote, "vote"));
composite->add_component (collect_container_info (node_observers.active_started, "active_started"));
composite->add_component (collect_container_info (node_observers.active_stopped, "active_stopped"));
composite->add_component (collect_container_info (node_observers.account_balance, "account_balance"));
composite->add_component (collect_container_info (node_observers.endpoint, "endpoint"));
composite->add_component (collect_container_info (node_observers.disconnect, "disconnect"));
composite->add_component (collect_container_info (node_observers.work_cancel, "work_cancel"));
composite->add_component (node_observers.blocks.collect_container_info ("blocks"));
composite->add_component (node_observers.wallet.collect_container_info ("wallet"));
composite->add_component (node_observers.vote.collect_container_info ("vote"));
composite->add_component (node_observers.active_started.collect_container_info ("active_started"));
composite->add_component (node_observers.active_stopped.collect_container_info ("active_stopped"));
composite->add_component (node_observers.account_balance.collect_container_info ("account_balance"));
composite->add_component (node_observers.endpoint.collect_container_info ("endpoint"));
composite->add_component (node_observers.disconnect.collect_container_info ("disconnect"));
composite->add_component (node_observers.work_cancel.collect_container_info ("work_cancel"));
return composite;
}

0 comments on commit 7221c2f

Please sign in to comment.