Introduction to stackless coroutines – Coroutines and async/await

Now that you’ve gotten a brief introduction to Rust’s async model, it’s time to take a look at how this fits in the context of everything else we’ve covered in this book so far.

Rust’s futures are an example of an asynchronous model based on stackless coroutines, and in this chapter, we’ll take a look at what that really means and how it differs from stackful coroutines (fibers/green threads).

We’ll center everything around an example based on a simplified model of futures and async/await and see how we can use that to create suspendable and resumable tasks just like we did when creating our own fibers.

The good news is that this is a lot easier than implementing our own fibers/green threads since we can stay in Rust, which is safer. The flip side is that it’s a little more abstract and ties into programming language theory as much as it does computer science.

In this chapter, we’ll cover the following:

  • Introduction to stackless coroutines
  • An example of hand-written coroutines
  • async/await

Technical requirements

The examples in this chapter will all be cross-platform, so the only thing you need is Rust installed and the repository that belongs to the book downloaded locally. All the code in this chapter will be found in the ch07 folder.

We’ll use delayserver in this example as well, so you need to open a terminal, enter the delayserver folder at the root of the repository, and write cargo run so it’s ready and available for the examples going forward.

Remember to change the ports in the code if you for some reason have to change what port delayserver listens on.

Introduction to stackless coroutines

So, we’ve finally arrived at the point where we introduce the last method of modeling asynchronous operations in this book. You probably remember that we gave a high-level overview of stackful and stackless coroutines in Chapter 2. In Chapter 5, we implemented an example of stackful coroutines when writing our own fibers/green threads, so now it’s time to take a closer look at how stackless coroutines are implemented and used.

A stackless coroutine is a way of representing a task that can be interrupted and resumed. If you remember all the way back in Chapter 1, we mentioned that if we want tasks to run concurrently (be in progress at the same time) but not necessarily in parallel, we need to be able to pause and resume the task.

In its simplest form, a coroutine is just a task that can stop and resume by yielding control to either its caller, another coroutine, or a scheduler.

Many languages will have a coroutine implementation that also provides a runtime that handles scheduling and non-blocking I/O for you, but it’s helpful to make a distinction between what a coroutine is and the rest of the machinery involved in creating an asynchronous system.

This is especially true in Rust, since Rust doesn’t come with a runtime and only provides the infrastructure you need to create coroutines that have native support in the language. Rust makes sure that everyone programming in Rust uses the same abstraction for tasks that can be paused and resumed, but it leaves all the other details of getting an asynchronous system up and running for the programmer.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post