revert that idea, laggy as hell and unstable-ish. hard cap thread count. still leave conversion in addtext

This commit is contained in:
Akash Mozumdar 2018-10-04 22:10:27 -04:00
parent 68bafcdda7
commit 5f74684b46
5 changed files with 18 additions and 19 deletions

View File

@ -35,7 +35,9 @@ namespace
LOCK(hostMutex); LOCK(hostMutex);
TextThread *it; TextThread *it;
if ((it = textThreadsByParams[tp]) == nullptr) if ((it = textThreadsByParams[tp]) == nullptr)
if (TextThread::ThreadCounter < MAX_THREAD_COUNT)
OnCreate(it = textThreadsByParams[tp] = new TextThread(tp, Host::GetHookParam(tp).type)); OnCreate(it = textThreadsByParams[tp] = new TextThread(tp, Host::GetHookParam(tp).type));
else return Host::AddConsoleOutput(L"too many text threads: stopping");
it->AddText(text, len); it->AddText(text, len);
} }

View File

@ -32,7 +32,7 @@ namespace Host
inline std::wstring StringToWideString(const std::string& text, UINT encoding) inline std::wstring StringToWideString(const std::string& text, UINT encoding)
{ {
std::wstring ret(text.size() + 1, 0); std::wstring ret(text.size() + 1, 0);
ret.resize(MultiByteToWideChar(encoding, 0, text.c_str(), -1, ret.data(), ret.capacity())); ret.resize(MultiByteToWideChar(encoding, 0, text.c_str(), -1, ret.data(), ret.capacity()) - 1);
return ret; return ret;
} }

View File

@ -10,9 +10,9 @@ TextThread::TextThread(ThreadParam tp, DWORD status) : handle(ThreadCounter++),
TextThread::~TextThread() TextThread::~TextThread()
{ {
SetEvent(cancelFlushEvent); SetEvent(deletionEvent);
flusher.join(); flushThread.join();
CloseHandle(cancelFlushEvent); CloseHandle(deletionEvent);
} }
std::wstring TextThread::GetStore() std::wstring TextThread::GetStore()
@ -26,6 +26,7 @@ void TextThread::Flush()
std::wstring sentence; std::wstring sentence;
{ {
LOCK(ttMutex); LOCK(ttMutex);
if (buffer.size() < MaxBufferSize && (GetTickCount() - timestamp < FlushDelay || buffer.size() < 2)) return;
sentence = buffer; sentence = buffer;
buffer.clear(); buffer.clear();
} }
@ -42,18 +43,12 @@ void TextThread::AddSentence(std::wstring sentence)
void TextThread::AddText(const BYTE* data, int len) void TextThread::AddText(const BYTE* data, int len)
{ {
if (buffer.size() > MaxBufferSize) Flush(); std::wstring wData = status & USING_UNICODE
SetEvent(cancelFlushEvent);
flusher.join();
flusher = std::thread([&]
{
ResetEvent(cancelFlushEvent);
if (WaitForSingleObject(cancelFlushEvent, FlushDelay) == WAIT_TIMEOUT) Flush();
});
LOCK(ttMutex);
buffer += status & USING_UNICODE
? std::wstring((wchar_t*)data, len / 2) ? std::wstring((wchar_t*)data, len / 2)
: StringToWideString(std::string((char*)data, len), status & USING_UTF8 ? CP_UTF8 : SHIFT_JIS); : StringToWideString(std::string((char*)data, len), status & USING_UTF8 ? CP_UTF8 : SHIFT_JIS);
LOCK(ttMutex);
buffer.append(wData);
timestamp = GetTickCount();
} }
// EOF // EOF

View File

@ -25,7 +25,7 @@ public:
const ThreadParam tp; const ThreadParam tp;
inline static int FlushDelay = 250; // flush every 250ms by default inline static int FlushDelay = 250; // flush every 250ms by default
inline static int MaxBufferSize = 500; inline static int MaxBufferSize = 200;
inline static int ThreadCounter = 0; inline static int ThreadCounter = 0;
private: private:
@ -34,8 +34,10 @@ private:
std::wstring buffer; std::wstring buffer;
std::wstring storage; std::wstring storage;
std::recursive_mutex ttMutex; std::recursive_mutex ttMutex;
std::thread flusher = std::thread([] {});
HANDLE cancelFlushEvent = CreateEventW(nullptr, TRUE, TRUE, NULL); HANDLE deletionEvent = CreateEventW(nullptr, FALSE, FALSE, NULL);
std::thread flushThread = std::thread([&] { while (WaitForSingleObject(deletionEvent, 10) == WAIT_TIMEOUT) Flush(); });
DWORD timestamp = GetTickCount();
ThreadOutputCallback Output; ThreadOutputCallback Output;
DWORD status; DWORD status;

View File

@ -4,7 +4,7 @@
// 8/23/2013 jichi // 8/23/2013 jichi
// Branch: ITH/common.h, rev 128 // Branch: ITH/common.h, rev 128
enum { MESSAGE_SIZE = 500, PIPE_BUFFER_SIZE = 0x1000, SHIFT_JIS = 932 }; enum { MESSAGE_SIZE = 500, PIPE_BUFFER_SIZE = 0x1000, SHIFT_JIS = 932, MAX_THREAD_COUNT };
// jichi 375/2014: Add offset of pusha/pushad // jichi 375/2014: Add offset of pusha/pushad
// http://faydoc.tripod.com/cpu/pushad.htm // http://faydoc.tripod.com/cpu/pushad.htm