rust/src/rt/rust.cpp

131 lines
4.1 KiB
C++
Raw Normal View History

2010-06-24 06:03:09 +02:00
#include "rust_internal.h"
struct
command_line_args : public dom_owned<command_line_args>
2010-06-24 06:03:09 +02:00
{
rust_dom *dom;
2010-06-24 06:03:09 +02:00
int argc;
char **argv;
// vec[str] passed to rust_task::start.
rust_vec *args;
command_line_args(rust_dom *dom,
2010-06-24 06:03:09 +02:00
int sys_argc,
char **sys_argv)
: dom(dom),
argc(sys_argc),
argv(sys_argv),
args(NULL)
{
#if defined(__WIN32__)
LPCWSTR cmdline = GetCommandLineW();
LPWSTR *wargv = CommandLineToArgvW(cmdline, &argc);
dom->win32_require("CommandLineToArgvW", wargv != NULL);
argv = (char **) dom->malloc(sizeof(char*) * argc);
2010-06-24 06:03:09 +02:00
for (int i = 0; i < argc; ++i) {
int n_chars = WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1,
NULL, 0, NULL, NULL);
dom->win32_require("WideCharToMultiByte(0)", n_chars != 0);
argv[i] = (char *) dom->malloc(n_chars);
2010-06-24 06:03:09 +02:00
n_chars = WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1,
argv[i], n_chars, NULL, NULL);
dom->win32_require("WideCharToMultiByte(1)", n_chars != 0);
2010-06-24 06:03:09 +02:00
}
LocalFree(wargv);
#endif
size_t vec_fill = sizeof(rust_str *) * argc;
size_t vec_alloc = next_power_of_two(sizeof(rust_vec) + vec_fill);
void *mem = dom->malloc(vec_alloc);
args = new (mem) rust_vec(dom, vec_alloc, 0, NULL);
2010-06-24 06:03:09 +02:00
rust_str **strs = (rust_str**) &args->data[0];
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 = dom->malloc(str_alloc);
strs[i] = new (mem) rust_str(dom, str_alloc, str_fill,
2010-06-24 06:03:09 +02:00
(uint8_t const *)argv[i]);
}
args->fill = vec_fill;
// If the caller has a declared args array, they may drop; but
// we don't know if they have such an array. So we pin the args
// array here to ensure it survives to program-shutdown.
args->ref();
}
~command_line_args() {
if (args) {
// Drop the args we've had pinned here.
rust_str **strs = (rust_str**) &args->data[0];
for (int i = 0; i < argc; ++i)
dom->free(strs[i]);
dom->free(args);
2010-06-24 06:03:09 +02:00
}
#ifdef __WIN32__
for (int i = 0; i < argc; ++i) {
dom->free(argv[i]);
2010-06-24 06:03:09 +02:00
}
dom->free(argv);
2010-06-24 06:03:09 +02: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-24 06:03:09 +02:00
extern "C" CDECL int
rust_start(uintptr_t main_fn, rust_crate const *crate, int argc,
char **argv, void* crate_map) {
2010-06-24 06:03:09 +02:00
2011-05-18 20:01:13 +02:00
update_log_settings(crate_map, getenv("RUST_LOG"));
rust_srv *srv = new rust_srv();
rust_kernel *kernel = new rust_kernel(srv);
kernel->start();
2011-05-25 01:07:30 +02:00
rust_handle<rust_dom> *handle = kernel->create_domain("main");
rust_dom *dom = handle->referent();
command_line_args *args = new (dom) command_line_args(dom, argc, argv);
2010-06-24 06:03:09 +02:00
DLOG(dom, dom, "startup: %d args in 0x%" PRIxPTR,
args->argc, (uintptr_t)args->args);
for (int i = 0; i < args->argc; i++) {
DLOG(dom, dom, "startup: arg[%d] = '%s'", i, args->argv[i]);
}
2010-06-24 06:03:09 +02:00
if (log_rt_dwarf) {
rust_crate_reader create_reader(dom, crate);
2010-06-24 06:03:09 +02:00
}
uintptr_t main_args[4] = {0, 0, 0, (uintptr_t)args->args};
dom->root_task->start(main_fn,
(uintptr_t)&main_args, sizeof(main_args));
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 = dom->start_main_loop();
delete args;
kernel->destroy_domain(dom);
kernel->join_all_domains();
delete kernel;
delete srv;
2010-06-24 06:03:09 +02: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 .. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
// End:
//