Skip to content

Commit

Permalink
Don't use NoSend/NoSync in libstd
Browse files Browse the repository at this point in the history
  • Loading branch information
flaper87 committed Jan 16, 2015
1 parent bb04121 commit c6ab9a6
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/libstd/lib.rs
Expand Up @@ -110,6 +110,7 @@
#![feature(slicing_syntax, unboxed_closures)]
#![feature(box_syntax)]
#![feature(old_impl_check)]
#![feature(optin_builtin_traits)]
#![allow(unknown_features)] #![feature(int_uint)]

// Don't link to std. We are std.
Expand Down
29 changes: 29 additions & 0 deletions src/libstd/sync/mpsc/blocking.rs
Expand Up @@ -14,6 +14,7 @@ use thread::Thread;
use sync::atomic::{AtomicBool, ATOMIC_BOOL_INIT, Ordering};
use sync::Arc;
use marker::{Sync, Send};
#[cfg(stage0)] // NOTE remove use after next snapshot
use marker::{NoSend, NoSync};
use mem;
use clone::Clone;
Expand All @@ -31,12 +32,25 @@ pub struct SignalToken {
inner: Arc<Inner>,
}

#[cfg(stage0)] // NOTE remove impl after next snapshot
pub struct WaitToken {
inner: Arc<Inner>,
no_send: NoSend,
no_sync: NoSync,
}

#[cfg(not(stage0))] // NOTE remove cfg after next snapshot
pub struct WaitToken {
inner: Arc<Inner>,
}

#[cfg(not(stage0))] // NOTE remove cfg after next snapshot
impl !Send for WaitToken {}

#[cfg(not(stage0))] // NOTE remove cfg after next snapshot
impl !Sync for WaitToken {}

#[cfg(stage0)] // NOTE remove impl after next snapshot
pub fn tokens() -> (WaitToken, SignalToken) {
let inner = Arc::new(Inner {
thread: Thread::current(),
Expand All @@ -53,6 +67,21 @@ pub fn tokens() -> (WaitToken, SignalToken) {
(wait_token, signal_token)
}

#[cfg(not(stage0))] // NOTE remove cfg after next snapshot
pub fn tokens() -> (WaitToken, SignalToken) {
let inner = Arc::new(Inner {
thread: Thread::current(),
woken: ATOMIC_BOOL_INIT,
});
let wait_token = WaitToken {
inner: inner.clone(),
};
let signal_token = SignalToken {
inner: inner
};
(wait_token, signal_token)
}

impl SignalToken {
pub fn signal(&self) -> bool {
let wake = !self.inner.woken.compare_and_swap(false, true, Ordering::SeqCst);
Expand Down
18 changes: 18 additions & 0 deletions src/libstd/sync/mpsc/mod.rs
Expand Up @@ -370,12 +370,24 @@ unsafe impl<T:Send> Send for Sender<T> { }
/// The sending-half of Rust's synchronous channel type. This half can only be
/// owned by one task, but it can be cloned to send to other tasks.
#[stable]
#[cfg(stage0)] // NOTE remove impl after next snapshot
pub struct SyncSender<T> {
inner: Arc<RacyCell<sync::Packet<T>>>,
// can't share in an arc
_marker: marker::NoSync,
}

/// The sending-half of Rust's synchronous channel type. This half can only be
/// owned by one task, but it can be cloned to send to other tasks.
#[stable]
#[cfg(not(stage0))] // NOTE remove cfg after next snapshot
pub struct SyncSender<T> {
inner: Arc<RacyCell<sync::Packet<T>>>,
}

#[cfg(not(stage0))] // NOTE remove cfg after next snapshot
impl<T> !marker::Sync for SyncSender<T> {}

/// An error returned from the `send` function on channels.
///
/// A `send` operation can only fail if the receiving end of a channel is
Expand Down Expand Up @@ -677,10 +689,16 @@ impl<T: Send> Drop for Sender<T> {
////////////////////////////////////////////////////////////////////////////////

impl<T: Send> SyncSender<T> {
#[cfg(stage0)] // NOTE remove impl after next snapshot
fn new(inner: Arc<RacyCell<sync::Packet<T>>>) -> SyncSender<T> {
SyncSender { inner: inner, _marker: marker::NoSync }
}

#[cfg(not(stage0))] // NOTE remove cfg after next snapshot
fn new(inner: Arc<RacyCell<sync::Packet<T>>>) -> SyncSender<T> {
SyncSender { inner: inner }
}

/// Sends a value on this synchronous channel.
///
/// This function will *block* until space in the internal buffer becomes
Expand Down
28 changes: 28 additions & 0 deletions src/libstd/sync/mpsc/select.rs
Expand Up @@ -66,13 +66,26 @@ use sync::mpsc::blocking::{self, SignalToken};

/// The "receiver set" of the select interface. This structure is used to manage
/// a set of receivers which are being selected over.
#[cfg(stage0)] // NOTE remove impl after next snapshot
pub struct Select {
head: *mut Handle<'static, ()>,
tail: *mut Handle<'static, ()>,
next_id: Cell<uint>,
marker1: marker::NoSend,
}

/// The "receiver set" of the select interface. This structure is used to manage
/// a set of receivers which are being selected over.
#[cfg(not(stage0))] // NOTE remove cfg after next snapshot
pub struct Select {
head: *mut Handle<'static, ()>,
tail: *mut Handle<'static, ()>,
next_id: Cell<uint>,
}

#[cfg(not(stage0))] // NOTE remove cfg after next snapshot
impl !marker::Send for Select {}

/// A handle to a receiver which is currently a member of a `Select` set of
/// receivers. This handle is used to keep the receiver in the set as well as
/// interact with the underlying receiver.
Expand Down Expand Up @@ -113,6 +126,7 @@ impl Select {
///
/// Usage of this struct directly can sometimes be burdensome, and usage is
/// rather much easier through the `select!` macro.
#[cfg(stage0)] // NOTE remove impl after next snapshot
pub fn new() -> Select {
Select {
marker1: marker::NoSend,
Expand All @@ -122,6 +136,20 @@ impl Select {
}
}

/// Creates a new selection structure. This set is initially empty and
/// `wait` will panic!() if called.
///
/// Usage of this struct directly can sometimes be burdensome, and usage is
/// rather much easier through the `select!` macro.
#[cfg(not(stage0))] // NOTE remove cfg after next snapshot
pub fn new() -> Select {
Select {
head: 0 as *mut Handle<'static, ()>,
tail: 0 as *mut Handle<'static, ()>,
next_id: Cell::new(1),
}
}

/// Creates a new handle into this receiver set for a new receiver. Note
/// that this does *not* add the receiver to the receiver set, for that you
/// must call the `add` method on the handle itself.
Expand Down
33 changes: 33 additions & 0 deletions src/libstd/sync/mutex.rs
Expand Up @@ -160,6 +160,7 @@ unsafe impl Sync for StaticMutex {}
/// Deref and DerefMut implementations
#[must_use]
#[stable]
#[cfg(stage0)] // NOTE remove impl after next snapshot
pub struct MutexGuard<'a, T: 'a> {
// funny underscores due to how Deref/DerefMut currently work (they
// disregard field privacy).
Expand All @@ -169,6 +170,25 @@ pub struct MutexGuard<'a, T: 'a> {
__marker: marker::NoSend,
}

/// An RAII implementation of a "scoped lock" of a mutex. When this structure is
/// dropped (falls out of scope), the lock will be unlocked.
///
/// The data protected by the mutex can be access through this guard via its
/// Deref and DerefMut implementations
#[must_use]
#[stable]
#[cfg(not(stage0))] // NOTE remove cfg after next snapshot
pub struct MutexGuard<'a, T: 'a> {
// funny underscores due to how Deref/DerefMut currently work (they
// disregard field privacy).
__lock: &'a StaticMutex,
__data: &'a UnsafeCell<T>,
__poison: poison::Guard,
}

#[cfg(not(stage0))] // NOTE remove cfg after next snapshot
impl<'a, T> !marker::Send for MutexGuard<'a, T> {}

/// Static initialization of a mutex. This constant can be used to initialize
/// other mutex constants.
#[unstable = "may be merged with Mutex in the future"]
Expand Down Expand Up @@ -279,6 +299,7 @@ impl StaticMutex {
}

impl<'mutex, T> MutexGuard<'mutex, T> {
#[cfg(stage0)] // NOTE remove afte next snapshot
fn new(lock: &'mutex StaticMutex, data: &'mutex UnsafeCell<T>)
-> LockResult<MutexGuard<'mutex, T>> {
poison::map_result(lock.poison.borrow(), |guard| {
Expand All @@ -290,6 +311,18 @@ impl<'mutex, T> MutexGuard<'mutex, T> {
}
})
}

#[cfg(not(stage0))] // NOTE remove cfg afte next snapshot
fn new(lock: &'mutex StaticMutex, data: &'mutex UnsafeCell<T>)
-> LockResult<MutexGuard<'mutex, T>> {
poison::map_result(lock.poison.borrow(), |guard| {
MutexGuard {
__lock: lock,
__data: data,
__poison: guard,
}
})
}
}

#[stable]
Expand Down
54 changes: 54 additions & 0 deletions src/libstd/sync/rwlock.rs
Expand Up @@ -110,23 +110,52 @@ pub const RW_LOCK_INIT: StaticRwLock = StaticRwLock {
/// dropped.
#[must_use]
#[stable]
#[cfg(stage0)] // NOTE remove impl after next snapshot
pub struct RwLockReadGuard<'a, T: 'a> {
__lock: &'a StaticRwLock,
__data: &'a UnsafeCell<T>,
__marker: marker::NoSend,
}

/// RAII structure used to release the shared read access of a lock when
/// dropped.
#[must_use]
#[stable]
#[cfg(not(stage0))] // NOTE remove cfg after next snapshot
pub struct RwLockReadGuard<'a, T: 'a> {
__lock: &'a StaticRwLock,
__data: &'a UnsafeCell<T>,
}

#[cfg(not(stage0))] // NOTE remove cfg after next snapshot
impl<'a, T> !marker::Send for RwLockReadGuard<'a, T> {}

/// RAII structure used to release the exclusive write access of a lock when
/// dropped.
#[must_use]
#[stable]
#[cfg(stage0)] // NOTE remove impl after next snapshot
pub struct RwLockWriteGuard<'a, T: 'a> {
__lock: &'a StaticRwLock,
__data: &'a UnsafeCell<T>,
__poison: poison::Guard,
__marker: marker::NoSend,
}

/// RAII structure used to release the exclusive write access of a lock when
/// dropped.
#[must_use]
#[stable]
#[cfg(not(stage0))] // NOTE remove cfg after next snapshot
pub struct RwLockWriteGuard<'a, T: 'a> {
__lock: &'a StaticRwLock,
__data: &'a UnsafeCell<T>,
__poison: poison::Guard,
}

#[cfg(not(stage0))] // NOTE remove cfg after next snapshot
impl<'a, T> !marker::Send for RwLockWriteGuard<'a, T> {}

impl<T: Send + Sync> RwLock<T> {
/// Creates a new instance of an RwLock which is unlocked and read to go.
#[stable]
Expand Down Expand Up @@ -303,6 +332,7 @@ impl StaticRwLock {
}

impl<'rwlock, T> RwLockReadGuard<'rwlock, T> {
#[cfg(stage0)] // NOTE remove impl after next snapshot
fn new(lock: &'rwlock StaticRwLock, data: &'rwlock UnsafeCell<T>)
-> LockResult<RwLockReadGuard<'rwlock, T>> {
poison::map_result(lock.poison.borrow(), |_| {
Expand All @@ -313,8 +343,20 @@ impl<'rwlock, T> RwLockReadGuard<'rwlock, T> {
}
})
}

#[cfg(not(stage0))] // NOTE remove cfg after next snapshot
fn new(lock: &'rwlock StaticRwLock, data: &'rwlock UnsafeCell<T>)
-> LockResult<RwLockReadGuard<'rwlock, T>> {
poison::map_result(lock.poison.borrow(), |_| {
RwLockReadGuard {
__lock: lock,
__data: data,
}
})
}
}
impl<'rwlock, T> RwLockWriteGuard<'rwlock, T> {
#[cfg(stage0)] // NOTE remove impl after next snapshot
fn new(lock: &'rwlock StaticRwLock, data: &'rwlock UnsafeCell<T>)
-> LockResult<RwLockWriteGuard<'rwlock, T>> {
poison::map_result(lock.poison.borrow(), |guard| {
Expand All @@ -326,6 +368,18 @@ impl<'rwlock, T> RwLockWriteGuard<'rwlock, T> {
}
})
}

#[cfg(not(stage0))] // NOTE remove cfg after next snapshot
fn new(lock: &'rwlock StaticRwLock, data: &'rwlock UnsafeCell<T>)
-> LockResult<RwLockWriteGuard<'rwlock, T>> {
poison::map_result(lock.poison.borrow(), |guard| {
RwLockWriteGuard {
__lock: lock,
__data: data,
__poison: guard,
}
})
}
}

#[stable]
Expand Down

0 comments on commit c6ab9a6

Please sign in to comment.