Skip to content

Commit 26d8392

Browse files
committed
Add more std docs
1 parent 00404eb commit 26d8392

File tree

2 files changed

+164
-7
lines changed

2 files changed

+164
-7
lines changed

src/lib/comm.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ Communication between tasks is facilitated by ports (in the receiving task),
77
and channels (in the sending task). Any number of channels may feed into a
88
single port.
99
10+
Ports and channels may only transmit values of unique types; that is,
11+
values that are statically guaranteed to be accessed by a single
12+
'owner' at a time. Unique types include scalars, vectors, strings,
13+
and records, tags, tuples and unique boxes (~T) thereof. Most notably,
14+
shared boxes (@T) may not be transmitted across channels.
15+
1016
Example:
1117
1218
> use std::task;

src/lib/task.rs

Lines changed: 158 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,29 @@
22
Module: task
33
44
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+
528
*/
629
import cast = unsafe::reinterpret_cast;
730
import comm;
@@ -49,6 +72,8 @@ native "c-stack-cdecl" mod rustrt2 = "rustrt" {
4972
fn migrate_alloc(alloc: *u8, target: task_id);
5073
}
5174

75+
/* Section: Types */
76+
5277
type rust_task =
5378
{id: task,
5479
mutable notify_enabled: u32,
@@ -58,24 +83,85 @@ type rust_task =
5883
resource rust_task_ptr(task: *rust_task) { rustrt2::drop_task(task); }
5984

6085
type task_id = int;
86+
87+
/*
88+
Type: task
89+
90+
A handle to a task
91+
*/
6192
type task = task_id;
93+
94+
/*
95+
Type: joinable_task
96+
97+
A task that sends notification upon termination
98+
*/
6299
type joinable_task = (task, comm::port<task_notification>);
63100

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+
*/
64130
fn get_task() -> task { rustrt2::get_task_id() }
65131

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+
*/
71141
fn sleep(time_in_us: uint) { ret rustrt::task_sleep(time_in_us); }
72142

143+
/*
144+
Function: yield
145+
146+
Yield control to the task scheduler
147+
148+
The scheduler may schedule another task to execute.
149+
*/
73150
fn yield() { ret rustrt::task_yield(); }
74151

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.
76160
77-
tag task_notification { exit(task, task_result); }
161+
Returns:
78162
163+
A task_result indicating whether the task terminated normally or failed
164+
*/
79165
fn join(task_port: joinable_task) -> task_result {
80166
let (id, port) = task_port;
81167
alt comm::recv::<task_notification>(port) {
@@ -87,23 +173,88 @@ fn join(task_port: joinable_task) -> task_result {
87173
}
88174
}
89175

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+
*/
90183
fn unsupervise() { ret sys::unsupervise(); }
91184

185+
/*
186+
Function: pin
187+
188+
Pins the current task and future child tasks to a single scheduler thread
189+
*/
92190
fn pin() { rustrt2::pin_task(); }
93191

192+
/*
193+
Function: unpin
194+
195+
Unpin the current task and future child tasks
196+
*/
94197
fn unpin() { rustrt2::unpin_task(); }
95198

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+
*/
96206
fn set_min_stack(stack_size: uint) { rustrt2::set_min_stack(stack_size); }
97207

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+
*/
98231
fn spawn<unique T>(-data: T, f: fn(T)) -> task {
99232
spawn_inner(data, f, none)
100233
}
101234

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+
*/
102244
fn spawn_notify<unique T>(-data: T, f: fn(T),
103245
notify: comm::chan<task_notification>) -> task {
104246
spawn_inner(data, f, some(notify))
105247
}
106248

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+
*/
107258
fn spawn_joinable<unique T>(-data: T, f: fn(T)) -> joinable_task {
108259
let p = comm::port::<task_notification>();
109260
let id = spawn_notify(data, f, comm::chan::<task_notification>(p));

0 commit comments

Comments
 (0)