Skip to content

Commit

Permalink
handle tcp tx in a separate thread
Browse files Browse the repository at this point in the history
  • Loading branch information
hikalium committed Aug 31, 2024
1 parent 888534e commit ecdbc6a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 0 additions & 1 deletion os/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ pub async fn run(cmdline: &str) -> Result<()> {
sock.tx_data()
.lock()
.extend(format!("GET / HTTP/1.0\nHost: {host}\n\n").bytes());
sock.poll_tx()?;
}
"arp" => {
println!("{:?}", network.arp_table_cloned())
Expand Down
17 changes: 17 additions & 0 deletions os/src/net/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ extern crate alloc;
use crate::error::Error;
use crate::error::Result;
use crate::executor::spawn_global;
use crate::executor::yield_execution;
use crate::executor::TimeoutFuture;
use crate::info;
use crate::mutex::Mutex;
Expand Down Expand Up @@ -206,6 +207,22 @@ impl Network {
self.register_tcp_socket(sock.clone())?;
sock.set_self_ip(self.self_ip());
sock.open()?;
{
// Launch a thread to process tx data
let sock = Rc::downgrade(&sock);
spawn_global(async move {
loop {
if let Some(sock) = sock.upgrade() {
let _ = sock.poll_tx();
} else {
info!("tcp: socket tx handler exiting");
break;
}
yield_execution().await;
}
Ok(())
})
}
Ok(sock)
}
}
Expand Down
3 changes: 3 additions & 0 deletions os/src/net/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,9 @@ impl TcpSocket {
Ok(())
}
pub fn poll_tx(&self) -> Result<()> {
if self.tx_data.lock().is_empty() {
return Ok(());
}
let to_ip = self
.another_ip()
.ok_or(Error::Failed("another_ip should be populated"))?;
Expand Down

0 comments on commit ecdbc6a

Please sign in to comment.