Skip to content

Commit dd64bd8

Browse files
committed
std: Move NativeMutex from &mut self to &self
The proper usage of shared types is now sharing through `&self` rather than `&mut self` because the mutable version will provide stronger guarantees (no aliasing on *any* thread).
1 parent da118e8 commit dd64bd8

File tree

7 files changed

+65
-56
lines changed

7 files changed

+65
-56
lines changed

src/libgreen/simple.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl Runtime for SimpleTask {
4040
// See libnative/task.rs for what's going on here with the `awoken`
4141
// field and the while loop around wait()
4242
unsafe {
43-
let mut guard = (*me).lock.lock();
43+
let guard = (*me).lock.lock();
4444
(*me).awoken = false;
4545
match f(task) {
4646
Ok(()) => {
@@ -60,7 +60,7 @@ impl Runtime for SimpleTask {
6060
to_wake.put_runtime(self as ~Runtime);
6161
unsafe {
6262
cast::forget(to_wake);
63-
let mut guard = (*me).lock.lock();
63+
let guard = (*me).lock.lock();
6464
(*me).awoken = true;
6565
guard.signal();
6666
}

src/libnative/io/timer_helper.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pub fn send(req: Req) {
7575
fn shutdown() {
7676
// Request a shutdown, and then wait for the task to exit
7777
unsafe {
78-
let mut guard = TIMER_HELPER_EXIT.lock();
78+
let guard = TIMER_HELPER_EXIT.lock();
7979
send(Shutdown);
8080
guard.wait();
8181
drop(guard);

src/libnative/task.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ impl rt::Runtime for Ops {
190190
let task = BlockedTask::block(cur_task);
191191

192192
if times == 1 {
193-
let mut guard = (*me).lock.lock();
193+
let guard = (*me).lock.lock();
194194
(*me).awoken = false;
195195
match f(task) {
196196
Ok(()) => {
@@ -202,7 +202,7 @@ impl rt::Runtime for Ops {
202202
}
203203
} else {
204204
let mut iter = task.make_selectable(times);
205-
let mut guard = (*me).lock.lock();
205+
let guard = (*me).lock.lock();
206206
(*me).awoken = false;
207207
let success = iter.all(|task| {
208208
match f(task) {
@@ -232,7 +232,7 @@ impl rt::Runtime for Ops {
232232
let me = &mut *self as *mut Ops;
233233
to_wake.put_runtime(self as ~rt::Runtime);
234234
cast::forget(to_wake);
235-
let mut guard = (*me).lock.lock();
235+
let guard = (*me).lock.lock();
236236
(*me).awoken = true;
237237
guard.signal();
238238
}

src/libstd/comm/shared.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub enum Failure {
6868
impl<T: Send> Packet<T> {
6969
// Creation of a packet *must* be followed by a call to inherit_blocker
7070
pub fn new() -> Packet<T> {
71-
let mut p = Packet {
71+
let p = Packet {
7272
queue: mpsc::Queue::new(),
7373
cnt: atomics::AtomicInt::new(0),
7474
steals: 0,

src/libstd/rt/bookkeeping.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub fn increment() {
3434
pub fn decrement() {
3535
unsafe {
3636
if TASK_COUNT.fetch_sub(1, atomics::SeqCst) == 1 {
37-
let mut guard = TASK_LOCK.lock();
37+
let guard = TASK_LOCK.lock();
3838
guard.signal();
3939
}
4040
}
@@ -44,7 +44,7 @@ pub fn decrement() {
4444
/// the entry points of native programs
4545
pub fn wait_for_other_tasks() {
4646
unsafe {
47-
let mut guard = TASK_LOCK.lock();
47+
let guard = TASK_LOCK.lock();
4848
while TASK_COUNT.load(atomics::SeqCst) > 0 {
4949
guard.wait();
5050
}

src/libstd/unstable/mutex.rs

Lines changed: 54 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ pub struct NativeMutex {
8686
/// then.
8787
#[must_use]
8888
pub struct LockGuard<'a> {
89-
priv lock: &'a mut StaticNativeMutex
89+
priv lock: &'a StaticNativeMutex
9090
}
9191

9292
pub static NATIVE_MUTEX_INIT: StaticNativeMutex = StaticNativeMutex {
@@ -106,6 +106,7 @@ impl StaticNativeMutex {
106106
/// already hold the lock.
107107
///
108108
/// # Example
109+
///
109110
/// ```rust
110111
/// use std::unstable::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
111112
/// static mut LOCK: StaticNativeMutex = NATIVE_MUTEX_INIT;
@@ -114,15 +115,15 @@ impl StaticNativeMutex {
114115
/// // critical section...
115116
/// } // automatically unlocked in `_guard`'s destructor
116117
/// ```
117-
pub unsafe fn lock<'a>(&'a mut self) -> LockGuard<'a> {
118+
pub unsafe fn lock<'a>(&'a self) -> LockGuard<'a> {
118119
self.inner.lock();
119120

120121
LockGuard { lock: self }
121122
}
122123

123124
/// Attempts to acquire the lock. The value returned is `Some` if
124125
/// the attempt succeeded.
125-
pub unsafe fn trylock<'a>(&'a mut self) -> Option<LockGuard<'a>> {
126+
pub unsafe fn trylock<'a>(&'a self) -> Option<LockGuard<'a>> {
126127
if self.inner.trylock() {
127128
Some(LockGuard { lock: self })
128129
} else {
@@ -134,36 +135,36 @@ impl StaticNativeMutex {
134135
///
135136
/// These needs to be paired with a call to `.unlock_noguard`. Prefer using
136137
/// `.lock`.
137-
pub unsafe fn lock_noguard(&mut self) { self.inner.lock() }
138+
pub unsafe fn lock_noguard(&self) { self.inner.lock() }
138139

139140
/// Attempts to acquire the lock without creating a
140141
/// `LockGuard`. The value returned is whether the lock was
141142
/// acquired or not.
142143
///
143144
/// If `true` is returned, this needs to be paired with a call to
144145
/// `.unlock_noguard`. Prefer using `.trylock`.
145-
pub unsafe fn trylock_noguard(&mut self) -> bool {
146+
pub unsafe fn trylock_noguard(&self) -> bool {
146147
self.inner.trylock()
147148
}
148149

149150
/// Unlocks the lock. This assumes that the current thread already holds the
150151
/// lock.
151-
pub unsafe fn unlock_noguard(&mut self) { self.inner.unlock() }
152+
pub unsafe fn unlock_noguard(&self) { self.inner.unlock() }
152153

153154
/// Block on the internal condition variable.
154155
///
155156
/// This function assumes that the lock is already held. Prefer
156157
/// using `LockGuard.wait` since that guarantees that the lock is
157158
/// held.
158-
pub unsafe fn wait_noguard(&mut self) { self.inner.wait() }
159+
pub unsafe fn wait_noguard(&self) { self.inner.wait() }
159160

160161
/// Signals a thread in `wait` to wake up
161-
pub unsafe fn signal_noguard(&mut self) { self.inner.signal() }
162+
pub unsafe fn signal_noguard(&self) { self.inner.signal() }
162163

163164
/// This function is especially unsafe because there are no guarantees made
164165
/// that no other thread is currently holding the lock or waiting on the
165166
/// condition variable contained inside.
166-
pub unsafe fn destroy(&mut self) { self.inner.destroy() }
167+
pub unsafe fn destroy(&self) { self.inner.destroy() }
167168
}
168169

169170
impl NativeMutex {
@@ -190,45 +191,45 @@ impl NativeMutex {
190191
/// } // automatically unlocked in `_guard`'s destructor
191192
/// }
192193
/// ```
193-
pub unsafe fn lock<'a>(&'a mut self) -> LockGuard<'a> {
194+
pub unsafe fn lock<'a>(&'a self) -> LockGuard<'a> {
194195
self.inner.lock()
195196
}
196197

197198
/// Attempts to acquire the lock. The value returned is `Some` if
198199
/// the attempt succeeded.
199-
pub unsafe fn trylock<'a>(&'a mut self) -> Option<LockGuard<'a>> {
200+
pub unsafe fn trylock<'a>(&'a self) -> Option<LockGuard<'a>> {
200201
self.inner.trylock()
201202
}
202203

203204
/// Acquire the lock without creating a `LockGuard`.
204205
///
205206
/// These needs to be paired with a call to `.unlock_noguard`. Prefer using
206207
/// `.lock`.
207-
pub unsafe fn lock_noguard(&mut self) { self.inner.lock_noguard() }
208+
pub unsafe fn lock_noguard(&self) { self.inner.lock_noguard() }
208209

209210
/// Attempts to acquire the lock without creating a
210211
/// `LockGuard`. The value returned is whether the lock was
211212
/// acquired or not.
212213
///
213214
/// If `true` is returned, this needs to be paired with a call to
214215
/// `.unlock_noguard`. Prefer using `.trylock`.
215-
pub unsafe fn trylock_noguard(&mut self) -> bool {
216+
pub unsafe fn trylock_noguard(&self) -> bool {
216217
self.inner.trylock_noguard()
217218
}
218219

219220
/// Unlocks the lock. This assumes that the current thread already holds the
220221
/// lock.
221-
pub unsafe fn unlock_noguard(&mut self) { self.inner.unlock_noguard() }
222+
pub unsafe fn unlock_noguard(&self) { self.inner.unlock_noguard() }
222223

223224
/// Block on the internal condition variable.
224225
///
225226
/// This function assumes that the lock is already held. Prefer
226227
/// using `LockGuard.wait` since that guarantees that the lock is
227228
/// held.
228-
pub unsafe fn wait_noguard(&mut self) { self.inner.wait_noguard() }
229+
pub unsafe fn wait_noguard(&self) { self.inner.wait_noguard() }
229230

230231
/// Signals a thread in `wait` to wake up
231-
pub unsafe fn signal_noguard(&mut self) { self.inner.signal_noguard() }
232+
pub unsafe fn signal_noguard(&self) { self.inner.signal_noguard() }
232233
}
233234

234235
impl Drop for NativeMutex {
@@ -239,12 +240,12 @@ impl Drop for NativeMutex {
239240

240241
impl<'a> LockGuard<'a> {
241242
/// Block on the internal condition variable.
242-
pub unsafe fn wait(&mut self) {
243+
pub unsafe fn wait(&self) {
243244
self.lock.wait_noguard()
244245
}
245246

246247
/// Signals a thread in `wait` to wake up.
247-
pub unsafe fn signal(&mut self) {
248+
pub unsafe fn signal(&self) {
248249
self.lock.signal_noguard()
249250
}
250251
}
@@ -262,6 +263,8 @@ mod imp {
262263
use self::os::{PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER,
263264
pthread_mutex_t, pthread_cond_t};
264265
use mem;
266+
use ty::Unsafe;
267+
use kinds::marker;
265268

266269
type pthread_mutexattr_t = libc::c_void;
267270
type pthread_condattr_t = libc::c_void;
@@ -369,40 +372,46 @@ mod imp {
369372
}
370373

371374
pub struct Mutex {
372-
priv lock: pthread_mutex_t,
373-
priv cond: pthread_cond_t,
375+
priv lock: Unsafe<pthread_mutex_t>,
376+
priv cond: Unsafe<pthread_cond_t>,
374377
}
375378

376379
pub static MUTEX_INIT: Mutex = Mutex {
377-
lock: PTHREAD_MUTEX_INITIALIZER,
378-
cond: PTHREAD_COND_INITIALIZER,
380+
lock: Unsafe {
381+
value: PTHREAD_MUTEX_INITIALIZER,
382+
marker1: marker::InvariantType,
383+
},
384+
cond: Unsafe {
385+
value: PTHREAD_COND_INITIALIZER,
386+
marker1: marker::InvariantType,
387+
},
379388
};
380389

381390
impl Mutex {
382391
pub unsafe fn new() -> Mutex {
383-
let mut m = Mutex {
384-
lock: mem::init(),
385-
cond: mem::init(),
392+
let m = Mutex {
393+
lock: Unsafe::new(mem::init()),
394+
cond: Unsafe::new(mem::init()),
386395
};
387396

388-
pthread_mutex_init(&mut m.lock, 0 as *libc::c_void);
389-
pthread_cond_init(&mut m.cond, 0 as *libc::c_void);
397+
pthread_mutex_init(m.lock.get(), 0 as *libc::c_void);
398+
pthread_cond_init(m.cond.get(), 0 as *libc::c_void);
390399

391400
return m;
392401
}
393402

394-
pub unsafe fn lock(&mut self) { pthread_mutex_lock(&mut self.lock); }
395-
pub unsafe fn unlock(&mut self) { pthread_mutex_unlock(&mut self.lock); }
396-
pub unsafe fn signal(&mut self) { pthread_cond_signal(&mut self.cond); }
397-
pub unsafe fn wait(&mut self) {
398-
pthread_cond_wait(&mut self.cond, &mut self.lock);
403+
pub unsafe fn lock(&self) { pthread_mutex_lock(self.lock.get()); }
404+
pub unsafe fn unlock(&self) { pthread_mutex_unlock(self.lock.get()); }
405+
pub unsafe fn signal(&self) { pthread_cond_signal(self.cond.get()); }
406+
pub unsafe fn wait(&self) {
407+
pthread_cond_wait(self.cond.get(), self.lock.get());
399408
}
400-
pub unsafe fn trylock(&mut self) -> bool {
401-
pthread_mutex_trylock(&mut self.lock) == 0
409+
pub unsafe fn trylock(&self) -> bool {
410+
pthread_mutex_trylock(self.lock.get()) == 0
402411
}
403-
pub unsafe fn destroy(&mut self) {
404-
pthread_mutex_destroy(&mut self.lock);
405-
pthread_cond_destroy(&mut self.cond);
412+
pub unsafe fn destroy(&self) {
413+
pthread_mutex_destroy(self.lock.get());
414+
pthread_cond_destroy(self.cond.get());
406415
}
407416
}
408417

@@ -454,37 +463,37 @@ mod imp {
454463
cond: atomics::AtomicUint::new(init_cond()),
455464
}
456465
}
457-
pub unsafe fn lock(&mut self) {
466+
pub unsafe fn lock(&self) {
458467
EnterCriticalSection(self.getlock() as LPCRITICAL_SECTION)
459468
}
460-
pub unsafe fn trylock(&mut self) -> bool {
469+
pub unsafe fn trylock(&self) -> bool {
461470
TryEnterCriticalSection(self.getlock() as LPCRITICAL_SECTION) != 0
462471
}
463-
pub unsafe fn unlock(&mut self) {
472+
pub unsafe fn unlock(&self) {
464473
LeaveCriticalSection(self.getlock() as LPCRITICAL_SECTION)
465474
}
466475

467-
pub unsafe fn wait(&mut self) {
476+
pub unsafe fn wait(&self) {
468477
self.unlock();
469478
WaitForSingleObject(self.getcond() as HANDLE, libc::INFINITE);
470479
self.lock();
471480
}
472481

473-
pub unsafe fn signal(&mut self) {
482+
pub unsafe fn signal(&self) {
474483
assert!(SetEvent(self.getcond() as HANDLE) != 0);
475484
}
476485

477486
/// This function is especially unsafe because there are no guarantees made
478487
/// that no other thread is currently holding the lock or waiting on the
479488
/// condition variable contained inside.
480-
pub unsafe fn destroy(&mut self) {
489+
pub unsafe fn destroy(&self) {
481490
let lock = self.lock.swap(0, atomics::SeqCst);
482491
let cond = self.cond.swap(0, atomics::SeqCst);
483492
if lock != 0 { free_lock(lock) }
484493
if cond != 0 { free_cond(cond) }
485494
}
486495

487-
unsafe fn getlock(&mut self) -> *mut c_void {
496+
unsafe fn getlock(&self) -> *mut c_void {
488497
match self.lock.load(atomics::SeqCst) {
489498
0 => {}
490499
n => return n as *mut c_void
@@ -498,7 +507,7 @@ mod imp {
498507
return self.lock.load(atomics::SeqCst) as *mut c_void;
499508
}
500509

501-
unsafe fn getcond(&mut self) -> *mut c_void {
510+
unsafe fn getcond(&self) -> *mut c_void {
502511
match self.cond.load(atomics::SeqCst) {
503512
0 => {}
504513
n => return n as *mut c_void

src/libstd/unstable/sync.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ impl<T:Send> Exclusive<T> {
7979
#[inline]
8080
pub unsafe fn hold_and_signal(&self, f: |x: &mut T|) {
8181
let rec = self.x.get();
82-
let mut guard = (*rec).lock.lock();
82+
let guard = (*rec).lock.lock();
8383
if (*rec).failed {
8484
fail!("Poisoned Exclusive::new - another task failed inside!");
8585
}
@@ -92,7 +92,7 @@ impl<T:Send> Exclusive<T> {
9292
#[inline]
9393
pub unsafe fn hold_and_wait(&self, f: |x: &T| -> bool) {
9494
let rec = self.x.get();
95-
let mut l = (*rec).lock.lock();
95+
let l = (*rec).lock.lock();
9696
if (*rec).failed {
9797
fail!("Poisoned Exclusive::new - another task failed inside!");
9898
}

0 commit comments

Comments
 (0)