From bacf1d7174ab8d977805698c3879543954a311e2 Mon Sep 17 00:00:00 2001 From: "U-i7\\gimy" Date: Wed, 16 Sep 2009 08:31:02 +0900 Subject: [PATCH] post WM_NULL to attached threads on yamy exit to unmap mayu{32,64}.dll --- engine.cpp | 19 +++++++++++++++++-- engine.h | 8 ++++++-- hook.cpp | 9 ++++++++- hook.h | 7 +++++++ mayu.cpp | 39 +++++++++++++++++++++++---------------- yamyd.cpp | 3 +-- 6 files changed, 62 insertions(+), 23 deletions(-) diff --git a/engine.cpp b/engine.cpp index f5dbd2c..fab96a8 100644 --- a/engine.cpp +++ b/engine.cpp @@ -38,7 +38,7 @@ restart: // erase dead thread if (!m_detachedThreadIds.empty()) { - for (DetachedThreadIds::iterator i = m_detachedThreadIds.begin(); + for (ThreadIds::iterator i = m_detachedThreadIds.begin(); i != m_detachedThreadIds.end(); i ++) { FocusOfThreads::iterator j = m_focusOfThreads.find((*i)); if (j != m_focusOfThreads.end()) { @@ -1282,6 +1282,11 @@ void Engine::stop() { CHECK_TRUE( CloseHandle(m_readEvent) ); m_readEvent = NULL; + + for (ThreadIds::iterator i = m_attachedThreadIds.begin(); + i != m_attachedThreadIds.end(); i++) { + PostThreadMessage(*i, WM_NULL, 0, 0); + } } @@ -1478,7 +1483,7 @@ bool Engine::setFocus(HWND i_hwndFocus, DWORD i_threadId, // remove newly created thread's id from m_detachedThreadIds if (!m_detachedThreadIds.empty()) { - DetachedThreadIds::iterator i; + ThreadIds::iterator i; bool retry; do { retry = false; @@ -1580,10 +1585,20 @@ bool Engine::syncNotify() { } +// thread attach notify +bool Engine::threadAttachNotify(DWORD i_threadId) { + Acquire a(&m_cs); + m_attachedThreadIds.push_back(i_threadId); + return true; +} + + // thread detach notify bool Engine::threadDetachNotify(DWORD i_threadId) { Acquire a(&m_cs); m_detachedThreadIds.push_back(i_threadId); + m_attachedThreadIds.erase(remove(m_attachedThreadIds.begin(), m_attachedThreadIds.end(), i_threadId), + m_attachedThreadIds.end()); return true; } diff --git a/engine.h b/engine.h index 35b3a65..a1967e7 100644 --- a/engine.h +++ b/engine.h @@ -59,7 +59,7 @@ private: }; typedef std::map FocusOfThreads; /// - typedef std::list DetachedThreadIds; /// + typedef std::list ThreadIds; /// /// current status in generateKeyboardEvents class Current @@ -235,7 +235,8 @@ private: FocusOfThread * volatile m_currentFocusOfThread; /// FocusOfThread m_globalFocus; /// HWND m_hwndFocus; /// current focus window - DetachedThreadIds m_detachedThreadIds; /// + ThreadIds m_attachedThreadIds; /// + ThreadIds m_detachedThreadIds; /// // for functions KeymapPtrList m_keymapPrefixHistory; /// for &KeymapPrevPrefix @@ -594,6 +595,9 @@ public: /// sync bool syncNotify(); + /// thread attach notify + bool threadAttachNotify(DWORD i_threadId); + /// thread detach notify bool threadDetachNotify(DWORD i_threadId); diff --git a/hook.cpp b/hook.cpp index 74b8b73..98b585d 100644 --- a/hook.cpp +++ b/hook.cpp @@ -100,6 +100,7 @@ static void notifyLog(_TCHAR *i_msg); static bool mapHookData(bool i_isYamy); static void unmapHookData(); static bool initialize(bool i_isYamy); +static bool notify(void *i_data, size_t i_dataSize); //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -160,6 +161,12 @@ bool initialize(bool i_isYamy) g.m_WM_MAYU_MESSAGE = RegisterWindowMessage(addSessionId(WM_MAYU_MESSAGE_NAME).c_str()); g.m_hwndTaskTray = g_hookData->m_hwndTaskTray; + if (!i_isYamy) { + NotifyThreadAttach ntd; + ntd.m_type = Notify::Type_threadAttach; + ntd.m_threadId = GetCurrentThreadId(); + notify(&ntd, sizeof(ntd)); + } g.m_isInitialized = true; return true; } @@ -252,7 +259,7 @@ static void unmapHookData() /// notify -DllExport bool notify(void *i_data, size_t i_dataSize) +bool notify(void *i_data, size_t i_dataSize) { COPYDATASTRUCT cd; #ifdef MAYU64 diff --git a/hook.h b/hook.h index 22f8cd8..a7e3294 100644 --- a/hook.h +++ b/hook.h @@ -35,6 +35,7 @@ struct Notify { Type_name, /// NotifySetFocus Type_lockState, /// NotifyLockState Type_sync, /// Notify + Type_threadAttach, /// NotifyThreadAttach Type_threadDetach, /// NotifyThreadDetach Type_command64, /// NotifyCommand64 Type_command32, /// NotifyCommand32 @@ -67,6 +68,12 @@ struct NotifyLockState : public Notify { /// +struct NotifyThreadAttach : public Notify { + DWORD m_threadId; /// +}; + + +/// struct NotifyThreadDetach : public Notify { DWORD m_threadId; /// }; diff --git a/mayu.cpp b/mayu.cpp index 7654653..91653b8 100644 --- a/mayu.cpp +++ b/mayu.cpp @@ -224,6 +224,12 @@ private: break; } + case Notify::Type_threadAttach: { + NotifyThreadAttach *n = (NotifyThreadAttach *)cd->lpData; + m_engine.threadAttachNotify(n->m_threadId); + break; + } + case Notify::Type_threadDetach: { NotifyThreadDetach *n = (NotifyThreadDetach *)cd->lpData; m_engine.threadDetachNotify(n->m_threadId); @@ -1177,18 +1183,6 @@ public: /// ~Mayu() { - CancelIo(m_hNotifyMailslot); - SleepEx(0, TRUE); - CloseHandle(m_hNotifyMailslot); - CloseHandle(m_hNotifyEvent); - ReleaseMutex(m_mutex); - WaitForSingleObject(m_mutex, INFINITE); - // first, detach log from edit control to avoid deadlock - m_log.detach(); -#ifdef LOG_TO_FILE - m_logFile.close(); -#endif // LOG_TO_FILE - // stop notify from mayu.dll g_hookData->m_hwndTaskTray = NULL; CHECK_FALSE( uninstallMessageHook() ); @@ -1201,10 +1195,18 @@ public: } CloseHandle(m_hMutexYamyd); #endif // _WIN64 - if (!(m_sessionState & SESSION_END_QUERIED)) { - DWORD_PTR result; - SendMessageTimeout(HWND_BROADCAST, WM_NULL, 0, 0, SMTO_ABORTIFHUNG, 3000, &result); - } + + CancelIo(m_hNotifyMailslot); + SleepEx(0, TRUE); + CloseHandle(m_hNotifyMailslot); + CloseHandle(m_hNotifyEvent); + ReleaseMutex(m_mutex); + WaitForSingleObject(m_mutex, INFINITE); + // first, detach log from edit control to avoid deadlock + m_log.detach(); +#ifdef LOG_TO_FILE + m_logFile.close(); +#endif // LOG_TO_FILE // destroy windows CHECK_TRUE( DestroyWindow(m_hwndVersion) ); @@ -1223,6 +1225,11 @@ public: // stop keyboard handler thread m_engine.stop(); + if (!(m_sessionState & SESSION_END_QUERIED)) { + DWORD_PTR result; + SendMessageTimeout(HWND_BROADCAST, WM_NULL, 0, 0, 0, 3000, &result); + } + // remove setting; delete m_setting; } diff --git a/yamyd.cpp b/yamyd.cpp index 88a018f..642fd61 100644 --- a/yamyd.cpp +++ b/yamyd.cpp @@ -14,9 +14,8 @@ int WINAPI _tWinMain(HINSTANCE /* i_hInstance */, HINSTANCE /* i_hPrevInstance * // wait for master process exit WaitForSingleObject(mutex, INFINITE); - ReleaseMutex(mutex); - CHECK_FALSE( uninstallMessageHook() ); + ReleaseMutex(mutex); } return 0; -- 2.11.0