2
2
Module: task
3
3
4
4
Task management.
5
+
6
+ An executing Rust program consists of a tree of tasks, each with their own
7
+ stack, and sole ownership of their allocated heap data. Tasks communicate
8
+ with each other using ports and channels.
9
+
10
+ When a task fails, that failure will propagate to its parent (the task
11
+ that spawned it) and the parent will fail as well. The reverse is not
12
+ true: when a parent task fails its children will continue executing. When
13
+ the root (main) task fails, all tasks fail, and then so does the entire
14
+ process.
15
+
16
+ A task may remove itself from this failure propagation mechanism by
17
+ calling the <unsupervise> function, after which failure will only
18
+ result in the termination of that task.
19
+
20
+ Tasks may execute in parallel and are scheduled automatically by the runtime.
21
+
22
+ Example:
23
+
24
+ > spawn("Hello, World", fn (&&msg: str) {
25
+ > log msg;
26
+ > });
27
+
5
28
*/
6
29
import cast = unsafe :: reinterpret_cast;
7
30
import comm;
@@ -49,6 +72,8 @@ native "c-stack-cdecl" mod rustrt2 = "rustrt" {
49
72
fn migrate_alloc ( alloc : * u8 , target : task_id ) ;
50
73
}
51
74
75
+ /* Section: Types */
76
+
52
77
type rust_task =
53
78
{ id: task,
54
79
mutable notify_enabled: u32,
@@ -58,24 +83,85 @@ type rust_task =
58
83
resource rust_task_ptr ( task: * rust_task) { rustrt2:: drop_task ( task) ; }
59
84
60
85
type task_id = int ;
86
+
87
+ /*
88
+ Type: task
89
+
90
+ A handle to a task
91
+ */
61
92
type task = task_id ;
93
+
94
+ /*
95
+ Type: joinable_task
96
+
97
+ A task that sends notification upon termination
98
+ */
62
99
type joinable_task = ( task , comm:: port < task_notification > ) ;
63
100
101
+ /*
102
+ Tag: task_result
103
+
104
+ Indicates the manner in which a task exited
105
+ */
106
+ tag task_result {
107
+ /* Variant: tr_success */
108
+ tr_success;
109
+ /* Variant: tr_failure */
110
+ tr_failure;
111
+ }
112
+
113
+ /*
114
+ Tag: task_notification
115
+
116
+ Message sent upon task exit to indicate normal or abnormal termination
117
+ */
118
+ tag task_notification {
119
+ /* Variant: exit */
120
+ exit( task, task_result) ;
121
+ }
122
+
123
+ /* Section: Operations */
124
+
125
+ /*
126
+ Type: get_task
127
+
128
+ Retreives a handle to the currently executing task
129
+ */
64
130
fn get_task ( ) -> task { rustrt2:: get_task_id ( ) }
65
131
66
- /**
67
- * Hints the scheduler to yield this task for a specified ammount of time.
68
- *
69
- * arg: time_in_us maximum number of microseconds to yield control for
70
- */
132
+ /*
133
+ Function: sleep
134
+
135
+ Hints the scheduler to yield this task for a specified ammount of time.
136
+
137
+ Parameters:
138
+
139
+ time_in_us - maximum number of microseconds to yield control for
140
+ */
71
141
fn sleep ( time_in_us : uint ) { ret rustrt:: task_sleep ( time_in_us) ; }
72
142
143
+ /*
144
+ Function: yield
145
+
146
+ Yield control to the task scheduler
147
+
148
+ The scheduler may schedule another task to execute.
149
+ */
73
150
fn yield ( ) { ret rustrt:: task_yield ( ) ; }
74
151
75
- tag task_result { tr_success; tr_failure; }
152
+ /*
153
+ Function: join
154
+
155
+ Wait for a child task to exit
156
+
157
+ The child task must have been spawned with <spawn_joinable>, which
158
+ produces a notification port that the child uses to communicate its
159
+ exit status.
76
160
77
- tag task_notification { exit ( task , task_result ) ; }
161
+ Returns:
78
162
163
+ A task_result indicating whether the task terminated normally or failed
164
+ */
79
165
fn join ( task_port : joinable_task ) -> task_result {
80
166
let ( id, port) = task_port;
81
167
alt comm:: recv :: < task_notification > ( port) {
@@ -87,23 +173,88 @@ fn join(task_port: joinable_task) -> task_result {
87
173
}
88
174
}
89
175
176
+ /*
177
+ Function: unsupervise
178
+
179
+ Detaches this task from its parent in the task tree
180
+
181
+ An unsupervised task will not propagate its failure up the task tree
182
+ */
90
183
fn unsupervise ( ) { ret sys:: unsupervise ( ) ; }
91
184
185
+ /*
186
+ Function: pin
187
+
188
+ Pins the current task and future child tasks to a single scheduler thread
189
+ */
92
190
fn pin ( ) { rustrt2:: pin_task ( ) ; }
93
191
192
+ /*
193
+ Function: unpin
194
+
195
+ Unpin the current task and future child tasks
196
+ */
94
197
fn unpin ( ) { rustrt2:: unpin_task ( ) ; }
95
198
199
+ /*
200
+ Function: set_min_stack
201
+
202
+ Set the minimum stack size (in bytes) for tasks spawned in the future.
203
+
204
+ This function has global effect and should probably not be used.
205
+ */
96
206
fn set_min_stack ( stack_size : uint ) { rustrt2:: set_min_stack ( stack_size) ; }
97
207
208
+ /*
209
+ Function: spawn
210
+
211
+ Creates and executes a new child task
212
+
213
+ Sets up a new task with its own call stack and schedules it to be executed.
214
+ Upon execution the new task will call function `f` with the provided
215
+ argument `data`.
216
+
217
+ Function `f` is a bare function, meaning it may not close over any data, as do
218
+ shared functions (fn@) and lambda blocks. `data` must be a uniquely owned
219
+ type; it is moved into the new task and thus can no longer be accessed
220
+ locally.
221
+
222
+ Parameters:
223
+
224
+ data - A unique-type value to pass to the new task
225
+ f - A function to execute in the new task
226
+
227
+ Returns:
228
+
229
+ A handle to the new task
230
+ */
98
231
fn spawn < unique T > ( -data : T , f : fn ( T ) ) -> task {
99
232
spawn_inner ( data, f, none)
100
233
}
101
234
235
+ /*
236
+ Function: spawn_notify
237
+
238
+ Create and execute a new child task, requesting notification upon its
239
+ termination
240
+
241
+ Immediately before termination, either on success or failure, the spawned
242
+ task will send a <task_notification> message on the provided channel.
243
+ */
102
244
fn spawn_notify < unique T > ( -data : T , f : fn ( T ) ,
103
245
notify : comm:: chan < task_notification > ) -> task {
104
246
spawn_inner ( data, f, some ( notify) )
105
247
}
106
248
249
+ /*
250
+ Function: spawn_joinable
251
+
252
+ Create and execute a task which can later be joined with the <join> function
253
+
254
+ This is a convenience wrapper around spawn_notify which, when paired
255
+ with <join> can be easily used to spawn a task then wait for it to
256
+ complete.
257
+ */
107
258
fn spawn_joinable < unique T > ( -data : T , f : fn ( T ) ) -> joinable_task {
108
259
let p = comm:: port :: < task_notification > ( ) ;
109
260
let id = spawn_notify ( data, f, comm:: chan :: < task_notification > ( p) ) ;
0 commit comments