News for hackers. Stuff that matters.
Re:Comparison of Rust async and Linux thread context switch ... by josephg on Friday February 12, 2021 @02:13AM (#26111368)
I’d happily take an async-by-default world over a world where some APIs only exist through blocking calls. A classically threaded program can easily block on a future, but wrapping a blocking call in an otherwise asynchronous program is complicated, expensive and error prone work.
Re:Comparison of Rust async and Linux thread context switch ... by Ciantic on Friday February 12, 2021 @04:46AM (#26112010)
Btw, have you read this: https://async.rs/blog/stop-worrying-about-blocking-the-new-a... async-std allows to run blocking calls without hoops rather efficiently:

   async fn read_to_string(path: impl AsRef<Path>) -> io::Result<String> {
       std::fs::read_to_string(path)
   }
It doesn't have await inside! My mind was blown as I saw that.
Re:Comparison of Rust async and Linux thread context switch ... by Freaky on Friday February 12, 2021 @10:28AM (#26114660)
Read the first sentence.

> This blog post describes a proposed scheduler for async-std that did not end up being merged for several reasons.

I don't think it's a particularly good idea in the first place - it's basically an automatic watchdog-driven block_in_place(). It doesn't remove the problem of blocking in futures, it just limits the damage to the local task rather than blocking the entire executor.

That's fine in the simple case of future-per-task, but it's pretty common to be polling multiple futures concurrently within one, so it's not a general solution.

Re:Comparison of Rust async and Linux thread context switch ... by masklinn on Friday February 12, 2021 @05:09AM (#26112097)
> should a task execute for too long, the runtime will automatically react by spawning a new executor thread taking over the current thread’s work.

That is a super interesting strategy, though obviously only works when you can « afford » a multithreaded scheduler.

Anyway I wonder how they manage this, signals?

Re:Comparison of Rust async and Linux thread context switch ... by Freaky on Friday February 12, 2021 @10:50AM (#26114941)
Nothing so fancy.

Each worker thread runs in a loop executing a queue of jobs. On every iteration it sets an atomic progress flag to true.

The runtime in which it's contained polls its workers every 1-10ms, atomically swapping in false and checking to see if the previous value was also false - if so, it steals its task queue and spins up another worker to execute it.

https://github.com/async-rs/async-std/blob/ceba324bef9641d61...

Re:Comparison of Rust async and Linux thread context switch ... by josephg on Friday February 12, 2021 @07:49AM (#26113068)
> though obviously only works when you can « afford » a multithreaded scheduler.

Yeah, for example in comparison actix-web only uses single threaded workers - one per core. Future in actix-web doesn’t have to be Send or Sync, and I think it’s incompatible with what async-std is doing here. That design is almost certainly one of the reasons actix-web tops phoronix

Re:Comparison of Rust async and Linux thread context switch ... by raggi on Friday February 12, 2021 @05:24AM (#26112163)
It also really doesn't scale. It'll do fine on your average <10 core laptop, but once you get on a multi-package system you're going to find you're constantly thrashing memory because it is making disruptive scheduling decisions and your pooled tasks have poor context locality.
Re:Comparison of Rust async and Linux thread context switch ... by masklinn on Friday February 12, 2021 @04:55AM (#26112034)
> A classically threaded program can easily block on a future, but wrapping a blocking call in an otherwise asynchronous program is complicated, expensive and error prone work.

It’s really not though, at least as long as the parameters and results are Send. For instance Tokio has a spawn_blocking which runs the function on one of the blocking threads it spawns on-demand specifically for that use.

Meanwhile « blocking on a future » requires adding and managing an entire async runtime and its interactions with the rest of the program, and locking up the runtime is a very real possibility.

Re:Comparison of Rust async and Linux thread context switch ... by josephg on Friday February 12, 2021 @07:52AM (#26113088)
I understand the desire to stave off dependencies but managing an async runtime should only be a simple function call or two. How do you end up locking up the runtime with something like that?
Re:Comparison of Rust async and Linux thread context switch ... by throwaway81523 on Friday February 12, 2021 @10:31PM (#26121159)
These days it is possible to eliminate almost all blocking calls in Linux apps. File opening was a long persisting one but io_uring fixes that. Async sockets and file i/o to already-open fd's have been around forever.