mirror of
https://github.com/XRPLF/rippled.git
synced 2025-11-27 14:35:52 +00:00
Convert Resource::Manager to use std::thread (RIPD-236)
This commit is contained in:
committed by
Vinnie Falco
parent
43a480f02d
commit
8296041f9e
@@ -18,90 +18,110 @@
|
|||||||
//==============================================================================
|
//==============================================================================
|
||||||
|
|
||||||
#include <BeastConfig.h>
|
#include <BeastConfig.h>
|
||||||
#include <ripple/basics/chrono.h>
|
|
||||||
#include <ripple/resource/ResourceManager.h>
|
#include <ripple/resource/ResourceManager.h>
|
||||||
|
#include <ripple/basics/chrono.h>
|
||||||
|
#include <ripple/basics/Log.h> // JLOG
|
||||||
#include <beast/threads/Thread.h>
|
#include <beast/threads/Thread.h>
|
||||||
#include <beast/cxx14/memory.h> // <memory>
|
#include <beast/cxx14/memory.h> // <memory>
|
||||||
|
|
||||||
namespace ripple {
|
namespace ripple {
|
||||||
namespace Resource {
|
namespace Resource {
|
||||||
|
|
||||||
class ManagerImp
|
class ManagerImp : public Manager
|
||||||
: public Manager
|
|
||||||
, public beast::Thread
|
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
beast::Journal m_journal;
|
beast::Journal journal_;
|
||||||
Logic m_logic;
|
Logic logic_;
|
||||||
|
std::thread thread_;
|
||||||
|
std::atomic<bool> run_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ManagerImp (beast::insight::Collector::ptr const& collector,
|
ManagerImp (beast::insight::Collector::ptr const& collector,
|
||||||
beast::Journal journal)
|
beast::Journal journal)
|
||||||
: Thread ("Resource::Manager")
|
: journal_ (journal)
|
||||||
, m_journal (journal)
|
, logic_ (collector, stopwatch(), journal)
|
||||||
, m_logic (collector, stopwatch(), journal)
|
, thread_ ()
|
||||||
|
, run_ (true)
|
||||||
{
|
{
|
||||||
startThread ();
|
thread_ = std::thread {&ManagerImp::run, this};
|
||||||
}
|
}
|
||||||
|
|
||||||
~ManagerImp ()
|
ManagerImp () = delete;
|
||||||
|
ManagerImp (ManagerImp const&) = delete;
|
||||||
|
ManagerImp& operator= (ManagerImp const&) = delete;
|
||||||
|
|
||||||
|
~ManagerImp () override
|
||||||
{
|
{
|
||||||
stopThread ();
|
run_.store (false);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
thread_.join();
|
||||||
|
}
|
||||||
|
catch (std::exception ex)
|
||||||
|
{
|
||||||
|
// Swallow the exception in a destructor.
|
||||||
|
JLOG(journal_.warning) << "std::exception in Resource::~Manager. "
|
||||||
|
<< ex.what();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Consumer newInboundEndpoint (beast::IP::Endpoint const& address)
|
Consumer newInboundEndpoint (beast::IP::Endpoint const& address) override
|
||||||
{
|
{
|
||||||
return m_logic.newInboundEndpoint (address);
|
return logic_.newInboundEndpoint (address);
|
||||||
}
|
}
|
||||||
|
|
||||||
Consumer newOutboundEndpoint (beast::IP::Endpoint const& address)
|
Consumer newOutboundEndpoint (beast::IP::Endpoint const& address) override
|
||||||
{
|
{
|
||||||
return m_logic.newOutboundEndpoint (address);
|
return logic_.newOutboundEndpoint (address);
|
||||||
}
|
}
|
||||||
|
|
||||||
Consumer newAdminEndpoint (std::string const& name)
|
Consumer newAdminEndpoint (std::string const& name) override
|
||||||
{
|
{
|
||||||
return m_logic.newAdminEndpoint (name);
|
return logic_.newAdminEndpoint (name);
|
||||||
}
|
}
|
||||||
|
|
||||||
Gossip exportConsumers ()
|
Gossip exportConsumers () override
|
||||||
{
|
{
|
||||||
return m_logic.exportConsumers();
|
return logic_.exportConsumers();
|
||||||
}
|
}
|
||||||
|
|
||||||
void importConsumers (std::string const& origin, Gossip const& gossip)
|
void importConsumers (
|
||||||
|
std::string const& origin, Gossip const& gossip) override
|
||||||
{
|
{
|
||||||
m_logic.importConsumers (origin, gossip);
|
logic_.importConsumers (origin, gossip);
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------
|
//--------------------------------------------------------------------------
|
||||||
|
|
||||||
Json::Value getJson ()
|
Json::Value getJson () override
|
||||||
{
|
{
|
||||||
return m_logic.getJson ();
|
return logic_.getJson ();
|
||||||
}
|
}
|
||||||
|
|
||||||
Json::Value getJson (int threshold)
|
Json::Value getJson (int threshold) override
|
||||||
{
|
{
|
||||||
return m_logic.getJson (threshold);
|
return logic_.getJson (threshold);
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------
|
//--------------------------------------------------------------------------
|
||||||
|
|
||||||
void onWrite (beast::PropertyStream::Map& map)
|
void onWrite (beast::PropertyStream::Map& map) override
|
||||||
{
|
{
|
||||||
m_logic.onWrite (map);
|
logic_.onWrite (map);
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------
|
//--------------------------------------------------------------------------
|
||||||
|
|
||||||
|
private:
|
||||||
void run ()
|
void run ()
|
||||||
{
|
{
|
||||||
|
beast::Thread::setCurrentThreadName ("Resource::Manager");
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
m_logic.periodicActivity();
|
logic_.periodicActivity();
|
||||||
wait (1000);
|
std::this_thread::sleep_for (std::chrono::seconds (1));
|
||||||
}
|
}
|
||||||
while (! threadShouldExit ());
|
while (run_.load());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user