Skip to content

Commit 417b2ac

Browse files
committed
core: Use a global lock instead of runtime lock for os::getenv, etc. rust-lang#4726
1 parent 333e74a commit 417b2ac

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
lines changed

src/libcore/os.rs

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

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

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

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

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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,5 @@ rust_try
234234
rust_begin_unwind
235235
rust_take_task_borrow_list
236236
rust_set_task_borrow_list
237+
rust_take_env_lock
238+
rust_drop_env_lock

0 commit comments

Comments
 (0)