std: Rename unstable::mutex::Mutex to StaticNativeMutex.

This better reflects its purpose and design.
This commit is contained in:
Huon Wilson
2014-02-15 11:18:49 +11:00
parent 75d92dbabe
commit b87ed605c0
13 changed files with 69 additions and 65 deletions

View File

@@ -15,7 +15,7 @@ use std::rt::rtio::{RemoteCallback, PausableIdleCallback, Callback, EventLoop};
use std::rt::task::BlockedTask; use std::rt::task::BlockedTask;
use std::rt::task::Task; use std::rt::task::Task;
use std::sync::deque; use std::sync::deque;
use std::unstable::mutex::Mutex; use std::unstable::mutex::StaticNativeMutex;
use std::unstable::raw; use std::unstable::raw;
use TaskState; use TaskState;
@@ -764,7 +764,7 @@ impl Scheduler {
// to it, but we're guaranteed that the task won't exit until we've // to it, but we're guaranteed that the task won't exit until we've
// unlocked the lock so there's no worry of this memory going away. // unlocked the lock so there's no worry of this memory going away.
let cur = self.change_task_context(cur, next, |sched, mut task| { let cur = self.change_task_context(cur, next, |sched, mut task| {
let lock: *mut Mutex = &mut task.nasty_deschedule_lock; let lock: *mut StaticNativeMutex = &mut task.nasty_deschedule_lock;
unsafe { unsafe {
let _guard = (*lock).lock(); let _guard = (*lock).lock();
f(sched, BlockedTask::block(task.swap())); f(sched, BlockedTask::block(task.swap()));
@@ -1453,8 +1453,8 @@ mod test {
#[test] #[test]
fn test_spawn_sched_blocking() { fn test_spawn_sched_blocking() {
use std::unstable::mutex::{Mutex, MUTEX_INIT}; use std::unstable::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
static mut LOCK: Mutex = MUTEX_INIT; static mut LOCK: StaticNativeMutex = NATIVE_MUTEX_INIT;
// Testing that a task in one scheduler can block in foreign code // Testing that a task in one scheduler can block in foreign code
// without affecting other schedulers // without affecting other schedulers

View File

@@ -25,7 +25,7 @@ use std::rt::local::Local;
use std::rt::rtio; use std::rt::rtio;
use std::rt::task::{Task, BlockedTask, SendMessage}; use std::rt::task::{Task, BlockedTask, SendMessage};
use std::task::TaskOpts; use std::task::TaskOpts;
use std::unstable::mutex::Mutex; use std::unstable::mutex::StaticNativeMutex;
use std::unstable::raw; use std::unstable::raw;
use context::Context; use context::Context;
@@ -65,7 +65,7 @@ pub struct GreenTask {
pool_id: uint, pool_id: uint,
// See the comments in the scheduler about why this is necessary // See the comments in the scheduler about why this is necessary
nasty_deschedule_lock: Mutex, nasty_deschedule_lock: StaticNativeMutex,
} }
pub enum TaskType { pub enum TaskType {
@@ -163,7 +163,7 @@ impl GreenTask {
task_type: task_type, task_type: task_type,
sched: None, sched: None,
handle: None, handle: None,
nasty_deschedule_lock: unsafe { Mutex::new() }, nasty_deschedule_lock: unsafe { StaticNativeMutex::new() },
task: Some(~Task::new()), task: Some(~Task::new()),
} }
} }
@@ -322,7 +322,7 @@ impl GreenTask {
// uncontended except for when the task is rescheduled). // uncontended except for when the task is rescheduled).
fn reawaken_remotely(mut ~self) { fn reawaken_remotely(mut ~self) {
unsafe { unsafe {
let mtx = &mut self.nasty_deschedule_lock as *mut Mutex; let mtx = &mut self.nasty_deschedule_lock as *mut StaticNativeMutex;
let handle = self.handle.get_mut_ref() as *mut SchedHandle; let handle = self.handle.get_mut_ref() as *mut SchedHandle;
let _guard = (*mtx).lock(); let _guard = (*mtx).lock();
(*handle).send(RunOnce(self)); (*handle).send(RunOnce(self));

View File

@@ -17,10 +17,10 @@
//! The green counterpart for this is bookkeeping on sched pools. //! The green counterpart for this is bookkeeping on sched pools.
use std::sync::atomics; use std::sync::atomics;
use std::unstable::mutex::{Mutex, MUTEX_INIT}; use std::unstable::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
static mut TASK_COUNT: atomics::AtomicUint = atomics::INIT_ATOMIC_UINT; static mut TASK_COUNT: atomics::AtomicUint = atomics::INIT_ATOMIC_UINT;
static mut TASK_LOCK: Mutex = MUTEX_INIT; static mut TASK_LOCK: StaticNativeMutex = NATIVE_MUTEX_INIT;
pub fn increment() { pub fn increment() {
let _ = unsafe { TASK_COUNT.fetch_add(1, atomics::SeqCst) }; let _ = unsafe { TASK_COUNT.fetch_add(1, atomics::SeqCst) };

View File

@@ -218,9 +218,9 @@ pub fn init() {
} }
unsafe { unsafe {
use std::unstable::mutex::{Mutex, MUTEX_INIT}; use std::unstable::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
static mut INITIALIZED: bool = false; static mut INITIALIZED: bool = false;
static mut LOCK: Mutex = MUTEX_INIT; static mut LOCK: StaticNativeMutex = NATIVE_MUTEX_INIT;
let _guard = LOCK.lock(); let _guard = LOCK.lock();
if !INITIALIZED { if !INITIALIZED {

View File

@@ -22,7 +22,7 @@
use std::cast; use std::cast;
use std::rt; use std::rt;
use std::unstable::mutex::{Mutex, MUTEX_INIT}; use std::unstable::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
use bookkeeping; use bookkeeping;
use io::timer::{Req, Shutdown}; use io::timer::{Req, Shutdown};
@@ -37,7 +37,7 @@ static mut HELPER_CHAN: *mut Chan<Req> = 0 as *mut Chan<Req>;
static mut HELPER_SIGNAL: imp::signal = 0 as imp::signal; static mut HELPER_SIGNAL: imp::signal = 0 as imp::signal;
pub fn boot(helper: fn(imp::signal, Port<Req>)) { pub fn boot(helper: fn(imp::signal, Port<Req>)) {
static mut LOCK: Mutex = MUTEX_INIT; static mut LOCK: StaticNativeMutex = NATIVE_MUTEX_INIT;
static mut INITIALIZED: bool = false; static mut INITIALIZED: bool = false;
unsafe { unsafe {

View File

@@ -22,7 +22,7 @@ use std::rt::task::{Task, BlockedTask, SendMessage};
use std::rt::thread::Thread; use std::rt::thread::Thread;
use std::rt; use std::rt;
use std::task::TaskOpts; use std::task::TaskOpts;
use std::unstable::mutex::Mutex; use std::unstable::mutex::StaticNativeMutex;
use std::unstable::stack; use std::unstable::stack;
use io; use io;
@@ -40,7 +40,7 @@ pub fn new(stack_bounds: (uint, uint)) -> ~Task {
fn ops() -> ~Ops { fn ops() -> ~Ops {
~Ops { ~Ops {
lock: unsafe { Mutex::new() }, lock: unsafe { StaticNativeMutex::new() },
awoken: false, awoken: false,
io: io::IoFactory::new(), io: io::IoFactory::new(),
// these *should* get overwritten // these *should* get overwritten
@@ -109,7 +109,7 @@ pub fn spawn_opts(opts: TaskOpts, f: proc()) {
// This structure is the glue between channels and the 1:1 scheduling mode. This // This structure is the glue between channels and the 1:1 scheduling mode. This
// structure is allocated once per task. // structure is allocated once per task.
struct Ops { struct Ops {
lock: Mutex, // native synchronization lock: StaticNativeMutex, // native synchronization
awoken: bool, // used to prevent spurious wakeups awoken: bool, // used to prevent spurious wakeups
io: io::IoFactory, // local I/O factory io: io::IoFactory, // local I/O factory

View File

@@ -28,7 +28,7 @@ use rt::local::Local;
use rt::task::{Task, BlockedTask}; use rt::task::{Task, BlockedTask};
use rt::thread::Thread; use rt::thread::Thread;
use sync::atomics; use sync::atomics;
use unstable::mutex::Mutex; use unstable::mutex::StaticNativeMutex;
use vec::OwnedVector; use vec::OwnedVector;
use mpsc = sync::mpsc_queue; use mpsc = sync::mpsc_queue;
@@ -53,7 +53,7 @@ pub struct Packet<T> {
// this lock protects various portions of this implementation during // this lock protects various portions of this implementation during
// select() // select()
select_lock: Mutex, select_lock: StaticNativeMutex,
} }
pub enum Failure { pub enum Failure {
@@ -72,7 +72,7 @@ impl<T: Send> Packet<T> {
channels: atomics::AtomicInt::new(2), channels: atomics::AtomicInt::new(2),
port_dropped: atomics::AtomicBool::new(false), port_dropped: atomics::AtomicBool::new(false),
sender_drain: atomics::AtomicInt::new(0), sender_drain: atomics::AtomicInt::new(0),
select_lock: unsafe { Mutex::new() }, select_lock: unsafe { StaticNativeMutex::new() },
}; };
// see comments in inherit_blocker about why we grab this lock // see comments in inherit_blocker about why we grab this lock
unsafe { p.select_lock.lock_noguard() } unsafe { p.select_lock.lock_noguard() }

View File

@@ -144,9 +144,9 @@ Accessing environment variables is not generally threadsafe.
Serialize access through a global lock. Serialize access through a global lock.
*/ */
fn with_env_lock<T>(f: || -> T) -> T { fn with_env_lock<T>(f: || -> T) -> T {
use unstable::mutex::{Mutex, MUTEX_INIT}; use unstable::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
static mut lock: Mutex = MUTEX_INIT; static mut lock: StaticNativeMutex = NATIVE_MUTEX_INIT;
unsafe { unsafe {
let _guard = lock.lock(); let _guard = lock.lock();

View File

@@ -68,11 +68,11 @@ mod imp {
use option::{Option, Some, None}; use option::{Option, Some, None};
use ptr::RawPtr; use ptr::RawPtr;
use iter::Iterator; use iter::Iterator;
use unstable::mutex::{Mutex, MUTEX_INIT}; use unstable::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
use mem; use mem;
static mut global_args_ptr: uint = 0; static mut global_args_ptr: uint = 0;
static mut lock: Mutex = MUTEX_INIT; static mut lock: StaticNativeMutex = NATIVE_MUTEX_INIT;
#[cfg(not(test))] #[cfg(not(test))]
pub unsafe fn init(argc: int, argv: **u8) { pub unsafe fn init(argc: int, argv: **u8) {

View File

@@ -152,8 +152,8 @@ pub mod dl {
} }
pub fn check_for_errors_in<T>(f: || -> T) -> Result<T, ~str> { pub fn check_for_errors_in<T>(f: || -> T) -> Result<T, ~str> {
use unstable::mutex::{Mutex, MUTEX_INIT}; use unstable::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
static mut lock: Mutex = MUTEX_INIT; static mut lock: StaticNativeMutex = NATIVE_MUTEX_INIT;
unsafe { unsafe {
// dlerror isn't thread safe, so we need to lock around this entire // dlerror isn't thread safe, so we need to lock around this entire
// sequence // sequence

View File

@@ -11,9 +11,9 @@
//! A native mutex and condition variable type //! A native mutex and condition variable type
//! //!
//! This module contains bindings to the platform's native mutex/condition //! This module contains bindings to the platform's native mutex/condition
//! variable primitives. It provides a single type, `Mutex`, which can be //! variable primitives. It provides a single type, `StaticNativeMutex`, which can be
//! statically initialized via the `MUTEX_INIT` value. This object serves as both a //! statically initialized via the `NATIVE_MUTEX_INIT` value. This object serves as
//! mutex and a condition variable simultaneously. //! both a mutex and a condition variable simultaneously.
//! //!
//! The lock is lazily initialized, but it can only be unsafely destroyed. A //! The lock is lazily initialized, but it can only be unsafely destroyed. A
//! statically initialized lock doesn't necessarily have a time at which it can //! statically initialized lock doesn't necessarily have a time at which it can
@@ -27,21 +27,23 @@
//! //!
//! # Example //! # Example
//! //!
//! use std::unstable::mutex::{Mutex, MUTEX_INIT}; //! use std::unstable::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
//! //!
//! // Use a statically initialized mutex //! // Use a statically initialized mutex
//! static mut lock: Mutex = MUTEX_INIT; //! static mut lock: StaticNativeMutex = NATIVE_MUTEX_INIT;
//! //!
//! unsafe { //! unsafe {
//! lock.lock(); //! let _guard = lock.lock();
//! lock.unlock(); //! } // automatically unlocked here
//! }
//! //!
//! // Use a normally initialized mutex //! // Use a normally initialized mutex
//! let mut lock = Mutex::new();
//! unsafe { //! unsafe {
//! lock.lock(); //! let mut lock = StaticNativeMutex::new();
//! lock.unlock(); //!
//! // sometimes the RAII guard isn't appropriate
//! lock.lock_noguard();
//! lock.unlock_noguard();
//!
//! lock.destroy(); //! lock.destroy();
//! } //! }
@@ -50,7 +52,9 @@
use option::{Option, None, Some}; use option::{Option, None, Some};
use ops::Drop; use ops::Drop;
pub struct Mutex { /// A native mutex suitable for storing in statics (that is, it has
/// the `destroy` method rather than a destructor).
pub struct StaticNativeMutex {
priv inner: imp::Mutex, priv inner: imp::Mutex,
} }
@@ -62,33 +66,33 @@ pub struct Mutex {
/// then. /// then.
#[must_use] #[must_use]
pub struct LockGuard<'a> { pub struct LockGuard<'a> {
priv lock: &'a mut Mutex priv lock: &'a mut StaticNativeMutex
} }
pub static MUTEX_INIT: Mutex = Mutex { pub static NATIVE_MUTEX_INIT: StaticNativeMutex = StaticNativeMutex {
inner: imp::MUTEX_INIT, inner: imp::MUTEX_INIT,
}; };
impl Mutex { impl StaticNativeMutex {
/// Creates a new mutex /// Creates a new mutex.
pub unsafe fn new() -> Mutex { ///
Mutex { inner: imp::Mutex::new() } /// Note that a mutex created in this way needs to be explicit
/// freed with a call to `destroy` or it will leak.
pub unsafe fn new() -> StaticNativeMutex {
StaticNativeMutex { inner: imp::Mutex::new() }
} }
/// Acquires this lock. This assumes that the current thread does not /// Acquires this lock. This assumes that the current thread does not
/// already hold the lock. /// already hold the lock.
/// ///
/// # Example /// # Example
/// ``` /// ```rust
/// use std::unstable::mutex::Mutex; /// use std::unstable::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
/// static mut LOCK: StaticNativeMutex = NATIVE_MUTEX_INIT;
/// unsafe { /// unsafe {
/// let mut lock = Mutex::new(); /// let _guard = LOCK.lock();
///
/// {
/// let _guard = lock.lock();
/// // critical section... /// // critical section...
/// } // automatically unlocked in `_guard`'s destructor /// } // automatically unlocked in `_guard`'s destructor
/// }
/// ``` /// ```
pub unsafe fn lock<'a>(&'a mut self) -> LockGuard<'a> { pub unsafe fn lock<'a>(&'a mut self) -> LockGuard<'a> {
self.inner.lock(); self.inner.lock();
@@ -455,12 +459,12 @@ mod test {
use prelude::*; use prelude::*;
use mem::drop; use mem::drop;
use super::{Mutex, MUTEX_INIT}; use super::{StaticNativeMutex, NATIVE_MUTEX_INIT};
use rt::thread::Thread; use rt::thread::Thread;
#[test] #[test]
fn smoke_lock() { fn smoke_lock() {
static mut lock: Mutex = MUTEX_INIT; static mut lock: StaticNativeMutex = NATIVE_MUTEX_INIT;
unsafe { unsafe {
let _guard = lock.lock(); let _guard = lock.lock();
} }
@@ -468,7 +472,7 @@ mod test {
#[test] #[test]
fn smoke_cond() { fn smoke_cond() {
static mut lock: Mutex = MUTEX_INIT; static mut lock: StaticNativeMutex = NATIVE_MUTEX_INIT;
unsafe { unsafe {
let mut guard = lock.lock(); let mut guard = lock.lock();
let t = Thread::start(proc() { let t = Thread::start(proc() {
@@ -484,7 +488,7 @@ mod test {
#[test] #[test]
fn smoke_lock_noguard() { fn smoke_lock_noguard() {
static mut lock: Mutex = MUTEX_INIT; static mut lock: StaticNativeMutex = NATIVE_MUTEX_INIT;
unsafe { unsafe {
lock.lock_noguard(); lock.lock_noguard();
lock.unlock_noguard(); lock.unlock_noguard();
@@ -493,7 +497,7 @@ mod test {
#[test] #[test]
fn smoke_cond_noguard() { fn smoke_cond_noguard() {
static mut lock: Mutex = MUTEX_INIT; static mut lock: StaticNativeMutex = NATIVE_MUTEX_INIT;
unsafe { unsafe {
lock.lock_noguard(); lock.lock_noguard();
let t = Thread::start(proc() { let t = Thread::start(proc() {
@@ -511,7 +515,7 @@ mod test {
#[test] #[test]
fn destroy_immediately() { fn destroy_immediately() {
unsafe { unsafe {
let mut m = Mutex::new(); let mut m = StaticNativeMutex::new();
m.destroy(); m.destroy();
} }
} }

View File

@@ -13,10 +13,10 @@ use kinds::Send;
use ops::Drop; use ops::Drop;
use option::Option; use option::Option;
use sync::arc::UnsafeArc; use sync::arc::UnsafeArc;
use unstable::mutex::{Mutex, LockGuard}; use unstable::mutex::{StaticNativeMutex, LockGuard};
pub struct LittleLock { pub struct LittleLock {
priv l: Mutex, priv l: StaticNativeMutex,
} }
pub struct LittleGuard<'a> { pub struct LittleGuard<'a> {
@@ -31,7 +31,7 @@ impl Drop for LittleLock {
impl LittleLock { impl LittleLock {
pub fn new() -> LittleLock { pub fn new() -> LittleLock {
unsafe { LittleLock { l: Mutex::new() } } unsafe { LittleLock { l: StaticNativeMutex::new() } }
} }
pub unsafe fn lock<'a>(&'a mut self) -> LittleGuard<'a> { pub unsafe fn lock<'a>(&'a mut self) -> LittleGuard<'a> {

View File

@@ -133,7 +133,7 @@ pub struct StaticMutex {
/// uint-cast of the native thread waiting for this mutex /// uint-cast of the native thread waiting for this mutex
priv native_blocker: uint, priv native_blocker: uint,
/// an OS mutex used by native threads /// an OS mutex used by native threads
priv lock: mutex::Mutex, priv lock: mutex::StaticNativeMutex,
/// A concurrent mpsc queue used by green threads, along with a count used /// A concurrent mpsc queue used by green threads, along with a count used
/// to figure out when to dequeue and enqueue. /// to figure out when to dequeue and enqueue.
@@ -150,7 +150,7 @@ pub struct Guard<'a> {
/// Static initialization of a mutex. This constant can be used to initialize /// Static initialization of a mutex. This constant can be used to initialize
/// other mutex constants. /// other mutex constants.
pub static MUTEX_INIT: StaticMutex = StaticMutex { pub static MUTEX_INIT: StaticMutex = StaticMutex {
lock: mutex::MUTEX_INIT, lock: mutex::NATIVE_MUTEX_INIT,
state: atomics::INIT_ATOMIC_UINT, state: atomics::INIT_ATOMIC_UINT,
flavor: Unlocked, flavor: Unlocked,
green_blocker: 0, green_blocker: 0,
@@ -441,7 +441,7 @@ impl Mutex {
native_blocker: 0, native_blocker: 0,
green_cnt: atomics::AtomicUint::new(0), green_cnt: atomics::AtomicUint::new(0),
q: q::Queue::new(), q: q::Queue::new(),
lock: unsafe { mutex::Mutex::new() }, lock: unsafe { mutex::StaticNativeMutex::new() },
} }
} }
} }