Skip to content

Commit aadd41f

Browse files
chore: use thread local node in the parking example
1 parent 24e93a9 commit aadd41f

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ required-features = ["barging"]
5151

5252
[[example]]
5353
name = "parking"
54-
required-features = ["parking"]
54+
required-features = ["parking", "thread_local"]
5555

5656
[[example]]
5757
name = "thread_local"

examples/parking.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ use std::thread;
66
// `spins::Mutex` spins for a while then parks during contention.
77
use mcslock::parking::raw::{spins::Mutex, MutexNode};
88

9+
// Requires that the `thread_local` feature is enabled.
10+
mcslock::thread_local_parking_node! {
11+
// * Allows multiple static definitions, must be separated with semicolons.
12+
// * Visibility is optional (private by default).
13+
// * Requires `static` keyword and a UPPER_SNAKE_CASE name.
14+
pub static NODE;
15+
static UNUSED_NODE;
16+
}
17+
918
fn main() {
1019
const N: usize = 10;
1120

@@ -41,7 +50,7 @@ fn main() {
4150
}
4251
let _message = rx.recv();
4352

44-
// A queue node is transparently allocated in the stack.
45-
let count = data.lock_then(|data| *data);
53+
// A thread local node is borrowed.
54+
let count = data.lock_with_local_then(&NODE, |data| *data);
4655
assert_eq!(count, N);
4756
}

examples/thread_local.rs

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ fn main() {
3535
// threads to ever fail while holding the lock.
3636
//
3737
// Data is exclusively accessed by the guard argument.
38+
// A thread local node is borrowed.
3839
data.lock_with_local_then(&NODE, |data| {
3940
*data += 1;
4041
if *data == N {
@@ -46,6 +47,7 @@ fn main() {
4647
}
4748
let _message = rx.recv();
4849

50+
// A thread local node is borrowed.
4951
let count = data.lock_with_local_then(&NODE, |data| *data);
5052
assert_eq!(count, N);
5153
}

0 commit comments

Comments
 (0)