Support green threads
Created by: zonyitoo
Rust does not support green threads officially, and the std::io
and std::net
are built for blocking I/O. But green threads should be an implementation detail, so it is possible to just switch the libstd
(let rustc
link another libstd
) and make it uses green threads under the hood. Then that Rust application will run with green threads without modifying anything.
Green threads are very helpful to build I/O bound applications, such as servers. Although you can always build an application by writing a state machine, or using callback style APIs. But the first method is hard to manage and it is not an easy job to do it well, and the second one will cause callback hell, your code will be teared apart into small pieces.
Green threads will not help to boost performance, but it is very helpful to build an I/O bound application. The simplest server model is one thread per connection. But because of using native threads are too heavy, we cannot do that in almost all realistic use cases, and green threads could help to solve this problem instead.
Things need to be done in the kernel:
- Non-blocking I/O, including async file I/O and non-blocking socket I/O. It would be better if they have unified APIs.
- I/O multiplexer (Eventloop). Something like Epoll, Kqueue or IOCP.
Things need to be done in the user-space:
- Basic supports of green threads, including context-switch, channels (lock-free queues), Mutex, Condvar,
thread_local
(green thread local) and the others. - Green threads' non-preemptive scheduler. This scheduler should be able to use multiple CPU cores with M:N model.
- A special version of
libstd
, which will make all I/O calls to be handled by the green thread library.thread::spawn
will spawn a green thread instead of a native thread.