playbackcache: made thread-safe

Since this class is regularly accessed by multiple threads, it makes the
most sense to make all of its functions thread-safe.
This commit is contained in:
itsmattkc
2020-06-03 03:38:28 +10:00
parent 08eb12710c
commit e30d278327
6 changed files with 183 additions and 70 deletions
+79 -48
View File
@@ -22,21 +22,93 @@
OLIVE_NAMESPACE_ENTER
PlaybackCache::PlaybackCache()
{
}
void PlaybackCache::Invalidate(const TimeRange &r)
{
invalidated_.InsertTimeRange(r);
QMutexLocker locker(&lock_);
InvalidateEvent(r);
NoLockInvalidate(r);
locker.unlock();
emit Invalidated(r);
}
void PlaybackCache::InvalidateAll()
{
QMutexLocker locker(&lock_);
TimeRange invalidate_range(0, length_);
NoLockInvalidate(invalidate_range);
locker.unlock();
emit Invalidated(invalidate_range);
}
void PlaybackCache::SetLength(const rational &r)
{
QMutexLocker locker(&lock_);
NoLockSetLength(r);
}
void PlaybackCache::Shift(const rational &from, const rational &to)
{
QMutexLocker locker(&lock_);
// An region between `from` and `to` will be inserted or spliced out
TimeRangeList ranges_to_shift = invalidated_.Intersects(TimeRange(from, RATIONAL_MAX));
// Remove everything from the minimum point
invalidated_.RemoveTimeRange(TimeRange(qMin(from, to), RATIONAL_MAX));
// Shift everything in our ranges to shift list
//
// `diff` is POSITIVE when moving forward -> and NEGATIVE when moving backward <-
rational diff = to - from;
foreach (const TimeRange& r, ranges_to_shift) {
invalidated_.InsertTimeRange(r + diff);
}
ShiftEvent(from, to);
if (diff > rational()) {
// If shifting forward, add this section to the invalidated region
TimeRange invalidate_range(from, to);
NoLockInvalidate(invalidate_range);
locker.unlock();
emit Invalidated(invalidate_range);
}
}
void PlaybackCache::Validate(const TimeRange &r)
{
QMutexLocker locker(&lock_);
NoLockValidate(r);
locker.unlock();
emit Validated(r);
}
void PlaybackCache::NoLockInvalidate(const TimeRange &r)
{
invalidated_.InsertTimeRange(r);
InvalidateEvent(r);
}
void PlaybackCache::NoLockValidate(const TimeRange &r)
{
invalidated_.RemoveTimeRange(r);
}
void PlaybackCache::NoLockSetLength(const rational &r)
{
if (length_ == r) {
// Same length - do nothing
@@ -56,47 +128,6 @@ void PlaybackCache::SetLength(const rational &r)
length_ = r;
}
bool PlaybackCache::IsFullyValidated() const
{
return invalidated_.isEmpty();
}
void PlaybackCache::Shift(const rational &from, const rational &to)
{
// An region between `from` and `to` will be inserted or spliced out
TimeRangeList ranges_to_shift = invalidated_.Intersects(TimeRange(from, RATIONAL_MAX));
// Remove everything from the minimum point
invalidated_.RemoveTimeRange(TimeRange(qMin(from, to), RATIONAL_MAX));
// Shift everything in our ranges to shift list
//
// `diff` is POSITIVE when moving forward -> and NEGATIVE when moving backward <-
rational diff = to - from;
foreach (const TimeRange& r, ranges_to_shift) {
invalidated_.InsertTimeRange(r + diff);
}
ShiftEvent(from, to);
if (diff > rational()) {
// If shifting forward, add this section to the invalidated region
Invalidate(TimeRange(from, to));
}
}
void PlaybackCache::Validate(const TimeRange &r)
{
invalidated_.RemoveTimeRange(r);
emit Validated(r);
}
void PlaybackCache::InvalidateAll()
{
Invalidate(TimeRange(0, length_));
}
void PlaybackCache::LengthChangedEvent(const rational &, const rational &)
{
}