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

Async/await waker support. #394

Merged
merged 1 commit into from
Dec 27, 2020
Merged

Async/await waker support. #394

merged 1 commit into from
Dec 27, 2020

Conversation

Dirbaio
Copy link
Member

@Dirbaio Dirbaio commented Dec 26, 2020

This adds register_recv_waker and register_send_waker methods to all socket types.

This is the minimum required changes to smoltcp to support being used in async/await code. This opens the door for higher-level crates that expose wrappers implementing async traits like AsyncRead/AsyncWrite, Future, Stream...

I believe this is the best way to go, because:

  • It is not clear which traits should smoltcp implement:
    • The std ecosystem has multiple competing incompatible traits, mainly tokio and futures.
    • The no_std ecosystem hasn't converged on any standard io traits yet. The tokio and futures traits aren't no_std compatible.
  • There are non-trivial tradeoffs when implementing async support: how are sockets allocated, is there a single global tcp/ip stack or multiple, how are lifetimes handled.

By delegating these choices to external crates, smoltcp can stay un-opinionated.

There's an example of using this to implement AsyncBufRead and AsyncWrite in embassy here. Wrappers for the tokio or futures traits would look very similar. The use of unsafe there is not necessary in theory, AsyncRead and AsyncWrite can be implemented in 100% safe Rust. AsyncBufRead probably will require unsafe in most cases, unfortunately.

Previously discussed on #382 and #267 .

Copy link
Contributor

@whitequark whitequark left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM other than inline comments. Nice work!

src/socket/waker_store.rs Outdated Show resolved Hide resolved
@Dirbaio Dirbaio merged commit 73bb62a into master Dec 27, 2020
@Dirbaio Dirbaio deleted the async branch December 27, 2020 16:33
stlankes added a commit to stlankes/hermit-rs that referenced this pull request Mar 22, 2021
smoltcp 0.7 provides async/await waker support (smoltcp-rs/smoltcp#394).
I followed the example implementation https://github.com/embassy-rs/embassy/tree/net/embassy-net
to add waker support in hermit-sys.

However, hermit-sys can be used in a multi-threaded applications.
Consequently, the implementation must be thread safed. In addition,
we still have an "network thread", which is wakeup by an interrupt.
The thread wakeups all waiting future. If no future is available,
the thread calls directly the IP stack.
stlankes added a commit to stlankes/hermit-rs that referenced this pull request Mar 30, 2021
smoltcp 0.7 provides async/await waker support (smoltcp-rs/smoltcp#394).
I followed the example implementation https://github.com/embassy-rs/embassy/tree/net/embassy-net
to add waker support in hermit-sys.

However, hermit-sys can be used in a multi-threaded applications.
Consequently, the implementation must be thread safed. In addition,
we still have an "network thread", which is wakeup by an interrupt.
The thread wakeups all waiting future. If no future is available,
the thread calls directly the IP stack.
@Charles-Schleich
Copy link

Looks like the AsyncBufRead and AsyncWrite example for embassy mentioned earlier has been moved here.
For those having trouble finding it.

stlankes added a commit to stlankes/hermit-rs that referenced this pull request Jun 25, 2021
smoltcp 0.7 provides async/await waker support (smoltcp-rs/smoltcp#394).
I followed the example implementation https://github.com/embassy-rs/embassy/tree/net/embassy-net
to add waker support in hermit-sys.

However, hermit-sys can be used in a multi-threaded applications.
Consequently, the implementation must be thread safed. In addition,
we still have an "network thread", which is wakeup by an interrupt.
The thread wakeups all waiting future. If no future is available,
the thread calls directly the IP stack.
mkroening pushed a commit to mkroening/rusty-hermit that referenced this pull request Jun 29, 2021
smoltcp 0.7 provides async/await waker support (smoltcp-rs/smoltcp#394).
I followed the example implementation https://github.com/embassy-rs/embassy/tree/net/embassy-net
to add waker support in hermit-sys.

However, hermit-sys can be used in a multi-threaded applications.
Consequently, the implementation must be thread safed. In addition,
we still have an "network thread", which is wakeup by an interrupt.
The thread wakeups all waiting future. If no future is available,
the thread calls directly the IP stack.
stlankes added a commit to stlankes/hermit-rs that referenced this pull request Aug 2, 2021
smoltcp 0.7 provides async/await waker support (smoltcp-rs/smoltcp#394).
I followed the example implementation https://github.com/embassy-rs/embassy/tree/net/embassy-net
to add waker support in hermit-sys.

However, hermit-sys can be used in a multi-threaded applications.
Consequently, the implementation must be thread safed. In addition,
we still have an "network thread", which is wakeup by an interrupt.
The thread wakeups all waiting future. If no future is available,
the thread calls directly the IP stack.
stlankes added a commit to stlankes/hermit-rs that referenced this pull request Aug 2, 2021
smoltcp 0.7 provides async/await waker support (smoltcp-rs/smoltcp#394).
I followed the example implementation https://github.com/embassy-rs/embassy/tree/net/embassy-net
to add waker support in hermit-sys.

However, hermit-sys can be used in a multi-threaded applications.
Consequently, the implementation must be thread safed. In addition,
we still have an "network thread", which is wakeup by an interrupt.
The thread wakeups all waiting future. If no future is available,
the thread calls directly the IP stack.
stlankes added a commit to hermit-os/hermit-rs that referenced this pull request Aug 7, 2021
smoltcp 0.7 provides async/await waker support (smoltcp-rs/smoltcp#394).
I followed the example implementation https://github.com/embassy-rs/embassy/tree/net/embassy-net
to add waker support in hermit-sys.

However, hermit-sys can be used in a multi-threaded applications.
Consequently, the implementation must be thread safed. In addition,
we still have an "network thread", which is wakeup by an interrupt.
The thread wakeups all waiting future. If no future is available,
the thread calls directly the IP stack.
stlankes added a commit to hermit-os/hermit-rs that referenced this pull request Aug 7, 2021
smoltcp 0.7 provides async/await waker support (smoltcp-rs/smoltcp#394).
I followed the example implementation https://github.com/embassy-rs/embassy/tree/net/embassy-net
to add waker support in hermit-sys.

However, hermit-sys can be used in a multi-threaded applications.
Consequently, the implementation must be thread safed. In addition,
we still have an "network thread", which is wakeup by an interrupt.
The thread wakeups all waiting future. If no future is available,
the thread calls directly the IP stack.
@Dirbaio
Copy link
Member Author

Dirbaio commented Dec 11, 2021

Now moved here https://github.com/embassy-rs/embassy/blob/master/embassy-net/src/tcp_socket.rs

@cameronelliott
Copy link

This example link shouldn't break unless they re-write history:

https://github.com/embassy-rs/embassy/blob/79cbad501c6e6166d0e53ab33b2401a674ee9f31/embassy-net/src/tcp_socket.rs#L23

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants