2010-06-23 23:03:09 -05:00
|
|
|
|
|
|
|
#include "rust_internal.h"
|
|
|
|
|
|
|
|
#include "valgrind.h"
|
|
|
|
#include "memcheck.h"
|
|
|
|
|
2010-10-11 18:40:18 -05:00
|
|
|
#ifndef __WIN32__
|
|
|
|
#include <execinfo.h>
|
|
|
|
#endif
|
|
|
|
|
2011-05-31 19:44:54 -05:00
|
|
|
#include "globals.h"
|
|
|
|
|
2011-07-27 16:34:39 -05:00
|
|
|
// Stack size
|
|
|
|
size_t g_custom_min_stack_size = 0;
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
get_min_stk_size(size_t default_size) {
|
|
|
|
if (g_custom_min_stack_size != 0) {
|
|
|
|
return g_custom_min_stack_size;
|
|
|
|
} else {
|
|
|
|
return default_size;
|
2011-07-08 13:34:42 -05:00
|
|
|
}
|
|
|
|
}
|
2011-06-15 20:16:17 -05:00
|
|
|
|
2011-07-27 16:34:39 -05:00
|
|
|
|
2010-06-23 23:03:09 -05:00
|
|
|
// Task stack segments. Heap allocated and chained together.
|
|
|
|
|
|
|
|
static stk_seg*
|
2011-07-27 16:34:39 -05:00
|
|
|
new_stk(rust_scheduler *sched, rust_task *task, size_t minsz)
|
2010-06-23 23:03:09 -05:00
|
|
|
{
|
2011-07-27 16:34:39 -05:00
|
|
|
size_t min_stk_bytes = get_min_stk_size(sched->min_stack_size);
|
2010-06-23 23:03:09 -05:00
|
|
|
if (minsz < min_stk_bytes)
|
|
|
|
minsz = min_stk_bytes;
|
|
|
|
size_t sz = sizeof(stk_seg) + minsz;
|
2011-07-18 14:02:26 -05:00
|
|
|
stk_seg *stk = (stk_seg *)task->malloc(sz, "stack");
|
2011-06-28 14:15:41 -05:00
|
|
|
LOGPTR(task->sched, "new stk", (uintptr_t)stk);
|
2010-06-23 23:03:09 -05:00
|
|
|
memset(stk, 0, sizeof(stk_seg));
|
|
|
|
stk->limit = (uintptr_t) &stk->data[minsz];
|
2011-06-28 14:15:41 -05:00
|
|
|
LOGPTR(task->sched, "stk limit", stk->limit);
|
2010-06-23 23:03:09 -05:00
|
|
|
stk->valgrind_id =
|
|
|
|
VALGRIND_STACK_REGISTER(&stk->data[0],
|
|
|
|
&stk->data[minsz]);
|
|
|
|
return stk;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-06-27 21:15:03 -05:00
|
|
|
del_stk(rust_task *task, stk_seg *stk)
|
2010-06-23 23:03:09 -05:00
|
|
|
{
|
|
|
|
VALGRIND_STACK_DEREGISTER(stk->valgrind_id);
|
2011-06-28 14:15:41 -05:00
|
|
|
LOGPTR(task->sched, "freeing stk segment", (uintptr_t)stk);
|
2011-06-27 21:15:03 -05:00
|
|
|
task->free(stk);
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tasks
|
2011-06-28 14:15:41 -05:00
|
|
|
rust_task::rust_task(rust_scheduler *sched, rust_task_list *state,
|
2010-09-10 03:21:29 -05:00
|
|
|
rust_task *spawner, const char *name) :
|
2011-07-27 16:51:25 -05:00
|
|
|
ref_count(1),
|
2011-06-27 21:15:03 -05:00
|
|
|
stk(NULL),
|
2010-06-23 23:03:09 -05:00
|
|
|
runtime_sp(0),
|
2011-06-28 13:12:00 -05:00
|
|
|
rust_sp(0),
|
2010-06-23 23:03:09 -05:00
|
|
|
gc_alloc_chain(0),
|
2011-06-28 14:15:41 -05:00
|
|
|
sched(sched),
|
2010-06-23 23:03:09 -05:00
|
|
|
cache(NULL),
|
2011-06-28 14:15:41 -05:00
|
|
|
kernel(sched->kernel),
|
2010-08-08 21:24:35 -05:00
|
|
|
name(name),
|
2010-09-10 03:21:29 -05:00
|
|
|
state(state),
|
2010-06-23 23:03:09 -05:00
|
|
|
cond(NULL),
|
2010-08-18 01:26:43 -05:00
|
|
|
cond_name("none"),
|
2010-07-05 16:43:40 -05:00
|
|
|
supervisor(spawner),
|
2010-09-10 03:21:29 -05:00
|
|
|
list_index(-1),
|
2011-08-08 20:09:42 -05:00
|
|
|
next_port_id(0),
|
2010-07-19 16:05:18 -05:00
|
|
|
rendezvous_ptr(0),
|
2011-06-29 20:47:47 -05:00
|
|
|
running_on(-1),
|
|
|
|
pinned_on(-1),
|
2011-06-28 14:15:41 -05:00
|
|
|
local_region(&sched->srv->local_region),
|
2011-07-14 21:39:53 -05:00
|
|
|
_on_wakeup(NULL),
|
2011-07-23 21:03:02 -05:00
|
|
|
failed(false),
|
|
|
|
propagate_failure(true)
|
2010-06-23 23:03:09 -05:00
|
|
|
{
|
2011-06-28 14:15:41 -05:00
|
|
|
LOGPTR(sched, "new task", (uintptr_t)this);
|
|
|
|
DLOG(sched, task, "sizeof(task) = %d (0x%x)", sizeof *this, sizeof *this);
|
2010-07-19 16:05:18 -05:00
|
|
|
|
2011-08-16 18:39:47 -05:00
|
|
|
assert((void*)this == (void*)&user);
|
|
|
|
|
|
|
|
user.notify_enabled = 0;
|
|
|
|
|
2011-07-27 16:34:39 -05:00
|
|
|
stk = new_stk(sched, this, 0);
|
2011-06-27 21:15:03 -05:00
|
|
|
rust_sp = stk->limit;
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
rust_task::~rust_task()
|
|
|
|
{
|
2011-06-28 14:15:41 -05:00
|
|
|
DLOG(sched, task, "~rust_task %s @0x%" PRIxPTR ", refcnt=%d",
|
2011-04-19 05:21:57 -05:00
|
|
|
name, (uintptr_t)this, ref_count);
|
2010-06-23 23:03:09 -05:00
|
|
|
|
2011-08-16 18:39:47 -05:00
|
|
|
if(user.notify_enabled) {
|
|
|
|
rust_chan *target =
|
|
|
|
get_chan_by_handle(&user.notify_chan);
|
|
|
|
if(target) {
|
|
|
|
task_notification msg;
|
|
|
|
msg.id = id;
|
|
|
|
msg.result = failed ? tr_failure : tr_success;
|
|
|
|
|
|
|
|
target->send(&msg);
|
|
|
|
target->deref();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-08 15:38:20 -05:00
|
|
|
kernel->release_task_id(id);
|
|
|
|
|
2010-06-23 23:03:09 -05:00
|
|
|
/* FIXME: tighten this up, there are some more
|
|
|
|
assertions that hold at task-lifecycle events. */
|
2011-07-25 20:00:37 -05:00
|
|
|
I(sched, ref_count == 0); // ||
|
2011-07-23 16:01:43 -05:00
|
|
|
// (ref_count == 1 && this == sched->root_task));
|
2010-06-23 23:03:09 -05:00
|
|
|
|
2011-06-27 21:15:03 -05:00
|
|
|
del_stk(this, stk);
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
|
2011-05-31 19:44:54 -05:00
|
|
|
struct spawn_args {
|
|
|
|
rust_task *task;
|
|
|
|
uintptr_t a3;
|
|
|
|
uintptr_t a4;
|
2011-06-27 12:08:57 -05:00
|
|
|
void (*CDECL f)(int *, rust_task *,
|
2011-05-31 19:44:54 -05:00
|
|
|
uintptr_t, uintptr_t);
|
|
|
|
};
|
|
|
|
|
2011-08-13 17:20:11 -05:00
|
|
|
struct rust_closure {
|
|
|
|
intptr_t ref_count;
|
|
|
|
type_desc *td;
|
|
|
|
};
|
|
|
|
|
2011-05-31 19:44:54 -05:00
|
|
|
extern "C" CDECL
|
2011-08-13 17:20:11 -05:00
|
|
|
void task_exit(rust_closure *env, int rval, rust_task *task) {
|
2011-05-31 19:44:54 -05:00
|
|
|
LOG(task, task, "task exited with value %d", rval);
|
2011-08-12 18:36:17 -05:00
|
|
|
if(env) {
|
|
|
|
// free the environment.
|
2011-08-13 17:20:11 -05:00
|
|
|
I(task->sched, 1 == env->ref_count); // the ref count better be 1
|
|
|
|
//env->td->drop_glue(NULL, task, NULL, env->td->first_param, env);
|
|
|
|
//env->td->free_glue(NULL, task, NULL, env->td->first_param, env);
|
2011-08-12 18:36:17 -05:00
|
|
|
task->free(env);
|
|
|
|
}
|
2011-07-06 14:48:43 -05:00
|
|
|
task->die();
|
2011-07-07 20:18:52 -05:00
|
|
|
task->lock.lock();
|
2011-07-06 14:48:43 -05:00
|
|
|
task->notify_tasks_waiting_to_join();
|
2011-07-07 20:18:52 -05:00
|
|
|
task->lock.unlock();
|
2011-06-27 12:08:57 -05:00
|
|
|
|
2011-05-31 19:44:54 -05:00
|
|
|
task->yield(1);
|
|
|
|
}
|
|
|
|
|
2011-08-10 20:48:57 -05:00
|
|
|
extern "C" CDECL
|
|
|
|
void task_start_wrapper(spawn_args *a)
|
|
|
|
{
|
|
|
|
rust_task *task = a->task;
|
|
|
|
int rval = 42;
|
|
|
|
|
|
|
|
a->f(&rval, task, a->a3, a->a4);
|
|
|
|
task_exit(NULL, rval, task);
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:03:09 -05:00
|
|
|
void
|
2011-05-24 14:51:22 -05:00
|
|
|
rust_task::start(uintptr_t spawnee_fn,
|
2011-06-15 20:16:17 -05:00
|
|
|
uintptr_t args)
|
2011-05-03 09:19:28 -05:00
|
|
|
{
|
2011-06-28 14:15:41 -05:00
|
|
|
LOGPTR(sched, "from spawnee", spawnee_fn);
|
2011-05-03 09:19:28 -05:00
|
|
|
|
2011-06-28 14:15:41 -05:00
|
|
|
I(sched, stk->data != NULL);
|
2011-07-13 17:44:09 -05:00
|
|
|
|
2011-06-15 20:16:17 -05:00
|
|
|
char *sp = (char *)rust_sp;
|
2011-05-03 09:19:28 -05:00
|
|
|
|
2011-05-31 19:44:54 -05:00
|
|
|
sp -= sizeof(spawn_args);
|
2011-05-03 09:19:28 -05:00
|
|
|
|
2011-05-31 19:44:54 -05:00
|
|
|
spawn_args *a = (spawn_args *)sp;
|
2011-05-03 09:19:28 -05:00
|
|
|
|
2011-05-31 19:44:54 -05:00
|
|
|
a->task = this;
|
2011-06-15 20:16:17 -05:00
|
|
|
a->a3 = 0;
|
2011-05-31 19:44:54 -05:00
|
|
|
a->a4 = args;
|
|
|
|
void **f = (void **)&a->f;
|
|
|
|
*f = (void *)spawnee_fn;
|
2011-06-27 12:08:57 -05:00
|
|
|
|
2011-05-31 19:44:54 -05:00
|
|
|
ctx.call((void *)task_start_wrapper, a, sp);
|
2010-06-23 23:03:09 -05:00
|
|
|
|
2011-08-10 20:48:57 -05:00
|
|
|
this->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void rust_task::start()
|
|
|
|
{
|
2011-07-13 14:25:36 -05:00
|
|
|
yield_timer.reset_us(0);
|
2011-06-28 14:15:41 -05:00
|
|
|
transition(&sched->newborn_tasks, &sched->running_tasks);
|
2011-07-25 20:00:37 -05:00
|
|
|
sched->lock.signal();
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rust_task::grow(size_t n_frame_bytes)
|
|
|
|
{
|
2010-08-20 13:05:06 -05:00
|
|
|
// FIXME (issue #151): Just fail rather than almost certainly crashing
|
|
|
|
// mysteriously later. The commented-out logic below won't work at all in
|
|
|
|
// the presence of non-word-aligned pointers.
|
|
|
|
abort();
|
|
|
|
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-07-13 15:43:35 -05:00
|
|
|
rust_task::yield() {
|
|
|
|
yield(0);
|
2010-08-11 23:23:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-07-13 15:43:35 -05:00
|
|
|
rust_task::yield(size_t time_in_us) {
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(this, task, "task %s @0x%" PRIxPTR " yielding for %d us",
|
2010-08-11 23:23:34 -05:00
|
|
|
name, this, time_in_us);
|
2011-05-31 19:44:54 -05:00
|
|
|
|
2011-07-13 14:25:36 -05:00
|
|
|
yield_timer.reset_us(time_in_us);
|
2010-06-23 23:03:09 -05:00
|
|
|
|
2011-05-31 19:44:54 -05:00
|
|
|
// Return to the scheduler.
|
|
|
|
ctx.next->swap(ctx);
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rust_task::kill() {
|
2010-08-09 10:15:34 -05:00
|
|
|
if (dead()) {
|
|
|
|
// Task is already dead, can't kill what's already dead.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:03:09 -05:00
|
|
|
// Note the distinction here: kill() is when you're in an upcall
|
|
|
|
// from task A and want to force-fail task B, you do B->kill().
|
2011-07-13 15:43:35 -05:00
|
|
|
// If you want to fail yourself you do self->fail().
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(this, task, "killing task %s @0x%" PRIxPTR, name, this);
|
2010-06-23 23:03:09 -05:00
|
|
|
// Unblock the task so it can unwind.
|
|
|
|
unblock();
|
2010-07-19 16:05:18 -05:00
|
|
|
|
2011-07-23 21:03:02 -05:00
|
|
|
if (NULL == supervisor && propagate_failure)
|
|
|
|
sched->fail();
|
2010-07-19 16:05:18 -05:00
|
|
|
|
2011-07-25 20:00:37 -05:00
|
|
|
sched->lock.signal();
|
|
|
|
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(this, task, "preparing to unwind task: 0x%" PRIxPTR, this);
|
2011-05-24 16:28:37 -05:00
|
|
|
// run_on_resume(rust_unwind_glue);
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-07-13 15:43:35 -05:00
|
|
|
rust_task::fail() {
|
2010-06-23 23:03:09 -05:00
|
|
|
// See note in ::kill() regarding who should call this.
|
2011-06-28 14:15:41 -05:00
|
|
|
DLOG(sched, task, "task %s @0x%" PRIxPTR " failing", name, this);
|
2010-10-11 18:40:18 -05:00
|
|
|
backtrace();
|
2010-06-23 23:03:09 -05:00
|
|
|
// Unblock the task so it can unwind.
|
|
|
|
unblock();
|
2010-07-05 16:43:40 -05:00
|
|
|
if (supervisor) {
|
2011-06-28 14:15:41 -05:00
|
|
|
DLOG(sched, task,
|
2011-04-19 05:21:57 -05:00
|
|
|
"task %s @0x%" PRIxPTR
|
|
|
|
" propagating failure to supervisor %s @0x%" PRIxPTR,
|
|
|
|
name, this, supervisor->name, supervisor);
|
2010-07-05 16:43:40 -05:00
|
|
|
supervisor->kill();
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
2011-05-04 10:04:53 -05:00
|
|
|
// FIXME: implement unwinding again.
|
2011-07-23 21:03:02 -05:00
|
|
|
if (NULL == supervisor && propagate_failure)
|
|
|
|
sched->fail();
|
2011-07-14 21:39:53 -05:00
|
|
|
failed = true;
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
|
2010-06-25 18:54:03 -05:00
|
|
|
void
|
2011-07-13 15:43:35 -05:00
|
|
|
rust_task::gc()
|
2010-06-25 18:54:03 -05:00
|
|
|
{
|
2011-06-27 12:08:57 -05:00
|
|
|
// FIXME: not presently implemented; was broken by rustc.
|
2011-06-28 14:15:41 -05:00
|
|
|
DLOG(sched, task,
|
2010-08-08 21:24:35 -05:00
|
|
|
"task %s @0x%" PRIxPTR " garbage collecting", name, this);
|
2010-06-25 18:54:03 -05:00
|
|
|
}
|
|
|
|
|
2010-07-05 16:43:40 -05:00
|
|
|
void
|
|
|
|
rust_task::unsupervise()
|
|
|
|
{
|
2011-06-28 14:15:41 -05:00
|
|
|
DLOG(sched, task,
|
2010-08-08 21:24:35 -05:00
|
|
|
"task %s @0x%" PRIxPTR
|
|
|
|
" disconnecting from supervisor %s @0x%" PRIxPTR,
|
|
|
|
name, this, supervisor->name, supervisor);
|
2010-07-05 16:43:40 -05:00
|
|
|
supervisor = NULL;
|
2011-07-23 21:03:02 -05:00
|
|
|
propagate_failure = false;
|
2010-07-05 16:43:40 -05:00
|
|
|
}
|
|
|
|
|
2010-07-28 18:24:50 -05:00
|
|
|
void
|
|
|
|
rust_task::notify_tasks_waiting_to_join() {
|
|
|
|
while (tasks_waiting_to_join.is_empty() == false) {
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(this, task, "notify_tasks_waiting_to_join: %d",
|
2010-07-28 18:24:50 -05:00
|
|
|
tasks_waiting_to_join.size());
|
2011-07-27 16:51:25 -05:00
|
|
|
rust_task *waiting_task = 0;
|
2010-09-07 20:18:37 -05:00
|
|
|
tasks_waiting_to_join.pop(&waiting_task);
|
2011-07-27 16:51:25 -05:00
|
|
|
if (waiting_task->blocked() == true) {
|
|
|
|
waiting_task->wakeup(this);
|
2010-07-28 18:24:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:03:09 -05:00
|
|
|
frame_glue_fns*
|
|
|
|
rust_task::get_frame_glue_fns(uintptr_t fp) {
|
|
|
|
fp -= sizeof(uintptr_t);
|
|
|
|
return *((frame_glue_fns**) fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
rust_task::running()
|
|
|
|
{
|
2011-06-28 14:15:41 -05:00
|
|
|
return state == &sched->running_tasks;
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
rust_task::blocked()
|
|
|
|
{
|
2011-06-28 14:15:41 -05:00
|
|
|
return state == &sched->blocked_tasks;
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
rust_task::blocked_on(rust_cond *on)
|
|
|
|
{
|
|
|
|
return blocked() && cond == on;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
rust_task::dead()
|
|
|
|
{
|
2011-06-28 14:15:41 -05:00
|
|
|
return state == &sched->dead_tasks;
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
|
2010-06-28 20:53:16 -05:00
|
|
|
void
|
|
|
|
rust_task::link_gc(gc_alloc *gcm) {
|
2011-06-28 14:15:41 -05:00
|
|
|
I(sched, gcm->prev == NULL);
|
|
|
|
I(sched, gcm->next == NULL);
|
2010-06-28 20:53:16 -05:00
|
|
|
gcm->prev = NULL;
|
|
|
|
gcm->next = gc_alloc_chain;
|
2010-07-02 10:17:41 -05:00
|
|
|
gc_alloc_chain = gcm;
|
2010-07-12 15:11:58 -05:00
|
|
|
if (gcm->next)
|
|
|
|
gcm->next->prev = gcm;
|
2010-06-28 20:53:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rust_task::unlink_gc(gc_alloc *gcm) {
|
|
|
|
if (gcm->prev)
|
|
|
|
gcm->prev->next = gcm->next;
|
|
|
|
if (gcm->next)
|
|
|
|
gcm->next->prev = gcm->prev;
|
2010-07-12 15:11:58 -05:00
|
|
|
if (gc_alloc_chain == gcm)
|
|
|
|
gc_alloc_chain = gcm->next;
|
2010-06-28 20:53:16 -05:00
|
|
|
gcm->prev = NULL;
|
|
|
|
gcm->next = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2011-07-18 14:02:26 -05:00
|
|
|
rust_task::malloc(size_t sz, const char *tag, type_desc *td)
|
2010-06-28 20:53:16 -05:00
|
|
|
{
|
2010-09-29 19:22:07 -05:00
|
|
|
// FIXME: GC is disabled for now.
|
2011-06-27 12:08:57 -05:00
|
|
|
// GC-memory classification is all wrong.
|
2010-09-29 19:22:07 -05:00
|
|
|
td = NULL;
|
|
|
|
|
2010-06-28 20:53:16 -05:00
|
|
|
if (td) {
|
|
|
|
sz += sizeof(gc_alloc);
|
|
|
|
}
|
2011-07-18 14:02:26 -05:00
|
|
|
|
|
|
|
void *mem = local_region.malloc(sz, tag);
|
2010-06-28 20:53:16 -05:00
|
|
|
if (!mem)
|
|
|
|
return mem;
|
|
|
|
if (td) {
|
|
|
|
gc_alloc *gcm = (gc_alloc*) mem;
|
2011-06-28 14:15:41 -05:00
|
|
|
DLOG(sched, task, "task %s @0x%" PRIxPTR
|
2011-04-19 05:21:57 -05:00
|
|
|
" allocated %d GC bytes = 0x%" PRIxPTR,
|
|
|
|
name, (uintptr_t)this, sz, gcm);
|
2010-06-28 20:53:16 -05:00
|
|
|
memset((void*) gcm, 0, sizeof(gc_alloc));
|
|
|
|
link_gc(gcm);
|
|
|
|
gcm->ctrl_word = (uintptr_t)td;
|
|
|
|
gc_alloc_accum += sz;
|
|
|
|
mem = (void*) &(gcm->data);
|
|
|
|
}
|
|
|
|
return mem;;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
rust_task::realloc(void *data, size_t sz, bool is_gc)
|
|
|
|
{
|
2010-09-29 19:22:07 -05:00
|
|
|
// FIXME: GC is disabled for now.
|
2011-06-27 12:08:57 -05:00
|
|
|
// Effects, GC-memory classification is all wrong.
|
2010-09-29 19:22:07 -05:00
|
|
|
is_gc = false;
|
2010-06-28 20:53:16 -05:00
|
|
|
if (is_gc) {
|
|
|
|
gc_alloc *gcm = (gc_alloc*)(((char *)data) - sizeof(gc_alloc));
|
|
|
|
unlink_gc(gcm);
|
|
|
|
sz += sizeof(gc_alloc);
|
2011-07-06 18:42:02 -05:00
|
|
|
gcm = (gc_alloc*) local_region.realloc((void*)gcm, sz);
|
2011-06-28 14:15:41 -05:00
|
|
|
DLOG(sched, task, "task %s @0x%" PRIxPTR
|
2011-04-19 05:21:57 -05:00
|
|
|
" reallocated %d GC bytes = 0x%" PRIxPTR,
|
|
|
|
name, (uintptr_t)this, sz, gcm);
|
2010-06-28 20:53:16 -05:00
|
|
|
if (!gcm)
|
|
|
|
return gcm;
|
|
|
|
link_gc(gcm);
|
|
|
|
data = (void*) &(gcm->data);
|
|
|
|
} else {
|
2011-07-06 18:42:02 -05:00
|
|
|
data = local_region.realloc(data, sz);
|
2010-06-28 20:53:16 -05:00
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rust_task::free(void *p, bool is_gc)
|
|
|
|
{
|
2010-09-29 19:22:07 -05:00
|
|
|
// FIXME: GC is disabled for now.
|
2011-06-27 12:08:57 -05:00
|
|
|
// GC-memory classification is all wrong.
|
2010-09-29 19:22:07 -05:00
|
|
|
is_gc = false;
|
2010-06-28 20:53:16 -05:00
|
|
|
if (is_gc) {
|
|
|
|
gc_alloc *gcm = (gc_alloc*)(((char *)p) - sizeof(gc_alloc));
|
|
|
|
unlink_gc(gcm);
|
2011-06-28 14:15:41 -05:00
|
|
|
DLOG(sched, mem,
|
2011-04-19 05:21:57 -05:00
|
|
|
"task %s @0x%" PRIxPTR " freeing GC memory = 0x%" PRIxPTR,
|
|
|
|
name, (uintptr_t)this, gcm);
|
2011-07-06 18:42:02 -05:00
|
|
|
DLOG(sched, mem, "rust_task::free(0x%" PRIxPTR ")", gcm);
|
|
|
|
local_region.free(gcm);
|
2010-06-28 20:53:16 -05:00
|
|
|
} else {
|
2011-07-06 18:42:02 -05:00
|
|
|
DLOG(sched, mem, "rust_task::free(0x%" PRIxPTR ")", p);
|
|
|
|
local_region.free(p);
|
2010-06-28 20:53:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:03:09 -05:00
|
|
|
void
|
2010-09-10 03:21:29 -05:00
|
|
|
rust_task::transition(rust_task_list *src, rust_task_list *dst) {
|
2011-08-16 18:39:47 -05:00
|
|
|
bool unlock = false;
|
|
|
|
if(!sched->lock.lock_held_by_current_thread()) {
|
|
|
|
unlock = true;
|
|
|
|
sched->lock.lock();
|
|
|
|
}
|
2011-06-28 14:15:41 -05:00
|
|
|
DLOG(sched, task,
|
2011-05-31 19:44:54 -05:00
|
|
|
"task %s " PTR " state change '%s' -> '%s' while in '%s'",
|
|
|
|
name, (uintptr_t)this, src->name, dst->name, state->name);
|
2011-06-28 14:15:41 -05:00
|
|
|
I(sched, state == src);
|
2010-09-10 03:21:29 -05:00
|
|
|
src->remove(this);
|
|
|
|
dst->append(this);
|
2010-06-23 23:03:09 -05:00
|
|
|
state = dst;
|
2011-08-16 18:39:47 -05:00
|
|
|
if(unlock)
|
|
|
|
sched->lock.unlock();
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-09-07 20:39:07 -05:00
|
|
|
rust_task::block(rust_cond *on, const char* name) {
|
2011-07-07 13:53:08 -05:00
|
|
|
scoped_lock with(lock);
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(this, task, "Blocking on 0x%" PRIxPTR ", cond: 0x%" PRIxPTR,
|
2010-07-28 16:45:44 -05:00
|
|
|
(uintptr_t) on, (uintptr_t) cond);
|
2011-06-28 14:15:41 -05:00
|
|
|
A(sched, cond == NULL, "Cannot block an already blocked task.");
|
|
|
|
A(sched, on != NULL, "Cannot block on a NULL object.");
|
2010-07-28 16:45:44 -05:00
|
|
|
|
2011-06-28 14:15:41 -05:00
|
|
|
transition(&sched->running_tasks, &sched->blocked_tasks);
|
2010-06-23 23:03:09 -05:00
|
|
|
cond = on;
|
2010-08-18 01:26:43 -05:00
|
|
|
cond_name = name;
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-09-10 03:21:29 -05:00
|
|
|
rust_task::wakeup(rust_cond *from) {
|
2011-07-07 13:53:08 -05:00
|
|
|
scoped_lock with(lock);
|
2011-06-28 14:15:41 -05:00
|
|
|
A(sched, cond != NULL, "Cannot wake up unblocked task.");
|
2011-04-19 05:21:57 -05:00
|
|
|
LOG(this, task, "Blocked on 0x%" PRIxPTR " woken up on 0x%" PRIxPTR,
|
2010-07-28 16:45:44 -05:00
|
|
|
(uintptr_t) cond, (uintptr_t) from);
|
2011-06-28 14:15:41 -05:00
|
|
|
A(sched, cond == from, "Cannot wake up blocked task on wrong condition.");
|
2010-07-28 16:45:44 -05:00
|
|
|
|
2011-06-28 14:15:41 -05:00
|
|
|
transition(&sched->blocked_tasks, &sched->running_tasks);
|
|
|
|
I(sched, cond == from);
|
2010-07-28 16:45:44 -05:00
|
|
|
cond = NULL;
|
2010-08-18 01:26:43 -05:00
|
|
|
cond_name = "none";
|
2011-07-06 13:10:40 -05:00
|
|
|
|
|
|
|
if(_on_wakeup) {
|
|
|
|
_on_wakeup->on_wakeup();
|
|
|
|
}
|
2011-07-25 20:00:37 -05:00
|
|
|
|
|
|
|
sched->lock.signal();
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-09-10 03:21:29 -05:00
|
|
|
rust_task::die() {
|
2011-07-07 13:53:08 -05:00
|
|
|
scoped_lock with(lock);
|
2011-06-28 14:15:41 -05:00
|
|
|
transition(&sched->running_tasks, &sched->dead_tasks);
|
2011-07-25 20:00:37 -05:00
|
|
|
sched->lock.signal();
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-09-10 03:21:29 -05:00
|
|
|
rust_task::unblock() {
|
2010-06-23 23:03:09 -05:00
|
|
|
if (blocked())
|
|
|
|
wakeup(cond);
|
|
|
|
}
|
|
|
|
|
|
|
|
rust_crate_cache *
|
2011-05-26 20:20:48 -05:00
|
|
|
rust_task::get_crate_cache()
|
2010-06-23 23:03:09 -05:00
|
|
|
{
|
|
|
|
if (!cache) {
|
2011-06-28 14:15:41 -05:00
|
|
|
DLOG(sched, task, "fetching cache for current crate");
|
|
|
|
cache = sched->get_cache();
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
return cache;
|
|
|
|
}
|
|
|
|
|
2010-10-11 18:40:18 -05:00
|
|
|
void
|
|
|
|
rust_task::backtrace() {
|
2011-04-19 05:21:57 -05:00
|
|
|
if (!log_rt_backtrace) return;
|
2010-10-11 18:40:18 -05:00
|
|
|
#ifndef __WIN32__
|
|
|
|
void *call_stack[256];
|
|
|
|
int nframes = ::backtrace(call_stack, 256);
|
|
|
|
backtrace_symbols_fd(call_stack + 1, nframes - 1, 2);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-06-29 20:47:47 -05:00
|
|
|
bool rust_task::can_schedule(int id)
|
2011-06-20 19:19:50 -05:00
|
|
|
{
|
2011-07-13 17:44:09 -05:00
|
|
|
return yield_timer.has_timed_out() &&
|
2011-06-29 20:47:47 -05:00
|
|
|
running_on == -1 &&
|
|
|
|
(pinned_on == -1 || pinned_on == id);
|
2011-06-20 19:19:50 -05:00
|
|
|
}
|
|
|
|
|
2011-06-27 21:15:03 -05:00
|
|
|
void *
|
2011-07-18 14:02:26 -05:00
|
|
|
rust_task::calloc(size_t size, const char *tag) {
|
|
|
|
return local_region.calloc(size, tag);
|
2011-06-27 21:15:03 -05:00
|
|
|
}
|
|
|
|
|
2011-06-29 20:47:47 -05:00
|
|
|
void rust_task::pin() {
|
2011-06-29 20:56:34 -05:00
|
|
|
I(this->sched, running_on != -1);
|
2011-06-29 20:47:47 -05:00
|
|
|
pinned_on = running_on;
|
|
|
|
}
|
|
|
|
|
2011-06-29 20:56:34 -05:00
|
|
|
void rust_task::pin(int id) {
|
|
|
|
I(this->sched, running_on == -1);
|
|
|
|
pinned_on = id;
|
|
|
|
}
|
|
|
|
|
2011-06-29 20:47:47 -05:00
|
|
|
void rust_task::unpin() {
|
|
|
|
pinned_on = -1;
|
|
|
|
}
|
|
|
|
|
2011-07-06 13:10:40 -05:00
|
|
|
void rust_task::on_wakeup(rust_task::wakeup_callback *callback) {
|
|
|
|
_on_wakeup = callback;
|
|
|
|
}
|
|
|
|
|
2011-08-08 20:09:42 -05:00
|
|
|
rust_port_id rust_task::register_port(rust_port *port) {
|
|
|
|
scoped_lock with(lock);
|
|
|
|
|
|
|
|
rust_port_id id = next_port_id++;
|
|
|
|
port_table.put(id, port);
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rust_task::release_port(rust_port_id id) {
|
|
|
|
scoped_lock with(lock);
|
|
|
|
port_table.remove(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
rust_port *rust_task::get_port_by_id(rust_port_id id) {
|
|
|
|
scoped_lock with(lock);
|
|
|
|
rust_port *port = NULL;
|
|
|
|
port_table.get(id, &port);
|
|
|
|
return port;
|
|
|
|
}
|
|
|
|
|
2011-08-15 18:54:02 -05:00
|
|
|
rust_chan *rust_task::get_chan_by_handle(chan_handle *handle) {
|
|
|
|
rust_task *target_task = kernel->get_task_by_id(handle->task);
|
|
|
|
if(target_task) {
|
|
|
|
rust_port *port = target_task->get_port_by_id(handle->port);
|
|
|
|
target_task->deref();
|
|
|
|
port->remote_chan->ref();
|
|
|
|
return port->remote_chan;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:03:09 -05:00
|
|
|
//
|
|
|
|
// Local Variables:
|
|
|
|
// mode: C++
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
2011-07-13 15:51:20 -05:00
|
|
|
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
2010-06-23 23:03:09 -05:00
|
|
|
// End:
|
|
|
|
//
|