Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement the tcp interface of wasi-sockets. #6837

Merged
merged 16 commits into from
Aug 23, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move tcp_state out of the Arc.
  • Loading branch information
sunfishcode committed Aug 22, 2023
commit 9aaa507af5cefea7c5f68a7544288397e917f153
26 changes: 9 additions & 17 deletions crates/wasi/src/preview2/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ pub(crate) struct HostTcpSocket {
/// The part of a `HostTcpSocket` which is reference-counted so that we
/// can pass it to async tasks.
pub(crate) inner: Arc<HostTcpSocketInner>,

/// The current state in the bind/listen/accept/connect progression.
pub(crate) tcp_state: RwLock<HostTcpState>,
}

/// The inner reference-counted state of a `HostTcpSocket`.
Expand All @@ -59,9 +62,6 @@ pub(crate) struct HostTcpSocketInner {
/// On non-Unix, we can use plain `poll`.
#[cfg(not(unix))]
pub(crate) tcp_socket: cap_std::net::TcpListener,

/// The current state in the bind/listen/accept/connect progression.
pub(crate) tcp_state: RwLock<HostTcpState>,
}

impl HostTcpSocket {
Expand All @@ -76,10 +76,8 @@ impl HostTcpSocket {
let tcp_socket = tokio::io::unix::AsyncFd::new(tcp_socket)?;

Ok(Self {
inner: Arc::new(HostTcpSocketInner {
tcp_socket,
tcp_state: RwLock::new(HostTcpState::Default),
}),
inner: Arc::new(HostTcpSocketInner { tcp_socket }),
tcp_state: RwLock::new(HostTcpState::Default),
})
}

Expand All @@ -95,10 +93,8 @@ impl HostTcpSocket {
let tcp_socket = tokio::io::unix::AsyncFd::new(tcp_socket)?;

Ok(Self {
inner: Arc::new(HostTcpSocketInner {
tcp_socket,
tcp_state: RwLock::new(HostTcpState::Default),
}),
inner: Arc::new(HostTcpSocketInner { tcp_socket }),
tcp_state: RwLock::new(HostTcpState::Default),
})
}

Expand All @@ -112,12 +108,12 @@ impl HostTcpSocket {

/// Acquire a reader lock for `self.tcp_state`.
pub fn tcp_state_read_lock(&self) -> RwLockReadGuard<HostTcpState> {
self.inner.tcp_state.read().unwrap()
self.tcp_state.read().unwrap()
}

/// Acquire a writer lock for `self.tcp_state`.
pub fn tcp_state_write_lock(&self) -> RwLockWriteGuard<HostTcpState> {
self.inner.tcp_state.write().unwrap()
self.tcp_state.write().unwrap()
}
}

Expand All @@ -132,10 +128,6 @@ impl HostTcpSocketInner {
tcp_socket
}

pub fn set_state(&self, new_state: HostTcpState) {
*self.tcp_state.write().unwrap() = new_state;
}

/// Spawn a task on tokio's blocking thread for performing blocking
/// syscalls on the underlying [`cap_std::net::TcpListener`].
#[cfg(not(unix))]
Expand Down