Skip to content

Commit

Permalink
Merge pull request #1661 from CastagnaIT/fix_spurious_wakeups
Browse files Browse the repository at this point in the history
[AdaptiveTree] Fix spurious wakeups on TreeUpdateThread
  • Loading branch information
CastagnaIT authored Aug 28, 2024
2 parents a7ef01d + 9648320 commit 57d6f36
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions src/common/AdaptiveTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,24 +279,31 @@ namespace adaptive

while (m_tree->m_updateInterval != NO_VALUE && m_tree->m_updateInterval > 0 && !m_threadStop)
{
if (m_cvUpdInterval.wait_for(updLck, std::chrono::milliseconds(m_tree->m_updateInterval)) ==
std::cv_status::timeout)
{
updLck.unlock();
// If paused, wait until last "Resume" will be called
std::unique_lock<std::mutex> lckWait(m_waitMutex);
m_cvWait.wait(lckWait, [&] { return m_waitQueue == 0; });
if (m_threadStop)
break;

updLck.lock();

// Reset interval value to allow forced update from manifest
if (m_resetInterval)
m_tree->m_updateInterval = PLAYLIST::NO_VALUE;

m_tree->OnUpdateSegments();
}
auto nowTime = std::chrono::steady_clock::now();

std::chrono::milliseconds intervalMs = std::chrono::milliseconds(m_tree->m_updateInterval);
// Wait for the interval time, the predicate method is used to avoid spurious wakeups
// and to allow exit early when notify_all is called to force stop operations
m_cvUpdInterval.wait_for(updLck, intervalMs,
[&nowTime, &intervalMs, this] {
return std::chrono::steady_clock::now() - nowTime >= intervalMs ||
m_threadStop;
});

updLck.unlock();
// If paused, wait until last "Resume" will be called
std::unique_lock<std::mutex> lckWait(m_waitMutex);
m_cvWait.wait(lckWait, [&] { return m_waitQueue == 0; });
if (m_threadStop)
break;

updLck.lock();

// Reset interval value to allow forced update from manifest
if (m_resetInterval)
m_tree->m_updateInterval = PLAYLIST::NO_VALUE;

m_tree->OnUpdateSegments();
}
}

Expand Down

0 comments on commit 57d6f36

Please sign in to comment.