From d35fb7a601ec6653e866319c933b8905e9640c8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=81=8D=E5=85=AE=E6=83=9A=E5=85=AE?= <1173718158@qq.com> Date: Sun, 3 Nov 2024 21:19:59 +0800 Subject: [PATCH] sync --- LunaHost/LunaHostDll.cpp | 18 +++++++++++++++--- LunaHost/host.cpp | 7 +++++++ LunaHost/textthread.cpp | 20 +++++++++++++++++--- LunaHost/textthread.h | 2 ++ 4 files changed, 41 insertions(+), 6 deletions(-) diff --git a/LunaHost/LunaHostDll.cpp b/LunaHost/LunaHostDll.cpp index 0dcf135..b565325 100644 --- a/LunaHost/LunaHostDll.cpp +++ b/LunaHost/LunaHostDll.cpp @@ -164,7 +164,19 @@ C_LUNA_API void Luna_embedcallback(DWORD pid, LPCWSTR text, LPCWSTR trans) C_LUNA_API void Luna_SyncThread(ThreadParam tp, bool sync) { - auto sm = Host::GetCommonSharedMem(tp.processId); - if (!sm) - return; + // 必须放到线程里去异步做,不然GetThread死锁 + std::thread([=]() + { + try + { + auto &&t=Host::GetThread(tp); + if (sync) + TextThread::syncThreads->insert(&t); + else + TextThread::syncThreads->erase(&t); + } + catch (...) + { + } }) + .detach(); } \ No newline at end of file diff --git a/LunaHost/host.cpp b/LunaHost/host.cpp index 1b918c9..66ffea3 100644 --- a/LunaHost/host.cpp +++ b/LunaHost/host.cpp @@ -61,6 +61,13 @@ namespace threadsToRemove.push_back(&thread); for (auto thread : threadsToRemove) { + try + { + TextThread::syncThreads->erase(thread); + } + catch (...) + { + } OnDestroy(*thread); textThreadsByParams->erase(thread->tp); } diff --git a/LunaHost/textthread.cpp b/LunaHost/textthread.cpp index 0c334a4..4862bd9 100644 --- a/LunaHost/textthread.cpp +++ b/LunaHost/textthread.cpp @@ -68,7 +68,7 @@ void TextThread::Push(BYTE *data, int length) else Host::AddConsoleOutput(INVALID_CODEPAGE); - lastPushTime = GetTickCount64(); + UpdateFlushTime(); if (filterRepetition) { @@ -89,12 +89,26 @@ void TextThread::Push(BYTE *data, int length) buffer.clear(); } } - +void TextThread::UpdateFlushTime(bool recursive) +{ + lastPushTime = GetTickCount64(); + if (!recursive) + return; + auto&& ths = syncThreads.Acquire().contents; + if (ths.find(this) == ths.end()) + return; + for (auto t : ths) + { + if (t == this) + continue; + t->UpdateFlushTime(false); + } +} void TextThread::Push(const wchar_t *data) { std::scoped_lock lock(bufferMutex); // not sure if this should filter repetition - lastPushTime = GetTickCount64(); + UpdateFlushTime(); buffer += data; } diff --git a/LunaHost/textthread.h b/LunaHost/textthread.h index 9cb829e..a7ba5d6 100644 --- a/LunaHost/textthread.h +++ b/LunaHost/textthread.h @@ -10,6 +10,7 @@ public: inline static int flushDelay = 100; inline static int maxBufferSize = 3000; inline static int maxHistorySize = 10'000'000; + inline static Synchronized> syncThreads; TextThread(ThreadParam tp, HookParam hp, std::optional name = {}); @@ -41,4 +42,5 @@ private: void operator()(HANDLE h) { DeleteTimerQueueTimer(NULL, h, INVALID_HANDLE_VALUE); } }; AutoHandle timer = NULL; + void UpdateFlushTime(bool recursive = true); };