Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[backport][AdaptiveTree] Fix spurious wakeups on TreeUpdateThread #1665

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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