Green Threads Explained in 200 lines of Rust updated with the new asm! syntax ++++

277 points cfsamson

Over time, I've written a few mdbooks/gitbooks using Rust to explain interesting concepts or try to take a deep dive into some of Rust's inner workings. Common to all of them is that they relied a lot on unstable features, most notably the "original" asm! macro (later renamed to llvm_asm!).

Due to frequent changes to these features, several examples didn't compile or work as they did when the books were written, and it's been a problem for quite a long time.

I just wanted to let everyone know that the books have now been updated with the new asm! syntax, the dialect changed from AT&T to Intel (which is the new default) and all examples are now working in addition to other minor improvements.

Green Threads Explained in 200 Lines of Rust

Centers around a 200 line main example implementation of green threads (or stackful coroutines, goroutines, userland threads if you prefer) and focuses on:

  • How the stack is used in different operating systems
  • How a context switch (stack swap) is done
  • Includes a gentle introduction to Rust's new inline assembly syntax ++

Exploring Async Basics with Rust

Focuses around concurrency in general and the basics of asynchronous programming. Centers around a "toy" implementation of a node-like event system, which is a totally different way of handling concurrency compared to green threads:

  • What concurrency and asynchronous programming is about
  • Syscalls and communication with the operating system
  • How interrupts, firmware, the CPU and the operating system handle concurrency and how concurrency relates to I/O
  • Introduction to Epoll, Kqueue and IOCP

Futures Explained in 200 Lines of Rust

What you probably didn't need to know about Rusts futures in way too much detail. Centers around a 200 line example where we create a simple Reactor, Executor and Future. Contains a full context switching, green thread example in the runnable mdbook code snippets (which I find just cool...):

  • Alternative ways to handle concurrency in a programming language
  • A mental model of how Futures work
  • Generators and Pinning in detail
  • What a runtime is and why you need it

So I just wanted to share that with everyone 👍