OSDN Git Service

post WM_NULL to attached threads on yamy exit to unmap mayu{32,64}.dll
authorU-i7\gimy <gimy@users.sourceforge.jp>
Tue, 15 Sep 2009 23:31:02 +0000 (08:31 +0900)
committerU-i7\gimy <gimy@users.sourceforge.jp>
Tue, 15 Sep 2009 23:31:02 +0000 (08:31 +0900)
engine.cpp
engine.h
hook.cpp
hook.h
mayu.cpp
yamyd.cpp

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