async-io icon indicating copy to clipboard operation
async-io copied to clipboard

Enable usage of timers on WASM

Open notgull opened this issue 3 years ago • 4 comments

WASM targets don't support Async and probably won't until WASI reaches a more stable point. However, since parking is usable on WASM thanks to atomics, it may be desirable to use the Timer implementation.

My suggestion is to add two new features: io and timers. They compose as such:

  • With both features enabled (they are by default), the crate functions as normal.
  • With io disabled but timers enabled, the Reactor retains its timer processing capabilities but instead of calling to polling, it calls parking (which works with WASM) with the timeout resulting from process_timer_ops.
  • With both features disabled, Reactor no longer exists and block_on is just an alias to the ones in futures_lite.

This would be a breaking change.

notgull avatar Aug 22 '22 02:08 notgull

I'm not sure if we need to introduce such a breaking change to support WASM. Currently, async-io fails to compile on WASM anyway, so I think just disabling some APIs in WASM is not a problem.

taiki-e avatar Sep 08 '22 02:09 taiki-e

That's probably the best way of going about this. I'll rearrange the PR I wrote to use OS flags instead of feature flags.

notgull avatar Sep 08 '22 04:09 notgull

A few points of note I discovered while implementing this:

  • Instant::now() is unimplemented for WASM, which means we'd have to use instant or something similar. However with the current API of Timer this means that instant would be in the public API, unless we made at or interval_at unavailable for WASM.
  • In order for the condvar waiting to work, the -Ctarget-feature=+atomics flag needs to be enabled. However, trying to rebuild libstd with that gives me an ICE. I'll investigate more before I file a Rust bug.

notgull avatar Dec 01 '22 04:12 notgull

Not relevant for now

notgull avatar Feb 04 '24 04:02 notgull