2010-08-27 20:26:36 -05:00
|
|
|
#include "rust_internal.h"
|
|
|
|
|
2011-04-19 05:21:57 -05:00
|
|
|
#define KLOG(...) \
|
|
|
|
do { \
|
|
|
|
if (log_rt_kern >= log_note) { \
|
|
|
|
log(log_note, __VA_ARGS__); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2011-04-07 15:05:45 -05:00
|
|
|
|
2010-08-27 20:26:36 -05:00
|
|
|
rust_kernel::rust_kernel(rust_srv *srv) :
|
2010-09-07 20:39:07 -05:00
|
|
|
_region(&srv->local_region),
|
2010-08-27 20:26:36 -05:00
|
|
|
_log(srv, NULL),
|
2010-09-07 20:39:07 -05:00
|
|
|
_srv(srv),
|
2011-06-24 17:56:12 -05:00
|
|
|
_interrupt_kernel_loop(FALSE)
|
|
|
|
{
|
|
|
|
dom = create_domain("main");
|
2010-08-27 20:26:36 -05:00
|
|
|
}
|
|
|
|
|
2011-06-24 17:56:12 -05:00
|
|
|
rust_dom *
|
2011-05-24 18:07:30 -05:00
|
|
|
rust_kernel::create_domain(const char *name) {
|
2010-09-08 21:13:49 -05:00
|
|
|
_kernel_lock.lock();
|
2010-09-07 20:39:07 -05:00
|
|
|
rust_message_queue *message_queue =
|
|
|
|
new (this) rust_message_queue(_srv, this);
|
|
|
|
rust_srv *srv = _srv->clone();
|
|
|
|
rust_dom *dom =
|
2011-05-24 18:07:30 -05:00
|
|
|
new (this) rust_dom(this, message_queue, srv, name);
|
2010-09-08 17:16:14 -05:00
|
|
|
rust_handle<rust_dom> *handle = internal_get_dom_handle(dom);
|
2010-09-07 20:39:07 -05:00
|
|
|
message_queue->associate(handle);
|
|
|
|
message_queues.append(message_queue);
|
2011-06-24 17:56:12 -05:00
|
|
|
KLOG("created domain: " PTR ", name: %s, index: %d",
|
|
|
|
dom, name, dom->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-24 17:56:12 -05:00
|
|
|
return dom;
|
2010-08-27 20:26:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-06-24 17:56:12 -05:00
|
|
|
rust_kernel::destroy_domain() {
|
2010-09-08 21:13:49 -05:00
|
|
|
_kernel_lock.lock();
|
2011-06-24 17:56:12 -05:00
|
|
|
KLOG("deleting domain: " PTR ", name: %s, index: %d",
|
|
|
|
dom, dom->name, dom->list_index);
|
2010-09-07 20:39:07 -05:00
|
|
|
dom->message_queue->disassociate();
|
|
|
|
rust_srv *srv = dom->srv;
|
|
|
|
delete dom;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
rust_handle<rust_dom> *
|
2010-09-08 17:16:14 -05:00
|
|
|
rust_kernel::internal_get_dom_handle(rust_dom *dom) {
|
2010-09-07 20:39:07 -05:00
|
|
|
rust_handle<rust_dom> *handle = NULL;
|
2010-09-08 17:16:14 -05:00
|
|
|
if (_dom_handles.get(dom, &handle) == false) {
|
|
|
|
handle =
|
|
|
|
new (this) rust_handle<rust_dom>(this, dom->message_queue, dom);
|
|
|
|
_dom_handles.put(dom, handle);
|
2010-09-07 20:39:07 -05:00
|
|
|
}
|
2010-09-08 17:16:14 -05:00
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
rust_handle<rust_dom> *
|
|
|
|
rust_kernel::get_dom_handle(rust_dom *dom) {
|
2010-09-08 21:13:49 -05:00
|
|
|
_kernel_lock.lock();
|
2010-09-08 17:16:14 -05:00
|
|
|
rust_handle<rust_dom> *handle = internal_get_dom_handle(dom);
|
2010-09-08 21:13:49 -05:00
|
|
|
_kernel_lock.unlock();
|
2010-09-07 20:39:07 -05:00
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 =
|
|
|
|
new (this) rust_handle<rust_task>(this, task->dom->message_queue,
|
|
|
|
task);
|
|
|
|
_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) {
|
|
|
|
handle =
|
|
|
|
new (this) rust_handle<rust_port>(this,
|
|
|
|
port->task->dom->message_queue,
|
|
|
|
port);
|
|
|
|
_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
|
|
|
|
rust_kernel::log_all_domain_state() {
|
2011-06-24 17:56:12 -05:00
|
|
|
KLOG("log_all_domain_state");
|
|
|
|
dom->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
|
|
|
|
|
|
|
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-04-07 15:05:45 -05:00
|
|
|
KLOG("started kernel loop");
|
2010-09-07 20:39:07 -05:00
|
|
|
start_kernel_loop();
|
2011-04-07 15:05:45 -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-04-07 15:05:45 -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-06-24 17:56:12 -05:00
|
|
|
destroy_domain();
|
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-04-07 15:05:45 -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-05-31 19:44:54 -05:00
|
|
|
KLOG("..task handles freed");
|
2010-09-07 20:39:07 -05:00
|
|
|
free_handles(_port_handles);
|
2011-05-31 19:44:54 -05:00
|
|
|
KLOG("..port handles freed");
|
2010-09-07 20:39:07 -05:00
|
|
|
free_handles(_dom_handles);
|
2011-05-31 19:44:54 -05:00
|
|
|
KLOG("..dom handles freed");
|
2010-09-07 20:39:07 -05:00
|
|
|
|
2011-04-07 15:05:45 -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)) {
|
|
|
|
K(_srv, queue->is_empty(), "Kernel message queue should be empty "
|
|
|
|
"before killing the kernel.");
|
|
|
|
delete queue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
rust_kernel::malloc(size_t size) {
|
|
|
|
return _region->malloc(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void rust_kernel::free(void *mem) {
|
|
|
|
_region->free(mem);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-05-31 19:44:54 -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();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
// compile-command: "make -k -C .. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
|
|
|
// End:
|
|
|
|
//
|