|
5 | 5 | // http://opensource.org/licenses/MIT>, at your option. This file may not be |
6 | 6 | // copied, modified, or distributed except according to those terms. |
7 | 7 |
|
8 | | -#[cfg(unix)] |
9 | | -use libc; |
10 | 8 | 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; |
15 | 10 |
|
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 | | -} |
55 | 11 |
|
56 | 12 | // Wastes some CPU time for the given number of iterations, |
57 | 13 | // using a hint to indicate to the CPU that we are spinning. |
@@ -98,7 +54,7 @@ impl SpinWait { |
98 | 54 | if self.counter <= 3 { |
99 | 55 | cpu_relax(1 << self.counter); |
100 | 56 | } else { |
101 | | - thread_yield(); |
| 57 | + thread_parker::thread_yield(); |
102 | 58 | } |
103 | 59 | true |
104 | 60 | } |
|
0 commit comments