2010-06-23 23:03:09 -05:00
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include "rust_internal.h"
|
|
|
|
|
2010-09-07 20:39:07 -05:00
|
|
|
rust_dom::rust_dom(rust_kernel *kernel,
|
|
|
|
rust_message_queue *message_queue, rust_srv *srv,
|
|
|
|
rust_crate const *root_crate, const char *name) :
|
2010-06-23 23:03:09 -05:00
|
|
|
interrupt_flag(0),
|
|
|
|
root_crate(root_crate),
|
|
|
|
_log(srv, this),
|
2011-04-19 05:21:57 -05:00
|
|
|
log_lvl(log_note),
|
2010-06-23 23:03:09 -05:00
|
|
|
srv(srv),
|
2010-08-18 01:40:07 -05:00
|
|
|
local_region(&srv->local_region),
|
|
|
|
synchronized_region(&srv->synchronized_region),
|
2010-08-08 21:24:35 -05:00
|
|
|
name(name),
|
2010-09-10 03:21:29 -05:00
|
|
|
newborn_tasks(this, "newborn"),
|
|
|
|
running_tasks(this, "running"),
|
|
|
|
blocked_tasks(this, "blocked"),
|
|
|
|
dead_tasks(this, "dead"),
|
2010-06-23 23:03:09 -05:00
|
|
|
caches(this),
|
|
|
|
root_task(NULL),
|
|
|
|
curr_task(NULL),
|
2010-08-27 20:26:36 -05:00
|
|
|
rval(0),
|
2010-09-07 20:39:07 -05:00
|
|
|
kernel(kernel),
|
|
|
|
message_queue(message_queue)
|
2010-06-23 23:03:09 -05:00
|
|
|
{
|
2011-04-07 15:05:45 -05:00
|
|
|
LOGPTR(this, "new dom", (uintptr_t)this);
|
2010-07-25 23:45:09 -05:00
|
|
|
isaac_init(this, &rctx);
|
|
|
|
#ifndef __WIN32__
|
2010-06-23 23:03:09 -05:00
|
|
|
pthread_attr_init(&attr);
|
|
|
|
pthread_attr_setstacksize(&attr, 1024 * 1024);
|
|
|
|
pthread_attr_setdetachstate(&attr, true);
|
|
|
|
#endif
|
2010-09-10 03:21:29 -05:00
|
|
|
root_task = create_task(NULL, name);
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
rust_dom::~rust_dom() {
|
2011-04-19 05:21:57 -05:00
|
|
|
DLOG(this, dom, "~rust_dom %s @0x%" PRIxPTR, name, (uintptr_t)this);
|
2010-09-10 03:21:29 -05:00
|
|
|
newborn_tasks.delete_all();
|
|
|
|
running_tasks.delete_all();
|
|
|
|
blocked_tasks.delete_all();
|
|
|
|
dead_tasks.delete_all();
|
2010-06-23 23:03:09 -05:00
|
|
|
#ifndef __WIN32__
|
|
|
|
pthread_attr_destroy(&attr);
|
|
|
|
#endif
|
2010-09-07 20:39:07 -05:00
|
|
|
while (caches.length()) {
|
2010-06-23 23:03:09 -05:00
|
|
|
delete caches.pop();
|
2010-09-07 20:39:07 -05:00
|
|
|
}
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
|
2011-05-24 16:00:45 -05:00
|
|
|
extern "C" void new_rust_activate_glue(rust_task *)
|
|
|
|
asm("new_rust_activate_glue");
|
|
|
|
|
2010-06-23 23:03:09 -05:00
|
|
|
void
|
|
|
|
rust_dom::activate(rust_task *task) {
|
|
|
|
curr_task = task;
|
2011-05-24 16:00:45 -05:00
|
|
|
new_rust_activate_glue(task);
|
2010-06-23 23:03:09 -05:00
|
|
|
curr_task = NULL;
|
|
|
|
}
|
|
|
|
|
2010-07-19 16:05:18 -05:00
|
|
|
void
|
2011-04-19 05:21:57 -05:00
|
|
|
rust_dom::log(rust_task* task, uint32_t level, char const *fmt, ...) {
|
2011-01-14 15:41:39 -06:00
|
|
|
char buf[BUF_BYTES];
|
2011-04-19 05:21:57 -05:00
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
|
|
|
vsnprintf(buf, sizeof(buf), fmt, args);
|
|
|
|
_log.trace_ln(task, level, buf);
|
|
|
|
va_end(args);
|
2010-07-19 16:05:18 -05:00
|
|
|
}
|
|
|
|
|
2010-06-23 23:03:09 -05:00
|
|
|
void
|
|
|
|
rust_dom::fail() {
|
2011-04-19 05:21:57 -05:00
|
|
|
log(NULL, log_err, "domain %s @0x%" PRIxPTR " root task failed",
|
2010-08-08 21:24:35 -05:00
|
|
|
name, this);
|
2010-06-23 23:03:09 -05:00
|
|
|
I(this, rval == 0);
|
|
|
|
rval = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2010-08-18 01:40:07 -05:00
|
|
|
rust_dom::malloc(size_t size) {
|
|
|
|
return malloc(size, memory_region::LOCAL);
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2010-08-18 01:40:07 -05:00
|
|
|
rust_dom::malloc(size_t size, memory_region::memory_region_type type) {
|
|
|
|
if (type == memory_region::LOCAL) {
|
|
|
|
return local_region.malloc(size);
|
|
|
|
} else if (type == memory_region::SYNCHRONIZED) {
|
|
|
|
return synchronized_region.malloc(size);
|
|
|
|
}
|
|
|
|
return NULL;
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2010-08-18 01:40:07 -05:00
|
|
|
rust_dom::calloc(size_t size) {
|
|
|
|
return calloc(size, memory_region::LOCAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
rust_dom::calloc(size_t size, memory_region::memory_region_type type) {
|
|
|
|
if (type == memory_region::LOCAL) {
|
|
|
|
return local_region.calloc(size);
|
|
|
|
} else if (type == memory_region::SYNCHRONIZED) {
|
|
|
|
return synchronized_region.calloc(size);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
rust_dom::realloc(void *mem, size_t size) {
|
|
|
|
return realloc(mem, size, memory_region::LOCAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
rust_dom::realloc(void *mem, size_t size,
|
|
|
|
memory_region::memory_region_type type) {
|
|
|
|
if (type == memory_region::LOCAL) {
|
|
|
|
return local_region.realloc(mem, size);
|
|
|
|
} else if (type == memory_region::SYNCHRONIZED) {
|
|
|
|
return synchronized_region.realloc(mem, size);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rust_dom::free(void *mem) {
|
|
|
|
free(mem, memory_region::LOCAL);
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-08-18 01:40:07 -05:00
|
|
|
rust_dom::free(void *mem, memory_region::memory_region_type type) {
|
2011-04-19 05:21:57 -05:00
|
|
|
DLOG(this, mem, "rust_dom::free(0x%" PRIxPTR ")", mem);
|
2010-08-18 01:40:07 -05:00
|
|
|
if (type == memory_region::LOCAL) {
|
|
|
|
local_region.free(mem);
|
|
|
|
} else if (type == memory_region::SYNCHRONIZED) {
|
|
|
|
synchronized_region.free(mem);
|
|
|
|
}
|
|
|
|
return;
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
|
2010-07-26 00:07:21 -05:00
|
|
|
#ifdef __WIN32__
|
|
|
|
void
|
|
|
|
rust_dom::win32_require(LPCTSTR fn, BOOL ok) {
|
|
|
|
if (!ok) {
|
|
|
|
LPTSTR buf;
|
|
|
|
DWORD err = GetLastError();
|
|
|
|
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
NULL, err,
|
|
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
|
|
(LPTSTR) &buf, 0, NULL );
|
2011-04-19 05:21:57 -05:00
|
|
|
DLOG_ERR(this, dom, "%s failed with error %ld: %s", fn, err, buf);
|
2010-07-26 00:07:21 -05:00
|
|
|
LocalFree((HLOCAL)buf);
|
|
|
|
I(this, ok);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-06-23 23:03:09 -05:00
|
|
|
size_t
|
2010-09-10 03:21:29 -05:00
|
|
|
rust_dom::number_of_live_tasks() {
|
2010-06-23 23:03:09 -05:00
|
|
|
return running_tasks.length() + blocked_tasks.length();
|
|
|
|
}
|
|
|
|
|
2010-07-19 16:05:18 -05:00
|
|
|
/**
|
|
|
|
* Delete any dead tasks.
|
|
|
|
*/
|
2010-06-23 23:03:09 -05:00
|
|
|
void
|
2010-07-19 16:05:18 -05:00
|
|
|
rust_dom::reap_dead_tasks() {
|
2010-06-23 23:03:09 -05:00
|
|
|
for (size_t i = 0; i < dead_tasks.length(); ) {
|
2010-07-19 16:05:18 -05:00
|
|
|
rust_task *task = dead_tasks[i];
|
|
|
|
if (task->ref_count == 0) {
|
2010-07-28 18:24:50 -05:00
|
|
|
I(this, task->tasks_waiting_to_join.is_empty());
|
2010-09-10 03:21:29 -05:00
|
|
|
dead_tasks.remove(task);
|
2011-04-19 05:21:57 -05:00
|
|
|
DLOG(this, task,
|
2010-08-08 21:24:35 -05:00
|
|
|
"deleting unreferenced dead task %s @0x%" PRIxPTR,
|
|
|
|
task->name, task);
|
2010-07-19 16:05:18 -05:00
|
|
|
delete task;
|
2010-06-23 23:03:09 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-19 16:05:18 -05:00
|
|
|
/**
|
|
|
|
* Drains and processes incoming pending messages.
|
|
|
|
*/
|
2010-08-24 23:06:56 -05:00
|
|
|
void rust_dom::drain_incoming_message_queue(bool process) {
|
2010-07-19 16:05:18 -05:00
|
|
|
rust_message *message;
|
2010-09-07 20:39:07 -05:00
|
|
|
while (message_queue->dequeue(&message)) {
|
2011-04-19 05:21:57 -05:00
|
|
|
DLOG(this, comm, "<== receiving \"%s\" " PTR,
|
2010-09-07 20:39:07 -05:00
|
|
|
message->label, message);
|
2010-08-24 23:06:56 -05:00
|
|
|
if (process) {
|
|
|
|
message->process();
|
|
|
|
}
|
2010-09-07 20:39:07 -05:00
|
|
|
delete message;
|
2010-07-28 18:24:50 -05:00
|
|
|
}
|
2010-07-28 18:46:13 -05:00
|
|
|
}
|
|
|
|
|
2010-07-19 16:05:18 -05:00
|
|
|
/**
|
|
|
|
* Schedules a running task for execution. Only running tasks can be
|
|
|
|
* activated. Blocked tasks have to be unblocked before they can be
|
|
|
|
* activated.
|
|
|
|
*
|
|
|
|
* Returns NULL if no tasks can be scheduled.
|
|
|
|
*/
|
2010-06-23 23:03:09 -05:00
|
|
|
rust_task *
|
2010-08-18 01:40:07 -05:00
|
|
|
rust_dom::schedule_task() {
|
2010-06-23 23:03:09 -05:00
|
|
|
I(this, this);
|
|
|
|
// FIXME: in the face of failing tasks, this is not always right.
|
|
|
|
// I(this, n_live_tasks() > 0);
|
|
|
|
if (running_tasks.length() > 0) {
|
|
|
|
size_t i = rand(&rctx);
|
|
|
|
i %= running_tasks.length();
|
2010-08-11 23:23:34 -05:00
|
|
|
if (running_tasks[i]->yield_timer.has_timed_out()) {
|
|
|
|
return (rust_task *)running_tasks[i];
|
|
|
|
}
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-07-28 16:53:08 -05:00
|
|
|
void
|
|
|
|
rust_dom::log_state() {
|
2011-04-19 05:21:57 -05:00
|
|
|
if (log_rt_task < log_note) return;
|
|
|
|
|
2010-07-28 16:53:08 -05:00
|
|
|
if (!running_tasks.is_empty()) {
|
2011-04-19 05:21:57 -05:00
|
|
|
log(NULL, log_note, "running tasks:");
|
2010-07-28 16:53:08 -05:00
|
|
|
for (size_t i = 0; i < running_tasks.length(); i++) {
|
2011-04-19 05:21:57 -05:00
|
|
|
log(NULL, log_note, "\t task: %s @0x%" PRIxPTR " timeout: %d",
|
2010-08-11 23:23:34 -05:00
|
|
|
running_tasks[i]->name,
|
|
|
|
running_tasks[i],
|
|
|
|
running_tasks[i]->yield_timer.get_timeout());
|
2010-07-28 16:53:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!blocked_tasks.is_empty()) {
|
2011-04-19 05:21:57 -05:00
|
|
|
log(NULL, log_note, "blocked tasks:");
|
2010-07-28 16:53:08 -05:00
|
|
|
for (size_t i = 0; i < blocked_tasks.length(); i++) {
|
2011-04-19 05:21:57 -05:00
|
|
|
log(NULL, log_note, "\t task: %s @0x%" PRIxPTR ", blocked on: 0x%"
|
|
|
|
PRIxPTR " '%s'",
|
2010-08-08 21:24:35 -05:00
|
|
|
blocked_tasks[i]->name, blocked_tasks[i],
|
2010-08-18 01:26:43 -05:00
|
|
|
blocked_tasks[i]->cond, blocked_tasks[i]->cond_name);
|
2010-07-28 16:53:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dead_tasks.is_empty()) {
|
2011-04-19 05:21:57 -05:00
|
|
|
log(NULL, log_note, "dead tasks:");
|
2010-07-28 16:53:08 -05:00
|
|
|
for (size_t i = 0; i < dead_tasks.length(); i++) {
|
2011-04-19 05:21:57 -05:00
|
|
|
log(NULL, log_note, "\t task: %s 0x%" PRIxPTR ", ref_count: %d",
|
2010-08-18 01:26:43 -05:00
|
|
|
dead_tasks[i]->name, dead_tasks[i],
|
2010-08-10 15:26:00 -05:00
|
|
|
dead_tasks[i]->ref_count);
|
2010-07-28 16:53:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-07-19 16:05:18 -05:00
|
|
|
/**
|
|
|
|
* Starts the main scheduler loop which performs task scheduling for this
|
|
|
|
* domain.
|
|
|
|
*
|
2010-07-28 18:49:16 -05:00
|
|
|
* Returns once no more tasks can be scheduled and all task ref_counts
|
|
|
|
* drop to zero.
|
2010-07-19 16:05:18 -05:00
|
|
|
*/
|
|
|
|
int
|
2010-09-10 03:21:29 -05:00
|
|
|
rust_dom::start_main_loop() {
|
2010-07-19 16:05:18 -05:00
|
|
|
// Make sure someone is watching, to pull us out of infinite loops.
|
|
|
|
rust_timer timer(this);
|
|
|
|
|
2011-04-19 05:21:57 -05:00
|
|
|
DLOG(this, dom, "started domain loop");
|
2010-07-19 16:05:18 -05:00
|
|
|
|
2010-09-10 03:21:29 -05:00
|
|
|
while (number_of_live_tasks() > 0) {
|
2010-09-07 20:39:07 -05:00
|
|
|
A(this, kernel->is_deadlocked() == false, "deadlock");
|
2010-08-18 01:47:11 -05:00
|
|
|
|
2010-08-24 23:06:56 -05:00
|
|
|
drain_incoming_message_queue(true);
|
2010-07-28 18:24:50 -05:00
|
|
|
|
2010-07-19 16:05:18 -05:00
|
|
|
rust_task *scheduled_task = schedule_task();
|
|
|
|
|
2010-08-16 20:44:26 -05:00
|
|
|
// The scheduler busy waits until a task is available for scheduling.
|
|
|
|
// Eventually we'll want a smarter way to do this, perhaps sleep
|
|
|
|
// for a minimum amount of time.
|
2010-07-19 16:05:18 -05:00
|
|
|
|
|
|
|
if (scheduled_task == NULL) {
|
2011-04-19 05:21:57 -05:00
|
|
|
log_state();
|
|
|
|
DLOG(this, task,
|
2010-08-09 10:06:08 -05:00
|
|
|
"all tasks are blocked, scheduler yielding ...");
|
2010-09-15 13:56:45 -05:00
|
|
|
sync::sleep(100);
|
2011-04-19 05:21:57 -05:00
|
|
|
DLOG(this, task,
|
2010-08-09 10:06:08 -05:00
|
|
|
"scheduler resuming ...");
|
2010-07-19 16:05:18 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
I(this, scheduled_task->running());
|
|
|
|
|
2011-04-19 05:21:57 -05:00
|
|
|
DLOG(this, task,
|
2010-08-10 15:26:00 -05:00
|
|
|
"activating task %s 0x%" PRIxPTR
|
|
|
|
", sp=0x%" PRIxPTR
|
|
|
|
", ref_count=%d"
|
|
|
|
", state: %s",
|
|
|
|
scheduled_task->name,
|
|
|
|
(uintptr_t)scheduled_task,
|
|
|
|
scheduled_task->rust_sp,
|
|
|
|
scheduled_task->ref_count,
|
2010-09-10 03:21:29 -05:00
|
|
|
scheduled_task->state->name);
|
2010-07-19 16:05:18 -05:00
|
|
|
|
|
|
|
interrupt_flag = 0;
|
|
|
|
|
|
|
|
activate(scheduled_task);
|
|
|
|
|
2011-04-19 05:21:57 -05:00
|
|
|
DLOG(this, task,
|
2010-08-08 21:24:35 -05:00
|
|
|
"returned from task %s @0x%" PRIxPTR
|
2010-07-19 16:05:18 -05:00
|
|
|
" in state '%s', sp=0x%" PRIxPTR,
|
2010-08-08 21:24:35 -05:00
|
|
|
scheduled_task->name,
|
2010-07-19 16:05:18 -05:00
|
|
|
(uintptr_t)scheduled_task,
|
2010-09-10 03:21:29 -05:00
|
|
|
scheduled_task->state->name,
|
2010-07-19 16:05:18 -05:00
|
|
|
scheduled_task->rust_sp);
|
|
|
|
|
|
|
|
I(this, scheduled_task->rust_sp >=
|
|
|
|
(uintptr_t) &scheduled_task->stk->data[0]);
|
|
|
|
I(this, scheduled_task->rust_sp < scheduled_task->stk->limit);
|
|
|
|
|
|
|
|
reap_dead_tasks();
|
|
|
|
}
|
|
|
|
|
2011-04-19 05:21:57 -05:00
|
|
|
DLOG(this, dom,
|
2011-04-07 15:05:45 -05:00
|
|
|
"terminated scheduler loop, reaping dead tasks ...");
|
2010-07-19 16:05:18 -05:00
|
|
|
|
|
|
|
while (dead_tasks.length() > 0) {
|
2010-09-07 20:39:07 -05:00
|
|
|
if (message_queue->is_empty()) {
|
2011-04-19 05:21:57 -05:00
|
|
|
DLOG(this, dom,
|
2010-08-09 10:06:08 -05:00
|
|
|
"waiting for %d dead tasks to become dereferenced, "
|
|
|
|
"scheduler yielding ...",
|
|
|
|
dead_tasks.length());
|
2011-04-19 05:21:57 -05:00
|
|
|
log_state();
|
2010-08-09 10:06:08 -05:00
|
|
|
sync::yield();
|
2010-07-19 16:05:18 -05:00
|
|
|
} else {
|
2010-08-24 23:06:56 -05:00
|
|
|
drain_incoming_message_queue(true);
|
2010-07-19 16:05:18 -05:00
|
|
|
}
|
|
|
|
reap_dead_tasks();
|
|
|
|
}
|
|
|
|
|
2011-04-19 05:21:57 -05:00
|
|
|
DLOG(this, dom, "finished main-loop (dom.rval = %d)", rval);
|
2010-07-19 16:05:18 -05:00
|
|
|
return rval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-23 23:03:09 -05:00
|
|
|
rust_crate_cache *
|
|
|
|
rust_dom::get_cache(rust_crate const *crate) {
|
2011-04-19 05:21:57 -05:00
|
|
|
DLOG(this, cache, "looking for crate-cache for crate 0x%" PRIxPTR, crate);
|
2010-06-23 23:03:09 -05:00
|
|
|
rust_crate_cache *cache = NULL;
|
|
|
|
for (size_t i = 0; i < caches.length(); ++i) {
|
|
|
|
rust_crate_cache *c = caches[i];
|
|
|
|
if (c->crate == crate) {
|
|
|
|
cache = c;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!cache) {
|
2011-04-19 05:21:57 -05:00
|
|
|
DLOG(this, cache,
|
2010-06-23 23:03:09 -05:00
|
|
|
"making new crate-cache for crate 0x%" PRIxPTR, crate);
|
|
|
|
cache = new (this) rust_crate_cache(this, crate);
|
|
|
|
caches.push(cache);
|
|
|
|
}
|
|
|
|
cache->ref();
|
|
|
|
return cache;
|
|
|
|
}
|
|
|
|
|
2010-09-10 03:21:29 -05:00
|
|
|
rust_task *
|
|
|
|
rust_dom::create_task(rust_task *spawner, const char *name) {
|
|
|
|
rust_task *task =
|
|
|
|
new (this) rust_task (this, &newborn_tasks, spawner, name);
|
2011-04-19 05:21:57 -05:00
|
|
|
DLOG(this, task, "created task: " PTR ", spawner: %s, name: %s",
|
2010-09-10 03:21:29 -05:00
|
|
|
task, spawner ? spawner->name : "null", name);
|
|
|
|
newborn_tasks.append(task);
|
|
|
|
return task;
|
|
|
|
}
|
2010-06-23 23:03:09 -05:00
|
|
|
|
|
|
|
//
|
|
|
|
// Local Variables:
|
|
|
|
// mode: C++
|
|
|
|
// fill-column: 70;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// compile-command: "make -k -C .. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
|
|
|
// End:
|
|
|
|
//
|