Merge pull request #2328 from rmja/signal-try-take

feat(embassy-sync): Add try_take() to signal
This commit is contained in:
Dario Nieuwenhuis 2023-12-20 12:34:01 +00:00 committed by GitHub
commit c8eb128a56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -111,6 +111,20 @@ where
poll_fn(move |cx| self.poll_wait(cx)) poll_fn(move |cx| self.poll_wait(cx))
} }
/// non-blocking method to try and take the signal value.
pub fn try_take(&self) -> Option<T> {
self.state.lock(|cell| {
let state = cell.replace(State::None);
match state {
State::Signaled(res) => Some(res),
state => {
cell.set(state);
None
}
}
})
}
/// non-blocking method to check whether this signal has been signaled. /// non-blocking method to check whether this signal has been signaled.
pub fn signaled(&self) -> bool { pub fn signaled(&self) -> bool {
self.state.lock(|cell| { self.state.lock(|cell| {