Chapter 8. Operating System Primitives
So far, we’ve mostly focused on non-blocking operations. If we want to implement something like a mutex or condition variable, something that can wait for another thread to unlock or notify it, we need a way to efficiently block the current thread.
As we saw in Chapter 4, we can do this ourselves without the help of the operating system by spinning, repeatedly trying something over and over again, which can easily waste a lot of processor time. If we want to block efficiently, however, we need the help of the operating system’s kernel.
The kernel, or more specifically the scheduler part of it, is responsible for deciding which process or thread gets to run when, for how long, and on which processor core. While a thread is waiting for something to happen, the kernel can stop giving it any processor time, prioritizing other threads that can make better use of this scarce resource.
We’ll need a way to inform the kernel that we’re waiting for something and ask it to put our thread to sleep until something relevant happens.
Interfacing with the Kernel
The way things are communicated with the kernel depends heavily on the
operating system, and often even its version.
Usually, the details of how this works are hidden behind one or more libraries
that handle this for us. For example, using the Rust standard library,
we can just call File::open()
to open a file, without having to know any details about the operating system’s kernel interface. ...
Get Rust Atomics and Locks now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.