1
0
mirror of https://github.com/rcore-os/rCore.git synced 2025-01-18 17:07:04 +04:00

Add yield to avoid starvation

This commit is contained in:
Jiajie Chen 2020-06-29 11:09:21 +08:00
parent bc525e1902
commit 68609a0f7c
2 changed files with 40 additions and 4 deletions

View File

@ -511,7 +511,10 @@ pub fn spawn(thread: Arc<Thread>) {
thread.end_running(cx);
if exit {
info!("thread {} stopped", thread.tid);
break;
} else {
yield_now().await;
}
}
};
@ -556,3 +559,28 @@ impl Future for PageTableSwitchWrapper {
res
}
}
/// Yields execution back to the async runtime.
pub fn yield_now() -> impl Future<Output = ()> {
YieldFuture::default()
}
#[must_use = "yield_now does nothing unless polled/`await`-ed"]
#[derive(Default)]
struct YieldFuture {
flag: bool,
}
impl Future for YieldFuture {
type Output = ();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
if self.flag {
Poll::Ready(())
} else {
self.flag = true;
cx.waker().clone().wake();
Poll::Pending
}
}
}

View File

@ -110,15 +110,23 @@ impl Syscall<'_> {
}
if !set.is_null() {
let set = set.read()?;
info!("rt_sigprocmask: set: {:?}", set);
const BLOCK: usize = 0;
const UNBLOCK: usize = 1;
const SETMASK: usize = 2;
let mut inner = self.thread.inner.lock();
match how {
BLOCK => inner.sig_mask.add_set(&set),
UNBLOCK => inner.sig_mask.remove_set(&set),
SETMASK => inner.sig_mask = set,
BLOCK => {
info!("rt_sigprocmask: block: {:x?}", set);
inner.sig_mask.add_set(&set);
}
UNBLOCK => {
info!("rt_sigprocmask: unblock: {:x?}", set);
inner.sig_mask.remove_set(&set)
}
SETMASK => {
info!("rt_sigprocmask: set: {:x?}", set);
inner.sig_mask = set;
}
_ => return Err(EINVAL),
}
}