Move the cleaning of events to destructor of protocol manager

So that the last one who deletes it can clear all the remaining data
properly (ie if it's STKHost listening thread)

Remove the assert in async update thread as it may not be true for
the first thread creation
This commit is contained in:
Benau 2018-02-16 00:48:27 +08:00
parent 05f7c014dd
commit c5788a2c90
2 changed files with 38 additions and 38 deletions

View File

@ -38,15 +38,24 @@ std::weak_ptr<ProtocolManager> ProtocolManager::m_protocol_manager;
// ============================================================================
std::shared_ptr<ProtocolManager> ProtocolManager::createInstance()
{
if (!m_protocol_manager.expired())
if (!emptyInstance())
{
Log::fatal("ProtocolManager",
"Create only 1 instance of ProtocolManager!");
return NULL;
}
auto pm = std::make_shared<ProtocolManager>();
pm->m_asynchronous_update_thread = std::thread(
std::bind(&ProtocolManager::startAsynchronousUpdateThread, pm));
pm->m_asynchronous_update_thread = std::thread([pm]()
{
VS::setThreadName("ProtocolManager");
while(!pm->m_exit.load())
{
pm->asynchronousUpdate();
PROFILER_PUSH_CPU_MARKER("sleep", 0, 255, 255);
StkTime::sleep(2);
PROFILER_POP_CPU_MARKER();
}
});
m_protocol_manager = pm;
return pm;
} // createInstance
@ -57,6 +66,12 @@ std::shared_ptr<ProtocolManager> ProtocolManager::lock()
return m_protocol_manager.lock();
} // lock
// ----------------------------------------------------------------------------
bool ProtocolManager::emptyInstance()
{
return m_protocol_manager.expired();
} // emptyInstance
// ----------------------------------------------------------------------------
ProtocolManager::ProtocolManager()
{
@ -67,37 +82,6 @@ ProtocolManager::ProtocolManager()
// ----------------------------------------------------------------------------
ProtocolManager::~ProtocolManager()
{
} // ~ProtocolManager
// ----------------------------------------------------------------------------
void ProtocolManager::startAsynchronousUpdateThread()
{
VS::setThreadName("ProtocolManager");
while(!m_exit.load())
{
asynchronousUpdate();
PROFILER_PUSH_CPU_MARKER("sleep", 0, 255, 255);
StkTime::sleep(2);
PROFILER_POP_CPU_MARKER();
}
} // startAsynchronousUpdateThread
// ----------------------------------------------------------------------------
void ProtocolManager::OneProtocolType::abort()
{
for (unsigned int i = 0; i < m_protocols.getData().size(); i++)
delete m_protocols.getData()[i];
m_protocols.getData().clear();
} // OneProtocolType::abort
// ----------------------------------------------------------------------------
/** \brief Stops the protocol manager.
*/
void ProtocolManager::abort()
{
m_exit.store(true);
m_asynchronous_update_thread.join(); // wait the thread to finish
// Now only this main thread is active, no more need for locks
for (unsigned int i = 0; i < m_all_protocols.size(); i++)
{
@ -122,6 +106,24 @@ void ProtocolManager::abort()
m_requests.getData().clear();
m_requests.unlock();
} // ~ProtocolManager
// ----------------------------------------------------------------------------
void ProtocolManager::OneProtocolType::abort()
{
for (unsigned int i = 0; i < m_protocols.getData().size(); i++)
delete m_protocols.getData()[i];
m_protocols.getData().clear();
} // OneProtocolType::abort
// ----------------------------------------------------------------------------
/** \brief Stops the protocol manager.
*/
void ProtocolManager::abort()
{
m_exit.store(true);
// wait the thread to finish
m_asynchronous_update_thread.join();
} // abort
// ----------------------------------------------------------------------------
@ -477,9 +479,6 @@ void ProtocolManager::update(float dt)
void ProtocolManager::asynchronousUpdate()
{
PROFILER_PUSH_CPU_MARKER("Message delivery", 255, 0, 0);
// Update from ProtocolManager thread only:
assert(std::this_thread::get_id() == m_asynchronous_update_thread.get_id());
// First deliver asynchronous messages for all protocols
// =====================================================
m_async_events_to_process.lock();

View File

@ -220,7 +220,6 @@ private:
/*! Single instance of protocol manager.*/
static std::weak_ptr<ProtocolManager> m_protocol_manager;
void startAsynchronousUpdateThread();
bool sendEvent(Event* event);
virtual void startProtocol(Protocol *protocol);
@ -251,6 +250,8 @@ public:
// ------------------------------------------------------------------------
static std::shared_ptr<ProtocolManager> createInstance();
// ------------------------------------------------------------------------
static bool emptyInstance();
// ------------------------------------------------------------------------
static std::shared_ptr<ProtocolManager> lock();
}; // class ProtocolManager