protected: /// worker-function of thread virtualvoid SAL_CALL run() override;
/// Checking and triggering of a timer event void checkForTimeout();
salhelper::Timer* &m_pHead; bool m_terminate; /// List Protection
std::mutex &m_Lock; /// Signal the insertion of a timer
std::condition_variable m_notEmpty;
namespace { class TimerManagerImpl final
{
std::mutex m_Lock; // shared lock with each impl. thread
salhelper::Timer* m_pHead; // the underlying shared queue
TimerManager::~TimerManager() {
{
std::scoped_lock g(m_Lock); // Sometimes, the TimerManager thread gets killed before the static's destruction; // in that case, notify_all could hang in unit tests if (!isRunning()) return;
m_terminate = true;
}
m_notEmpty.notify_all();
join();
}
void TimerManager::registerTimer(Timer* pTimer)
{ if (!pTimer) return;
// try to find one with equal or lower remaining time.
Timer** ppIter = &m_pHead;
while (*ppIter)
{ if (pTimer->expiresBefore(*ppIter))
{ // next element has higher remaining time, // => insert new timer before break;
}
ppIter= &((*ppIter)->m_pNext);
}
// next element has higher remaining time, // => insert new timer before
pTimer->m_pNext= *ppIter;
*ppIter = pTimer;
if (pTimer == m_pHead)
{
notify = true;
}
}
if (notify) { // it was inserted as new head // signal it to TimerManager Thread
m_notEmpty.notify_all();
}
}
void TimerManager::unregisterTimer(Timer * pTimer)
{ if (!pTimer) return;
auto found = false;
{ // lock access
std::lock_guard Guard(m_Lock);
Timer** ppIter = &m_pHead;
while (*ppIter)
{ if (pTimer == (*ppIter))
{ // remove timer from list
*ppIter = (*ppIter)->m_pNext;
found = true; break;
}
ppIter= &((*ppIter)->m_pNext);
}
}
if (found) {
pTimer->release();
}
}
bool TimerManager::lookupTimer(const Timer* pTimer)
{ if (!pTimer) returnfalse;
// lock access
std::lock_guard Guard(m_Lock);
// check the list for (Timer* pIter = m_pHead; pIter != nullptr; pIter= pIter->m_pNext)
{ if (pIter == pTimer) returntrue;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.