From aa1c3ae7a47002df6e5be2505da90c582e0558de Mon Sep 17 00:00:00 2001 From: Povilas Kanapickas Date: Wed, 3 Nov 2021 02:58:39 +0200 Subject: [PATCH] lib/arch/win32: Use standard mutex primitives in ArchTaskBarWindows --- src/lib/arch/win32/ArchTaskBarWindows.cpp | 49 +++++++---------------- src/lib/arch/win32/ArchTaskBarWindows.h | 6 ++- 2 files changed, 19 insertions(+), 36 deletions(-) diff --git a/src/lib/arch/win32/ArchTaskBarWindows.cpp b/src/lib/arch/win32/ArchTaskBarWindows.cpp index a6814d14..34df4c96 100644 --- a/src/lib/arch/win32/ArchTaskBarWindows.cpp +++ b/src/lib/arch/win32/ArchTaskBarWindows.cpp @@ -39,8 +39,6 @@ static const UINT kFirstReceiverID = WM_USER + 14; ArchTaskBarWindows* ArchTaskBarWindows::s_instance = NULL; ArchTaskBarWindows::ArchTaskBarWindows() : - m_mutex(NULL), - m_condVar(NULL), m_ready(false), m_result(0), m_thread(NULL), @@ -59,29 +57,19 @@ ArchTaskBarWindows::~ArchTaskBarWindows() ARCH->wait(m_thread, -1.0); ARCH->closeThread(m_thread); } - if (m_condVar != NULL) { - ARCH->closeCondVar(m_condVar); - } - if (m_mutex != NULL) { - ARCH->closeMutex(m_mutex); - } s_instance = NULL; } void ArchTaskBarWindows::init() { - // we need a mutex - m_mutex = ARCH->newMutex(); - // and a condition variable which uses the above mutex m_ready = false; - m_condVar = ARCH->newCondVar(); // we're going to want to get a result from the thread we're // about to create to know if it initialized successfully. // so we lock the condition variable. - ArchMutexLock lock(m_mutex); + std::unique_lock lock(mutex_); // open a window and run an event loop in a separate thread. // this has to happen in a separate thread because if we @@ -92,7 +80,7 @@ ArchTaskBarWindows::init() // wait for child thread while (!m_ready) { - ARCH->waitCondVar(m_condVar, lock, -1.0); + ARCH->wait_cond_var(cond_var_, lock, -1.0); } } @@ -186,53 +174,48 @@ ArchTaskBarWindows::recycleID(UINT id) void ArchTaskBarWindows::addIcon(UINT id) { - ARCH->lockMutex(m_mutex); + std::lock_guard lock(mutex_); CIDToReceiverMap::const_iterator index = m_idTable.find(id); if (index != m_idTable.end()) { modifyIconNoLock(index->second, NIM_ADD); } - ARCH->unlockMutex(m_mutex); } void ArchTaskBarWindows::removeIcon(UINT id) { - ARCH->lockMutex(m_mutex); + std::lock_guard lock(mutex_); removeIconNoLock(id); - ARCH->unlockMutex(m_mutex); } void ArchTaskBarWindows::updateIcon(UINT id) { - ARCH->lockMutex(m_mutex); + std::lock_guard lock(mutex_); CIDToReceiverMap::const_iterator index = m_idTable.find(id); if (index != m_idTable.end()) { modifyIconNoLock(index->second, NIM_MODIFY); } - ARCH->unlockMutex(m_mutex); } void ArchTaskBarWindows::addAllIcons() { - ARCH->lockMutex(m_mutex); + std::lock_guard lock(mutex_); for (ReceiverToInfoMap::const_iterator index = m_receivers.begin(); index != m_receivers.end(); ++index) { modifyIconNoLock(index, NIM_ADD); } - ARCH->unlockMutex(m_mutex); } void ArchTaskBarWindows::removeAllIcons() { - ARCH->lockMutex(m_mutex); + std::lock_guard lock(mutex_); for (ReceiverToInfoMap::const_iterator index = m_receivers.begin(); index != m_receivers.end(); ++index) { removeIconNoLock(index->second.m_id); } - ARCH->unlockMutex(m_mutex); } void @@ -332,7 +315,7 @@ ArchTaskBarWindows::processDialogs(MSG* msg) // at any given time. that's not a problem since only our event // loop calls this method and there's just one of those. - ARCH->lockMutex(m_mutex); + std::unique_lock lock(mutex_); // remove removed dialogs m_dialogs.erase(false); @@ -344,7 +327,6 @@ ArchTaskBarWindows::processDialogs(MSG* msg) } m_addedDialogs.clear(); - ARCH->unlockMutex(m_mutex); // check message against all dialogs until one handles it. // note that we don't hold a lock while checking because @@ -352,18 +334,16 @@ ArchTaskBarWindows::processDialogs(MSG* msg) // object. that's okay because addDialog() and // removeDialog() don't change the map itself (just the // values of some elements). - ARCH->lockMutex(m_mutex); for (Dialogs::const_iterator index = m_dialogs.begin(); index != m_dialogs.end(); ++index) { if (index->second) { - ARCH->unlockMutex(m_mutex); + lock.unlock(); if (IsDialogMessage(index->first, msg)) { return true; } - ARCH->lockMutex(m_mutex); + lock.lock(); } } - ARCH->unlockMutex(m_mutex); return false; } @@ -473,10 +453,11 @@ ArchTaskBarWindows::threadMainLoop() static_cast(this)); // signal ready - ARCH->lockMutex(m_mutex); - m_ready = true; - ARCH->broadcastCondVar(m_condVar); - ARCH->unlockMutex(m_mutex); + { + std::lock_guard lock(mutex_); + m_ready = true; + cond_var_.notify_all(); + } // handle failure if (m_hwnd == NULL) { diff --git a/src/lib/arch/win32/ArchTaskBarWindows.h b/src/lib/arch/win32/ArchTaskBarWindows.h index 2b8b7ad6..95d57f04 100644 --- a/src/lib/arch/win32/ArchTaskBarWindows.h +++ b/src/lib/arch/win32/ArchTaskBarWindows.h @@ -26,6 +26,8 @@ #define WIN32_LEAN_AND_MEAN #include +#include + #define ARCH_TASKBAR ArchTaskBarWindows //! Win32 implementation of IArchTaskBar @@ -91,8 +93,8 @@ private: static ArchTaskBarWindows* s_instance; // multithread data - ArchMutex m_mutex; - ArchCond m_condVar; + std::mutex mutex_; + std::condition_variable cond_var_; bool m_ready; int m_result; ArchThread m_thread;