1
0
Fork 0

cNetwork: Fixed possible hang when terminating immediately after init.

This commit is contained in:
Mattes D 2016-06-17 16:25:31 +02:00
parent 4d9769a484
commit 8610083a8e
2 changed files with 27 additions and 2 deletions

View File

@ -6,7 +6,6 @@
#include "Globals.h"
#include "NetworkSingleton.h"
#include <event2/event.h>
#include <event2/thread.h>
#include <event2/bufferevent.h>
#include <event2/dns.h>
@ -91,8 +90,11 @@ void cNetworkSingleton::Initialise(void)
}
// Create the event loop thread:
m_EventLoopThread = std::thread(RunEventLoop, this);
m_HasTerminated = false;
m_StartupEvent.reset(new cEvent);
m_EventLoopThread = std::thread(RunEventLoop, this);
m_StartupEvent->Wait(); // Wait for the LibEvent loop to actually start running (otherwise calling Terminate too soon would hang, see #3228)
m_StartupEvent.reset(); // Don't need the cEvent any more, release all its resources
}
@ -153,6 +155,9 @@ void cNetworkSingleton::LogCallback(int a_Severity, const char * a_Msg)
void cNetworkSingleton::RunEventLoop(cNetworkSingleton * a_Self)
{
auto timer = evtimer_new(a_Self->m_EventBase, SignalizeStartup, a_Self);
timeval timeout{}; // Zero timeout - execute immediately
evtimer_add(timer, &timeout);
event_base_loop(a_Self->m_EventBase, EVLOOP_NO_EXIT_ON_EMPTY);
}
@ -160,6 +165,18 @@ void cNetworkSingleton::RunEventLoop(cNetworkSingleton * a_Self)
void cNetworkSingleton::SignalizeStartup(evutil_socket_t a_Socket, short a_Events, void * a_Self)
{
auto self = reinterpret_cast<cNetworkSingleton *>(a_Self);
ASSERT(self != nullptr);
ASSERT(self->m_StartupEvent != nullptr);
self->m_StartupEvent->Set();
}
void cNetworkSingleton::AddHostnameLookup(cHostnameLookupPtr a_HostnameLookup)
{
ASSERT(!m_HasTerminated);

View File

@ -13,6 +13,7 @@
#pragma once
#include <event2/event.h>
#include "Network.h"
#include "CriticalSection.h"
#include "Event.h"
@ -127,11 +128,18 @@ protected:
/** The thread in which the main LibEvent loop runs. */
std::thread m_EventLoopThread;
/** Event that is signalled once the startup is finished and the LibEvent loop is running. */
UniquePtr<cEvent> m_StartupEvent;
/** Converts LibEvent-generated log events into log messages in MCS log. */
static void LogCallback(int a_Severity, const char * a_Msg);
/** Implements the thread that runs LibEvent's event dispatcher loop. */
static void RunEventLoop(cNetworkSingleton * a_Self);
/** Callback called by LibEvent when the event loop is started. */
static void SignalizeStartup(evutil_socket_t a_Socket, short a_Events, void * a_Self);
};