LibCore+LibThreading: Add a thread-safe weak link for EventLoop #7039
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Since the event loop has a very specifically scoped lifetime, we can't ensure that it outlives threads that hold a reference to it without blocking the thread that owns it. In order to make threads use the event loop safely, we now have an atomically ref-counted EventLoopWeak class that can be passed off to threads to safely post events/callbacks to it.
Another possibility was to use an RWLock per event loop that each thread holds a read lock on, while ~EventLoop() uses a write lock to block and prevent it being destroyed until all its threads exit. However, media data providers don't receive a signal to exit due to the GC heap being intentionally leaked, so the process never actually exits. It would be possible to specifically drop the reference to PlaybackManager in HTMLMediaElement in order to make those data providers die on their own, but that doesn't help prevent this problem in other cases where it may arise.