lib/arch/win32: Use standard mutex primitives in ArchTaskBarWindows

This commit is contained in:
Povilas Kanapickas 2021-11-03 02:58:39 +02:00
parent cdbf0dc15c
commit aa1c3ae7a4
2 changed files with 19 additions and 36 deletions

View File

@ -39,8 +39,6 @@ static const UINT kFirstReceiverID = WM_USER + 14;
ArchTaskBarWindows* ArchTaskBarWindows::s_instance = NULL; ArchTaskBarWindows* ArchTaskBarWindows::s_instance = NULL;
ArchTaskBarWindows::ArchTaskBarWindows() : ArchTaskBarWindows::ArchTaskBarWindows() :
m_mutex(NULL),
m_condVar(NULL),
m_ready(false), m_ready(false),
m_result(0), m_result(0),
m_thread(NULL), m_thread(NULL),
@ -59,29 +57,19 @@ ArchTaskBarWindows::~ArchTaskBarWindows()
ARCH->wait(m_thread, -1.0); ARCH->wait(m_thread, -1.0);
ARCH->closeThread(m_thread); ARCH->closeThread(m_thread);
} }
if (m_condVar != NULL) {
ARCH->closeCondVar(m_condVar);
}
if (m_mutex != NULL) {
ARCH->closeMutex(m_mutex);
}
s_instance = NULL; s_instance = NULL;
} }
void void
ArchTaskBarWindows::init() ArchTaskBarWindows::init()
{ {
// we need a mutex
m_mutex = ARCH->newMutex();
// and a condition variable which uses the above mutex // and a condition variable which uses the above mutex
m_ready = false; m_ready = false;
m_condVar = ARCH->newCondVar();
// we're going to want to get a result from the thread we're // we're going to want to get a result from the thread we're
// about to create to know if it initialized successfully. // about to create to know if it initialized successfully.
// so we lock the condition variable. // so we lock the condition variable.
ArchMutexLock lock(m_mutex); std::unique_lock<std::mutex> lock(mutex_);
// open a window and run an event loop in a separate thread. // open a window and run an event loop in a separate thread.
// this has to happen in a separate thread because if we // this has to happen in a separate thread because if we
@ -92,7 +80,7 @@ ArchTaskBarWindows::init()
// wait for child thread // wait for child thread
while (!m_ready) { 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 void
ArchTaskBarWindows::addIcon(UINT id) ArchTaskBarWindows::addIcon(UINT id)
{ {
ARCH->lockMutex(m_mutex); std::lock_guard<std::mutex> lock(mutex_);
CIDToReceiverMap::const_iterator index = m_idTable.find(id); CIDToReceiverMap::const_iterator index = m_idTable.find(id);
if (index != m_idTable.end()) { if (index != m_idTable.end()) {
modifyIconNoLock(index->second, NIM_ADD); modifyIconNoLock(index->second, NIM_ADD);
} }
ARCH->unlockMutex(m_mutex);
} }
void void
ArchTaskBarWindows::removeIcon(UINT id) ArchTaskBarWindows::removeIcon(UINT id)
{ {
ARCH->lockMutex(m_mutex); std::lock_guard<std::mutex> lock(mutex_);
removeIconNoLock(id); removeIconNoLock(id);
ARCH->unlockMutex(m_mutex);
} }
void void
ArchTaskBarWindows::updateIcon(UINT id) ArchTaskBarWindows::updateIcon(UINT id)
{ {
ARCH->lockMutex(m_mutex); std::lock_guard<std::mutex> lock(mutex_);
CIDToReceiverMap::const_iterator index = m_idTable.find(id); CIDToReceiverMap::const_iterator index = m_idTable.find(id);
if (index != m_idTable.end()) { if (index != m_idTable.end()) {
modifyIconNoLock(index->second, NIM_MODIFY); modifyIconNoLock(index->second, NIM_MODIFY);
} }
ARCH->unlockMutex(m_mutex);
} }
void void
ArchTaskBarWindows::addAllIcons() ArchTaskBarWindows::addAllIcons()
{ {
ARCH->lockMutex(m_mutex); std::lock_guard<std::mutex> lock(mutex_);
for (ReceiverToInfoMap::const_iterator index = m_receivers.begin(); for (ReceiverToInfoMap::const_iterator index = m_receivers.begin();
index != m_receivers.end(); ++index) { index != m_receivers.end(); ++index) {
modifyIconNoLock(index, NIM_ADD); modifyIconNoLock(index, NIM_ADD);
} }
ARCH->unlockMutex(m_mutex);
} }
void void
ArchTaskBarWindows::removeAllIcons() ArchTaskBarWindows::removeAllIcons()
{ {
ARCH->lockMutex(m_mutex); std::lock_guard<std::mutex> lock(mutex_);
for (ReceiverToInfoMap::const_iterator index = m_receivers.begin(); for (ReceiverToInfoMap::const_iterator index = m_receivers.begin();
index != m_receivers.end(); ++index) { index != m_receivers.end(); ++index) {
removeIconNoLock(index->second.m_id); removeIconNoLock(index->second.m_id);
} }
ARCH->unlockMutex(m_mutex);
} }
void void
@ -332,7 +315,7 @@ ArchTaskBarWindows::processDialogs(MSG* msg)
// at any given time. that's not a problem since only our event // at any given time. that's not a problem since only our event
// loop calls this method and there's just one of those. // loop calls this method and there's just one of those.
ARCH->lockMutex(m_mutex); std::unique_lock<std::mutex> lock(mutex_);
// remove removed dialogs // remove removed dialogs
m_dialogs.erase(false); m_dialogs.erase(false);
@ -344,7 +327,6 @@ ArchTaskBarWindows::processDialogs(MSG* msg)
} }
m_addedDialogs.clear(); m_addedDialogs.clear();
ARCH->unlockMutex(m_mutex);
// check message against all dialogs until one handles it. // check message against all dialogs until one handles it.
// note that we don't hold a lock while checking because // 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 // object. that's okay because addDialog() and
// removeDialog() don't change the map itself (just the // removeDialog() don't change the map itself (just the
// values of some elements). // values of some elements).
ARCH->lockMutex(m_mutex);
for (Dialogs::const_iterator index = m_dialogs.begin(); for (Dialogs::const_iterator index = m_dialogs.begin();
index != m_dialogs.end(); ++index) { index != m_dialogs.end(); ++index) {
if (index->second) { if (index->second) {
ARCH->unlockMutex(m_mutex); lock.unlock();
if (IsDialogMessage(index->first, msg)) { if (IsDialogMessage(index->first, msg)) {
return true; return true;
} }
ARCH->lockMutex(m_mutex); lock.lock();
} }
} }
ARCH->unlockMutex(m_mutex);
return false; return false;
} }
@ -473,10 +453,11 @@ ArchTaskBarWindows::threadMainLoop()
static_cast<void*>(this)); static_cast<void*>(this));
// signal ready // signal ready
ARCH->lockMutex(m_mutex); {
std::lock_guard<std::mutex> lock(mutex_);
m_ready = true; m_ready = true;
ARCH->broadcastCondVar(m_condVar); cond_var_.notify_all();
ARCH->unlockMutex(m_mutex); }
// handle failure // handle failure
if (m_hwnd == NULL) { if (m_hwnd == NULL) {

View File

@ -26,6 +26,8 @@
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
#include <Windows.h> #include <Windows.h>
#include <condition_variable>
#define ARCH_TASKBAR ArchTaskBarWindows #define ARCH_TASKBAR ArchTaskBarWindows
//! Win32 implementation of IArchTaskBar //! Win32 implementation of IArchTaskBar
@ -91,8 +93,8 @@ private:
static ArchTaskBarWindows* s_instance; static ArchTaskBarWindows* s_instance;
// multithread data // multithread data
ArchMutex m_mutex; std::mutex mutex_;
ArchCond m_condVar; std::condition_variable cond_var_;
bool m_ready; bool m_ready;
int m_result; int m_result;
ArchThread m_thread; ArchThread m_thread;