@@ -32,6 +32,7 @@ use cmp::Eq;
3232use result:: Result ;
3333use pipes:: { stream, Chan , Port } ;
3434use local_data_priv:: { local_get, local_set} ;
35+ use util:: replace;
3536
3637use rt:: task_id;
3738use rt:: rust_task;
@@ -72,25 +73,6 @@ impl TaskResult : Eq {
7273 pure fn ne ( other : & TaskResult ) -> bool { !self . eq ( other) }
7374}
7475
75- /// A message type for notifying of task lifecycle events
76- pub enum Notification {
77- /// Sent when a task exits with the task handle and result
78- Exit ( Task , TaskResult )
79- }
80-
81- impl Notification : cmp:: Eq {
82- pure fn eq ( other : & Notification ) -> bool {
83- match self {
84- Exit ( e0a, e1a) => {
85- match ( * other) {
86- Exit ( e0b, e1b) => e0a == e0b && e1a == e1b
87- }
88- }
89- }
90- }
91- pure fn ne ( other : & Notification ) -> bool { !self . eq ( other) }
92- }
93-
9476/// Scheduler modes
9577pub enum SchedMode {
9678 /// All tasks run in the same OS thread
@@ -200,7 +182,7 @@ pub type SchedOpts = {
200182pub type TaskOpts = {
201183 linked : bool ,
202184 supervised : bool ,
203- mut notify_chan : Option < Chan < Notification > > ,
185+ mut notify_chan : Option < Chan < TaskResult > > ,
204186 sched : Option < SchedOpts > ,
205187} ;
206188
@@ -246,11 +228,7 @@ priv impl TaskBuilder {
246228 fail ~"Cannot copy a task_builder"; // Fake move mode on self
247229 }
248230 self . consumed = true ;
249- let notify_chan = if self . opts . notify_chan . is_none ( ) {
250- None
251- } else {
252- Some ( option:: swap_unwrap ( & mut self . opts . notify_chan ) )
253- } ;
231+ let notify_chan = replace ( & mut self . opts . notify_chan , None ) ;
254232 TaskBuilder ( {
255233 opts : {
256234 linked : self . opts . linked ,
@@ -271,11 +249,7 @@ impl TaskBuilder {
271249 * the other will not be killed.
272250 */
273251 fn unlinked ( ) -> TaskBuilder {
274- let notify_chan = if self . opts . notify_chan . is_none ( ) {
275- None
276- } else {
277- Some ( option:: swap_unwrap ( & mut self . opts . notify_chan ) )
278- } ;
252+ let notify_chan = replace ( & mut self . opts . notify_chan , None ) ;
279253 TaskBuilder ( {
280254 opts : {
281255 linked : false ,
@@ -293,11 +267,7 @@ impl TaskBuilder {
293267 * the child.
294268 */
295269 fn supervised ( ) -> TaskBuilder {
296- let notify_chan = if self . opts . notify_chan . is_none ( ) {
297- None
298- } else {
299- Some ( option:: swap_unwrap ( & mut self . opts . notify_chan ) )
300- } ;
270+ let notify_chan = replace ( & mut self . opts . notify_chan , None ) ;
301271 TaskBuilder ( {
302272 opts : {
303273 linked : false ,
@@ -314,11 +284,7 @@ impl TaskBuilder {
314284 * other will be killed.
315285 */
316286 fn linked ( ) -> TaskBuilder {
317- let notify_chan = if self . opts . notify_chan . is_none ( ) {
318- None
319- } else {
320- Some ( option:: swap_unwrap ( & mut self . opts . notify_chan ) )
321- } ;
287+ let notify_chan = replace ( & mut self . opts . notify_chan , None ) ;
322288 TaskBuilder ( {
323289 opts : {
324290 linked : true ,
@@ -348,7 +314,7 @@ impl TaskBuilder {
348314 * # Failure
349315 * Fails if a future_result was already set for this task.
350316 */
351- fn future_result( blk : fn ( v : future :: Future < TaskResult > ) ) -> TaskBuilder {
317+ fn future_result( blk : fn ( v : Port < TaskResult > ) ) -> TaskBuilder {
352318 // FIXME (#3725): Once linked failure and notification are
353319 // handled in the library, I can imagine implementing this by just
354320 // registering an arbitrary number of task::on_exit handlers and
@@ -359,13 +325,9 @@ impl TaskBuilder {
359325 }
360326
361327 // Construct the future and give it to the caller.
362- let (notify_pipe_ch, notify_pipe_po) = stream::<Notification >();
328+ let (notify_pipe_ch, notify_pipe_po) = stream::<TaskResult >();
363329
364- blk(do future::from_fn |move notify_pipe_po| {
365- match notify_pipe_po.recv() {
366- Exit(_, result) => result
367- }
368- });
330+ blk(move notify_pipe_po);
369331
370332 // Reconfigure self to use a notify channel.
371333 TaskBuilder({
@@ -381,11 +343,7 @@ impl TaskBuilder {
381343 }
382344 /// Configure a custom scheduler mode for the task.
383345 fn sched_mode(mode: SchedMode) -> TaskBuilder {
384- let notify_chan = if self.opts.notify_chan.is_none() {
385- None
386- } else {
387- Some(option::swap_unwrap(&mut self.opts.notify_chan))
388- };
346+ let notify_chan = replace(&mut self.opts.notify_chan, None);
389347 TaskBuilder({
390348 opts: {
391349 linked: self.opts.linked,
@@ -412,11 +370,7 @@ impl TaskBuilder {
412370 */
413371 fn add_wrapper(wrapper: fn@(v: fn~()) -> fn~()) -> TaskBuilder {
414372 let prev_gen_body = self.gen_body;
415- let notify_chan = if self.opts.notify_chan.is_none() {
416- None
417- } else {
418- Some(option::swap_unwrap(&mut self.opts.notify_chan))
419- };
373+ let notify_chan = replace(&mut self.opts.notify_chan, None);
420374 TaskBuilder({
421375 opts: {
422376 linked: self.opts.linked,
@@ -447,13 +401,7 @@ impl TaskBuilder {
447401 * must be greater than zero.
448402 */
449403 fn spawn(f: fn~()) {
450- let notify_chan = if self.opts.notify_chan.is_none() {
451- None
452- } else {
453- let swapped_notify_chan =
454- option::swap_unwrap(&mut self.opts.notify_chan);
455- Some(move swapped_notify_chan)
456- };
404+ let notify_chan = replace(&mut self.opts.notify_chan, None);
457405 let x = self.consume();
458406 let opts = {
459407 linked: x.opts.linked,
@@ -532,7 +480,7 @@ impl TaskBuilder {
532480 do fr_task_builder.spawn |move f| {
533481 comm::send(ch, f());
534482 }
535- match future::get(& option::unwrap(move result)) {
483+ match option::unwrap(move result).recv( ) {
536484 Success => result::Ok(comm::recv(po)),
537485 Failure => result::Err(())
538486 }
@@ -949,14 +897,14 @@ fn test_add_wrapper() {
949897fn test_future_result( ) {
950898 let mut result = None ;
951899 do task( ) . future_result( |+r| { result = Some ( move r) ; } ) . spawn { }
952- assert future :: get ( & option:: unwrap( move result) ) == Success ;
900+ assert option:: unwrap( move result) . recv ( ) == Success ;
953901
954902 result = None ;
955903 do task( ) . future_result( |+r|
956904 { result = Some ( move r) ; } ) . unlinked( ) . spawn {
957905 fail;
958906 }
959- assert future :: get ( & option:: unwrap( move result) ) == Failure ;
907+ assert option:: unwrap( move result) . recv ( ) == Failure ;
960908}
961909
962910#[ test] #[ should_fail] #[ ignore( cfg( windows) ) ]
0 commit comments