Skip to content

Commit b8d1fa3

Browse files
committed
auto merge of #8645 : alexcrichton/rust/issue-6436-run-non-blocking, r=brson
This overhauls `std::run` to instead run on top of libuv. This is *not* in a mergeable state, I've been attempting to diagnose failures in the compiletest suite. I've managed to find a fair number of bugs so far, but I still don't seem to be done yet. Notable changes: * This requires upgrading libuv. From the discussion on #6567, I took libuv master from a few days ago, applied one patch to fix process spawning with multiple event loops in libuv, and pushed to my own fork * The build system for libuv has changed since we last used it. There's some extra checkout from a google build system which apparently does all the magic if you don't want to require autotools, and the google system just requires python. I updated the Makefile to get this build system and build libuv with it instead. This is untested on windows and arm, and both will probably need to see some improvement. * This required adding some pipe bindings to libuv as well. Currently the support is pretty simple and probably completely unsafe for pipes, but you at least get read/write methods. This is necessary for capturing output of processes. * I didn't redesign `std::run` at all, I simply tried to reimplement all the existing functionality on top of libuv. Some functions ended up dying, but nothing major. All uses of `std::run` in the compiler still work just fine. I'm not quite sure how the rest of the runtime deals with this, but I marked process structures as `no_send` because the waiting/waking up has to happen in the same event loop right now. If processes start migrating between event loops then very bad things can happen. This may be what threadsafe I/O would fix, and I would be more than willing to rebase on that if it lands first. Anyway, for now I wanted to put this up for review, I'm still investigating the corruption/deadlock bugs, but this is in an *almost* workable state. Once I find the bugs I'll also rebase on the current master.
2 parents f22b4b1 + 4635644 commit b8d1fa3

27 files changed

+1151
-1126
lines changed

.gitmodules

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
branch = master
55
[submodule "src/libuv"]
66
path = src/libuv
7-
url = https://github.com/brson/libuv.git
7+
url = https://github.com/alexcrichton/libuv.git
88
branch = master

mk/rt.mk

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -163,34 +163,49 @@ LIBUV_DEPS := $$(wildcard \
163163
$$(S)src/libuv/*/*/*/*)
164164
endif
165165

166+
LIBUV_GYP := $$(S)src/libuv/build/gyp
167+
LIBUV_MAKEFILE_$(1)_$(2) := $$(CFG_BUILD_DIR)rt/$(1)/stage$(2)/libuv/Makefile
168+
LIBUV_NO_LOAD = run-benchmarks.target.mk run-tests.target.mk \
169+
uv_dtrace_header.target.mk uv_dtrace_provider.target.mk
170+
171+
$$(LIBUV_MAKEFILE_$(1)_$(2)): $$(LIBUV_GYP)
172+
(cd $(S)src/libuv/ && \
173+
./gyp_uv -f make -Dtarget_arch=$$(HOST_$(1)) -D ninja \
174+
-Goutput_dir=$$(@D) --generator-output $$(@D))
175+
166176
# XXX: Shouldn't need platform-specific conditions here
167177
ifdef CFG_WINDOWSY_$(1)
168178
$$(LIBUV_LIB_$(1)_$(2)): $$(LIBUV_DEPS)
169-
$$(Q)$$(MAKE) -C $$(S)src/libuv/ \
170-
builddir_name="$$(CFG_BUILD_DIR)/rt/$(1)/stage$(2)/libuv" \
171-
OS=mingw \
179+
$$(Q)rm -f $$(S)src/libuv/libuv.a
180+
$$(Q)$$(MAKE) -C $$(S)src/libuv -f Makefile.mingw \
181+
CFLAGS="$$(CFG_GCCISH_CFLAGS) $$(LIBUV_FLAGS_$$(HOST_$(1))) $$(SNAP_DEFINES)" \
182+
AR="$$(AR_$(1))" \
172183
V=$$(VERBOSE)
184+
$$(Q)cp $$(S)src/libuv/libuv.a $$@
173185
else ifeq ($(OSTYPE_$(1)), linux-androideabi)
174-
$$(LIBUV_LIB_$(1)_$(2)): $$(LIBUV_DEPS)
175-
$$(Q)$$(MAKE) -C $$(S)src/libuv/ \
186+
$$(LIBUV_LIB_$(1)_$(2)): $$(LIBUV_DEPS) $$(LIBUV_MAKEFILE_$(1)_$(2))
187+
$$(Q)$$(MAKE) -C $$(@D) \
176188
CFLAGS="$$(CFG_GCCISH_CFLAGS) $$(LIBUV_FLAGS_$$(HOST_$(1))) $$(SNAP_DEFINES)" \
177189
LDFLAGS="$$(CFG_GCCISH_LINK_FLAGS) $$(LIBUV_FLAGS_$$(HOST_$(1)))" \
178190
CC="$$(CC_$(1))" \
179191
CXX="$$(CXX_$(1))" \
180192
AR="$$(AR_$(1))" \
181-
BUILDTYPE=Release \
182-
builddir_name="$$(CFG_BUILD_DIR)/rt/$(1)/stage$(2)/libuv" \
183193
host=android OS=linux \
194+
builddir="." \
195+
BUILDTYPE=Release \
196+
NO_LOAD="$$(LIBUV_NO_LOAD)" \
184197
V=$$(VERBOSE)
185198
else
186-
$$(LIBUV_LIB_$(1)_$(2)): $$(LIBUV_DEPS)
187-
$$(Q)$$(MAKE) -C $$(S)src/libuv/ \
199+
$$(LIBUV_LIB_$(1)_$(2)): $$(LIBUV_DEPS) $$(LIBUV_MAKEFILE_$(1)_$(2))
200+
$$(Q)$$(MAKE) -C $$(@D) \
188201
CFLAGS="$$(CFG_GCCISH_CFLAGS) $$(LIBUV_FLAGS_$$(HOST_$(1))) $$(SNAP_DEFINES)" \
189202
LDFLAGS="$$(CFG_GCCISH_LINK_FLAGS) $$(LIBUV_FLAGS_$$(HOST_$(1)))" \
190203
CC="$$(CC_$(1))" \
191204
CXX="$$(CXX_$(1))" \
192205
AR="$$(AR_$(1))" \
193-
builddir_name="$$(CFG_BUILD_DIR)/rt/$(1)/stage$(2)/libuv" \
206+
builddir="." \
207+
BUILDTYPE=Release \
208+
NO_LOAD="$$(LIBUV_NO_LOAD)" \
194209
V=$$(VERBOSE)
195210
endif
196211

@@ -254,3 +269,7 @@ endef
254269
$(foreach stage,$(STAGES), \
255270
$(foreach target,$(CFG_TARGET_TRIPLES), \
256271
$(eval $(call DEF_RUNTIME_TARGETS,$(target),$(stage)))))
272+
273+
$(LIBUV_GYP):
274+
mkdir -p $(S)src/libuv/build
275+
git clone https://git.chromium.org/external/gyp.git $(S)src/libuv/build/gyp

src/compiletest/procsrv.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ pub fn run(lib_path: &str,
5454
in_fd: None,
5555
out_fd: None,
5656
err_fd: None
57-
});
57+
}).unwrap();
5858

5959
for input in input.iter() {
60-
proc.input().write_str(*input);
60+
proc.input().write(input.as_bytes());
6161
}
6262
let output = proc.finish_with_output();
6363

src/compiletest/runtest.rs

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,41 +20,16 @@ use procsrv;
2020
use util;
2121
use util::logv;
2222

23-
use std::cell::Cell;
2423
use std::io;
2524
use std::os;
2625
use std::str;
27-
use std::task::{spawn_sched, SingleThreaded};
2826
use std::vec;
29-
use std::unstable::running_on_valgrind;
3027

3128
use extra::test::MetricMap;
3229

3330
pub fn run(config: config, testfile: ~str) {
34-
let config = Cell::new(config);
35-
let testfile = Cell::new(testfile);
36-
// FIXME #6436: Creating another thread to run the test because this
37-
// is going to call waitpid. The new scheduler has some strange
38-
// interaction between the blocking tasks and 'friend' schedulers
39-
// that destroys parallelism if we let normal schedulers block.
40-
// It should be possible to remove this spawn once std::run is
41-
// rewritten to be non-blocking.
42-
//
43-
// We do _not_ create another thread if we're running on V because
44-
// it serializes all threads anyways.
45-
if running_on_valgrind() {
46-
let config = config.take();
47-
let testfile = testfile.take();
48-
let mut _mm = MetricMap::new();
49-
run_metrics(config, testfile, &mut _mm);
50-
} else {
51-
do spawn_sched(SingleThreaded) {
52-
let config = config.take();
53-
let testfile = testfile.take();
54-
let mut _mm = MetricMap::new();
55-
run_metrics(config, testfile, &mut _mm);
56-
}
57-
}
31+
let mut _mm = MetricMap::new();
32+
run_metrics(config, testfile, &mut _mm);
5833
}
5934

6035
pub fn run_metrics(config: config, testfile: ~str, mm: &mut MetricMap) {

src/librustdoc/markdown_writer.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,14 @@ fn pandoc_writer(
104104
];
105105
106106
do generic_writer |markdown| {
107-
use std::io::WriterUtil;
108-
109107
debug!("pandoc cmd: %s", pandoc_cmd);
110108
debug!("pandoc args: %s", pandoc_args.connect(" "));
111109

112-
let mut proc = run::Process::new(pandoc_cmd, pandoc_args, run::ProcessOptions::new());
110+
let proc = run::Process::new(pandoc_cmd, pandoc_args,
111+
run::ProcessOptions::new());
112+
let mut proc = proc.unwrap();
113113

114-
proc.input().write_str(markdown);
114+
proc.input().write(markdown.as_bytes());
115115
let output = proc.finish_with_output();
116116

117117
debug!("pandoc result: %i", output.status);

src/librustpkg/source_control.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ pub fn git_clone_general(source: &str, target: &Path, v: &Version) -> bool {
8989

9090
fn process_output_in_cwd(prog: &str, args: &[~str], cwd: &Path) -> ProcessOutput {
9191
let mut prog = Process::new(prog, args, ProcessOptions{ dir: Some(cwd)
92-
,..ProcessOptions::new()});
92+
,..ProcessOptions::new()}).unwrap();
9393
prog.finish_with_output()
9494
}
9595

src/librustpkg/tests.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,14 @@ fn mk_temp_workspace(short_name: &Path, version: &Version) -> Path {
112112

113113
fn run_git(args: &[~str], env: Option<~[(~str, ~str)]>, cwd: &Path, err_msg: &str) {
114114
let cwd = (*cwd).clone();
115-
let mut prog = run::Process::new("git", args, run::ProcessOptions {
115+
let prog = run::Process::new("git", args, run::ProcessOptions {
116116
env: env,
117117
dir: Some(&cwd),
118118
in_fd: None,
119119
out_fd: None,
120120
err_fd: None
121121
});
122+
let mut prog = prog.unwrap();
122123
let rslt = prog.finish_with_output();
123124
if rslt.status != 0 {
124125
fail!("%s [git returned %?, output = %s, error = %s]", err_msg,
@@ -226,7 +227,7 @@ fn command_line_test_with_env(args: &[~str], cwd: &Path, env: Option<~[(~str, ~s
226227
in_fd: None,
227228
out_fd: None,
228229
err_fd: None
229-
});
230+
}).unwrap();
230231
let output = prog.finish_with_output();
231232
debug!("Output from command %s with args %? was %s {%s}[%?]",
232233
cmd, args, str::from_bytes(output.output),
@@ -1027,16 +1028,17 @@ fn test_extern_mod() {
10271028
test_sysroot().to_str(),
10281029
exec_file.to_str());
10291030
1030-
let mut prog = run::Process::new(rustc.to_str(), [main_file.to_str(),
1031-
~"--sysroot", test_sysroot().to_str(),
1032-
~"-o", exec_file.to_str()],
1033-
run::ProcessOptions {
1031+
let prog = run::Process::new(rustc.to_str(), [main_file.to_str(),
1032+
~"--sysroot", test_sysroot().to_str(),
1033+
~"-o", exec_file.to_str()],
1034+
run::ProcessOptions {
10341035
env: env,
10351036
dir: Some(&dir),
10361037
in_fd: None,
10371038
out_fd: None,
10381039
err_fd: None
10391040
});
1041+
let mut prog = prog.unwrap();
10401042
let outp = prog.finish_with_output();
10411043
if outp.status != 0 {
10421044
fail!("output was %s, error was %s",

src/libstd/rt/io/file.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,6 @@ pub struct FileStream {
7171
last_nread: int,
7272
}
7373

74-
impl FileStream {
75-
}
76-
7774
impl Reader for FileStream {
7875
fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
7976
match self.fd.read(buf) {

src/libstd/rt/io/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,9 @@ pub use self::extensions::WriterByteConversions;
268268
/// Synchronous, non-blocking file I/O.
269269
pub mod file;
270270

271+
/// Synchronous, in-memory I/O.
272+
pub mod pipe;
273+
271274
/// Synchronous, non-blocking network I/O.
272275
pub mod net {
273276
pub mod tcp;

src/libstd/rt/io/net/tcp.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rt::io::{io_error, read_error, EndOfFile};
1616
use rt::rtio::{IoFactory, IoFactoryObject,
1717
RtioSocket, RtioTcpListener,
1818
RtioTcpListenerObject, RtioTcpStream,
19-
RtioTcpStreamObject};
19+
RtioTcpStreamObject, RtioStream};
2020
use rt::local::Local;
2121

2222
pub struct TcpStream(~RtioTcpStreamObject);
@@ -69,7 +69,7 @@ impl TcpStream {
6969

7070
impl Reader for TcpStream {
7171
fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
72-
match (**self).read(buf) {
72+
match (***self).read(buf) {
7373
Ok(read) => Some(read),
7474
Err(ioerr) => {
7575
// EOF is indicated by returning None
@@ -86,7 +86,7 @@ impl Reader for TcpStream {
8686

8787
impl Writer for TcpStream {
8888
fn write(&mut self, buf: &[u8]) {
89-
match (**self).write(buf) {
89+
match (***self).write(buf) {
9090
Ok(_) => (),
9191
Err(ioerr) => io_error::cond.raise(ioerr),
9292
}
@@ -166,7 +166,7 @@ mod test {
166166
do run_in_newsched_task {
167167
let mut called = false;
168168
do io_error::cond.trap(|e| {
169-
assert!(e.kind == ConnectionRefused);
169+
assert_eq!(e.kind, ConnectionRefused);
170170
called = true;
171171
}).inside {
172172
let addr = SocketAddr { ip: Ipv4Addr(0, 0, 0, 0), port: 1 };

src/libstd/rt/io/pipe.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
//! Synchronous, in-memory pipes.
12+
//!
13+
//! Currently these aren't particularly useful, there only exists bindings
14+
//! enough so that pipes can be created to child processes.
15+
16+
use prelude::*;
17+
use super::{Reader, Writer};
18+
use rt::io::{io_error, read_error, EndOfFile};
19+
use rt::local::Local;
20+
use rt::rtio::{RtioPipeObject, RtioStream, IoFactoryObject, IoFactory};
21+
use rt::uv::pipe;
22+
23+
pub struct PipeStream(~RtioPipeObject);
24+
25+
impl PipeStream {
26+
/// Creates a new pipe initialized, but not bound to any particular
27+
/// source/destination
28+
pub fn new() -> Option<PipeStream> {
29+
let pipe = unsafe {
30+
let io: *mut IoFactoryObject = Local::unsafe_borrow();
31+
(*io).pipe_init(false)
32+
};
33+
match pipe {
34+
Ok(p) => Some(PipeStream(p)),
35+
Err(ioerr) => {
36+
io_error::cond.raise(ioerr);
37+
None
38+
}
39+
}
40+
}
41+
42+
/// Extracts the underlying libuv pipe to be bound to another source.
43+
pub fn uv_pipe(&self) -> pipe::Pipe {
44+
// Did someone say multiple layers of indirection?
45+
(**self).uv_pipe()
46+
}
47+
}
48+
49+
impl Reader for PipeStream {
50+
fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
51+
match (***self).read(buf) {
52+
Ok(read) => Some(read),
53+
Err(ioerr) => {
54+
// EOF is indicated by returning None
55+
if ioerr.kind != EndOfFile {
56+
read_error::cond.raise(ioerr);
57+
}
58+
return None;
59+
}
60+
}
61+
}
62+
63+
fn eof(&mut self) -> bool { fail!() }
64+
}
65+
66+
impl Writer for PipeStream {
67+
fn write(&mut self, buf: &[u8]) {
68+
match (***self).write(buf) {
69+
Ok(_) => (),
70+
Err(ioerr) => {
71+
io_error::cond.raise(ioerr);
72+
}
73+
}
74+
}
75+
76+
fn flush(&mut self) { fail!() }
77+
}

0 commit comments

Comments
 (0)