@@ -98,6 +98,7 @@ impl StaticNativeMutex {
98
98
///
99
99
/// Note that a mutex created in this way needs to be explicit
100
100
/// freed with a call to `destroy` or it will leak.
101
+ /// Also it is important to avoid locking until mutex has stopped moving
101
102
pub unsafe fn new ( ) -> StaticNativeMutex {
102
103
StaticNativeMutex { inner : imp:: Mutex :: new ( ) }
103
104
}
@@ -172,6 +173,7 @@ impl NativeMutex {
172
173
///
173
174
/// The user must be careful to ensure the mutex is not locked when its is
174
175
/// being destroyed.
176
+ /// Also it is important to avoid locking until mutex has stopped moving
175
177
pub unsafe fn new ( ) -> NativeMutex {
176
178
NativeMutex { inner : StaticNativeMutex :: new ( ) }
177
179
}
@@ -262,7 +264,6 @@ mod imp {
262
264
use libc;
263
265
use self :: os:: { PTHREAD_MUTEX_INITIALIZER , PTHREAD_COND_INITIALIZER ,
264
266
pthread_mutex_t, pthread_cond_t} ;
265
- use mem;
266
267
use ty:: Unsafe ;
267
268
use kinds:: marker;
268
269
@@ -294,6 +295,7 @@ mod imp {
294
295
static __PTHREAD_MUTEX_SIZE__: uint = 40 ;
295
296
#[ cfg( target_arch = "x86" ) ]
296
297
static __PTHREAD_COND_SIZE__: uint = 24 ;
298
+
297
299
static _PTHREAD_MUTEX_SIG_init: libc:: c_long = 0x32AAABA7 ;
298
300
static _PTHREAD_COND_SIG_init: libc:: c_long = 0x3CB0B1BB ;
299
301
@@ -389,14 +391,14 @@ mod imp {
389
391
390
392
impl Mutex {
391
393
pub unsafe fn new ( ) -> Mutex {
394
+ // As mutex might be moved and address is changing it
395
+ // is better to avoid initialization of potentially
396
+ // opaque OS data before it landed
392
397
let m = Mutex {
393
- lock : Unsafe :: new ( mem :: zeroed ( ) ) ,
394
- cond : Unsafe :: new ( mem :: zeroed ( ) ) ,
398
+ lock : Unsafe :: new ( PTHREAD_MUTEX_INITIALIZER ) ,
399
+ cond : Unsafe :: new ( PTHREAD_COND_INITIALIZER ) ,
395
400
} ;
396
401
397
- pthread_mutex_init ( m. lock . get ( ) , 0 as * libc:: c_void ) ;
398
- pthread_cond_init ( m. cond . get ( ) , 0 as * libc:: c_void ) ;
399
-
400
402
return m;
401
403
}
402
404
@@ -416,11 +418,7 @@ mod imp {
416
418
}
417
419
418
420
extern {
419
- fn pthread_mutex_init ( lock : * mut pthread_mutex_t ,
420
- attr : * pthread_mutexattr_t ) -> libc:: c_int ;
421
421
fn pthread_mutex_destroy ( lock : * mut pthread_mutex_t ) -> libc:: c_int ;
422
- fn pthread_cond_init ( cond : * mut pthread_cond_t ,
423
- attr : * pthread_condattr_t ) -> libc:: c_int ;
424
422
fn pthread_cond_destroy ( cond : * mut pthread_cond_t ) -> libc:: c_int ;
425
423
fn pthread_mutex_lock ( lock : * mut pthread_mutex_t ) -> libc:: c_int ;
426
424
fn pthread_mutex_trylock ( lock : * mut pthread_mutex_t ) -> libc:: c_int ;
0 commit comments