Skip to content

Commit b1c7a46

Browse files
committed
Turn TimedOut into a BarrierWaitResult style opaque type
1 parent 46156de commit b1c7a46

File tree

2 files changed

+25
-38
lines changed

2 files changed

+25
-38
lines changed

src/libstd/sync/condvar.rs

Lines changed: 24 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,12 @@ use time::Duration;
2222
/// due to a time out or not.
2323
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
2424
#[unstable(feature = "wait_timeout", reason = "newly added")]
25-
pub enum TimedOut {
26-
/// The wait timed out.
27-
Yes,
28-
/// The wait did not time out.
29-
No
30-
}
25+
pub struct WaitTimeoutResult(bool);
3126

32-
impl TimedOut {
33-
/// Returns `true` iff the value of `self` is `Yes`.
27+
impl WaitTimeoutResult {
28+
/// Returns whether the wait was known to have timed out.
3429
pub fn timed_out(&self) -> bool {
35-
*self == TimedOut::Yes
30+
self.0
3631
}
3732
}
3833

@@ -188,16 +183,16 @@ impl Condvar {
188183
/// preemption or platform differences that may not cause the maximum
189184
/// amount of time waited to be precisely `dur`.
190185
///
191-
/// The returned `TimedOut` value indicates if the timeout is known to have
192-
/// elapsed.
186+
/// The returned `WaitTimeoutResult` value indicates if the timeout is
187+
/// known to have elapsed.
193188
///
194189
/// Like `wait`, the lock specified will be re-acquired when this function
195190
/// returns, regardless of whether the timeout elapsed or not.
196191
#[unstable(feature = "wait_timeout", reason = "waiting for Duration",
197192
issue = "27772")]
198193
pub fn wait_timeout<'a, T>(&self, guard: MutexGuard<'a, T>,
199194
dur: Duration)
200-
-> LockResult<(MutexGuard<'a, T>, TimedOut)> {
195+
-> LockResult<(MutexGuard<'a, T>, WaitTimeoutResult)> {
201196
unsafe {
202197
let me: &'static Condvar = &*(self as *const _);
203198
me.inner.wait_timeout(guard, dur)
@@ -217,7 +212,7 @@ impl Condvar {
217212
guard: MutexGuard<'a, T>,
218213
dur: Duration,
219214
f: F)
220-
-> LockResult<(MutexGuard<'a, T>, TimedOut)>
215+
-> LockResult<(MutexGuard<'a, T>, WaitTimeoutResult)>
221216
where F: FnMut(LockResult<&mut T>) -> bool {
222217
unsafe {
223218
let me: &'static Condvar = &*(self as *const _);
@@ -297,10 +292,10 @@ impl StaticCondvar {
297292
pub fn wait_timeout_ms<'a, T>(&'static self, guard: MutexGuard<'a, T>, ms: u32)
298293
-> LockResult<(MutexGuard<'a, T>, bool)> {
299294
match self.wait_timeout(guard, Duration::from_millis(ms as u64)) {
300-
Ok((guard, timed_out)) => Ok((guard, timed_out == TimedOut::No)),
295+
Ok((guard, timed_out)) => Ok((guard, !timed_out.timed_out())),
301296
Err(poison) => {
302297
let (guard, timed_out) = poison.into_inner();
303-
Err(PoisonError::new((guard, timed_out == TimedOut::No)))
298+
Err(PoisonError::new((guard, !timed_out.timed_out())))
304299
}
305300
}
306301
}
@@ -315,21 +310,17 @@ impl StaticCondvar {
315310
pub fn wait_timeout<'a, T>(&'static self,
316311
guard: MutexGuard<'a, T>,
317312
timeout: Duration)
318-
-> LockResult<(MutexGuard<'a, T>, TimedOut)> {
319-
let (poisoned, success) = unsafe {
313+
-> LockResult<(MutexGuard<'a, T>, WaitTimeoutResult)> {
314+
let (poisoned, result) = unsafe {
320315
let lock = mutex::guard_lock(&guard);
321316
self.verify(lock);
322-
let success = if self.inner.wait_timeout(lock, timeout) {
323-
TimedOut::No
324-
} else {
325-
TimedOut::Yes
326-
};
327-
(mutex::guard_poison(&guard).get(), success)
317+
let success = self.inner.wait_timeout(lock, timeout);
318+
(mutex::guard_poison(&guard).get(), WaitTimeoutResult(!success))
328319
};
329320
if poisoned {
330-
Err(PoisonError::new((guard, success)))
321+
Err(PoisonError::new((guard, result)))
331322
} else {
332-
Ok((guard, success))
323+
Ok((guard, result))
333324
}
334325
}
335326

@@ -347,7 +338,7 @@ impl StaticCondvar {
347338
guard: MutexGuard<'a, T>,
348339
dur: Duration,
349340
mut f: F)
350-
-> LockResult<(MutexGuard<'a, T>, TimedOut)>
341+
-> LockResult<(MutexGuard<'a, T>, WaitTimeoutResult)>
351342
where F: FnMut(LockResult<&mut T>) -> bool {
352343
// This could be made more efficient by pushing the implementation into
353344
// sys::condvar
@@ -361,7 +352,7 @@ impl StaticCondvar {
361352
let consumed = &now - &start;
362353
let guard = guard_result.unwrap_or_else(|e| e.into_inner());
363354
let (new_guard_result, timed_out) = if consumed > dur {
364-
(Ok(guard), TimedOut::Yes)
355+
(Ok(guard), WaitTimeoutResult(true))
365356
} else {
366357
match self.wait_timeout(guard, dur - consumed) {
367358
Ok((new_guard, timed_out)) => (Ok(new_guard), timed_out),
@@ -372,21 +363,17 @@ impl StaticCondvar {
372363
}
373364
};
374365
guard_result = new_guard_result;
375-
if timed_out == TimedOut::Yes {
366+
if timed_out.timed_out() {
376367
let result = f(guard_result
377368
.as_mut()
378369
.map(|g| &mut **g)
379370
.map_err(|e| PoisonError::new(&mut **e.get_mut())));
380-
let result = if result {
381-
TimedOut::No
382-
} else {
383-
TimedOut::Yes
384-
};
371+
let result = WaitTimeoutResult(!result);
385372
return poison::map_result(guard_result, |g| (g, result));
386373
}
387374
}
388375

389-
poison::map_result(guard_result, |g| (g, TimedOut::No))
376+
poison::map_result(guard_result, |g| (g, WaitTimeoutResult(false)))
390377
}
391378

392379
/// Wakes up one blocked thread on this condvar.
@@ -443,7 +430,7 @@ mod tests {
443430

444431
use super::StaticCondvar;
445432
use sync::mpsc::channel;
446-
use sync::{StaticMutex, Condvar, Mutex, Arc, TimedOut};
433+
use sync::{StaticMutex, Condvar, Mutex, Arc};
447434
use sync::atomic::{AtomicUsize, Ordering};
448435
use thread;
449436
use time::Duration;
@@ -544,7 +531,7 @@ mod tests {
544531
let (g, timed_out) = C.wait_timeout_with(g, Duration::new(0, 1000), |_| {
545532
false
546533
}).unwrap();
547-
assert_eq!(timed_out, TimedOut::Yes);
534+
assert!(timed_out.timed_out());
548535

549536
let (tx, rx) = channel();
550537
let _t = thread::spawn(move || {
@@ -577,7 +564,7 @@ mod tests {
577564
_ => true,
578565
}
579566
}).unwrap();
580-
assert_eq!(timed_out, TimedOut::No);
567+
assert!(!timed_out.timed_out());
581568
}
582569

583570
#[test]

src/libstd/sync/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub use alloc::arc::{Arc, Weak};
2121
pub use core::atomic;
2222

2323
pub use self::barrier::{Barrier, BarrierWaitResult};
24-
pub use self::condvar::{Condvar, StaticCondvar, TimedOut, CONDVAR_INIT};
24+
pub use self::condvar::{Condvar, StaticCondvar, WaitTimeoutResult, CONDVAR_INIT};
2525
pub use self::mutex::MUTEX_INIT;
2626
pub use self::mutex::{Mutex, MutexGuard, StaticMutex};
2727
pub use self::once::{Once, ONCE_INIT};

0 commit comments

Comments
 (0)