about summary refs log tree commit diff
path: root/src/libcore/task.rs
diff options
context:
space:
mode:
authorEric Holk <eric.holk@gmail.com>2012-07-06 15:15:52 -0700
committerEric Holk <eric.holk@gmail.com>2012-07-06 15:16:16 -0700
commitb925648ac71cfd28298ad994428cafc19f49692b (patch)
tree1acf87eafe6c529cdc7ade83fa475aec4495a91c /src/libcore/task.rs
parentfce064db6b40de3c75714e6a1323eee0726675d6 (diff)
downloadrust-b925648ac71cfd28298ad994428cafc19f49692b.tar.gz
rust-b925648ac71cfd28298ad994428cafc19f49692b.zip
Added a k-nucleotide version that uses pipes. 31% speedup.
Diffstat (limited to 'src/libcore/task.rs')
-rw-r--r--src/libcore/task.rs39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/libcore/task.rs b/src/libcore/task.rs
index 2d2c2660fc8..f41e24c623f 100644
--- a/src/libcore/task.rs
+++ b/src/libcore/task.rs
@@ -47,6 +47,7 @@ export unsupervise;
 export run_listener;
 
 export spawn;
+export spawn_with;
 export spawn_listener;
 export spawn_sched;
 export try;
@@ -338,6 +339,28 @@ fn unsupervise(builder: builder) {
     });
 }
 
+fn run_with<A:send>(-builder: builder,
+                    +arg: A,
+                    +f: fn~(+A)) {
+
+    /*!
+     *
+     * Runs a task, while transfering ownership of one argument to the
+     * child.
+     *
+     * This is useful for transfering ownership of noncopyables to
+     * another task.
+     *
+     */
+
+    let arg = ~mut some(arg);
+    do run(builder) {
+        let mut my_arg = none;
+        my_arg <-> *arg;
+        f(option::unwrap(my_arg))
+    }
+}
+
 fn run_listener<A:send>(-builder: builder,
                         +f: fn~(comm::port<A>)) -> comm::chan<A> {
     /*!
@@ -381,6 +404,22 @@ fn spawn(+f: fn~()) {
     run(builder(), f);
 }
 
+fn spawn_with<A:send>(+arg: A, +f: fn~(+A)) {
+    /*!
+     * Runs a new task while providing a channel from the parent to the child
+     *
+     * Sets up a communication channel from the current task to the new
+     * child task, passes the port to child's body, and returns a channel
+     * linked to the port to the parent.
+     *
+     * This encapsulates some boilerplate handshaking logic that would
+     * otherwise be required to establish communication from the parent
+     * to the child.
+     */
+
+    run_with(builder(), arg, f)
+}
+
 fn spawn_listener<A:send>(+f: fn~(comm::port<A>)) -> comm::chan<A> {
     /*!
      * Runs a new task while providing a channel from the parent to the child