Skip to content

Commit de36753

Browse files
committed
move WaitTimeoutResult up to mod.rs
Since `WaitTimeoutResult` is poison-agnostic, we want to use the same type for both variants of `Condvar`.
1 parent 80b9e54 commit de36753

File tree

5 files changed

+68
-137
lines changed

5 files changed

+68
-137
lines changed

library/std/src/sync/mod.rs

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ pub use self::poison::{LockResult, PoisonError};
209209
#[doc(inline)]
210210
pub use self::poison::{
211211
Mutex, MutexGuard, TryLockError, TryLockResult,
212-
Condvar, WaitTimeoutResult,
212+
Condvar,
213213
Once, OnceState,
214214
RwLock, RwLockReadGuard, RwLockWriteGuard,
215215
};
@@ -234,3 +234,66 @@ mod barrier;
234234
mod lazy_lock;
235235
mod once_lock;
236236
mod reentrant_lock;
237+
238+
/// A type indicating whether a timed wait on a condition variable returned
239+
/// due to a time out or not.
240+
///
241+
/// It is returned by the [`wait_timeout`] method.
242+
///
243+
/// [`wait_timeout`]: Condvar::wait_timeout
244+
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
245+
#[stable(feature = "wait_timeout", since = "1.5.0")]
246+
pub struct WaitTimeoutResult(bool);
247+
248+
impl WaitTimeoutResult {
249+
/// Returns `true` if the wait was known to have timed out.
250+
///
251+
/// # Examples
252+
///
253+
/// This example spawns a thread which will sleep 20 milliseconds before
254+
/// updating a boolean value and then notifying the condvar.
255+
///
256+
/// The main thread will wait with a 10 millisecond timeout on the condvar
257+
/// and will leave the loop upon timeout.
258+
///
259+
/// ```
260+
/// use std::sync::{Arc, Condvar, Mutex};
261+
/// use std::thread;
262+
/// use std::time::Duration;
263+
///
264+
/// let pair = Arc::new((Mutex::new(false), Condvar::new()));
265+
/// let pair2 = Arc::clone(&pair);
266+
///
267+
/// # let handle =
268+
/// thread::spawn(move || {
269+
/// let (lock, cvar) = &*pair2;
270+
///
271+
/// // Let's wait 20 milliseconds before notifying the condvar.
272+
/// thread::sleep(Duration::from_millis(20));
273+
///
274+
/// let mut started = lock.lock().unwrap();
275+
/// // We update the boolean value.
276+
/// *started = true;
277+
/// cvar.notify_one();
278+
/// });
279+
///
280+
/// // Wait for the thread to start up.
281+
/// let (lock, cvar) = &*pair;
282+
/// loop {
283+
/// // Let's put a timeout on the condvar's wait.
284+
/// let result = cvar.wait_timeout(lock.lock().unwrap(), Duration::from_millis(10)).unwrap();
285+
/// // 10 milliseconds have passed.
286+
/// if result.1.timed_out() {
287+
/// // timed out now and we can leave.
288+
/// break
289+
/// }
290+
/// }
291+
/// # // Prevent leaks for Miri.
292+
/// # let _ = handle.join();
293+
/// ```
294+
#[must_use]
295+
#[stable(feature = "wait_timeout", since = "1.5.0")]
296+
pub fn timed_out(&self) -> bool {
297+
self.0
298+
}
299+
}

library/std/src/sync/nonpoison.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl fmt::Display for WouldBlock {
3030
}
3131

3232
#[unstable(feature = "nonpoison_condvar", issue = "134645")]
33-
pub use self::condvar::{Condvar, WaitTimeoutResult};
33+
pub use self::condvar::Condvar;
3434
#[unstable(feature = "mapped_lock_guards", issue = "117108")]
3535
pub use self::mutex::MappedMutexGuard;
3636
#[unstable(feature = "nonpoison_mutex", issue = "134645")]

library/std/src/sync/nonpoison/condvar.rs

Lines changed: 1 addition & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,9 @@
11
use crate::fmt;
2+
use crate::sync::WaitTimeoutResult;
23
use crate::sync::nonpoison::{MutexGuard, mutex};
34
use crate::sys::sync as sys;
45
use crate::time::{Duration, Instant};
56

6-
/// A type indicating whether a timed wait on a condition variable returned
7-
/// due to a time out or not.
8-
///
9-
/// It is returned by the [`wait_timeout`] method.
10-
///
11-
/// [`wait_timeout`]: Condvar::wait_timeout
12-
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
13-
#[unstable(feature = "nonpoison_condvar", issue = "134645")]
14-
pub struct WaitTimeoutResult(bool);
15-
16-
// FIXME(nonpoison_condvar) this type is duplicated in `poison`. How do we share types that are
17-
// poison-agnostic?
18-
impl WaitTimeoutResult {
19-
/// Returns `true` if the wait was known to have timed out.
20-
///
21-
/// # Examples
22-
///
23-
/// This example spawns a thread which will sleep 20 milliseconds before
24-
/// updating a boolean value and then notifying the condvar.
25-
///
26-
/// The main thread will wait with a 10 millisecond timeout on the condvar
27-
/// and will leave the loop upon timeout.
28-
///
29-
/// ```
30-
/// #![feature(nonpoison_mutex)]
31-
/// #![feature(nonpoison_condvar)]
32-
///
33-
/// use std::sync::nonpoison::{Mutex, Condvar};
34-
/// use std::sync::Arc;
35-
/// use std::thread;
36-
/// use std::time::Duration;
37-
///
38-
/// let pair = Arc::new((Mutex::new(false), Condvar::new()));
39-
/// let pair2 = Arc::clone(&pair);
40-
///
41-
/// # let handle =
42-
/// thread::spawn(move || {
43-
/// let (lock, cvar) = &*pair2;
44-
///
45-
/// // Let's wait 20 milliseconds before notifying the condvar.
46-
/// thread::sleep(Duration::from_millis(20));
47-
///
48-
/// let mut started = lock.lock();
49-
/// // We update the boolean value.
50-
/// *started = true;
51-
/// cvar.notify_one();
52-
/// });
53-
///
54-
/// // Wait for the thread to start up.
55-
/// let (lock, cvar) = &*pair;
56-
/// loop {
57-
/// // Let's put a timeout on the condvar's wait.
58-
/// let result = cvar.wait_timeout(lock.lock(), Duration::from_millis(10));
59-
/// // 10 milliseconds have passed.
60-
/// if result.1.timed_out() {
61-
/// // timed out now and we can leave.
62-
/// break
63-
/// }
64-
/// }
65-
/// # // Prevent leaks for Miri.
66-
/// # let _ = handle.join();
67-
/// ```
68-
#[must_use]
69-
#[unstable(feature = "nonpoison_condvar", issue = "134645")]
70-
pub fn timed_out(&self) -> bool {
71-
self.0
72-
}
73-
}
74-
757
/// A Condition Variable
768
///
779
/// For more information about condition variables, check out the documentation for the poisoning

library/std/src/sync/poison.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
//! then the lock will not be poisoned.
6060
6161
#[stable(feature = "rust1", since = "1.0.0")]
62-
pub use self::condvar::{Condvar, WaitTimeoutResult};
62+
pub use self::condvar::Condvar;
6363
#[unstable(feature = "mapped_lock_guards", issue = "117108")]
6464
pub use self::mutex::MappedMutexGuard;
6565
#[stable(feature = "rust1", since = "1.0.0")]

library/std/src/sync/poison/condvar.rs

Lines changed: 1 addition & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,9 @@
11
use crate::fmt;
2+
use crate::sync::WaitTimeoutResult;
23
use crate::sync::poison::{self, LockResult, MutexGuard, PoisonError, mutex};
34
use crate::sys::sync as sys;
45
use crate::time::{Duration, Instant};
56

6-
/// A type indicating whether a timed wait on a condition variable returned
7-
/// due to a time out or not.
8-
///
9-
/// It is returned by the [`wait_timeout`] method.
10-
///
11-
/// [`wait_timeout`]: Condvar::wait_timeout
12-
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
13-
#[stable(feature = "wait_timeout", since = "1.5.0")]
14-
pub struct WaitTimeoutResult(bool);
15-
16-
// FIXME(nonpoison_condvar): `WaitTimeoutResult` is actually poisoning-agnostic, it seems.
17-
// Should we take advantage of this fact?
18-
impl WaitTimeoutResult {
19-
/// Returns `true` if the wait was known to have timed out.
20-
///
21-
/// # Examples
22-
///
23-
/// This example spawns a thread which will sleep 20 milliseconds before
24-
/// updating a boolean value and then notifying the condvar.
25-
///
26-
/// The main thread will wait with a 10 millisecond timeout on the condvar
27-
/// and will leave the loop upon timeout.
28-
///
29-
/// ```
30-
/// use std::sync::{Arc, Condvar, Mutex};
31-
/// use std::thread;
32-
/// use std::time::Duration;
33-
///
34-
/// let pair = Arc::new((Mutex::new(false), Condvar::new()));
35-
/// let pair2 = Arc::clone(&pair);
36-
///
37-
/// # let handle =
38-
/// thread::spawn(move || {
39-
/// let (lock, cvar) = &*pair2;
40-
///
41-
/// // Let's wait 20 milliseconds before notifying the condvar.
42-
/// thread::sleep(Duration::from_millis(20));
43-
///
44-
/// let mut started = lock.lock().unwrap();
45-
/// // We update the boolean value.
46-
/// *started = true;
47-
/// cvar.notify_one();
48-
/// });
49-
///
50-
/// // Wait for the thread to start up.
51-
/// let (lock, cvar) = &*pair;
52-
/// loop {
53-
/// // Let's put a timeout on the condvar's wait.
54-
/// let result = cvar.wait_timeout(lock.lock().unwrap(), Duration::from_millis(10)).unwrap();
55-
/// // 10 milliseconds have passed.
56-
/// if result.1.timed_out() {
57-
/// // timed out now and we can leave.
58-
/// break
59-
/// }
60-
/// }
61-
/// # // Prevent leaks for Miri.
62-
/// # let _ = handle.join();
63-
/// ```
64-
#[must_use]
65-
#[stable(feature = "wait_timeout", since = "1.5.0")]
66-
pub fn timed_out(&self) -> bool {
67-
self.0
68-
}
69-
}
70-
717
/// A Condition Variable
728
///
739
/// Condition variables represent the ability to block a thread such that it

0 commit comments

Comments
 (0)