rearrange textthread logic. dont have threads where not needed.
This commit is contained in:
parent
2947c5a497
commit
effe03fd96
@ -10,10 +10,9 @@ TextThread::TextThread(ThreadParam tp, DWORD status) : handle(ThreadCounter++),
|
||||
|
||||
TextThread::~TextThread()
|
||||
{
|
||||
SetEvent(deletionEvent);
|
||||
flushThread.join();
|
||||
CloseHandle(deletionEvent);
|
||||
LOCK(ttMutex);
|
||||
SetEvent(cancelFlushEvent);
|
||||
flusher.join();
|
||||
CloseHandle(cancelFlushEvent);
|
||||
}
|
||||
|
||||
std::wstring TextThread::GetStore()
|
||||
@ -27,10 +26,7 @@ void TextThread::Flush()
|
||||
std::wstring sentence;
|
||||
{
|
||||
LOCK(ttMutex);
|
||||
if (buffer.size() < MaxBufferSize && (GetTickCount() - timestamp < FlushDelay || buffer.size() < 2)) return;
|
||||
sentence = status & USING_UNICODE
|
||||
? std::wstring((wchar_t*)buffer.data(), buffer.size() / 2)
|
||||
: StringToWideString(std::string(buffer.data(), buffer.size()), status & USING_UTF8 ? CP_UTF8 : SHIFT_JIS);
|
||||
sentence = buffer;
|
||||
buffer.clear();
|
||||
}
|
||||
AddSentence(sentence);
|
||||
@ -46,9 +42,19 @@ void TextThread::AddSentence(std::wstring sentence)
|
||||
|
||||
void TextThread::AddText(const BYTE* data, int len)
|
||||
{
|
||||
if (buffer.size() > MaxBufferSize) Flush();
|
||||
SetEvent(cancelFlushEvent);
|
||||
flusher.join();
|
||||
flusher = std::thread([&]
|
||||
{
|
||||
ResetEvent(cancelFlushEvent);
|
||||
WaitForSingleObject(cancelFlushEvent, FlushDelay);
|
||||
Flush();
|
||||
});
|
||||
LOCK(ttMutex);
|
||||
buffer.insert(buffer.end(), data, data + len);
|
||||
timestamp = GetTickCount();
|
||||
buffer += status & USING_UNICODE
|
||||
? std::wstring((wchar_t*)data, len / 2)
|
||||
: StringToWideString(std::string((char*)data, len), status & USING_UTF8 ? CP_UTF8 : SHIFT_JIS);
|
||||
}
|
||||
|
||||
// EOF
|
||||
|
@ -10,6 +10,7 @@
|
||||
class TextThread
|
||||
{
|
||||
typedef std::function<std::wstring(TextThread*, std::wstring)> ThreadOutputCallback;
|
||||
|
||||
public:
|
||||
TextThread(ThreadParam tp, DWORD status);
|
||||
~TextThread();
|
||||
@ -17,7 +18,6 @@ public:
|
||||
std::wstring GetStore();
|
||||
void AddText(const BYTE* data, int len);
|
||||
void AddSentence(std::wstring sentence);
|
||||
|
||||
void RegisterOutputCallBack(ThreadOutputCallback cb) { Output = cb; }
|
||||
|
||||
const int64_t handle;
|
||||
@ -31,13 +31,11 @@ public:
|
||||
private:
|
||||
void Flush();
|
||||
|
||||
std::vector<char> buffer;
|
||||
std::wstring buffer;
|
||||
std::wstring storage;
|
||||
std::recursive_mutex ttMutex;
|
||||
|
||||
HANDLE deletionEvent = CreateEventW(nullptr, FALSE, FALSE, NULL);
|
||||
std::thread flushThread = std::thread([&] { while (WaitForSingleObject(deletionEvent, 10) == WAIT_TIMEOUT) Flush(); });
|
||||
DWORD timestamp = GetTickCount();
|
||||
std::thread flusher = std::thread([] {});
|
||||
HANDLE cancelFlushEvent = CreateEventW(nullptr, TRUE, TRUE, NULL);
|
||||
|
||||
ThreadOutputCallback Output;
|
||||
DWORD status;
|
||||
|
Loading…
Reference in New Issue
Block a user