rust/src/rt/rust.cpp

141 lines
4.3 KiB
C++
Raw Normal View History

2010-06-23 21:03:09 -07:00
#include "rust_internal.h"
struct
command_line_args : public kernel_owned<command_line_args>
2010-06-23 21:03:09 -07:00
{
rust_kernel *kernel;
rust_task *task;
2010-06-23 21:03:09 -07:00
int argc;
char **argv;
rust_str **strs;
2010-06-23 21:03:09 -07:00
// [str] passed to rust_task::start.
rust_vec *args;
2010-06-23 21:03:09 -07:00
command_line_args(rust_task *task,
2010-06-23 21:03:09 -07:00
int sys_argc,
char **sys_argv)
: kernel(task->kernel),
task(task),
2010-06-23 21:03:09 -07:00
argc(sys_argc),
argv(sys_argv)
2010-06-23 21:03:09 -07:00
{
#if defined(__WIN32__)
LPCWSTR cmdline = GetCommandLineW();
LPWSTR *wargv = CommandLineToArgvW(cmdline, &argc);
2011-06-28 11:34:20 -07:00
kernel->win32_require("CommandLineToArgvW", wargv != NULL);
argv = (char **) kernel->malloc(sizeof(char*) * argc,
"win32 command line");
2010-06-23 21:03:09 -07:00
for (int i = 0; i < argc; ++i) {
int n_chars = WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1,
NULL, 0, NULL, NULL);
2011-06-28 11:34:20 -07:00
kernel->win32_require("WideCharToMultiByte(0)", n_chars != 0);
argv[i] = (char *) kernel->malloc(n_chars,
"win32 command line arg");
2010-06-23 21:03:09 -07:00
n_chars = WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1,
argv[i], n_chars, NULL, NULL);
2011-06-28 11:34:20 -07:00
kernel->win32_require("WideCharToMultiByte(1)", n_chars != 0);
2010-06-23 21:03:09 -07:00
}
LocalFree(wargv);
#endif
size_t vec_fill = sizeof(rust_str *) * argc;
size_t vec_alloc = next_power_of_two(vec_fill);
void *mem = kernel->malloc(vec_alloc, "command line");
strs = (rust_str**) mem;
2010-06-23 21:03:09 -07:00
for (int i = 0; i < argc; ++i) {
size_t str_fill = strlen(argv[i]) + 1;
size_t str_alloc = next_power_of_two(sizeof(rust_str) + str_fill);
mem = kernel->malloc(str_alloc, "command line arg");
strs[i] = new (mem) rust_str(str_alloc, str_fill,
2010-06-23 21:03:09 -07:00
(uint8_t const *)argv[i]);
strs[i]->ref_count++;
2010-06-23 21:03:09 -07:00
}
args = (rust_vec *)
kernel->malloc(vec_size<rust_str*>(argc),
"command line arg interior");
args->fill = args->alloc = sizeof(rust_str *) * argc;
// NB: _rust_main owns the vec and will be responsible for
// freeing it
memcpy(&args->data[0], strs, args->fill);
2010-06-23 21:03:09 -07:00
}
~command_line_args() {
kernel->free(args);
for (int i = 0; i < argc; ++i)
kernel->free(strs[i]);
kernel->free(strs);
2010-06-23 21:03:09 -07:00
#ifdef __WIN32__
for (int i = 0; i < argc; ++i) {
2011-06-28 11:12:00 -07:00
kernel->free(argv[i]);
2010-06-23 21:03:09 -07:00
}
2011-06-28 11:12:00 -07:00
kernel->free(argv);
2010-06-23 21:03:09 -07:00
#endif
}
};
/**
* Main entry point into the Rust runtime. Here we create a Rust service,
* initialize the kernel, create the root domain and run it.
*/
2010-06-23 21:03:09 -07:00
int check_claims = 0;
2010-06-23 21:03:09 -07:00
extern "C" CDECL int
rust_start(uintptr_t main_fn, int argc, char **argv,
void* crate_map) {
2010-06-23 21:03:09 -07:00
rust_env *env = load_env();
update_log_settings(crate_map, env->logspec);
check_claims = env->check_claims;
rust_srv *srv = new rust_srv(env);
rust_kernel *kernel = new rust_kernel(srv, env->num_sched_threads);
rust_task_id root_id = kernel->create_task(NULL, "main");
rust_task *root_task = kernel->get_task_by_id(root_id);
I(kernel, root_task != NULL);
2011-07-23 14:01:43 -07:00
rust_scheduler *sched = root_task->sched;
command_line_args *args
= new (kernel, "main command line args")
command_line_args(root_task, argc, argv);
2010-06-23 21:03:09 -07:00
DLOG(sched, dom, "startup: %d args in 0x%" PRIxPTR,
args->argc, (uintptr_t)args->args);
for (int i = 0; i < args->argc; i++) {
DLOG(sched, dom, "startup: arg[%d] = '%s'", i, args->argv[i]);
}
2010-06-23 21:03:09 -07:00
root_task->start(main_fn, (uintptr_t)args->args);
root_task->deref();
root_task = NULL;
Make log the log level configurable per module This overloads the meaning of RUST_LOG to also allow 'module.submodule' or 'module.somethingelse=2' forms. The first turn on all logging for a module (loglevel 3), the second sets its loglevel to 2. Log levels are: 0: Show only errors 1: Errors and warnings 2: Errors, warnings, and notes 3: Everything, including debug logging Right now, since we only have one 'log' operation, everything happens at level 1 (warning), so the only meaningful thing that can be done with the new RUST_LOG support is disable logging (=0) for some modules. TODOS: * Language support for logging at a specific level * Also add a log level field to tasks, query the current task as well as the current module before logging (log if one of them allows it) * Revise the C logging API to conform to this set-up (globals for per-module log level, query the task level before logging, stop using a global mask) Implementation notes: Crates now contain two extra data structures. A 'module map' that contains names and pointers to the module-log-level globals for each module in the crate that logs, and a 'crate map' that points at the crate's module map, as well as at the crate maps of all external crates it depends on. These are walked by the runtime (in rust_crate.cpp) to set the currect log levels based on RUST_LOG. These module log globals are allocated as-needed whenever a log expression is encountered, and their location is hard-coded into the logging code, which compares the current level to the log statement's level, and skips over all logging code when it is lower.
2011-04-17 16:29:18 +02:00
int ret = kernel->start_task_threads();
delete args;
delete kernel;
delete srv;
free_env(env);
2010-06-23 21:03:09 -07:00
#if !defined(__WIN32__)
// Don't take down the process if the main thread exits without an
// error.
if (!ret)
pthread_exit(NULL);
#endif
return ret;
}
//
// 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 $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
2010-06-23 21:03:09 -07:00
// End:
//