use crate::ebr::Barrier;
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
pub(crate) async fn async_yield() {
#[derive(Default)]
struct YieldOnce(bool);
impl Future for YieldOnce {
type Output = ();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
if self.0 {
return Poll::Ready(());
}
self.0 = true;
cx.waker().wake_by_ref();
Poll::Pending
}
}
YieldOnce::default().await;
}
#[derive(Default)]
pub(super) struct AwaitableBarrier {
barrier: Option<Barrier>,
}
impl AwaitableBarrier {
pub(super) fn barrier(&mut self) -> &Barrier {
self.barrier.get_or_insert_with(Barrier::new)
}
pub(super) async fn drop_barrier_and_yield(&mut self) {
self.barrier.take();
async_yield().await;
}
}
#[allow(clippy::non_send_fields_in_send_ty)]
unsafe impl Send for AwaitableBarrier {
}