File tree Expand file tree Collapse file tree 3 files changed +27
-9
lines changed Expand file tree Collapse file tree 3 files changed +27
-9
lines changed Original file line number Diff line number Diff line change @@ -49,3 +49,26 @@ use intrinsics;
4949pub unsafe fn unreachable_unchecked ( ) -> ! {
5050 intrinsics:: unreachable ( )
5151}
52+
53+ /// Save power or switch hyperthreads in a busy-wait spin-loop.
54+ ///
55+ /// This function is deliberately more primitive than
56+ /// [`std::thread::yield_now`](../../std/thread/fn.yield_now.html) and
57+ /// does not directly yield to the system's scheduler.
58+ /// In some cases it might be useful to use a combination of both functions.
59+ /// Careful benchmarking is advised.
60+ ///
61+ /// On some platforms this function may not do anything at all.
62+ #[ inline]
63+ #[ unstable( feature = "renamed_spin_loop" , issue = "55002" ) ]
64+ pub fn spin_loop ( ) {
65+ #[ cfg( any( target_arch = "x86" , target_arch = "x86_64" ) ) ]
66+ unsafe {
67+ asm ! ( "pause" :: : "memory" : "volatile" ) ;
68+ }
69+
70+ #[ cfg( target_arch = "aarch64" ) ]
71+ unsafe {
72+ asm ! ( "yield" :: : "memory" : "volatile" ) ;
73+ }
74+ }
Original file line number Diff line number Diff line change @@ -84,6 +84,8 @@ use intrinsics;
8484use cell:: UnsafeCell ;
8585use fmt;
8686
87+ use hint:: spin_loop;
88+
8789/// Save power or switch hyperthreads in a busy-wait spin-loop.
8890///
8991/// This function is deliberately more primitive than
@@ -96,15 +98,7 @@ use fmt;
9698#[ inline]
9799#[ stable( feature = "spin_loop_hint" , since = "1.24.0" ) ]
98100pub fn spin_loop_hint ( ) {
99- #[ cfg( any( target_arch = "x86" , target_arch = "x86_64" ) ) ]
100- unsafe {
101- asm ! ( "pause" :: : "memory" : "volatile" ) ;
102- }
103-
104- #[ cfg( target_arch = "aarch64" ) ]
105- unsafe {
106- asm ! ( "yield" :: : "memory" : "volatile" ) ;
107- }
101+ spin_loop ( )
108102}
109103
110104/// A boolean type which can be safely shared between threads.
Original file line number Diff line number Diff line change 286286#![ feature( staged_api) ]
287287#![ feature( stmt_expr_attributes) ]
288288#![ feature( str_internals) ]
289+ #![ feature( renamed_spin_loop) ]
289290#![ feature( rustc_private) ]
290291#![ feature( thread_local) ]
291292#![ feature( toowned_clone_into) ]
You can’t perform that action at this time.
0 commit comments