Skip to content

Commit 14fb02d

Browse files
committed
Move thread parker to a separate module.
1 parent 4e8a8b4 commit 14fb02d

File tree

2 files changed

+142
-112
lines changed

2 files changed

+142
-112
lines changed

library/std/src/thread/mod.rs

Lines changed: 17 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@
148148
#[cfg(all(test, not(target_os = "emscripten")))]
149149
mod tests;
150150

151+
mod parker;
152+
151153
use crate::any::Any;
152154
use crate::cell::UnsafeCell;
153155
use crate::ffi::{CStr, CString};
@@ -158,15 +160,14 @@ use crate::num::NonZeroU64;
158160
use crate::panic;
159161
use crate::panicking;
160162
use crate::str;
161-
use crate::sync::atomic::AtomicUsize;
162-
use crate::sync::atomic::Ordering::SeqCst;
163-
use crate::sync::{Arc, Condvar, Mutex};
163+
use crate::sync::Arc;
164164
use crate::sys::thread as imp;
165165
use crate::sys_common::mutex;
166166
use crate::sys_common::thread;
167167
use crate::sys_common::thread_info;
168168
use crate::sys_common::{AsInner, IntoInner};
169169
use crate::time::Duration;
170+
use parker::Parker;
170171

171172
////////////////////////////////////////////////////////////////////////////////
172173
// Thread-local storage
@@ -655,6 +656,8 @@ pub fn current() -> Thread {
655656
///
656657
/// [`channel`]: crate::sync::mpsc
657658
/// [`join`]: JoinHandle::join
659+
/// [`Condvar`]: crate::sync::Condvar
660+
/// [`Mutex`]: crate::sync::Mutex
658661
#[stable(feature = "rust1", since = "1.0.0")]
659662
pub fn yield_now() {
660663
imp::Thread::yield_now()
@@ -700,6 +703,8 @@ pub fn yield_now() {
700703
/// panic!()
701704
/// }
702705
/// ```
706+
///
707+
/// [Mutex]: crate::sync::Mutex
703708
#[inline]
704709
#[stable(feature = "rust1", since = "1.0.0")]
705710
pub fn panicking() -> bool {
@@ -767,11 +772,6 @@ pub fn sleep(dur: Duration) {
767772
imp::Thread::sleep(dur)
768773
}
769774

770-
// constants for park/unpark
771-
const EMPTY: usize = 0;
772-
const PARKED: usize = 1;
773-
const NOTIFIED: usize = 2;
774-
775775
/// Blocks unless or until the current thread's token is made available.
776776
///
777777
/// A call to `park` does not guarantee that the thread will remain parked
@@ -858,45 +858,11 @@ const NOTIFIED: usize = 2;
858858
///
859859
/// [`unpark`]: Thread::unpark
860860
/// [`thread::park_timeout`]: park_timeout
861-
//
862-
// The implementation currently uses the trivial strategy of a Mutex+Condvar
863-
// with wakeup flag, which does not actually allow spurious wakeups. In the
864-
// future, this will be implemented in a more efficient way, perhaps along the lines of
865-
// http://cr.openjdk.java.net/~stefank/6989984.1/raw_files/new/src/os/linux/vm/os_linux.cpp
866-
// or futuxes, and in either case may allow spurious wakeups.
867861
#[stable(feature = "rust1", since = "1.0.0")]
868862
pub fn park() {
869-
let thread = current();
870-
871-
// If we were previously notified then we consume this notification and
872-
// return quickly.
873-
if thread.inner.state.compare_exchange(NOTIFIED, EMPTY, SeqCst, SeqCst).is_ok() {
874-
return;
875-
}
876-
877-
// Otherwise we need to coordinate going to sleep
878-
let mut m = thread.inner.lock.lock().unwrap();
879-
match thread.inner.state.compare_exchange(EMPTY, PARKED, SeqCst, SeqCst) {
880-
Ok(_) => {}
881-
Err(NOTIFIED) => {
882-
// We must read here, even though we know it will be `NOTIFIED`.
883-
// This is because `unpark` may have been called again since we read
884-
// `NOTIFIED` in the `compare_exchange` above. We must perform an
885-
// acquire operation that synchronizes with that `unpark` to observe
886-
// any writes it made before the call to unpark. To do that we must
887-
// read from the write it made to `state`.
888-
let old = thread.inner.state.swap(EMPTY, SeqCst);
889-
assert_eq!(old, NOTIFIED, "park state changed unexpectedly");
890-
return;
891-
} // should consume this notification, so prohibit spurious wakeups in next park.
892-
Err(_) => panic!("inconsistent park state"),
893-
}
894-
loop {
895-
m = thread.inner.cvar.wait(m).unwrap();
896-
match thread.inner.state.compare_exchange(NOTIFIED, EMPTY, SeqCst, SeqCst) {
897-
Ok(_) => return, // got a notification
898-
Err(_) => {} // spurious wakeup, go back to sleep
899-
}
863+
// SAFETY: park_timeout is called on the parker owned by this thread.
864+
unsafe {
865+
current().inner.parker.park();
900866
}
901867
}
902868

@@ -958,35 +924,9 @@ pub fn park_timeout_ms(ms: u32) {
958924
/// ```
959925
#[stable(feature = "park_timeout", since = "1.4.0")]
960926
pub fn park_timeout(dur: Duration) {
961-
let thread = current();
962-
963-
// Like `park` above we have a fast path for an already-notified thread, and
964-
// afterwards we start coordinating for a sleep.
965-
// return quickly.
966-
if thread.inner.state.compare_exchange(NOTIFIED, EMPTY, SeqCst, SeqCst).is_ok() {
967-
return;
968-
}
969-
let m = thread.inner.lock.lock().unwrap();
970-
match thread.inner.state.compare_exchange(EMPTY, PARKED, SeqCst, SeqCst) {
971-
Ok(_) => {}
972-
Err(NOTIFIED) => {
973-
// We must read again here, see `park`.
974-
let old = thread.inner.state.swap(EMPTY, SeqCst);
975-
assert_eq!(old, NOTIFIED, "park state changed unexpectedly");
976-
return;
977-
} // should consume this notification, so prohibit spurious wakeups in next park.
978-
Err(_) => panic!("inconsistent park_timeout state"),
979-
}
980-
981-
// Wait with a timeout, and if we spuriously wake up or otherwise wake up
982-
// from a notification we just want to unconditionally set the state back to
983-
// empty, either consuming a notification or un-flagging ourselves as
984-
// parked.
985-
let (_m, _result) = thread.inner.cvar.wait_timeout(m, dur).unwrap();
986-
match thread.inner.state.swap(EMPTY, SeqCst) {
987-
NOTIFIED => {} // got a notification, hurray!
988-
PARKED => {} // no notification, alas
989-
n => panic!("inconsistent park_timeout state: {}", n),
927+
// SAFETY: park_timeout is called on the parker owned by this thread.
928+
unsafe {
929+
current().inner.parker.park_timeout(dur);
990930
}
991931
}
992932

@@ -1065,11 +1005,7 @@ impl ThreadId {
10651005
struct Inner {
10661006
name: Option<CString>, // Guaranteed to be UTF-8
10671007
id: ThreadId,
1068-
1069-
// state for thread park/unpark
1070-
state: AtomicUsize,
1071-
lock: Mutex<()>,
1072-
cvar: Condvar,
1008+
parker: Parker,
10731009
}
10741010

10751011
#[derive(Clone)]
@@ -1103,13 +1039,7 @@ impl Thread {
11031039
let cname =
11041040
name.map(|n| CString::new(n).expect("thread name may not contain interior null bytes"));
11051041
Thread {
1106-
inner: Arc::new(Inner {
1107-
name: cname,
1108-
id: ThreadId::new(),
1109-
state: AtomicUsize::new(EMPTY),
1110-
lock: Mutex::new(()),
1111-
cvar: Condvar::new(),
1112-
}),
1042+
inner: Arc::new(Inner { name: cname, id: ThreadId::new(), parker: Parker::new() }),
11131043
}
11141044
}
11151045

@@ -1145,32 +1075,7 @@ impl Thread {
11451075
/// ```
11461076
#[stable(feature = "rust1", since = "1.0.0")]
11471077
pub fn unpark(&self) {
1148-
// To ensure the unparked thread will observe any writes we made
1149-
// before this call, we must perform a release operation that `park`
1150-
// can synchronize with. To do that we must write `NOTIFIED` even if
1151-
// `state` is already `NOTIFIED`. That is why this must be a swap
1152-
// rather than a compare-and-swap that returns if it reads `NOTIFIED`
1153-
// on failure.
1154-
match self.inner.state.swap(NOTIFIED, SeqCst) {
1155-
EMPTY => return, // no one was waiting
1156-
NOTIFIED => return, // already unparked
1157-
PARKED => {} // gotta go wake someone up
1158-
_ => panic!("inconsistent state in unpark"),
1159-
}
1160-
1161-
// There is a period between when the parked thread sets `state` to
1162-
// `PARKED` (or last checked `state` in the case of a spurious wake
1163-
// up) and when it actually waits on `cvar`. If we were to notify
1164-
// during this period it would be ignored and then when the parked
1165-
// thread went to sleep it would never wake up. Fortunately, it has
1166-
// `lock` locked at this stage so we can acquire `lock` to wait until
1167-
// it is ready to receive the notification.
1168-
//
1169-
// Releasing `lock` before the call to `notify_one` means that when the
1170-
// parked thread wakes it doesn't get woken only to have to wait for us
1171-
// to release `lock`.
1172-
drop(self.inner.lock.lock().unwrap());
1173-
self.inner.cvar.notify_one()
1078+
self.inner.parker.unpark();
11741079
}
11751080

11761081
/// Gets the thread's unique identifier.

library/std/src/thread/parker/mod.rs

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
//! Parker implementaiton based on a Mutex and Condvar.
2+
//!
3+
//! The implementation currently uses the trivial strategy of a Mutex+Condvar
4+
//! with wakeup flag, which does not actually allow spurious wakeups. In the
5+
//! future, this will be implemented in a more efficient way, perhaps along the lines of
6+
//! http://cr.openjdk.java.net/~stefank/6989984.1/raw_files/new/src/os/linux/vm/os_linux.cpp
7+
//! or futuxes, and in either case may allow spurious wakeups.
8+
9+
use crate::sync::atomic::AtomicUsize;
10+
use crate::sync::atomic::Ordering::SeqCst;
11+
use crate::sync::{Condvar, Mutex};
12+
use crate::time::Duration;
13+
14+
const EMPTY: usize = 0;
15+
const PARKED: usize = 1;
16+
const NOTIFIED: usize = 2;
17+
18+
pub struct Parker {
19+
state: AtomicUsize,
20+
lock: Mutex<()>,
21+
cvar: Condvar,
22+
}
23+
24+
impl Parker {
25+
pub fn new() -> Self {
26+
Parker { state: AtomicUsize::new(EMPTY), lock: Mutex::new(()), cvar: Condvar::new() }
27+
}
28+
29+
// This implementaiton doesn't require `unsafe`, but other implementations
30+
// may assume this is only called by the thread that owns the Parker.
31+
pub unsafe fn park(&self) {
32+
// If we were previously notified then we consume this notification and
33+
// return quickly.
34+
if self.state.compare_exchange(NOTIFIED, EMPTY, SeqCst, SeqCst).is_ok() {
35+
return;
36+
}
37+
38+
// Otherwise we need to coordinate going to sleep
39+
let mut m = self.lock.lock().unwrap();
40+
match self.state.compare_exchange(EMPTY, PARKED, SeqCst, SeqCst) {
41+
Ok(_) => {}
42+
Err(NOTIFIED) => {
43+
// We must read here, even though we know it will be `NOTIFIED`.
44+
// This is because `unpark` may have been called again since we read
45+
// `NOTIFIED` in the `compare_exchange` above. We must perform an
46+
// acquire operation that synchronizes with that `unpark` to observe
47+
// any writes it made before the call to unpark. To do that we must
48+
// read from the write it made to `state`.
49+
let old = self.state.swap(EMPTY, SeqCst);
50+
assert_eq!(old, NOTIFIED, "park state changed unexpectedly");
51+
return;
52+
} // should consume this notification, so prohibit spurious wakeups in next park.
53+
Err(_) => panic!("inconsistent park state"),
54+
}
55+
loop {
56+
m = self.cvar.wait(m).unwrap();
57+
match self.state.compare_exchange(NOTIFIED, EMPTY, SeqCst, SeqCst) {
58+
Ok(_) => return, // got a notification
59+
Err(_) => {} // spurious wakeup, go back to sleep
60+
}
61+
}
62+
}
63+
64+
// This implementaiton doesn't require `unsafe`, but other implementations
65+
// may assume this is only called by the thread that owns the Parker.
66+
pub unsafe fn park_timeout(&self, dur: Duration) {
67+
// Like `park` above we have a fast path for an already-notified thread, and
68+
// afterwards we start coordinating for a sleep.
69+
// return quickly.
70+
if self.state.compare_exchange(NOTIFIED, EMPTY, SeqCst, SeqCst).is_ok() {
71+
return;
72+
}
73+
let m = self.lock.lock().unwrap();
74+
match self.state.compare_exchange(EMPTY, PARKED, SeqCst, SeqCst) {
75+
Ok(_) => {}
76+
Err(NOTIFIED) => {
77+
// We must read again here, see `park`.
78+
let old = self.state.swap(EMPTY, SeqCst);
79+
assert_eq!(old, NOTIFIED, "park state changed unexpectedly");
80+
return;
81+
} // should consume this notification, so prohibit spurious wakeups in next park.
82+
Err(_) => panic!("inconsistent park_timeout state"),
83+
}
84+
85+
// Wait with a timeout, and if we spuriously wake up or otherwise wake up
86+
// from a notification we just want to unconditionally set the state back to
87+
// empty, either consuming a notification or un-flagging ourselves as
88+
// parked.
89+
let (_m, _result) = self.cvar.wait_timeout(m, dur).unwrap();
90+
match self.state.swap(EMPTY, SeqCst) {
91+
NOTIFIED => {} // got a notification, hurray!
92+
PARKED => {} // no notification, alas
93+
n => panic!("inconsistent park_timeout state: {}", n),
94+
}
95+
}
96+
97+
pub fn unpark(&self) {
98+
// To ensure the unparked thread will observe any writes we made
99+
// before this call, we must perform a release operation that `park`
100+
// can synchronize with. To do that we must write `NOTIFIED` even if
101+
// `state` is already `NOTIFIED`. That is why this must be a swap
102+
// rather than a compare-and-swap that returns if it reads `NOTIFIED`
103+
// on failure.
104+
match self.state.swap(NOTIFIED, SeqCst) {
105+
EMPTY => return, // no one was waiting
106+
NOTIFIED => return, // already unparked
107+
PARKED => {} // gotta go wake someone up
108+
_ => panic!("inconsistent state in unpark"),
109+
}
110+
111+
// There is a period between when the parked thread sets `state` to
112+
// `PARKED` (or last checked `state` in the case of a spurious wake
113+
// up) and when it actually waits on `cvar`. If we were to notify
114+
// during this period it would be ignored and then when the parked
115+
// thread went to sleep it would never wake up. Fortunately, it has
116+
// `lock` locked at this stage so we can acquire `lock` to wait until
117+
// it is ready to receive the notification.
118+
//
119+
// Releasing `lock` before the call to `notify_one` means that when the
120+
// parked thread wakes it doesn't get woken only to have to wait for us
121+
// to release `lock`.
122+
drop(self.lock.lock().unwrap());
123+
self.cvar.notify_one()
124+
}
125+
}

0 commit comments

Comments
 (0)