Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Concurrency fixes #2118

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 71 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ members = [
"src/borrowing",
"src/control-flow-basics",
"src/error-handling",
"src/concurrency",
"src/concurrency/sync-exercises",
"src/concurrency/async-exercises",
"src/concurrency/async-exercises/chat-async",
"src/generics",
"src/iterators",
Expand All @@ -23,6 +25,9 @@ members = [
"src/std-traits",
"src/std-types",
"src/testing",
"src/concurrency/async",
"src/concurrency/async-control-flow",
"src/concurrency/async-pitfalls",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this fixes #567. Thanks!!

"src/tuples-and-arrays",
"src/types-and-values",
"src/unsafe-rust",
Expand Down
6 changes: 4 additions & 2 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
- [Exercise: Builder Type](memory-management/exercise.md)
- [Solution](memory-management/solution.md)
- [Smart Pointers](smart-pointers.md)
- [`Box<T>`](smart-pointers/box.md)
- [`Box`](smart-pointers/box.md)
- [`Rc`](smart-pointers/rc.md)
- [Owned Trait Objects](smart-pointers/trait-objects.md)
- [Exercise: Binary Tree](smart-pointers/exercise.md)
Expand Down Expand Up @@ -305,6 +305,7 @@
---

- [Welcome](bare-metal.md)
- [Setup](bare-metal/setup.md)
- [`no_std`](bare-metal/no_std.md)
- [A Minimal Example](bare-metal/minimal.md)
- [`alloc`](bare-metal/alloc.md)
Expand Down Expand Up @@ -356,6 +357,7 @@
---

- [Welcome](concurrency/welcome.md)
- [Fearless Concurrency](concurrency/fearless.md)
- [Threads](concurrency/threads.md)
- [Plain Threads](concurrency/threads/plain.md)
- [Scoped Threads](concurrency/threads/scoped.md)
Expand Down Expand Up @@ -385,7 +387,7 @@
- [Futures](concurrency/async/futures.md)
- [Runtimes](concurrency/async/runtimes.md)
- [Tokio](concurrency/async/runtimes/tokio.md)
- [Tasks](concurrency/async/tasks.md)
<!-- - [Tasks](concurrency/async/tasks.md) -->
- [Channels and Control Flow](concurrency/async-control-flow.md)
- [Async Channels](concurrency/async-control-flow/channels.md)
- [Join](concurrency/async-control-flow/join.md)
Expand Down
16 changes: 13 additions & 3 deletions src/android.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,19 @@ session: Android

# Welcome to Rust in Android

Rust is supported for system software on Android. This means that you can write
new services, libraries, drivers or even firmware in Rust (or improve existing
code as needed).
This is a one-day course about Rust in Android: you can write new Android
platform services, libraries, drivers or even firmware in Rust.

## Target Audience

This course builds on [Rust Fundamentals](welcome-day-1.md) and we expect you
are familiar with the basics of Rust. You should also be familiar with
development on the Android Platform (AOSP).

## Class Format

The class is meant to be very interactive! Please ask questions to drive the
exploration of Rust!

> We will attempt to call Rust from one of your own projects today. So try to
> find a little corner of your code base where we can move some lines of code to
Expand Down
54 changes: 10 additions & 44 deletions src/bare-metal.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,57 +5,23 @@ session: Morning

# Welcome to Bare Metal Rust

This is a standalone one-day course about bare-metal Rust, aimed at people who
are familiar with the basics of Rust (perhaps from completing the Comprehensive
Rust course), and ideally also have some experience with bare-metal programming
in some other language such as C.
This is a one-day course about bare-metal Rust: running Rust code without an OS
underneath us.

Today we will talk about 'bare-metal' Rust: running Rust code without an OS
underneath us. This will be divided into several parts:
The class is divided into several parts:

- What is `no_std` Rust?
- Writing firmware for microcontrollers.
- Writing bootloader / kernel code for application processors.
- Some useful crates for bare-metal Rust development.

For the microcontroller part of the course we will use the
[BBC micro:bit](https://microbit.org/) v2 as an example. It's a
[development board](https://tech.microbit.org/hardware/) based on the Nordic
nRF52833 microcontroller with some LEDs and buttons, an I2C-connected
accelerometer and compass, and an on-board SWD debugger.
## Target Audience

To get started, install some tools we'll need later. On gLinux or Debian:
This course builds on [Rust Fundamentals](welcome-day-1.md) and we expect you
are familiar with the basics of Rust. You should ideally also have some
experience with bare-metal programming in some other language such as C.

<!-- mdbook-xgettext: skip -->
## Class Format

```bash
sudo apt install gcc-aarch64-linux-gnu gdb-multiarch libudev-dev picocom pkg-config qemu-system-arm
rustup update
rustup target add aarch64-unknown-none thumbv7em-none-eabihf
rustup component add llvm-tools-preview
cargo install cargo-binutils cargo-embed
```

And give users in the `plugdev` group access to the micro:bit programmer:

<!-- mdbook-xgettext: skip -->

```bash
echo 'SUBSYSTEM=="usb", ATTR{idVendor}=="0d28", MODE="0664", GROUP="plugdev"' |\
sudo tee /etc/udev/rules.d/50-microbit.rules
sudo udevadm control --reload-rules
```

On MacOS:

<!-- mdbook-xgettext: skip -->

```bash
xcode-select --install
brew install gdb picocom qemu
brew install --cask gcc-aarch64-embedded
rustup update
rustup target add aarch64-unknown-none thumbv7em-none-eabihf
rustup component add llvm-tools-preview
cargo install cargo-binutils cargo-embed
```
The class is meant to be very interactive! Please ask questions to drive the
exploration of Rust!
49 changes: 49 additions & 0 deletions src/bare-metal/setup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Setup

For the microcontroller part of the course we will use the
[BBC micro:bit](https://microbit.org/) v2 as an example. It's a
[development board](https://tech.microbit.org/hardware/) based on the Nordic
nRF52833 microcontroller with some LEDs and buttons, an I2C-connected
accelerometer and compass, and an on-board SWD debugger.

To get started, install some tools we'll need later.

## Linux

Please run the following on gLinux or Debian:

<!-- mdbook-xgettext: skip -->

```bash
sudo apt install gcc-aarch64-linux-gnu gdb-multiarch libudev-dev picocom pkg-config qemu-system-arm
rustup update
rustup target add aarch64-unknown-none thumbv7em-none-eabihf
rustup component add llvm-tools-preview
cargo install cargo-binutils cargo-embed
```

And give users in the `plugdev` group access to the micro:bit programmer:

<!-- mdbook-xgettext: skip -->

```bash
echo 'SUBSYSTEM=="usb", ATTR{idVendor}=="0d28", MODE="0664", GROUP="plugdev"' |\
sudo tee /etc/udev/rules.d/50-microbit.rules
sudo udevadm control --reload-rules
```

## MacOS

Please run the following on MacOS:

<!-- mdbook-xgettext: skip -->

```bash
xcode-select --install
brew install gdb picocom qemu
brew install --cask gcc-aarch64-embedded
rustup update
rustup target add aarch64-unknown-none thumbv7em-none-eabihf
rustup component add llvm-tools-preview
cargo install cargo-binutils cargo-embed
```
11 changes: 8 additions & 3 deletions src/borrowing/interior-mutability.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ while still ensuring safety, typically by performing a runtime check.

## `RefCell`

A [`RefCell<T>`] gives you mutable access to a value behind a shared reference:

```rust,editable
use std::cell::RefCell;

Expand All @@ -36,9 +38,9 @@ fn main() {

## `Cell`

`Cell` wraps a value and allows getting or setting the value, even with a shared
reference to the `Cell`. However, it does not allow any references to the value.
Since there are no references, borrowing rules cannot be broken.
[`Cell<T>`] wraps a `T` value. It allows getting or setting the value, even with
a shared reference to the `Cell`. However, it does not allow any references to
the value. Since there are no references, the borrowing rules cannot be broken.

```rust,editable
use std::cell::Cell;
Expand Down Expand Up @@ -72,3 +74,6 @@ that safety, and `RefCell` and `Cell` are two of them.
have its own cost.

</details>

[`Cell<T>`]: https://doc.rust-lang.org/std/cell/struct.Cell.html
[`RefCell<T>`]: https://doc.rust-lang.org/std/cell/struct.RefCell.html
11 changes: 11 additions & 0 deletions src/chromium.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,14 @@ code to connect between Rust and existing Chromium C++ code.
> a corner of the code where you're displaying a UTF8 string to the user, feel
> free to follow this recipe in your part of the codebase instead of the exact
> part we talk about.

## Target Audience

This course builds on [Rust Fundamentals](welcome-day-1.md) and we expect you
are familiar with the basics of Rust. You should also be familiar with Chromium
development.

## Class Format

The class is meant to be very interactive! Please ask questions to drive the
exploration of Rust!
24 changes: 24 additions & 0 deletions src/concurrency/async-control-flow/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "async-control-flow"
version = "0.1.0"
edition = "2021"
publish = false

[[bin]]
name = "channels"
path = "channels.rs"

[[bin]]
name = "join"
path = "join.rs"

[[bin]]
name = "select"
path = "select.rs"

[dependencies]
anyhow = "1.0.81"
async-trait = "0.1.79"
futures = { version = "0.3.30", default-features = false }
reqwest = { version = "0.12.1", default-features = false }
tokio = { version = "1.36.0", features = ["full"] }
28 changes: 2 additions & 26 deletions src/concurrency/async-control-flow/channels.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,10 @@ minutes: 8

# Async Channels

Several crates have support for asynchronous channels. For instance `tokio`:
Asynchronous channels are very similar to synchronous channels:

```rust,editable,compile_fail
use tokio::sync::mpsc::{self, Receiver};

async fn ping_handler(mut input: Receiver<()>) {
let mut count: usize = 0;

while let Some(_) = input.recv().await {
count += 1;
println!("Received {count} pings so far.");
}

println!("ping_handler complete");
}

#[tokio::main]
async fn main() {
let (sender, receiver) = mpsc::channel(32);
let ping_handler_task = tokio::spawn(ping_handler(receiver));
for i in 0..10 {
sender.send(()).await.expect("Failed to send ping.");
println!("Sent {} pings so far.", i + 1);
}

drop(sender);
ping_handler_task.await.expect("Something went wrong in ping handler task.");
}
{{#include channels.rs}}
```

<details>
Expand Down
Loading