2010-08-27 20:26:36 -05:00
|
|
|
#include "rust_internal.h"
|
|
|
|
|
2011-07-29 13:00:44 -05:00
|
|
|
#define KLOG_(...) \
|
2011-07-27 18:33:31 -05:00
|
|
|
KLOG(this, kern, __VA_ARGS__)
|
2011-07-29 13:00:44 -05:00
|
|
|
#define KLOG_ERR_(field, ...) \
|
2011-07-28 12:41:48 -05:00
|
|
|
KLOG_LVL(this, field, log_err, __VA_ARGS__)
|
2011-04-07 15:05:45 -05:00
|
|
|
|
2011-07-23 21:03:02 -05:00
|
|
|
rust_kernel::rust_kernel(rust_srv *srv, size_t num_threads) :
|
2011-07-18 14:02:26 -05:00
|
|
|
_region(srv, true),
|
2010-08-27 20:26:36 -05:00
|
|
|
_log(srv, NULL),
|
2011-07-23 21:03:02 -05:00
|
|
|
srv(srv),
|
2011-08-08 15:38:20 -05:00
|
|
|
max_id(0),
|
2011-07-23 21:03:02 -05:00
|
|
|
num_threads(num_threads),
|
|
|
|
rval(0),
|
2011-07-27 16:34:39 -05:00
|
|
|
live_tasks(0),
|
|
|
|
env(srv->env)
|
2011-06-24 17:56:12 -05:00
|
|
|
{
|
2011-07-23 21:03:02 -05:00
|
|
|
isaac_init(this, &rctx);
|
|
|
|
create_schedulers();
|
2010-08-27 20:26:36 -05:00
|
|
|
}
|
|
|
|
|
2011-06-28 14:15:41 -05:00
|
|
|
rust_scheduler *
|
2011-07-23 21:03:02 -05:00
|
|
|
rust_kernel::create_scheduler(int id) {
|
2010-09-08 21:13:49 -05:00
|
|
|
_kernel_lock.lock();
|
2011-07-23 21:03:02 -05:00
|
|
|
rust_srv *srv = this->srv->clone();
|
2011-06-28 14:15:41 -05:00
|
|
|
rust_scheduler *sched =
|
2011-07-29 13:00:44 -05:00
|
|
|
new (this, "rust_scheduler") rust_scheduler(this, srv, id);
|
2011-07-27 18:33:31 -05:00
|
|
|
KLOG_("created scheduler: " PTR ", id: %d, index: %d",
|
|
|
|
sched, id, sched->list_index);
|
2010-09-09 18:01:49 -05:00
|
|
|
_kernel_lock.signal_all();
|
2010-09-09 00:20:31 -05:00
|
|
|
_kernel_lock.unlock();
|
2011-06-28 14:15:41 -05:00
|
|
|
return sched;
|
2010-08-27 20:26:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-07-23 21:03:02 -05:00
|
|
|
rust_kernel::destroy_scheduler(rust_scheduler *sched) {
|
2010-09-08 21:13:49 -05:00
|
|
|
_kernel_lock.lock();
|
2011-07-27 18:33:31 -05:00
|
|
|
KLOG_("deleting scheduler: " PTR ", name: %s, index: %d",
|
2011-06-28 14:15:41 -05:00
|
|
|
sched, sched->name, sched->list_index);
|
|
|
|
rust_srv *srv = sched->srv;
|
|
|
|
delete sched;
|
2010-09-07 20:39:07 -05:00
|
|
|
delete srv;
|
2010-09-09 18:01:49 -05:00
|
|
|
_kernel_lock.signal_all();
|
2010-09-09 00:20:31 -05:00
|
|
|
_kernel_lock.unlock();
|
2010-09-07 20:39:07 -05:00
|
|
|
}
|
|
|
|
|
2011-07-23 21:03:02 -05:00
|
|
|
void rust_kernel::create_schedulers() {
|
2011-07-27 16:34:39 -05:00
|
|
|
KLOG_("Using %d scheduler threads.", num_threads);
|
|
|
|
|
2011-07-25 20:00:37 -05:00
|
|
|
for(size_t i = 0; i < num_threads; ++i) {
|
2011-07-23 21:03:02 -05:00
|
|
|
threads.push(create_scheduler(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void rust_kernel::destroy_schedulers() {
|
2011-07-25 20:00:37 -05:00
|
|
|
for(size_t i = 0; i < num_threads; ++i) {
|
2011-07-23 21:03:02 -05:00
|
|
|
destroy_scheduler(threads[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-27 20:26:36 -05:00
|
|
|
void
|
2011-06-28 14:15:41 -05:00
|
|
|
rust_kernel::log_all_scheduler_state() {
|
2011-07-25 20:00:37 -05:00
|
|
|
for(size_t i = 0; i < num_threads; ++i) {
|
2011-07-23 21:03:02 -05:00
|
|
|
threads[i]->log_state();
|
|
|
|
}
|
2010-08-27 20:26:36 -05:00
|
|
|
}
|
|
|
|
|
2010-09-07 20:39:07 -05:00
|
|
|
/**
|
|
|
|
* Checks for simple deadlocks.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
rust_kernel::is_deadlocked() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-08-27 20:26:36 -05:00
|
|
|
void
|
2011-04-19 05:21:57 -05:00
|
|
|
rust_kernel::log(uint32_t level, char const *fmt, ...) {
|
2011-01-14 18:01:43 -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(NULL, level, buf);
|
|
|
|
va_end(args);
|
2010-08-27 20:26:36 -05:00
|
|
|
}
|
2010-09-07 20:39:07 -05:00
|
|
|
|
2011-07-06 17:06:30 -05:00
|
|
|
void
|
|
|
|
rust_kernel::fatal(char const *fmt, ...) {
|
|
|
|
char buf[BUF_BYTES];
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
|
|
|
vsnprintf(buf, sizeof(buf), fmt, args);
|
|
|
|
_log.trace_ln(NULL, (uint32_t)0, buf);
|
|
|
|
exit(1);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
2010-09-07 20:39:07 -05:00
|
|
|
rust_kernel::~rust_kernel() {
|
2011-07-23 21:03:02 -05:00
|
|
|
destroy_schedulers();
|
2010-09-07 20:39:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2011-07-18 14:02:26 -05:00
|
|
|
rust_kernel::malloc(size_t size, const char *tag) {
|
|
|
|
return _region.malloc(size, tag);
|
2010-09-07 20:39:07 -05:00
|
|
|
}
|
|
|
|
|
2011-07-06 00:44:22 -05:00
|
|
|
void *
|
|
|
|
rust_kernel::realloc(void *mem, size_t size) {
|
2011-07-18 14:02:26 -05:00
|
|
|
return _region.realloc(mem, size);
|
2011-07-06 00:44:22 -05:00
|
|
|
}
|
|
|
|
|
2010-09-07 20:39:07 -05:00
|
|
|
void rust_kernel::free(void *mem) {
|
2011-07-18 14:02:26 -05:00
|
|
|
_region.free(mem);
|
2010-09-07 20:39:07 -05:00
|
|
|
}
|
|
|
|
|
2010-09-15 13:56:45 -05:00
|
|
|
void
|
|
|
|
rust_kernel::signal_kernel_lock() {
|
|
|
|
_kernel_lock.lock();
|
|
|
|
_kernel_lock.signal_all();
|
|
|
|
_kernel_lock.unlock();
|
|
|
|
}
|
|
|
|
|
2011-07-23 21:03:02 -05:00
|
|
|
int rust_kernel::start_task_threads()
|
2011-06-24 18:50:06 -05:00
|
|
|
{
|
2011-07-25 20:00:37 -05:00
|
|
|
for(size_t i = 0; i < num_threads; ++i) {
|
2011-07-23 21:03:02 -05:00
|
|
|
rust_scheduler *thread = threads[i];
|
2011-06-24 18:50:06 -05:00
|
|
|
thread->start();
|
|
|
|
}
|
2011-07-13 17:44:09 -05:00
|
|
|
|
2011-07-25 20:00:37 -05:00
|
|
|
for(size_t i = 0; i < num_threads; ++i) {
|
2011-07-23 21:03:02 -05:00
|
|
|
rust_scheduler *thread = threads[i];
|
2011-06-24 18:50:06 -05:00
|
|
|
thread->join();
|
|
|
|
}
|
|
|
|
|
2011-07-23 21:03:02 -05:00
|
|
|
return rval;
|
2011-06-24 18:50:06 -05:00
|
|
|
}
|
|
|
|
|
2011-08-10 14:57:53 -05:00
|
|
|
void
|
|
|
|
rust_kernel::fail() {
|
2011-08-15 21:25:47 -05:00
|
|
|
// FIXME: On windows we're getting "Application has requested the
|
|
|
|
// Runtime to terminate it in an unusual way" when trying to shutdown
|
|
|
|
// cleanly.
|
|
|
|
#if defined(__WIN32__)
|
2011-08-24 13:55:31 -05:00
|
|
|
exit(1);
|
2011-08-15 21:25:47 -05:00
|
|
|
#endif
|
2011-08-10 14:57:53 -05:00
|
|
|
for(size_t i = 0; i < num_threads; ++i) {
|
|
|
|
rust_scheduler *thread = threads[i];
|
|
|
|
thread->kill_all_tasks();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-08 15:38:20 -05:00
|
|
|
rust_task_id
|
2011-07-23 16:01:43 -05:00
|
|
|
rust_kernel::create_task(rust_task *spawner, const char *name) {
|
2011-07-29 23:43:22 -05:00
|
|
|
rust_scheduler *thread = threads[rand(&rctx) % num_threads];
|
2011-08-08 15:38:20 -05:00
|
|
|
rust_task *t = thread->create_task(spawner, name);
|
|
|
|
{
|
|
|
|
scoped_lock with(_kernel_lock);
|
2011-08-17 16:42:28 -05:00
|
|
|
t->user.id = max_id++;
|
|
|
|
task_table.put(t->user.id, t);
|
2011-08-08 15:38:20 -05:00
|
|
|
}
|
2011-08-17 16:42:28 -05:00
|
|
|
return t->user.id;
|
2011-08-08 15:38:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
rust_task *
|
|
|
|
rust_kernel::get_task_by_id(rust_task_id id) {
|
|
|
|
scoped_lock with(_kernel_lock);
|
|
|
|
rust_task *task = NULL;
|
|
|
|
// get leaves task unchanged if not found.
|
|
|
|
task_table.get(id, &task);
|
2011-08-15 13:34:12 -05:00
|
|
|
if(task) {
|
|
|
|
if(task->get_ref_count() == 0) {
|
|
|
|
// this means the destructor is running, since the destructor
|
|
|
|
// grabs the kernel lock to unregister the task. Pretend this
|
|
|
|
// doesn't actually exist.
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
task->ref();
|
|
|
|
}
|
|
|
|
}
|
2011-08-08 15:38:20 -05:00
|
|
|
return task;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rust_kernel::release_task_id(rust_task_id id) {
|
|
|
|
scoped_lock with(_kernel_lock);
|
|
|
|
task_table.remove(id);
|
2011-07-23 16:01:43 -05:00
|
|
|
}
|
|
|
|
|
2011-07-25 20:00:37 -05:00
|
|
|
void rust_kernel::wakeup_schedulers() {
|
|
|
|
for(size_t i = 0; i < num_threads; ++i) {
|
|
|
|
threads[i]->lock.signal_all();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-28 13:34:20 -05:00
|
|
|
#ifdef __WIN32__
|
|
|
|
void
|
|
|
|
rust_kernel::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-07-28 12:41:48 -05:00
|
|
|
KLOG_ERR_(dom, "%s failed with error %ld: %s", fn, err, buf);
|
2011-06-28 13:34:20 -05:00
|
|
|
LocalFree((HLOCAL)buf);
|
2011-07-28 12:41:48 -05:00
|
|
|
I(this, ok);
|
2011-06-28 13:34:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-09-08 17:48:10 -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-09-08 17:48:10 -05:00
|
|
|
// End:
|
|
|
|
//
|