diff options
Diffstat (limited to 'src/libstd/sync')
| -rw-r--r-- | src/libstd/sync/condvar.rs | 53 | ||||
| -rw-r--r-- | src/libstd/sync/mod.rs | 4 | ||||
| -rw-r--r-- | src/libstd/sync/semaphore.rs | 226 |
3 files changed, 4 insertions, 279 deletions
diff --git a/src/libstd/sync/condvar.rs b/src/libstd/sync/condvar.rs index 0ff3a690702..64468be396f 100644 --- a/src/libstd/sync/condvar.rs +++ b/src/libstd/sync/condvar.rs @@ -167,13 +167,12 @@ impl Condvar { /// returns, regardless of whether the timeout elapsed or not. #[stable(feature = "rust1", since = "1.0.0")] #[rustc_deprecated(since = "1.6.0", reason = "replaced by `std::sync::Condvar::wait_timeout`")] - #[allow(deprecated)] pub fn wait_timeout_ms<'a, T>(&self, guard: MutexGuard<'a, T>, ms: u32) -> LockResult<(MutexGuard<'a, T>, bool)> { - unsafe { - let me: &'static Condvar = &*(self as *const _); - me.inner.wait_timeout_ms(guard, ms) - } + let res = self.wait_timeout(guard, Duration::from_millis(ms as u64)); + poison::map_result(res, |(a, b)| { + (a, !b.timed_out()) + }) } /// Waits on this condition variable for a notification, timing out after a @@ -200,30 +199,6 @@ impl Condvar { } } - /// Waits on this condition variable for a notification, timing out after a - /// specified duration. - /// - /// The semantics of this function are equivalent to `wait_timeout` except - /// that the implementation will repeatedly wait while the duration has not - /// passed and the provided function returns `false`. - #[unstable(feature = "wait_timeout_with", - reason = "unsure if this API is broadly needed or what form it should take", - issue = "27748")] - #[rustc_deprecated(since = "1.8.0", - reason = "wonky signature and questionable \ - implementation didn't justify existence")] - pub fn wait_timeout_with<'a, T, F>(&self, - guard: MutexGuard<'a, T>, - dur: Duration, - f: F) - -> LockResult<(MutexGuard<'a, T>, WaitTimeoutResult)> - where F: FnMut(LockResult<&mut T>) -> bool { - unsafe { - let me: &'static Condvar = &*(self as *const _); - me.inner.wait_timeout_with(guard, dur, f) - } - } - /// Wakes up one blocked thread on this condvar. /// /// If there is a blocked thread on this condition variable, then it will @@ -293,26 +268,6 @@ impl StaticCondvar { #[unstable(feature = "static_condvar", reason = "may be merged with Condvar in the future", issue = "27717")] - #[rustc_deprecated(since = "1.6.0", - reason = "replaced by `std::sync::StaticCondvar::wait_timeout`")] - pub fn wait_timeout_ms<'a, T>(&'static self, guard: MutexGuard<'a, T>, ms: u32) - -> LockResult<(MutexGuard<'a, T>, bool)> { - match self.wait_timeout(guard, Duration::from_millis(ms as u64)) { - Ok((guard, timed_out)) => Ok((guard, !timed_out.timed_out())), - Err(poison) => { - let (guard, timed_out) = poison.into_inner(); - Err(PoisonError::new((guard, !timed_out.timed_out()))) - } - } - } - - /// Waits on this condition variable for a notification, timing out after a - /// specified duration. - /// - /// See `Condvar::wait_timeout`. - #[unstable(feature = "static_condvar", - reason = "may be merged with Condvar in the future", - issue = "27717")] pub fn wait_timeout<'a, T>(&'static self, guard: MutexGuard<'a, T>, timeout: Duration) diff --git a/src/libstd/sync/mod.rs b/src/libstd/sync/mod.rs index 1a42b091831..c20b422d40c 100644 --- a/src/libstd/sync/mod.rs +++ b/src/libstd/sync/mod.rs @@ -38,9 +38,6 @@ pub use sys_common::poison::{PoisonError, TryLockError, TryLockResult, LockResul pub use self::rwlock::{RwLockReadGuard, RwLockWriteGuard}; #[stable(feature = "rust1", since = "1.0.0")] pub use self::rwlock::{RwLock, StaticRwLock, RW_LOCK_INIT}; -#[stable(feature = "rust1", since = "1.0.0")] -#[allow(deprecated)] -pub use self::semaphore::{Semaphore, SemaphoreGuard}; pub mod mpsc; @@ -49,4 +46,3 @@ mod condvar; mod mutex; mod once; mod rwlock; -mod semaphore; diff --git a/src/libstd/sync/semaphore.rs b/src/libstd/sync/semaphore.rs deleted file mode 100644 index dd76444d3ae..00000000000 --- a/src/libstd/sync/semaphore.rs +++ /dev/null @@ -1,226 +0,0 @@ -// Copyright 2014 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or -// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license -// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -#![unstable(feature = "semaphore", - reason = "the interaction between semaphores and the acquisition/release \ - of resources is currently unclear", - issue = "27798")] -#![allow(deprecated)] - -use ops::Drop; -use sync::{Mutex, Condvar}; - -/// A counting, blocking, semaphore. -/// -/// Semaphores are a form of atomic counter where access is only granted if the -/// counter is a positive value. Each acquisition will block the calling thread -/// until the counter is positive, and each release will increment the counter -/// and unblock any threads if necessary. -/// -/// # Examples -/// -/// ``` -/// #![feature(semaphore)] -/// -/// use std::sync::Semaphore; -/// -/// // Create a semaphore that represents 5 resources -/// let sem = Semaphore::new(5); -/// -/// // Acquire one of the resources -/// sem.acquire(); -/// -/// // Acquire one of the resources for a limited period of time -/// { -/// let _guard = sem.access(); -/// // ... -/// } // resources is released here -/// -/// // Release our initially acquired resource -/// sem.release(); -/// ``` -#[rustc_deprecated(since = "1.7.0", - reason = "easily confused with system semaphores and not \ - used enough to pull its weight")] -#[unstable(feature = "semaphore", - reason = "the interaction between semaphores and the acquisition/release \ - of resources is currently unclear", - issue = "27798")] -pub struct Semaphore { - lock: Mutex<isize>, - cvar: Condvar, -} - -/// An RAII guard which will release a resource acquired from a semaphore when -/// dropped. -#[rustc_deprecated(since = "1.7.0", - reason = "easily confused with system semaphores and not \ - used enough to pull its weight")] -#[unstable(feature = "semaphore", - reason = "the interaction between semaphores and the acquisition/release \ - of resources is currently unclear", - issue = "27798")] -pub struct SemaphoreGuard<'a> { - sem: &'a Semaphore, -} - -#[rustc_deprecated(since = "1.7.0", - reason = "easily confused with system semaphores and not \ - used enough to pull its weight")] -#[unstable(feature = "semaphore", - reason = "the interaction between semaphores and the acquisition/release \ - of resources is currently unclear", - issue = "27798")] -impl Semaphore { - /// Creates a new semaphore with the initial count specified. - /// - /// The count specified can be thought of as a number of resources, and a - /// call to `acquire` or `access` will block until at least one resource is - /// available. It is valid to initialize a semaphore with a negative count. - pub fn new(count: isize) -> Semaphore { - Semaphore { - lock: Mutex::new(count), - cvar: Condvar::new(), - } - } - - /// Acquires a resource of this semaphore, blocking the current thread until - /// it can do so. - /// - /// This method will block until the internal count of the semaphore is at - /// least 1. - pub fn acquire(&self) { - let mut count = self.lock.lock().unwrap(); - while *count <= 0 { - count = self.cvar.wait(count).unwrap(); - } - *count -= 1; - } - - /// Release a resource from this semaphore. - /// - /// This will increment the number of resources in this semaphore by 1 and - /// will notify any pending waiters in `acquire` or `access` if necessary. - pub fn release(&self) { - *self.lock.lock().unwrap() += 1; - self.cvar.notify_one(); - } - - /// Acquires a resource of this semaphore, returning an RAII guard to - /// release the semaphore when dropped. - /// - /// This function is semantically equivalent to an `acquire` followed by a - /// `release` when the guard returned is dropped. - pub fn access(&self) -> SemaphoreGuard { - self.acquire(); - SemaphoreGuard { sem: self } - } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl<'a> Drop for SemaphoreGuard<'a> { - fn drop(&mut self) { - self.sem.release(); - } -} - -#[cfg(test)] -mod tests { - use prelude::v1::*; - - use sync::Arc; - use super::Semaphore; - use sync::mpsc::channel; - use thread; - - #[test] - fn test_sem_acquire_release() { - let s = Semaphore::new(1); - s.acquire(); - s.release(); - s.acquire(); - } - - #[test] - fn test_sem_basic() { - let s = Semaphore::new(1); - let _g = s.access(); - } - - #[test] - fn test_sem_as_mutex() { - let s = Arc::new(Semaphore::new(1)); - let s2 = s.clone(); - let _t = thread::spawn(move|| { - let _g = s2.access(); - }); - let _g = s.access(); - } - - #[test] - fn test_sem_as_cvar() { - /* Child waits and parent signals */ - let (tx, rx) = channel(); - let s = Arc::new(Semaphore::new(0)); - let s2 = s.clone(); - let _t = thread::spawn(move|| { - s2.acquire(); - tx.send(()).unwrap(); - }); - s.release(); - let _ = rx.recv(); - - /* Parent waits and child signals */ - let (tx, rx) = channel(); - let s = Arc::new(Semaphore::new(0)); - let s2 = s.clone(); - let _t = thread::spawn(move|| { - s2.release(); - let _ = rx.recv(); - }); - s.acquire(); - tx.send(()).unwrap(); - } - - #[test] - fn test_sem_multi_resource() { - // Parent and child both get in the critical section at the same - // time, and shake hands. - let s = Arc::new(Semaphore::new(2)); - let s2 = s.clone(); - let (tx1, rx1) = channel(); - let (tx2, rx2) = channel(); - let _t = thread::spawn(move|| { - let _g = s2.access(); - let _ = rx2.recv(); - tx1.send(()).unwrap(); - }); - let _g = s.access(); - tx2.send(()).unwrap(); - rx1.recv().unwrap(); - } - - #[test] - fn test_sem_runtime_friendly_blocking() { - let s = Arc::new(Semaphore::new(1)); - let s2 = s.clone(); - let (tx, rx) = channel(); - { - let _g = s.access(); - thread::spawn(move|| { - tx.send(()).unwrap(); - drop(s2.access()); - tx.send(()).unwrap(); - }); - rx.recv().unwrap(); // wait for child to come alive - } - rx.recv().unwrap(); // wait for child to be done - } -} |
