2012-06-10 16:50:54 +00:00
|
|
|
/*
|
2024-09-17 19:00:25 +00:00
|
|
|
* Deskflow -- mouse and keyboard sharing utility
|
2025-01-24 00:11:17 +00:00
|
|
|
* SPDX-FileCopyrightText: (C) 2012 - 2016 Symless Ltd.
|
|
|
|
|
* SPDX-FileCopyrightText: (C) 2002 Chris Schoeneman
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
|
2012-06-10 16:50:54 +00:00
|
|
|
*/
|
|
|
|
|
|
2014-02-28 12:36:45 +00:00
|
|
|
#include "mt/CondVar.h"
|
|
|
|
|
#include "arch/Arch.h"
|
|
|
|
|
#include "base/Stopwatch.h"
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
|
//
|
2014-11-11 13:51:47 +00:00
|
|
|
// CondVarBase
|
2012-06-10 16:50:54 +00:00
|
|
|
//
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
CondVarBase::CondVarBase(Mutex *mutex) : m_mutex(mutex)
|
|
|
|
|
{
|
2025-04-27 19:39:23 +00:00
|
|
|
assert(m_mutex != nullptr);
|
2024-07-02 19:07:06 +00:00
|
|
|
m_cond = ARCH->newCondVar();
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
CondVarBase::~CondVarBase()
|
|
|
|
|
{
|
|
|
|
|
ARCH->closeCondVar(m_cond);
|
|
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void CondVarBase::lock() const
|
|
|
|
|
{
|
|
|
|
|
m_mutex->lock();
|
|
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void CondVarBase::unlock() const
|
|
|
|
|
{
|
|
|
|
|
m_mutex->unlock();
|
|
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void CondVarBase::signal()
|
|
|
|
|
{
|
|
|
|
|
ARCH->signalCondVar(m_cond);
|
|
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void CondVarBase::broadcast()
|
|
|
|
|
{
|
|
|
|
|
ARCH->broadcastCondVar(m_cond);
|
|
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
bool CondVarBase::wait(Stopwatch &timer, double timeout) const
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
double remain = timeout - timer.getTime();
|
|
|
|
|
// Some ARCH wait()s return prematurely, retry until really timed out
|
|
|
|
|
// In particular, ArchMultithreadPosix::waitCondVar() returns every 100ms
|
|
|
|
|
do {
|
|
|
|
|
// Always call wait at least once, even if remain is 0, to give
|
|
|
|
|
// other thread a chance to grab the mutex to avoid deadlocks on
|
|
|
|
|
// busy waiting.
|
|
|
|
|
if (remain < 0.0)
|
|
|
|
|
remain = 0.0;
|
|
|
|
|
if (wait(remain))
|
|
|
|
|
return true;
|
|
|
|
|
remain = timeout - timer.getTime();
|
|
|
|
|
} while (remain >= 0.0);
|
|
|
|
|
return false;
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
bool CondVarBase::wait(double timeout) const
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
return ARCH->waitCondVar(m_cond, m_mutex->m_mutex, timeout);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
Mutex *CondVarBase::getMutex() const
|
|
|
|
|
{
|
|
|
|
|
return m_mutex;
|
|
|
|
|
}
|