From ac7b9ddc545b7f62f00bf8f4d490d31ff4b90d1d Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 12 Jun 2014 11:40:13 -0700 Subject: Audit usage of NativeMutex Once a native mutex has been used once, it is never allowed to be moved again. This is because some pthreads implementations take pointers inside the mutex itself. This commit adds stern wording around the methods on native mutexes, and fixes one use case in the codebase. The Mutex type in libsync was susceptible to movement, so the inner static mutex is now boxed to ensure that the address of the native mutex is constant. --- src/libsync/mutex.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'src/libsync') diff --git a/src/libsync/mutex.rs b/src/libsync/mutex.rs index 6b9ff3cf052..ef558d3f924 100644 --- a/src/libsync/mutex.rs +++ b/src/libsync/mutex.rs @@ -97,7 +97,14 @@ pub static NATIVE_BLOCKED: uint = 1 << 2; /// drop(guard); // unlock the lock /// ``` pub struct Mutex { - lock: StaticMutex, + // Note that this static mutex is in a *box*, not inlined into the struct + // itself. This is done for memory safety reasons with the usage of a + // StaticNativeMutex inside the static mutex above. Once a native mutex has + // been used once, its address can never change (it can't be moved). This + // mutex type can be safely moved at any time, so to ensure that the native + // mutex is used correctly we box the inner lock to give it a constant + // address. + lock: Box, } #[deriving(PartialEq, Show)] @@ -458,7 +465,7 @@ impl Mutex { /// Creates a new mutex in an unlocked state ready for use. pub fn new() -> Mutex { Mutex { - lock: StaticMutex { + lock: box StaticMutex { state: atomics::AtomicUint::new(0), flavor: Unsafe::new(Unlocked), green_blocker: Unsafe::new(0), -- cgit 1.4.1-3-g733a5