Skip to content

Commit 8587bd1

Browse files
committed
avoid the usage of blocking spinlock by polling the wasm interface
1 parent 4dcf117 commit 8587bd1

File tree

3 files changed

+30
-23
lines changed

3 files changed

+30
-23
lines changed

src/executor/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ pub(crate) fn run() {
113113
)]
114114
pub(crate) fn spawn<F>(future: F)
115115
where
116-
F: Future<Output = ()> + 'static,
116+
F: Future<Output = ()> + 'static + core::marker::Send,
117117
{
118118
core_local::ex().spawn(AsyncTask::new(future)).detach();
119119
}

src/executor/network.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ async fn dhcpv4_run() {
182182
nic.dns_handle = None;
183183
}
184184
}
185-
}
185+
};
186186

187187
Poll::<()>::Pending
188188
})
@@ -229,7 +229,7 @@ pub(crate) async fn get_query_result(query: QueryHandle) -> io::Result<Vec<IpAdd
229229
ips.push(*x);
230230
}
231231

232-
Poll::Ready(Ok(ips))
232+
Poll::Ready(Ok(ips))
233233
}
234234
Err(GetQueryResultError::Pending) => {
235235
socket.register_query_waker(query, cx.waker());

src/wasm/mod.rs

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use core::ffi::CStr;
66
use core::future;
77
use core::hint::black_box;
88
use core::mem::MaybeUninit;
9+
use core::pin::pin;
910
use core::task::Poll;
1011

1112
use hermit_sync::{InterruptTicketMutex, Lazy};
@@ -579,28 +580,34 @@ impl WasmManager {
579580

580581
async fn wasm_run() {
581582
loop {
582-
while let Some((fd, data)) = OUTPUT.lock().data.pop_front() {
583-
let obj = match fd {
584-
Descriptor::Stdout => crate::core_scheduler()
585-
.get_object(fd::STDOUT_FILENO)
586-
.unwrap(),
587-
Descriptor::Stderr => crate::core_scheduler()
588-
.get_object(fd::STDERR_FILENO)
589-
.unwrap(),
590-
Descriptor::RawFd(raw_fd) => crate::core_scheduler().get_object(raw_fd).unwrap(),
591-
_ => panic!("Unsuppted {fd:?}"),
592-
};
593-
594-
obj.write(&data).await.unwrap();
595-
}
596-
597583
future::poll_fn(|cx| {
598-
let mut guard = OUTPUT.lock();
599-
if guard.data.is_empty() {
600-
guard.waker.register(cx.waker());
601-
Poll::Pending
584+
if let Some(mut guard) = OUTPUT.try_lock() {
585+
if let Some((fd, data)) = guard.data.pop_front() {
586+
let obj = match fd {
587+
Descriptor::Stdout => crate::core_scheduler()
588+
.get_object(fd::STDOUT_FILENO)
589+
.unwrap(),
590+
Descriptor::Stderr => crate::core_scheduler()
591+
.get_object(fd::STDERR_FILENO)
592+
.unwrap(),
593+
Descriptor::RawFd(raw_fd) => {
594+
crate::core_scheduler().get_object(raw_fd).unwrap()
595+
}
596+
_ => panic!("Unsuppted {fd:?}"),
597+
};
598+
599+
drop(guard);
600+
while let Poll::Pending = pin!(obj.write(&data)).poll(cx) {}
601+
602+
cx.waker().wake_by_ref();
603+
Poll::<()>::Pending
604+
} else {
605+
guard.waker.register(cx.waker());
606+
Poll::<()>::Pending
607+
}
602608
} else {
603-
Poll::Ready(())
609+
cx.waker().wake_by_ref();
610+
Poll::<()>::Pending
604611
}
605612
})
606613
.await;

0 commit comments

Comments
 (0)