Skip to content

Commit ce9b96b

Browse files
doc: minor changes to code examples
1 parent 8cbdcc5 commit ce9b96b

File tree

4 files changed

+36
-38
lines changed

4 files changed

+36
-38
lines changed

README.md

+15-16
Original file line numberDiff line numberDiff line change
@@ -77,26 +77,24 @@ module for more information.
7777
use std::sync::Arc;
7878
use std::thread;
7979

80-
// `spins::Mutex` simply spins during contention.
80+
// Simply spins during contention.
8181
use mcslock::raw::{spins::Mutex, MutexNode};
8282

8383
fn main() {
8484
let mutex = Arc::new(Mutex::new(0));
8585
let c_mutex = Arc::clone(&mutex);
8686

8787
thread::spawn(move || {
88-
// A queue node must be mutably accessible.
88+
// A node may also be transparently allocated in the stack.
8989
// Critical section must be defined as a closure.
90-
let mut node = MutexNode::new();
91-
c_mutex.lock_with_then(&mut node, |data| {
92-
*data = 10;
93-
});
90+
c_mutex.try_lock_then(|data| *data.unwrap() = 10);
9491
})
9592
.join().expect("thread::spawn failed");
9693

97-
// A node is transparently allocated in the stack.
94+
// A queue node must be mutably accessible.
9895
// Critical section must be defined as a closure.
99-
assert_eq!(mutex.try_lock_then(|data| *data.unwrap()), 10);
96+
let mut node = MutexNode::new();
97+
assert_eq!(mutex.lock_with_then(&mut node, |data| *data), 10);
10098
}
10199
```
102100

@@ -112,7 +110,7 @@ the `thread_local` feature.
112110
use std::sync::Arc;
113111
use std::thread;
114112

115-
// `spins::Mutex` simply spins during contention.
113+
// Simply spins during contention.
116114
use mcslock::raw::spins::Mutex;
117115

118116
// Requires `thread_local` feature.
@@ -131,7 +129,7 @@ fn main() {
131129

132130
// Local node handles are provided by reference.
133131
// Critical section must be defined as a closure.
134-
assert_eq!(mutex.try_lock_with_local_then(&NODE, |data| *data.unwrap()), 10);
132+
assert_eq!(mutex.lock_with_local_then(&NODE, |data| *data), 10);
135133
}
136134
```
137135

@@ -148,19 +146,19 @@ use std::sync::Arc;
148146
use std::thread;
149147

150148
// Requires `barging` feature.
151-
// `spins::backoff::Mutex` spins with exponential backoff during contention.
149+
// Spins with exponential backoff during contention.
152150
use mcslock::barging::spins::backoff::Mutex;
153151

154152
fn main() {
155153
let mutex = Arc::new(Mutex::new(0));
156154
let c_mutex = Arc::clone(&mutex);
157155

158156
thread::spawn(move || {
159-
*c_mutex.lock() = 10;
157+
*c_mutex.try_lock().unwrap() = 10;
160158
})
161159
.join().expect("thread::spawn failed");
162160

163-
assert_eq!(*mutex.try_lock().unwrap(), 10);
161+
assert_eq!(*mutex.lock(), 10);
164162
}
165163
```
166164

@@ -177,7 +175,7 @@ use std::sync::Arc;
177175
use std::thread;
178176

179177
// Requires `parking` feature.
180-
// `spins::Mutex` spins for a while then parks during contention.
178+
// Spins for a while then parks during contention.
181179
use mcslock::parking::raw::{spins::Mutex, MutexNode};
182180

183181
// Requires `parking` and `thread_local` features.
@@ -194,9 +192,10 @@ fn main() {
194192
})
195193
.join().expect("thread::spawn failed");
196194

197-
// A node is transparently allocated in the stack.
195+
// A queue node must be mutably accessible.
198196
// Critical section must be defined as a closure.
199-
assert_eq!(mutex.try_lock_then(|data| *data.unwrap()), 10);
197+
let mut node = MutexNode::new();
198+
assert_eq!(mutex.lock_with_then(&mut node, |data| *data), 10);
200199
}
201200
```
202201

src/lib.rs

+15-16
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,23 @@
4343
//! use std::sync::Arc;
4444
//! use std::thread;
4545
//!
46-
//! // `spins::Mutex` simply spins during contention.
46+
//! // Simply spins during contention.
4747
//! use mcslock::raw::{spins::Mutex, MutexNode};
4848
//!
4949
//! let mutex = Arc::new(Mutex::new(0));
5050
//! let c_mutex = Arc::clone(&mutex);
5151
//!
5252
//! thread::spawn(move || {
53-
//! // A queue node must be mutably accessible.
53+
//! // A node may also be transparently allocated in the stack.
5454
//! // Critical section must be defined as a closure.
55-
//! let mut node = MutexNode::new();
56-
//! c_mutex.lock_with_then(&mut node, |data| {
57-
//! *data = 10;
58-
//! });
55+
//! c_mutex.try_lock_then(|data| *data.unwrap() = 10);
5956
//! })
6057
//! .join().expect("thread::spawn failed");
6158
//!
62-
//! // A node is transparently allocated in the stack.
59+
//! // A queue node must be mutably accessible.
6360
//! // Critical section must be defined as a closure.
64-
//! assert_eq!(mutex.try_lock_then(|data| *data.unwrap()), 10);
61+
//! let mut node = MutexNode::new();
62+
//! assert_eq!(mutex.lock_with_then(&mut node, |data| *data), 10);
6563
//! ```
6664
//!
6765
//! ## Thread local queue nodes
@@ -78,7 +76,7 @@
7876
//! use std::sync::Arc;
7977
//! use std::thread;
8078
//!
81-
//! // `spins::Mutex` simply spins during contention.
79+
//! // Simply spins during contention.
8280
//! use mcslock::raw::spins::Mutex;
8381
//!
8482
//! // Requires `thread_local` feature.
@@ -96,7 +94,7 @@
9694
//!
9795
//! // Local node handles are provided by reference.
9896
//! // Critical section must be defined as a closure.
99-
//! assert_eq!(mutex.try_lock_with_local_then(&NODE, |data| *data.unwrap()), 10);
97+
//! assert_eq!(mutex.lock_with_local_then(&NODE, |data| *data), 10);
10098
//! # }
10199
//! # #[cfg(not(feature = "thread_local"))]
102100
//! # fn main() {}
@@ -118,18 +116,18 @@
118116
//! use std::thread;
119117
//!
120118
//! // Requires `barging` feature.
121-
//! // `spins::backoff::Mutex` spins with exponential backoff during contention.
119+
//! // Spins with exponential backoff during contention.
122120
//! use mcslock::barging::spins::backoff::Mutex;
123121
//!
124122
//! let mutex = Arc::new(Mutex::new(0));
125123
//! let c_mutex = Arc::clone(&mutex);
126124
//!
127125
//! thread::spawn(move || {
128-
//! *c_mutex.lock() = 10;
126+
//! *c_mutex.try_lock().unwrap() = 10;
129127
//! })
130128
//! .join().expect("thread::spawn failed");
131129
//!
132-
//! assert_eq!(*mutex.try_lock().unwrap(), 10);
130+
//! assert_eq!(*mutex.lock(), 10);
133131
//! # }
134132
//! # #[cfg(not(feature = "barging"))]
135133
//! # fn main() {}
@@ -150,7 +148,7 @@
150148
//! use std::thread;
151149
//!
152150
//! // Requires `parking` feature.
153-
//! // `spins::Mutex` spins for a while then parks during contention.
151+
//! // Spins for a while then parks during contention.
154152
//! use mcslock::parking::raw::{spins::Mutex, MutexNode};
155153
//!
156154
//! // Requires `parking` and `thread_local` features.
@@ -166,9 +164,10 @@
166164
//! })
167165
//! .join().expect("thread::spawn failed");
168166
//!
169-
//! // A node is transparently allocated in the stack.
167+
//! // A queue node must be mutably accessible.
170168
//! // Critical section must be defined as a closure.
171-
//! assert_eq!(mutex.try_lock_then(|data| *data.unwrap()), 10);
169+
//! let mut node = MutexNode::new();
170+
//! assert_eq!(mutex.lock_with_then(&mut node, |data| *data), 10);
172171
//! # }
173172
//! # #[cfg(not(all(feature = "thread_local", feature = "parking")))]
174173
//! # fn main() {}

src/parking/raw/thread_local.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -371,9 +371,9 @@ impl<T: ?Sized, P: Park> Mutex<T, P> {
371371
/// let mutex = Mutex::new(0);
372372
///
373373
/// mutex.lock_with_local_then(&NODE, |_data| {
374-
/// // `NODE` is already mutably borrowed in this thread by the
375-
/// // enclosing `lock_with_local_then`, the borrow is live for the full
376-
/// // duration of this closure scope.
374+
/// // `NODE` is already mutably borrowed in this thread by the enclosing
375+
/// // `lock_with_local_then`, the borrow is live for the full duration
376+
/// // of this closure scope.
377377
/// let mutex = Mutex::new(());
378378
/// mutex.lock_with_local_then(&NODE, |_data| ());
379379
/// });

src/raw/thread_local.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,9 @@ impl<T: ?Sized, R: Relax> Mutex<T, R> {
198198
/// let mutex = Mutex::new(0);
199199
///
200200
/// mutex.lock_with_local_then(&NODE, |_data| {
201-
/// // `NODE` is already mutably borrowed in this thread by the
202-
/// // enclosing `lock_with_local_then`, the borrow is live for the full
203-
/// // duration of this closure scope.
201+
/// // `NODE` is already mutably borrowed in this thread by the enclosing
202+
/// // `lock_with_local_then`, the borrow is live for the full duration
203+
/// // of this closure scope.
204204
/// let mutex = Mutex::new(());
205205
/// mutex.try_lock_with_local_then(&NODE, |_data| ());
206206
/// });

0 commit comments

Comments
 (0)