2010-08-27 20:26:36 -05:00
|
|
|
#include "rust_internal.h"
|
|
|
|
|
2011-07-27 18:33:31 -05:00
|
|
|
#define KLOG_(...) \
|
|
|
|
KLOG(this, kern, __VA_ARGS__)
|
2011-07-28 12:41:48 -05:00
|
|
|
#define KLOG_ERR_(field, ...) \
|
|
|
|
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),
|
|
|
|
_interrupt_kernel_loop(FALSE),
|
|
|
|
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();
|
2010-09-07 20:39:07 -05:00
|
|
|
rust_message_queue *message_queue =
|
2011-07-23 21:03:02 -05:00
|
|
|
new (this, "rust_message_queue") rust_message_queue(srv, this);
|
|
|
|
rust_srv *srv = this->srv->clone();
|
2011-06-28 14:15:41 -05:00
|
|
|
rust_scheduler *sched =
|
2011-07-18 14:02:26 -05:00
|
|
|
new (this, "rust_scheduler")
|
2011-07-23 21:03:02 -05:00
|
|
|
rust_scheduler(this, message_queue, srv, id);
|
2011-06-28 14:15:41 -05:00
|
|
|
rust_handle<rust_scheduler> *handle = internal_get_sched_handle(sched);
|
2010-09-07 20:39:07 -05:00
|
|
|
message_queue->associate(handle);
|
|
|
|
message_queues.append(message_queue);
|
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);
|
|
|
|
sched->message_queue->disassociate();
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-28 14:15:41 -05:00
|
|
|
rust_handle<rust_scheduler> *
|
|
|
|
rust_kernel::internal_get_sched_handle(rust_scheduler *sched) {
|
|
|
|
rust_handle<rust_scheduler> *handle = NULL;
|
|
|
|
if (_sched_handles.get(sched, &handle) == false) {
|
2011-07-18 14:02:26 -05:00
|
|
|
handle = new (this, "rust_handle<rust_scheduler")
|
|
|
|
rust_handle<rust_scheduler>(this, sched->message_queue, sched);
|
2011-06-28 14:15:41 -05:00
|
|
|
_sched_handles.put(sched, handle);
|
2010-09-07 20:39:07 -05:00
|
|
|
}
|
2010-09-08 17:16:14 -05:00
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2010-09-07 20:39:07 -05:00
|
|
|
rust_handle<rust_task> *
|
|
|
|
rust_kernel::get_task_handle(rust_task *task) {
|
2010-09-08 21:13:49 -05:00
|
|
|
_kernel_lock.lock();
|
2010-09-07 20:39:07 -05:00
|
|
|
rust_handle<rust_task> *handle = NULL;
|
2010-09-08 17:16:14 -05:00
|
|
|
if (_task_handles.get(task, &handle) == false) {
|
|
|
|
handle =
|
2011-07-18 14:02:26 -05:00
|
|
|
new (this, "rust_handle<rust_task>")
|
|
|
|
rust_handle<rust_task>(this, task->sched->message_queue, task);
|
2010-09-08 17:16:14 -05:00
|
|
|
_task_handles.put(task, handle);
|
2010-09-07 20:39:07 -05:00
|
|
|
}
|
2010-09-08 21:13:49 -05:00
|
|
|
_kernel_lock.unlock();
|
2010-09-07 20:39:07 -05:00
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
rust_handle<rust_port> *
|
|
|
|
rust_kernel::get_port_handle(rust_port *port) {
|
2010-09-08 21:13:49 -05:00
|
|
|
_kernel_lock.lock();
|
2010-09-07 20:39:07 -05:00
|
|
|
rust_handle<rust_port> *handle = NULL;
|
2010-09-08 17:16:14 -05:00
|
|
|
if (_port_handles.get(port, &handle) == false) {
|
2011-07-18 14:02:26 -05:00
|
|
|
handle = new (this, "rust_handle<rust_port>")
|
2011-06-29 17:11:20 -05:00
|
|
|
rust_handle<rust_port>(this,
|
|
|
|
port->task->sched->message_queue,
|
|
|
|
port);
|
2010-09-08 17:16:14 -05:00
|
|
|
_port_handles.put(port, handle);
|
2010-09-07 20:39:07 -05:00
|
|
|
}
|
2010-09-08 21:13:49 -05:00
|
|
|
_kernel_lock.unlock();
|
2010-09-07 20:39:07 -05:00
|
|
|
return handle;
|
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
|
|
|
void
|
2010-09-08 01:37:51 -05:00
|
|
|
rust_kernel::pump_message_queues() {
|
|
|
|
for (size_t i = 0; i < message_queues.length(); i++) {
|
|
|
|
rust_message_queue *queue = message_queues[i];
|
|
|
|
if (queue->is_associated() == false) {
|
|
|
|
rust_message *message = NULL;
|
|
|
|
while (queue->dequeue(&message)) {
|
|
|
|
message->kernel_process();
|
|
|
|
delete message;
|
2010-09-07 20:39:07 -05:00
|
|
|
}
|
|
|
|
}
|
2010-09-08 01:37:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rust_kernel::start_kernel_loop() {
|
2010-09-08 21:13:49 -05:00
|
|
|
_kernel_lock.lock();
|
2010-09-08 01:37:51 -05:00
|
|
|
while (_interrupt_kernel_loop == false) {
|
2010-09-08 21:13:49 -05:00
|
|
|
_kernel_lock.wait();
|
2010-09-08 01:37:51 -05:00
|
|
|
pump_message_queues();
|
2010-09-07 20:39:07 -05:00
|
|
|
}
|
2010-09-08 21:13:49 -05:00
|
|
|
_kernel_lock.unlock();
|
2010-09-07 20:39:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rust_kernel::run() {
|
2011-07-27 18:33:31 -05:00
|
|
|
KLOG_("started kernel loop");
|
2010-09-07 20:39:07 -05:00
|
|
|
start_kernel_loop();
|
2011-07-27 18:33:31 -05:00
|
|
|
KLOG_("finished kernel loop");
|
2010-09-07 20:39:07 -05:00
|
|
|
}
|
|
|
|
|
2010-09-08 01:37:51 -05:00
|
|
|
void
|
|
|
|
rust_kernel::terminate_kernel_loop() {
|
2011-07-27 18:33:31 -05:00
|
|
|
KLOG_("terminating kernel loop");
|
2010-09-08 01:37:51 -05:00
|
|
|
_interrupt_kernel_loop = true;
|
2010-09-15 13:56:45 -05:00
|
|
|
signal_kernel_lock();
|
2010-09-08 01:37:51 -05:00
|
|
|
join();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2010-09-08 01:37:51 -05:00
|
|
|
terminate_kernel_loop();
|
|
|
|
|
|
|
|
// It's possible that the message pump misses some messages because
|
|
|
|
// of races, so pump any remaining messages here. By now all domain
|
|
|
|
// threads should have been joined, so we shouldn't miss any more
|
|
|
|
// messages.
|
|
|
|
pump_message_queues();
|
2010-09-07 20:39:07 -05:00
|
|
|
|
2011-07-27 18:33:31 -05:00
|
|
|
KLOG_("freeing handles");
|
2010-09-08 17:16:14 -05:00
|
|
|
|
2010-09-07 20:39:07 -05:00
|
|
|
free_handles(_task_handles);
|
2011-07-27 18:33:31 -05:00
|
|
|
KLOG_("..task handles freed");
|
2010-09-07 20:39:07 -05:00
|
|
|
free_handles(_port_handles);
|
2011-07-27 18:33:31 -05:00
|
|
|
KLOG_("..port handles freed");
|
2011-06-28 14:15:41 -05:00
|
|
|
free_handles(_sched_handles);
|
2011-07-27 18:33:31 -05:00
|
|
|
KLOG_("..sched handles freed");
|
2010-09-07 20:39:07 -05:00
|
|
|
|
2011-07-27 18:33:31 -05:00
|
|
|
KLOG_("freeing queues");
|
2010-09-08 17:16:14 -05:00
|
|
|
|
2010-09-07 20:39:07 -05:00
|
|
|
rust_message_queue *queue = NULL;
|
|
|
|
while (message_queues.pop(&queue)) {
|
2011-07-23 21:03:02 -05:00
|
|
|
K(srv, queue->is_empty(), "Kernel message queue should be empty "
|
2010-09-07 20:39:07 -05:00
|
|
|
"before killing the kernel.");
|
|
|
|
delete queue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
template<class T> void
|
|
|
|
rust_kernel::free_handles(hash_map<T*, rust_handle<T>* > &map) {
|
|
|
|
T* key;
|
|
|
|
rust_handle<T> *value;
|
|
|
|
while (map.pop(&key, &value)) {
|
2011-07-27 18:33:31 -05:00
|
|
|
KLOG_("...freeing " PTR, value);
|
2010-09-07 20:39:07 -05:00
|
|
|
delete value;
|
|
|
|
}
|
|
|
|
}
|
2010-09-08 17:48:10 -05:00
|
|
|
|
2010-09-15 13:56:45 -05:00
|
|
|
void
|
|
|
|
rust_kernel::notify_message_enqueued(rust_message_queue *queue,
|
|
|
|
rust_message *message) {
|
|
|
|
// The message pump needs to handle this message if the queue is not
|
|
|
|
// associated with a domain, therefore signal the message pump.
|
|
|
|
if (queue->is_associated() == false) {
|
|
|
|
signal_kernel_lock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07-23 16:01:43 -05:00
|
|
|
rust_task *
|
|
|
|
rust_kernel::create_task(rust_task *spawner, const char *name) {
|
2011-07-23 21:03:02 -05:00
|
|
|
return threads[rand(&rctx) % num_threads]->create_task(spawner, name);
|
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:
|
|
|
|
//
|