Optimistic update to controls.position in try_seek #608
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.
Hi! Big fan of Rodio here. I've been using it in a project of mine for about a month and absolutely love it. Thanks for maintaining it ❤️
I'm using this change and it's working, but I haven't thoroughly tested it, nor deeply thought about possible side effects, and I'm pretty new to Rust... so I hope this PR isn't too bad 🙏
The goal of this PR is that the following no longer happens:
With this PR, we won't need the
thread::sleep(Duration::from_millis(20));
. Since thetry_seek
blocks,get_pos
should return the correct value immediately after callingtry_seek
(if it succeeds).What comes to mind when I think of what this change could break is: is anything relying on
controls.position
not changing at all here? As far as I can see, we only read this value inpub fn get_pos(&self)
, and don't do any math with it, so I guess it should be fine.Maybe there could be a race condition here, if
periodic_access
happens to run afterTrackPosition.samples_counted
has been updated with the new position but before we get to run*self.controls.position.lock().unwrap() = pos;
, or, more generally, ifSource.try_seek()
isn't in sync withTrackPosition.next()
, in which case we'd overwrite the correct value with a less-correct one. But, even then, wouldn't that error be better than the current one?Also now
try_seek
locks oncontrols.position
, but I think all read/writes from/to it are pseudo-atomic (lock, read/write, release). I can't think of a way this could dead-lock, but I n ever did any professional work with threads.