Skip to content

Commit f4f136e

Browse files
author
Arno Haase
committed
newly phrased documentation for spin loop hints
1 parent 51c49e2 commit f4f136e

File tree

2 files changed

+20
-29
lines changed

2 files changed

+20
-29
lines changed

src/libcore/hint.rs

Lines changed: 5 additions & 17 deletions
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 or core and where the
63-
/// waiting times are relatively small. Because entering busy-wait spin-loop does not trigger the
64-
/// system's scheduler, no overhead for switching threads occurs. However, if the thread holding the
65-
/// contended lock is running on the same CPU or core, the spin-loop is likely to occupy an entire
66-
/// CPU slice before switching to the thread that holds the lock. If the contending lock is held by
67-
/// a thread on the same CPU or core or if the waiting times for acquiring the lock are longer, it
68-
/// is often better to 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

Lines changed: 15 additions & 12 deletions
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 or core and where the
138-
/// waiting times are relatively small. Because entering busy-wait spin-loop does not trigger the
139-
/// system's scheduler, no overhead for switching threads occurs. However, if the thread holding the
140-
/// contended lock is running on the same CPU or core, the spin-loop is likely to occupy an entire
141-
/// CPU slice before switching to the thread that holds the lock. If the contending lock is held by
142-
/// a thread on the same CPU or core or if the waiting times for acquiring the lock are longer, it
143-
/// is often better to 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/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)