Skip to content

Commit 053b38e

Browse files
committed
core::rt: Fix two multithreading bugs and add a threadring test
This properly distributes the load now
1 parent 8eb358b commit 053b38e

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

src/libstd/rt/sched.rs

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ pub impl Scheduler {
131131

132132
let mut self_sched = self;
133133

134+
// Always run through the scheduler loop at least once so that
135+
// we enter the sleep state and can then be woken up by other
136+
// schedulers.
137+
self_sched.event_loop.callback(Scheduler::run_sched_once);
138+
134139
unsafe {
135140
let event_loop: *mut ~EventLoopObject = {
136141
let event_loop: *mut ~EventLoopObject = &mut self_sched.event_loop;
@@ -258,7 +263,7 @@ pub impl Scheduler {
258263
let mut handle = handle;
259264
handle.send(Wake);
260265
}
261-
None => (/* pass */)
266+
None => break
262267
}
263268
}
264269
}
@@ -781,4 +786,63 @@ mod test {
781786
}
782787
}
783788
}
789+
790+
#[test]
791+
fn thread_ring() {
792+
use rt::comm::*;
793+
use iter::Times;
794+
use vec::OwnedVector;
795+
use container::Container;
796+
use comm::{GenericPort, GenericChan};
797+
798+
do run_in_mt_newsched_task {
799+
let (end_port, end_chan) = oneshot();
800+
801+
let n_tasks = 10;
802+
let token = 2000;
803+
804+
let mut (p, ch1) = stream();
805+
ch1.send((token, end_chan));
806+
let mut i = 2;
807+
while i <= n_tasks {
808+
let (next_p, ch) = stream();
809+
let imm_i = i;
810+
let imm_p = p;
811+
do spawntask_random {
812+
roundtrip(imm_i, n_tasks, &imm_p, &ch);
813+
};
814+
p = next_p;
815+
i += 1;
816+
}
817+
let imm_p = p;
818+
let imm_ch = ch1;
819+
do spawntask_random {
820+
roundtrip(1, n_tasks, &imm_p, &imm_ch);
821+
}
822+
823+
end_port.recv();
824+
}
825+
826+
fn roundtrip(id: int, n_tasks: int,
827+
p: &Port<(int, ChanOne<()>)>, ch: &Chan<(int, ChanOne<()>)>) {
828+
while (true) {
829+
match p.recv() {
830+
(1, end_chan) => {
831+
debug!("%d\n", id);
832+
end_chan.send(());
833+
return;
834+
}
835+
(token, end_chan) => {
836+
debug!("thread: %d got token: %d", id, token);
837+
ch.send((token - 1, end_chan));
838+
if token <= n_tasks {
839+
return;
840+
}
841+
}
842+
}
843+
}
844+
}
845+
846+
}
847+
784848
}

src/libstd/rt/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub fn run_in_mt_newsched_task(f: ~fn()) {
6666
let f_cell = Cell(f);
6767

6868
do run_in_bare_thread {
69-
static N: uint = 2;
69+
static N: uint = 4;
7070

7171
let sleepers = SleeperList::new();
7272
let work_queue = WorkQueue::new();

0 commit comments

Comments
 (0)