Skip to content

Commit

Permalink
simplify initialization / startup / embedded / task-switch api
Browse files Browse the repository at this point in the history
  • Loading branch information
vtjnash committed Dec 13, 2014
1 parent 5069121 commit 54affdb
Show file tree
Hide file tree
Showing 14 changed files with 221 additions and 197 deletions.
2 changes: 1 addition & 1 deletion base/boot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export
# method reflection
applicable, invoke, method_exists,
# constants
JULIA_HOME, nothing, Main,
nothing, Main,
# intrinsics module
Intrinsics
#ccall, cglobal, llvmcall, abs_float, add_float, add_int, and_int, ashr_int,
Expand Down
11 changes: 4 additions & 7 deletions base/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ function load_machine_file(path::AbstractString)
end

function early_init()
global const JULIA_HOME = ccall(:jl_get_julia_home, Any, ())
Sys.init_sysinfo()
if CPU_CORES > 8 && !("OPENBLAS_NUM_THREADS" in keys(ENV)) && !("OMP_NUM_THREADS" in keys(ENV))
# Prevent openblas from stating to many threads, unless/until specifically requested
Expand Down Expand Up @@ -409,19 +410,16 @@ function _start()
# note: currently IOStream is used for file STDIN
if isa(STDIN,File) || isa(STDIN,IOStream)
# reading from a file, behave like include
eval(parse_input_line(readall(STDIN)))
eval(Main,parse_input_line(readall(STDIN)))
else
# otherwise behave repl-like
while !eof(STDIN)
eval_user_input(parse_input_line(STDIN), true)
end
end
if have_color
print(color_normal)
end
quit()
else
REPL.run_repl(active_repl)
end
REPL.run_repl(active_repl)
end
catch err
display_error(err,catch_backtrace())
Expand All @@ -434,7 +432,6 @@ function _start()
end
println()
end
ccall(:uv_atexit_hook, Void, ())
end

const atexit_hooks = []
Expand Down
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ export
Inf,
Inf16,
Inf32,
JULIA_HOME,
LOAD_PATH,
MS_ASYNC,
MS_INVALIDATE,
Expand Down
2 changes: 1 addition & 1 deletion src/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ static builtinspec_t julia_flisp_ast_ext[] = {
{ NULL, NULL }
};

DLLEXPORT void jl_init_frontend(void)
void jl_init_frontend(void)
{
fl_init(4*1024*1024);
value_t img = cvalue(iostreamtype, sizeof(ios_t));
Expand Down
5 changes: 2 additions & 3 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,8 @@ void __attribute__(()) __stack_chk_fail()
#endif
{
/* put your panic function or similar in here */
fprintf(stderr, "warning: stack corruption detected\n");
//assert(0 && "stack corruption detected");
//abort();
fprintf(stderr, "fatal error: stack corruption detected\n");
abort(); // end with abort, since the compiler destroyed the stack upon entry to this function
}
}

Expand Down
12 changes: 1 addition & 11 deletions src/dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -452,21 +452,14 @@ static void jl_serialize_module(ios_t *s, jl_module_t *m)
jl_serialize_value(s, m->parent);
if (ref_only)
return;
// set on every startup; don't save value
jl_sym_t *jhsym = jl_symbol("JULIA_HOME");
size_t i;
void **table = m->bindings.table;
for(i=1; i < m->bindings.size; i+=2) {
if (table[i] != HT_NOTFOUND) {
jl_binding_t *b = (jl_binding_t*)table[i];
if (b->owner == m || m != jl_main_module) {
jl_serialize_value(s, b->name);
if (table[i-1] == jhsym && m == jl_core_module) {
jl_serialize_value(s, NULL);
}
else {
jl_serialize_value(s, b->value);
}
jl_serialize_value(s, b->value);
jl_serialize_value(s, b->type);
jl_serialize_value(s, b->owner);
write_int8(s, (b->constp<<2) | (b->exportp<<1) | (b->imported));
Expand Down Expand Up @@ -1435,9 +1428,6 @@ void jl_restore_system_image(const char *fname)
#ifdef JL_GC_MARKSWEEP
if (en) jl_gc_enable();
#endif
// restore the value of our "magic" JULIA_HOME variable/constant
jl_get_binding_wr(jl_core_module, jl_symbol("JULIA_HOME"))->value =
jl_cstr_to_string(jl_compileropts.julia_home);
mode = last_mode;
jl_update_all_fptrs();
}
Expand Down
2 changes: 1 addition & 1 deletion src/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ static void gc_mark_task(jl_task_t *ta, int d)
gc_mark_stack(jl_pgcstack, offset, d);
}
else {
offset = (char *)ta->stkbuf - ((char *)ta->stackbase - ta->ssize);
offset = (char *)ta->stkbuf - ((char *)jl_stackbase - ta->ssize);
gc_mark_stack(ta->gcstack, offset, d);
}
#else
Expand Down
77 changes: 27 additions & 50 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <setjmp.h>
#include <assert.h>

#if defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__)
Expand Down Expand Up @@ -88,6 +87,7 @@ DLLEXPORT void gdblookup(ptrint_t ip);
static const char system_image_path[256] = JL_SYSTEM_IMAGE_PATH;

jl_compileropts_t jl_compileropts = { NULL, // julia_home
NULL, // julia_bin
NULL, // build_path
system_image_path, // image_file
0, // code_coverage
Expand Down Expand Up @@ -436,7 +436,7 @@ static void jl_uv_exitcleanup_walk(uv_handle_t *handle, void *arg)
void jl_write_coverage_data(void);
void jl_write_malloc_log(void);

DLLEXPORT void uv_atexit_hook()
DLLEXPORT void jl_atexit_hook()
{
#if defined(JL_GC_MARKSWEEP) && defined(GC_FINAL_STATS)
jl_print_gc_stats(JL_STDERR);
Expand Down Expand Up @@ -784,57 +784,53 @@ static void jl_resolve_sysimg_location()
{ // note: if you care about lost memory, you should compare the
// pointers in jl_compileropts before and after calling julia_init()
// and call the appropriate free function on the originals for any that changed
char *free_path = NULL;
char *free_path = (char*)malloc(PATH_MAX);
size_t path_size = PATH_MAX;
if (uv_exepath(free_path, &path_size)) {
ios_printf(ios_stderr, "fatal error: unexpected error while retrieving exepath\n");
exit(1);
}
if (path_size >= PATH_MAX) {
ios_printf(ios_stderr, "fatal error: jl_compileropts.julia_bin path too long\n");
exit(1);
}
jl_compileropts.julia_bin = strdup(free_path);
if (!jl_compileropts.julia_home) {
jl_compileropts.julia_home = getenv("JULIA_HOME");
if (!jl_compileropts.julia_home) {
size_t path_size = PATH_MAX;
char *path = (char*)malloc(PATH_MAX);
if (uv_exepath(path, &path_size)) {
ios_printf(ios_stderr, "fatal error: unexpected error while retrieving exepath\n");
exit(1);
}
if (path_size >= PATH_MAX) {
ios_printf(ios_stderr, "fatal error: jl_compileropts.image_file path too long\n");
exit(1);
}
free_path = path;
jl_compileropts.julia_home = dirname(path);
jl_compileropts.julia_home = dirname(free_path);
}
}
if (jl_compileropts.julia_home)
jl_compileropts.julia_home = abspath(jl_compileropts.julia_home);
if (free_path) {
free(free_path);
free_path = NULL;
}
free(free_path);
free_path = NULL;
if (jl_compileropts.image_file) {
if (jl_compileropts.image_file[0] != PATHSEPSTRING[0]) {
if (jl_compileropts.image_file == system_image_path) {
// build time path, relative to JULIA_HOME
char *path = (char*)malloc(PATH_MAX);
int n = snprintf(path, PATH_MAX, "%s" PATHSEPSTRING "%s",
free_path = (char*)malloc(PATH_MAX);
int n = snprintf(free_path, PATH_MAX, "%s" PATHSEPSTRING "%s",
jl_compileropts.julia_home, jl_compileropts.image_file);
if (n >= PATH_MAX || n < 0) {
ios_printf(ios_stderr, "fatal error: jl_compileropts.image_file path too long\n");
exit(1);
}
free_path = path;
jl_compileropts.image_file = path;
jl_compileropts.image_file = free_path;
}
}
}
if (jl_compileropts.image_file)
jl_compileropts.image_file = abspath(jl_compileropts.image_file);
if (free_path) {
free(free_path);
free_path = NULL;
if (jl_compileropts.image_file)
jl_compileropts.image_file = abspath(jl_compileropts.image_file);
if (free_path) {
free(free_path);
free_path = NULL;
}
}
if (jl_compileropts.build_path)
jl_compileropts.build_path = abspath(jl_compileropts.build_path);
}

void julia_init()
void _julia_init()
{
jl_io_loop = uv_default_loop(); // this loop will internal events (spawning process etc.),
// best to call this first, since it also initializes libuv
Expand Down Expand Up @@ -926,11 +922,6 @@ void julia_init()
jl_get_builtin_hooks();
jl_boot_file_loaded = 1;
jl_init_box_caches();
// Core.JULIA_HOME is a "magic" constant, we set it at runtime here
// since its value gets excluded from the system image
jl_set_const(jl_core_module, jl_symbol("JULIA_HOME"),
jl_cstr_to_string(jl_compileropts.julia_home));
jl_module_export(jl_core_module, jl_symbol("JULIA_HOME"));
}

if (jl_compileropts.image_file) {
Expand Down Expand Up @@ -1105,18 +1096,8 @@ extern void *__stack_chk_guard;

void jl_compile_all(void);

DLLEXPORT int julia_trampoline(int argc, char **argv, int (*pmain)(int ac,char *av[]))
DLLEXPORT int julia_save()
{
unsigned char *p = (unsigned char *)&__stack_chk_guard;
char a = p[sizeof(__stack_chk_guard)-1];
char b = p[sizeof(__stack_chk_guard)-2];
char c = p[0];
/* If you have the ability to generate random numbers in your kernel then they should be used here */
p[sizeof(__stack_chk_guard)-1] = 255;
p[sizeof(__stack_chk_guard)-2] = '\n';
p[0] = 0;
JL_SET_STACK_BASE;
int ret = pmain(argc, argv);
const char *build_path = jl_compileropts.build_path;
if (build_path) {
if (jl_compileropts.compile_enabled == JL_COMPILEROPT_COMPILE_ALL)
Expand Down Expand Up @@ -1148,10 +1129,6 @@ DLLEXPORT int julia_trampoline(int argc, char **argv, int (*pmain)(int ac,char *
ios_printf(ios_stderr,"\nFATAL: failed to create string for .ji build path\n");
}
}
p[sizeof(__stack_chk_guard)-1] = a;
p[sizeof(__stack_chk_guard)-2] = b;
p[0] = c;
return ret;
}

jl_function_t *jl_typeinf_func=NULL;
Expand Down
6 changes: 1 addition & 5 deletions src/jl_uv.c
Original file line number Diff line number Diff line change
Expand Up @@ -686,14 +686,10 @@ char *jl_bufptr(ios_t *s)
return s->buf;
}

DLLEXPORT void uv_atexit_hook();
DLLEXPORT void jl_exit(int exitcode)
{
/*if (jl_io_loop) {
jl_process_events(&jl_io_loop);
}*/
uv_tty_reset_mode();
uv_atexit_hook();
jl_atexit_hook();
exit(exitcode);
}

Expand Down
26 changes: 15 additions & 11 deletions src/jlapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,6 @@ DLLEXPORT void jl_init(char *julia_home_dir)

DLLEXPORT void *jl_eval_string(char *str)
{
#ifdef COPY_STACKS
int outside_task = (jl_root_task->stackbase == NULL);
if (outside_task) {
JL_SET_STACK_BASE;
}
#endif
jl_value_t *r;
JL_TRY {
jl_value_t *ast = jl_parse_input_line(str);
Expand All @@ -74,11 +68,6 @@ DLLEXPORT void *jl_eval_string(char *str)
//jl_show(jl_stderr_obj(), jl_exception_in_transit);
r = NULL;
}
#ifdef COPY_STACKS
if (outside_task) {
jl_root_task->stackbase = NULL;
}
#endif
return r;
}

Expand Down Expand Up @@ -255,6 +244,21 @@ DLLEXPORT int jl_is_debugbuild(void)
#endif
}

DLLEXPORT jl_value_t *jl_get_julia_home(void)
{
return jl_cstr_to_string(jl_compileropts.julia_home);
}

DLLEXPORT jl_value_t *jl_get_julia_bin(void)
{
return jl_cstr_to_string(jl_compileropts.julia_bin);
}

DLLEXPORT jl_value_t *jl_get_image_file(void)
{
return jl_cstr_to_string(jl_compileropts.image_file);
}

#ifdef __cplusplus
}
#endif
Loading

0 comments on commit 54affdb

Please sign in to comment.