Remove task::worker

It was too unsafe to live. It already apeared to be causing problems with
eholk's incoming task changes, so I'm killing it now before it can spread.
This commit is contained in:
Brian Anderson 2011-07-27 15:52:25 -07:00
parent 55a65a51aa
commit 92e9382193
3 changed files with 10 additions and 107 deletions

View File

@ -40,88 +40,6 @@ fn send[T](c: chan[T], v: &T) { c <| v; }
fn recv[T](p: port[T]) -> T { let v; p |> v; v }
// Spawn a task and immediately return a channel for communicating to it
fn worker[T](f: fn(port[T]) ) -> {task: task, chan: chan[T]} {
// FIXME: This is frighteningly unsafe and only works for
// a few cases
type opaque = int;
// FIXME: This terrible hackery is because worktask can't currently
// have type params
type wordsz1 = int;
type wordsz2 = {a: int, b: int};
type wordsz3 = {a: int, b: int, c: int};
type wordsz4 = {a: int, b: int, c: int, d: int};
type wordsz5 = {a: int, b: int, c: int, d: int, e: int};
type opaquechan_1wordsz = chan[chan[wordsz1]];
type opaquechan_2wordsz = chan[chan[wordsz2]];
type opaquechan_3wordsz = chan[chan[wordsz3]];
type opaquechan_4wordsz = chan[chan[wordsz4]];
type opaquechan_5wordsz = chan[chan[wordsz5]];
fn worktask1(setupch: opaquechan_1wordsz, fptr: opaque) {
let f: *fn(port[wordsz1]) = unsafe::reinterpret_cast(fptr);
let p = port[wordsz1]();
setupch <| chan(p);
(*f)(p);
}
fn worktask2(setupch: opaquechan_2wordsz, fptr: opaque) {
let f: *fn(port[wordsz2]) = unsafe::reinterpret_cast(fptr);
let p = port[wordsz2]();
setupch <| chan(p);
(*f)(p);
}
fn worktask3(setupch: opaquechan_3wordsz, fptr: opaque) {
let f: *fn(port[wordsz3]) = unsafe::reinterpret_cast(fptr);
let p = port[wordsz3]();
setupch <| chan(p);
(*f)(p);
}
fn worktask4(setupch: opaquechan_4wordsz, fptr: opaque) {
let f: *fn(port[wordsz4]) = unsafe::reinterpret_cast(fptr);
let p = port[wordsz4]();
setupch <| chan(p);
(*f)(p);
}
fn worktask5(setupch: opaquechan_5wordsz, fptr: opaque) {
let f: *fn(port[wordsz5]) = unsafe::reinterpret_cast(fptr);
let p = port[wordsz5]();
setupch <| chan(p);
(*f)(p);
}
let p = port[chan[T]]();
let setupch = chan(p);
let fptr = unsafe::reinterpret_cast(ptr::addr_of(f));
let Tsz = sys::size_of[T]();
let t =
if Tsz == sys::size_of[wordsz1]() {
let setupchptr = unsafe::reinterpret_cast(setupch);
spawn worktask1(setupchptr, fptr)
} else if (Tsz == sys::size_of[wordsz2]()) {
let setupchptr = unsafe::reinterpret_cast(setupch);
spawn worktask2(setupchptr, fptr)
} else if (Tsz == sys::size_of[wordsz3]()) {
let setupchptr = unsafe::reinterpret_cast(setupch);
spawn worktask3(setupchptr, fptr)
} else if (Tsz == sys::size_of[wordsz4]()) {
let setupchptr = unsafe::reinterpret_cast(setupch);
spawn worktask4(setupchptr, fptr)
} else if (Tsz == sys::size_of[wordsz5]()) {
let setupchptr = unsafe::reinterpret_cast(setupch);
spawn worktask5(setupchptr, fptr)
} else { fail #fmt("unhandled type size %u in task::worker", Tsz) };
let ch;
p |> ch;
ret {task: t, chan: ch};
}
// Local Variables:
// mode: rust;
// fill-column: 78;

View File

@ -573,8 +573,16 @@ mod procsrv {
type response = {pid: int, outfd: int};
fn mk() -> handle {
let res = task::worker(worker);
ret {task: option::some(res.task), chan: res.chan};
auto setupport = port();
auto task = spawn fn(chan[chan[request]] setupchan) {
auto reqport = port();
auto reqchan = chan(reqport);
task::send(setupchan, reqchan);
worker(reqport);
} (chan(setupport));
ret {task: option::some(task),
chan: task::recv(setupport)
};
}
fn from_chan(ch: &reqchan) -> handle { {task: option::none, chan: ch} }

View File

@ -34,26 +34,3 @@ fn test_send_recv() {
assert (task::recv(p) == 10);
}
#[test]
fn test_worker() {
task::worker(fn (p: port[int]) { let x; p |> x; assert (x == 10); }).chan
<| 10;
task::worker(fn (p: port[{x: int, y: int}]) {
let x;
p |> x;
assert (x.y == 20);
}).chan <| {x: 10, y: 20};
task::worker(fn (p: port[{x: int, y: int, z: int}]) {
let x;
p |> x;
assert (x.z == 30);
}).chan <| {x: 10, y: 20, z: 30};
task::worker(fn (p: port[{a: int, b: int, c: int, d: int}]) {
let x;
p |> x;
assert (x.d == 40);
}).chan <| {a: 10, b: 20, c: 30, d: 40};
}