Skip to content

Commit 33448ed

Browse files
committed
---
yaml --- r: 63475 b: refs/heads/snap-stage3 c: e1555f9 h: refs/heads/master i: 63473: 6fea534 63471: ab7fe74 v: v3
1 parent 3d91604 commit 33448ed

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
refs/heads/master: 2d28d645422c1617be58c8ca7ad9a457264ca850
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
4-
refs/heads/snap-stage3: 5722c953e5180ae3e086b4354f65ee8b5fb8d868
4+
refs/heads/snap-stage3: e1555f9b5628af2b6c6ed344cad621399cb7684d
55
refs/heads/try: 7b78b52e602bb3ea8174f9b2006bff3315f03ef9
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b

branches/snap-stage3/src/libstd/rt/mod.rs

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ use cell::Cell;
6363
use clone::Clone;
6464
use container::Container;
6565
use from_str::FromStr;
66+
use iter::Times;
6667
use iterator::IteratorUtil;
6768
use option::{Some, None};
6869
use os;
6970
use ptr::RawPtr;
70-
use uint;
7171
use rt::sched::{Scheduler, Coroutine, Shutdown};
7272
use rt::sleeper_list::SleeperList;
7373
use rt::task::Task;
@@ -150,7 +150,7 @@ pub mod local_ptr;
150150
/// Bindings to pthread/windows thread-local storage.
151151
pub mod thread_local_storage;
152152

153-
/// A concurrent data structure with which parent tasks wait on child tasks.
153+
/// For waiting on child tasks.
154154
pub mod join_latch;
155155

156156
pub mod metrics;
@@ -188,43 +188,58 @@ pub fn init(crate_map: *u8) {
188188
logging::init(crate_map);
189189
}
190190

191+
/// One-time runtime cleanup.
191192
pub fn cleanup() {
192193
global_heap::cleanup();
193194
}
194195

196+
/// Execute the main function in a scheduler.
197+
///
198+
/// Configures the runtime according to the environment, by default
199+
/// using a task scheduler with the same number of threads as cores.
200+
/// Returns a process exit code.
195201
pub fn run(main: ~fn()) -> int {
202+
196203
static DEFAULT_ERROR_CODE: int = 101;
197204

198205
let nthreads = match os::getenv("RUST_THREADS") {
199206
Some(nstr) => FromStr::from_str(nstr).get(),
200207
None => unsafe { util::num_cpus() }
201208
};
202209

210+
// The shared list of sleeping schedulers. Schedulers wake each other
211+
// occassionally to do new work.
203212
let sleepers = SleeperList::new();
213+
// The shared work queue. Temporary until work stealing is implemented.
204214
let work_queue = WorkQueue::new();
205215

206-
let mut handles = ~[];
216+
// The schedulers.
207217
let mut scheds = ~[];
218+
// Handles to the schedulers. When the main task ends these will be
219+
// sent the Shutdown message to terminate the schedulers.
220+
let mut handles = ~[];
208221

209-
for uint::range(0, nthreads) |_| {
222+
for nthreads.times {
223+
// Every scheduler is driven by an I/O event loop.
210224
let loop_ = ~UvEventLoop::new();
211225
let mut sched = ~Scheduler::new(loop_, work_queue.clone(), sleepers.clone());
212226
let handle = sched.make_handle();
213227

214-
handles.push(handle);
215228
scheds.push(sched);
229+
handles.push(handle);
216230
}
217231

232+
// Create a shared cell for transmitting the process exit
233+
// code from the main task to this function.
218234
let exit_code = UnsafeAtomicRcBox::new(AtomicInt::new(0));
219235
let exit_code_clone = exit_code.clone();
220236

221-
let main_cell = Cell::new(main);
237+
// When the main task exits, after all the tasks in the main
238+
// task tree, shut down the schedulers and set the exit code.
222239
let handles = Cell::new(handles);
223-
let mut new_task = ~Task::new_root();
224240
let on_exit: ~fn(bool) = |exit_success| {
225241

226242
let mut handles = handles.take();
227-
// Tell schedulers to exit
228243
for handles.mut_iter().advance |handle| {
229244
handle.send(Shutdown);
230245
}
@@ -234,13 +249,17 @@ pub fn run(main: ~fn()) -> int {
234249
(*exit_code_clone.get()).store(exit_code, SeqCst);
235250
}
236251
};
252+
253+
// Create and enqueue the main task.
254+
let main_cell = Cell::new(main);
255+
let mut new_task = ~Task::new_root();
237256
new_task.on_exit = Some(on_exit);
238257
let main_task = ~Coroutine::with_task(&mut scheds[0].stack_pool,
239258
new_task, main_cell.take());
240259
scheds[0].enqueue_task(main_task);
241260

261+
// Run each scheduler in a thread.
242262
let mut threads = ~[];
243-
244263
while !scheds.is_empty() {
245264
let sched = scheds.pop();
246265
let sched_cell = Cell::new(sched);
@@ -253,8 +272,9 @@ pub fn run(main: ~fn()) -> int {
253272
}
254273

255274
// Wait for schedulers
256-
let _threads = threads;
275+
{ let _threads = threads; }
257276

277+
// Return the exit code
258278
unsafe {
259279
(*exit_code.get()).load(SeqCst)
260280
}

0 commit comments

Comments
 (0)