@@ -86,7 +86,7 @@ pub struct NativeMutex {
86
86
/// then.
87
87
#[ must_use]
88
88
pub struct LockGuard < ' a > {
89
- priv lock : & ' a mut StaticNativeMutex
89
+ priv lock : & ' a StaticNativeMutex
90
90
}
91
91
92
92
pub static NATIVE_MUTEX_INIT : StaticNativeMutex = StaticNativeMutex {
@@ -106,6 +106,7 @@ impl StaticNativeMutex {
106
106
/// already hold the lock.
107
107
///
108
108
/// # Example
109
+ ///
109
110
/// ```rust
110
111
/// use std::unstable::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
111
112
/// static mut LOCK: StaticNativeMutex = NATIVE_MUTEX_INIT;
@@ -114,15 +115,15 @@ impl StaticNativeMutex {
114
115
/// // critical section...
115
116
/// } // automatically unlocked in `_guard`'s destructor
116
117
/// ```
117
- pub unsafe fn lock < ' a > ( & ' a mut self ) -> LockGuard < ' a > {
118
+ pub unsafe fn lock < ' a > ( & ' a self ) -> LockGuard < ' a > {
118
119
self . inner . lock ( ) ;
119
120
120
121
LockGuard { lock : self }
121
122
}
122
123
123
124
/// Attempts to acquire the lock. The value returned is `Some` if
124
125
/// 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 > > {
126
127
if self . inner . trylock ( ) {
127
128
Some ( LockGuard { lock : self } )
128
129
} else {
@@ -134,36 +135,36 @@ impl StaticNativeMutex {
134
135
///
135
136
/// These needs to be paired with a call to `.unlock_noguard`. Prefer using
136
137
/// `.lock`.
137
- pub unsafe fn lock_noguard ( & mut self ) { self . inner . lock ( ) }
138
+ pub unsafe fn lock_noguard ( & self ) { self . inner . lock ( ) }
138
139
139
140
/// Attempts to acquire the lock without creating a
140
141
/// `LockGuard`. The value returned is whether the lock was
141
142
/// acquired or not.
142
143
///
143
144
/// If `true` is returned, this needs to be paired with a call to
144
145
/// `.unlock_noguard`. Prefer using `.trylock`.
145
- pub unsafe fn trylock_noguard ( & mut self ) -> bool {
146
+ pub unsafe fn trylock_noguard ( & self ) -> bool {
146
147
self . inner . trylock ( )
147
148
}
148
149
149
150
/// Unlocks the lock. This assumes that the current thread already holds the
150
151
/// lock.
151
- pub unsafe fn unlock_noguard ( & mut self ) { self . inner . unlock ( ) }
152
+ pub unsafe fn unlock_noguard ( & self ) { self . inner . unlock ( ) }
152
153
153
154
/// Block on the internal condition variable.
154
155
///
155
156
/// This function assumes that the lock is already held. Prefer
156
157
/// using `LockGuard.wait` since that guarantees that the lock is
157
158
/// held.
158
- pub unsafe fn wait_noguard ( & mut self ) { self . inner . wait ( ) }
159
+ pub unsafe fn wait_noguard ( & self ) { self . inner . wait ( ) }
159
160
160
161
/// 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 ( ) }
162
163
163
164
/// This function is especially unsafe because there are no guarantees made
164
165
/// that no other thread is currently holding the lock or waiting on the
165
166
/// condition variable contained inside.
166
- pub unsafe fn destroy ( & mut self ) { self . inner . destroy ( ) }
167
+ pub unsafe fn destroy ( & self ) { self . inner . destroy ( ) }
167
168
}
168
169
169
170
impl NativeMutex {
@@ -190,45 +191,45 @@ impl NativeMutex {
190
191
/// } // automatically unlocked in `_guard`'s destructor
191
192
/// }
192
193
/// ```
193
- pub unsafe fn lock < ' a > ( & ' a mut self ) -> LockGuard < ' a > {
194
+ pub unsafe fn lock < ' a > ( & ' a self ) -> LockGuard < ' a > {
194
195
self . inner . lock ( )
195
196
}
196
197
197
198
/// Attempts to acquire the lock. The value returned is `Some` if
198
199
/// 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 > > {
200
201
self . inner . trylock ( )
201
202
}
202
203
203
204
/// Acquire the lock without creating a `LockGuard`.
204
205
///
205
206
/// These needs to be paired with a call to `.unlock_noguard`. Prefer using
206
207
/// `.lock`.
207
- pub unsafe fn lock_noguard ( & mut self ) { self . inner . lock_noguard ( ) }
208
+ pub unsafe fn lock_noguard ( & self ) { self . inner . lock_noguard ( ) }
208
209
209
210
/// Attempts to acquire the lock without creating a
210
211
/// `LockGuard`. The value returned is whether the lock was
211
212
/// acquired or not.
212
213
///
213
214
/// If `true` is returned, this needs to be paired with a call to
214
215
/// `.unlock_noguard`. Prefer using `.trylock`.
215
- pub unsafe fn trylock_noguard ( & mut self ) -> bool {
216
+ pub unsafe fn trylock_noguard ( & self ) -> bool {
216
217
self . inner . trylock_noguard ( )
217
218
}
218
219
219
220
/// Unlocks the lock. This assumes that the current thread already holds the
220
221
/// lock.
221
- pub unsafe fn unlock_noguard ( & mut self ) { self . inner . unlock_noguard ( ) }
222
+ pub unsafe fn unlock_noguard ( & self ) { self . inner . unlock_noguard ( ) }
222
223
223
224
/// Block on the internal condition variable.
224
225
///
225
226
/// This function assumes that the lock is already held. Prefer
226
227
/// using `LockGuard.wait` since that guarantees that the lock is
227
228
/// held.
228
- pub unsafe fn wait_noguard ( & mut self ) { self . inner . wait_noguard ( ) }
229
+ pub unsafe fn wait_noguard ( & self ) { self . inner . wait_noguard ( ) }
229
230
230
231
/// 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 ( ) }
232
233
}
233
234
234
235
impl Drop for NativeMutex {
@@ -239,12 +240,12 @@ impl Drop for NativeMutex {
239
240
240
241
impl < ' a > LockGuard < ' a > {
241
242
/// Block on the internal condition variable.
242
- pub unsafe fn wait ( & mut self ) {
243
+ pub unsafe fn wait ( & self ) {
243
244
self . lock . wait_noguard ( )
244
245
}
245
246
246
247
/// Signals a thread in `wait` to wake up.
247
- pub unsafe fn signal ( & mut self ) {
248
+ pub unsafe fn signal ( & self ) {
248
249
self . lock . signal_noguard ( )
249
250
}
250
251
}
@@ -262,6 +263,8 @@ mod imp {
262
263
use self :: os:: { PTHREAD_MUTEX_INITIALIZER , PTHREAD_COND_INITIALIZER ,
263
264
pthread_mutex_t, pthread_cond_t} ;
264
265
use mem;
266
+ use ty:: Unsafe ;
267
+ use kinds:: marker;
265
268
266
269
type pthread_mutexattr_t = libc:: c_void ;
267
270
type pthread_condattr_t = libc:: c_void ;
@@ -369,40 +372,46 @@ mod imp {
369
372
}
370
373
371
374
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 > ,
374
377
}
375
378
376
379
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
+ } ,
379
388
} ;
380
389
381
390
impl Mutex {
382
391
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 ( ) ) ,
386
395
} ;
387
396
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 ) ;
390
399
391
400
return m;
392
401
}
393
402
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 ( ) ) ;
399
408
}
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
402
411
}
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 ( ) ) ;
406
415
}
407
416
}
408
417
@@ -454,37 +463,37 @@ mod imp {
454
463
cond : atomics:: AtomicUint :: new ( init_cond ( ) ) ,
455
464
}
456
465
}
457
- pub unsafe fn lock ( & mut self ) {
466
+ pub unsafe fn lock ( & self ) {
458
467
EnterCriticalSection ( self . getlock ( ) as LPCRITICAL_SECTION )
459
468
}
460
- pub unsafe fn trylock ( & mut self ) -> bool {
469
+ pub unsafe fn trylock ( & self ) -> bool {
461
470
TryEnterCriticalSection ( self . getlock ( ) as LPCRITICAL_SECTION ) != 0
462
471
}
463
- pub unsafe fn unlock ( & mut self ) {
472
+ pub unsafe fn unlock ( & self ) {
464
473
LeaveCriticalSection ( self . getlock ( ) as LPCRITICAL_SECTION )
465
474
}
466
475
467
- pub unsafe fn wait ( & mut self ) {
476
+ pub unsafe fn wait ( & self ) {
468
477
self . unlock ( ) ;
469
478
WaitForSingleObject ( self . getcond ( ) as HANDLE , libc:: INFINITE ) ;
470
479
self . lock ( ) ;
471
480
}
472
481
473
- pub unsafe fn signal ( & mut self ) {
482
+ pub unsafe fn signal ( & self ) {
474
483
assert ! ( SetEvent ( self . getcond( ) as HANDLE ) != 0 ) ;
475
484
}
476
485
477
486
/// This function is especially unsafe because there are no guarantees made
478
487
/// that no other thread is currently holding the lock or waiting on the
479
488
/// condition variable contained inside.
480
- pub unsafe fn destroy ( & mut self ) {
489
+ pub unsafe fn destroy ( & self ) {
481
490
let lock = self . lock . swap ( 0 , atomics:: SeqCst ) ;
482
491
let cond = self . cond . swap ( 0 , atomics:: SeqCst ) ;
483
492
if lock != 0 { free_lock ( lock) }
484
493
if cond != 0 { free_cond ( cond) }
485
494
}
486
495
487
- unsafe fn getlock ( & mut self ) -> * mut c_void {
496
+ unsafe fn getlock ( & self ) -> * mut c_void {
488
497
match self . lock . load ( atomics:: SeqCst ) {
489
498
0 => { }
490
499
n => return n as * mut c_void
@@ -498,7 +507,7 @@ mod imp {
498
507
return self . lock . load ( atomics:: SeqCst ) as * mut c_void ;
499
508
}
500
509
501
- unsafe fn getcond ( & mut self ) -> * mut c_void {
510
+ unsafe fn getcond ( & self ) -> * mut c_void {
502
511
match self . cond . load ( atomics:: SeqCst ) {
503
512
0 => { }
504
513
n => return n as * mut c_void
0 commit comments