Rust Atomics and Locks
by Mara Bos
Copyright 2023 Mara Bos. All rights reserved.
Printed in the United States of America.
Published by OReilly Media, Inc. , 1005 Gravenstein Highway North, Sebastopol, CA 95472.
OReilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (https://oreilly.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com .
- Acquisitions Editor: Brian Guerin
- Development Editor: Shira Evans
- Production Editor: Elizabeth Faerm
- Copyeditor: Liz Wheeler
- Proofreader: Penelope Perkins
- Indexer: Ellen Troutman-Zaig
- Interior Designer: David Futato
- Cover Designer: Karen Montgomery
- Illustrator: Kate Dullea
- December 2022: First Edition
Revision History for the Early Release
- 2022-04-13: First Release
- 2022-06-23: Second Release
- 2022-08-09: Third Release
- 2022-10-20: Final Release
See https://oreilly.com/catalog/errata.csp?isbn=9781098119447 for release details.
The OReilly logo is a registered trademark of OReilly Media, Inc. Rust Atomics and Locks, the cover image, and related trade dress are trademarks of OReilly Media, Inc.
The views expressed in this work are those of the author, and do not represent the publishers views. While the publisher and the author have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights.
978-1-098-11938-6
[LSI]
And to my loved ones, too, of course.
Foreword
This book provides an excellent overview of low-level concurrency in the Rust language, including threads, locks, reference counts, atomics, mailboxes/channels, and much else besides. It digs into issues with CPUs and operating systems, the latter summarizing challenges inherent in making concurrent code work correctly on Linux, macOS, and Windows. I was particularly happy to see that Mara illustrates these topics with working Rust code. It wraps up by discussing semaphores, lock-free linked lists, queued locks, sequence locks, and even RCU.
So what does this book offer someone like myself, who has been slinging C code for almost 40 years, most recently in the nether depths of the Linux kernel?
I first learned of Rust from any number of enthusiasts and Linux-related conferences. Nevertheless, I was happily minding my own business until I was called out by name in a Rust-related LWN article, Using Rust for Kernel Development.Thus prodded, I wrote a blog series entitled So You Want to Rust the Linux Kernel?. This blog series sparked a number of spirited discussions, a few of which are visible in the seriess comments.
In one such discussion, a long-time Linux-kernel developer who has also written a lot of Rust code told me that when writing concurrent code in Rust, you should write it the way Rust wants you to. I have since learned that although this is great advice, it leaves open the question of exactly what Rust wants. This book gives excellent answers to this question, and is thus valuable both to Rust developers wishing to learn concurrency and to developers of concurrent code in other languages who would like to learn how best to do so in Rust.
I of course fall into this latter category. However, I must confess that many of the spirited discussions about Rust concurrency remind me of my parents and grandparents long-ago complaints about the inconvenient safety features that were being added to power tools such as saws and drills. Some of those safety features are now ubiquitous, but hammers, chisels, and chainsaws have not changed all that much. It was not at all easy to work out which mechanical safety features would stand the test of time, so I recommend approaching software safety features with an attitude of profound humility. And please understand that I am addressing the proponents of such features as well as their detractors.
Which brings us to another group of potential readers, the Rust skeptics. While I do believe that most Rust skeptics are doing the community a valuable service by pointing out opportunities for improvement, all but the most Rust-savvy of skeptics would benefit from reading this book. If nothing else, doing so would enable them to provide sharper and better-targeted criticisms.
Then there are those dyed-in-the-wool non-Rust developers who would prefer to implement Rusts concurrency-related safety mechanisms in their own favorite language. This book will give them a deeper understanding of the Rust mechanisms that they would like to replicate, or, better yet, improve upon.
Finally, any number of Linux-kernel developers are noting the progress that Rust is making toward being included in the Linux kernel. Next Steps for Rust in the Kernel. As of October 2022, this is still an experiment, but one that is being taken increasingly seriously. In fact, seriously enough that Linus Torvalds has accepted the first bits of Rust-language support into Version 6.1 of the Linux kernel.
Whether you are reading this book to expand your Rust repertoire to include concurrency, to expand your concurrency repertoire to include Rust, to improve your existing non-Rust environment, or just to look at concurrency from a different viewpoint, I wish you the very best on your journey!
Paul E. McKenney
Meta Platforms Kernel Team
Meta
October 2022
Preface
Rust has played, and keeps playing, a significant role in making systems programming more accessible.However, low-level concurrency topics such as atomics and memory orderingare still often thought of as somewhat mystical topics that are best left to avery small group of experts.
While working on Rust-based real-time control systems and the Rust standard library over the past few years,I found that many of the available resources on atomics and related topics onlycover a small part of the information I was looking for.Many resources focus entirely on C and C++,which can make it hard to form the connection withRusts concept of (memory and thread) safety and type system.The resources that cover the details of the abstract theory, like C++s memory model,often only vaguely explain how it relates to actual hardware, if at all.There are many resources that cover every detail of the actual hardware,such as processor instructions and cache coherency,but forming a holistic understanding often requires collecting bits and piecesof information from many different places.
This book is an attempt to put relevant information in one place, connecting it all together,providing everything you need to build your own correct, safe, and ergonomic concurrency primitives,while understanding enough about the underlying hardware and the role of the operating systemto be able to make design decisions and basic optimization trade-offs.