@@ -156,7 +156,7 @@ pub struct SchedOpts {
156
156
pub struct TaskOpts {
157
157
linked : bool ,
158
158
supervised : bool ,
159
- mut notify_chan : Option < Chan < TaskResult > > ,
159
+ notify_chan : Option < Chan < TaskResult > > ,
160
160
sched : SchedOpts
161
161
}
162
162
@@ -176,9 +176,9 @@ pub struct TaskOpts {
176
176
// FIXME (#3724): Replace the 'consumed' bit with move mode on self
177
177
pub struct TaskBuilder {
178
178
opts : TaskOpts ,
179
- mut gen_body : Option < ~fn ( v : ~fn ( ) ) -> ~fn ( ) > ,
179
+ gen_body : Option < ~fn ( v : ~fn ( ) ) -> ~fn ( ) > ,
180
180
can_not_copy : Option < util:: NonCopyable > ,
181
- mut consumed : bool ,
181
+ consumed : bool ,
182
182
}
183
183
184
184
/**
@@ -191,13 +191,13 @@ pub fn task() -> TaskBuilder {
191
191
opts : default_task_opts ( ) ,
192
192
gen_body : None ,
193
193
can_not_copy : None ,
194
- mut consumed : false ,
194
+ consumed : false ,
195
195
}
196
196
}
197
197
198
198
#[ doc( hidden) ] // FIXME #3538
199
199
priv impl TaskBuilder {
200
- fn consume ( & self ) -> TaskBuilder {
200
+ fn consume ( & mut self ) -> TaskBuilder {
201
201
if self . consumed {
202
202
fail ! ( ~"Cannot copy a task_builder"); // Fake move mode on self
203
203
}
@@ -219,57 +219,23 @@ priv impl TaskBuilder {
219
219
}
220
220
221
221
pub impl TaskBuilder {
222
- /**
223
- * Decouple the child task's failure from the parent's. If either fails,
224
- * the other will not be killed.
225
- */
226
- fn unlinked(&self) -> TaskBuilder {
227
- let notify_chan = replace(&mut self.opts.notify_chan, None);
228
- TaskBuilder {
229
- opts: TaskOpts {
230
- linked: false,
231
- supervised: self.opts.supervised,
232
- notify_chan: notify_chan,
233
- sched: self.opts.sched
234
- },
235
- can_not_copy: None,
236
- .. self.consume()
237
- }
222
+ /// Decouple the child task's failure from the parent's. If either fails,
223
+ /// the other will not be killed.
224
+ fn unlinked(&mut self) {
225
+ self.opts.linked = false;
238
226
}
239
- /**
240
- * Unidirectionally link the child task's failure with the parent's. The
241
- * child's failure will not kill the parent, but the parent's will kill
242
- * the child.
243
- */
244
- fn supervised(&self) -> TaskBuilder {
245
- let notify_chan = replace(&mut self.opts.notify_chan, None);
246
- TaskBuilder {
247
- opts: TaskOpts {
248
- linked: false,
249
- supervised: true,
250
- notify_chan: notify_chan,
251
- sched: self.opts.sched
252
- },
253
- can_not_copy: None,
254
- .. self.consume()
255
- }
227
+
228
+ /// Unidirectionally link the child task's failure with the parent's. The
229
+ /// child's failure will not kill the parent, but the parent's will kill
230
+ /// the child.
231
+ fn supervised(&mut self) {
232
+ self.opts.supervised = true;
256
233
}
257
- /**
258
- * Link the child task's and parent task's failures. If either fails, the
259
- * other will be killed.
260
- */
261
- fn linked(&self) -> TaskBuilder {
262
- let notify_chan = replace(&mut self.opts.notify_chan, None);
263
- TaskBuilder {
264
- opts: TaskOpts {
265
- linked: true,
266
- supervised: false,
267
- notify_chan: notify_chan,
268
- sched: self.opts.sched
269
- },
270
- can_not_copy: None,
271
- .. self.consume()
272
- }
234
+
235
+ /// Link the child task's and parent task's failures. If either fails, the
236
+ /// other will be killed.
237
+ fn linked(&mut self) {
238
+ self.opts.linked = true;
273
239
}
274
240
275
241
/**
@@ -289,7 +255,7 @@ pub impl TaskBuilder {
289
255
* # Failure
290
256
* Fails if a future_result was already set for this task.
291
257
*/
292
- fn future_result(&self, blk: &fn(v: Port<TaskResult>)) -> TaskBuilder {
258
+ fn future_result(&mut self, blk: &fn(v: Port<TaskResult>)) {
293
259
// FIXME (#3725): Once linked failure and notification are
294
260
// handled in the library, I can imagine implementing this by just
295
261
// registering an arbitrary number of task::on_exit handlers and
@@ -305,30 +271,12 @@ pub impl TaskBuilder {
305
271
blk(notify_pipe_po);
306
272
307
273
// Reconfigure self to use a notify channel.
308
- TaskBuilder {
309
- opts: TaskOpts {
310
- linked: self.opts.linked,
311
- supervised: self.opts.supervised,
312
- notify_chan: Some(notify_pipe_ch),
313
- sched: self.opts.sched
314
- },
315
- can_not_copy: None,
316
- .. self.consume()
317
- }
274
+ self.opts.notify_chan = Some(notify_pipe_ch);
318
275
}
276
+
319
277
/// Configure a custom scheduler mode for the task.
320
- fn sched_mode(&self, mode: SchedMode) -> TaskBuilder {
321
- let notify_chan = replace(&mut self.opts.notify_chan, None);
322
- TaskBuilder {
323
- opts: TaskOpts {
324
- linked: self.opts.linked,
325
- supervised: self.opts.supervised,
326
- notify_chan: notify_chan,
327
- sched: SchedOpts { mode: mode, foreign_stack_size: None}
328
- },
329
- can_not_copy: None,
330
- .. self.consume()
331
- }
278
+ fn sched_mode(&mut self, mode: SchedMode) {
279
+ self.opts.sched.mode = mode;
332
280
}
333
281
334
282
/**
@@ -343,7 +291,7 @@ pub impl TaskBuilder {
343
291
* generator by applying the task body which results from the
344
292
* existing body generator to the new body generator.
345
293
*/
346
- fn add_wrapper(&self, wrapper: ~fn(v: ~fn()) -> ~fn()) -> TaskBuilder {
294
+ fn add_wrapper(&mut self, wrapper: ~fn(v: ~fn()) -> ~fn()) {
347
295
let prev_gen_body = replace(&mut self.gen_body, None);
348
296
let prev_gen_body = match prev_gen_body {
349
297
Some(gen) => gen,
@@ -360,18 +308,7 @@ pub impl TaskBuilder {
360
308
};
361
309
f
362
310
};
363
- let notify_chan = replace(&mut self.opts.notify_chan, None);
364
- TaskBuilder {
365
- opts: TaskOpts {
366
- linked: self.opts.linked,
367
- supervised: self.opts.supervised,
368
- notify_chan: notify_chan,
369
- sched: self.opts.sched
370
- },
371
- gen_body: Some(next_gen_body),
372
- can_not_copy: None,
373
- .. self.consume()
374
- }
311
+ self.gen_body = Some(next_gen_body);
375
312
}
376
313
377
314
/**
@@ -386,7 +323,7 @@ pub impl TaskBuilder {
386
323
* When spawning into a new scheduler, the number of threads requested
387
324
* must be greater than zero.
388
325
*/
389
- fn spawn(&self, f: ~fn()) {
326
+ fn spawn(&mut self, f: ~fn()) {
390
327
let gen_body = replace(&mut self.gen_body, None);
391
328
let notify_chan = replace(&mut self.opts.notify_chan, None);
392
329
let x = self.consume();
@@ -406,8 +343,9 @@ pub impl TaskBuilder {
406
343
};
407
344
spawn::spawn_raw(opts, f);
408
345
}
346
+
409
347
/// Runs a task, while transfering ownership of one argument to the child.
410
- fn spawn_with<A:Owned>(&self, arg: A, f: ~fn(v: A)) {
348
+ fn spawn_with<A:Owned>(&mut self, arg: A, f: ~fn(v: A)) {
411
349
let arg = Cell(arg);
412
350
do self.spawn {
413
351
f(arg.take());
@@ -427,16 +365,16 @@ pub impl TaskBuilder {
427
365
* # Failure
428
366
* Fails if a future_result was already set for this task.
429
367
*/
430
- fn try<T:Owned>(&self, f: ~fn() -> T) -> Result<T,()> {
368
+ fn try<T:Owned>(&mut self, f: ~fn() -> T) -> Result<T,()> {
431
369
let (po, ch) = stream::<T>();
432
370
let mut result = None;
433
371
434
- let fr_task_builder = self.future_result(|+r| {
435
- result = Some(r);
436
- });
437
- do fr_task_builder.spawn || {
372
+ self.future_result(|+r| { result = Some(r); });
373
+
374
+ do self.spawn {
438
375
ch.send(f());
439
376
}
377
+
440
378
match result.unwrap().recv() {
441
379
Success => result::Ok(po.recv()),
442
380
Failure => result::Err(())
@@ -468,26 +406,23 @@ pub fn default_task_opts() -> TaskOpts {
468
406
469
407
/* Spawn convenience functions */
470
408
409
+ /// Creates and executes a new child task
410
+ ///
411
+ /// Sets up a new task with its own call stack and schedules it to run
412
+ /// the provided unique closure.
413
+ ///
414
+ /// This function is equivalent to `task().spawn(f)`.
471
415
pub fn spawn(f: ~fn()) {
472
- /*!
473
- * Creates and executes a new child task
474
- *
475
- * Sets up a new task with its own call stack and schedules it to run
476
- * the provided unique closure.
477
- *
478
- * This function is equivalent to `task().spawn(f)`.
479
- */
480
-
481
- task().spawn(f)
416
+ let mut task = task();
417
+ task.spawn(f)
482
418
}
483
419
420
+ /// Creates a child task unlinked from the current one. If either this
421
+ /// task or the child task fails, the other will not be killed.
484
422
pub fn spawn_unlinked(f: ~fn()) {
485
- /*!
486
- * Creates a child task unlinked from the current one. If either this
487
- * task or the child task fails, the other will not be killed.
488
- */
489
-
490
- task().unlinked().spawn(f)
423
+ let mut task = task();
424
+ task.unlinked();
425
+ task.spawn(f)
491
426
}
492
427
493
428
pub fn spawn_supervised(f: ~fn()) {
@@ -497,7 +432,9 @@ pub fn spawn_supervised(f: ~fn()) {
497
432
* the child will be killed.
498
433
*/
499
434
500
- task().supervised().spawn(f)
435
+ let mut task = task();
436
+ task.supervised();
437
+ task.spawn(f)
501
438
}
502
439
503
440
pub fn spawn_with<A:Owned>(arg: A, f: ~fn(v: A)) {
@@ -511,7 +448,8 @@ pub fn spawn_with<A:Owned>(arg: A, f: ~fn(v: A)) {
511
448
* This function is equivalent to `task().spawn_with(arg, f)`.
512
449
*/
513
450
514
- task().spawn_with(arg, f)
451
+ let mut task = task();
452
+ task.spawn_with(arg, f)
515
453
}
516
454
517
455
pub fn spawn_sched(mode: SchedMode, f: ~fn()) {
@@ -527,7 +465,9 @@ pub fn spawn_sched(mode: SchedMode, f: ~fn()) {
527
465
* greater than zero.
528
466
*/
529
467
530
- task().sched_mode(mode).spawn(f)
468
+ let mut task = task();
469
+ task.sched_mode(mode);
470
+ task.spawn(f)
531
471
}
532
472
533
473
pub fn try<T:Owned>(f: ~fn() -> T) -> Result<T,()> {
@@ -538,7 +478,9 @@ pub fn try<T:Owned>(f: ~fn() -> T) -> Result<T,()> {
538
478
* This is equivalent to task().supervised().try.
539
479
*/
540
480
541
- task().supervised().try(f)
481
+ let mut task = task();
482
+ task.supervised();
483
+ task.try(f)
542
484
}
543
485
544
486
@@ -822,7 +764,7 @@ fn test_run_basic() {
822
764
823
765
#[cfg(test)]
824
766
struct Wrapper {
825
- mut f: Option<Chan<()>>
767
+ f: Option<Chan<()>>
826
768
}
827
769
828
770
#[test]
0 commit comments