Skip to content

Add mutex to linenoise calls in std::rl #5312

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 7 additions & 22 deletions src/libcore/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ use str;
use task;
use uint;
use vec;
use unstable::global::with_global_lock;

pub use libc::fclose;
pub use os::consts::*;
Expand Down Expand Up @@ -151,23 +152,6 @@ 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
*/
fn with_env_lock<T>(f: &fn() -> T) -> T {
use unstable::global::global_data_clone_create;
use unstable::{Exclusive, exclusive};

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

unsafe {
let lock: ValueMutex = global_data_clone_create(key, || {
~exclusive(SharedValue(()))
});

lock.with_imm(|_| f() )
}
}

pub fn env() -> ~[(~str,~str)] {
unsafe {
#[cfg(windows)]
Expand Down Expand Up @@ -227,7 +211,8 @@ pub fn env() -> ~[(~str,~str)] {
}
pairs
}
do with_env_lock {

do with_global_lock {
let unparsed_environ = get_env_pairs();
env_convert(unparsed_environ)
}
Expand All @@ -237,7 +222,7 @@ pub fn env() -> ~[(~str,~str)] {
#[cfg(unix)]
pub fn getenv(n: &str) -> Option<~str> {
unsafe {
do with_env_lock {
do with_global_lock {
let s = str::as_c_str(n, |s| libc::getenv(s));
if ptr::null::<u8>() == cast::reinterpret_cast(&s) {
option::None::<~str>
Expand All @@ -252,7 +237,7 @@ pub fn getenv(n: &str) -> Option<~str> {
#[cfg(windows)]
pub fn getenv(n: &str) -> Option<~str> {
unsafe {
do with_env_lock {
do with_global_lock {
use os::win32::{as_utf16_p, fill_utf16_buf_and_decode};
do as_utf16_p(n) |u| {
do fill_utf16_buf_and_decode() |buf, sz| {
Expand All @@ -267,7 +252,7 @@ pub fn getenv(n: &str) -> Option<~str> {
#[cfg(unix)]
pub fn setenv(n: &str, v: &str) {
unsafe {
do with_env_lock {
do with_global_lock {
do str::as_c_str(n) |nbuf| {
do str::as_c_str(v) |vbuf| {
libc::funcs::posix01::unistd::setenv(nbuf, vbuf, 1);
Expand All @@ -281,7 +266,7 @@ pub fn setenv(n: &str, v: &str) {
#[cfg(windows)]
pub fn setenv(n: &str, v: &str) {
unsafe {
do with_env_lock {
do with_global_lock {
use os::win32::as_utf16_p;
do as_utf16_p(n) |nbuf| {
do as_utf16_p(v) |vbuf| {
Expand Down
14 changes: 14 additions & 0 deletions src/libcore/unstable/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,20 @@ impl Drop for GlobalState {
}
}

pub unsafe fn with_global_lock<T>(f: &fn() -> T) -> T {
struct SharedValue(());
type ValueMutex = Exclusive<SharedValue>;
fn key(_: ValueMutex) { }

unsafe {
let lock: ValueMutex = global_data_clone_create(key, || {
~exclusive(SharedValue(()))
});

lock.with_imm(|_| f() )
}
}

fn get_global_state() -> Exclusive<GlobalState> {

const POISON: int = -1;
Expand Down
31 changes: 23 additions & 8 deletions src/libstd/rl.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
Expand All @@ -15,6 +15,7 @@ use core::libc::{c_char, c_int};
use core::prelude::*;
use core::str;
use core::task;
use core::unstable::global::with_global_lock;

pub mod rustrt {
use core::libc::{c_char, c_int};
Expand All @@ -30,29 +31,43 @@ pub mod rustrt {
}
}

/*
The linenoise library is not threadsafe,
This uses a per-runtime lock to serialize access.
FIXME #4726: It would probably be appropriate to make this a real global
*/

/// Add a line to history
pub unsafe fn add_history(line: ~str) -> bool {
do str::as_c_str(line) |buf| {
rustrt::linenoiseHistoryAdd(buf) == 1 as c_int
do with_global_lock {
do str::as_c_str(line) |buf| {
rustrt::linenoiseHistoryAdd(buf) == 1 as c_int
}
}
}

/// Set the maximum amount of lines stored
pub unsafe fn set_history_max_len(len: int) -> bool {
rustrt::linenoiseHistorySetMaxLen(len as c_int) == 1 as c_int
do with_global_lock {
rustrt::linenoiseHistorySetMaxLen(len as c_int) == 1 as c_int
}
}

/// Save line history to a file
pub unsafe fn save_history(file: ~str) -> bool {
do str::as_c_str(file) |buf| {
rustrt::linenoiseHistorySave(buf) == 1 as c_int
do with_global_lock {
do str::as_c_str(file) |buf| {
rustrt::linenoiseHistorySave(buf) == 1 as c_int
}
}
}

/// Load line history from a file
pub unsafe fn load_history(file: ~str) -> bool {
do str::as_c_str(file) |buf| {
rustrt::linenoiseHistoryLoad(buf) == 1 as c_int
do with_global_lock {
do str::as_c_str(file) |buf| {
rustrt::linenoiseHistoryLoad(buf) == 1 as c_int
}
}
}

Expand Down