Skip to content

Truly independent web browser

LibMedia: Make state that threads read without a lock atomic

Andreas Kling4ef9b74f304f
diff --git a/Libraries/LibMedia/IncrementallyPopulatedStream.cpp b/Libraries/LibMedia/IncrementallyPopulatedStream.cpp
index f572b3e9e61..30653bcccf4 100644
--- a/Libraries/LibMedia/IncrementallyPopulatedStream.cpp
+++ b/Libraries/LibMedia/IncrementallyPopulatedStream.cpp
@@ -301,13 +301,14 @@ u64 IncrementallyPopulatedStream::select_request_position_while_locked(u64 posit
continue;
if (now >= other_cursor.m_active_timeout && !other_cursor.m_blocked)
continue;
- if (other_cursor.m_position < request_position) {
- auto* other_cursor_chunk = m_chunks.find_largest_not_above(other_cursor.m_position);
- if (other_cursor_chunk && other_cursor_chunk->end() >= other_cursor.m_position) {
+ u64 other_position = other_cursor.m_position.load();
+ if (other_position < request_position) {
+ auto* other_cursor_chunk = m_chunks.find_largest_not_above(other_position);
+ if (other_cursor_chunk && other_cursor_chunk->end() >= other_position) {
request_position = other_cursor_chunk->end();
continue;
}
- request_position = other_cursor.m_position;
+ request_position = other_position;
}
}
return request_position;
@@ -504,7 +505,7 @@ DecoderErrorOr<void> IncrementallyPopulatedStream::Cursor::seek(i64 offset, AK::
if (position.has_overflow() || position.value() < 0)
return DecoderError::corrupted("Seek target lies outside the stream"sv);
- m_position = static_cast<size_t>(position.value());
+ m_position.store(static_cast<size_t>(position.value()));
return {};
}
diff --git a/Libraries/LibMedia/IncrementallyPopulatedStream.h b/Libraries/LibMedia/IncrementallyPopulatedStream.h
index a7c8538fa66..33e3c66dbd2 100644
--- a/Libraries/LibMedia/IncrementallyPopulatedStream.h
+++ b/Libraries/LibMedia/IncrementallyPopulatedStream.h
@@ -84,7 +84,9 @@ public:
NonnullRefPtr<IncrementallyPopulatedStream> m_stream;
bool m_is_blocking { true };
- size_t m_position { 0 };
+ // NB: Other cursors read this under the lock of the stream, but its own thread moves it without the lock. It only
+ // steers where the next request starts, so it needs no ordering.
+ Atomic<size_t, AK::memory_order_relaxed> m_position { 0 };
// 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 };
diff --git a/Libraries/LibMedia/Sinks/AudioPlaybackSink.cpp b/Libraries/LibMedia/Sinks/AudioPlaybackSink.cpp
index 4ac9b07180b..a369d2242fc 100644
--- a/Libraries/LibMedia/Sinks/AudioPlaybackSink.cpp
+++ b/Libraries/LibMedia/Sinks/AudioPlaybackSink.cpp
@@ -79,7 +79,7 @@ public:
Atomic<u32> m_seek_id { 0 };
// While the input is seeking, it still reports its state from before the seek.
bool m_input_seek_in_flight { false };
- bool m_audio_processor_should_exit { false };
+ Atomic<bool, AK::memory_order_relaxed> m_audio_processor_should_exit { false };
bool m_waiting_for_upstream_data { false };
bool m_resume_from_suspension_requested { false };
bool m_played_out_check_requested { false };