Skip to content

Commit 96652a8

Browse files
committed
Move parking_lot specific thread_yield into sys
1 parent dc6caac commit 96652a8

File tree

4 files changed

+23
-46
lines changed

4 files changed

+23
-46
lines changed

src/libstd/sys/unix/thread_parker/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,8 @@ pub use self::futex::*;
2424
mod pthread;
2525
#[cfg(not(target_os = "linux"))]
2626
pub use self::pthread::*;
27+
28+
#[inline]
29+
pub fn thread_yield() {
30+
::thread::yield_now();
31+
}

src/libstd/sys/windows/thread_parker/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,14 @@ impl UnparkHandle {
145145
}
146146
}
147147
}
148+
149+
// Yields the rest of the current timeslice to the OS
150+
#[inline]
151+
pub fn thread_yield() {
152+
unsafe {
153+
// We don't use SwitchToThread here because it doesn't consider all
154+
// threads in the system and the thread we are waiting for may not get
155+
// selected.
156+
Sleep(0);
157+
}
158+
}

src/libstd/sys_common/parking_lot_core/spinwait.rs

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,53 +5,9 @@
55
// http://opensource.org/licenses/MIT>, at your option. This file may not be
66
// copied, modified, or distributed except according to those terms.
77

8-
#[cfg(unix)]
9-
use libc;
108
use sync::atomic::spin_loop_hint;
11-
#[cfg(not(any(windows, unix)))]
12-
use thread;
13-
#[cfg(windows)]
14-
use sys::c;
9+
use sys_common::thread_parker;
1510

16-
// Yields the rest of the current timeslice to the OS
17-
#[cfg(windows)]
18-
#[inline]
19-
fn thread_yield() {
20-
// Note that this is manually defined here rather than using the definition
21-
// through `winapi`. The `winapi` definition comes from the `synchapi`
22-
// header which enables the "synchronization.lib" library. It turns out,
23-
// however that `Sleep` comes from `kernel32.dll` so this activation isn't
24-
// necessary.
25-
//
26-
// This was originally identified in rust-lang/rust where on MinGW the
27-
// libsynchronization.a library pulls in a dependency on a newer DLL not
28-
// present in older versions of Windows. (see rust-lang/rust#49438)
29-
//
30-
// This is a bit of a hack for now and ideally we'd fix MinGW's own import
31-
// libraries, but that'll probably take a lot longer than patching this here
32-
// and avoiding the `synchapi` feature entirely.
33-
extern "system" {
34-
fn Sleep(a: c::DWORD);
35-
}
36-
unsafe {
37-
// We don't use SwitchToThread here because it doesn't consider all
38-
// threads in the system and the thread we are waiting for may not get
39-
// selected.
40-
Sleep(0);
41-
}
42-
}
43-
#[cfg(unix)]
44-
#[inline]
45-
fn thread_yield() {
46-
unsafe {
47-
libc::sched_yield();
48-
}
49-
}
50-
#[cfg(not(any(windows, unix)))]
51-
#[inline]
52-
fn thread_yield() {
53-
thread::yield_now();
54-
}
5511

5612
// Wastes some CPU time for the given number of iterations,
5713
// using a hint to indicate to the CPU that we are spinning.
@@ -98,7 +54,7 @@ impl SpinWait {
9854
if self.counter <= 3 {
9955
cpu_relax(1 << self.counter);
10056
} else {
101-
thread_yield();
57+
thread_parker::thread_yield();
10258
}
10359
true
10460
}

src/libstd/sys_common/thread_parker.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,8 @@ impl UnparkHandle {
7878
// released to avoid blocking the queue for too long.
7979
pub fn unpark(self) {}
8080
}
81+
82+
#[inline]
83+
pub fn thread_yield() {
84+
thread::yield_now();
85+
}

0 commit comments

Comments
 (0)