Skip to content

Commit f6401ba

Browse files
committed
core: Use a global lock instead of runtime lock for os::getenv, etc. rust-lang#4726
1 parent 36ad366 commit f6401ba

File tree

4 files changed

+35
-14
lines changed

4 files changed

+35
-14
lines changed

src/libcore/os.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -147,23 +147,25 @@ pub mod win32 {
147147

148148
/*
149149
Accessing environment variables is not generally threadsafe.
150-
This uses a per-runtime lock to serialize access.
151-
FIXME #4726: It would probably be appropriate to make this a real global
150+
Serialize access through a global lock.
152151
*/
153152
fn with_env_lock<T>(f: &fn() -> T) -> T {
154-
use unstable::global::global_data_clone_create;
155-
use unstable::sync::{Exclusive, exclusive};
156-
157-
struct SharedValue(());
158-
type ValueMutex = Exclusive<SharedValue>;
159-
fn key(_: ValueMutex) { }
153+
use unstable::finally::Finally;
160154

161155
unsafe {
162-
let lock: ValueMutex = global_data_clone_create(key, || {
163-
~exclusive(SharedValue(()))
164-
});
156+
return do (|| {
157+
rust_take_env_lock();
158+
f()
159+
}).finally {
160+
rust_drop_env_lock();
161+
};
162+
}
165163

166-
lock.with_imm(|_| f() )
164+
extern {
165+
#[fast_ffi]
166+
fn rust_take_env_lock();
167+
#[fast_ffi]
168+
fn rust_drop_env_lock();
167169
}
168170
}
169171

src/libuv

src/rt/rust_env.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// that might come from the environment is loaded here, once, during
1414
// init.
1515

16+
#include "sync/lock_and_signal.h"
1617
#include "rust_env.h"
1718

1819
// The environment variables that the runtime knows about
@@ -26,6 +27,18 @@
2627
#define RUST_DEBUG_MEM "RUST_DEBUG_MEM"
2728
#define RUST_DEBUG_BORROW "RUST_DEBUG_BORROW"
2829

30+
static lock_and_signal env_lock;
31+
32+
extern "C" CDECL void
33+
rust_take_env_lock() {
34+
env_lock.lock();
35+
}
36+
37+
extern "C" CDECL void
38+
rust_drop_env_lock() {
39+
env_lock.unlock();
40+
}
41+
2942
#if defined(__WIN32__)
3043
static int
3144
get_num_cpus() {
@@ -119,6 +132,8 @@ copyenv(const char* name) {
119132

120133
rust_env*
121134
load_env(int argc, char **argv) {
135+
scoped_lock with(env_lock);
136+
122137
rust_env *env = (rust_env*)malloc(sizeof(rust_env));
123138

124139
env->num_sched_threads = (size_t)get_num_threads();
@@ -141,3 +156,4 @@ free_env(rust_env *env) {
141156
free(env->rust_seed);
142157
free(env);
143158
}
159+

src/rt/rustrt.def.in

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,4 +235,7 @@ rust_begin_unwind
235235
rust_take_task_borrow_list
236236
rust_set_task_borrow_list
237237
rust_valgrind_stack_register
238-
rust_valgrind_stack_deregister
238+
rust_valgrind_stack_deregister
239+
rust_take_env_lock
240+
rust_drop_env_lock
241+

0 commit comments

Comments
 (0)