about summary refs log tree commit diff
path: root/src/libsync
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2014-06-12 11:40:13 -0700
committerAlex Crichton <alex@alexcrichton.com>2014-06-13 13:53:34 -0700
commitac7b9ddc545b7f62f00bf8f4d490d31ff4b90d1d (patch)
tree88392ca6e57d1ec5481f9cc9d002b7b153bd8148 /src/libsync
parentb612ae9edea26cb8704363c47a66d583b644ad09 (diff)
downloadrust-ac7b9ddc545b7f62f00bf8f4d490d31ff4b90d1d.tar.gz
rust-ac7b9ddc545b7f62f00bf8f4d490d31ff4b90d1d.zip
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.
Diffstat (limited to 'src/libsync')
-rw-r--r--src/libsync/mutex.rs11
1 files changed, 9 insertions, 2 deletions
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<StaticMutex>,
 }
 
 #[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),