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