Skip to content

Commit 04369ad

Browse files
committed
define MutEx and spinlock
1 parent 99414d8 commit 04369ad

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/concurrency.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,13 @@ access to memory.
250250

251251
### Atomics
252252

253-
Accessing atomics stored in `static` variables is memory safe. If you are
253+
Accessing [atomic types] stored in `static` variables is memory safe. If you are
254254
building abstractions like channels on top of them (which likely will require
255255
`unsafe` code to access some shared buffer) make sure you use the right
256256
`Ordering` or your abstraction will be unsound.
257257

258+
[atomic types]: https://doc.rust-lang.org/core/sync/atomic/index.html
259+
258260
Here's an example of using a static variable for synchronization (a delay in
259261
this case).
260262

@@ -480,6 +482,13 @@ bound is required for this pattern.
480482
We can abstract the "disable all interrupts" critical section pattern into a
481483
`Mutex` type.
482484

485+
> Aside: "MutEx" stands for Mutual Exclusion and it's a synchronization
486+
> mechanism that ensures that execution contexts (threads or interrupt handlers)
487+
> get access to a single memory location in a "mutually exclusive" fashion.
488+
> Meaning that at any point in time at most one execution context gets exclusive
489+
> access over the memory location; only the execution context with exclusive
490+
> access can read / write to said memory location.
491+
483492
``` rust
484493
{{#include ../ci/concurrency/examples/mutex.rs}}```
485494

@@ -696,6 +705,16 @@ instructions are inserted where appropriate. If you are using the correct
696705
sections based on atomics, AKA spinlocks, are memory safe to use on multi-core
697706
devices though they can deadlock.
698707

708+
> Aside: an spinlock is a Mutual Exclusion mechanism that uses an atomic
709+
> variable to synchronize access to a memory location. While an execution
710+
> context has exclusive access over the memory location, any other execution
711+
> context that attempts to access the memory location will continuously check
712+
> the state of the atomic variable in a loop ("spin") until it indicates that
713+
> the shared memory location is free to access. It's probably easiest to look at
714+
> the [implementation] of a spinlock to understand how it works.
715+
716+
[implementation]: https://docs.rs/spin/0.5.0/src/spin/mutex.rs.html#129-164
717+
699718
``` rust
700719
// spin = "0.5.0"
701720
use spin::Mutex;

0 commit comments

Comments
 (0)