2012-06-03 01:14:25 -05:00
|
|
|
/**
|
|
|
|
* Main entry point into the Rust runtime. Here we initialize the kernel,
|
|
|
|
* create the initial scheduler and run the main task.
|
|
|
|
*/
|
2012-04-02 22:18:01 -05:00
|
|
|
|
|
|
|
#include "rust_globals.h"
|
|
|
|
#include "rust_kernel.h"
|
2012-01-06 14:06:35 -06:00
|
|
|
#include "rust_util.h"
|
2012-02-03 17:45:59 -06:00
|
|
|
#include "rust_scheduler.h"
|
2010-06-23 23:03:09 -05:00
|
|
|
|
2012-06-03 01:14:25 -05:00
|
|
|
// Creates a rust argument vector from the platform argument vector
|
2010-06-23 23:03:09 -05:00
|
|
|
struct
|
2011-06-27 21:15:03 -05:00
|
|
|
command_line_args : public kernel_owned<command_line_args>
|
2010-06-23 23:03:09 -05:00
|
|
|
{
|
2011-06-27 21:15:03 -05:00
|
|
|
rust_kernel *kernel;
|
|
|
|
rust_task *task;
|
2010-06-23 23:03:09 -05:00
|
|
|
int argc;
|
|
|
|
char **argv;
|
|
|
|
|
2011-08-17 23:48:35 -05:00
|
|
|
// [str] passed to rust_task::start.
|
2012-05-21 20:36:52 -05:00
|
|
|
rust_vec_box *args;
|
2010-06-23 23:03:09 -05:00
|
|
|
|
2011-06-27 21:15:03 -05:00
|
|
|
command_line_args(rust_task *task,
|
2010-06-23 23:03:09 -05:00
|
|
|
int sys_argc,
|
2011-08-17 22:31:55 -05:00
|
|
|
char **sys_argv)
|
2011-06-27 21:15:03 -05:00
|
|
|
: kernel(task->kernel),
|
|
|
|
task(task),
|
2010-06-23 23:03:09 -05:00
|
|
|
argc(sys_argc),
|
2011-08-17 23:48:35 -05:00
|
|
|
argv(sys_argv)
|
2010-06-23 23:03:09 -05:00
|
|
|
{
|
|
|
|
#if defined(__WIN32__)
|
|
|
|
LPCWSTR cmdline = GetCommandLineW();
|
|
|
|
LPWSTR *wargv = CommandLineToArgvW(cmdline, &argc);
|
2011-06-28 13:34:20 -05:00
|
|
|
kernel->win32_require("CommandLineToArgvW", wargv != NULL);
|
2011-07-18 14:02:26 -05:00
|
|
|
argv = (char **) kernel->malloc(sizeof(char*) * argc,
|
|
|
|
"win32 command line");
|
2010-06-23 23:03:09 -05: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 13:34:20 -05:00
|
|
|
kernel->win32_require("WideCharToMultiByte(0)", n_chars != 0);
|
2011-07-18 14:02:26 -05:00
|
|
|
argv[i] = (char *) kernel->malloc(n_chars,
|
|
|
|
"win32 command line arg");
|
2010-06-23 23:03:09 -05:00
|
|
|
n_chars = WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1,
|
|
|
|
argv[i], n_chars, NULL, NULL);
|
2011-06-28 13:34:20 -05:00
|
|
|
kernel->win32_require("WideCharToMultiByte(1)", n_chars != 0);
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
LocalFree(wargv);
|
|
|
|
#endif
|
2011-08-30 20:33:22 -05:00
|
|
|
|
2012-02-07 20:55:02 -06:00
|
|
|
args = make_str_vec(kernel, argc, argv);
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
~command_line_args() {
|
2011-08-30 20:33:22 -05:00
|
|
|
for (int i = 0; i < argc; ++i) {
|
2012-05-21 20:36:52 -05:00
|
|
|
rust_vec *s = ((rust_vec**)&args->body.data)[i];
|
2011-08-30 20:33:22 -05:00
|
|
|
kernel->free(s);
|
|
|
|
}
|
2011-09-02 13:28:19 -05:00
|
|
|
kernel->free(args);
|
2011-08-30 20:33:22 -05:00
|
|
|
|
2010-06-23 23:03:09 -05:00
|
|
|
#ifdef __WIN32__
|
|
|
|
for (int i = 0; i < argc; ++i) {
|
2011-06-28 13:12:00 -05:00
|
|
|
kernel->free(argv[i]);
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
2011-06-28 13:12:00 -05:00
|
|
|
kernel->free(argv);
|
2010-06-23 23:03:09 -05:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-06-03 01:14:25 -05:00
|
|
|
// A global that indicates whether Rust typestate claim statements should be
|
2012-06-21 18:44:10 -05:00
|
|
|
// executed Generated code will read this variable directly (I think).
|
|
|
|
// FIXME (#2670): This belongs somewhere else
|
2011-06-29 13:24:57 -05:00
|
|
|
int check_claims = 0;
|
|
|
|
|
2012-06-03 01:14:25 -05:00
|
|
|
/**
|
|
|
|
The runtime entrypoint. The (C ABI) main function generated by rustc calls
|
|
|
|
`rust_start`, providing the address of the Rust ABI main function, the
|
|
|
|
platform argument vector, and a `crate_map` the provides some logging
|
|
|
|
metadata.
|
|
|
|
*/
|
2010-06-23 23:03:09 -05:00
|
|
|
extern "C" CDECL int
|
2011-10-20 06:48:10 -05:00
|
|
|
rust_start(uintptr_t main_fn, int argc, char **argv, void* crate_map) {
|
2011-11-14 15:52:35 -06:00
|
|
|
|
2012-06-03 01:14:25 -05:00
|
|
|
// Load runtime configuration options from the environment.
|
|
|
|
// FIXME #1497: Should provide a way to get these from the command
|
|
|
|
// line as well.
|
2011-07-27 16:34:39 -05:00
|
|
|
rust_env *env = load_env();
|
|
|
|
|
|
|
|
update_log_settings(crate_map, env->logspec);
|
2012-06-03 01:14:25 -05:00
|
|
|
|
|
|
|
// Maybe turn on typestate claim checking
|
2011-07-27 16:34:39 -05:00
|
|
|
check_claims = env->check_claims;
|
2011-06-29 13:24:57 -05:00
|
|
|
|
2012-04-01 22:18:40 -05:00
|
|
|
rust_kernel *kernel = new rust_kernel(env);
|
2012-06-03 01:14:25 -05:00
|
|
|
|
|
|
|
// Create the main scheduler and the main task
|
2012-02-06 23:06:12 -06:00
|
|
|
rust_sched_id sched_id = kernel->create_scheduler(env->num_sched_threads);
|
|
|
|
rust_scheduler *sched = kernel->get_scheduler_by_id(sched_id);
|
2012-06-28 18:01:55 -05:00
|
|
|
assert(sched != NULL);
|
2012-03-21 16:47:48 -05:00
|
|
|
rust_task *root_task = sched->create_task(NULL, "main");
|
2012-06-03 01:14:25 -05:00
|
|
|
|
|
|
|
// Build the command line arguments to pass to the root task
|
2011-07-13 17:44:09 -05:00
|
|
|
command_line_args *args
|
2011-07-18 14:02:26 -05:00
|
|
|
= new (kernel, "main command line args")
|
2011-08-17 22:31:55 -05:00
|
|
|
command_line_args(root_task, argc, argv);
|
2010-06-23 23:03:09 -05:00
|
|
|
|
2012-03-29 18:31:30 -05:00
|
|
|
LOG(root_task, dom, "startup: %d args in 0x%" PRIxPTR,
|
2012-06-03 01:14:25 -05:00
|
|
|
args->argc, (uintptr_t)args->args);
|
2010-09-07 20:39:07 -05:00
|
|
|
for (int i = 0; i < args->argc; i++) {
|
2012-03-29 18:31:30 -05:00
|
|
|
LOG(root_task, dom, "startup: arg[%d] = '%s'", i, args->argv[i]);
|
2010-09-07 20:39:07 -05:00
|
|
|
}
|
2010-06-23 23:03:09 -05:00
|
|
|
|
2012-06-03 01:14:25 -05:00
|
|
|
// Schedule the main Rust task
|
2012-01-04 22:11:39 -06:00
|
|
|
root_task->start((spawn_fn)main_fn, NULL, args->args);
|
2012-06-03 01:14:25 -05:00
|
|
|
|
|
|
|
// At this point the task lifecycle is responsible for it
|
|
|
|
// and our pointer may not be valid
|
2011-08-13 20:15:56 -05:00
|
|
|
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 09:29:18 -05:00
|
|
|
|
2012-06-03 01:14:25 -05:00
|
|
|
// Run the kernel until all schedulers exit
|
2012-04-01 20:42:28 -05:00
|
|
|
int ret = kernel->run();
|
2012-06-03 01:14:25 -05:00
|
|
|
|
2010-09-07 20:39:07 -05:00
|
|
|
delete args;
|
|
|
|
delete kernel;
|
2011-07-27 16:34:39 -05:00
|
|
|
free_env(env);
|
|
|
|
|
2010-06-23 23:03:09 -05:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Local Variables:
|
|
|
|
// mode: C++
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|
|
|
|
//
|