Skip to content

Commit

Permalink
core: Use a global lock instead of runtime lock for os::getenv, etc. r…
Browse files Browse the repository at this point in the history
  • Loading branch information
brson committed May 15, 2013
1 parent 36ad366 commit f6401ba
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 14 deletions.
26 changes: 14 additions & 12 deletions src/libcore/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,23 +147,25 @@ pub mod win32 {

/*
Accessing environment variables is not generally threadsafe.
This uses a per-runtime lock to serialize access.
FIXME #4726: It would probably be appropriate to make this a real global
Serialize access through a global lock.
*/
fn with_env_lock<T>(f: &fn() -> T) -> T {
use unstable::global::global_data_clone_create;
use unstable::sync::{Exclusive, exclusive};

struct SharedValue(());
type ValueMutex = Exclusive<SharedValue>;
fn key(_: ValueMutex) { }
use unstable::finally::Finally;

unsafe {
let lock: ValueMutex = global_data_clone_create(key, || {
~exclusive(SharedValue(()))
});
return do (|| {
rust_take_env_lock();
f()
}).finally {
rust_drop_env_lock();
};
}

lock.with_imm(|_| f() )
extern {
#[fast_ffi]
fn rust_take_env_lock();
#[fast_ffi]
fn rust_drop_env_lock();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/libuv
Submodule libuv updated 1 files
+5 −6 src/unix/linux-core.c
16 changes: 16 additions & 0 deletions src/rt/rust_env.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// that might come from the environment is loaded here, once, during
// init.

#include "sync/lock_and_signal.h"
#include "rust_env.h"

// The environment variables that the runtime knows about
Expand All @@ -26,6 +27,18 @@
#define RUST_DEBUG_MEM "RUST_DEBUG_MEM"
#define RUST_DEBUG_BORROW "RUST_DEBUG_BORROW"

static lock_and_signal env_lock;

extern "C" CDECL void
rust_take_env_lock() {
env_lock.lock();
}

extern "C" CDECL void
rust_drop_env_lock() {
env_lock.unlock();
}

#if defined(__WIN32__)
static int
get_num_cpus() {
Expand Down Expand Up @@ -119,6 +132,8 @@ copyenv(const char* name) {

rust_env*
load_env(int argc, char **argv) {
scoped_lock with(env_lock);

rust_env *env = (rust_env*)malloc(sizeof(rust_env));

env->num_sched_threads = (size_t)get_num_threads();
Expand All @@ -141,3 +156,4 @@ free_env(rust_env *env) {
free(env->rust_seed);
free(env);
}

5 changes: 4 additions & 1 deletion src/rt/rustrt.def.in
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,7 @@ rust_begin_unwind
rust_take_task_borrow_list
rust_set_task_borrow_list
rust_valgrind_stack_register
rust_valgrind_stack_deregister
rust_valgrind_stack_deregister
rust_take_env_lock
rust_drop_env_lock

0 comments on commit f6401ba

Please sign in to comment.