Convert relevant static mutexes to Once

This commit is contained in:
Alex Crichton 2013-12-28 19:44:52 -08:00
parent f3370295b7
commit c22fed9424
7 changed files with 41 additions and 65 deletions

View File

@ -164,19 +164,14 @@ pub fn init() {
}
unsafe {
use std::unstable::mutex::{Mutex, MUTEX_INIT};
static mut LOCK: Mutex = MUTEX_INIT;
static mut INITIALIZED: bool = false;
if INITIALIZED { return }
LOCK.lock();
if !INITIALIZED {
use std::unstable::mutex::{Once, ONCE_INIT};
static mut INIT: Once = ONCE_INIT;
INIT.doit(|| {
let mut data: WSADATA = intrinsics::init();
let ret = WSAStartup(0x202, // version 2.2
&mut data);
assert_eq!(ret, 0);
INITIALIZED = true;
}
LOCK.unlock();
});
}
}

View File

@ -310,9 +310,8 @@ pub mod write {
}
unsafe fn configure_llvm(sess: Session) {
use std::unstable::mutex::{MUTEX_INIT, Mutex};
static mut LOCK: Mutex = MUTEX_INIT;
static mut CONFIGURED: bool = false;
use std::unstable::mutex::{Once, ONCE_INIT};
static mut INIT: Once = ONCE_INIT;
// Copy what clan does by turning on loop vectorization at O2 and
// slp vectorization at O3
@ -341,8 +340,7 @@ pub mod write {
add(*arg);
}
LOCK.lock();
if !CONFIGURED {
INIT.doit(|| {
llvm::LLVMInitializePasses();
// Only initialize the platforms supported by Rust here, because
@ -369,9 +367,7 @@ pub mod write {
llvm::LLVMRustSetLLVMOptions(llvm_args.len() as c_int,
llvm_args.as_ptr());
CONFIGURED = true;
}
LOCK.unlock();
});
}
unsafe fn populate_llvm_passes(fpm: lib::llvm::PassManagerRef,

View File

@ -1455,6 +1455,9 @@ pub mod llvm {
BufferName: *c_char)
-> MemoryBufferRef;
pub fn LLVMIsMultithreaded() -> Bool;
pub fn LLVMStartMultithreaded() -> Bool;
/** Returns a string describing the last error caused by an LLVMRust*
call. */
pub fn LLVMRustGetLastError() -> *c_char;
@ -1462,8 +1465,6 @@ pub mod llvm {
/// Print the pass timings since static dtors aren't picking them up.
pub fn LLVMRustPrintPassTimings();
pub fn LLVMRustStartMultithreading() -> bool;
pub fn LLVMStructCreateNamed(C: ContextRef, Name: *c_char) -> TypeRef;
pub fn LLVMStructSetBody(StructTy: TypeRef,

View File

@ -3189,8 +3189,21 @@ pub fn trans_crate(sess: session::Session,
analysis: &CrateAnalysis,
output: &Path) -> CrateTranslation {
// Before we touch LLVM, make sure that multithreading is enabled.
if unsafe { !llvm::LLVMRustStartMultithreading() } {
sess.bug("couldn't enable multi-threaded LLVM");
unsafe {
use std::unstable::mutex::{Once, ONCE_INIT};
static mut INIT: Once = ONCE_INIT;
static mut POISONED: bool = false;
INIT.doit(|| {
if llvm::LLVMStartMultithreaded() != 1 {
// use an extra bool to make sure that all future usage of LLVM
// cannot proceed despite the Once not running more than once.
POISONED = true;
}
});
if POISONED {
sess.bug("couldn't enable multi-threaded LLVM");
}
}
let mut symbol_hasher = Sha256::new();

View File

@ -160,30 +160,20 @@ pub mod native {
use option::{Option, Some, None};
use ptr;
use tls = rt::thread_local_storage;
use unstable::mutex::{Mutex, MUTEX_INIT};
static mut LOCK: Mutex = MUTEX_INIT;
static mut INITIALIZED: bool = false;
static mut RT_TLS_KEY: tls::Key = -1;
/// Initialize the TLS key. Other ops will fail if this isn't executed
/// first.
pub fn init() {
unsafe {
LOCK.lock();
if !INITIALIZED {
tls::create(&mut RT_TLS_KEY);
INITIALIZED = true;
}
LOCK.unlock();
tls::create(&mut RT_TLS_KEY);
}
}
pub unsafe fn cleanup() {
rtassert!(INITIALIZED);
rtassert!(RT_TLS_KEY != -1);
tls::destroy(RT_TLS_KEY);
LOCK.destroy();
INITIALIZED = false;
}
/// Give a pointer to thread-local storage.

View File

@ -47,6 +47,7 @@
#[allow(non_camel_case_types)];
use int;
use libc::c_void;
use sync::atomics;
@ -339,15 +340,15 @@ mod imp {
/// ```
pub struct Once {
priv mutex: Mutex,
priv cnt: AtomicInt,
priv lock_cnt: AtomicInt,
priv cnt: atomics::AtomicInt,
priv lock_cnt: atomics::AtomicInt,
}
/// Initialization value for static `Once` values.
pub static ONCE_INIT: Once = Once {
mutex: MUTEX_INIT,
cnt: INIT_ATOMIC_INT,
lock_cnt: INIT_ATOMIC_INT,
cnt: atomics::INIT_ATOMIC_INT,
lock_cnt: atomics::INIT_ATOMIC_INT,
};
impl Once {
@ -388,11 +389,11 @@ impl Once {
// calling `doit` will return immediately before the initialization has
// completed.
let prev = self.cnt.fetch_add(1, SeqCst);
let prev = self.cnt.fetch_add(1, atomics::SeqCst);
if prev < 0 {
// Make sure we never overflow, we'll never have int::min_value
// simultaneous calls to `doit` to make this value go back to 0
self.cnt.store(int::min_value, SeqCst);
self.cnt.store(int::min_value, atomics::SeqCst);
return
}
@ -400,15 +401,15 @@ impl Once {
// otherwise we run the job and record how many people will try to grab
// this lock
unsafe { self.mutex.lock() }
if self.cnt.load(SeqCst) > 0 {
if self.cnt.load(atomics::SeqCst) > 0 {
f();
let prev = self.cnt.swap(int::min_value, SeqCst);
self.lock_cnt.store(prev, SeqCst);
let prev = self.cnt.swap(int::min_value, atomics::SeqCst);
self.lock_cnt.store(prev, atomics::SeqCst);
}
unsafe { self.mutex.unlock() }
// Last one out cleans up after everyone else, no leaks!
if self.lock_cnt.fetch_add(-1, SeqCst) == 1 {
if self.lock_cnt.fetch_add(-1, atomics::SeqCst) == 1 {
unsafe { self.mutex.destroy() }
}
}
@ -416,6 +417,8 @@ impl Once {
#[cfg(test)]
mod test {
use prelude::*;
use rt::thread::Thread;
use super::{ONCE_INIT, Once, Mutex, MUTEX_INIT};
use task;

View File

@ -149,28 +149,6 @@ extern "C" LLVMValueRef LLVMInlineAsm(LLVMTypeRef Ty,
IsAlignStack, (InlineAsm::AsmDialect) Dialect));
}
/**
* This function is intended to be a threadsafe interface into enabling a
* multithreaded LLVM. This is invoked at the start of the translation phase of
* compilation to ensure that LLVM is ready.
*
* All of trans properly isolates LLVM with the use of a different
* LLVMContextRef per task, thus allowing parallel compilation of different
* crates in the same process. At the time of this writing, the use case for
* this is unit tests for rusti, but there are possible other applications.
*/
extern "C" bool LLVMRustStartMultithreading() {
static Mutex lock;
bool ret = true;
assert(lock.acquire());
if (!LLVMIsMultithreaded()) {
ret = LLVMStartMultithreaded();
}
assert(lock.release());
return ret;
}
typedef DIBuilder* DIBuilderRef;
template<typename DIT>