diff options
-rw-r--r-- | include/m_system.h | 20 | ||||
-rw-r--r-- | plugins/Console/src/Console.cpp | 15 | ||||
-rw-r--r-- | plugins/KeyboardNotify/src/main.cpp | 8 | ||||
-rw-r--r-- | plugins/NotifyAnything/src/main.cpp | 12 | ||||
-rw-r--r-- | plugins/Ping/src/pingthread.cpp | 33 | ||||
-rw-r--r-- | plugins/Popup/src/popup_thread.cpp | 9 | ||||
-rw-r--r-- | protocols/EmLanProto/src/lan.cpp | 4 | ||||
-rw-r--r-- | protocols/JabberG/src/jabber_console.cpp | 3 |
8 files changed, 65 insertions, 39 deletions
diff --git a/include/m_system.h b/include/m_system.h index 73b0544554..18000d6778 100644 --- a/include/m_system.h +++ b/include/m_system.h @@ -495,6 +495,26 @@ public: };
///////////////////////////////////////////////////////////////////////////////
+// thread handle controller
+
+class MThreadHandle
+{
+ HANDLE &m_pHandle;
+
+public:
+ MThreadHandle(HANDLE &pHandle) :
+ m_pHandle(pHandle)
+ {
+ pHandle = ::GetCurrentThread();
+ }
+
+ ~MThreadHandle()
+ {
+ m_pHandle = nullptr;
+ }
+};
+
+///////////////////////////////////////////////////////////////////////////////
// parameter classes for XML, JSON & HTTP requests
struct PARAM
diff --git a/plugins/Console/src/Console.cpp b/plugins/Console/src/Console.cpp index cb19571c36..6bf70f4454 100644 --- a/plugins/Console/src/Console.cpp +++ b/plugins/Console/src/Console.cpp @@ -894,6 +894,7 @@ static INT_PTR CALLBACK ConsoleDlgProc(HWND hwndDlg, UINT message, WPARAM wParam void __cdecl ConsoleThread(void*)
{
+ MThreadHandle threadLock(hConsoleThread);
CoInitialize(nullptr);
HWND hwnd = CreateDialog(g_plugin.getInst(), MAKEINTRESOURCE(IDD_CONSOLE), nullptr, ConsoleDlgProc);
@@ -1211,7 +1212,7 @@ void InitConsole() LoadSettings();
- hConsoleThread = mir_forkthread(ConsoleThread);
+ mir_forkthread(ConsoleThread);
HookEvent(ME_SYSTEM_PRESHUTDOWN, PreshutdownConsole);
HookEvent(ME_SYSTEM_MODULESLOADED, OnSystemModulesLoaded);
@@ -1225,15 +1226,17 @@ void ShutdownConsole(void) List_Destroy(&lModules);
- if (gImg) ImageList_Destroy(gImg);
+ if (gImg)
+ ImageList_Destroy(gImg);
- for (i = 0; i < _countof(hIcons); i++) {
- if (hIcons[i]) DestroyIcon(hIcons[i]);
- }
+ for (i = 0; i < _countof(hIcons); i++)
+ if (hIcons[i])
+ DestroyIcon(hIcons[i]);
if (hwndConsole)
EndDialog(hwndConsole, TRUE);
- WaitForSingleObject(hConsoleThread, INFINITE);
+ if (hConsoleThread)
+ WaitForSingleObject(hConsoleThread, INFINITE);
}
////////////////////////////////////////////////////////////////////////////////
diff --git a/plugins/KeyboardNotify/src/main.cpp b/plugins/KeyboardNotify/src/main.cpp index 6bab75efb5..321aa68a9e 100644 --- a/plugins/KeyboardNotify/src/main.cpp +++ b/plugins/KeyboardNotify/src/main.cpp @@ -266,8 +266,9 @@ static void __cdecl FlashThreadFunction(void*) BYTE data, unchangedLeds;
Thread_SetName("KeyboardNotify: FlashThreadFunction");
+ MThreadHandle threadLock(hThread);
- while (TRUE) {
+ while (true) {
unchangedLeds = (BYTE)(LedState(VK_PAUSE) * !bFlashLed[2] + ((LedState(VK_NUMLOCK) * !bFlashLed[0]) << 1) + ((LedState(VK_CAPITAL) * !bFlashLed[1]) << 2));
GetAsyncKeyState(VK_PAUSE); // empty Pause/Break's keystroke buffer
@@ -902,7 +903,7 @@ static int ModulesLoaded(WPARAM, LPARAM) mir_snwprintf(eventName, L"%s/ExitEvent", eventPrefix);
hExitEvent = CreateEvent(nullptr, FALSE, FALSE, eventName);
- hThread = mir_forkthread(FlashThreadFunction);
+ mir_forkthread(FlashThreadFunction);
CreateServiceFunction(MS_KBDNOTIFY_ENABLE, EnableService);
CreateServiceFunction(MS_KBDNOTIFY_DISABLE, DisableService);
@@ -934,7 +935,8 @@ int CMPlugin::Unload() UnhookWindowsHooks();
// Wait for thread to exit
- WaitForSingleObject(hThread, INFINITE);
+ if (hThread)
+ WaitForSingleObject(hThread, INFINITE);
if (hExitEvent)
CloseHandle(hExitEvent);
diff --git a/plugins/NotifyAnything/src/main.cpp b/plugins/NotifyAnything/src/main.cpp index c47d8fb24d..6c0c7fb9ae 100644 --- a/plugins/NotifyAnything/src/main.cpp +++ b/plugins/NotifyAnything/src/main.cpp @@ -837,6 +837,8 @@ void initWinsock() void __cdecl udptcpThreadFunc(void *useUdp)
{
+ MThreadHandle threadLock(useUdp ? g_udp_thread : g_tcp_thread);
+
try
{
initWinsock();
@@ -946,8 +948,8 @@ void __cdecl udptcpThreadFunc(void *useUdp) void start_threads()
{
g_exit_threads = false;
- g_udp_thread = mir_forkthread(udptcpThreadFunc, (void *)1);
- g_tcp_thread = mir_forkthread(udptcpThreadFunc);
+ mir_forkthread(udptcpThreadFunc, (void *)1);
+ mir_forkthread(udptcpThreadFunc);
}
void stop_threads()
@@ -957,8 +959,10 @@ void stop_threads() shutdown(g_tcp_socket, 2);
closesocket(g_udp_socket);
closesocket(g_tcp_socket);
- WaitForSingleObject(g_udp_thread, INFINITE);
- WaitForSingleObject(g_tcp_thread, INFINITE);
+ if (g_udp_thread)
+ WaitForSingleObject(g_udp_thread, INFINITE);
+ if (g_tcp_thread)
+ WaitForSingleObject(g_tcp_thread, INFINITE);
}
/////////////////////////////////////////////////////////////////////////////////////////
diff --git a/plugins/Ping/src/pingthread.cpp b/plugins/Ping/src/pingthread.cpp index 0dad666e44..03847a4f3d 100644 --- a/plugins/Ping/src/pingthread.cpp +++ b/plugins/Ping/src/pingthread.cpp @@ -74,6 +74,8 @@ void SetProtoStatus(wchar_t *pszLabel, char *pszProto, int if_status, int new_st void __cdecl sttCheckStatusThreadProc(void*)
{
+ MThreadHandle threadLock(status_update_thread);
+
clock_t start_t = clock(), end_t;
while (!get_thread_finished()) {
end_t = clock();
@@ -222,24 +224,6 @@ void __cdecl sttCheckStatusThreadProc(void*) }
}
-void start_ping_thread()
-{
- if (status_update_thread)
- CloseHandle(status_update_thread);
- status_update_thread = mir_forkthread(sttCheckStatusThreadProc);
-}
-
-void stop_ping_thread()
-{
- set_thread_finished(true);
- SetEvent(hWakeEvent);
- //ICMP::get_instance()->stop();
- WaitForSingleObject(status_update_thread, 2000);
- TerminateThread(status_update_thread, 0);
- CloseHandle(status_update_thread);
- status_update_thread = nullptr;
-}
-
bool FrameIsFloating()
{
if (frame_id == -1)
@@ -921,12 +905,19 @@ void InitList() ReloadFont(0, 0);
- start_ping_thread();
+ mir_forkthread(sttCheckStatusThreadProc);
}
void DeinitList()
{
DestroyWindow(hpwnd);
- stop_ping_thread();
- if (hFont) DeleteObject(hFont);
+
+ SetEvent(hWakeEvent);
+ if (status_update_thread) {
+ WaitForSingleObject(status_update_thread, INFINITE);
+ status_update_thread = nullptr;
+ }
+
+ if (hFont)
+ DeleteObject(hFont);
}
diff --git a/plugins/Popup/src/popup_thread.cpp b/plugins/Popup/src/popup_thread.cpp index d6fa27d649..76113d68ec 100644 --- a/plugins/Popup/src/popup_thread.cpp +++ b/plugins/Popup/src/popup_thread.cpp @@ -210,6 +210,7 @@ static LRESULT CALLBACK PopupThreadManagerWndProc(HWND hwnd, UINT message, WPARA static unsigned __stdcall PopupThread(void *)
{
Thread_SetName("Popup: PopupThread");
+ MThreadHandle threadLock(hThread);
// Create manager window
WNDCLASSEX wcl;
@@ -265,9 +266,11 @@ void StopPopupThread() void UnloadPopupThread()
{
- // We won't waint for thread to exit, Miranda's thread unsind mechanism will do that for us.
- WaitForSingleObject(hThread, INFINITE);
- CloseHandle(hThread);
+ // We won't waint for thread to exit, Miranda's thread unwind mechanism will do that for us.
+ if (hThread) {
+ WaitForSingleObject(hThread, INFINITE);
+ CloseHandle(hThread);
+ }
for (auto &it : popupList)
delete it;
diff --git a/protocols/EmLanProto/src/lan.cpp b/protocols/EmLanProto/src/lan.cpp index 157470f2b0..deff7edd18 100644 --- a/protocols/EmLanProto/src/lan.cpp +++ b/protocols/EmLanProto/src/lan.cpp @@ -146,7 +146,7 @@ void CLan::StartListen() return;
}
- m_hListenThread = mir_forkthread(ListenProc, this);
+ mir_forkthread(ListenProc, this);
m_hAcceptTCPThread = mir_forkthread(AcceptTCPProc, this);
if (m_hListenThread == nullptr || m_hAcceptTCPThread == nullptr) {
m_mode = LM_ON;
@@ -176,6 +176,8 @@ void __cdecl CLan::ListenProc(void *lpParameter) void CLan::Listen()
{
+ MThreadHandle threadLock(m_hListenThread);
+
if (m_mode == LM_LISTEN) {
char buf[65536];
while (true) {
diff --git a/protocols/JabberG/src/jabber_console.cpp b/protocols/JabberG/src/jabber_console.cpp index 4e7a1e05bb..7a03299590 100644 --- a/protocols/JabberG/src/jabber_console.cpp +++ b/protocols/JabberG/src/jabber_console.cpp @@ -571,6 +571,7 @@ public: void __cdecl CJabberProto::ConsoleThread(void*)
{
Thread_SetName("Jabber: ConsoleThread");
+ MThreadHandle threadLock(m_hThreadConsole);
MSG msg;
while (GetMessage(&msg, nullptr, 0, 0)) {
@@ -588,7 +589,7 @@ void __cdecl CJabberProto::ConsoleThread(void*) void CJabberProto::ConsoleInit()
{
- m_hThreadConsole = ForkThreadEx(&CJabberProto::ConsoleThread, nullptr, &m_dwConsoleThreadId);
+ ForkThreadEx(&CJabberProto::ConsoleThread, nullptr, &m_dwConsoleThreadId);
}
void CJabberProto::ConsoleUninit()
|