Skip to content

Commit 12bb5fd

Browse files
committed
Auto merge of #64348 - arnohaase:pr_documentation_spin_loop_hint, r=alexcrichton
PR: documentation spin loop hint The documentation for 'spin loop hint' explains that yield is better if the lock holder is running on the same CPU. I suggest that 'CPU or core' would be clearer.
2 parents dece573 + eb48b5d commit 12bb5fd

File tree

2 files changed

+20
-29
lines changed

2 files changed

+20
-29
lines changed

src/libcore/hint.rs

+5-17
Original file line numberDiff line numberDiff line change
@@ -49,28 +49,16 @@ pub unsafe fn unreachable_unchecked() -> ! {
4949
intrinsics::unreachable()
5050
}
5151

52-
/// Signals the processor that it is entering a busy-wait spin-loop.
52+
/// Emits a machine instruction hinting to the processor that it is running in busy-wait
53+
/// spin-loop ("spin lock").
5354
///
54-
/// Upon receiving spin-loop signal the processor can optimize its behavior by, for example, saving
55-
/// power or switching hyper-threads.
56-
///
57-
/// This function is different than [`std::thread::yield_now`] which directly yields to the
58-
/// system's scheduler, whereas `spin_loop` only signals the processor that it is entering a
59-
/// busy-wait spin-loop without yielding control to the system's scheduler.
60-
///
61-
/// Using a busy-wait spin-loop with `spin_loop` is ideally used in situations where a
62-
/// contended lock is held by another thread executed on a different CPU and where the waiting
63-
/// times are relatively small. Because entering busy-wait spin-loop does not trigger the system's
64-
/// scheduler, no overhead for switching threads occurs. However, if the thread holding the
65-
/// contended lock is running on the same CPU, the spin-loop is likely to occupy an entire CPU slice
66-
/// before switching to the thread that holds the lock. If the contending lock is held by a thread
67-
/// on the same CPU or if the waiting times for acquiring the lock are longer, it is often better to
68-
/// use [`std::thread::yield_now`].
55+
/// For a discussion of different locking strategies and their trade-offs, see
56+
/// [`core::sync::atomic::spin_loop_hint`].
6957
///
7058
/// **Note**: On platforms that do not support receiving spin-loop hints this function does not
7159
/// do anything at all.
7260
///
73-
/// [`std::thread::yield_now`]: ../../std/thread/fn.yield_now.html
61+
/// [`core::sync::atomic::spin_loop_hint`]: ../sync/atomic/fn.spin_loop_hint.html
7462
#[inline]
7563
#[unstable(feature = "renamed_spin_loop", issue = "55002")]
7664
pub fn spin_loop() {

src/libcore/sync/atomic.rs

+15-12
Original file line numberDiff line numberDiff line change
@@ -124,28 +124,31 @@ use crate::fmt;
124124

125125
use crate::hint::spin_loop;
126126

127-
/// Signals the processor that it is entering a busy-wait spin-loop.
127+
/// Signals the processor that it is inside a busy-wait spin-loop ("spin lock").
128128
///
129129
/// Upon receiving spin-loop signal the processor can optimize its behavior by, for example, saving
130130
/// power or switching hyper-threads.
131131
///
132-
/// This function is different than [`std::thread::yield_now`] which directly yields to the
133-
/// system's scheduler, whereas `spin_loop_hint` only signals the processor that it is entering a
134-
/// busy-wait spin-loop without yielding control to the system's scheduler.
132+
/// This function is different from [`std::thread::yield_now`] which directly yields to the
133+
/// system's scheduler, whereas `spin_loop_hint` does not interact with the operating system.
135134
///
136-
/// Using a busy-wait spin-loop with `spin_loop_hint` is ideally used in situations where a
137-
/// contended lock is held by another thread executed on a different CPU and where the waiting
138-
/// times are relatively small. Because entering busy-wait spin-loop does not trigger the system's
139-
/// scheduler, no overhead for switching threads occurs. However, if the thread holding the
140-
/// contended lock is running on the same CPU, the spin-loop is likely to occupy an entire CPU slice
141-
/// before switching to the thread that holds the lock. If the contending lock is held by a thread
142-
/// on the same CPU or if the waiting times for acquiring the lock are longer, it is often better to
143-
/// use [`std::thread::yield_now`].
135+
/// Spin locks can be very efficient for short lock durations because they do not involve context
136+
/// switches or interaction with the operating system. For long lock durations they become wasteful
137+
/// however because they use CPU cycles for the entire lock duration, and using a
138+
/// [`std::sync::Mutex`] is likely the better approach. If actively spinning for a long time is
139+
/// required, e.g. because code polls a non-blocking API, calling [`std::thread::yield_now`]
140+
/// or [`std::thread::sleep`] may be the best option.
141+
///
142+
/// **Note**: Spin locks are based on the underlying assumption that another thread will release
143+
/// the lock 'soon'. In order for this to work, that other thread must run on a different CPU or
144+
/// core (at least potentially). Spin locks do not work efficiently on single CPU / core platforms.
144145
///
145146
/// **Note**: On platforms that do not support receiving spin-loop hints this function does not
146147
/// do anything at all.
147148
///
148149
/// [`std::thread::yield_now`]: ../../../std/thread/fn.yield_now.html
150+
/// [`std::thread::sleep`]: ../../../std/thread/fn.sleep.html
151+
/// [`std::sync::Mutex`]: ../../../std/sync/struct.Mutex.html
149152
#[inline]
150153
#[stable(feature = "spin_loop_hint", since = "1.24.0")]
151154
pub fn spin_loop_hint() {

0 commit comments

Comments
 (0)