Fix mutex references and the global lock - #322
Open
martin-hughes wants to merge 1 commit into
Open
Conversation
Allow mutexes to be acquired and released by reference. This then allows the global reference `_GL` to be acquired and released. Correct the semantics of the global lock so that it handles the firmware lock correctly
|
Perhaps a syntax sugar closure like fn with_lock<R>(
&self,
mutex: Handle,
timeout: u16,
f: impl FnOnce() -> R,
) -> Result<R, AmlError> {
self.acquire(mutex, timeout)?;
struct Guard<'a, H: Handler> {
handler: &'a H,
mutex: Handle,
}
impl<H: Handler> Drop for Guard<'_, H> {
fn drop(&mut self) {
self.handler.release(self.mutex);
}
}
let _guard = Guard {
handler: self,
mutex,
};
Ok(f())
}Little helpers like this saved plenty of boot wedges for me |
Contributor
Author
|
Do you mean to add to this PR? If so would you show me an example of it being used? I can think why it might be generally useful but I'm not sure about needing it here |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This fixes #305 by correcting the mutex acquisition and release. It also allows mutexes to be acquired and released by reference, since otherwise it is not possible to acquire (or release)
_GL.@IsaacWoods the main question I had was about where to keep track of the reentrant acquisition count of
global_lock_mutex. Clearly theHandlershould not release the actual mutex until the count reaches zero, but we don't know what the count is.There were two options that I could think of:
Interpreter(which I chose), orHandlerto return a value indicating whether the mutex was fully released or not.Option 2 would require an API change, and also relies on the implementer to correctly deal with reentrancy. Whereas option 1 allows the crate to deal with an inadequately implemented
Handlerbut ifHandleris wrong then the actual mutex may come out of sync with the firmware lock.I would be happy to switch to option 2 if you prefer, Isaac.