From beb15c0662d6e31237f32983f7e58141054277c5 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Wed, 21 Feb 2018 18:37:57 +0300 Subject: mir_core: C++'11 iterators --- src/mir_core/src/CCtrlPages.cpp | 4 +- src/mir_core/src/CCtrlTreeView.cpp | 14 ++--- src/mir_core/src/CDlgBase.cpp | 10 ++-- src/mir_core/src/langpack.cpp | 7 ++- src/mir_core/src/logger.cpp | 14 +++-- src/mir_core/src/miranda.cpp | 4 +- src/mir_core/src/miranda.h | 2 +- src/mir_core/src/modules.cpp | 13 +++-- src/mir_core/src/threads.cpp | 104 +++++++++++++++++-------------------- src/mir_core/src/timezones.cpp | 25 ++++----- 10 files changed, 90 insertions(+), 107 deletions(-) diff --git a/src/mir_core/src/CCtrlPages.cpp b/src/mir_core/src/CCtrlPages.cpp index f655ce02bc..0857e62552 100644 --- a/src/mir_core/src/CCtrlPages.cpp +++ b/src/mir_core/src/CCtrlPages.cpp @@ -51,8 +51,8 @@ void CCtrlPages::OnInit() CSuper::OnInit(); Subclass(); - for (int i = 0; i < m_pages.getCount(); i++) - InsertPage(m_pages[i]); + for (auto &it : m_pages) + InsertPage(it); m_pages.destroy(); ::SetWindowLongPtr(m_hwnd, GWL_EXSTYLE, ::GetWindowLongPtr(m_hwnd, GWL_EXSTYLE) | WS_EX_CONTROLPARENT); diff --git a/src/mir_core/src/CCtrlTreeView.cpp b/src/mir_core/src/CCtrlTreeView.cpp index 1852517a9d..dca6c1b726 100644 --- a/src/mir_core/src/CCtrlTreeView.cpp +++ b/src/mir_core/src/CCtrlTreeView.cpp @@ -169,14 +169,14 @@ LRESULT CCtrlTreeView::CustomWndProc(UINT msg, WPARAM wParam, LPARAM lParam) GetSelected(arItems); // Proceed moving - for (int i = 0; i < arItems.getCount(); i++) { + for (auto &it : arItems) { if (!insertAfter) break; - if (GetParent(arItems[i]) != hParent) // prevent subitems from being inserted at the same level + if (GetParent(it) != hParent) // prevent subitems from being inserted at the same level continue; - insertAfter = MoveItemAbove(arItems[i], insertAfter, hParent); - if (!i) + insertAfter = MoveItemAbove(it, insertAfter, hParent); + if (it == arItems[0]) FirstItem = insertAfter; } } @@ -511,9 +511,9 @@ void CCtrlTreeView::GetSelected(LIST<_TREEITEM> &selected) void CCtrlTreeView::Select(LIST<_TREEITEM> &selected) { - for (int i = 0; i < selected.getCount(); i++) - if (selected[i] != nullptr) - Select(selected[i]); + for (auto &it : selected) + if (it != nullptr) + Select(it); } ///////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/mir_core/src/CDlgBase.cpp b/src/mir_core/src/CDlgBase.cpp index e6a4d6f08b..79f865d7bb 100644 --- a/src/mir_core/src/CDlgBase.cpp +++ b/src/mir_core/src/CDlgBase.cpp @@ -349,8 +349,8 @@ void CDlgBase::AddControl(CCtrlBase *ctrl) void CDlgBase::NotifyControls(void (CCtrlBase::*fn)()) { - for (int i = 0; i < m_controls.getCount(); i++) - (m_controls[i]->*fn)(); + for (auto &it : m_controls) + (it->*fn)(); } CCtrlBase* CDlgBase::FindControl(int idCtrl) @@ -361,9 +361,9 @@ CCtrlBase* CDlgBase::FindControl(int idCtrl) CCtrlBase* CDlgBase::FindControl(HWND hwnd) { - for (int i = 0; i < m_controls.getCount(); i++) - if (m_controls[i]->GetHwnd() == hwnd) - return m_controls[i]; + for (auto &it : m_controls) + if (it->GetHwnd() == hwnd) + return it; return nullptr; } diff --git a/src/mir_core/src/langpack.cpp b/src/mir_core/src/langpack.cpp index 83885656fa..3fdd9f6d3d 100644 --- a/src/mir_core/src/langpack.cpp +++ b/src/mir_core/src/langpack.cpp @@ -720,13 +720,12 @@ void UnloadLangPackModule() { if (!bModuleInitialized) return; - int i; - for (i = 0; i < lMuuids.getCount(); i++) - mir_free(lMuuids[i]); + for (auto &it : lMuuids) + mir_free(it); lMuuids.destroy(); LangPackEntry *p = g_pEntries; - for (i = 0; i < g_entryCount; i++, p++) { + for (int i = 0; i < g_entryCount; i++, p++) { if (p->pNext != nullptr) { for (LangPackEntry *p1 = p->pNext; p1 != nullptr;) { LangPackEntry *p2 = p1; p1 = p1->pNext; diff --git a/src/mir_core/src/logger.cpp b/src/mir_core/src/logger.cpp index 29dfcc92c0..f316329b87 100644 --- a/src/mir_core/src/logger.cpp +++ b/src/mir_core/src/logger.cpp @@ -79,15 +79,13 @@ void CheckLogs() LARGE_INTEGER li; QueryPerformanceCounter(&li); - for (int i=0; i < arLoggers.getCount(); i++) { - Logger &p = arLoggers[i]; - - mir_cslock lck(p.m_cs); - if (p.m_out && li.QuadPart - p.m_lastwrite > llIdlePeriod) { - fclose(p.m_out); - p.m_out = nullptr; + for (auto &p : arLoggers) { + mir_cslock lck(p->m_cs); + if (p->m_out && li.QuadPart - p->m_lastwrite > llIdlePeriod) { + fclose(p->m_out); + p->m_out = nullptr; } - else fflush(p.m_out); + else fflush(p->m_out); } } diff --git a/src/mir_core/src/miranda.cpp b/src/mir_core/src/miranda.cpp index 12d1b5513a..fda0f29a80 100644 --- a/src/mir_core/src/miranda.cpp +++ b/src/mir_core/src/miranda.cpp @@ -44,7 +44,7 @@ int hLangpack = 0; HINSTANCE g_hInst = nullptr; HCURSOR g_hCursorNS, g_hCursorWE; -HANDLE hStackMutex, hThreadQueueEmpty; +HANDLE hThreadQueueEmpty; DWORD mir_tls = 0; ///////////////////////////////////////////////////////////////////////////////////////// @@ -106,7 +106,6 @@ static void LoadCoreModule(void) hAPCWindow = CreateWindowEx(0, L"STATIC", nullptr, 0, 0, 0, 0, 0, nullptr, nullptr, nullptr, nullptr); SetWindowLongPtr(hAPCWindow, GWLP_WNDPROC, (LONG_PTR)APCWndProc); SetTimer(hAPCWindow, 1, 1000, nullptr); - hStackMutex = CreateMutex(nullptr, FALSE, nullptr); hThreadQueueEmpty = CreateEvent(nullptr, TRUE, TRUE, nullptr); InitWinver(); @@ -126,7 +125,6 @@ static void LoadCoreModule(void) MIR_CORE_DLL(void) UnloadCoreModule(void) { DestroyWindow(hAPCWindow); - CloseHandle(hStackMutex); CloseHandle(hThreadQueueEmpty); TlsFree(mir_tls); diff --git a/src/mir_core/src/miranda.h b/src/mir_core/src/miranda.h index af85d79892..9337590baf 100644 --- a/src/mir_core/src/miranda.h +++ b/src/mir_core/src/miranda.h @@ -39,7 +39,7 @@ HINSTANCE ProtoGetInstance(const char *szModuleName); extern HINSTANCE g_hInst; extern HWND hAPCWindow; -extern HANDLE hStackMutex, hThreadQueueEmpty; +extern HANDLE hThreadQueueEmpty; extern HCURSOR g_hCursorNS, g_hCursorWE; extern MIDatabase *currDb; diff --git a/src/mir_core/src/modules.cpp b/src/mir_core/src/modules.cpp index 925ab69cc2..c410084c65 100644 --- a/src/mir_core/src/modules.cpp +++ b/src/mir_core/src/modules.cpp @@ -385,9 +385,9 @@ MIR_CORE_DLL(int) UnhookEvent(HANDLE hHook) mir_cslock lck(csHooks); THook *p = nullptr; - for (int i = 0; i < hooks.getCount(); i++) - if (hooks[i]->id == hookId) { - p = hooks[i]; + for (auto &it : hooks) + if (it->id == hookId) { + p = it; break; } @@ -452,8 +452,7 @@ static void DestroyHooks() { mir_cslock lck(csHooks); - for (int i = 0; i < hooks.getCount(); i++) { - THook *p = hooks[i]; + for (auto &p : hooks) { if (p->subscriberCount) mir_free(p->subscriber); DeleteCriticalSection(&p->csHook); @@ -662,8 +661,8 @@ static void DestroyServices() { mir_cslock lck(csServices); - for (int j = 0; j < services.getCount(); j++) - mir_free(services[j]); + for (auto &it : services) + mir_free(it); } /////////////////////////////////////////////////////////////////////////////// diff --git a/src/mir_core/src/threads.cpp b/src/mir_core/src/threads.cpp index 4971794418..9d293c2e5e 100644 --- a/src/mir_core/src/threads.cpp +++ b/src/mir_core/src/threads.cpp @@ -27,6 +27,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include +static mir_cs csThreads; + ///////////////////////////////////////////////////////////////////////////////////////// // APC and mutex functions @@ -211,23 +213,23 @@ MIR_CORE_DLL(void) KillObjectThreads(void* owner) if (owner == nullptr) return; - WaitForSingleObject(hStackMutex, INFINITE); - HANDLE *threadPool = (HANDLE*)alloca(threads.getCount()*sizeof(HANDLE)); int threadCount = 0; + { + mir_cslock lck(csThreads); - for (int j = threads.getCount(); j--;) { - THREAD_WAIT_ENTRY *p = threads[j]; - if (p->pObject == owner) - threadPool[threadCount++] = p->hThread; + for (int j = threads.getCount(); j--;) { + THREAD_WAIT_ENTRY *p = threads[j]; + if (p->pObject == owner) + threadPool[threadCount++] = p->hThread; + } } - ReleaseMutex(hStackMutex); // is there anything to kill? if (threadCount > 0) { if (WaitForMultipleObjects(threadCount, threadPool, TRUE, 5000) == WAIT_TIMEOUT) { // forcibly kill all remaining threads after 5 secs - WaitForSingleObject(hStackMutex, INFINITE); + mir_cslock lck(csThreads); for (int j = threads.getCount() - 1; j >= 0; j--) { THREAD_WAIT_ENTRY *p = threads[j]; if (p->pObject == owner) { @@ -240,7 +242,6 @@ MIR_CORE_DLL(void) KillObjectThreads(void* owner) mir_free(p); } } - ReleaseMutex(hStackMutex); } } } @@ -249,9 +250,9 @@ MIR_CORE_DLL(void) KillObjectThreads(void* owner) static void CALLBACK KillAllThreads(HWND, UINT, UINT_PTR, DWORD) { - if (MirandaWaitForMutex(hStackMutex)) { - for (int j = 0; j < threads.getCount(); j++) { - THREAD_WAIT_ENTRY *p = threads[j]; + { + mir_cslock lck(csThreads); + for (auto &p : threads) { char szModuleName[MAX_PATH]; GetModuleFileNameA(p->hOwner, szModuleName, sizeof(szModuleName)); Netlib_Logf(nullptr, "Killing thread %s:%p (%p)", szModuleName, p->dwThreadId, p->pEntryPoint); @@ -261,19 +262,18 @@ static void CALLBACK KillAllThreads(HWND, UINT, UINT_PTR, DWORD) } threads.destroy(); - - ReleaseMutex(hStackMutex); - SetEvent(hThreadQueueEmpty); } + + SetEvent(hThreadQueueEmpty); } MIR_CORE_DLL(void) Thread_Wait(void) { // acquire the list and wake up any alertable threads - if (MirandaWaitForMutex(hStackMutex)) { - for (int j = 0; j < threads.getCount(); j++) - QueueUserAPC(DummyAPCFunc, threads[j]->hThread, 0); - ReleaseMutex(hStackMutex); + { + mir_cslock lck(csThreads); + for (auto &p : threads) + QueueUserAPC(DummyAPCFunc, p->hThread, 0); } // give all unclosed threads 5 seconds to close @@ -310,22 +310,20 @@ static void* GetCurrentThreadEntryPoint() MIR_CORE_DLL(INT_PTR) Thread_Push(HINSTANCE hInst, void* pOwner) { ResetEvent(hThreadQueueEmpty); // thread list is not empty - if (WaitForSingleObject(hStackMutex, INFINITE) == WAIT_OBJECT_0) { - THREAD_WAIT_ENTRY *p = (THREAD_WAIT_ENTRY*)mir_calloc(sizeof(THREAD_WAIT_ENTRY)); - - DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), GetCurrentProcess(), &p->hThread, 0, FALSE, DUPLICATE_SAME_ACCESS); - p->dwThreadId = GetCurrentThreadId(); - p->pObject = pOwner; - if (pluginListAddr.getIndex(hInst) != -1) - p->hOwner = hInst; - else - p->hOwner = GetInstByAddress((hInst != nullptr) ? (PVOID)hInst : GetCurrentThreadEntryPoint()); - p->pEntryPoint = hInst; - - threads.insert(p); + + mir_cslock lck(csThreads); + + THREAD_WAIT_ENTRY *p = (THREAD_WAIT_ENTRY*)mir_calloc(sizeof(THREAD_WAIT_ENTRY)); + DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), GetCurrentProcess(), &p->hThread, 0, FALSE, DUPLICATE_SAME_ACCESS); + p->dwThreadId = GetCurrentThreadId(); + p->pObject = pOwner; + if (pluginListAddr.getIndex(hInst) != -1) + p->hOwner = hInst; + else + p->hOwner = GetInstByAddress((hInst != nullptr) ? (PVOID)hInst : GetCurrentThreadEntryPoint()); + p->pEntryPoint = hInst; - ReleaseMutex(hStackMutex); - } + threads.insert(p); return 0; } @@ -333,30 +331,24 @@ MIR_CORE_DLL(INT_PTR) Thread_Push(HINSTANCE hInst, void* pOwner) MIR_CORE_DLL(INT_PTR) Thread_Pop() { - if (WaitForSingleObject(hStackMutex, INFINITE) == WAIT_OBJECT_0) { - DWORD dwThreadId = GetCurrentThreadId(); - for (int j = 0; j < threads.getCount(); j++) { - THREAD_WAIT_ENTRY *p = threads[j]; - if (p->dwThreadId == dwThreadId) { - SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL); - CloseHandle(p->hThread); - threads.remove(j); - mir_free(p); - - if (!threads.getCount()) { - threads.destroy(); - ReleaseMutex(hStackMutex); - SetEvent(hThreadQueueEmpty); // thread list is empty now - return 0; - } + DWORD dwThreadId = GetCurrentThreadId(); - ReleaseMutex(hStackMutex); - return 0; - } - } - ReleaseMutex(hStackMutex); + mir_cslock lck(csThreads); + THREAD_WAIT_ENTRY *p = threads.find((THREAD_WAIT_ENTRY*)&dwThreadId); + if (p == nullptr) + return 1; + + SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL); + CloseHandle(p->hThread); + threads.remove(p); + mir_free(p); + + if (!threads.getCount()) { + threads.destroy(); + SetEvent(hThreadQueueEmpty); // thread list is empty now } - return 1; + + return 0; } ///////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/mir_core/src/timezones.cpp b/src/mir_core/src/timezones.cpp index 675bfb1b19..a4e74b1eb0 100644 --- a/src/mir_core/src/timezones.cpp +++ b/src/mir_core/src/timezones.cpp @@ -165,7 +165,7 @@ MIR_CORE_DLL(LPCTSTR) TimeZone_GetName(HANDLE hTZ) MIM_TIMEZONE *tz = (MIM_TIMEZONE*)hTZ; if (tz == nullptr) return myInfo.myTZ.tszName; - else if (tz == UTC_TIME_HANDLE) + if (tz == UTC_TIME_HANDLE) return L"UTC"; return tz->tszName; @@ -173,12 +173,10 @@ MIR_CORE_DLL(LPCTSTR) TimeZone_GetName(HANDLE hTZ) MIR_CORE_DLL(LPCTSTR) TimeZone_GetDescription(LPCTSTR TZname) { - for (int i = 0; i < g_timezonesBias.getCount(); i++) { - MIM_TIMEZONE *tz = g_timezonesBias[i]; - + for (auto &tz : g_timezonesBias) if (!mir_wstrcmp(tz->tszName, TZname)) return tz->szDisplay; - } + return L""; } @@ -386,12 +384,12 @@ static const ListMessages* GetListMessages(HWND hWnd, DWORD dwFlags) else if (!mir_wstrcmpi(tszClassName, L"LISTBOX")) dwFlags |= TZF_PLF_LB; } + if (dwFlags & TZF_PLF_CB) return &cbMessages; - else if (dwFlags & TZF_PLF_LB) + if (dwFlags & TZF_PLF_LB) return &lbMessages; - else - return nullptr; + return nullptr; } /////////////////////////////////////////////////////////////////////////////// @@ -510,14 +508,13 @@ void RecalculateTime(void) found = true; } - for (int i = 0; i < g_timezones.getCount(); i++) { - MIM_TIMEZONE &tz = g_timezones[i]; - if (tz.offset != INT_MIN) - tz.offset = INT_MIN; + for (auto &tz : g_timezones) { + if (tz->offset != INT_MIN) + tz->offset = INT_MIN; if (!found) { - if (!mir_wstrcmp(tz.tzi.StandardName, myInfo.myTZ.tzi.StandardName) || !mir_wstrcmp(tz.tzi.DaylightName, myInfo.myTZ.tzi.DaylightName)) { - wcsncpy_s(myInfo.myTZ.tszName, tz.tszName, _TRUNCATE); + if (!mir_wstrcmp(tz->tzi.StandardName, myInfo.myTZ.tzi.StandardName) || !mir_wstrcmp(tz->tzi.DaylightName, myInfo.myTZ.tzi.DaylightName)) { + wcsncpy_s(myInfo.myTZ.tszName, tz->tszName, _TRUNCATE); found = true; } } -- cgit v1.2.3