@@ -52,7 +52,7 @@ use prelude::*;
52
52
use ptr;
53
53
use result;
54
54
use task:: local_data_priv:: { local_get, local_set} ;
55
- use task:: rt:: { task_id, rust_task} ;
55
+ use task:: rt:: { task_id, sched_id , rust_task} ;
56
56
use task;
57
57
use util;
58
58
use util:: replace;
@@ -62,6 +62,12 @@ pub mod local_data;
62
62
pub mod rt;
63
63
pub mod spawn;
64
64
65
+ /// A handle to a scheduler
66
+ #[ deriving_eq]
67
+ pub enum Scheduler {
68
+ SchedulerHandle ( sched_id )
69
+ }
70
+
65
71
/// A handle to a task
66
72
#[ deriving_eq]
67
73
pub enum Task {
@@ -95,7 +101,21 @@ impl TaskResult : Eq {
95
101
}
96
102
97
103
/// Scheduler modes
104
+ #[ deriving_eq]
98
105
pub enum SchedMode {
106
+ /// Run task on the default scheduler
107
+ DefaultScheduler ,
108
+ /// Run task on the current scheduler
109
+ CurrentScheduler ,
110
+ /// Run task on a specific scheduler
111
+ ExistingScheduler ( Scheduler ) ,
112
+ /**
113
+ * Tasks are scheduled on the main OS thread
114
+ *
115
+ * The main OS thread is the thread used to launch the runtime which,
116
+ * in most cases, is the process's initial thread as created by the OS.
117
+ */
118
+ PlatformThread ,
99
119
/// All tasks run in the same OS thread
100
120
SingleThreaded ,
101
121
/// Tasks are distributed among available CPUs
@@ -104,53 +124,6 @@ pub enum SchedMode {
104
124
ThreadPerTask ,
105
125
/// Tasks are distributed among a fixed number of OS threads
106
126
ManualThreads ( uint ) ,
107
- /**
108
- * Tasks are scheduled on the main OS thread
109
- *
110
- * The main OS thread is the thread used to launch the runtime which,
111
- * in most cases, is the process's initial thread as created by the OS.
112
- */
113
- PlatformThread
114
- }
115
-
116
- impl SchedMode : cmp:: Eq {
117
- pure fn eq ( & self , other : & SchedMode ) -> bool {
118
- match ( * self ) {
119
- SingleThreaded => {
120
- match ( * other) {
121
- SingleThreaded => true ,
122
- _ => false
123
- }
124
- }
125
- ThreadPerCore => {
126
- match ( * other) {
127
- ThreadPerCore => true ,
128
- _ => false
129
- }
130
- }
131
- ThreadPerTask => {
132
- match ( * other) {
133
- ThreadPerTask => true ,
134
- _ => false
135
- }
136
- }
137
- ManualThreads ( e0a) => {
138
- match ( * other) {
139
- ManualThreads ( e0b) => e0a == e0b,
140
- _ => false
141
- }
142
- }
143
- PlatformThread => {
144
- match ( * other) {
145
- PlatformThread => true ,
146
- _ => false
147
- }
148
- }
149
- }
150
- }
151
- pure fn ne ( & self , other : & SchedMode ) -> bool {
152
- !( * self ) . eq ( other)
153
- }
154
127
}
155
128
156
129
/**
@@ -204,7 +177,7 @@ pub type TaskOpts = {
204
177
linked : bool ,
205
178
supervised : bool ,
206
179
mut notify_chan : Option < Chan < TaskResult > > ,
207
- sched : Option < SchedOpts > ,
180
+ sched : SchedOpts ,
208
181
} ;
209
182
210
183
/**
@@ -370,7 +343,7 @@ impl TaskBuilder {
370
343
linked: self.opts.linked,
371
344
supervised: self.opts.supervised,
372
345
mut notify_chan: move notify_chan,
373
- sched: Some( { mode: mode, foreign_stack_size: None})
346
+ sched: { mode: mode, foreign_stack_size: None}
374
347
},
375
348
can_not_copy: None,
376
349
.. self.consume()
@@ -486,7 +459,10 @@ pub fn default_task_opts() -> TaskOpts {
486
459
linked: true,
487
460
supervised: false,
488
461
mut notify_chan: None,
489
- sched: None
462
+ sched: {
463
+ mode: DefaultScheduler,
464
+ foreign_stack_size: None
465
+ }
490
466
}
491
467
}
492
468
@@ -539,10 +515,9 @@ pub fn spawn_with<A:Owned>(arg: A, f: fn~(v: A)) {
539
515
540
516
pub fn spawn_sched(mode: SchedMode, f: fn~()) {
541
517
/*!
542
- * Creates a new scheduler and executes a task on it
543
- *
544
- * Tasks subsequently spawned by that task will also execute on
545
- * the new scheduler. When there are no more tasks to execute the
518
+ * Creates a new task on a new or existing scheduler
519
+
520
+ * When there are no more tasks to execute the
546
521
* scheduler terminates.
547
522
*
548
523
* # Failure
@@ -590,6 +565,10 @@ pub fn get_task() -> Task {
590
565
TaskHandle ( rt:: get_task_id( ) )
591
566
}
592
567
568
+ pub fn get_scheduler( ) -> Scheduler {
569
+ SchedulerHandle ( rt:: rust_get_sched_id( ) )
570
+ }
571
+
593
572
/**
594
573
* Temporarily make the task unkillable
595
574
*
@@ -927,16 +906,19 @@ fn test_spawn_sched() {
927
906
}
928
907
929
908
#[ test]
930
- fn test_spawn_sched_childs_on_same_sched ( ) {
909
+ fn test_spawn_sched_childs_on_default_sched ( ) {
931
910
let po = oldcomm : : Port ( ) ;
932
911
let ch = oldcomm:: Chan ( & po) ;
933
912
913
+ // Assuming tests run on the default scheduler
914
+ let default_id = rt:: rust_get_sched_id( ) ;
915
+
934
916
do spawn_sched( SingleThreaded ) {
935
917
let parent_sched_id = rt:: rust_get_sched_id( ) ;
936
918
do spawn {
937
919
let child_sched_id = rt : : rust_get_sched_id( ) ;
938
- // This should be on the same scheduler
939
- assert parent_sched_id == child_sched_id ;
920
+ assert parent_sched_id != child_sched_id ;
921
+ assert child_sched_id == default_id ;
940
922
oldcomm:: send( ch, ( ) ) ;
941
923
} ;
942
924
} ;
@@ -1206,7 +1188,7 @@ fn test_spawn_thread_on_demand() {
1206
1188
1207
1189
let ( port2, chan2) = pipes:: stream( ) ;
1208
1190
1209
- do spawn ( ) |move chan2| {
1191
+ do spawn_sched ( CurrentScheduler ) |move chan2| {
1210
1192
chan2. send( ( ) ) ;
1211
1193
}
1212
1194
0 commit comments