-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add barging MCS lock * Add lock_api feature
- Loading branch information
1 parent
81c4e1b
commit bc62bbe
Showing
18 changed files
with
1,608 additions
and
208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,22 +6,31 @@ spin-lock for mutual exclusion, referred to as MCS lock. | |
name = "mcslock" | ||
version = "0.1.0" | ||
edition = "2021" | ||
rust-version = "1.60.0" | ||
# NOTE: Rust 1.65 is required for GATs and let-else statements. | ||
rust-version = "1.65.0" | ||
license = "MIT OR Apache-2.0" | ||
readme = "README.md" | ||
# documentation = "https://docs.rs/mcslock" | ||
# homepage = "https://crates.io/mcslock" | ||
repository = "https://github.com/pedromfedricci/mcslock" | ||
authors = ["Pedro de Matos Fedricci <[email protected]>"] | ||
categories = ["no-std", "no-std::no-alloc", "concurrency"] | ||
categories = ["no-std", "concurrency"] | ||
keywords = ["no_std", "mutex", "spin-lock", "mcs-lock"] | ||
|
||
[workspace] | ||
members = [".", "benches"] | ||
|
||
[features] | ||
# NOTE: Features `yield` and `thread_local` require std. | ||
yield = [] | ||
thread_local = [] | ||
# NOTE: The `dep:` syntax requires Rust 1.60. | ||
lock_api = ["dep:lock_api"] | ||
|
||
[dependencies.lock_api] | ||
version = "0.4" | ||
default-features = false | ||
optional = true | ||
|
||
[target.'cfg(loom)'.dev-dependencies] | ||
loom = { version = "0.7" } | ||
|
@@ -30,6 +39,14 @@ loom = { version = "0.7" } | |
all-features = true | ||
rustdoc-args = ["--cfg", "docsrs"] | ||
|
||
[[test]] | ||
name = "lock_api" | ||
required-features = ["lock_api", "yield"] | ||
|
||
[[example]] | ||
name = "thread_local" | ||
required-features = ["thread_local"] | ||
|
||
[[example]] | ||
name = "lock_api" | ||
required-features = ["lock_api"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use std::sync::mpsc::channel; | ||
use std::sync::Arc; | ||
use std::thread; | ||
|
||
use mcslock::barging::spins::Mutex; | ||
|
||
fn main() { | ||
const N: usize = 10; | ||
|
||
// Spawn a few threads to increment a shared variable (non-atomically), and | ||
// let the main thread know once all increments are done. | ||
// | ||
// Here we're using an Arc to share memory among threads, and the data inside | ||
// the Arc is protected with a mutex. | ||
let data = Arc::new(Mutex::new(0)); | ||
|
||
let (tx, rx) = channel(); | ||
for _ in 0..N { | ||
let (data, tx) = (data.clone(), tx.clone()); | ||
thread::spawn(move || { | ||
// The shared state can only be accessed once the lock is held. | ||
// Our non-atomic increment is safe because we're the only thread | ||
// which can access the shared state when the lock is held. | ||
// | ||
// We unwrap() the return value to assert that we are not expecting | ||
// threads to ever fail while holding the lock. | ||
let mut data = data.lock(); | ||
*data += 1; | ||
if *data == N { | ||
tx.send(()).unwrap(); | ||
} | ||
// the lock is unlocked here when `data` goes out of scope. | ||
}); | ||
} | ||
let _message = rx.recv(); | ||
|
||
let count = data.lock(); | ||
assert_eq!(*count, N); | ||
// lock is unlock here when `count` goes out of scope. | ||
} |
Oops, something went wrong.