Skip to content

Commit 25e81e3

Browse files
committed
rewrite task tests
1 parent 98f5109 commit 25e81e3

File tree

6 files changed

+55
-67
lines changed

6 files changed

+55
-67
lines changed

src/rt/rust.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ rust_start(uintptr_t main_fn, int argc, char **argv, void* crate_map) {
9898
DLOG(sched, dom, "startup: arg[%d] = '%s'", i, args->argv[i]);
9999
}
100100

101-
root_task->start((spawn_fn)main_fn, (uintptr_t)args->args);
101+
root_task->start((spawn_fn)main_fn, NULL, args->args);
102102
root_task->deref();
103103
root_task = NULL;
104104

src/rt/rust_builtin.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,15 +421,15 @@ rust_get_task() {
421421
}
422422

423423
struct fn_env_pair {
424-
intptr_t f;
425-
intptr_t env;
424+
spawn_fn f;
425+
rust_boxed_closure *env;
426426
};
427427

428428
extern "C" CDECL void
429429
start_task(rust_task_id id, fn_env_pair *f) {
430430
rust_task *task = rust_scheduler::get_task();
431431
rust_task *target = task->kernel->get_task_by_id(id);
432-
target->start((spawn_fn)f->f, f->env);
432+
target->start(f->f, f->env, NULL);
433433
target->deref();
434434
}
435435

src/rt/rust_task.cpp

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -292,18 +292,9 @@ rust_task::~rust_task()
292292

293293
struct spawn_args {
294294
rust_task *task;
295-
uintptr_t envptr;
296295
spawn_fn f;
297-
};
298-
299-
struct rust_closure {
300-
const type_desc *td;
301-
// ... see trans_closure.rs for full description ...
302-
};
303-
304-
struct rust_boxed_closure {
305-
intptr_t ref_count;
306-
rust_closure closure;
296+
rust_boxed_closure *envptr;
297+
void *argptr;
307298
};
308299

309300
struct cleanup_args {
@@ -319,14 +310,6 @@ cleanup_task(cleanup_args *args) {
319310

320311
cc::do_cc(task);
321312

322-
rust_boxed_closure* boxed_env = (rust_boxed_closure*)a->envptr;
323-
if(boxed_env) {
324-
// free the environment.
325-
rust_closure *env = &boxed_env->closure;
326-
env->td->drop_glue(NULL, NULL, &env->td, env);
327-
env->td->free_glue(NULL, NULL, &env->td, env);
328-
}
329-
330313
task->die();
331314

332315
if (task->killed && !failed) {
@@ -345,6 +328,8 @@ cleanup_task(cleanup_args *args) {
345328
}
346329
}
347330

331+
extern "C" void upcall_shared_free(void* ptr);
332+
348333
// This runs on the Rust stack
349334
extern "C" CDECL
350335
void task_start_wrapper(spawn_args *a)
@@ -355,27 +340,36 @@ void task_start_wrapper(spawn_args *a)
355340
try {
356341
// The first argument is the return pointer; as the task fn
357342
// must have void return type, we can safely pass 0.
358-
a->f(0, a->envptr);
343+
a->f(0, a->envptr, a->argptr);
359344
} catch (rust_task *ex) {
360345
A(task->sched, ex == task,
361346
"Expected this task to be thrown for unwinding");
362347
failed = true;
363348
}
364349

365-
cleanup_args ca = {a, failed};
350+
rust_boxed_closure* boxed_env = (rust_boxed_closure*)a->envptr;
351+
if(boxed_env) {
352+
// free the environment.
353+
const type_desc *td = boxed_env->closure.td;
354+
td->drop_glue(NULL, NULL, td->first_param, &boxed_env->closure);
355+
upcall_shared_free(boxed_env);
356+
}
366357

367358
// The cleanup work needs lots of stack
359+
cleanup_args ca = {a, failed};
368360
task->sched->c_context.call_shim_on_c_stack(&ca, (void*)cleanup_task);
369361

370362
task->ctx.next->swap(task->ctx);
371363
}
372364

373365
void
374366
rust_task::start(spawn_fn spawnee_fn,
375-
uintptr_t env)
367+
rust_boxed_closure *envptr,
368+
void *argptr)
376369
{
377370
LOG(this, task, "starting task from fn 0x%" PRIxPTR
378-
" with env 0x%" PRIxPTR, spawnee_fn, env);
371+
" with env 0x%" PRIxPTR " and arg 0x%" PRIxPTR,
372+
spawnee_fn, envptr, argptr);
379373

380374
I(sched, stk->data != NULL);
381375

@@ -386,7 +380,8 @@ rust_task::start(spawn_fn spawnee_fn,
386380
spawn_args *a = (spawn_args *)sp;
387381

388382
a->task = this;
389-
a->envptr = env;
383+
a->envptr = envptr;
384+
a->argptr = argptr;
390385
a->f = spawnee_fn;
391386

392387
ctx.call((void *)task_start_wrapper, a, sp);

src/rt/rust_task.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,17 @@ struct chan_handle {
2121
rust_port_id port;
2222
};
2323

24-
typedef void (*CDECL spawn_fn)(uintptr_t, uintptr_t);
24+
struct rust_closure {
25+
const type_desc *td;
26+
// ... see trans_closure.rs for full description ...
27+
};
28+
29+
struct rust_boxed_closure {
30+
intptr_t ref_count;
31+
rust_closure closure;
32+
};
33+
34+
typedef void (*CDECL spawn_fn)(void*, rust_boxed_closure*, void *);
2535

2636
struct rust_box;
2737

@@ -135,7 +145,8 @@ rust_task : public kernel_owned<rust_task>, rust_cond
135145
~rust_task();
136146

137147
void start(spawn_fn spawnee_fn,
138-
uintptr_t args);
148+
rust_boxed_closure *env,
149+
void *args);
139150
void start();
140151
bool running();
141152
bool blocked();

src/rt/test/rust_test_runtime.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ rust_domain_test::run() {
3939
return true;
4040
}
4141

42-
void task_entry(uintptr_t retptr, uintptr_t env) {
42+
void task_entry(void *, rust_boxed_closure *, void *) {
4343
printf("task entry\n");
4444
}
4545

4646
void
4747
rust_task_test::worker::run() {
4848
rust_task_id root_id = kernel->create_task(NULL, "main");
4949
rust_task *root_task = kernel->get_task_by_id(root_id);
50-
root_task->start(&task_entry, (uintptr_t)NULL);
50+
root_task->start(&task_entry, NULL, NULL);
5151
root_task->sched->start_main_loop();
5252
}
5353

src/test/stdtest/task.rs

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,29 @@ fn test_sleep() { task::sleep(1000000u); }
1111
#[test]
1212
#[ignore(cfg(target_os = "win32"))]
1313
fn test_unsupervise() {
14-
fn f(&&_i: ()) { task::unsupervise(); fail; }
15-
task::spawn((), f);
14+
fn f() { task::unsupervise(); fail; }
15+
task::spawn {|| f};
1616
}
1717

1818
#[test]
1919
fn test_lib_spawn() {
20-
fn foo(&&_i: ()) { #error("Hello, World!"); }
21-
task::spawn((), foo);
20+
fn foo() { #error("Hello, World!"); }
21+
task::spawn {|| foo};
2222
}
2323

2424
#[test]
2525
fn test_lib_spawn2() {
26-
fn foo(&&x: int) { assert (x == 42); }
27-
task::spawn(42, foo);
26+
fn foo(x: int) { assert (x == 42); }
27+
task::spawn {|| foo(42);};
2828
}
2929

3030
#[test]
3131
fn test_join_chan() {
32-
fn winner(&&_i: ()) { }
32+
fn winner() { }
3333

34-
let p = comm::port();
35-
task::spawn_notify((), winner, comm::chan(p));
36-
let s = comm::recv(p);
37-
#error("received task status message");
38-
log(error, s);
39-
alt s {
40-
task::exit(_, task::tr_success.) {/* yay! */ }
34+
let t = task::spawn_joinable {|| winner();};
35+
alt task::join(t) {
36+
task::tr_success. {/* yay! */ }
4137
_ { fail "invalid task status received" }
4238
}
4339
}
@@ -46,32 +42,18 @@ fn test_join_chan() {
4642
#[test]
4743
#[ignore(cfg(target_os = "win32"))]
4844
fn test_join_chan_fail() {
49-
fn failer(&&_i: ()) { task::unsupervise(); fail }
45+
fn failer() { task::unsupervise(); fail }
5046

51-
let p = comm::port();
52-
task::spawn_notify((), failer, comm::chan(p));
53-
let s = comm::recv(p);
54-
#error("received task status message");
55-
log(error, s);
56-
alt s {
57-
task::exit(_, task::tr_failure.) {/* yay! */ }
47+
let t = task::spawn_joinable {|| failer();};
48+
alt task::join(t) {
49+
task::tr_failure. {/* yay! */ }
5850
_ { fail "invalid task status received" }
5951
}
6052
}
6153

6254
#[test]
63-
fn test_join_convenient() {
64-
fn winner(&&_i: ()) { }
65-
let handle = task::spawn_joinable((), winner);
66-
assert (task::tr_success == task::join(handle));
67-
}
68-
69-
#[test]
70-
#[ignore]
7155
fn spawn_polymorphic() {
72-
// FIXME #1038: Can't spawn palymorphic functions
73-
/*fn foo<T: send>(x: T) { log(error, x); }
74-
75-
task::spawn(true, foo);
76-
task::spawn(42, foo);*/
56+
fn foo<send T>(x: T) { log(error, x); }
57+
task::spawn {|| foo(true);}
58+
task::spawn {|| foo(42);}
7759
}

0 commit comments

Comments
 (0)