Skip to content

Commit f436f9c

Browse files
committed
Make Send and Sync traits unsafe
1 parent 686ce66 commit f436f9c

32 files changed

+73
-55
lines changed

src/liballoc/arc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@ pub struct Weak<T> {
129129
_ptr: *mut ArcInner<T>,
130130
}
131131

132-
impl<T: Sync + Send> Send for Arc<T> { }
132+
unsafe impl<T: Sync + Send> Send for Arc<T> { }
133133

134-
impl<T: Sync + Send> Sync for Arc<T> { }
134+
unsafe impl<T: Sync + Send> Sync for Arc<T> { }
135135

136136
struct ArcInner<T> {
137137
strong: atomic::AtomicUint,

src/libcore/cell.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,6 @@ impl<T> RacyCell<T> {
577577
}
578578
}
579579

580-
impl<T:Send> Send for RacyCell<T> { }
580+
unsafe impl<T:Send> Send for RacyCell<T> { }
581581

582-
impl<T> Sync for RacyCell<T> { } // Oh dear
582+
unsafe impl<T> Sync for RacyCell<T> { } // Oh dear

src/libcore/kinds.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
2020
/// Types able to be transferred across task boundaries.
2121
#[lang="send"]
22-
pub trait Send for Sized? : 'static {
22+
pub unsafe trait Send for Sized? : 'static {
2323
// empty.
2424
}
2525

@@ -81,7 +81,7 @@ pub trait Copy for Sized? {
8181
/// reference; not doing this is undefined behaviour (for example,
8282
/// `transmute`-ing from `&T` to `&mut T` is illegal).
8383
#[lang="sync"]
84-
pub trait Sync for Sized? {
84+
pub unsafe trait Sync for Sized? {
8585
// Empty
8686
}
8787

src/libcore/ptr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -515,13 +515,13 @@ pub struct UniquePtr<T>(pub *mut T);
515515
/// reference is unaliased. Note that this aliasing invariant is
516516
/// unenforced by the type system; the abstraction using the
517517
/// `UniquePtr` must enforce it.
518-
impl<T:Send> Send for UniquePtr<T> { }
518+
unsafe impl<T:Send> Send for UniquePtr<T> { }
519519

520520
/// `UniquePtr` pointers are `Sync` if `T` is `Sync` because the data they
521521
/// reference is unaliased. Note that this aliasing invariant is
522522
/// unenforced by the type system; the abstraction using the
523523
/// `UniquePtr` must enforce it.
524-
impl<T:Sync> Sync for UniquePtr<T> { }
524+
unsafe impl<T:Sync> Sync for UniquePtr<T> { }
525525

526526
impl<T> UniquePtr<T> {
527527
/// Returns a null UniquePtr.

src/librustc_trans/back/write.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ struct ModuleConfig {
281281
time_passes: bool,
282282
}
283283

284-
impl Send for ModuleConfig { }
284+
unsafe impl Send for ModuleConfig { }
285285

286286
impl ModuleConfig {
287287
fn new(tm: TargetMachineRef, passes: Vec<String>) -> ModuleConfig {

src/librustc_trans/trans/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ pub struct ModuleTranslation {
6060
pub llmod: ModuleRef,
6161
}
6262

63-
impl Send for ModuleTranslation { }
64-
impl Sync for ModuleTranslation { }
63+
unsafe impl Send for ModuleTranslation { }
64+
unsafe impl Sync for ModuleTranslation { }
6565

6666
pub struct CrateTranslation {
6767
pub modules: Vec<ModuleTranslation>,

src/libstd/c_str.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ pub struct CString {
8989
owns_buffer_: bool,
9090
}
9191

92-
impl Send for CString { }
93-
impl Sync for CString { }
92+
unsafe impl Send for CString { }
93+
unsafe impl Sync for CString { }
9494

9595
impl Clone for CString {
9696
/// Clone this CString into a new, uniquely owned CString. For safety

src/libstd/comm/blocking.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,19 @@
1313
use thread::Thread;
1414
use sync::atomic::{AtomicBool, INIT_ATOMIC_BOOL, Ordering};
1515
use sync::Arc;
16+
use kinds::{Sync, Send};
1617
use kinds::marker::{NoSend, NoSync};
1718
use mem;
1819
use clone::Clone;
1920

20-
#[deriving(Send, Sync)]
2121
struct Inner {
2222
thread: Thread,
2323
woken: AtomicBool,
2424
}
2525

26+
unsafe impl Send for Inner {}
27+
unsafe impl Sync for Inner {}
28+
2629
#[deriving(Clone)]
2730
pub struct SignalToken {
2831
inner: Arc<Inner>,

src/libstd/comm/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ pub struct Receiver<T> {
363363

364364
// The receiver port can be sent from place to place, so long as it
365365
// is not used to receive non-sendable things.
366-
impl<T:Send> Send for Receiver<T> { }
366+
unsafe impl<T:Send> Send for Receiver<T> { }
367367

368368
/// An iterator over messages on a receiver, this iterator will block
369369
/// whenever `next` is called, waiting for a new message, and `None` will be
@@ -382,7 +382,7 @@ pub struct Sender<T> {
382382

383383
// The send port can be sent from place to place, so long as it
384384
// is not used to send non-sendable things.
385-
impl<T:Send> Send for Sender<T> { }
385+
unsafe impl<T:Send> Send for Sender<T> { }
386386

387387
/// The sending-half of Rust's synchronous channel type. This half can only be
388388
/// owned by one task, but it can be cloned to send to other tasks.

src/libstd/comm/mpsc_queue.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ pub struct Queue<T> {
7676
tail: UnsafeCell<*mut Node<T>>,
7777
}
7878

79-
impl<T:Send> Send for Queue<T> { }
80-
impl<T:Send> Sync for Queue<T> { }
79+
unsafe impl<T:Send> Send for Queue<T> { }
80+
unsafe impl<T:Send> Sync for Queue<T> { }
8181

8282
impl<T> Node<T> {
8383
unsafe fn new(v: Option<T>) -> *mut Node<T> {

src/libstd/comm/spsc_queue.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ pub struct Queue<T> {
7373
cache_subtractions: AtomicUint,
7474
}
7575

76-
impl<T: Send> Send for Queue<T> { }
76+
unsafe impl<T: Send> Send for Queue<T> { }
7777

78-
impl<T: Send> Sync for Queue<T> { }
78+
unsafe impl<T: Send> Sync for Queue<T> { }
7979

8080
impl<T: Send> Node<T> {
8181
fn new() -> *mut Node<T> {

src/libstd/comm/sync.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,10 @@ pub struct Packet<T> {
5353
lock: Mutex<State<T>>,
5454
}
5555

56-
impl<T:Send> Send for Packet<T> { }
56+
unsafe impl<T:Send> Send for Packet<T> { }
5757

58-
impl<T:Send> Sync for Packet<T> { }
58+
unsafe impl<T:Send> Sync for Packet<T> { }
5959

60-
#[deriving(Send)]
6160
struct State<T> {
6261
disconnected: bool, // Is the channel disconnected yet?
6362
queue: Queue, // queue of senders waiting to send data
@@ -74,6 +73,8 @@ struct State<T> {
7473
canceled: Option<&'static mut bool>,
7574
}
7675

76+
unsafe impl<T: Send> Send for State<T> {}
77+
7778
/// Possible flavors of threads who can be blocked on this channel.
7879
enum Blocker {
7980
BlockedSender(SignalToken),
@@ -93,7 +94,7 @@ struct Node {
9394
next: *mut Node,
9495
}
9596

96-
impl Send for Node {}
97+
unsafe impl Send for Node {}
9798

9899
/// A simple ring-buffer
99100
struct Buffer<T> {

src/libstd/rt/exclusive.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ pub struct Exclusive<T> {
2626
data: UnsafeCell<T>,
2727
}
2828

29-
impl<T:Send> Send for Exclusive<T> { }
29+
unsafe impl<T:Send> Send for Exclusive<T> { }
3030

31-
impl<T:Send> Sync for Exclusive<T> { }
31+
unsafe impl<T:Send> Sync for Exclusive<T> { }
3232

3333
/// An RAII guard returned via `lock`
3434
pub struct ExclusiveGuard<'a, T:'a> {

src/libstd/sync/mutex.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use prelude::*;
1212

1313
use cell::{UnsafeCell, RacyCell};
14-
use kinds::marker;
14+
use kinds::{marker, Sync};
1515
use sync::{poison, AsMutexGuard};
1616
use sys_common::mutex as sys;
1717

@@ -73,9 +73,9 @@ pub struct Mutex<T> {
7373
data: RacyCell<T>,
7474
}
7575

76-
impl<T:Send> Send for Mutex<T> { }
76+
unsafe impl<T:Send> Send for Mutex<T> { }
7777

78-
impl<T:Send> Sync for Mutex<T> { }
78+
unsafe impl<T:Send> Sync for Mutex<T> { }
7979

8080
/// The static mutex type is provided to allow for static allocation of mutexes.
8181
///
@@ -98,12 +98,13 @@ impl<T:Send> Sync for Mutex<T> { }
9898
/// }
9999
/// // lock is unlocked here.
100100
/// ```
101-
#[deriving(Sync)]
102101
pub struct StaticMutex {
103102
lock: sys::Mutex,
104103
poison: RacyCell<poison::Flag>,
105104
}
106105

106+
unsafe impl Sync for StaticMutex {}
107+
107108
/// An RAII implementation of a "scoped lock" of a mutex. When this structure is
108109
/// dropped (falls out of scope), the lock will be unlocked.
109110
///

src/libstd/sync/once.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
//! example use case would be for initializing an FFI library.
1515
1616
use int;
17+
use kinds::Sync;
1718
use mem::drop;
1819
use ops::FnOnce;
1920
use sync::atomic;
@@ -35,13 +36,14 @@ use sync::{StaticMutex, MUTEX_INIT};
3536
/// // run initialization here
3637
/// });
3738
/// ```
38-
#[deriving(Sync)]
3939
pub struct Once {
4040
mutex: StaticMutex,
4141
cnt: atomic::AtomicInt,
4242
lock_cnt: atomic::AtomicInt,
4343
}
4444

45+
unsafe impl Sync for Once {}
46+
4547
/// Initialization value for static `Once` values.
4648
pub const ONCE_INIT: Once = Once {
4749
mutex: MUTEX_INIT,

src/libstd/sys/common/helper_thread.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ pub struct Helper<M> {
5959
pub shutdown: UnsafeCell<bool>,
6060
}
6161

62-
impl<M:Send> Send for Helper<M> { }
62+
unsafe impl<M:Send> Send for Helper<M> { }
6363

64-
impl<M:Send> Sync for Helper<M> { }
64+
unsafe impl<M:Send> Sync for Helper<M> { }
6565

6666
impl<M: Send> Helper<M> {
6767
/// Lazily boots a helper thread, becoming a no-op if the helper has already

src/libstd/sys/common/mutex.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use kinds::Sync;
1112
use sys::mutex as imp;
1213

1314
/// An OS-based mutual exclusion lock.
1415
///
1516
/// This is the thinnest cross-platform wrapper around OS mutexes. All usage of
1617
/// this mutex is unsafe and it is recommended to instead use the safe wrapper
1718
/// at the top level of the crate instead of this type.
18-
#[deriving(Sync)]
1919
pub struct Mutex(imp::Mutex);
2020

21+
unsafe impl Sync for Mutex {}
22+
2123
/// Constant initializer for statically allocated mutexes.
2224
pub const MUTEX_INIT: Mutex = Mutex(imp::MUTEX_INIT);
2325

src/libstd/sys/unix/c.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ mod signal {
162162
sa_restorer: *mut libc::c_void,
163163
}
164164

165-
impl ::kinds::Send for sigaction { }
166-
impl ::kinds::Sync for sigaction { }
165+
unsafe impl ::kinds::Send for sigaction { }
166+
unsafe impl ::kinds::Sync for sigaction { }
167167

168168
#[repr(C)]
169169
#[cfg(target_word_size = "32")]

src/libstd/sys/unix/mutex.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use kinds::Sync;
1112
use cell::{UnsafeCell, RacyCell};
1213
use sys::sync as ffi;
1314
use sys_common::mutex;
1415

15-
#[deriving(Sync)]
1616
pub struct Mutex { inner: RacyCell<ffi::pthread_mutex_t> }
1717

1818
#[inline]
@@ -24,6 +24,8 @@ pub const MUTEX_INIT: Mutex = Mutex {
2424
inner: RacyCell(UnsafeCell { value: ffi::PTHREAD_MUTEX_INITIALIZER }),
2525
};
2626

27+
unsafe impl Sync for Mutex {}
28+
2729
impl Mutex {
2830
#[inline]
2931
pub unsafe fn new() -> Mutex {

src/libstd/sys/unix/pipe.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,12 +210,13 @@ impl Clone for UnixStream {
210210
// Unix Listener
211211
////////////////////////////////////////////////////////////////////////////////
212212

213-
#[deriving(Sync)]
214213
pub struct UnixListener {
215214
inner: Inner,
216215
path: CString,
217216
}
218217

218+
unsafe impl Sync for UnixListener {}
219+
219220
impl UnixListener {
220221
pub fn bind(addr: &CString) -> IoResult<UnixListener> {
221222
bind(addr, libc::SOCK_STREAM).map(|fd| {
@@ -253,14 +254,15 @@ pub struct UnixAcceptor {
253254
deadline: u64,
254255
}
255256

256-
#[deriving(Sync)]
257257
struct AcceptorInner {
258258
listener: UnixListener,
259259
reader: FileDesc,
260260
writer: FileDesc,
261261
closed: atomic::AtomicBool,
262262
}
263263

264+
unsafe impl Sync for AcceptorInner {}
265+
264266
impl UnixAcceptor {
265267
pub fn fd(&self) -> fd_t { self.inner.listener.fd() }
266268

src/libstd/sys/unix/tcp.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ pub use sys_common::net::TcpStream;
2929
// TCP listeners
3030
////////////////////////////////////////////////////////////////////////////////
3131

32-
#[deriving(Sync)]
3332
pub struct TcpListener {
3433
pub inner: FileDesc,
3534
}
3635

36+
unsafe impl Sync for TcpListener {}
37+
3738
impl TcpListener {
3839
pub fn bind(addr: ip::SocketAddr) -> IoResult<TcpListener> {
3940
let fd = try!(net::socket(addr, libc::SOCK_STREAM));
@@ -90,14 +91,15 @@ pub struct TcpAcceptor {
9091
deadline: u64,
9192
}
9293

93-
#[deriving(Sync)]
9494
struct AcceptorInner {
9595
listener: TcpListener,
9696
reader: FileDesc,
9797
writer: FileDesc,
9898
closed: atomic::AtomicBool,
9999
}
100100

101+
unsafe impl Sync for AcceptorInner {}
102+
101103
impl TcpAcceptor {
102104
pub fn fd(&self) -> sock_t { self.inner.listener.fd() }
103105

0 commit comments

Comments
 (0)