Skip to content

Commit f65fa69

Browse files
committed
auto merge of #8304 : brson/rust/newrt, r=brson
Depends on #8303 and #8299.
2 parents 29099e4 + 35a0fc4 commit f65fa69

File tree

10 files changed

+101
-52
lines changed

10 files changed

+101
-52
lines changed

src/librustc/rustc.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,10 +300,18 @@ bug and need to present an error.
300300
*/
301301
pub fn monitor(f: ~fn(diagnostic::Emitter)) {
302302
use std::comm::*;
303+
304+
// XXX: This is a hack for newsched since it doesn't support split stacks.
305+
// rustc needs a lot of stack!
306+
static STACK_SIZE: uint = 4000000;
307+
303308
let (p, ch) = stream();
304309
let ch = SharedChan::new(ch);
305310
let ch_capture = ch.clone();
306-
match do task::try || {
311+
let mut task_builder = task::task();
312+
task_builder.supervised();
313+
task_builder.opts.stack_size = Some(STACK_SIZE);
314+
match do task_builder.try {
307315
let ch = ch_capture.clone();
308316
let ch_capture = ch.clone();
309317
// The 'diagnostics emitter'. Every error, warning, etc. should

src/libstd/rt/env.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010

1111
//! Runtime environment settings
1212
13+
use from_str::FromStr;
1314
use libc::{size_t, c_char, c_int};
15+
use option::{Some, None};
16+
use os;
17+
18+
// OLD RT stuff
1419

1520
pub struct Environment {
1621
/// The number of threads to use by default
@@ -47,3 +52,26 @@ pub fn get() -> &Environment {
4752
extern {
4853
fn rust_get_rt_env() -> &Environment;
4954
}
55+
56+
// NEW RT stuff
57+
58+
// Note that these are all accessed without any synchronization.
59+
// They are expected to be initialized once then left alone.
60+
61+
static mut MIN_STACK: uint = 2000000;
62+
63+
pub fn init() {
64+
unsafe {
65+
match os::getenv("RUST_MIN_STACK") {
66+
Some(s) => match FromStr::from_str(s) {
67+
Some(i) => MIN_STACK = i,
68+
None => ()
69+
},
70+
None => ()
71+
}
72+
}
73+
}
74+
75+
pub fn min_stack() -> uint {
76+
unsafe { MIN_STACK }
77+
}

src/libstd/rt/local.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ impl Local for IoFactoryObject {
126126

127127
#[cfg(test)]
128128
mod test {
129+
use option::None;
129130
use unstable::run_in_bare_thread;
130131
use rt::test::*;
131132
use super::*;
@@ -137,7 +138,7 @@ mod test {
137138
do run_in_bare_thread {
138139
local_ptr::init_tls_key();
139140
let mut sched = ~new_test_uv_sched();
140-
let task = ~Task::new_root(&mut sched.stack_pool, || {});
141+
let task = ~Task::new_root(&mut sched.stack_pool, None, || {});
141142
Local::put(task);
142143
let task: ~Task = Local::take();
143144
cleanup_task(task);
@@ -149,11 +150,11 @@ mod test {
149150
do run_in_bare_thread {
150151
local_ptr::init_tls_key();
151152
let mut sched = ~new_test_uv_sched();
152-
let task = ~Task::new_root(&mut sched.stack_pool, || {});
153+
let task = ~Task::new_root(&mut sched.stack_pool, None, || {});
153154
Local::put(task);
154155
let task: ~Task = Local::take();
155156
cleanup_task(task);
156-
let task = ~Task::new_root(&mut sched.stack_pool, || {});
157+
let task = ~Task::new_root(&mut sched.stack_pool, None, || {});
157158
Local::put(task);
158159
let task: ~Task = Local::take();
159160
cleanup_task(task);
@@ -166,7 +167,7 @@ mod test {
166167
do run_in_bare_thread {
167168
local_ptr::init_tls_key();
168169
let mut sched = ~new_test_uv_sched();
169-
let task = ~Task::new_root(&mut sched.stack_pool, || {});
170+
let task = ~Task::new_root(&mut sched.stack_pool, None, || {});
170171
Local::put(task);
171172

172173
unsafe {
@@ -182,7 +183,7 @@ mod test {
182183
do run_in_bare_thread {
183184
local_ptr::init_tls_key();
184185
let mut sched = ~new_test_uv_sched();
185-
let task = ~Task::new_root(&mut sched.stack_pool, || {});
186+
let task = ~Task::new_root(&mut sched.stack_pool, None, || {});
186187
Local::put(task);
187188

188189
let res = do Local::borrow::<Task,bool> |_task| {

src/libstd/rt/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ pub fn init(argc: int, argv: **u8, crate_map: *u8) {
212212
// Need to propagate the unsafety to `start`.
213213
unsafe {
214214
args::init(argc, argv);
215+
env::init();
215216
logging::init(crate_map);
216217
rust_update_gc_metadata(crate_map);
217218
}
@@ -330,8 +331,7 @@ fn run_(main: ~fn(), use_main_sched: bool) -> int {
330331
// In the case where we do not use a main_thread scheduler we
331332
// run the main task in one of our threads.
332333

333-
let mut main_task = ~Task::new_root(&mut scheds[0].stack_pool,
334-
main.take());
334+
let mut main_task = ~Task::new_root(&mut scheds[0].stack_pool, None, main.take());
335335
main_task.death.on_exit = Some(on_exit.take());
336336
let main_task_cell = Cell::new(main_task);
337337

@@ -351,7 +351,7 @@ fn run_(main: ~fn(), use_main_sched: bool) -> int {
351351
let sched_cell = Cell::new(sched);
352352
let thread = do Thread::start {
353353
let mut sched = sched_cell.take();
354-
let bootstrap_task = ~do Task::new_root(&mut sched.stack_pool) || {
354+
let bootstrap_task = ~do Task::new_root(&mut sched.stack_pool, None) || {
355355
rtdebug!("boostraping a non-primary scheduler");
356356
};
357357
sched.bootstrap(bootstrap_task);
@@ -368,7 +368,7 @@ fn run_(main: ~fn(), use_main_sched: bool) -> int {
368368
let mut main_sched = main_sched.get();
369369

370370
let home = Sched(main_sched.make_handle());
371-
let mut main_task = ~Task::new_root_homed(&mut main_sched.stack_pool,
371+
let mut main_task = ~Task::new_root_homed(&mut main_sched.stack_pool, None,
372372
home, main.take());
373373
main_task.death.on_exit = Some(on_exit.take());
374374
rtdebug!("boostrapping main_task");

src/libstd/rt/sched.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,7 @@ mod test {
833833
let mut sched = ~new_test_uv_sched();
834834
let sched_handle = sched.make_handle();
835835

836-
let mut task = ~do Task::new_root_homed(&mut sched.stack_pool,
836+
let mut task = ~do Task::new_root_homed(&mut sched.stack_pool, None,
837837
Sched(sched_handle)) {
838838
unsafe { *task_ran_ptr = true };
839839
assert!(Task::on_appropriate_sched());
@@ -893,21 +893,21 @@ mod test {
893893
// 3) task not homed, sched requeues
894894
// 4) task not home, send home
895895

896-
let task1 = ~do Task::new_root_homed(&mut special_sched.stack_pool,
896+
let task1 = ~do Task::new_root_homed(&mut special_sched.stack_pool, None,
897897
Sched(t1_handle)) || {
898898
rtassert!(Task::on_appropriate_sched());
899899
};
900900
rtdebug!("task1 id: **%u**", borrow::to_uint(task1));
901901

902-
let task2 = ~do Task::new_root(&mut normal_sched.stack_pool) {
902+
let task2 = ~do Task::new_root(&mut normal_sched.stack_pool, None) {
903903
rtassert!(Task::on_appropriate_sched());
904904
};
905905

906-
let task3 = ~do Task::new_root(&mut normal_sched.stack_pool) {
906+
let task3 = ~do Task::new_root(&mut normal_sched.stack_pool, None) {
907907
rtassert!(Task::on_appropriate_sched());
908908
};
909909

910-
let task4 = ~do Task::new_root_homed(&mut special_sched.stack_pool,
910+
let task4 = ~do Task::new_root_homed(&mut special_sched.stack_pool, None,
911911
Sched(t4_handle)) {
912912
rtassert!(Task::on_appropriate_sched());
913913
};
@@ -923,7 +923,7 @@ mod test {
923923
let port = Cell::new(port);
924924
let chan = Cell::new(chan);
925925

926-
let normal_task = ~do Task::new_root(&mut normal_sched.stack_pool) {
926+
let normal_task = ~do Task::new_root(&mut normal_sched.stack_pool, None) {
927927
rtdebug!("*about to submit task2*");
928928
Scheduler::run_task(task2.take());
929929
rtdebug!("*about to submit task4*");
@@ -938,7 +938,7 @@ mod test {
938938

939939
rtdebug!("normal task: %u", borrow::to_uint(normal_task));
940940

941-
let special_task = ~do Task::new_root(&mut special_sched.stack_pool) {
941+
let special_task = ~do Task::new_root(&mut special_sched.stack_pool, None) {
942942
rtdebug!("*about to submit task1*");
943943
Scheduler::run_task(task1.take());
944944
rtdebug!("*about to submit task3*");

src/libstd/rt/task.rs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use libc::{c_void, uintptr_t};
2020
use ptr;
2121
use prelude::*;
2222
use option::{Option, Some, None};
23+
use rt::env;
2324
use rt::kill::Death;
2425
use rt::local::Local;
2526
use rt::logging::StdErrLogger;
@@ -85,38 +86,40 @@ impl Task {
8586

8687
// A helper to build a new task using the dynamically found
8788
// scheduler and task. Only works in GreenTask context.
88-
pub fn build_homed_child(f: ~fn(), home: SchedHome) -> ~Task {
89+
pub fn build_homed_child(stack_size: Option<uint>, f: ~fn(), home: SchedHome) -> ~Task {
8990
let f = Cell::new(f);
9091
let home = Cell::new(home);
9192
do Local::borrow::<Task, ~Task> |running_task| {
9293
let mut sched = running_task.sched.take_unwrap();
9394
let new_task = ~running_task.new_child_homed(&mut sched.stack_pool,
95+
stack_size,
9496
home.take(),
9597
f.take());
9698
running_task.sched = Some(sched);
9799
new_task
98100
}
99101
}
100102

101-
pub fn build_child(f: ~fn()) -> ~Task {
102-
Task::build_homed_child(f, AnySched)
103+
pub fn build_child(stack_size: Option<uint>, f: ~fn()) -> ~Task {
104+
Task::build_homed_child(stack_size, f, AnySched)
103105
}
104106

105-
pub fn build_homed_root(f: ~fn(), home: SchedHome) -> ~Task {
107+
pub fn build_homed_root(stack_size: Option<uint>, f: ~fn(), home: SchedHome) -> ~Task {
106108
let f = Cell::new(f);
107109
let home = Cell::new(home);
108110
do Local::borrow::<Task, ~Task> |running_task| {
109111
let mut sched = running_task.sched.take_unwrap();
110112
let new_task = ~Task::new_root_homed(&mut sched.stack_pool,
111-
home.take(),
112-
f.take());
113+
stack_size,
114+
home.take(),
115+
f.take());
113116
running_task.sched = Some(sched);
114117
new_task
115118
}
116119
}
117120

118-
pub fn build_root(f: ~fn()) -> ~Task {
119-
Task::build_homed_root(f, AnySched)
121+
pub fn build_root(stack_size: Option<uint>, f: ~fn()) -> ~Task {
122+
Task::build_homed_root(stack_size, f, AnySched)
120123
}
121124

122125
pub fn new_sched_task() -> Task {
@@ -137,17 +140,20 @@ impl Task {
137140
}
138141

139142
pub fn new_root(stack_pool: &mut StackPool,
143+
stack_size: Option<uint>,
140144
start: ~fn()) -> Task {
141-
Task::new_root_homed(stack_pool, AnySched, start)
145+
Task::new_root_homed(stack_pool, stack_size, AnySched, start)
142146
}
143147

144148
pub fn new_child(&mut self,
145149
stack_pool: &mut StackPool,
150+
stack_size: Option<uint>,
146151
start: ~fn()) -> Task {
147-
self.new_child_homed(stack_pool, AnySched, start)
152+
self.new_child_homed(stack_pool, stack_size, AnySched, start)
148153
}
149154

150155
pub fn new_root_homed(stack_pool: &mut StackPool,
156+
stack_size: Option<uint>,
151157
home: SchedHome,
152158
start: ~fn()) -> Task {
153159
Task {
@@ -160,14 +166,15 @@ impl Task {
160166
death: Death::new(),
161167
destroyed: false,
162168
name: None,
163-
coroutine: Some(Coroutine::new(stack_pool, start)),
169+
coroutine: Some(Coroutine::new(stack_pool, stack_size, start)),
164170
sched: None,
165171
task_type: GreenTask(Some(~home))
166172
}
167173
}
168174

169175
pub fn new_child_homed(&mut self,
170176
stack_pool: &mut StackPool,
177+
stack_size: Option<uint>,
171178
home: SchedHome,
172179
start: ~fn()) -> Task {
173180
Task {
@@ -181,7 +188,7 @@ impl Task {
181188
death: self.death.new_child(),
182189
destroyed: false,
183190
name: None,
184-
coroutine: Some(Coroutine::new(stack_pool, start)),
191+
coroutine: Some(Coroutine::new(stack_pool, stack_size, start)),
185192
sched: None,
186193
task_type: GreenTask(Some(~home))
187194
}
@@ -325,11 +332,13 @@ impl Drop for Task {
325332

326333
impl Coroutine {
327334

328-
pub fn new(stack_pool: &mut StackPool, start: ~fn()) -> Coroutine {
329-
static MIN_STACK_SIZE: uint = 3000000; // XXX: Too much stack
330-
335+
pub fn new(stack_pool: &mut StackPool, stack_size: Option<uint>, start: ~fn()) -> Coroutine {
336+
let stack_size = match stack_size {
337+
Some(size) => size,
338+
None => env::min_stack()
339+
};
331340
let start = Coroutine::build_start_wrapper(start);
332-
let mut stack = stack_pool.take_segment(MIN_STACK_SIZE);
341+
let mut stack = stack_pool.take_segment(stack_size);
333342
let initial_context = Context::new(start, &mut stack);
334343
Coroutine {
335344
current_stack_segment: stack,

src/libstd/rt/test.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub fn run_in_newsched_task_core(f: ~fn()) {
5757
exit_handle.take().send(Shutdown);
5858
rtassert!(exit_status);
5959
};
60-
let mut task = ~Task::new_root(&mut sched.stack_pool, f);
60+
let mut task = ~Task::new_root(&mut sched.stack_pool, None, f);
6161
task.death.on_exit = Some(on_exit);
6262

6363
sched.bootstrap(task);
@@ -190,8 +190,7 @@ pub fn run_in_mt_newsched_task(f: ~fn()) {
190190

191191
rtassert!(exit_status);
192192
};
193-
let mut main_task = ~Task::new_root(&mut scheds[0].stack_pool,
194-
f.take());
193+
let mut main_task = ~Task::new_root(&mut scheds[0].stack_pool, None, f.take());
195194
main_task.death.on_exit = Some(on_exit);
196195

197196
let mut threads = ~[];
@@ -209,7 +208,7 @@ pub fn run_in_mt_newsched_task(f: ~fn()) {
209208

210209
while !scheds.is_empty() {
211210
let mut sched = scheds.pop();
212-
let bootstrap_task = ~do Task::new_root(&mut sched.stack_pool) || {
211+
let bootstrap_task = ~do Task::new_root(&mut sched.stack_pool, None) || {
213212
rtdebug!("bootstrapping non-primary scheduler");
214213
};
215214
let bootstrap_task_cell = Cell::new(bootstrap_task);
@@ -232,12 +231,12 @@ pub fn run_in_mt_newsched_task(f: ~fn()) {
232231

233232
/// Test tasks will abort on failure instead of unwinding
234233
pub fn spawntask(f: ~fn()) {
235-
Scheduler::run_task(Task::build_child(f));
234+
Scheduler::run_task(Task::build_child(None, f));
236235
}
237236

238237
/// Create a new task and run it right now. Aborts on failure
239238
pub fn spawntask_later(f: ~fn()) {
240-
Scheduler::run_task_later(Task::build_child(f));
239+
Scheduler::run_task_later(Task::build_child(None, f));
241240
}
242241

243242
pub fn spawntask_random(f: ~fn()) {
@@ -259,7 +258,7 @@ pub fn spawntask_try(f: ~fn()) -> Result<(),()> {
259258
let chan = Cell::new(chan);
260259
let on_exit: ~fn(bool) = |exit_status| chan.take().send(exit_status);
261260

262-
let mut new_task = Task::build_root(f);
261+
let mut new_task = Task::build_root(None, f);
263262
new_task.death.on_exit = Some(on_exit);
264263

265264
Scheduler::run_task(new_task);
@@ -285,7 +284,7 @@ pub fn spawntask_thread(f: ~fn()) -> Thread {
285284
pub fn with_test_task(blk: ~fn(~Task) -> ~Task) {
286285
do run_in_bare_thread {
287286
let mut sched = ~new_test_uv_sched();
288-
let task = blk(~Task::new_root(&mut sched.stack_pool, ||{}));
287+
let task = blk(~Task::new_root(&mut sched.stack_pool, None, ||{}));
289288
cleanup_task(task);
290289
}
291290
}

0 commit comments

Comments
 (0)