@@ -86,7 +86,7 @@ pub struct NativeMutex {
8686/// then.
8787#[ must_use]
8888pub struct LockGuard < ' a > {
89- priv lock : & ' a mut StaticNativeMutex
89+ priv lock : & ' a StaticNativeMutex
9090}
9191
9292pub 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
169170impl 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
234235impl Drop for NativeMutex {
@@ -239,12 +240,12 @@ impl Drop for NativeMutex {
239240
240241impl < ' 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
0 commit comments