round_based/state_machine/
runtime.rs

1use core::task::{Poll, ready};
2
3/// State machine runtime
4pub struct Runtime<M> {
5    shared_state: super::shared_state::SharedStateRef<M>,
6}
7
8impl<M> Runtime<M> {
9    pub(super) fn new(shared_state: super::shared_state::SharedStateRef<M>) -> Self {
10        Self { shared_state }
11    }
12}
13
14impl<M> crate::mpc::party::AsyncRuntime for Runtime<M> {
15    async fn yield_now(&self) {
16        YieldNow {
17            shared_state: self.shared_state.clone(),
18            yielded: false,
19        }
20        .await
21    }
22}
23
24/// Future returned by [`runtime.yield_now()`](Runtime)
25pub struct YieldNow<M> {
26    shared_state: super::shared_state::SharedStateRef<M>,
27    yielded: bool,
28}
29
30impl<M> core::future::Future for YieldNow<M> {
31    type Output = ();
32
33    fn poll(
34        mut self: core::pin::Pin<&mut Self>,
35        _cx: &mut core::task::Context<'_>,
36    ) -> Poll<Self::Output> {
37        if !self.yielded {
38            let scheduler = ready!(self.shared_state.can_schedule());
39            scheduler.protocol_yields();
40            self.yielded = true;
41            Poll::Pending
42        } else {
43            Poll::Ready(())
44        }
45    }
46}