Skip to content

Truly independent web browser

LibMedia: Make the incremental stream cursors' abort flag atomic

Zaggy102400e2f5cdb63d
diff --git a/Libraries/LibMedia/IncrementallyPopulatedStream.cpp b/Libraries/LibMedia/IncrementallyPopulatedStream.cpp
index 00ee416ea57..11309d15769 100644
--- a/Libraries/LibMedia/IncrementallyPopulatedStream.cpp
+++ b/Libraries/LibMedia/IncrementallyPopulatedStream.cpp
@@ -355,7 +355,7 @@ DecoderErrorOr<size_t> IncrementallyPopulatedStream::read_at(Cursor& cursor, siz
MutexLocker locker { m_mutex };
bool notified_blocked = false;
- while (!cursor.m_aborted && cursor.m_is_blocking) {
+ while (!cursor.m_aborted.load(AK::MemoryOrder::memory_order_relaxed) && cursor.m_is_blocking) {
if (check_if_data_is_available_or_begin_request_while_locked(cursor, position, bytes.size()))
break;
@@ -375,7 +375,7 @@ DecoderErrorOr<size_t> IncrementallyPopulatedStream::read_at(Cursor& cursor, siz
cursor.m_read_blocked_change_handler(ReadBlocked::No);
}
- if (cursor.m_aborted)
+ if (cursor.m_aborted.load(AK::MemoryOrder::memory_order_relaxed))
return DecoderError::with_description(DecoderErrorCategory::Aborted, "Blocking read was aborted"sv);
if (m_closed && position >= m_expected_size.value())
@@ -473,7 +473,7 @@ DecoderErrorOr<FixedArray<u8>> IncrementallyPopulatedStream::Cursor::read_bytes(
void IncrementallyPopulatedStream::Cursor::abort()
{
MutexLocker locker { m_stream->m_mutex };
- m_aborted = true;
+ m_aborted.store(true, AK::MemoryOrder::memory_order_relaxed);
m_stream->m_state_changed.broadcast();
}
diff --git a/Libraries/LibMedia/IncrementallyPopulatedStream.h b/Libraries/LibMedia/IncrementallyPopulatedStream.h
index a63d325a288..b13444f789a 100644
--- a/Libraries/LibMedia/IncrementallyPopulatedStream.h
+++ b/Libraries/LibMedia/IncrementallyPopulatedStream.h
@@ -68,8 +68,8 @@ public:
virtual size_t blocking_size() const override { return m_stream->size(); }
virtual void abort() override;
- virtual void reset_abort() override { m_aborted = false; }
- virtual bool is_aborted() const override { return m_aborted; }
+ virtual void reset_abort() override { m_aborted.store(false, AK::MemoryOrder::memory_order_relaxed); }
+ virtual bool is_aborted() const override { return m_aborted.load(AK::MemoryOrder::memory_order_relaxed); }
virtual void set_blocked_change_handler(ReadBlockedChangeHandler) override;
@@ -81,7 +81,9 @@ public:
NonnullRefPtr<IncrementallyPopulatedStream> m_stream;
bool m_is_blocking { true };
size_t m_position { 0 };
- bool m_aborted { false };
+ // Set from any thread under the stream's mutex, which orders it against the blocked read; only the reading
+ // thread clears it, between its reads.
+ Atomic<bool> m_aborted { false };
bool m_blocked { false };
ReadBlockedChangeHandler m_read_blocked_change_handler;
MonotonicTime m_active_timeout { MonotonicTime::now_coarse() };