Skip to content

Commit 65ce505

Browse files
committed
std::rt: require known stack bounds for all tasks.
We just approximate with a 1 or 2 MB stack for native::start.
1 parent 1f1838e commit 65ce505

File tree

6 files changed

+36
-15
lines changed

6 files changed

+36
-15
lines changed

src/libgreen/simple.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl Runtime for SimpleTask {
7575
fail!()
7676
}
7777
fn local_io<'a>(&'a mut self) -> Option<rtio::LocalIo<'a>> { None }
78-
fn stack_bounds(&self) -> Option<(uint, uint)> { None }
78+
fn stack_bounds(&self) -> (uint, uint) { fail!() }
7979
fn wrap(~self) -> ~Any { fail!() }
8080
}
8181

src/libgreen/task.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -450,11 +450,12 @@ impl Runtime for GreenTask {
450450
}
451451
}
452452

453-
fn stack_bounds(&self) -> Option<(uint, uint)> {
454-
self.coroutine.as_ref().map(|c| {
455-
(c.current_stack_segment.start() as uint,
456-
c.current_stack_segment.end() as uint)
457-
})
453+
fn stack_bounds(&self) -> (uint, uint) {
454+
let c = self.coroutine.as_ref()
455+
.expect("GreenTask.stack_bounds called without a coroutine");
456+
457+
(c.current_stack_segment.start() as uint,
458+
c.current_stack_segment.end() as uint)
458459
}
459460

460461
fn wrap(~self) -> ~Any { self as ~Any }

src/libnative/lib.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ mod bookeeping;
3333
pub mod io;
3434
pub mod task;
3535

36+
#[cfg(windows)]
37+
#[cfg(android)]
38+
static OS_DEFAULT_STACK_ESTIMATE: uint = 1 << 20;
39+
#[cfg(unix, not(android))]
40+
static OS_DEFAULT_STACK_ESTIMATE: uint = 2 * (1 << 20);
41+
42+
3643
// XXX: this should not exist here
3744
#[cfg(stage0)]
3845
#[lang = "start"]
@@ -66,10 +73,19 @@ pub fn lang_start(main: *u8, argc: int, argv: **u8) -> int {
6673
/// This function will only return once *all* native threads in the system have
6774
/// exited.
6875
pub fn start(argc: int, argv: **u8, main: proc()) -> int {
76+
let something_around_the_top_of_the_stack = 1;
77+
let addr = &something_around_the_top_of_the_stack as *int;
78+
let my_stack_top = addr as uint;
79+
80+
// FIXME #11359 we just assume that this thread has a stack of a
81+
// certain size, and estimate that there's at most 20KB of stack
82+
// frames above our current position.
83+
let my_stack_bottom = my_stack_top + 20000 - OS_DEFAULT_STACK_ESTIMATE;
84+
6985
rt::init(argc, argv);
7086
let mut exit_code = None;
7187
let mut main = Some(main);
72-
task::new().run(|| {
88+
task::new((my_stack_bottom, my_stack_top)).run(|| {
7389
exit_code = Some(run(main.take_unwrap()));
7490
});
7591
unsafe { rt::cleanup(); }

src/libnative/task.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@ use task;
3030
use bookeeping;
3131

3232
/// Creates a new Task which is ready to execute as a 1:1 task.
33-
pub fn new() -> ~Task {
33+
pub fn new(stack_bounds: (uint, uint)) -> ~Task {
3434
let mut task = ~Task::new();
35-
task.put_runtime(ops() as ~rt::Runtime);
35+
let mut ops = ops();
36+
ops.stack_bounds = stack_bounds;
37+
task.put_runtime(ops as ~rt::Runtime);
3638
return task;
3739
}
3840

@@ -41,7 +43,8 @@ fn ops() -> ~Ops {
4143
lock: unsafe { Mutex::new() },
4244
awoken: false,
4345
io: io::IoFactory::new(),
44-
stack_bounds: None,
46+
// these *should* get overwritten
47+
stack_bounds: (0, 0),
4548
}
4649
}
4750

@@ -91,7 +94,7 @@ pub fn spawn_opts(opts: TaskOpts, f: proc()) {
9194
stack::record_stack_bounds(my_stack - stack + 1024, my_stack);
9295
}
9396
let mut ops = ops;
94-
ops.stack_bounds = Some((my_stack - stack + 1024, my_stack));
97+
ops.stack_bounds = (my_stack - stack + 1024, my_stack);
9598

9699
let mut f = Some(f);
97100
let mut task = task;
@@ -111,7 +114,7 @@ struct Ops {
111114
// This field holds the known bounds of the stack in (lo, hi) form. Not all
112115
// native tasks necessarily know their precise bounds, hence this is
113116
// optional.
114-
stack_bounds: Option<(uint, uint)>,
117+
stack_bounds: (uint, uint),
115118
}
116119

117120
impl rt::Runtime for Ops {
@@ -133,7 +136,7 @@ impl rt::Runtime for Ops {
133136
self as ~Any
134137
}
135138

136-
fn stack_bounds(&self) -> Option<(uint, uint)> { self.stack_bounds }
139+
fn stack_bounds(&self) -> (uint, uint) { self.stack_bounds }
137140

138141
// This function gets a little interesting. There are a few safety and
139142
// ownership violations going on here, but this is all done in the name of

src/libstd/rt/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ pub trait Runtime {
159159
// you're in.
160160
fn spawn_sibling(~self, cur_task: ~Task, opts: TaskOpts, f: proc());
161161
fn local_io<'a>(&'a mut self) -> Option<rtio::LocalIo<'a>>;
162-
fn stack_bounds(&self) -> Option<(uint, uint)>; // (lo, hi)
162+
/// The (low, high) edges of the current stack.
163+
fn stack_bounds(&self) -> (uint, uint); // (lo, hi)
163164

164165
// XXX: This is a serious code smell and this should not exist at all.
165166
fn wrap(~self) -> ~Any;

src/libstd/rt/task.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ impl Task {
285285
/// Returns the stack bounds for this task in (lo, hi) format. The stack
286286
/// bounds may not be known for all tasks, so the return value may be
287287
/// `None`.
288-
pub fn stack_bounds(&self) -> Option<(uint, uint)> {
288+
pub fn stack_bounds(&self) -> (uint, uint) {
289289
self.imp.get_ref().stack_bounds()
290290
}
291291
}

0 commit comments

Comments
 (0)