Merge pull request #2028 from unknownbrackets/console-th

Keep only one actual console writer thread (Windows)
This commit is contained in:
Henrik Rydgård 2013-06-02 00:27:09 -07:00
commit 946b7bd310
2 changed files with 44 additions and 28 deletions

View File

@ -37,6 +37,15 @@ const int LOG_PENDING_MAX = 120 * 10000;
const int LOG_LATENCY_DELAY_MS = 20; const int LOG_LATENCY_DELAY_MS = 20;
const int LOG_SHUTDOWN_DELAY_MS = 250; const int LOG_SHUTDOWN_DELAY_MS = 250;
const int LOG_MAX_DISPLAY_LINES = 4000; const int LOG_MAX_DISPLAY_LINES = 4000;
int ConsoleListener::refCount = 0;
HANDLE ConsoleListener::hThread = NULL;
HANDLE ConsoleListener::hTriggerEvent = NULL;
CRITICAL_SECTION ConsoleListener::criticalSection;
char *ConsoleListener::logPending = NULL;
volatile u32 ConsoleListener::logPendingReadPos = 0;
volatile u32 ConsoleListener::logPendingWritePos = 0;
#endif #endif
ConsoleListener::ConsoleListener() ConsoleListener::ConsoleListener()
@ -45,12 +54,12 @@ ConsoleListener::ConsoleListener()
hConsole = NULL; hConsole = NULL;
bUseColor = true; bUseColor = true;
hThread = NULL; if (hTriggerEvent == NULL)
hTriggerEvent = CreateEvent(NULL, FALSE, FALSE, NULL); {
InitializeCriticalSection(&criticalSection); hTriggerEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
logPending = NULL; InitializeCriticalSection(&criticalSection);
logPendingReadPos = 0; }
logPendingWritePos = 0; ++refCount;
#else #else
bUseColor = isatty(fileno(stdout)); bUseColor = isatty(fileno(stdout));
#endif #endif
@ -59,13 +68,6 @@ ConsoleListener::ConsoleListener()
ConsoleListener::~ConsoleListener() ConsoleListener::~ConsoleListener()
{ {
Close(); Close();
#ifdef _WIN32
DeleteCriticalSection(&criticalSection);
if (logPending != NULL)
delete [] logPending;
#endif
} }
// 100, 100, "Dolphin Log Console" // 100, 100, "Dolphin Log Console"
@ -96,7 +98,7 @@ void ConsoleListener::Open(bool Hidden, int Width, int Height, const char *Title
hConsole = GetStdHandle(STD_OUTPUT_HANDLE); hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
} }
if (hTriggerEvent != NULL) if (hTriggerEvent != NULL && hThread == NULL)
{ {
logPending = new char[LOG_PENDING_MAX]; logPending = new char[LOG_PENDING_MAX];
hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &ConsoleListener::RunThread, this, 0, NULL); hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &ConsoleListener::RunThread, this, 0, NULL);
@ -135,17 +137,30 @@ void ConsoleListener::Close()
if (hConsole == NULL) if (hConsole == NULL)
return; return;
if (hThread != NULL) if (--refCount <= 0)
{ {
Common::AtomicStoreRelease(logPendingWritePos, (u32) -1); if (hThread != NULL)
{
Common::AtomicStoreRelease(logPendingWritePos, (u32) -1);
SetEvent(hTriggerEvent); SetEvent(hTriggerEvent);
WaitForSingleObject(hThread, LOG_SHUTDOWN_DELAY_MS); WaitForSingleObject(hThread, LOG_SHUTDOWN_DELAY_MS);
CloseHandle(hThread); CloseHandle(hThread);
hThread = NULL; hThread = NULL;
}
if (hTriggerEvent != NULL)
{
DeleteCriticalSection(&criticalSection);
CloseHandle(hTriggerEvent);
hTriggerEvent = NULL;
}
if (logPending != NULL)
{
delete [] logPending;
logPending = NULL;
}
refCount = 0;
} }
if (hTriggerEvent != NULL)
CloseHandle(hTriggerEvent);
FreeConsole(); FreeConsole();
hConsole = NULL; hConsole = NULL;

View File

@ -55,13 +55,14 @@ private:
void SendToThread(LogTypes::LOG_LEVELS Level, const char *Text); void SendToThread(LogTypes::LOG_LEVELS Level, const char *Text);
void WriteToConsole(LogTypes::LOG_LEVELS Level, const char *Text, size_t Len); void WriteToConsole(LogTypes::LOG_LEVELS Level, const char *Text, size_t Len);
HANDLE hThread; static int refCount;
HANDLE hTriggerEvent; static HANDLE hThread;
CRITICAL_SECTION criticalSection; static HANDLE hTriggerEvent;
static CRITICAL_SECTION criticalSection;
char *logPending; static char *logPending;
volatile u32 logPendingReadPos; static volatile u32 logPendingReadPos;
volatile u32 logPendingWritePos; static volatile u32 logPendingWritePos;
#endif #endif
bool bHidden; bool bHidden;
bool bUseColor; bool bUseColor;