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() std::shared_ptr<ProtocolManager> ProtocolManager::createInstance()
{ {
if (!m_protocol_manager.expired()) if (!emptyInstance())
{ {
Log::fatal("ProtocolManager", Log::fatal("ProtocolManager",
"Create only 1 instance of ProtocolManager!"); "Create only 1 instance of ProtocolManager!");
return NULL; return NULL;
} }
auto pm = std::make_shared<ProtocolManager>(); auto pm = std::make_shared<ProtocolManager>();
pm->m_asynchronous_update_thread = std::thread( pm->m_asynchronous_update_thread = std::thread([pm]()
std::bind(&ProtocolManager::startAsynchronousUpdateThread, 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; m_protocol_manager = pm;
return pm; return pm;
} // createInstance } // createInstance
@ -57,6 +66,12 @@ std::shared_ptr<ProtocolManager> ProtocolManager::lock()
return m_protocol_manager.lock(); return m_protocol_manager.lock();
} // lock } // lock
// ----------------------------------------------------------------------------
bool ProtocolManager::emptyInstance()
{
return m_protocol_manager.expired();
} // emptyInstance
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
ProtocolManager::ProtocolManager() ProtocolManager::ProtocolManager()
{ {
@ -67,37 +82,6 @@ ProtocolManager::ProtocolManager()
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
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 // Now only this main thread is active, no more need for locks
for (unsigned int i = 0; i < m_all_protocols.size(); i++) for (unsigned int i = 0; i < m_all_protocols.size(); i++)
{ {
@ -122,6 +106,24 @@ void ProtocolManager::abort()
m_requests.getData().clear(); m_requests.getData().clear();
m_requests.unlock(); 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 } // abort
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -477,9 +479,6 @@ void ProtocolManager::update(float dt)
void ProtocolManager::asynchronousUpdate() void ProtocolManager::asynchronousUpdate()
{ {
PROFILER_PUSH_CPU_MARKER("Message delivery", 255, 0, 0); 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 // First deliver asynchronous messages for all protocols
// ===================================================== // =====================================================
m_async_events_to_process.lock(); m_async_events_to_process.lock();

View File

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