Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 16 additions & 32 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
#![feature(unsafe_destructor)]

use std::any::Any;
use std::kinds::marker;
use std::mem::transmute;
use std::raw;
use std::sync::Future;
use std::task;
use std::thread;

mod test;

pub type TaskBody<'s> = ||:Sync+'s;
pub type TaskBody<'s> = &'s mut (FnMut() + Sync + Send);

pub struct Section<'s> {
marker: marker::ContravariantLifetime<'s>,
tasks: Vec<Future<Result<(), Box<Any+Send>>>>
tasks: Vec<thread::JoinGuard<'s, ()>>,
}

pub fn execute<'s>(closures: &'s mut [TaskBody<'s>]) {
pub fn execute<'s>(closures: &'s mut [TaskBody]) {
let mut join = Section::new();
for closure in closures.iter_mut() {
join.fork(closure);
Expand All @@ -26,38 +20,29 @@ pub fn execute<'s>(closures: &'s mut [TaskBody<'s>]) {

impl<'s> Section<'s> {
pub fn new() -> Section<'s> {
Section { marker: marker::ContravariantLifetime,
tasks: Vec::new() }
Section { tasks: Vec::new() }
}

pub fn fork(&mut self, body: &'s mut TaskBody<'s>) {
unsafe {
let body: *mut raw::Closure = transmute(body);
pub fn fork(&mut self, body: &'s mut TaskBody) {
// really don't want the `push` below to fail
// after task has been spawned
self.tasks.reserve(1);

// really don't want the `push` below to fail
// after task has been spawned
self.tasks.reserve_additional(1);
let future = thread::scoped(move || {
(*body)()
});

let future = task::try_future(proc() {
let body: &mut TaskBody = transmute(body);
(*body)()
});

// due to reserve above, should be infallible
self.tasks.push(future);
}
// due to reserve above, should be infallible
self.tasks.push(future);
}

pub fn sync(&mut self) {
loop {
match self.tasks.pop() {
None => { break; }
Some(task) => {
Some(joinguard) => {
// propoagate any failure
match task.unwrap() {
Ok(()) => { }
Err(_) => { fail!() }
}
joinguard.join();
}
}
}
Expand All @@ -70,4 +55,3 @@ impl<'s> Drop for Section<'s> {
self.sync();
}
}

49 changes: 38 additions & 11 deletions src/test.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,48 @@
#![cfg(test)]

use super::{execute, Section};
use super::execute;

#[test]
fn use_it() {
let mut left: int = 0;
let mut right: int = 0;
let mut left: isize = 0;
let mut right: isize = 0;
execute(&mut [
|| left = 22,
|| right = 44
&mut || left = 22,
&mut || right = 44
]);
assert_eq!(left, 22);
assert_eq!(right, 44);
}

#[cfg(test)]
fn quicksort(v: &mut [int]) {
fn fib(n: usize, depth: usize) -> usize {
if n < 2 {
n
} else {
let mut r1: usize = 0;
let mut r2: usize = 0;
if depth > 0 {
execute(&mut [
&mut || r1 = fib(n-1, depth-1),
&mut || r2 = fib(n-2, depth-1),
]);
} else {
r1 = fib(n-1, 0);
r2 = fib(n-2, 0);
}
r1 + r2
}
}

#[test]
fn calc_fib() {
let n: usize = 20;
let res = fib(n, 3);
assert_eq!(res, 6765);
}

#[cfg(test)]
fn quicksort(v: &mut [isize]) {
if v.len() <= 1 {
return;
}
Expand All @@ -24,13 +51,13 @@ fn quicksort(v: &mut [int]) {
let mid = partition(pivot_value, v);
let (left, right) = v.split_at_mut(mid);
execute(&mut [
|| quicksort(left),
|| quicksort(right)
&mut || quicksort(left),
&mut || quicksort(right)
]);

fn partition(pivot_value: int,
v: &mut [int])
-> uint
fn partition(pivot_value: isize,
v: &mut [isize])
-> usize
{
// Invariant:
// .. l ==> less than or equal to pivot
Expand Down