Merge pull request #4043 from embassy-rs/james/wake-race

embassy-rp: defensive change to ensure wakers are registered
This commit is contained in:
James Munns 2025-04-04 11:45:13 +00:00 committed by GitHub
commit 61b7762421
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 4 deletions

View File

@ -114,17 +114,19 @@ impl<'d, T: Instance> I2c<'d, T, Async> {
}
/// Calls `f` to check if we are ready or not.
/// If not, `g` is called once the waker is set (to eg enable the required interrupts).
/// If not, `g` is called once(to eg enable the required interrupts).
/// The waker will always be registered prior to calling `f`.
async fn wait_on<F, U, G>(&mut self, mut f: F, mut g: G) -> U
where
F: FnMut(&mut Self) -> Poll<U>,
G: FnMut(&mut Self),
{
future::poll_fn(|cx| {
// Register prior to checking the condition
T::waker().register(cx.waker());
let r = f(self);
if r.is_pending() {
T::waker().register(cx.waker());
g(self);
}
r

View File

@ -159,7 +159,8 @@ impl<'d, T: Instance> I2cSlave<'d, T> {
}
/// Calls `f` to check if we are ready or not.
/// If not, `g` is called once the waker is set (to eg enable the required interrupts).
/// If not, `g` is called once(to eg enable the required interrupts).
/// The waker will always be registered prior to calling `f`.
#[inline(always)]
async fn wait_on<F, U, G>(&mut self, mut f: F, mut g: G) -> U
where
@ -167,10 +168,11 @@ impl<'d, T: Instance> I2cSlave<'d, T> {
G: FnMut(&mut Self),
{
future::poll_fn(|cx| {
// Register prior to checking the condition
T::waker().register(cx.waker());
let r = f(self);
if r.is_pending() {
T::waker().register(cx.waker());
g(self);
}