Skip to content

Commit 54affdb

Browse files
committed
simplify initialization / startup / embedded / task-switch api
1 parent 5069121 commit 54affdb

14 files changed

+221
-197
lines changed

base/boot.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ export
147147
# method reflection
148148
applicable, invoke, method_exists,
149149
# constants
150-
JULIA_HOME, nothing, Main,
150+
nothing, Main,
151151
# intrinsics module
152152
Intrinsics
153153
#ccall, cglobal, llvmcall, abs_float, add_float, add_int, and_int, ashr_int,

base/client.jl

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ function load_machine_file(path::AbstractString)
356356
end
357357

358358
function early_init()
359+
global const JULIA_HOME = ccall(:jl_get_julia_home, Any, ())
359360
Sys.init_sysinfo()
360361
if CPU_CORES > 8 && !("OPENBLAS_NUM_THREADS" in keys(ENV)) && !("OMP_NUM_THREADS" in keys(ENV))
361362
# Prevent openblas from stating to many threads, unless/until specifically requested
@@ -409,19 +410,16 @@ function _start()
409410
# note: currently IOStream is used for file STDIN
410411
if isa(STDIN,File) || isa(STDIN,IOStream)
411412
# reading from a file, behave like include
412-
eval(parse_input_line(readall(STDIN)))
413+
eval(Main,parse_input_line(readall(STDIN)))
413414
else
414415
# otherwise behave repl-like
415416
while !eof(STDIN)
416417
eval_user_input(parse_input_line(STDIN), true)
417418
end
418419
end
419-
if have_color
420-
print(color_normal)
421-
end
422-
quit()
420+
else
421+
REPL.run_repl(active_repl)
423422
end
424-
REPL.run_repl(active_repl)
425423
end
426424
catch err
427425
display_error(err,catch_backtrace())
@@ -434,7 +432,6 @@ function _start()
434432
end
435433
println()
436434
end
437-
ccall(:uv_atexit_hook, Void, ())
438435
end
439436

440437
const atexit_hooks = []

base/exports.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ export
163163
Inf,
164164
Inf16,
165165
Inf32,
166+
JULIA_HOME,
166167
LOAD_PATH,
167168
MS_ASYNC,
168169
MS_INVALIDATE,

src/ast.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ static builtinspec_t julia_flisp_ast_ext[] = {
114114
{ NULL, NULL }
115115
};
116116

117-
DLLEXPORT void jl_init_frontend(void)
117+
void jl_init_frontend(void)
118118
{
119119
fl_init(4*1024*1024);
120120
value_t img = cvalue(iostreamtype, sizeof(ios_t));

src/codegen.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,8 @@ void __attribute__(()) __stack_chk_fail()
130130
#endif
131131
{
132132
/* put your panic function or similar in here */
133-
fprintf(stderr, "warning: stack corruption detected\n");
134-
//assert(0 && "stack corruption detected");
135-
//abort();
133+
fprintf(stderr, "fatal error: stack corruption detected\n");
134+
abort(); // end with abort, since the compiler destroyed the stack upon entry to this function
136135
}
137136
}
138137

src/dump.c

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -452,21 +452,14 @@ static void jl_serialize_module(ios_t *s, jl_module_t *m)
452452
jl_serialize_value(s, m->parent);
453453
if (ref_only)
454454
return;
455-
// set on every startup; don't save value
456-
jl_sym_t *jhsym = jl_symbol("JULIA_HOME");
457455
size_t i;
458456
void **table = m->bindings.table;
459457
for(i=1; i < m->bindings.size; i+=2) {
460458
if (table[i] != HT_NOTFOUND) {
461459
jl_binding_t *b = (jl_binding_t*)table[i];
462460
if (b->owner == m || m != jl_main_module) {
463461
jl_serialize_value(s, b->name);
464-
if (table[i-1] == jhsym && m == jl_core_module) {
465-
jl_serialize_value(s, NULL);
466-
}
467-
else {
468-
jl_serialize_value(s, b->value);
469-
}
462+
jl_serialize_value(s, b->value);
470463
jl_serialize_value(s, b->type);
471464
jl_serialize_value(s, b->owner);
472465
write_int8(s, (b->constp<<2) | (b->exportp<<1) | (b->imported));
@@ -1435,9 +1428,6 @@ void jl_restore_system_image(const char *fname)
14351428
#ifdef JL_GC_MARKSWEEP
14361429
if (en) jl_gc_enable();
14371430
#endif
1438-
// restore the value of our "magic" JULIA_HOME variable/constant
1439-
jl_get_binding_wr(jl_core_module, jl_symbol("JULIA_HOME"))->value =
1440-
jl_cstr_to_string(jl_compileropts.julia_home);
14411431
mode = last_mode;
14421432
jl_update_all_fptrs();
14431433
}

src/gc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ static void gc_mark_task(jl_task_t *ta, int d)
712712
gc_mark_stack(jl_pgcstack, offset, d);
713713
}
714714
else {
715-
offset = (char *)ta->stkbuf - ((char *)ta->stackbase - ta->ssize);
715+
offset = (char *)ta->stkbuf - ((char *)jl_stackbase - ta->ssize);
716716
gc_mark_stack(ta->gcstack, offset, d);
717717
}
718718
#else

src/init.c

Lines changed: 27 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include <stdlib.h>
88
#include <string.h>
99
#include <stdio.h>
10-
#include <setjmp.h>
1110
#include <assert.h>
1211

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

9089
jl_compileropts_t jl_compileropts = { NULL, // julia_home
90+
NULL, // julia_bin
9191
NULL, // build_path
9292
system_image_path, // image_file
9393
0, // code_coverage
@@ -436,7 +436,7 @@ static void jl_uv_exitcleanup_walk(uv_handle_t *handle, void *arg)
436436
void jl_write_coverage_data(void);
437437
void jl_write_malloc_log(void);
438438

439-
DLLEXPORT void uv_atexit_hook()
439+
DLLEXPORT void jl_atexit_hook()
440440
{
441441
#if defined(JL_GC_MARKSWEEP) && defined(GC_FINAL_STATS)
442442
jl_print_gc_stats(JL_STDERR);
@@ -784,57 +784,53 @@ static void jl_resolve_sysimg_location()
784784
{ // note: if you care about lost memory, you should compare the
785785
// pointers in jl_compileropts before and after calling julia_init()
786786
// and call the appropriate free function on the originals for any that changed
787-
char *free_path = NULL;
787+
char *free_path = (char*)malloc(PATH_MAX);
788+
size_t path_size = PATH_MAX;
789+
if (uv_exepath(free_path, &path_size)) {
790+
ios_printf(ios_stderr, "fatal error: unexpected error while retrieving exepath\n");
791+
exit(1);
792+
}
793+
if (path_size >= PATH_MAX) {
794+
ios_printf(ios_stderr, "fatal error: jl_compileropts.julia_bin path too long\n");
795+
exit(1);
796+
}
797+
jl_compileropts.julia_bin = strdup(free_path);
788798
if (!jl_compileropts.julia_home) {
789799
jl_compileropts.julia_home = getenv("JULIA_HOME");
790800
if (!jl_compileropts.julia_home) {
791-
size_t path_size = PATH_MAX;
792-
char *path = (char*)malloc(PATH_MAX);
793-
if (uv_exepath(path, &path_size)) {
794-
ios_printf(ios_stderr, "fatal error: unexpected error while retrieving exepath\n");
795-
exit(1);
796-
}
797-
if (path_size >= PATH_MAX) {
798-
ios_printf(ios_stderr, "fatal error: jl_compileropts.image_file path too long\n");
799-
exit(1);
800-
}
801-
free_path = path;
802-
jl_compileropts.julia_home = dirname(path);
801+
jl_compileropts.julia_home = dirname(free_path);
803802
}
804803
}
805804
if (jl_compileropts.julia_home)
806805
jl_compileropts.julia_home = abspath(jl_compileropts.julia_home);
807-
if (free_path) {
808-
free(free_path);
809-
free_path = NULL;
810-
}
806+
free(free_path);
807+
free_path = NULL;
811808
if (jl_compileropts.image_file) {
812809
if (jl_compileropts.image_file[0] != PATHSEPSTRING[0]) {
813810
if (jl_compileropts.image_file == system_image_path) {
814811
// build time path, relative to JULIA_HOME
815-
char *path = (char*)malloc(PATH_MAX);
816-
int n = snprintf(path, PATH_MAX, "%s" PATHSEPSTRING "%s",
812+
free_path = (char*)malloc(PATH_MAX);
813+
int n = snprintf(free_path, PATH_MAX, "%s" PATHSEPSTRING "%s",
817814
jl_compileropts.julia_home, jl_compileropts.image_file);
818815
if (n >= PATH_MAX || n < 0) {
819816
ios_printf(ios_stderr, "fatal error: jl_compileropts.image_file path too long\n");
820817
exit(1);
821818
}
822-
free_path = path;
823-
jl_compileropts.image_file = path;
819+
jl_compileropts.image_file = free_path;
824820
}
825821
}
826-
}
827-
if (jl_compileropts.image_file)
828-
jl_compileropts.image_file = abspath(jl_compileropts.image_file);
829-
if (free_path) {
830-
free(free_path);
831-
free_path = NULL;
822+
if (jl_compileropts.image_file)
823+
jl_compileropts.image_file = abspath(jl_compileropts.image_file);
824+
if (free_path) {
825+
free(free_path);
826+
free_path = NULL;
827+
}
832828
}
833829
if (jl_compileropts.build_path)
834830
jl_compileropts.build_path = abspath(jl_compileropts.build_path);
835831
}
836832

837-
void julia_init()
833+
void _julia_init()
838834
{
839835
jl_io_loop = uv_default_loop(); // this loop will internal events (spawning process etc.),
840836
// best to call this first, since it also initializes libuv
@@ -926,11 +922,6 @@ void julia_init()
926922
jl_get_builtin_hooks();
927923
jl_boot_file_loaded = 1;
928924
jl_init_box_caches();
929-
// Core.JULIA_HOME is a "magic" constant, we set it at runtime here
930-
// since its value gets excluded from the system image
931-
jl_set_const(jl_core_module, jl_symbol("JULIA_HOME"),
932-
jl_cstr_to_string(jl_compileropts.julia_home));
933-
jl_module_export(jl_core_module, jl_symbol("JULIA_HOME"));
934925
}
935926

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

11061097
void jl_compile_all(void);
11071098

1108-
DLLEXPORT int julia_trampoline(int argc, char **argv, int (*pmain)(int ac,char *av[]))
1099+
DLLEXPORT int julia_save()
11091100
{
1110-
unsigned char *p = (unsigned char *)&__stack_chk_guard;
1111-
char a = p[sizeof(__stack_chk_guard)-1];
1112-
char b = p[sizeof(__stack_chk_guard)-2];
1113-
char c = p[0];
1114-
/* If you have the ability to generate random numbers in your kernel then they should be used here */
1115-
p[sizeof(__stack_chk_guard)-1] = 255;
1116-
p[sizeof(__stack_chk_guard)-2] = '\n';
1117-
p[0] = 0;
1118-
JL_SET_STACK_BASE;
1119-
int ret = pmain(argc, argv);
11201101
const char *build_path = jl_compileropts.build_path;
11211102
if (build_path) {
11221103
if (jl_compileropts.compile_enabled == JL_COMPILEROPT_COMPILE_ALL)
@@ -1148,10 +1129,6 @@ DLLEXPORT int julia_trampoline(int argc, char **argv, int (*pmain)(int ac,char *
11481129
ios_printf(ios_stderr,"\nFATAL: failed to create string for .ji build path\n");
11491130
}
11501131
}
1151-
p[sizeof(__stack_chk_guard)-1] = a;
1152-
p[sizeof(__stack_chk_guard)-2] = b;
1153-
p[0] = c;
1154-
return ret;
11551132
}
11561133

11571134
jl_function_t *jl_typeinf_func=NULL;

src/jl_uv.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -686,14 +686,10 @@ char *jl_bufptr(ios_t *s)
686686
return s->buf;
687687
}
688688

689-
DLLEXPORT void uv_atexit_hook();
690689
DLLEXPORT void jl_exit(int exitcode)
691690
{
692-
/*if (jl_io_loop) {
693-
jl_process_events(&jl_io_loop);
694-
}*/
695691
uv_tty_reset_mode();
696-
uv_atexit_hook();
692+
jl_atexit_hook();
697693
exit(exitcode);
698694
}
699695

src/jlapi.c

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,6 @@ DLLEXPORT void jl_init(char *julia_home_dir)
5656

5757
DLLEXPORT void *jl_eval_string(char *str)
5858
{
59-
#ifdef COPY_STACKS
60-
int outside_task = (jl_root_task->stackbase == NULL);
61-
if (outside_task) {
62-
JL_SET_STACK_BASE;
63-
}
64-
#endif
6559
jl_value_t *r;
6660
JL_TRY {
6761
jl_value_t *ast = jl_parse_input_line(str);
@@ -74,11 +68,6 @@ DLLEXPORT void *jl_eval_string(char *str)
7468
//jl_show(jl_stderr_obj(), jl_exception_in_transit);
7569
r = NULL;
7670
}
77-
#ifdef COPY_STACKS
78-
if (outside_task) {
79-
jl_root_task->stackbase = NULL;
80-
}
81-
#endif
8271
return r;
8372
}
8473

@@ -255,6 +244,21 @@ DLLEXPORT int jl_is_debugbuild(void)
255244
#endif
256245
}
257246

247+
DLLEXPORT jl_value_t *jl_get_julia_home(void)
248+
{
249+
return jl_cstr_to_string(jl_compileropts.julia_home);
250+
}
251+
252+
DLLEXPORT jl_value_t *jl_get_julia_bin(void)
253+
{
254+
return jl_cstr_to_string(jl_compileropts.julia_bin);
255+
}
256+
257+
DLLEXPORT jl_value_t *jl_get_image_file(void)
258+
{
259+
return jl_cstr_to_string(jl_compileropts.image_file);
260+
}
261+
258262
#ifdef __cplusplus
259263
}
260264
#endif

0 commit comments

Comments
 (0)