2010-08-27 18:26:36 -07:00
|
|
|
#include "rust_internal.h"
|
2012-01-06 12:06:35 -08:00
|
|
|
#include "rust_util.h"
|
2012-02-03 15:12:18 -08:00
|
|
|
#include "rust_scheduler.h"
|
2010-08-27 18:26:36 -07:00
|
|
|
|
2012-03-28 13:49:39 -07:00
|
|
|
#include <vector>
|
|
|
|
|
2011-07-29 11:00:44 -07:00
|
|
|
#define KLOG_(...) \
|
2011-07-27 16:33:31 -07:00
|
|
|
KLOG(this, kern, __VA_ARGS__)
|
2011-07-29 11:00:44 -07:00
|
|
|
#define KLOG_ERR_(field, ...) \
|
2011-07-28 10:41:48 -07:00
|
|
|
KLOG_LVL(this, field, log_err, __VA_ARGS__)
|
2011-04-07 22:05:45 +02:00
|
|
|
|
2012-04-01 22:18:40 -05:00
|
|
|
rust_kernel::rust_kernel(rust_env *env) :
|
|
|
|
_region(env, true),
|
2012-04-02 03:11:58 -05:00
|
|
|
_log(NULL),
|
2012-02-03 17:26:54 -08:00
|
|
|
max_task_id(0),
|
2012-03-14 17:24:19 -07:00
|
|
|
max_port_id(0),
|
2012-02-03 17:26:54 -08:00
|
|
|
rval(0),
|
2012-02-07 16:11:57 -08:00
|
|
|
max_sched_id(0),
|
2012-03-30 13:54:37 -07:00
|
|
|
sched_reaper(this),
|
2012-04-01 22:18:40 -05:00
|
|
|
env(env)
|
2011-06-24 15:56:12 -07:00
|
|
|
{
|
2012-02-04 14:54:10 -08:00
|
|
|
}
|
|
|
|
|
2010-08-27 18:26:36 -07:00
|
|
|
void
|
2011-04-19 12:21:57 +02:00
|
|
|
rust_kernel::log(uint32_t level, char const *fmt, ...) {
|
2011-01-14 16:01:43 -08:00
|
|
|
char buf[BUF_BYTES];
|
2011-04-19 12:21:57 +02: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 18:26:36 -07:00
|
|
|
}
|
2010-09-07 18:39:07 -07:00
|
|
|
|
2011-07-06 15:06:30 -07: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 18:39:07 -07:00
|
|
|
void *
|
2011-07-18 12:02:26 -07:00
|
|
|
rust_kernel::malloc(size_t size, const char *tag) {
|
|
|
|
return _region.malloc(size, tag);
|
2010-09-07 18:39:07 -07:00
|
|
|
}
|
|
|
|
|
2011-07-05 22:44:22 -07:00
|
|
|
void *
|
|
|
|
rust_kernel::realloc(void *mem, size_t size) {
|
2011-07-18 12:02:26 -07:00
|
|
|
return _region.realloc(mem, size);
|
2011-07-05 22:44:22 -07:00
|
|
|
}
|
|
|
|
|
2010-09-07 18:39:07 -07:00
|
|
|
void rust_kernel::free(void *mem) {
|
2011-07-18 12:02:26 -07:00
|
|
|
_region.free(mem);
|
2010-09-07 18:39:07 -07:00
|
|
|
}
|
|
|
|
|
2012-02-06 21:06:12 -08:00
|
|
|
rust_sched_id
|
|
|
|
rust_kernel::create_scheduler(size_t num_threads) {
|
2012-02-07 17:43:54 -08:00
|
|
|
rust_sched_id id;
|
2012-02-07 16:11:57 -08:00
|
|
|
rust_scheduler *sched;
|
|
|
|
{
|
|
|
|
scoped_lock with(sched_lock);
|
2012-03-30 13:54:37 -07:00
|
|
|
// If this is the first scheduler then we need to launch
|
|
|
|
// the scheduler reaper.
|
|
|
|
bool start_reaper = sched_table.empty();
|
2012-02-07 17:43:54 -08:00
|
|
|
id = max_sched_id++;
|
2012-04-01 21:14:16 -05:00
|
|
|
assert(id != INTPTR_MAX && "Hit the maximum scheduler id");
|
2012-02-07 16:11:57 -08:00
|
|
|
sched = new (this, "rust_scheduler")
|
2012-04-01 22:18:40 -05:00
|
|
|
rust_scheduler(this, num_threads, id);
|
2012-02-07 16:11:57 -08:00
|
|
|
bool is_new = sched_table
|
2012-03-31 23:12:06 -07:00
|
|
|
.insert(std::pair<rust_sched_id,
|
|
|
|
rust_scheduler*>(id, sched)).second;
|
2012-04-01 21:14:16 -05:00
|
|
|
assert(is_new && "Reusing a sched id?");
|
2012-03-30 13:54:37 -07:00
|
|
|
if (start_reaper) {
|
|
|
|
sched_reaper.start();
|
|
|
|
}
|
2012-02-07 16:11:57 -08:00
|
|
|
}
|
2012-02-06 21:12:59 -08:00
|
|
|
sched->start_task_threads();
|
2012-02-07 17:43:54 -08:00
|
|
|
return id;
|
2011-06-24 16:50:06 -07:00
|
|
|
}
|
|
|
|
|
2012-02-03 15:45:59 -08:00
|
|
|
rust_scheduler *
|
2012-02-06 21:06:12 -08:00
|
|
|
rust_kernel::get_scheduler_by_id(rust_sched_id id) {
|
2012-02-07 16:11:57 -08:00
|
|
|
scoped_lock with(sched_lock);
|
|
|
|
sched_map::iterator iter = sched_table.find(id);
|
|
|
|
if (iter != sched_table.end()) {
|
|
|
|
return iter->second;
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-02-03 15:45:59 -08:00
|
|
|
}
|
|
|
|
|
2012-02-04 14:54:10 -08:00
|
|
|
void
|
2012-02-06 21:06:12 -08:00
|
|
|
rust_kernel::release_scheduler_id(rust_sched_id id) {
|
2012-02-04 14:54:10 -08:00
|
|
|
scoped_lock with(sched_lock);
|
2012-02-27 13:36:54 -08:00
|
|
|
// This list will most likely only ever have a single element in it, but
|
|
|
|
// it's an actual list because we could potentially get here multiple
|
|
|
|
// times before the main thread ever calls wait_for_schedulers()
|
|
|
|
join_list.push_back(id);
|
|
|
|
sched_lock.signal();
|
2012-02-04 14:54:10 -08:00
|
|
|
}
|
|
|
|
|
2012-02-27 13:36:54 -08:00
|
|
|
/*
|
2012-03-30 13:54:37 -07:00
|
|
|
Called by rust_sched_reaper to join every every terminating scheduler thread,
|
|
|
|
so that we can be sure they have completely exited before the process exits.
|
|
|
|
If we don't join them then we can see valgrind errors due to un-freed pthread
|
|
|
|
memory.
|
2012-02-27 13:36:54 -08:00
|
|
|
*/
|
2012-03-30 13:54:37 -07:00
|
|
|
void
|
2012-02-06 21:06:12 -08:00
|
|
|
rust_kernel::wait_for_schedulers()
|
|
|
|
{
|
2012-02-06 21:12:59 -08:00
|
|
|
scoped_lock with(sched_lock);
|
2012-02-27 13:36:54 -08:00
|
|
|
while (!sched_table.empty()) {
|
|
|
|
while (!join_list.empty()) {
|
|
|
|
rust_sched_id id = join_list.back();
|
|
|
|
join_list.pop_back();
|
|
|
|
sched_map::iterator iter = sched_table.find(id);
|
2012-04-01 21:14:16 -05:00
|
|
|
assert(iter != sched_table.end());
|
2012-02-27 13:36:54 -08:00
|
|
|
rust_scheduler *sched = iter->second;
|
|
|
|
sched_table.erase(iter);
|
|
|
|
sched->join_task_threads();
|
|
|
|
delete sched;
|
|
|
|
}
|
|
|
|
if (!sched_table.empty()) {
|
|
|
|
sched_lock.wait();
|
|
|
|
}
|
2012-02-06 21:06:12 -08:00
|
|
|
}
|
2012-03-30 13:54:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Called on the main thread to wait for the kernel to exit */
|
|
|
|
int
|
|
|
|
rust_kernel::wait_for_exit() {
|
|
|
|
sched_reaper.join();
|
2012-02-06 21:12:59 -08:00
|
|
|
return rval;
|
2012-02-06 21:06:12 -08:00
|
|
|
}
|
|
|
|
|
2012-02-07 16:11:57 -08:00
|
|
|
// FIXME: Fix all these FIXMEs
|
2011-08-10 12:57:53 -07:00
|
|
|
void
|
|
|
|
rust_kernel::fail() {
|
2011-08-15 19:25:47 -07:00
|
|
|
// FIXME: On windows we're getting "Application has requested the
|
|
|
|
// Runtime to terminate it in an unusual way" when trying to shutdown
|
|
|
|
// cleanly.
|
2012-01-12 22:17:21 -08:00
|
|
|
set_exit_status(PROC_FAIL_CODE);
|
2011-08-15 19:25:47 -07:00
|
|
|
#if defined(__WIN32__)
|
2011-09-08 13:42:04 -07:00
|
|
|
exit(rval);
|
2011-08-15 19:25:47 -07:00
|
|
|
#endif
|
2012-02-07 16:11:57 -08:00
|
|
|
// Copy the list of schedulers so that we don't hold the lock while
|
|
|
|
// running kill_all_tasks.
|
|
|
|
// FIXME: There's a lot that happens under kill_all_tasks, and I don't
|
|
|
|
// know that holding sched_lock here is ok, but we need to hold the
|
|
|
|
// sched lock to prevent the scheduler from being destroyed while
|
|
|
|
// we are using it. Probably we need to make rust_scheduler atomicly
|
|
|
|
// reference counted.
|
|
|
|
std::vector<rust_scheduler*> scheds;
|
|
|
|
{
|
|
|
|
scoped_lock with(sched_lock);
|
|
|
|
for (sched_map::iterator iter = sched_table.begin();
|
|
|
|
iter != sched_table.end(); iter++) {
|
|
|
|
scheds.push_back(iter->second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: This is not a foolproof way to kill all tasks while ensuring
|
|
|
|
// that no new tasks or schedulers are created in the meantime that
|
|
|
|
// keep the scheduler alive.
|
|
|
|
for (std::vector<rust_scheduler*>::iterator iter = scheds.begin();
|
|
|
|
iter != scheds.end(); iter++) {
|
|
|
|
(*iter)->kill_all_tasks();
|
|
|
|
}
|
2011-08-10 12:57:53 -07:00
|
|
|
}
|
|
|
|
|
2012-03-14 20:55:57 -07:00
|
|
|
rust_task_id
|
|
|
|
rust_kernel::generate_task_id() {
|
|
|
|
rust_task_id id = sync::increment(max_task_id);
|
2012-04-01 21:14:16 -05:00
|
|
|
assert(id != INTPTR_MAX && "Hit the maximum task id");
|
2012-03-14 20:55:57 -07:00
|
|
|
return id;
|
2011-08-08 13:38:20 -07:00
|
|
|
}
|
|
|
|
|
2012-03-14 17:24:19 -07:00
|
|
|
rust_port_id
|
|
|
|
rust_kernel::register_port(rust_port *port) {
|
|
|
|
uintptr_t new_live_ports;
|
|
|
|
rust_port_id new_port_id;
|
|
|
|
{
|
|
|
|
scoped_lock with(port_lock);
|
|
|
|
new_port_id = max_port_id++;
|
|
|
|
port_table.put(new_port_id, port);
|
|
|
|
new_live_ports = port_table.count();
|
|
|
|
}
|
2012-04-01 21:14:16 -05:00
|
|
|
assert(new_port_id != INTPTR_MAX && "Hit the maximum port id");
|
2012-03-14 17:24:19 -07:00
|
|
|
KLOG_("Registered port %" PRIdPTR, new_port_id);
|
|
|
|
KLOG_("Total outstanding ports: %d", new_live_ports);
|
|
|
|
return new_port_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rust_kernel::release_port_id(rust_port_id id) {
|
|
|
|
KLOG_("Releasing port %" PRIdPTR, id);
|
|
|
|
uintptr_t new_live_ports;
|
|
|
|
{
|
|
|
|
scoped_lock with(port_lock);
|
|
|
|
port_table.remove(id);
|
|
|
|
new_live_ports = port_table.count();
|
|
|
|
}
|
|
|
|
KLOG_("Total outstanding ports: %d", new_live_ports);
|
|
|
|
}
|
|
|
|
|
|
|
|
rust_port *
|
|
|
|
rust_kernel::get_port_by_id(rust_port_id id) {
|
|
|
|
scoped_lock with(port_lock);
|
|
|
|
rust_port *port = NULL;
|
|
|
|
// get leaves port unchanged if not found.
|
|
|
|
port_table.get(id, &port);
|
|
|
|
if(port) {
|
|
|
|
port->ref();
|
|
|
|
}
|
|
|
|
return port;
|
|
|
|
}
|
|
|
|
|
2011-06-28 11:34:20 -07: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 10:41:48 -07:00
|
|
|
KLOG_ERR_(dom, "%s failed with error %ld: %s", fn, err, buf);
|
2011-06-28 11:34:20 -07:00
|
|
|
LocalFree((HLOCAL)buf);
|
2012-04-01 21:14:16 -05:00
|
|
|
assert(ok);
|
2011-06-28 11:34:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-01-12 22:17:21 -08:00
|
|
|
void
|
|
|
|
rust_kernel::set_exit_status(int code) {
|
2012-02-03 17:26:54 -08:00
|
|
|
scoped_lock with(rval_lock);
|
2012-01-12 22:17:21 -08:00
|
|
|
// If we've already failed then that's the code we're going to use
|
|
|
|
if (rval != PROC_FAIL_CODE) {
|
|
|
|
rval = code;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-08 15:48:10 -07:00
|
|
|
//
|
|
|
|
// Local Variables:
|
|
|
|
// mode: C++
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|
|
|
|
//
|