This commit is contained in:
恍兮惚兮 2024-11-03 21:19:59 +08:00
parent 8ccecb045a
commit d35fb7a601
4 changed files with 41 additions and 6 deletions

View File

@ -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();
}

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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<std::set<TextThread *>> syncThreads;
TextThread(ThreadParam tp, HookParam hp, std::optional<std::wstring> name = {});
@ -41,4 +42,5 @@ private:
void operator()(HANDLE h) { DeleteTimerQueueTimer(NULL, h, INVALID_HANDLE_VALUE); }
};
AutoHandle<TimerDeleter> timer = NULL;
void UpdateFlushTime(bool recursive = true);
};